Skip to content

Commit

Permalink
fixup! Add stubs link flags when using ctypes.
Browse files Browse the repository at this point in the history
  • Loading branch information
frejsoya committed Sep 28, 2023
1 parent ac80e4a commit e4de22d
Showing 1 changed file with 42 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@ 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.

DYLD_LIBRARY_PATH="$LIBEX" LD_LIBRARY_PATH="$LIBEX" PKG_CONFIG_PATH="$LIBEX/pkgconfig" PKG_CONFIG_ARGN="--define-prefix" dune exec ./example.bc --display=short
Reference output


The program is run as bytecode wihout linking in either the stub stub dll or
external dll.


Run the program in bytecode

$ LIBEX=$(realpath "$PWD/../libexample")
$ tree $LIBEX
/workspace_root/test/blackbox-tests/test-cases/ctypes/libexample
Expand All @@ -23,11 +16,15 @@ Run the program in bytecode
`-- libexample.pc -> ../../../../../../../../../default/test/blackbox-tests/test-cases/ctypes/libexample/pkgconfig/libexample.pc

1 directory, 4 files

Build the stubs.so


$ DYLD_LIBRARY_PATH="$LIBEX" LD_LIBRARY_PATH="$LIBEX" PKG_CONFIG_PATH="$LIBEX/pkgconfig" PKG_CONFIG_ARGN="--define-prefix" dune build stubgen/dllexamplelib_stubs.so
+ cc -shared -undefined dynamic_lookup -Wl,-w -g -o stubgen/dllexamplelib_stubs.so stubgen/libexample__c_cout_generated_functions__Function_description__Functions.o -L/workspace_root/test/blackbox-tests/test-cases/ctypes/libexample -lexample
+ ar rcs stubgen/libexamplelib_stubs.a stubgen/libexample__c_cout_generated_functions__Function_description__Functions.o

$ CAML_LD_LIBRARY_PATH="$LIBEX/_build/default/stubgen" 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 build --display=short ./example.bc
$ PKG_CONFIG_PATH="$LIBEX/pkgconfig" PKG_CONFIG_ARGN="--define-prefix" dune build --display=short ./example.bc
ocamldep stubgen/.examplelib.objs/examplelib__C.impl.d
pkg-config stubgen/.pkg-config/libexample.cflags
pkg-config stubgen/.pkg-config/libexample.libs
Expand All @@ -40,66 +37,62 @@ Run the program in bytecode
ocamlc .example.eobjs/byte/dune__exe__Example.{cmo,cmt}
ocamlc example.bc

$ 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

Verify libexample.so is referenced.. (not portable)

$ otool -L _build/default/stubgen/dllexamplelib_stubs.so
_build/default/stubgen/dllexamplelib_stubs.so:
stubgen/dllexamplelib_stubs.so (compatibility version 0.0.0, current version 0.0.0)
libexample.so (compatibility version 0.0.0, current version 0.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1319.100.3)

ocamlrun + bytcode with ocaml requires CAML_LD_LIBRARY_PATH.
Explictly set LIBRARY_PATH at runtime,otherwise dlopen cannot find libexample. (TODO, fix with rpath and clean after install?)

$ DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:$LIBEX" CAML_LD_LIBRARY_PATH="$CAML_LD_LIBRARY_PATH:$PWD/_build/default/stubgen" ocamlrun _build/default/example.bc --display=short2>&1|sed 's/.sandbox\/[^/]*/SANDBOX_ID/g'
$ 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
pkg-config stubgen/.pkg-config/libexample.cflags
pkg-config stubgen/.pkg-config/libexample.libs
4

Utop and ctypes work together (How to test with complete exe)
TODO: add rpath information to _stubs.so to avoid DYLD_LIBRARY_PATH

$ 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


Trace of pkg-config flags

$ cat _build/default/stubgen/.pkg-config/libexample.cflags
-I/workspace_root/test/blackbox-tests/test-cases/ctypes/libexample
$ cat _build/default/stubgen/.pkg-config/libexample.libs
-L/workspace_root/test/blackbox-tests/test-cases/ctypes/libexample -lexample


Verify libexample is referenced in stubs.

TODO: example of manual/relative paths
osx:
install_name_tool -id libexample.so @rpath/libexample.so
#install_name_tool -id libexample.so @rpath/libexample.so
#Then some other tool sets @rpath.
- Use @rpath when building/linking
-

Example of manual hack to reference libexample.. no env paths!
(But broken.. needs to be reletive to dllexample?)

$ install_name_tool -change libexample.so ../../../libexample/libexample.so _build/default/stubgen/dllexamplelib_stubs.so

$ install_name_tool -change libexample.so ../libexample/libexample.so _build/default/stubgen/dllexamplelib_stubs.so
$ ls _build/default/stubgen/../../../../libexample
example.h
libexample.a
libexample.so
pkgconfig
$ otool -l _build/default/stubgen/dllexamplelib_stubs.so |grep -A3 RPATH
[1]
$ otool -L _build/default/stubgen/dllexamplelib_stubs.so
_build/default/stubgen/dllexamplelib_stubs.so:
stubgen/dllexamplelib_stubs.so (compatibility version 0.0.0, current version 0.0.0)
../libexample/libexample.so (compatibility version 0.0.0, current version 0.0.0)
../../../libexample/libexample.so (compatibility version 0.0.0, current version 0.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1319.100.3)

$ ls ../libexample
example.h
libexample.a
libexample.so
pkgconfig
$ echo "STRING $TESTCASE_ROOT"| sed 's/a//'
STRING
$ echo "$LIBEX"
/workspace_root/test/blackbox-tests/test-cases/ctypes/libexample

$ echo $PWD
$TESTCASE_ROOT
$ ocamlrun -I _build/default/stubgen _build/default/example.bc --display=short2>&1|sed
$ cd _build/default && ocamlrun -I stubgen example.bc --display=short
4


Trace of pkg-config flags

$ cat _build/default/stubgen/.pkg-config/libexample.cflags
-I/workspace_root/test/blackbox-tests/test-cases/ctypes/libexample
$ cat _build/default/stubgen/.pkg-config/libexample.libs
-L/workspace_root/test/blackbox-tests/test-cases/ctypes/libexample -lexample

0 comments on commit e4de22d

Please sign in to comment.