forked from ocaml/dune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.ml
565 lines (513 loc) · 15.2 KB
/
bootstrap.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
[@@@ocaml.warning "-40"]
(* This module is here to build a version of jbuilder that is capable of
* building itself. It accomplishes this by concatenating all its source files
* into a single .ml file and simply compiling it. The source code of the
* vendored libraries are omitted, being replaced by stubs, just to speed up
* the bootstrapping process. This is possible because the features used in
* jbuilder's jbuild files use a minimal set of features that do not actually
* hit codepaths in which the vendored libraries are used. In order for this to
* continue to work, jbuild files in the jbuilder repository should not use
* globs. *)
module Array = ArrayLabels
module List = ListLabels
module String = struct
include StringLabels
include struct
[@@@warning "-3"]
let capitalize_ascii = String.capitalize
let uncapitalize_ascii = String.uncapitalize
end
let break s ~pos =
(sub s ~pos:0 ~len:pos,
sub s ~pos ~len:(String.length s - pos))
end
(* Directories with library names *)
let dirs =
[ "src/wp" , Some "Wp"
; "src/stdune/result" , Some "Dune_result"
; "src/stdune/caml" , Some "Dune_caml"
; "src/stdune" , Some "Stdune"
; "src/fiber" , Some "Fiber"
; "src/xdg" , Some "Xdg"
; "vendor/incremental-cycles/src" , Some "Incremental_cycles"
; "src/dag" , Some "Dag"
; "src/memo" , Some "Memo"
; "src/ocaml-config" , Some "Ocaml_config"
; "vendor/boot" , None
; "src/dune_lang" , Some "Dune_lang"
; "src" , None
]
open Printf
module String_set = Set.Make(String)
module Make_map(Key : Map.OrderedType) = struct
include Map.Make(Key)
let of_alist_multi l =
List.fold_left (List.rev l) ~init:empty ~f:(fun acc (k, v) ->
let l =
try
find k acc
with Not_found ->
[]
in
add k (v :: l) acc)
end
module String_map = Make_map(String)
module String_option_map = Make_map(struct type t = string option let compare = compare end)
let () =
match Sys.getenv "OCAMLPARAM" with
| s -> Printf.eprintf "OCAMLPARAM is set to %S\n%!" s
| exception Not_found -> ()
let ( ^/ ) = Filename.concat
let protectx x ~finally ~f =
match f x with
| y -> finally x; y
| exception e -> finally x; raise e
let starts_with s ~prefix =
let plen = String.length prefix in
let slen = String.length s in
slen >= plen && String.sub s ~pos:0 ~len:plen = prefix
let split_words s =
let rec skip_blanks i =
if i = String.length s then
[]
else
match s.[i] with
| ' ' | '\t' -> skip_blanks (i + 1)
| _ -> parse_word i (i + 1)
and parse_word i j =
if j = String.length s then
[String.sub s ~pos:i ~len:(j - i)]
else
match s.[j] with
| ' ' | '\t' -> String.sub s ~pos:i ~len:(j - i) :: skip_blanks (j + 1)
| _ -> parse_word i (j + 1)
in
skip_blanks 0
let read_lines fn =
let ic = open_in fn in
let rec loop ic acc =
match try Some (input_line ic) with End_of_file -> None with
| Some line -> loop ic (line :: acc)
| None -> List.rev acc
in
let lines = loop ic [] in
close_in ic;
lines
let exec fmt =
ksprintf (fun cmd ->
print_endline cmd;
Sys.command cmd)
fmt
let path_sep =
if Sys.win32 then
';'
else
':'
let split_path s =
let rec loop i j =
if j = String.length s then
[String.sub s ~pos:i ~len:(j - i)]
else if s.[j] = path_sep then
String.sub s ~pos:i ~len:(j - i) :: loop (j + 1) (j + 1)
else
loop i (j + 1)
in
loop 0 0
let path =
match Sys.getenv "PATH" with
| exception Not_found -> []
| s -> split_path s
let exe = if Sys.win32 then ".exe" else ""
let prog_not_found prog =
eprintf "Program %s not found in PATH" prog;
exit 2
let best_prog dir prog =
let fn = dir ^/ prog ^ ".opt" ^ exe in
if Sys.file_exists fn then
Some fn
else
let fn = dir ^/ prog ^ exe in
if Sys.file_exists fn then
Some fn
else
None
let find_prog prog =
let rec search = function
| [] -> None
| dir :: rest ->
match best_prog dir prog with
| None -> search rest
| Some fn -> Some (dir, fn)
in
search path
let get_prog dir prog =
match best_prog dir prog with
| None -> prog_not_found prog
| Some fn -> fn
let bin_dir, ocamlc =
match find_prog "ocamlc" with
| None -> prog_not_found "ocamlc"
| Some x -> x
let ocamlopt = best_prog bin_dir "ocamlopt"
let to_delete = ref []
let add_to_delete fn =
to_delete := fn :: !to_delete
let () =
at_exit (fun () ->
List.iter !to_delete ~f:(fun fn ->
try Sys.remove fn with _ -> ()))
let ocamllex = get_prog bin_dir "ocamllex"
let ocamldep = get_prog bin_dir "ocamldep"
let run_ocamllex src =
let dst = String.sub src ~pos:0 ~len:(String.length src - 1) in
let x = Sys.file_exists dst in
let n = exec "%s -q %s" ocamllex src in
if n <> 0 then exit n;
if not x then add_to_delete dst;
dst
let copy a b =
printf "cp %s %s\n%!" a b;
let ic = open_in_bin a in
let len = in_channel_length ic in
let s = really_input_string ic len in
close_in ic;
let oc = open_out_bin b in
fprintf oc "# 1 %S\n" a;
output_string oc s;
close_out oc;
add_to_delete b
type module_info =
{ impl : string
; intf : string option
; name : string
; libname : string option
; fqn : string (** Fully qualified name *)
}
let fqn libname mod_name =
match libname with
| None -> mod_name
| Some s -> if s = mod_name then s else s ^ "." ^ mod_name
let cleanup ~keep_ml_file =
try
Array.iter (Sys.readdir ".") ~f:(fun fn ->
if not (Filename.check_suffix fn ".exe") &&
(starts_with fn ~prefix:"boot." ||
starts_with fn ~prefix:"boot_pp.") &&
((fn <> "boot.ml" && fn <> "boot_pp.ml") || not keep_ml_file) then
Sys.remove fn)
with _ ->
()
let compile ~dirs ~generated_file ~exe ~main ~flags ~byte_flags ~native_flags
~pp =
(* Map from module names to ml/mli filenames *)
let modules =
let files_of (dir, libname) =
Sys.readdir dir |> Array.to_list |> List.map ~f:(fun fn ->
(Filename.concat dir fn, libname))
in
let impls, intfs =
List.map dirs ~f:files_of
|> List.concat
|> List.fold_left ~init:(String_map.empty, String_map.empty)
~f:(fun ((impls, intfs) as acc) (fn, libname) ->
let base = Filename.basename fn in
match String.index base '.' with
| exception Not_found -> acc
| i ->
let base, ext = String.break base i in
let is_boot, ext =
match String.rindex ext '.' with
| exception Not_found -> (false, ext)
| i ->
let a, b = String.break ext i in
if a = ".boot" then
(true, b)
else
(false, ext)
in
match ext with
| ".ml" | ".mll" ->
let mod_name = String.capitalize_ascii base in
let fqn = fqn libname mod_name in
if is_boot || not (String_map.mem fqn impls) then
let fn =
if ext = ".mll" then lazy (run_ocamllex fn) else lazy fn
in
(String_map.add fqn (libname, mod_name, fn) impls, intfs)
else
acc
| ".mli" ->
let mod_name = String.capitalize_ascii base in
let fqn = fqn libname mod_name in
if is_boot || not (String_map.mem fqn intfs) then
(impls, String_map.add fqn fn intfs)
else
acc
| _ -> acc)
in
String_map.merge
(fun fqn impl intf ->
match impl with
| None -> None
| Some (libname, name, impl) ->
let impl = Lazy.force impl in
Some { impl
; intf
; name
; libname
; fqn
})
impls intfs
in
let pp =
match pp with
| None -> ""
| Some s -> " -pp " ^ Filename.quote s
in
let read_deps files_by_lib =
let out_fn = "boot-depends.txt" in
add_to_delete out_fn;
List.map files_by_lib ~f:(fun (libname, files) ->
let n =
exec "%s -modules%s %s > %s"
ocamldep
pp
(String.concat ~sep:" " files)
out_fn
in
if n <> 0 then exit n;
List.map (read_lines out_fn) ~f:(fun line ->
let i = String.index line ':' in
let unit =
String.sub line ~pos:0 ~len:i
|> Filename.basename
|> (fun s -> String.sub s ~pos:0 ~len:(String.index s '.'))
|> String.capitalize_ascii
in
let deps =
split_words (String.sub line ~pos:(i + 1)
~len:(String.length line - (i + 1)))
in
let rec resolve deps acc =
match deps with
| [] -> List.rev acc
| dep :: deps ->
let fqn = fqn libname dep in
let acc =
if String_map.mem fqn modules then
fqn :: acc
else if String_map.mem dep modules then
dep :: acc
else
acc
in
resolve deps acc
in
(fqn libname unit, resolve deps [])))
|> List.concat
in
let topsort deps =
let n = List.length deps in
let deps_by_module = Hashtbl.create n in
List.iter deps ~f:(fun (m, deps) ->
Hashtbl.add deps_by_module m deps);
let not_seen = ref (List.map deps ~f:fst |> String_set.of_list) in
let res = ref [] in
let rec loop m =
if String_set.mem m !not_seen then begin
not_seen := String_set.remove m !not_seen;
List.iter (Hashtbl.find deps_by_module m) ~f:loop;
res := m :: !res
end
in
while not (String_set.is_empty !not_seen) do
loop (String_set.choose !not_seen)
done;
List.rev !res
in
let modules_deps =
let files_by_lib =
List.map (String_map.bindings modules) ~f:(fun (_, x) -> (x.libname, x.impl))
|> String_option_map.of_alist_multi
|> String_option_map.bindings
in
read_deps files_by_lib
in
let topsorted_module_names = topsort modules_deps in
let topsorted_libs =
let get_lib m =
match (String_map.find m modules).libname with
| None -> ""
| Some s -> s
in
let libs_deps =
List.map modules_deps ~f:(fun (m, deps) ->
(get_lib m, deps))
|> String_map.of_alist_multi
|> String_map.map
(fun l ->
List.concat l
|> List.map ~f:get_lib
|> String_set.of_list
|> String_set.elements)
|> String_map.bindings
in
List.map (topsort libs_deps) ~f:(function
| "" -> None
| s -> Some s)
in
let generate_file_with_all_the_sources () =
let oc = open_out_bin generated_file in
let in_generated_code = ref true in
let line = ref 0 in
let pr fmt =
ksprintf (fun s ->
if not !in_generated_code then begin
in_generated_code := true;
fprintf oc "# %d %S\n" (!line + 1) generated_file;
incr line;
end;
output_string oc s;
output_char oc '\n';
incr line)
fmt
in
let dump fname =
let ic = open_in fname in
in_generated_code := false;
fprintf oc "# 1 %S\n" fname;
incr line;
let rec loop () =
match input_line ic with
| s ->
output_string oc s;
output_char oc '\n';
incr line;
loop ()
| exception End_of_file ->
close_in ic
in
loop ()
in
pr "let () = Printexc.record_backtrace true";
let modules_by_lib =
List.map topsorted_module_names ~f:(fun m ->
let info = String_map.find m modules in
(info.libname, info))
|> String_option_map.of_alist_multi
in
List.iter topsorted_libs ~f:(fun libname ->
let modules = String_option_map.find libname modules_by_lib in
(match libname with
| None -> ()
| Some s -> pr "module %s = struct" s);
let main, modules =
match List.partition modules ~f:(fun m -> Some m.name = libname) with
| [m], l -> (Some m, l)
| [] , l -> (None , l)
| _ , l -> assert false
in
(match main with
| None -> ()
| Some _ -> pr "module XXXX = struct");
List.iter modules ~f:(fun { name; intf; impl; _ } ->
match intf with
| Some intf ->
pr "module %s : sig" name;
dump intf;
pr "end = struct";
dump impl;
pr "end"
| None ->
pr "module %s = struct" name;
dump impl;
pr "end");
(match main with
| None -> ()
| Some { intf; impl } ->
pr "end";
pr "open XXXX";
match intf with
| Some intf ->
pr "include (struct";
dump impl;
pr "end : sig";
dump intf;
pr "end)"
| None ->
dump impl);
(match libname with
| None -> ()
| Some _ -> pr "end"));
pr main;
close_out oc
in
generate_file_with_all_the_sources ();
let compiler, backend_specific_flags =
match ocamlopt, native_flags with
| Some x, Some y -> (x, y)
| _ -> ocamlc, byte_flags
in
let n =
try exec "%s -g -w -40 -o %s%s %s %s %s"
compiler
exe
pp
flags
backend_specific_flags
generated_file
with e -> cleanup ~keep_ml_file:true; raise e
in
cleanup ~keep_ml_file:(n <> 0);
if n <> 0 then exit n
(* Shims to reuse the selection scripts in src/future-syntax *)
module Sys = struct
include Sys
let argv = [|Sys.argv.(0); Sys.ocaml_version|]
end
let real_print_string = print_string
let cell = ref None
let print_string s =
assert (!cell = None);
cell := Some s
let get () =
match !cell with
| None -> assert false
| Some s -> cell := None; s
;;
#use "src/future-syntax/select-impl";;
let impl = get ();;
#use "src/future-syntax/select-shims";;
let shims = get ();;
let print_string = real_print_string
(* Compile the preprocessor *)
let pp =
if impl = "real" then begin
copy (sprintf "src/future-syntax/pp.%s.ml" impl)
"src/future-syntax/pp.ml";
copy (sprintf "src/future-syntax/shims.%s.ml" shims)
"src/future-syntax/shims.ml";
compile
~generated_file:"boot_pp.ml"
~exe:"boot-pp.exe"
~main:"let () = ()"
~dirs:["src/future-syntax", None]
~flags:"-I +compiler-libs"
~byte_flags:"ocamlcommon.cma"
~native_flags:(Some "ocamlcommon.cmxa")
~pp:None;
add_to_delete "boot-pp.exe";
Some (sprintf "%s -dump-ast" (Filename.concat "." "boot-pp.exe"))
end else
None
(* Compile the bootstrap dune executable *)
let () =
compile
~generated_file:"boot.ml"
~exe:"boot.exe"
~main:"let () = Main.bootstrap ()"
~dirs
~flags:"-I +threads"
~byte_flags:"unix.cma threads.cma"
~native_flags:None
~pp