Skip to content

Commit cd9595a

Browse files
authored
boot: add a --static option to build a static dune (ocaml#10528)
This options adds `-ccopt -static` to the link flags (this requires a libc that supports static linking). The motivation is `nix build .#dune-static`, which otherwise requires patching sources. Signed-off-by: Etienne Millon <[email protected]>
1 parent 5904a00 commit cd9595a

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

boot/bootstrap.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ let keep_generated_files =
1717
; ( "--force-byte-compilation"
1818
, Arg.Unit ignore
1919
, " Force bytecode compilation even if ocamlopt is available" )
20+
; "--static", Arg.Unit ignore, " Build a static binary"
2021
]
2122
anon
2223
"Usage: ocaml bootstrap.ml <options>\nOptions are:";

boot/duneboot.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
(** {2 Command line} *)
22

3-
let concurrency, verbose, debug, secondary, force_byte_compilation =
3+
let concurrency, verbose, debug, secondary, force_byte_compilation, static =
44
let anon s = raise (Arg.Bad (Printf.sprintf "don't know what to do with %s\n" s)) in
55
let concurrency = ref None in
66
let verbose = ref false in
77
let prog = Filename.basename Sys.argv.(0) in
88
let debug = ref false in
99
let secondary = ref false in
1010
let force_byte_compilation = ref false in
11+
let static = ref false in
1112
Arg.parse
1213
[ "-j", Int (fun n -> concurrency := Some n), "JOBS Concurrency"
1314
; "--verbose", Set verbose, " Set the display mode"
@@ -17,10 +18,11 @@ let concurrency, verbose, debug, secondary, force_byte_compilation =
1718
; ( "--force-byte-compilation"
1819
, Set force_byte_compilation
1920
, " Force bytecode compilation even if ocamlopt is available" )
21+
; "--static", Set static, " Build a static binary"
2022
]
2123
anon
2224
(Printf.sprintf "Usage: %s <options>\nOptions are:" prog);
23-
!concurrency, !verbose, !debug, !secondary, !force_byte_compilation
25+
!concurrency, !verbose, !debug, !secondary, !force_byte_compilation, !static
2426
;;
2527

2628
(** {2 General configuration} *)
@@ -1123,6 +1125,7 @@ let build
11231125
| ".ml" -> Some (Filename.remove_extension fn ^ compiled_ml_ext)
11241126
| _ -> None)
11251127
in
1128+
let static_flags = if static then [ "-ccopt"; "-static" ] else [] in
11261129
write_args "compiled_ml_files" compiled_ml_files;
11271130
Process.run
11281131
~cwd:build_dir
@@ -1132,6 +1135,7 @@ let build
11321135
; obj_files
11331136
; [ "-args"; "compiled_ml_files" ]
11341137
; link_flags
1138+
; static_flags
11351139
; allow_unstable_sources
11361140
])
11371141
;;

flake.nix

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,7 @@
4747
ocamlPackages = super.ocaml-ng.ocamlPackages_4_14.overrideScope (oself: osuper: {
4848
dune_3 = osuper.dune_3.overrideAttrs (a: {
4949
src = ./.;
50-
postPatch = ''
51-
substituteInPlace \
52-
boot/duneboot.ml \
53-
--replace-fail \
54-
'; link_flags' \
55-
'; link_flags; ["-ccopt"; "-static"]'
56-
'';
50+
preBuild = "ocaml boot/bootstrap.ml --static";
5751
});
5852
});
5953
};

0 commit comments

Comments
 (0)