Skip to content

Commit

Permalink
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 committed Oct 1, 2023
1 parent 5d6a2bd commit d8d72b1
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/dune_rules/ctypes/ctypes_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,7 @@ let gen_rules ~cctx ~(buildable : Buildable.t) ~loc ~scope ~dir ~sctx =
~c_types_includer_module))
;;

let ctypes_cclib_flags sctx ~expander ~(buildable : Buildable.t) =
let standard = Action_builder.return [] in
let ctypes_cclib_flags ?(standard = Action_builder.return []) sctx ~expander ~(buildable : Buildable.t) =
match buildable.ctypes with
| None -> standard
| Some ctypes ->
Expand Down
3 changes: 2 additions & 1 deletion src/dune_rules/ctypes/ctypes_rules.mli
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ val gen_rules
-> unit Memo.t

val ctypes_cclib_flags
: Super_context.t
: ?standard:(string list Action_builder.t)
-> Super_context.t
-> expander:Expander.t
-> buildable:Dune_file.Buildable.t
-> string list Action_builder.t
4 changes: 1 addition & 3 deletions src/dune_rules/lib_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,7 @@ let build_stubs lib ~cctx ~dir ~expander ~requires ~dir_contents ~vlib_stubs_o_f
Cxx_flags.get_flags ~for_:Link (Context.build_context ctx)
| _ -> Action_builder.return []
in
let c_library_flags =
Expander.expand_and_eval_set expander lib.c_library_flags ~standard
in
let c_library_flags = Ctypes_rules.ctypes_cclib_flags sctx ~expander ~buildable:lib.buildable ~standard 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
if Mode.Dict.Set.to_list modes.ocaml
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(executable
(name example)
(modes byte)
(libraries examplelib))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(lang dune 3.10)

(using ctypes 0.3)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let () =
Printf.printf "%d\n" (Examplelib.C.Functions.add2 2)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Build an example library as a DLL and set up the environment so that it looks
like a system/distro library that can be probed with pkg-config and dynamically
loaded.

Run the program in bytecode
$ LIBEX=$(realpath "$PWD/../libexample")

ocamlrun + requires CAML_LD_LIBRARY_PATH such that dlopen can find
dllexamplelib_stub.so

Explictly set {DYLD,LD}_LIBRARY_PATH at runtime for this testcase, otherwise
dlopen cannot find libexample.

$ 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
4

Utop works with ctypes pkg-config external library.

$ DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:$LIBEX" LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$LIBEX" PKG_CONFIG_PATH="$LIBEX/pkgconfig" PKG_CONFIG_ARGN="--define-prefix" dune utop --display=short ./ -- example.ml
pkg-config stubgen/.pkg-config/libexample.cflags
pkg-config stubgen/.pkg-config/libexample.libs
ocamlc .utop/.utop.eobjs/byte/dune__exe__Utop.{cmi,cmo,cmt}
ocamlc .utop/utop.bc
4
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(library
(name examplelib)
(flags (:standard -w -9-27))
(ctypes
(external_library_name libexample)
(build_flags_resolver pkg_config)
(headers (include "example.h"))
(type_description
(instance Types)
(functor Type_description))
(function_description
(instance Functions)
(functor Function_description))
(generated_entry_point C)))
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
open Ctypes

module Types = Types_generated

module Functions (F : Ctypes.FOREIGN) = struct
open F
let add2 = foreign "example_add2" (int @-> returning int)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Types (F : Ctypes.TYPE) = struct

end
1 change: 1 addition & 0 deletions test/blackbox-tests/test-cases/ctypes/dune
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

(cram
(applies_to
bytecode-stubs-external-lib
lib-pkg_config
lib-pkg_config-multiple-fd
lib-external-name-need-mangling
Expand Down

0 comments on commit d8d72b1

Please sign in to comment.