diff --git a/src/bin/text/dune b/src/bin/text/dune index 793e68dc2..c913b5711 100644 --- a/src/bin/text/dune +++ b/src/bin/text/dune @@ -1,9 +1,14 @@ (documentation (package alt-ergo)) +(executable + (name gen_link_flags) + (libraries unix fmt stdcompat) + (modules gen_link_flags)) + (rule (with-stdout-to link_flags.dune - (run ./gen-link-flags.sh %{env:LINK_MODE=dynamic} %{ocaml-config:system}))) + (run ./gen_link_flags.exe %{env:LINK_MODE=dynamic} %{ocaml-config:system}))) (executable (name Main_text) diff --git a/src/bin/text/gen-link-flags.sh b/src/bin/text/gen-link-flags.sh deleted file mode 100755 index fb048aec3..000000000 --- a/src/bin/text/gen-link-flags.sh +++ /dev/null @@ -1,56 +0,0 @@ -#!/bin/sh - -set -ue - -LINK_MODE="$1" -OS="$2" -FLAGS= -CCLIB= - -case "$LINK_MODE" in - dynamic) - ;; # No extra flags needed - static) - case "$OS" in - linux) - CCLIB="-static -no-pie";; - *) - echo "No known static compilation flags for '$OS'" >&2 - exit 1 - esac;; - mixed) - FLAGS="-noautolink" - # Note: for OCaml 5, use -lcamlstrnat and -lunixnat and mind zlib - # https://github.com/ocaml/ocaml/issues/12562 - CCLIB="-lstdcompat_stubs -lcamlzip -lzarith -lcamlstr -lunix -lz" - LIBS="gmp" - case "$OS" in - linux) - for lib in $LIBS; do - CCLIB="$CCLIB -l$lib" - done - CCLIB="-Wl,-Bstatic $CCLIB -Wl,-Bdynamic";; - macosx) - for lib in $LIBS; do - if [[ $lib == lib* ]]; then - archive="$lib.a" - else - archive="lib$lib.a" - fi - CCLIB="$CCLIB $(pkg-config $lib --variable libdir)/$archive" - done;; - *) - echo "No known mixed compilation flags for '$OS'" >&2 - exit 1 - esac;; - - *) - echo "Invalid link mode '$LINK_MODE'" >&2 - exit 2 -esac - -echo '(' -echo ' -linkall' -for f in $FLAGS; do echo " $f"; done -for f in $CCLIB; do echo " -cclib $f"; done -echo ')' diff --git a/src/bin/text/gen_link_flags.ml b/src/bin/text/gen_link_flags.ml new file mode 100644 index 000000000..97f7839b0 --- /dev/null +++ b/src/bin/text/gen_link_flags.ml @@ -0,0 +1,49 @@ +let pkgconfig lib archive = + let cmd = Fmt.str "pkg-config %s --variable libdir" lib in + let output = Unix.open_process_in cmd |> input_line in + Fmt.str "%s/%s" output archive + +let pp_lib ppf s = Fmt.pf ppf "-cclib %s" s + +let () = + let mixed_flags = ["-noautolink"] in + (* Note: for OCaml 5, use -lcamlstrnat and -lunixnat and mind zlib + https://github.com/ocaml/ocaml/issues/12562 *) + let mixed_cclib = [ + "-lstdcompat_stubs"; + "-lcamlzip"; + "-lzarith"; + "-lcamlstr"; + "-lunix"; + "-lz" + ] + in + let libs = ["gmp"] in + let link_mode = Sys.argv.(1) and os = Sys.argv.(2) in + let flags, cclib = + match link_mode, os with + | "dynamic", _ -> [], [] + | "static", "linux" -> [], ["-static"; "-no-pie"] + | "mixed", "linux" -> + let cclib = mixed_cclib @ List.map (fun s -> "-l" ^ s) libs in + mixed_flags, "-Wl,-Bdynamic" :: "-Wl,-Bstatic" :: cclib + | "mixed", "macosx" -> + let cclib = mixed_cclib @ + List.map + (fun lib -> + let archive = + if Stdcompat.String.starts_with + ~prefix:"lib" lib then + Fmt.str "%s.a" lib + else + Fmt.str "lib%s.a" lib + in + pkgconfig lib archive + ) libs + in + mixed_flags, cclib + | _ -> Fmt.invalid_arg "unsupported mode %s and OS %s" link_mode os + in + Fmt.pr "@[(-linkall %a %a)@]" + Fmt.(list ~sep:sp string) flags + Fmt.(list ~sep:sp pp_lib) cclib;