Skip to content

Commit

Permalink
Rewrite gen_link_flags in OCaml (#1189)
Browse files Browse the repository at this point in the history
  • Loading branch information
Halbaroth authored Jul 31, 2024
1 parent f7796d1 commit d3d48cb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 57 deletions.
7 changes: 6 additions & 1 deletion src/bin/text/dune
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
56 changes: 0 additions & 56 deletions src/bin/text/gen-link-flags.sh

This file was deleted.

49 changes: 49 additions & 0 deletions src/bin/text/gen_link_flags.ml
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit d3d48cb

Please sign in to comment.