Skip to content

Commit

Permalink
Use dune-configurator to set C flags
Browse files Browse the repository at this point in the history
  • Loading branch information
yawaramin committed Jan 1, 2025
1 parent e1fa28d commit d895032
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

(generate_opam_files true)

(use_standard_c_and_cxx_flags true)

(source
(github anmonteiro/piaf))

Expand Down
29 changes: 29 additions & 0 deletions lib/cflags/cflags.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module C = Configurator.V1

let directory_exists fsp = Sys.file_exists fsp && Sys.is_directory fsp

let default c : C.Pkg_config.package_conf =
if C.ocaml_config_var_exn c "system" = "macosx"
then
if directory_exists "/usr/local/opt/openssl"
then
{ libs = [ "-L/usr/local/opt/openssl/lib" ]
; cflags = [ "-I/usr/local/opt/openssl/include" ]
}
else { libs = [ "-L/opt/local/lib" ]; cflags = [ "-I/opt/local/include" ] }
else { libs = [ "-lssl"; "-lcrypto" ]; cflags = [] }

let () =
C.main ~name:"ssl" (fun c ->
let default = default c in
let conf =
match C.Pkg_config.get c with
| None -> default
| Some pc ->
(match C.Pkg_config.query pc ~package:"openssl" with
| Some s -> s
| None -> default)
in
C.Flags.write_sexp "c_library_flags.sexp" conf.libs;
C.Flags.write_sexp "c_flags.sexp" conf.cflags)

4 changes: 4 additions & 0 deletions lib/cflags/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(executable
(name cflags)
(libraries dune-configurator))

11 changes: 10 additions & 1 deletion lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,13 @@
piaf.stream)
(foreign_stubs
(language c)
(names piaf_bigarray_read piaf_openssl_rand piaf_openssl_sha piaf_fdutils)))
(names piaf_bigarray_read piaf_openssl_rand piaf_openssl_sha piaf_fdutils)
(flags (:include c_flags.sexp)))
(c_library_flags
(:include c_library_flags.sexp)))

(rule
(targets c_flags.sexp c_library_flags.sexp)
(action
(run ./cflags/cflags.exe)))

0 comments on commit d895032

Please sign in to comment.