Skip to content

Commit

Permalink
fix(ctypes): Add link flags for external dependencies when using ctypes.
Browse files Browse the repository at this point in the history
Required when the external lib is not statically linked, in which case
linker needs to be aware at runtime what libs to call. This is the case
for

- dune exec <file>.bc
- dune utop
- mdx tests

That is add to ocamlmklib, link flags such as -l<libname> -L<libdir>
from ctypes if info is available.

If dependent library are not installed in expected locations, env
variable such as LD_LIBRARY_PATH must be extended when required program
is run, depending on os. This may be fixed by adding relocatable paths to
link flags.

Signed-off-by: Frej Soya <frej.soya@gmail.com>
  • Loading branch information
frejsoya authored and rgrinberg committed Mar 13, 2024
1 parent 6e0b25e commit 2ad6ac9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
1 change: 1 addition & 0 deletions doc/changes/8784.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add link flags to to ocamlmklib for ctypes stubs (#8784, @frejsoya)
9 changes: 8 additions & 1 deletion src/dune_rules/lib_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,14 @@ let build_stubs lib ~cctx ~dir ~expander ~requires ~dir_contents ~vlib_stubs_o_f
| _ -> Action_builder.return []
in
let c_library_flags =
Expander.expand_and_eval_set expander lib.c_library_flags ~standard
let open Action_builder.O in
let+ c_lib = Expander.expand_and_eval_set expander lib.c_library_flags ~standard
and+ ctypes_lib =
(* CR rgrinberg: Should we add these flags to :standard? to make
it possible for users to remove these *)
Ctypes_rules.ctypes_cclib_flags sctx ~expander ~buildable:lib.buildable
in
c_lib @ ctypes_lib
in
let lib_o_files_for_all_modes = Mode.Map.Multi.for_all_modes o_files in
let for_all_modes = List.rev_append vlib_stubs_o_files lib_o_files_for_all_modes in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ Explictly set {DYLD,LD}_LIBRARY_PATH at runtime for this testcase, otherwise
dlopen cannot find libexample, after loading dllexamplelib_stub.so

$ PKG_CONFIG_PATH="$LIBEX/pkgconfig" PKG_CONFIG_ARGN="--define-prefix" DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:$LIBEX" LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$LIBEX" CAML_LD_LIBRARY_PATH="$CAML_LD_LIBRARY_PATH:$PWD/_build/default/stubgen" dune exec ./example.bc
Fatal error: cannot load shared library dllexamplelib_stubs
Reason: $TESTCASE_ROOT/_build/default/stubgen/dllexamplelib_stubs.so: undefined symbol: example_add2
Aborted
[134]
4

Utop works with ctypes pkg-config external library.

Expand All @@ -23,7 +20,4 @@ Utop works with ctypes pkg-config external library.
pkg-config stubgen/.pkg-config/libexample.libs
ocamlc .utop/.utop.eobjs/byte/dune__exe__Utop.{cmi,cmo,cmt}
ocamlc .utop/utop.bc
Fatal error: cannot load shared library dllexamplelib_stubs
Reason: $TESTCASE_ROOT/_build/default/stubgen/dllexamplelib_stubs.so: undefined symbol: example_add2
Aborted
[134]
4

0 comments on commit 2ad6ac9

Please sign in to comment.