Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(ctypes): showcase deps field not supporting the full dependency specification #11443

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 42 additions & 38 deletions src/dune_rules/ctypes/ctypes_field.ml
Original file line number Diff line number Diff line change
Expand Up @@ -158,44 +158,48 @@ include Stanza.Make (struct
let decode =
let open Dune_lang.Decoder in
fields
(let+ external_library_name = field "external_library_name" string
and+ build_flags_resolver =
field_o "build_flags_resolver" Build_flags_resolver.decode
and+ type_description = field "type_description" Type_description.decode
and+ loc_fd, function_description =
located (multi_field "function_description" Function_description.decode)
and+ headers = field_o "headers" Headers.decode
and+ generated_types = field_o "generated_types" Module_name.decode
and+ generated_entry_point = field "generated_entry_point" Module_name.decode
and+ deps = field_o "deps" (repeat Dep_conf.decode)
and+ version = Syntax.get_exn syntax in
let external_library_name = External_lib_name.of_string external_library_name in
(match
String.Map.of_list_map function_description ~f:(fun fd ->
let key = c_generated_functions_cout_c_of_lib ~external_library_name fd in
key, ())
with
| Ok _ -> ()
| Error (_, a, _) ->
User_error.raise
~loc:loc_fd
[ Pp.textf
"Only a single (function_description) can instantiate %s as %s."
(Module_name.to_string a.functor_)
(Module_name.to_string a.instance)
]);
{ external_library_name
; build_flags_resolver =
Option.value build_flags_resolver ~default:Build_flags_resolver.default
; headers = Option.value headers ~default:Headers.default
; type_description
; function_description
; generated_types =
Option.value generated_types ~default:(Module_name.of_string "Types_generated")
; generated_entry_point
; deps = Option.value ~default:[] deps
; version
})
(let* deps = field "deps" (Bindings.decode Dep_conf.decode) ~default:Bindings.empty in
String_with_vars.add_user_vars_to_decoding_env
(Bindings.var_names deps)
(let+ external_library_name = field "external_library_name" string
and+ build_flags_resolver =
field_o "build_flags_resolver" Build_flags_resolver.decode
and+ type_description = field "type_description" Type_description.decode
and+ loc_fd, function_description =
located (multi_field "function_description" Function_description.decode)
and+ headers = field_o "headers" Headers.decode
and+ generated_types = field_o "generated_types" Module_name.decode
and+ generated_entry_point = field "generated_entry_point" Module_name.decode
and+ version = Syntax.get_exn syntax in
let external_library_name = External_lib_name.of_string external_library_name in
(match
String.Map.of_list_map function_description ~f:(fun fd ->
let key = c_generated_functions_cout_c_of_lib ~external_library_name fd in
key, ())
with
| Ok _ -> ()
| Error (_, a, _) ->
User_error.raise
~loc:loc_fd
[ Pp.textf
"Only a single (function_description) can instantiate %s as %s."
(Module_name.to_string a.functor_)
(Module_name.to_string a.instance)
]);
{ external_library_name
; build_flags_resolver =
Option.value build_flags_resolver ~default:Build_flags_resolver.default
; headers = Option.value headers ~default:Headers.default
; type_description
; function_description
; generated_types =
Option.value
generated_types
~default:(Module_name.of_string "Types_generated")
; generated_entry_point
; deps = Bindings.to_list deps
; version
}))
;;

let () =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#define BAR_VERSION 1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#define BAZ_VERSION 1
21 changes: 21 additions & 0 deletions test/blackbox-tests/test-cases/ctypes/deps-full-spec.t/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(executable
(name example)
(flags
(:standard -w -9-27))
(ctypes
(external_library_name libexample)
(build_flags_resolver pkg_config)
(deps
(:foo_h "foo.h")
(:bar_h bar.h)
baz.h)
(headers
(preamble
"#include <example.h>\n#include \"%{foo_h}\"\n#include \"%{bar_h}\"\n#include \"baz.h\"\n#include \"%{dep:qux.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,5 @@
(lang dune 3.8)

(using ctypes 0.3)

(use_standard_c_and_cxx_flags false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let () =
Printf.printf "%d\n" (C.Functions.add2 C.Types.foo_version);
Printf.printf "%d\n" (C.Functions.add2 C.Types.bar_version);
Printf.printf "%d\n" (C.Functions.add2 C.Types.baz_version);
Printf.printf "%d\n" (C.Functions.add2 C.Types.qux_version);
()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#define FOO_VERSION 1
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 @@
#define QUX_VERSION 1
18 changes: 18 additions & 0 deletions test/blackbox-tests/test-cases/ctypes/deps-full-spec.t/run.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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.

Then generate cstubs for it, build an executable that uses those cstubs, and
run the executable that tests the library through the cstubs.

$ LIBEX=$(realpath "$PWD/../libexample")
$ DYLD_LIBRARY_PATH="$LIBEX" LD_LIBRARY_PATH="$LIBEX" PKG_CONFIG_PATH="$LIBEX/pkgconfig" PKG_CONFIG_ARGN="--define-prefix" dune exec ./example.exe
File "dune", line 14, characters 38-46:
14 | "#include <example.h>\n#include \"%{foo_h}\"\n#include \"%{bar_h}\"\n#include \"baz.h\"\n#include \"%{dep:qux.h}\""))
^^^^^^^^
Error: %{foo_h} isn't allowed in this position.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what to look at to be able to fix this error

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be %{dep:foo.h}? `

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I want to use the name of the dependency, this is on purpose.

File "dune", line 14, characters 61-69:
14 | "#include <example.h>\n#include \"%{foo_h}\"\n#include \"%{bar_h}\"\n#include \"baz.h\"\n#include \"%{dep:qux.h}\""))
^^^^^^^^
Error: %{bar_h} isn't allowed in this position.
[1]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Types (F : Ctypes.TYPE) = struct
open F

let foo_version = constant "FOO_VERSION" int
let bar_version = constant "BAR_VERSION" int
let baz_version = constant "BAZ_VERSION" int
let qux_version = constant "QUX_VERSION" int

end
Loading