From 658ec98600099ce9a9e86ad589e631c0d1becbd1 Mon Sep 17 00:00:00 2001 From: Albert Peschar Date: Thu, 28 Sep 2023 10:17:34 +0000 Subject: [PATCH] Use pkg-config to discover libsodium --- config/discover.ml | 17 +++++++++++++++++ config/dune | 7 +++++++ lib/dune | 2 +- lib_gen/dune | 5 +++-- 4 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 config/discover.ml create mode 100644 config/dune diff --git a/config/discover.ml b/config/discover.ml new file mode 100644 index 0000000..f972bcf --- /dev/null +++ b/config/discover.ml @@ -0,0 +1,17 @@ +module C = Configurator.V1 + +let () = + C.main ~name:"sodium" (fun c -> + let default : C.Pkg_config.package_conf = + { libs = [ "-lsodium" ]; cflags = [] } + in + let conf = + match C.Pkg_config.get c with + | None -> default + | Some pc -> ( + match C.Pkg_config.query pc ~package:"libsodium" with + | None -> default + | Some deps -> deps) + in + C.Flags.write_sexp "c_flags.sexp" conf.cflags; + C.Flags.write_sexp "c_library_flags.sexp" conf.libs) diff --git a/config/dune b/config/dune new file mode 100644 index 0000000..2935a78 --- /dev/null +++ b/config/dune @@ -0,0 +1,7 @@ +(executable + (name discover) + (libraries dune-configurator)) + +(rule + (targets c_flags.sexp c_library_flags.sexp) + (action (run ./discover.exe))) diff --git a/lib/dune b/lib/dune index 86b6e24..2fe9828 100644 --- a/lib/dune +++ b/lib/dune @@ -2,7 +2,7 @@ (name sodium) (public_name sodium) (libraries ctypes sodium.storage sodium.generated) - (c_library_flags :standard -lsodium) + (c_library_flags (:include ../config/c_library_flags.sexp)) (modules :standard \ sodium_storage)) (library diff --git a/lib_gen/dune b/lib_gen/dune index ac538a6..2eb6ed5 100644 --- a/lib_gen/dune +++ b/lib_gen/dune @@ -1,7 +1,8 @@ (library (name sodium_generated) (public_name sodium.generated) - (c_library_flags :standard -lsodium) + (c_flags (:include ../config/c_flags.sexp)) + (c_library_flags (:include ../config/c_library_flags.sexp)) (libraries ctypes.stubs sodium_bindings) (modules sodium_generated) (c_names sodium_stubs)) @@ -38,7 +39,7 @@ (rule (deps sodium_types_detect.c) (targets sodium_types_detect) - (action (bash "cc -I \"$(ocamlfind query ctypes)\" -I \"$(ocamlc -where)\" -o sodium_types_detect sodium_types_detect.c"))) + (action (bash "cc -I \"$(ocamlfind query ctypes)\" -I \"$(ocamlc -where)\" $(pkg-config --cflags libsodium) -o sodium_types_detect sodium_types_detect.c"))) (rule (deps sodium_types_detect)