Skip to content

Commit

Permalink
test: ctypes and relative include paths
Browse files Browse the repository at this point in the history
This adds a test that shows that under `(using ctypes 0.2)`, local
headers need to be added in two different locations because rules are
executed from different places. See ocaml#5325.

Signed-off-by: Etienne Millon <me@emillon.org>
  • Loading branch information
emillon committed Jan 6, 2023
1 parent 1c84d1b commit 04e0dab
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions test/blackbox-tests/test-cases/ctypes/directories.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
This test characterizes how the ctypes support deals with directories, in
particular when relative paths are passed as `-I`.

With `(using ctypes 0.2)`, some commands are executed in workspace root and
others are executed in the directory where `(ctypes)` is found, so it is
necessary to pass relative dirs twice. (this prevents vendorability, since this
requires knowing the path from the workspace root to the current directory).

To test that, we create a binding to `O_RDWR` and `open`, both in the C library.
These are provided by `fcntl.h`, but instead of using this header, we're
proxying it through a `lib.h` which should be in the include path for
compilation to work.
$ cat > dune-project << EOF
> (lang dune 3.4)
> (using ctypes 0.2)
> EOF
$ mkdir lib
$ mk_dune () {
> local flags=$1
> cat << EOF
> (executable
> (name e)
> (flags :standard -w -27)
> (ctypes
> (build_flags_resolver
> (vendored (c_flags :standard $flags)))
> (external_library_name l)
> (headers (include lib.h))
> (deps lib.h)
> (type_description
> (functor f)
> (instance types))
> (function_description
> (functor f)
> (instance functions))
> (generated_entry_point entry)))
> EOF
> }
$ mk_dune "-I lib -I ." > lib/dune
$ cat > lib/lib.h << EOF
> #include <fcntl.h>
> EOF
$ cat > lib/e.ml << EOF
> let () = Printf.printf "%d\n" Entry.Types.rdwr
> let (_:char Ctypes.ptr -> int -> int) = Entry.Functions.open_
> EOF
$ cat > lib/f.ml << EOF
> module Types(F:Ctypes.TYPE) = struct
> open F
> let rdwr = constant "O_RDWR" int
> end
>
> module Functions(F:Ctypes.FOREIGN) = struct
> open Ctypes
> open F
> let open_ = foreign "open" (ptr char @-> int @-> returning int)
> end
> EOF
$ dune build
We ensure that just `-I lib` or `-I .` are not enough on their own.
$ mk_dune "-I lib" > lib/dune
$ dune build > /dev/null 2>&1
[1]
$ mk_dune "-I ." > lib/dune
$ dune build > /dev/null 2>&1
[1]

0 comments on commit 04e0dab

Please sign in to comment.