-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add link flags for external dependencies when using ctypes.
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
Showing
11 changed files
with
121 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
test/blackbox-tests/test-cases/ctypes/bytecode-stubs-external-lib.t/dune
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(executable | ||
(name example) | ||
(modes byte) | ||
(libraries examplelib)) |
3 changes: 3 additions & 0 deletions
3
test/blackbox-tests/test-cases/ctypes/bytecode-stubs-external-lib.t/dune-project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
(lang dune 3.10) | ||
|
||
(using ctypes 0.3) |
2 changes: 2 additions & 0 deletions
2
test/blackbox-tests/test-cases/ctypes/bytecode-stubs-external-lib.t/example.ml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
let () = | ||
Printf.printf "%d\n" (Examplelib.C.Functions.add2 2) |
82 changes: 82 additions & 0 deletions
82
test/blackbox-tests/test-cases/ctypes/bytecode-stubs-external-lib.t/run.t
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
|
||
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") | ||
$ tree $LIBEX | ||
/workspace_root/test/blackbox-tests/test-cases/ctypes/libexample | ||
|-- example.h -> ../../../../../../../../default/test/blackbox-tests/test-cases/ctypes/libexample/example.h | ||
|-- libexample.a -> ../../../../../../../../default/test/blackbox-tests/test-cases/ctypes/libexample/libexample.a | ||
|-- libexample.so -> ../../../../../../../../default/test/blackbox-tests/test-cases/ctypes/libexample/libexample.so | ||
`-- pkgconfig | ||
`-- libexample.pc -> ../../../../../../../../../default/test/blackbox-tests/test-cases/ctypes/libexample/pkgconfig/libexample.pc | ||
|
||
1 directory, 4 files | ||
|
||
Build the stubs.so | ||
|
||
|
||
$ PKG_CONFIG_PATH="$LIBEX/pkgconfig" PKG_CONFIG_ARGN="--define-prefix" dune build stubgen/dllexamplelib_stubs.so | ||
|
||
$ PKG_CONFIG_PATH="$LIBEX/pkgconfig" PKG_CONFIG_ARGN="--define-prefix" dune build ./example.bc | ||
|
||
ocamlrun + bytcode with ocaml requires CAML_LD_LIBRARY_PATH. | ||
Explictly set LIBRARY_PATH at runtime,otherwise dlopen cannot find libexample. | ||
Eventually setting proper build flags for libexample | ||
|
||
$ dune clean | ||
$ PKG_CONFIG_PATH="$LIBEX/pkgconfig" PKG_CONFIG_ARGN="--define-prefix" DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:$LIBEX" CAML_LD_LIBRARY_PATH="$CAML_LD_LIBRARY_PATH:$PWD/_build/default/stubgen" dune exec ./example.bc --display=short | ||
ocamlc stubgen/.examplelib.objs/byte/examplelib.{cmi,cmo,cmt} | ||
ocamldep stubgen/.examplelib.objs/examplelib__C.impl.d | ||
ocamldep stubgen/.examplelib.objs/examplelib__Function_description.impl.d | ||
ocamldep stubgen/.examplelib.objs/examplelib__Libexample__function_gen__Function_description__Functions.impl.d | ||
ocamldep stubgen/.examplelib.objs/examplelib__Libexample__type_gen.impl.d | ||
cc .dune/ccomp/ccomp | ||
pkg-config stubgen/.pkg-config/libexample.cflags | ||
ocamldep stubgen/.examplelib.objs/examplelib__Type_description.impl.d | ||
ocamldep stubgen/.examplelib.objs/examplelib__Types_generated.impl.d | ||
pkg-config stubgen/.pkg-config/libexample.libs | ||
ocamlc stubgen/.examplelib.objs/byte/examplelib__Type_description.{cmi,cmo,cmt} | ||
ocamlopt stubgen/.examplelib.objs/native/examplelib.{cmx,o} | ||
ocamlopt stubgen/.examplelib.objs/native/examplelib__Type_description.{cmx,o} | ||
ocamlc stubgen/.examplelib.objs/byte/examplelib__Libexample__type_gen.{cmi,cmo,cmt} | ||
ocamlopt stubgen/.examplelib.objs/native/examplelib__Libexample__type_gen.{cmx,o} | ||
ocamlopt stubgen/libexample__type_gen.exe | ||
libexample__type_gen stubgen/libexample__c_cout_generated_types.c | ||
cc stubgen/libexample__c_cout_generated_types.exe | ||
libexample__c_cout_generated_types stubgen/libexample__c_generated_types.ml | ||
ocamldep stubgen/.examplelib.objs/examplelib__Libexample__c_generated_types.impl.d | ||
ocamlc stubgen/.examplelib.objs/byte/examplelib__Libexample__c_generated_types.{cmi,cmo,cmt} | ||
ocamlc stubgen/.examplelib.objs/byte/examplelib__Types_generated.{cmi,cmo,cmt} | ||
ocamlopt stubgen/.examplelib.objs/native/examplelib__Libexample__c_generated_types.{cmx,o} | ||
ocamlc stubgen/.examplelib.objs/byte/examplelib__Function_description.{cmi,cmo,cmt} | ||
ocamlopt stubgen/.examplelib.objs/native/examplelib__Types_generated.{cmx,o} | ||
ocamlopt stubgen/.examplelib.objs/native/examplelib__Function_description.{cmx,o} | ||
ocamlc stubgen/.examplelib.objs/byte/examplelib__Libexample__function_gen__Function_description__Functions.{cmi,cmo,cmt} | ||
ocamlopt stubgen/.examplelib.objs/native/examplelib__Libexample__function_gen__Function_description__Functions.{cmx,o} | ||
ocamlopt stubgen/libexample__function_gen__Function_description__Functions.exe | ||
libexample__function_gen__Function_description__Functions stubgen/libexample__c_generated_functions__Function_description__Functions.ml | ||
libexample__function_gen__Function_description__Functions stubgen/libexample__c_cout_generated_functions__Function_description__Functions.c | ||
ocamldep stubgen/.examplelib.objs/examplelib__Libexample__c_generated_functions__Function_description__Functions.impl.d | ||
cc stubgen/libexample__c_cout_generated_functions__Function_description__Functions.o | ||
ocamlc stubgen/.examplelib.objs/byte/examplelib__Libexample__c_generated_functions__Function_description__Functions.{cmi,cmo,cmt} | ||
ocamlmklib stubgen/dllexamplelib_stubs.so,stubgen/libexamplelib_stubs.a | ||
ocamlc stubgen/.examplelib.objs/byte/examplelib__C.{cmi,cmo,cmt} | ||
ocamlc .example.eobjs/byte/dune__exe__Example.{cmi,cmti} | ||
ocamlc stubgen/examplelib.cma | ||
ocamlc .example.eobjs/byte/dune__exe__Example.{cmo,cmt} | ||
ocamlc example.bc | ||
4 | ||
|
||
Utop works with ctypes pkg-config external library. | ||
|
||
$ DYLD_LIBRARY_PATH="$DYLD_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 |
14 changes: 14 additions & 0 deletions
14
test/blackbox-tests/test-cases/ctypes/bytecode-stubs-external-lib.t/stubgen/dune
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
8 changes: 8 additions & 0 deletions
8
...box-tests/test-cases/ctypes/bytecode-stubs-external-lib.t/stubgen/function_description.ml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
3 changes: 3 additions & 0 deletions
3
...lackbox-tests/test-cases/ctypes/bytecode-stubs-external-lib.t/stubgen/type_description.ml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module Types (F : Ctypes.TYPE) = struct | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters