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 9, 2023
1 parent 0a6ba08 commit 903ba8f
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions test/blackbox-tests/test-cases/ctypes/directories.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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

$ cat > lib/dune << EOF
> (executable
> (name e)
> (flags :standard -w -27)
> (ctypes
> (build_flags_resolver
> (vendored (c_flags :standard (:include extra_flags.sexp))))
> (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

$ echo "(-I lib -I .)" > lib/extra_flags.sexp

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

$ echo "(-I lib)" > lib/extra_flags.sexp
$ dune build > /dev/null 2>&1
[1]

$ echo "(-I .)" > lib/extra_flags.sexp
$ dune build > /dev/null 2>&1
[1]

0 comments on commit 903ba8f

Please sign in to comment.