Skip to content

Commit

Permalink
Rewrite gen-link-flags in OCaml
Browse files Browse the repository at this point in the history
  • Loading branch information
Halbaroth committed Jul 30, 2024
1 parent 7c472b9 commit 55a2928
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
11 changes: 10 additions & 1 deletion 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 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)
Expand Down
69 changes: 69 additions & 0 deletions src/bin/text/gen_link_flags.ml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 55a2928

Please sign in to comment.