Skip to content
Open
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
69 changes: 28 additions & 41 deletions boot/bootstrap.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ open Printf

let min_supported_natively = 4, 08, 0

let keep_generated_files =
let () =
let anon s = raise (Arg.Bad (sprintf "don't know what to do with %s\n" s)) in
let keep_generated_files = ref false in
Arg.parse
[ "-j", Arg.Int ignore, "JOBS Concurrency"
; "--verbose", Arg.Unit ignore, " Set the display mode"
; "--keep-generated-files", Arg.Set keep_generated_files, " Keep generated files"
; "--debug", Arg.Unit ignore, " Enable various debugging options"
; ( "--force-byte-compilation"
, Arg.Unit ignore
Expand All @@ -22,32 +20,10 @@ let keep_generated_files =
]
anon
"Usage: ocaml bootstrap.ml <options>\nOptions are:";
!keep_generated_files
;;

let modules = [ "boot/libs"; "boot/duneboot" ]
let duneboot = ".duneboot"
let prog = duneboot ^ ".exe"

let () =
at_exit (fun () ->
Array.iter (Sys.readdir "boot") ~f:(fun fn ->
let fn = Filename.concat "boot" fn in
if Filename.check_suffix fn ".cmi" || Filename.check_suffix fn ".cmo"
then (
try Sys.remove fn with
| Sys_error _ -> ())));
if not keep_generated_files
then
at_exit (fun () ->
Array.iter (Sys.readdir ".") ~f:(fun fn ->
if
String.length fn >= String.length duneboot
&& String.sub fn ~pos:0 ~len:(String.length duneboot) = duneboot
then (
try Sys.remove fn with
| Sys_error _ -> ())))
;;
let main = "boot/duneboot"
let modules = [ "boot/libs" ]

let runf fmt =
ksprintf
Expand All @@ -69,13 +45,23 @@ let read_file fn =
s
;;

let script chan =
let pwd = Sys.getcwd () in
let directive ~directive_name ~module_ =
let fn = Filename.concat pwd (module_ ^ ".ml") in
fprintf chan "#%s %S;;\n" directive_name fn
in
List.iter modules ~f:(fun module_ -> directive ~directive_name:"mod_use" ~module_);
directive ~directive_name:"use" ~module_:main
;;

let () =
let v = Scanf.sscanf Sys.ocaml_version "%d.%d.%d" (fun a b c -> a, b, c) in
let compiler, which =
if v >= min_supported_natively
then "ocamlc", None
then "ocaml", None
else (
let compiler = "ocamlfind -toolchain secondary ocamlc" in
let compiler = "ocamlfind -toolchain secondary ocaml" in
let output_fn, out = Filename.open_temp_file "duneboot" "ocamlfind-output" in
let n = runf "%s 2>%s" compiler output_fn in
let s = read_file output_fn in
Expand All @@ -97,22 +83,23 @@ let () =
exit 2);
compiler, Some "--secondary")
in
exit_if_non_zero
(runf
"%s %s -g -o %s -I boot %sunix.cma %s"
compiler
(* Make sure to produce a self-contained binary as dlls tend to cause
issues *)
(if v < (4, 10, 1) then "-custom" else "-output-complete-exe")
prog
(if v >= (5, 0, 0) then "-I +unix " else "")
(List.map modules ~f:(fun m -> m ^ ".ml") |> String.concat ~sep:" "));
let script =
let fname, out = Filename.open_temp_file "duneboot" "main" in
script out;
close_out out;
fname
in
let args = List.tl (Array.to_list Sys.argv) in
let args =
match which with
| None -> args
| Some x -> x :: args
in
let args = Filename.concat "." prog :: args in
exit (runf "%s" (String.concat ~sep:" " args))
exit_if_non_zero
(runf
"%s %sunix.cma %s %s"
compiler
(if v >= (5, 0, 0) then "-I +unix " else "")
script
(String.concat ~sep:" " args))
;;
Loading