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

Rewrite gen_link_flags in OCaml #1190

Closed
Closed
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
13 changes: 11 additions & 2 deletions src/bin/text/dune
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
(documentation
(package alt-ergo))

(executable
(name gen_link_flags)
(libraries unix fmt cmdliner)
(modules gen_link_flags)
(promote (until-clean)))

(rule
(with-stdout-to link_flags.dune
(run ./gen-link-flags.sh %{env:LINK_MODE=dynamic} %{ocaml-config:system})))
(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}))))

(executable
(name Main_text)
Expand Down
56 changes: 0 additions & 56 deletions src/bin/text/gen-link-flags.sh

This file was deleted.

58 changes: 58 additions & 0 deletions src/bin/text/gen_link_flags.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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 starts_with ~prefix s =
let open String in
let len_s = length s
and len_pre = length prefix in
let rec aux i =
if i = len_pre then true
else if unsafe_get s i <> unsafe_get prefix i then false
else aux (i + 1)
in len_s >= len_pre && aux 0

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 = [
"-lcamlzip";
"-lnums";
"-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 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
Loading