From 34d5f51ee5d247d17833796db7a844fd3f31f839 Mon Sep 17 00:00:00 2001 From: vbot Date: Tue, 11 Feb 2025 18:16:17 +0100 Subject: [PATCH 1/2] Fix C runtime build on windows --- runtimes/c/discover.ml | 22 ++++++++++++++++++++++ runtimes/c/dune | 24 ++++++++++++++++++------ 2 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 runtimes/c/discover.ml diff --git a/runtimes/c/discover.ml b/runtimes/c/discover.ml new file mode 100644 index 000000000..73b8c7485 --- /dev/null +++ b/runtimes/c/discover.ml @@ -0,0 +1,22 @@ +let () = + let open Re in + if Array.length Sys.argv <> 2 then + failwith "expected either ar or gmp_flags as argument" + else + match Sys.argv.(1) with + | "ar" -> + (* On mingw's opam windows install gcc is named: x86_64-w64-mingw32-gcc, + and, ar is named: x86_64-w64-mingw32-ar. We substitute gcc by ar from + the c compiler retrieved in the OCaml compiler's config to make the + build system handle it properly. *) + let r = compile (seq [str "gcc"; opt (str ".exe"); eol]) in + if (Sys.win32 || Sys.cygwin) && Re.execp r Config.c_compiler then + Config.c_compiler |> replace_string r ~by:"ar" |> print_string + else print_string "ar" + | "gmp_flags" -> + if Sys.win32 || Sys.cygwin then + (* On cygwin/windows, pkg-config is not aware of gmp but opam is aware + of the include dir. *) + () + else exit @@ Sys.command "pkg-config --cflags gmp" + | s -> Format.ksprintf failwith "unknown command: %s" s diff --git a/runtimes/c/dune b/runtimes/c/dune index 2bbe149e8..b64d39151 100644 --- a/runtimes/c/dune +++ b/runtimes/c/dune @@ -1,7 +1,11 @@ +(executable + (name discover) + (modes native) + (libraries re compiler-libs.common)) + (rule - (with-stdout-to - gmph - (run pkg-config --cflags gmp))) + (with-stdout-to gmph + (run ./discover.exe gmp_flags))) (rule (deps runtime.c runtime.h dates_calc.h) @@ -10,13 +14,21 @@ (system "%{cc} --std=c89 -Wall -Werror -pedantic -c runtime.c -I . %{read-strings:gmph} -o %{target}"))) +(rule + (with-stdout-to + ar_bin_name + (run ./discover.exe ar))) + (rule (deps runtime.o) (target catala_runtime.a) (action - ; FIXME: ar is not portable, it makes the build fails on windows. - ; A workaround is to install a mingw toolchain that exposes ar. - (run ar rcs %{target} %{lib:dates_calc:c/dates_calc.o} %{deps}))) + (run + %{read-strings:ar_bin_name} + rcs + %{target} + %{lib:dates_calc:c/dates_calc.o} + %{deps}))) (rule (target dates_calc.h) From b7a75c97f0e67602d21bafd3bd5ac016be451c87 Mon Sep 17 00:00:00 2001 From: vbot Date: Tue, 11 Feb 2025 18:16:48 +0100 Subject: [PATCH 2/2] Rename dune variable --- runtimes/c/dune | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/runtimes/c/dune b/runtimes/c/dune index b64d39151..39f156149 100644 --- a/runtimes/c/dune +++ b/runtimes/c/dune @@ -4,7 +4,8 @@ (libraries re compiler-libs.common)) (rule - (with-stdout-to gmph + (with-stdout-to + gmp_include_flags (run ./discover.exe gmp_flags))) (rule @@ -12,7 +13,7 @@ (target runtime.o) (action (system - "%{cc} --std=c89 -Wall -Werror -pedantic -c runtime.c -I . %{read-strings:gmph} -o %{target}"))) + "%{cc} --std=c89 -Wall -Werror -pedantic -c runtime.c -I . %{read-strings:gmp_include_flags} -o %{target}"))) (rule (with-stdout-to