Skip to content

Commit

Permalink
Use dune-configurator to set C flags (#216)
Browse files Browse the repository at this point in the history
* Use dune-configurator to set C flags

* Upgrade to dune language version 3.0

This automatically has the `(use_standard_c_and_cxx_flags true)` set.

* add dune-configurator

* dune-configurator in dune-project

* wip

* fmt

---------

Co-authored-by: Antonio Nuno Monteiro <[email protected]>
  • Loading branch information
yawaramin and anmonteiro authored Jan 6, 2025
1 parent 5b2f446 commit 6c310f3
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 3 deletions.
3 changes: 2 additions & 1 deletion dune-project
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(lang dune 2.8)
(lang dune 3.0)

(using dune_site 0.1)

Expand Down Expand Up @@ -27,6 +27,7 @@
(depends
(ocaml
(>= "5.1"))
dune-configurator
logs
(eio-ssl
(>= 0.3.0))
Expand Down
28 changes: 28 additions & 0 deletions lib/cflags/cflags.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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)
3 changes: 3 additions & 0 deletions lib/cflags/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(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)))
1 change: 1 addition & 0 deletions nix/ci/test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ let
dune
findlib
ocamlformat
dune-configurator
]);
doCheck = true;
checkPhase = ''
Expand Down
2 changes: 2 additions & 0 deletions nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ rec {
];
};

buildInputs = with ocamlPackages; [ dune-configurator ];

propagatedBuildInputs = with ocamlPackages; [
logs
eio-ssl
Expand Down
5 changes: 4 additions & 1 deletion piaf.opam
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ license: "BSD-3-clause"
homepage: "https://github.com/anmonteiro/piaf"
bug-reports: "https://github.com/anmonteiro/piaf/issues"
depends: [
"dune" {>= "2.8"}
"dune" {>= "3.0"}
"ocaml" {>= "5.1"}
"dune-configurator"
"logs"
"eio-ssl" {>= "0.3.0"}
"magic-mime"
Expand Down Expand Up @@ -38,9 +39,11 @@ build: [
name
"-j"
jobs
"--promote-install-files=false"
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
["dune" "install" "-p" name "--create-install-files" name]
]
dev-repo: "git+https://github.com/anmonteiro/piaf.git"

0 comments on commit 6c310f3

Please sign in to comment.