From 55a2928f70404a94aa6cd46073c72222db74578e Mon Sep 17 00:00:00 2001 From: Pierre Villemot Date: Mon, 29 Jul 2024 14:43:24 +0200 Subject: [PATCH 01/10] Rewrite gen-link-flags in OCaml --- src/bin/text/dune | 11 +++++- src/bin/text/gen_link_flags.ml | 69 ++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 src/bin/text/gen_link_flags.ml diff --git a/src/bin/text/dune b/src/bin/text/dune index 793e68dc2..f2a0569a0 100644 --- a/src/bin/text/dune +++ b/src/bin/text/dune @@ -1,9 +1,18 @@ (documentation (package alt-ergo)) +(executable + (name gen_link_flags) + (libraries unix fmt stdcompat) + (modules gen_link_flags) + (promote (until-clean))) + (rule + (target link_flags.dune) + (deps (:gen gen_link_flags.exe)) + (action (with-stdout-to link_flags.dune - (run ./gen-link-flags.sh %{env:LINK_MODE=dynamic} %{ocaml-config:system}))) + (run %{gen} %{env:LINK_MODE=dynamic} %{ocaml-config:system})))) (executable (name Main_text) diff --git a/src/bin/text/gen_link_flags.ml b/src/bin/text/gen_link_flags.ml new file mode 100644 index 000000000..53a13815b --- /dev/null +++ b/src/bin/text/gen_link_flags.ml @@ -0,0 +1,69 @@ +let pkgconfig lib archive = + let cmd = Fmt.str "pkg-config %s --variable libdir" lib in + let output = + Unix.open_process_in cmd + |> In_channel.input_line + |> Option.get + in + Fmt.str "%s/%s" output archive + +let pp_lib ppf s = Fmt.pf ppf "-cclib %s" s + +let () = + let link_mode = Sys.argv.(1) in + let os = Sys.argv.(2) in + let flags, cclib = + match link_mode with + | "dynamic" -> [], [] + | "static" -> + begin + match os with + | "linux" -> ["-static"; "-no-pie"], [] + | _ -> + Fmt.epr "No known static compilation flags for %s" os; + exit 1 + end + | "mixed" -> + begin + let flags = ["-noautolink"] in + let cclib = [ + "-lstdcompat_stubs"; + "-lcamlzip"; + "-lzarith"; + "-lcamlstr"; + "-lunix"; + "-lz" + ] + in + let libs = ["gmp"] in + match os with + | "linux" -> + let cclib = cclib @ List.map (fun s -> "-l" ^ s) libs in + flags, + "-Wl,-Bstatic" :: cclib @ ["-Wl,-Bdynamic"] + | "macosx" -> + let cclib = 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 + flags, + cclib + | _ -> + Fmt.epr "No known mixed compilation flags for %s" os; + exit 1 + end + | _ -> + Fmt.epr "Invalid link mode %s" link_mode; + exit 1 + in + Fmt.pr "@[(-linkall %a %a)@]" + Fmt.(list ~sep:sp string) flags + Fmt.(list ~sep:sp pp_lib) cclib From 1b27d4f20ca3e63dfe8baf65b2b70968ed729a1a Mon Sep 17 00:00:00 2001 From: Pierre Villemot Date: Tue, 30 Jul 2024 12:22:09 +0200 Subject: [PATCH 02/10] Fix script --- src/bin/text/dune | 4 ++-- src/bin/text/gen_link_flags.ml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bin/text/dune b/src/bin/text/dune index f2a0569a0..647363f60 100644 --- a/src/bin/text/dune +++ b/src/bin/text/dune @@ -11,8 +11,8 @@ (target link_flags.dune) (deps (:gen gen_link_flags.exe)) (action - (with-stdout-to link_flags.dune - (run %{gen} %{env:LINK_MODE=dynamic} %{ocaml-config:system})))) + (with-stdout-to link_flags.dune + (run %{gen} %{env:LINK_MODE=dynamic} %{ocaml-config:system})))) (executable (name Main_text) diff --git a/src/bin/text/gen_link_flags.ml b/src/bin/text/gen_link_flags.ml index 53a13815b..c16af97e9 100644 --- a/src/bin/text/gen_link_flags.ml +++ b/src/bin/text/gen_link_flags.ml @@ -18,7 +18,7 @@ let () = | "static" -> begin match os with - | "linux" -> ["-static"; "-no-pie"], [] + | "linux" -> [], ["-static"; "-no-pie"] | _ -> Fmt.epr "No known static compilation flags for %s" os; exit 1 From 9d2b7676a24e88b9518ccec0e3f9f72dbe7ea34b Mon Sep 17 00:00:00 2001 From: Pierre Villemot Date: Tue, 30 Jul 2024 14:04:44 +0200 Subject: [PATCH 03/10] rewrite again --- src/bin/text/dune | 4 +- src/bin/text/gen_link_flags.ml | 137 ++++++++++++++++++++------------- 2 files changed, 84 insertions(+), 57 deletions(-) diff --git a/src/bin/text/dune b/src/bin/text/dune index 647363f60..c1c868de4 100644 --- a/src/bin/text/dune +++ b/src/bin/text/dune @@ -3,7 +3,7 @@ (executable (name gen_link_flags) - (libraries unix fmt stdcompat) + (libraries unix fmt stdcompat cmdliner) (modules gen_link_flags) (promote (until-clean))) @@ -12,7 +12,7 @@ (deps (:gen gen_link_flags.exe)) (action (with-stdout-to link_flags.dune - (run %{gen} %{env:LINK_MODE=dynamic} %{ocaml-config:system})))) + (run %{gen} --link-mode %{env:LINK_MODE=dynamic} --os %{ocaml-config:system})))) (executable (name Main_text) diff --git a/src/bin/text/gen_link_flags.ml b/src/bin/text/gen_link_flags.ml index c16af97e9..14b98491a 100644 --- a/src/bin/text/gen_link_flags.ml +++ b/src/bin/text/gen_link_flags.ml @@ -1,3 +1,43 @@ +type os = Linux | Macos +type link_mode = Dynamic | Static | Mixed + +module Cmd = struct + open Cmdliner + + let show l = List.map (fun (s, _) -> s) l + + let os_term = + let all = [ + ("linux", Linux); + ("macosx", Macos) + ] + in + let doc = + Fmt.str "Choose the operating system, $(docv) must be %s." + (Arg.doc_alts @@ show all) + in + Arg.(value & opt (enum all) Linux & info ["os"] ~docv:"OS" ~doc) + + let link_mode_term = + let all = [ + ("dynamic", Dynamic); + ("static", Static); + ("mixed", Mixed) + ] + in + let doc = + Fmt.str "Choose the operating system, $(docv) must be %s." + (Arg.doc_alts @@ show all) + in + Arg.(value & opt (enum all) Dynamic & + info ["link-mode"] ~docv:"MODE" ~doc) + + let parse k = + let info = Cmd.info "rewrite-gen-link-flags" in + Cmd.v info Term.(ret (const k $ link_mode_term $ os_term)) + |> Cmd.eval +end + let pkgconfig lib archive = let cmd = Fmt.str "pkg-config %s --variable libdir" lib in let output = @@ -9,61 +49,48 @@ let pkgconfig lib archive = let pp_lib ppf s = Fmt.pf ppf "-cclib %s" s + let () = - let link_mode = Sys.argv.(1) in - let os = Sys.argv.(2) in - let flags, cclib = - match link_mode with - | "dynamic" -> [], [] - | "static" -> - begin - match os with - | "linux" -> [], ["-static"; "-no-pie"] - | _ -> - Fmt.epr "No known static compilation flags for %s" os; - exit 1 - end - | "mixed" -> - begin - let flags = ["-noautolink"] in - let cclib = [ - "-lstdcompat_stubs"; - "-lcamlzip"; - "-lzarith"; - "-lcamlstr"; - "-lunix"; - "-lz" - ] + let mixed_flags = ["-noautolink"] in + let mixed_cclib = [ + "-lstdcompat_stubs"; + "-lcamlzip"; + "-lzarith"; + "-lcamlstr"; + "-lunix"; + "-lz" + ] + in + let libs = ["gmp"] in + let rc = + Cmd.parse @@ fun link_mode os -> + 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, Macos -> + 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 - let libs = ["gmp"] in - match os with - | "linux" -> - let cclib = cclib @ List.map (fun s -> "-l" ^ s) libs in - flags, - "-Wl,-Bstatic" :: cclib @ ["-Wl,-Bdynamic"] - | "macosx" -> - let cclib = 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 - flags, - cclib - | _ -> - Fmt.epr "No known mixed compilation flags for %s" os; - exit 1 - end - | _ -> - Fmt.epr "Invalid link mode %s" link_mode; - exit 1 + mixed_flags, cclib + | _ -> Fmt.invalid_arg "unsupported mode and OS" + in + Fmt.pr "@[(-linkall %a %a)@]" + Fmt.(list ~sep:sp string) flags + Fmt.(list ~sep:sp pp_lib) cclib; + `Ok () in - Fmt.pr "@[(-linkall %a %a)@]" - Fmt.(list ~sep:sp string) flags - Fmt.(list ~sep:sp pp_lib) cclib + exit rc From a1c50d3ef4bed0c1988ee2ef5fe076021838cea9 Mon Sep 17 00:00:00 2001 From: Pierre Villemot Date: Tue, 30 Jul 2024 14:37:08 +0200 Subject: [PATCH 04/10] clean up --- src/bin/text/dune | 2 +- src/bin/text/gen_link_flags.ml | 99 ++++++++++------------------------ 2 files changed, 28 insertions(+), 73 deletions(-) diff --git a/src/bin/text/dune b/src/bin/text/dune index c1c868de4..846158757 100644 --- a/src/bin/text/dune +++ b/src/bin/text/dune @@ -12,7 +12,7 @@ (deps (:gen gen_link_flags.exe)) (action (with-stdout-to link_flags.dune - (run %{gen} --link-mode %{env:LINK_MODE=dynamic} --os %{ocaml-config:system})))) + (run %{gen} %{env:LINK_MODE=dynamic} %{ocaml-config:system})))) (executable (name Main_text) diff --git a/src/bin/text/gen_link_flags.ml b/src/bin/text/gen_link_flags.ml index 14b98491a..e275ecc71 100644 --- a/src/bin/text/gen_link_flags.ml +++ b/src/bin/text/gen_link_flags.ml @@ -1,43 +1,3 @@ -type os = Linux | Macos -type link_mode = Dynamic | Static | Mixed - -module Cmd = struct - open Cmdliner - - let show l = List.map (fun (s, _) -> s) l - - let os_term = - let all = [ - ("linux", Linux); - ("macosx", Macos) - ] - in - let doc = - Fmt.str "Choose the operating system, $(docv) must be %s." - (Arg.doc_alts @@ show all) - in - Arg.(value & opt (enum all) Linux & info ["os"] ~docv:"OS" ~doc) - - let link_mode_term = - let all = [ - ("dynamic", Dynamic); - ("static", Static); - ("mixed", Mixed) - ] - in - let doc = - Fmt.str "Choose the operating system, $(docv) must be %s." - (Arg.doc_alts @@ show all) - in - Arg.(value & opt (enum all) Dynamic & - info ["link-mode"] ~docv:"MODE" ~doc) - - let parse k = - let info = Cmd.info "rewrite-gen-link-flags" in - Cmd.v info Term.(ret (const k $ link_mode_term $ os_term)) - |> Cmd.eval -end - let pkgconfig lib archive = let cmd = Fmt.str "pkg-config %s --variable libdir" lib in let output = @@ -49,7 +9,6 @@ let pkgconfig lib archive = let pp_lib ppf s = Fmt.pf ppf "-cclib %s" s - let () = let mixed_flags = ["-noautolink"] in let mixed_cclib = [ @@ -62,35 +21,31 @@ let () = ] in let libs = ["gmp"] in - let rc = - Cmd.parse @@ fun link_mode os -> - 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, Macos -> - 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 and OS" - in - Fmt.pr "@[(-linkall %a %a)@]" - Fmt.(list ~sep:sp string) flags - Fmt.(list ~sep:sp pp_lib) cclib; - `Ok () + 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 - exit rc + Fmt.pr "@[(-linkall %a %a)@]" + Fmt.(list ~sep:sp string) flags + Fmt.(list ~sep:sp pp_lib) cclib; From b11fa2bff2b6c0ef84e0eae8db85708d4cd45528 Mon Sep 17 00:00:00 2001 From: Pierre Villemot Date: Tue, 30 Jul 2024 14:45:26 +0200 Subject: [PATCH 05/10] remove old scripts --- src/bin/text/gen-link-flags.sh | 56 ---------------------------------- 1 file changed, 56 deletions(-) delete mode 100755 src/bin/text/gen-link-flags.sh 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 ')' From 8eaf2e029d733d00eff723c7a6764e76d20063ac Mon Sep 17 00:00:00 2001 From: Pierre Villemot Date: Tue, 30 Jul 2024 14:55:43 +0200 Subject: [PATCH 06/10] restore note --- src/bin/text/gen_link_flags.ml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/bin/text/gen_link_flags.ml b/src/bin/text/gen_link_flags.ml index e275ecc71..77e4a93d3 100644 --- a/src/bin/text/gen_link_flags.ml +++ b/src/bin/text/gen_link_flags.ml @@ -11,6 +11,8 @@ 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"; From dd336d3c7924b52331df27b7f8f334acc333cef6 Mon Sep 17 00:00:00 2001 From: Pierre Villemot Date: Tue, 30 Jul 2024 15:02:55 +0200 Subject: [PATCH 07/10] Use stdcompat.In_channel --- src/bin/text/gen_link_flags.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/text/gen_link_flags.ml b/src/bin/text/gen_link_flags.ml index 77e4a93d3..a3a4866fd 100644 --- a/src/bin/text/gen_link_flags.ml +++ b/src/bin/text/gen_link_flags.ml @@ -2,7 +2,7 @@ let pkgconfig lib archive = let cmd = Fmt.str "pkg-config %s --variable libdir" lib in let output = Unix.open_process_in cmd - |> In_channel.input_line + |> Stdcompat.In_channel.input_line |> Option.get in Fmt.str "%s/%s" output archive From 137059144d945e7fa91678ab1cf8ad8b70f491bb Mon Sep 17 00:00:00 2001 From: Pierre Villemot Date: Tue, 30 Jul 2024 15:08:21 +0200 Subject: [PATCH 08/10] Use Stdlib.input_line --- src/bin/text/gen_link_flags.ml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/bin/text/gen_link_flags.ml b/src/bin/text/gen_link_flags.ml index a3a4866fd..97f7839b0 100644 --- a/src/bin/text/gen_link_flags.ml +++ b/src/bin/text/gen_link_flags.ml @@ -1,10 +1,6 @@ let pkgconfig lib archive = let cmd = Fmt.str "pkg-config %s --variable libdir" lib in - let output = - Unix.open_process_in cmd - |> Stdcompat.In_channel.input_line - |> Option.get - 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 From 400e8ff9365deb67cc06c29097315b1372bfc6ba Mon Sep 17 00:00:00 2001 From: Pierre Villemot Date: Wed, 31 Jul 2024 13:03:02 +0200 Subject: [PATCH 09/10] review changes --- src/bin/text/dune | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/bin/text/dune b/src/bin/text/dune index 846158757..772f17ea4 100644 --- a/src/bin/text/dune +++ b/src/bin/text/dune @@ -3,16 +3,14 @@ (executable (name gen_link_flags) - (libraries unix fmt stdcompat cmdliner) - (modules gen_link_flags) - (promote (until-clean))) + (public_name gen_link_flags) + (package alt-ergo) + (libraries unix fmt stdcompat) + (modules gen_link_flags)) (rule - (target link_flags.dune) - (deps (:gen gen_link_flags.exe)) - (action - (with-stdout-to link_flags.dune - (run %{gen} %{env:LINK_MODE=dynamic} %{ocaml-config:system})))) + (with-stdout-to link_flags.dune + (run %{bin:gen_link_flags} %{env:LINK_MODE=dynamic} %{ocaml-config:system}))) (executable (name Main_text) From 2985a27effd1cc767f50b6e5757df84b681b66db Mon Sep 17 00:00:00 2001 From: Pierre Villemot Date: Wed, 31 Jul 2024 14:41:25 +0200 Subject: [PATCH 10/10] no promotion --- src/bin/text/dune | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/bin/text/dune b/src/bin/text/dune index 772f17ea4..c913b5711 100644 --- a/src/bin/text/dune +++ b/src/bin/text/dune @@ -3,14 +3,12 @@ (executable (name gen_link_flags) - (public_name gen_link_flags) - (package alt-ergo) (libraries unix fmt stdcompat) (modules gen_link_flags)) (rule (with-stdout-to link_flags.dune - (run %{bin:gen_link_flags} %{env:LINK_MODE=dynamic} %{ocaml-config:system}))) + (run ./gen_link_flags.exe %{env:LINK_MODE=dynamic} %{ocaml-config:system}))) (executable (name Main_text)