Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teach ocamlbuild to create shared libraries using -output-obj #6733

Closed
vicuna opened this issue Dec 24, 2014 · 6 comments
Closed

Teach ocamlbuild to create shared libraries using -output-obj #6733

vicuna opened this issue Dec 24, 2014 · 6 comments

Comments

@vicuna
Copy link

vicuna commented Dec 24, 2014

Original bug ID: 6733
Reporter: @whitequark
Assigned to: @gasche
Status: closed (set by @xavierleroy on 2017-02-16T14:15:07Z)
Resolution: fixed
Priority: normal
Severity: feature
Fixed in version: 4.02.2+dev / +rc1
Category: -for ocamlbuild use https://github.com/ocaml/ocamlbuild/issues
Tags: patch
Related to: #6693 #6797 #6845 #6918 #6927
Monitored by: @hcarty

Bug description

Currently I am using the following code (parts of which are pulled into myocamlbuild, not being accessible from ocamlbuild library):

  let native_link_gen linker =
    Ocaml_compiler.link_gen "cmx" "cmxa"
      !Options.ext_lib [!Options.ext_obj; "cmi"] linker
  in
  let native_output_obj x = native_link_gen Ocaml_compiler.ocamlopt_link_prog
    (fun tags -> tags++"ocaml"++"link"++"native"++"output_obj"++"output_so") x
  in
  flag ["ocaml"; "link"; "output_so"] & S[A"-cclib"; A"-shared"];
  flag ["ocaml"; "link"; "output_obj"; "debug"] & A"-g";
  rule "ocaml: cmx & o -> native.(so|dll|dylib)"
    ~prod:("%.native"-.-(!Options.ext_dll))
    ~deps:["%.cmx"; "%.o"]
    (native_output_obj "%.cmx" ("%.native"-.-(!Options.ext_dll)));

Then, I just do ocamlbuild foo.native.so. It works flawlessly.

Possibly it is also useful to provide foo.byte.so.

File attachments

@vicuna
Copy link
Author

vicuna commented Dec 24, 2014

Comment author: @gasche

If you have a working patch to provide, I'm interested.

@vicuna
Copy link
Author

vicuna commented Dec 29, 2014

Comment author: @whitequark

@gasche, I added a patch. It adds the .so targets, and also a runtime_variant parametric flag.

The flag is needed on, I think, mainly x86, where all code in a shared library must be linked with -fPIC. On e.g. ARM, there is no such restriction.

Until #6693 is fixed, the ocamlbuild testsuite will fail (on x86, at least).

I did not add a runtime_variant(_shared) flag to the default tags for .so targets, because:

  • tags++"runtime_variant(x)" doesn't seem to do anything;
  • the _shared variant of the tag is probably not what most users of the .so targets want, as libcamlrun_shared.so is not even in default library search path.

I think it is OK to leave the default tags as-is, or if there will be a _pic variant of the runtime--a .a archive built with -fPIC--we could add runtime_variant(_pic) so that everything builds by default on all platforms. Having to manually specify "-runtime_variant(_pic), runtime_variant(_some_other)" is a bit awkward, but should be rarely needed.

@vicuna
Copy link
Author

vicuna commented May 2, 2015

Comment author: @gasche

Using the proposed patch, the test fails on my machine with the following log:

/home/gasche/.opam/4.03.0+local-git-trunk/bin/ocamldep.opt -modules hello.ml > hello.ml.depends
/home/gasche/.opam/4.03.0+local-git-trunk/bin/ocamlc.opt -c -o hello.cmo hello.ml
/home/gasche/.opam/4.03.0+local-git-trunk/bin/ocamlc.opt -output-obj -cclib -shared -runtime-variant _shared hello.cmo -o hello.byte.so
/home/gasche/.opam/4.03.0+local-git-trunk/bin/ocamlopt.opt -c -o hello.cmx hello.ml
/home/gasche/.opam/4.03.0+local-git-trunk/bin/ocamlopt.opt -output-obj -cclib -shared -runtime-variant _shared hello.cmx -o hello.native.so

  • /home/gasche/.opam/4.03.0+local-git-trunk/bin/ocamlopt.opt -output-obj -cclib -shared -runtime-variant _shared hello.cmx -o hello.native.so
    File "caml_startup", line 1:
    Error: Cannot find file libasmrun_shared.a
    Command exited with code 2.

Indeed, #6693 builds no libasmrun_shared.a, only libasmrun_shared.so.

@vicuna
Copy link
Author

vicuna commented May 2, 2015

Comment author: @whitequark

It should work if you replace runtime_variant(_shared) with runtime_variant(_pic) (which really makes much more sense for users, as libasmrun_shared.so is not in the library search path.)

@vicuna
Copy link
Author

vicuna commented May 2, 2015

Comment author: @gasche

Indeed, I can confirm that the proposed change works. I have the patch ready to apply, but I may wait a bit to see what happens to #6693.

@vicuna
Copy link
Author

vicuna commented May 10, 2015

Comment author: @gasche

Merged in 4.02 and trunk, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants