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

Fix C runtime windows build #786

Merged
merged 2 commits into from
Feb 13, 2025
Merged
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
22 changes: 22 additions & 0 deletions runtimes/c/discover.ml
Original file line number Diff line number Diff line change
@@ -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
25 changes: 19 additions & 6 deletions runtimes/c/dune
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
(executable
(name discover)
(modes native)
(libraries re compiler-libs.common))

(rule
(with-stdout-to
gmph
(run pkg-config --cflags gmp)))
gmp_include_flags
(run ./discover.exe gmp_flags)))

(rule
(deps runtime.c runtime.h dates_calc.h)
(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
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)
Expand Down