-
Notifications
You must be signed in to change notification settings - Fork 25
/
make.ml
866 lines (797 loc) · 26.6 KB
/
make.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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
(* This file is distributed under the terms and conditions of the GNU GPL
(General Public License), as detailed in the file LICENSE.
Copyright (C) 2016 by Gerd Stolpmann.
*)
(* Run this as:
ocaml make.ml <args>
This is a little "make"-like utility. It is not fully implemented, and
mainly intended to help building omake under Windows.
*)
(* TODO:
- globbing for sources
- emulate cp, rm, ln, echo for Windows
- suffix substitution
*)
#warnings "-3";;
#load "str.cma";;
#load "unix.cma";;
open Printf
module StrMap = Map.Make(String)
module StrSet = Set.Make(String)
type exp_rule =
{ targets : string list;
sources : string list;
commands : string list; (* reverse order *)
}
type suffix_rule =
{ target_suffix : string;
source_suffix : string;
sfx_commands : string list; (* reverse order *)
}
type rule =
| Explicit of exp_rule
| DblColon of exp_rule
| Suffix of suffix_rule
type rules =
{ exp_rules : exp_rule StrMap.t; (* keyed by target *)
dblcolon_rules : exp_rule list StrMap.t; (* keyed by target; rev order *)
suffix_rules : suffix_rule list;
enabled_suffixes : StrSet.t;
phonies : StrSet.t;
default : string list;
}
let empty_rules =
{ exp_rules = StrMap.empty;
dblcolon_rules = StrMap.empty;
suffix_rules = [];
enabled_suffixes = StrSet.empty;
phonies = StrSet.empty;
default = [];
}
type expr =
| Concat of expr list
| Literal of string
| Variable of string
| Shell of expr
type varvalue =
| Expanded of string
| Unexpanded of expr
type varmap = varvalue StrMap.t (* keyed by var name *)
let debug_enabled = ref false
let debug s =
if !debug_enabled then
print_endline s
let catch_not_found f arg ~on_result ~on_not_found =
match
try Some(f arg) with Not_found -> None
with
| Some r -> on_result r
| None -> on_not_found()
let starts_with s u =
let sl = String.length s in
let ul = String.length u in
sl >= ul && String.sub s 0 ul = u
let multi_index s k charlist =
let l = String.length s in
let rec search k =
if k >= l then
raise Not_found;
let c = s.[k] in
if List.mem c charlist then
k
else
search (k+1) in
search k
let read_data ch =
(* textual data *)
let buf = Buffer.create 1024 in
try
while true do
let line = input_line ch in
if Buffer.length buf > 0 then
Buffer.add_char buf '\n';
let n = String.length line in
if n > 0 && line.[n-1] = '\r' then
Buffer.add_substring buf line 0 (n-1)
else
Buffer.add_string buf line
done;
assert false
with
| End_of_file ->
Buffer.contents buf
let ws_re = Str.regexp "[ \t\r\n]+"
let wildcard_re = Str.regexp "\\*\\(\\.[a-zA-Z0-9]+\\)"
let expand_command cmd =
(* Windows shells do not expand wildcards, so we need to do it here.
The following is super-primitive and only covers the form *.suffix.
Quoting is not taken into account.
*)
if Sys.os_type = "Win32" then
let words = Str.split ws_re cmd in
if List.exists (fun w -> Str.string_match wildcard_re w 0) words then
let words' =
List.flatten
(List.map
(fun word ->
if Str.string_match wildcard_re word 0 then
let suffix = Str.matched_group 1 word in
List.filter
(fun file -> Filename.check_suffix file suffix)
(Array.to_list(Sys.readdir "."))
else
[word]
)
words
) in
String.concat " " words'
else
cmd
else
cmd
let command_output cmd =
let ch = Unix.open_process_in (expand_command cmd) in
try
let data = read_data ch in
let status = Unix.close_process_in ch in
match status with
| Unix.WEXITED 0 ->
data
| Unix.WEXITED n ->
failwith ("Command exited with error: " ^ cmd)
| _ ->
failwith ("Command exited with signal: " ^ cmd)
with
| Unix.Unix_error(code,_,name) ->
ignore(Unix.close_process_in ch);
let prefix =
if name = "" then "" else name ^ ": " in
raise (Sys_error(prefix ^ Unix.error_message code))
let rec eval_expr active env expr =
match expr with
| Concat l ->
let l' = List.map (eval_expr active env) l in
String.concat "" l'
| Literal s ->
s
| Variable name ->
if StrSet.mem name active then
failwith ("Recursive variable: " ^ name);
let value =
try StrMap.find name env
with Not_found ->
try Expanded(Sys.getenv name)
with Not_found ->
Expanded "" in
( match value with
| Expanded s -> s
| Unexpanded expr -> eval_expr (StrSet.add name active) env expr
)
| Shell expr ->
let cmd = eval_expr active env expr in
command_output cmd
let rec parse_expr s =
let l = String.length s in
let literal_list k0 k1 =
if k1 > k0 then
[ Literal(String.sub s k0 (k1-k0)) ]
else
[] in
let rec find_dollar delims onend acc k =
let delimchars = List.map fst delims in
catch_not_found
(multi_index s k) ('$' :: delimchars)
~on_result:(
fun d ->
let c = s.[d] in
if c = '$' then (
if d+1 >= l then failwith "Bad expression: '$' at end";
let litl = literal_list k d in
let c = s.[d+1] in
match c with
| '$' ->
let acc' = [ Literal "$" ] @ litl @ acc in
find_dollar delims onend acc' (d+2)
| '(' ->
let acc' = litl @ acc in
find_varend delims onend acc' ')' (d+2)
| '{' ->
let acc' = litl @ acc in
find_varend delims onend acc' '}' (d+2)
| _ ->
let name = String.make 1 c in
let acc' = [ Variable name ] @ litl @ acc in
find_dollar delims onend acc' (d+2)
) else
let litl = literal_list k d in
let f = List.assoc c delims in
f (litl @ acc) (d+1)
)
~on_not_found:(
fun () ->
let acc' = literal_list k l @ acc in
onend acc'
)
and find_varend old_delims old_onend acc cend k =
catch_not_found
(multi_index s k) [cend; ' '; '\t'; '('; '{'; '$']
~on_result:(
fun e ->
let c = s.[e] in
if c = cend then
let name = String.sub s k (e-k) in
let acc' = [ Variable name ] @ acc in
find_dollar old_delims old_onend acc' (e+1)
else
match c with
| ' '
| '\t' ->
let name = String.sub s k (e-k) in
if name = "shell" then
let new_delims =
[ cend, (fun subexpr p ->
let acc' =
Shell (Concat (List.rev subexpr)) :: acc in
find_dollar old_delims old_onend acc' p
)
] @ old_delims in
let new_onend _ =
failwith "Bad expression: unterminated shell command" in
find_dollar new_delims new_onend [] (e+1)
else
failwith ("Bad expression: unsupported function: " ^
name)
| _ ->
Printf.eprintf "Char: %c\n%!" c;
failwith "Bad expression: variable ref contains bad char"
)
~on_not_found:(fun () ->
failwith "Bad expression: unterminated variable ref"
)
in
find_dollar [] (fun l -> Concat(List.rev l)) [] 0
let expand env s =
let expr = parse_expr s in
eval_expr StrSet.empty env expr
let ws_re =
Str.regexp "^[ \t\t\n]*$";;
let set_var_re =
Str.regexp "^\\([^ +?=:]+\\)[ \t]*\\([:?+]?=\\)[ \t]*\\(.*\\)$"
let dblcolon_rule_re =
Str.regexp "^\\([^:]+\\)::\\(.*\\)$"
let rule_re =
Str.regexp "^\\([^:]+\\):\\(.*\\)$"
let include_re =
Str.regexp "^include[ \t]+\\([^ \t].*\\)$"
let split_ws_re =
Str.regexp "[ \t\r\n]+"
let rec fold_lines fh f number acc =
match
try Some(input_line fh)
with End_of_file -> None
with
| Some line ->
let acc' = f number line acc in
fold_lines fh f (number+1) acc'
| None ->
acc
let update_env name op value env =
match op with
| "=" ->
let expr = parse_expr value in
StrMap.add name (Unexpanded expr) env
| "?=" ->
let expr = parse_expr value in
if StrMap.mem name env then
env
else
StrMap.add name (Unexpanded expr) env
| "+=" ->
let expr = parse_expr value in
let old_value =
match
try StrMap.find name env
with Not_found -> Unexpanded (Concat [])
with
| Unexpanded e -> e
| Expanded s -> Literal s in
let new_value =
if old_value = Concat [] then
expr
else
Concat [old_value; Literal " "; expr] in
StrMap.add name (Unexpanded new_value) env
| ":=" ->
let expr = parse_expr value in
let value = eval_expr StrSet.empty env expr in
StrMap.add name (Expanded value) env
| _ ->
assert false
let is_empty line =
Str.string_match ws_re line 0
let append_to_rule rule line =
match rule with
| Explicit r -> Explicit { r with commands = line :: r.commands }
| DblColon r -> DblColon { r with commands = line :: r.commands }
| Suffix r -> Suffix { r with sfx_commands = line :: r.sfx_commands }
let same_suffix_rule r1 r2 =
r1.target_suffix = r2.target_suffix &&
r1.source_suffix = r2.source_suffix
let enter_rule1 current env rules =
match current with
| None ->
rules
| Some (Explicit r) ->
if r.commands = [] then
List.fold_left
(fun rules target ->
if StrMap.mem target rules.dblcolon_rules then
failwith "Cannot have both normal and double-colon rules for \
the same target";
if StrMap.mem target rules.exp_rules then
(* add further dependencies to existing explicit rule *)
let r1 = StrMap.find target rules.exp_rules in
let r2 = { r1 with sources = r1.sources @ r.sources } in
{ rules with
exp_rules = StrMap.add target r2 rules.exp_rules
}
else (
debug ("Enter " ^ target);
(* add command-less rule *)
{ rules with
exp_rules = StrMap.add target r rules.exp_rules
}
)
)
rules
r.targets
else (
(* new explicit rule *)
List.iter
(fun target ->
if StrMap.mem target rules.dblcolon_rules then
failwith "Cannot have both normal and double-colon rules for \
the same target";
try
let r = StrMap.find target rules.exp_rules in
if r.commands <> [] then
failwith("Rule for target already exists: " ^ target);
with Not_found -> ()
)
r.targets;
List.fold_left
(fun rules target ->
debug ("Enter " ^ target);
let r1 =
try
let r0 = StrMap.find target rules.exp_rules in
{ r with sources = r0.sources @ r.sources }
with Not_found -> r in
{ rules with
exp_rules = StrMap.add target r1 rules.exp_rules
}
)
rules
r.targets
)
| Some (DblColon r) ->
let target = List.hd r.targets in (* only one target here *)
if StrMap.mem target rules.exp_rules then
failwith "Cannot have both normal and double-colon rules for the \
same target";
( try
let l = StrMap.find target rules.dblcolon_rules in
{ rules with
dblcolon_rules = StrMap.add target (r :: l) rules.dblcolon_rules
}
with
| Not_found ->
{ rules with
dblcolon_rules = StrMap.add target [r] rules.dblcolon_rules
}
)
| Some (Suffix r) ->
if List.exists (same_suffix_rule r) rules.suffix_rules then
failwith ("Suffix rule already exists: " ^
r.source_suffix ^ r.target_suffix);
{ rules with
suffix_rules = r :: rules.suffix_rules
}
let enter_rule current env rules =
let rules = enter_rule1 current env rules in
match current with
| Some (Explicit r) when rules.default = [] ->
{ rules with
default = r.targets
}
| _ ->
rules
let is_suffix_comb suffixes word =
StrSet.fold
(fun source_suffix acc ->
match acc with
| None ->
if starts_with word source_suffix then
let l1 = String.length source_suffix in
let l2 = String.length word in
let target_suffix = String.sub word l1 (l2-l1) in
if StrSet.mem target_suffix suffixes then
Some(target_suffix,source_suffix)
else
None
else
None
| Some _ ->
acc
)
suffixes
None
let match_rule text =
if Str.string_match dblcolon_rule_re text 0 then
let target_str = Str.matched_group 1 text in
let source_str = Str.matched_group 2 text in
Some(`DblColon, target_str, source_str)
else
if Str.string_match rule_re text 0 then
let target_str = Str.matched_group 1 text in
let source_str = Str.matched_group 2 text in
Some(`Colon, target_str, source_str)
else
None
let process_rule env rules' ty target_str source_str =
let target_words0 = Str.split split_ws_re target_str in
(* not handled: glob expressions in sources *)
( match target_words0 with
| [] ->
failwith "Empty target"
| [ ".PHONY" ] ->
if ty = `DblColon then
failwith ".PHONY not possible with double-colon rule";
let source_words =
Str.split split_ws_re (expand env source_str) in
let phonies =
List.fold_left
(fun acc phony -> StrSet.add phony acc)
rules'.phonies
source_words in
let rules'' = { rules' with phonies } in
([],None,env,rules'')
| [ ".SUFFIXES" ] ->
if ty = `DblColon then
failwith ".SUFFIXES not possible with double-colon rule";
let source_words =
Str.split split_ws_re (expand env source_str) in
let enabled_suffixes =
List.fold_left
(fun acc suffix -> StrSet.add suffix acc)
rules'.enabled_suffixes
source_words in
let rules'' = { rules' with enabled_suffixes } in
([],None,env,rules'')
| [ word ] when is_empty source_str ->
( match is_suffix_comb rules'.enabled_suffixes word
with
| Some(target_suffix, source_suffix) ->
if ty = `DblColon then
failwith "Suffix rule not possible with double-colon";
let r =
{ target_suffix;
source_suffix;
sfx_commands = []
} in
([],Some(Suffix r),env,rules')
| None ->
let r =
{ targets = Str.split
split_ws_re
(expand env word);
sources = [];
commands = []
} in
let rule =
match ty with
| `Colon -> Explicit r
| `DblColon -> DblColon r in
([],Some rule,env,rules')
)
| _ ->
let target_words =
Str.split split_ws_re (expand env target_str) in
let source_words =
Str.split split_ws_re (expand env source_str) in
if ty = `DblColon && List.length target_words > 1 then
failwith "Only one target allowed for double-colon rule";
let r =
{ targets = target_words;
sources = source_words;
commands = []
} in
let rule =
match ty with
| `Colon -> Explicit r
| `DblColon -> DblColon r in
([],Some rule,env,rules')
)
let rec parse_statements ?(exec = fun _ _ _ -> ()) env rules file =
let fh = open_in file in
let last_num = ref 0 in
try
let (preceding,current,env,rules) =
fold_lines
fh
(fun number line (preceding,current,env,rules) ->
last_num := number;
let lline = String.length line in
if preceding = [] && (is_empty line || line.[0] = '#') then
(preceding,current,env,rules)
else
if line <> "" && line.[lline - 1] = '\\' then
let data = String.sub line 0 (lline-1) in
(data::preceding,current,env,rules)
else
let multiline =
String.concat "" (List.rev (line :: preceding)) in
if multiline.[0] = '\t' then
match current with
| None ->
failwith "TAB found outside a rule definition"
| Some rule ->
let current' =
Some (append_to_rule rule multiline) in
([],current',env,rules)
else
let rules' =
enter_rule current env rules in
if Str.string_match set_var_re multiline 0 then
let name = Str.matched_group 1 multiline in
let op = Str.matched_group 2 multiline in
let value = Str.matched_group 3 multiline in
let env' = update_env name op value env in
([],None,env',rules')
else
match match_rule multiline with
| Some(ty, target_str, source_str) ->
process_rule env rules' ty target_str source_str
| None ->
if Str.string_match include_re multiline 0 then (
let target = Str.matched_group 1 multiline in
exec env rules target;
let (env',rules'') =
parse_statements ~exec env rules' target in
([],None,env',rules'')
)
else
failwith "Cannot parse line"
)
1
([],None,env,rules) in
if preceding <> [] then
failwith "Last line ends with backslash";
let rules =
enter_rule current env rules in
(env, rules)
with
| Failure msg ->
close_in fh;
failwith ("File " ^ file ^ ", line " ^ string_of_int !last_num ^ ": " ^
msg)
| error ->
close_in fh;
raise error
let strip_spaces_re = Str.regexp "^[ \t]*\\(.*\\)$"
let run_command1 cmd =
if is_empty cmd then
()
else
(* CHECK order: @- or -@ *)
let no_echo, cmd1 =
if cmd <> "" && cmd.[0] = '@' then
true, String.sub cmd 1 (String.length cmd - 1)
else
false, cmd in
let no_errcheck, cmd2 =
if cmd1 <> "" && cmd1.[0] = '-' then
true, String.sub cmd1 1 (String.length cmd1 - 1)
else
false, cmd1 in
if not no_echo then
print_endline cmd2;
let code = Sys.command (expand_command cmd2) in
if not no_errcheck && code <> 0 then
failwith ("Command failed: " ^ cmd2)
let run_command s =
if Str.string_match strip_spaces_re s 0 then
let cmd = Str.matched_group 1 s in
run_command1 cmd
else
run_command1 s
let exec made_targets env rules target =
let rec make ancestors target =
debug ("Make: " ^ target);
if not (StrSet.mem target !made_targets) then (
match search_rule ancestors target with
| Some list ->
List.iter
(fun (r,local_env) ->
let env' =
List.fold_left
(fun acc (n,v) -> StrMap.add n v acc)
env
local_env in
let ancestors' =
StrSet.add target ancestors in
List.iter (make ancestors') r.sources;
let cmds1 = List.rev r.commands in
let cmds2 = List.map (expand env') cmds1 in
List.iter run_command cmds2;
List.iter
(fun tgt ->
made_targets := StrSet.add tgt !made_targets
)
r.targets
)
list
| None ->
if StrSet.mem target rules.phonies then
failwith("No way to execute phony target: " ^ target);
if not(Sys.file_exists target) then
failwith("No way to make target: " ^ target)
);
debug ("Made: " ^ target);
and search_rule ancestors target =
if StrSet.mem target ancestors then
None
else
try
let r = StrMap.find target rules.exp_rules in
if r.commands = [] then raise Not_found;
let localenv =
[ "@", (Expanded (String.concat " " r.targets));
"<", (Expanded (String.concat " " r.sources));
] in
debug (" target: " ^ target ^ " = direct");
Some [r, localenv]
with
| Not_found ->
try
(* Double-colon rules are always executed if r.sources=[].
Well, we always execute anyway, so this does not make a
difference.
*)
let rlist = StrMap.find target rules.dblcolon_rules in
debug (" target: " ^ target ^ " = double-colon");
Some
(List.map
(fun r ->
let localenv =
[ "@", (Expanded (String.concat " " r.targets));
"<", (Expanded (String.concat " " r.sources));
] in
(r,localenv)
)
rlist
)
with
| Not_found ->
search_implicit_rule ancestors target
and search_implicit_rule ancestors target =
debug (" target: " ^ target ^ " = search");
let r_opt =
StrSet.fold
(fun target_suffix rule_opt ->
match rule_opt with
| Some r -> Some r
| None ->
if Filename.check_suffix target target_suffix then (
debug(" target_suffix: " ^ target_suffix);
let irules =
List.filter
(fun suffr ->
suffr.target_suffix = target_suffix
)
rules.suffix_rules in
if irules = [] then
None
else (
let target_base =
Filename.chop_suffix target target_suffix in
try
let ancestors' =
StrSet.add target ancestors in
let irule =
List.find
(fun suffr ->
debug(" source_suffix: " ^ suffr.source_suffix);
let source =
target_base ^ suffr.source_suffix in
search_rule ancestors' source <> None
|| Sys.file_exists source
)
irules in
let source =
target_base ^ irule.source_suffix in
let deps =
try
let r = StrMap.find target rules.exp_rules in
r.sources
with
| Not_found -> [] in
Some
[ { targets = [target];
sources = source :: deps;
commands = irule.sfx_commands
},
[ "*", (Expanded target_base);
"@", (Expanded target);
"<", (Expanded source);
]
]
with Not_found ->
None
)
)
else
None
)
rules.enabled_suffixes
None in
r_opt in
try
make StrSet.empty target
with
| Failure msg ->
let d = Sys.getcwd() in
failwith ("*** Error in directory " ^ d ^ ": " ^ msg)
let set_re = Str.regexp "^\\([^=]+\\)=\\(.*\\)$"
let main() =
let self = Sys.argv.(0) in
let self =
if Filename.is_relative self then
Filename.concat (Sys.getcwd()) self
else
self in
let env = ref StrMap.empty in
let targets = ref [] in
let setvar name value =
env := StrMap.add name (Expanded value) !env in
Arg.parse
[ "-C", Arg.String (fun s -> Sys.chdir s),
"<dir> Change to this directory";
"-debug", Arg.Set debug_enabled,
" Enable debug logging";
]
(fun s ->
if Str.string_match set_re s 0 then
let name = Str.matched_group 1 s in
let value = Str.matched_group 2 s in
setvar name value
else
targets := !targets @ [s]
)
"ocaml make.ml [options] (var=value | target) ...";
setvar
"MAKE"
(sprintf "ocaml %s %s"
self
(if !debug_enabled then "-debug" else "")
);
let made_targets = ref StrSet.empty in
let (env,rules) =
parse_statements ~exec:(exec made_targets) !env empty_rules "Makefile" in
debug("Suffixes: " ^
String.concat "," (StrSet.elements rules.enabled_suffixes));
if !targets = [] then
targets := rules.default;
List.iter
(exec made_targets env rules)
!targets
let () =
try main()
with
| Failure msg
| Arg.Bad msg
| Sys_error msg ->
flush stdout;
prerr_endline msg;
flush stderr;
exit 1