Skip to content

Commit 7fc1e88

Browse files
committed
static link
1 parent eb85486 commit 7fc1e88

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

config/discover.ml

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module C = Configurator.V1
22

3+
[@@@warning "-40"]
4+
35
module List = struct
46
(* Compatibility with OCaml < 4.10 *)
57
let [@warning "-32"] rec concat_map f = function
@@ -389,16 +391,34 @@ let extract_all_declarations c ~cflags ~libs =
389391
extract_declarations c ~cflags ~libs ~declarations:curl_h_declarations ~includes:[ "#include <curl/curl.h>" ]
390392
@ extract_declarations c ~cflags ~libs ~declarations:header_h_declarations ~includes:[ "#include <curl/curl.h>"; "#include <curl/header.h>" ]
391393

394+
(* naive (what if some lib requires -L) *)
395+
let dynamic over libs =
396+
(* NB adding all as dynamic, not only the ones present, because libcurl.pc misses sasl dep *)
397+
let (_over,libs) = List.partition (fun l -> List.mem l over) libs in
398+
(if over <> [] then ["-Wl,--push-state"; "-Wl,-Bdynamic"] @ over @ ["-Wl,--pop-state"] else []) @ libs
399+
400+
(* basically Pkg_config.query_expr_err with --static and no smartness finding pkg-config itself (it is up to the host owner to set it up properly) *)
401+
let pkg_config ~package ?(expr=package) c =
402+
let open C in
403+
let run args =
404+
Flags.extract_blank_separated_words @@ String.trim @@ Process.run_capture_exn c "pkg-config" @@ [ "--print-errors"] @ args @ [ package ]
405+
in
406+
match Process.run c "pkg-config" [ "--print-errors"; expr ] with
407+
| { exit_code = 0; _ } -> Ok { Pkg_config.libs = run ["--static"; "--libs"]; cflags = run ["--static"; "--cflags"] }
408+
| { stderr; _ } -> Error stderr
409+
392410
let main c =
393411
let cflags, libs =
394412
match C.ocaml_config_var c "ccomp_type" with
395413
| Some "cc" ->
396414
let {C.Pkg_config.cflags; libs} =
397415
match C.Pkg_config.get c with
398416
| None -> { cflags = []; C.Pkg_config.libs = [ "-lcurl" ]}
399-
| Some pc ->
400-
(match C.Pkg_config.query_expr_err pc ~package:"libcurl" ~expr:"libcurl >= 7.28.0" with
401-
| Ok c -> c
417+
| Some _pc ->
418+
(match pkg_config c ~package:"libcurl" ~expr:"libcurl >= 7.28.0" with
419+
(* static sasl is unusable (requires a lot of dependencies), p11-kit doesn't provide static lib *)
420+
(* TODO figure the absence of static lib automatically *)
421+
| Ok c -> { c with libs = dynamic ["-lsasl2";"-lp11-kit"] c.libs }
402422
| Error err -> C.die "%s" err)
403423
in
404424
"-Wall" :: "-Wno-deprecated-declarations" :: cflags, libs

examples/dune

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
test_memory_leaks
88
test_enum)
99
(libraries curl unix)
10+
; static link (except system libs)
11+
(link_flags -cc "%{cc} -Wl,-Bstatic" -cclib -Wl,-Bdynamic)
1012
(modules
1113
ocurl
1214
ominimal

0 commit comments

Comments
 (0)