-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathocaml_actions.ml
1574 lines (1447 loc) · 53 KB
/
ocaml_actions.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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Sebastien Hinderer, projet Gallium, INRIA Paris *)
(* *)
(* Copyright 2017 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(* Actions specific to the OCaml compilers *)
open Ocamltest_stdlib
open Actions
(* Extracting information from environment *)
let no_native_compilers _log env =
(Result.skip_with_reason "native compilers disabled", env)
let native_action a =
if Ocamltest_config.native_compiler then a
else (Actions.update a no_native_compilers)
let get_backend_value_from_env env bytecode_var native_var =
Ocaml_backends.make_backend_function
(Environments.safe_lookup bytecode_var env)
(Environments.safe_lookup native_var env)
let modules env =
Actions_helpers.words_of_variable env Ocaml_variables.modules
let plugins env =
Actions_helpers.words_of_variable env Ocaml_variables.plugins
let directories env =
Actions_helpers.words_of_variable env Ocaml_variables.directories
let directory_flags env =
let f dir = ("-I " ^ dir) in
let l = List.map f (directories env) in
String.concat " " l
let flags env = Environments.safe_lookup Ocaml_variables.flags env
let last_flags env = Environments.safe_lookup Ocaml_variables.last_flags env
let ocamllex_flags env =
Environments.safe_lookup Ocaml_variables.ocamllex_flags env
let ocamlyacc_flags env =
Environments.safe_lookup Ocaml_variables.ocamlyacc_flags env
let filelist env variable extension =
let value = Environments.safe_lookup variable env in
let filenames = String.words value in
let add_extension filename = Filename.make_filename filename extension in
String.concat " " (List.map add_extension filenames)
let libraries backend env =
let extension = Ocaml_backends.library_extension backend in
filelist env Ocaml_variables.libraries extension
let binary_modules backend env =
let extension = Ocaml_backends.module_extension backend in
filelist env Ocaml_variables.binary_modules extension
let backend_default_flags env =
get_backend_value_from_env env
Ocaml_variables.ocamlc_default_flags
Ocaml_variables.ocamlopt_default_flags
let backend_flags env =
get_backend_value_from_env env
Ocaml_variables.ocamlc_flags
Ocaml_variables.ocamlopt_flags
let env_setting env_reader default_setting =
Printf.sprintf "%s=%s"
env_reader.Clflags.env_var
(env_reader.Clflags.print default_setting)
let default_ocaml_env = [|
"TERM=dumb";
env_setting Clflags.color_reader Misc.Color.default_setting;
env_setting Clflags.error_style_reader Misc.Error_style.default_setting;
|]
type module_generator = {
description : string;
command : string;
flags : Environments.t -> string;
generated_compilation_units :
string -> (string * Ocaml_filetypes.t) list
}
let ocamllex =
{
description = "lexer";
command = Ocaml_commands.ocamlrun_ocamllex;
flags = ocamllex_flags;
generated_compilation_units =
fun lexer_name -> [(lexer_name, Ocaml_filetypes.Implementation)]
}
let ocamlyacc =
{
description = "parser";
command = Ocaml_files.ocamlyacc;
flags = ocamlyacc_flags;
generated_compilation_units =
fun parser_name ->
[
(parser_name, Ocaml_filetypes.Interface);
(parser_name, Ocaml_filetypes.Implementation)
]
}
let generate_module generator output_variable input log env =
let basename = fst input in
let input_file = Ocaml_filetypes.make_filename input in
let what =
Printf.sprintf "Generating %s module from %s"
generator.description input_file
in
Printf.fprintf log "%s\n%!" what;
let commandline =
[
generator.command;
generator.flags env;
input_file
] in
let expected_exit_status = 0 in
let exit_status =
Actions_helpers.run_cmd
~environment:default_ocaml_env
~stdin_variable: Ocaml_variables.compiler_stdin
~stdout_variable:output_variable
~stderr_variable:output_variable
~append:true
log env commandline in
if exit_status=expected_exit_status
then generator.generated_compilation_units basename
else begin
let reason =
(Actions_helpers.mkreason
what (String.concat " " commandline) exit_status) in
Printf.fprintf log "%s\n%!" reason;
[]
end
let generate_lexer = generate_module ocamllex
let generate_parser = generate_module ocamlyacc
exception Cannot_compile_file_type of string
let prepare_module output_variable log env input =
let input_type = snd input in
let open Ocaml_filetypes in
match input_type with
| Implementation | Interface | C | Obj -> [input]
| Binary_interface -> [input]
| Backend_specific _ -> [input]
| Lexer ->
generate_lexer output_variable input log env
| Grammar ->
generate_parser output_variable input log env
| Text | C_minus_minus | Other _ ->
raise (Cannot_compile_file_type (string_of_filetype input_type))
let get_program_file backend env =
let testfile = Actions_helpers.testfile env in
let testfile_basename = Filename.chop_extension testfile in
let program_filename =
Filename.mkexe
(Filename.make_filename
testfile_basename (Ocaml_backends.executable_extension backend)) in
let test_build_directory =
Actions_helpers.test_build_directory env in
Filename.make_path [test_build_directory; program_filename]
let is_c_file (_filename, filetype) = filetype=Ocaml_filetypes.C
let cmas_need_dynamic_loading directories libraries =
let loads_c_code library =
let library = Misc.find_in_path directories library in
let ic = open_in_bin library in
try
let len_magic_number = String.length Config.cma_magic_number in
let magic_number = really_input_string ic len_magic_number in
if magic_number = Config.cma_magic_number then
let toc_pos = input_binary_int ic in
seek_in ic toc_pos;
let toc = (input_value ic : Cmo_format.library) in
close_in ic;
if toc.Cmo_format.lib_dllibs <> [] then Some (Ok ()) else None
else
raise End_of_file
with End_of_file
| Sys_error _ ->
begin try close_in ic with Sys_error _ -> () end;
Some (Error ("Corrupt or non-CMA file: " ^ library))
in
List.find_map loads_c_code (String.words libraries)
let compile_program (compiler : Ocaml_compilers.compiler) log env =
let program_variable = compiler#program_variable in
let program_file = Environments.safe_lookup program_variable env in
let all_modules =
Actions_helpers.words_of_variable env Ocaml_variables.all_modules in
let output_variable = compiler#output_variable in
let prepare = prepare_module output_variable log env in
let modules =
List.concatmap prepare (List.map Ocaml_filetypes.filetype all_modules) in
let has_c_file = List.exists is_c_file modules in
let c_headers_flags =
if has_c_file then Ocaml_flags.c_includes else "" in
let expected_exit_status =
Ocaml_tools.expected_exit_status env (compiler :> Ocaml_tools.tool) in
let module_names =
(binary_modules compiler#target env) ^ " " ^
(String.concat " " (List.map Ocaml_filetypes.make_filename modules)) in
let what = Printf.sprintf "Compiling program %s from modules %s"
program_file module_names in
Printf.fprintf log "%s\n%!" what;
let compile_only =
Environments.lookup_as_bool Ocaml_variables.compile_only env = Some true
in
let compile_flags =
if compile_only then " -c " else ""
in
let output = if compile_only then "" else "-o " ^ program_file in
let libraries = libraries compiler#target env in
let cmas_need_dynamic_loading =
if not Config.supports_shared_libraries &&
compiler#target = Ocaml_backends.Bytecode then
cmas_need_dynamic_loading (directories env) libraries
else
None
in
match cmas_need_dynamic_loading with
| Some (Error reason) ->
(Result.fail_with_reason reason, env)
| Some (Ok _) | None ->
let bytecode_links_c_code = (cmas_need_dynamic_loading = Some (Ok ())) in
let commandline =
[
compiler#name;
Ocaml_flags.runtime_flags env compiler#target
(has_c_file || bytecode_links_c_code);
c_headers_flags;
Ocaml_flags.stdlib;
directory_flags env;
flags env;
libraries;
backend_default_flags env compiler#target;
backend_flags env compiler#target;
compile_flags;
output;
(Environments.safe_lookup Ocaml_variables.ocaml_filetype_flag env);
module_names;
last_flags env
] in
let exit_status =
Actions_helpers.run_cmd
~environment:default_ocaml_env
~stdin_variable: Ocaml_variables.compiler_stdin
~stdout_variable:compiler#output_variable
~stderr_variable:compiler#output_variable
~append:true
log env commandline in
if exit_status=expected_exit_status
then (Result.pass, env)
else begin
let reason =
(Actions_helpers.mkreason
what (String.concat " " commandline) exit_status) in
(Result.fail_with_reason reason, env)
end
let compile_module compiler module_ log env =
let expected_exit_status =
Ocaml_tools.expected_exit_status env (compiler :> Ocaml_tools.tool) in
let what = Printf.sprintf "Compiling module %s" module_ in
Printf.fprintf log "%s\n%!" what;
let module_with_filetype = Ocaml_filetypes.filetype module_ in
let is_c = is_c_file module_with_filetype in
let c_headers_flags =
if is_c then Ocaml_flags.c_includes else "" in
let compile_only_flag_opt =
if Environments.lookup_as_bool Ocaml_variables.compile_only env = Some false
then ""
else " -c "
in
let commandline =
[
compiler#name;
Ocaml_flags.stdlib;
c_headers_flags;
directory_flags env;
flags env;
libraries compiler#target env;
backend_default_flags env compiler#target;
backend_flags env compiler#target;
compile_only_flag_opt;
module_;
] in
let exit_status =
Actions_helpers.run_cmd
~environment:default_ocaml_env
~stdin_variable: Ocaml_variables.compiler_stdin
~stdout_variable:compiler#output_variable
~stderr_variable:compiler#output_variable
~append:true
log env commandline in
if exit_status=expected_exit_status
then (Result.pass, env)
else begin
let reason =
(Actions_helpers.mkreason
what (String.concat " " commandline) exit_status) in
(Result.fail_with_reason reason, env)
end
let module_has_interface directory module_name =
let interface_name =
Ocaml_filetypes.make_filename (module_name, Ocaml_filetypes.Interface) in
let interface_fullpath = Filename.make_path [directory;interface_name] in
Sys.file_exists interface_fullpath
let add_module_interface directory module_description =
match[@ocaml.warning "-fragile-match"] module_description with
| (filename, Ocaml_filetypes.Implementation) when
module_has_interface directory filename ->
[(filename, Ocaml_filetypes.Interface); module_description]
| _ -> [module_description]
let print_module_names log description modules =
Printf.fprintf log "%s modules: %s\n%!"
description
(String.concat " " (List.map Ocaml_filetypes.make_filename modules))
let find_source_modules log env =
let source_directory = Actions_helpers.test_source_directory env in
let specified_modules =
List.map Ocaml_filetypes.filetype
((plugins env) @ (modules env) @ [(Actions_helpers.testfile env)]) in
print_module_names log "Specified" specified_modules;
let source_modules =
List.concatmap
(add_module_interface source_directory)
specified_modules in
print_module_names log "Source" source_modules;
Environments.add
Ocaml_variables.all_modules
(String.concat " " (List.map Ocaml_filetypes.make_filename source_modules))
env
let setup_tool_build_env tool log env =
let source_directory = Actions_helpers.test_source_directory env in
let testfile = Actions_helpers.testfile env in
let testfile_basename = Filename.chop_extension testfile in
let tool_reference_variable =
tool#reference_variable in
let tool_reference_prefix =
Filename.make_path [source_directory; testfile_basename] in
let tool_reference_file =
tool#reference_file env tool_reference_prefix
in
let env =
Environments.add_if_undefined
tool_reference_variable
tool_reference_file env
in
let source_modules =
Actions_helpers.words_of_variable env Ocaml_variables.all_modules in
let tool_directory_suffix =
Environments.safe_lookup Ocaml_variables.compiler_directory_suffix env in
let tool_directory_name =
tool#directory ^ tool_directory_suffix in
let build_dir = Filename.concat
(Environments.safe_lookup
Builtin_variables.test_build_directory_prefix env)
tool_directory_name in
let tool_output_variable = tool#output_variable in
let tool_output_filename =
Filename.make_filename tool#directory "output" in
let tool_output_file =
Filename.make_path [build_dir; tool_output_filename]
in
let env =
Environments.add_if_undefined
tool_output_variable
tool_output_file env
in
Sys.force_remove tool_output_file;
let env =
Environments.add Builtin_variables.test_build_directory build_dir env in
Actions_helpers.setup_build_env false source_modules log env
let setup_compiler_build_env (compiler : Ocaml_compilers.compiler) log env =
let (r, env) = setup_tool_build_env compiler log env in
if Result.is_pass r then
begin
let prog_var = compiler#program_variable in
let prog_output_var = compiler#program_output_variable in
let default_prog_file = get_program_file compiler#target env in
let env = Environments.add_if_undefined prog_var default_prog_file env in
let prog_file = Environments.safe_lookup prog_var env in
let prog_output_file = prog_file ^ ".output" in
let env = match prog_output_var with
| None -> env
| Some outputvar ->
Environments.add_if_undefined outputvar prog_output_file env
in
(r, env)
end else (r, env)
let setup_toplevel_build_env (toplevel : Ocaml_toplevels.toplevel) log env =
setup_tool_build_env toplevel log env
let mk_compiler_env_setup name (compiler : Ocaml_compilers.compiler) =
Actions.make ~name ~description:(Printf.sprintf "Setup build env (%s)" name)
~does_something:false
(setup_compiler_build_env compiler)
let mk_toplevel_env_setup name (toplevel : Ocaml_toplevels.toplevel) =
Actions.make ~name
~description:(Printf.sprintf "Setup toplevel env (%s)" name)
~does_something:false
(setup_toplevel_build_env toplevel)
let setup_ocamlc_byte_build_env =
mk_compiler_env_setup
"setup-ocamlc.byte-build-env"
Ocaml_compilers.ocamlc_byte
let setup_ocamlc_opt_build_env =
native_action
(mk_compiler_env_setup
"setup-ocamlc.opt-build-env"
Ocaml_compilers.ocamlc_opt)
let setup_ocamlopt_byte_build_env =
native_action
(mk_compiler_env_setup
"setup-ocamlopt.byte-build-env"
Ocaml_compilers.ocamlopt_byte)
let setup_ocamlopt_opt_build_env =
native_action
(mk_compiler_env_setup
"setup-ocamlopt.opt-build-env"
Ocaml_compilers.ocamlopt_opt)
let setup_ocaml_build_env =
mk_toplevel_env_setup
"setup-ocaml-build-env"
Ocaml_toplevels.ocaml
let setup_ocamlnat_build_env =
native_action
(mk_toplevel_env_setup
"setup-ocamlnat-build-env"
Ocaml_toplevels.ocamlnat)
let compile (compiler : Ocaml_compilers.compiler) log env =
match Environments.lookup_nonempty Builtin_variables.commandline env with
| None ->
begin
match Environments.lookup_nonempty Ocaml_variables.module_ env with
| None -> compile_program compiler log env
| Some module_ -> compile_module compiler module_ log env
end
| Some cmdline ->
let expected_exit_status =
Ocaml_tools.expected_exit_status env (compiler :> Ocaml_tools.tool) in
let what = Printf.sprintf "Compiling using commandline %s" cmdline in
Printf.fprintf log "%s\n%!" what;
let commandline = [compiler#name; cmdline] in
let exit_status =
Actions_helpers.run_cmd
~environment:default_ocaml_env
~stdin_variable: Ocaml_variables.compiler_stdin
~stdout_variable:compiler#output_variable
~stderr_variable:compiler#output_variable
~append:true
log env commandline in
if exit_status=expected_exit_status
then (Result.pass, env)
else begin
let reason =
(Actions_helpers.mkreason
what (String.concat " " commandline) exit_status) in
(Result.fail_with_reason reason, env)
end
(* Compile actions *)
let ocamlc_byte =
Actions.make
~name:"ocamlc.byte"
~description:"Compile the program using ocamlc.byte"
~does_something:true
(compile Ocaml_compilers.ocamlc_byte)
let ocamlc_opt =
native_action
(Actions.make
~name:"ocamlc.opt"
~description:"Compile the program using ocamlc.opt"
~does_something:true
(compile Ocaml_compilers.ocamlc_opt))
let ocamlopt_byte =
native_action
(Actions.make
~name:"ocamlopt.byte"
~description:"Compile the program using ocamlopt.byte"
~does_something:true
(compile Ocaml_compilers.ocamlopt_byte))
let ocamlopt_opt =
native_action
(Actions.make
~name:"ocamlopt.opt"
~description:"Compile the program using ocamlopt.opt"
~does_something:true
(compile Ocaml_compilers.ocamlopt_opt))
let env_with_lib_unix env =
let libunixdir = Ocaml_directories.libunix in
let newlibs =
match Environments.lookup Ocaml_variables.caml_ld_library_path env with
| None -> libunixdir
| Some libs -> libunixdir ^ " " ^ libs
in
Environments.add Ocaml_variables.caml_ld_library_path newlibs env
let debug log env =
let program = Environments.safe_lookup Builtin_variables.program env in
let what = Printf.sprintf "Debugging program %s" program in
Printf.fprintf log "%s\n%!" what;
let commandline =
[
Ocaml_commands.ocamlrun_ocamldebug;
Ocaml_flags.ocamldebug_default_flags;
program
] in
let systemenv =
Environments.append_to_system_env
default_ocaml_env
(env_with_lib_unix env)
in
let expected_exit_status = 0 in
let exit_status =
Actions_helpers.run_cmd
~environment:systemenv
~stdin_variable: Ocaml_variables.ocamldebug_script
~stdout_variable:Builtin_variables.output
~stderr_variable:Builtin_variables.output
~append:true
log (env_with_lib_unix env) commandline in
if exit_status=expected_exit_status
then (Result.pass, env)
else begin
let reason =
(Actions_helpers.mkreason
what (String.concat " " commandline) exit_status) in
(Result.fail_with_reason reason, env)
end
let ocamldebug =
Actions.make ~name:"ocamldebug" ~description:"Run ocamldebug on the program"
~does_something:true
debug
let objinfo log env =
let tools_directory = Ocaml_directories.tools in
let program = Environments.safe_lookup Builtin_variables.program env in
let what = Printf.sprintf "Running ocamlobjinfo on %s" program in
Printf.fprintf log "%s\n%!" what;
let commandline =
[
Ocaml_commands.ocamlrun_ocamlobjinfo;
Ocaml_flags.ocamlobjinfo_default_flags;
program
] in
let ocamllib = [| (Printf.sprintf "OCAMLLIB=%s" tools_directory) |] in
let systemenv =
Environments.append_to_system_env
(Array.concat
[
default_ocaml_env;
ocamllib;
])
(env_with_lib_unix env)
in
let expected_exit_status = 0 in
let exit_status =
Actions_helpers.run_cmd
~environment:systemenv
~stdout_variable:Builtin_variables.output
~stderr_variable:Builtin_variables.output
~append:true
log (env_with_lib_unix env) commandline in
if exit_status=expected_exit_status
then (Result.pass, env)
else begin
let reason =
(Actions_helpers.mkreason
what (String.concat " " commandline) exit_status) in
(Result.fail_with_reason reason, env)
end
let ocamlobjinfo =
Actions.make ~name:"ocamlobjinfo"
~description:"Run ocamlobjinfo on the program"
~does_something:true
objinfo
let mklib log env =
let program = Environments.safe_lookup Builtin_variables.program env in
let what = Printf.sprintf "Running ocamlmklib to produce %s" program in
Printf.fprintf log "%s\n%!" what;
let ocamlc_command =
String.concat " "
[
Ocaml_commands.ocamlrun_ocamlc;
Ocaml_flags.stdlib;
]
in
let commandline =
[
Ocaml_commands.ocamlrun_ocamlmklib;
"-ocamlc '" ^ ocamlc_command ^ "'";
"-o " ^ program
] @ modules env in
let expected_exit_status = 0 in
let exit_status =
Actions_helpers.run_cmd
~environment:default_ocaml_env
~stdout_variable:Ocaml_variables.compiler_output
~stderr_variable:Ocaml_variables.compiler_output
~append:true
log env commandline in
if exit_status=expected_exit_status
then (Result.pass, env)
else begin
let reason =
(Actions_helpers.mkreason
what (String.concat " " commandline) exit_status) in
(Result.fail_with_reason reason, env)
end
let ocamlmklib =
Actions.make ~name:"ocamlmklib"
~description:"Run ocamlmklib to produce the program"
~does_something:false
mklib
let finalise_codegen_cc test_basename _log env =
let test_module =
Filename.make_filename test_basename "s"
in
let archmod = Ocaml_files.asmgen_archmod in
let modules = test_module ^ " " ^ archmod in
let program = Filename.make_filename test_basename "out" in
let env = Environments.add_bindings
[
Ocaml_variables.modules, modules;
Builtin_variables.program, program;
] env in
(Result.pass, env)
let finalise_codegen_msvc test_basename log env =
let obj = Filename.make_filename test_basename Ocamltest_config.objext in
let src = Filename.make_filename test_basename "s" in
let what = "Running Microsoft assembler" in
Printf.fprintf log "%s\n%!" what;
let commandline = [Ocamltest_config.asm; obj; src] in
let expected_exit_status = 0 in
let exit_status =
Actions_helpers.run_cmd
~environment:default_ocaml_env
~stdout_variable:Ocaml_variables.compiler_output
~stderr_variable:Ocaml_variables.compiler_output
~append:true
log env commandline in
if exit_status=expected_exit_status
then begin
let archmod = Ocaml_files.asmgen_archmod in
let modules = obj ^ " " ^ archmod in
let program = Filename.make_filename test_basename "out" in
let env = Environments.add_bindings
[
Ocaml_variables.modules, modules;
Builtin_variables.program, program;
] env in
(Result.pass, env)
end else begin
let reason =
(Actions_helpers.mkreason
what (String.concat " " commandline) exit_status) in
(Result.fail_with_reason reason, env)
end
let run_codegen log env =
let testfile = Actions_helpers.testfile env in
let testfile_basename = Filename.chop_extension testfile in
let what = Printf.sprintf "Running codegen on %s" testfile in
Printf.fprintf log "%s\n%!" what;
let test_build_directory =
Actions_helpers.test_build_directory env in
let compiler_output =
Filename.make_path [test_build_directory; "compiler-output"]
in
let env =
Environments.add_if_undefined
Ocaml_variables.compiler_output
compiler_output
env
in
let output_file = Filename.make_filename testfile_basename "output" in
let output = Filename.make_path [test_build_directory; output_file] in
let env = Environments.add Builtin_variables.output output env in
let commandline =
[
Ocaml_commands.codegen;
flags env;
"-S " ^ testfile
] in
let expected_exit_status =
Actions_helpers.exit_status_of_variable env
Ocaml_variables.codegen_exit_status
in
let exit_status =
Actions_helpers.run_cmd
~environment:default_ocaml_env
~stdout_variable:Ocaml_variables.compiler_output
~stderr_variable:Ocaml_variables.compiler_output
~append:true
log env commandline in
if exit_status=expected_exit_status
then begin
if exit_status=0
then begin
let finalise =
if Ocamltest_config.ccomptype="msvc"
then finalise_codegen_msvc
else finalise_codegen_cc
in
finalise testfile_basename log env
end else (Result.pass, env)
end else begin
let reason =
(Actions_helpers.mkreason
what (String.concat " " commandline) exit_status) in
(Result.fail_with_reason reason, env)
end
let codegen =
Actions.make ~name:"codegen" ~description:"Run codegen on the test file"
~does_something:true
run_codegen
let run_cc log env =
let program = Environments.safe_lookup Builtin_variables.program env in
let what = Printf.sprintf "Running C compiler to build %s" program in
Printf.fprintf log "%s\n%!" what;
let output_exe =
if Ocamltest_config.ccomptype="msvc" then "/Fe" else "-o "
in
let commandline =
[
Ocamltest_config.cc;
Ocamltest_config.cflags;
"-I" ^ Ocaml_directories.runtime;
output_exe ^ program;
Environments.safe_lookup Builtin_variables.arguments env;
] @ modules env in
let expected_exit_status = 0 in
let exit_status =
Actions_helpers.run_cmd
~environment:default_ocaml_env
~stdout_variable:Ocaml_variables.compiler_output
~stderr_variable:Ocaml_variables.compiler_output
~append:true
log env commandline in
if exit_status=expected_exit_status
then (Result.pass, env)
else begin
let reason =
(Actions_helpers.mkreason
what (String.concat " " commandline) exit_status) in
(Result.fail_with_reason reason, env)
end
let cc =
Actions.make ~name:"cc" ~description:"Run C compiler to build the program"
~does_something:true
run_cc
let run_expect_once input_file principal log env =
let expect_flags = Sys.safe_getenv "EXPECT_FLAGS" in
let principal_flag = if principal then "-principal" else "" in
let commandline =
[
Ocaml_commands.expect;
expect_flags;
Ocaml_flags.toplevel_default_flags;
Ocaml_flags.stdlib;
directory_flags env;
Ocaml_flags.include_toplevel_directory;
flags env;
principal_flag;
libraries Bytecode env;
binary_modules Bytecode env;
input_file
]
in
let exit_status =
Actions_helpers.run_cmd ~environment:default_ocaml_env log env commandline
in
if exit_status=0 then (Result.pass, env)
else begin
let reason = (Actions_helpers.mkreason
"expect" (String.concat " " commandline) exit_status) in
(Result.fail_with_reason reason, env)
end
let run_expect_twice input_file log env =
let corrected filename = Filename.make_filename filename "corrected" in
let (result1, env1) = run_expect_once input_file false log env in
if Result.is_pass result1 then begin
let intermediate_file = corrected input_file in
let (result2, env2) =
run_expect_once intermediate_file true log env1 in
if Result.is_pass result2 then begin
let output_file = corrected intermediate_file in
let output_env = Environments.add_bindings
[
Builtin_variables.reference, input_file;
Builtin_variables.output, output_file
] env2 in
(Result.pass, output_env)
end else (result2, env2)
end else (result1, env1)
let run_expect log env =
let input_file = Actions_helpers.testfile env in
run_expect_twice input_file log env
let run_expect =
Actions.make ~name:"run-expect" ~description:"Run expect test"
~does_something:true
run_expect
let make_check_tool_output name tool = Actions.make
~name
~description:(Printf.sprintf "Check tool output (%s)" name)
~does_something:true
(Actions_helpers.check_output
tool#family
tool#output_variable
tool#reference_variable)
let check_ocamlc_byte_output = make_check_tool_output
"check-ocamlc.byte-output" Ocaml_compilers.ocamlc_byte
let check_ocamlc_opt_output =
native_action
(make_check_tool_output
"check-ocamlc.opt-output" Ocaml_compilers.ocamlc_opt)
let check_ocamlopt_byte_output =
native_action
(make_check_tool_output
"check-ocamlopt.byte-output" Ocaml_compilers.ocamlopt_byte)
let check_ocamlopt_opt_output =
native_action
(make_check_tool_output
"check-ocamlopt.opt-output" Ocaml_compilers.ocamlopt_opt)
let really_compare_programs backend comparison_tool log env =
let program = Environments.safe_lookup Builtin_variables.program env in
let program2 = Environments.safe_lookup Builtin_variables.program2 env in
let what = Printf.sprintf "Comparing %s programs %s and %s"
(Ocaml_backends.string_of_backend backend) program program2 in
Printf.fprintf log "%s\n%!" what;
let files = {
Filecompare.filetype = Filecompare.Binary;
Filecompare.reference_filename = program;
Filecompare.output_filename = program2
} in
match Filecompare.compare_files ~tool:comparison_tool files with
| Filecompare.Same -> (Result.pass, env)
| Filecompare.Different ->
let reason = Printf.sprintf "Files %s and %s are different"
program program2 in
(Result.fail_with_reason reason, env)
| Filecompare.Unexpected_output -> assert false
| Filecompare.Error (commandline, exitcode) ->
let reason = Actions_helpers.mkreason what commandline exitcode in
(Result.fail_with_reason reason, env)
let compare_programs backend comparison_tool log env =
let compare_programs =
Environments.lookup_as_bool Ocaml_variables.compare_programs env in
if compare_programs = Some false then begin
let reason = "program comparison disabled" in
(Result.pass_with_reason reason, env)
end else really_compare_programs backend comparison_tool log env
(* See CR in compare_bytecode_programs_code below.
let _make_bytecode_programs_comparison_tool =
let ocamlrun = Ocaml_files.ocamlrun in
let cmpbyt = Ocaml_files.cmpbyt in
let tool_name = ocamlrun ^ " " ^ cmpbyt in
Filecompare.make_comparison_tool tool_name ""*)
let native_programs_comparison_tool = Filecompare.default_comparison_tool
let compare_bytecode_programs_code _log env : Result.t * Environments.t =
(* CR xclerc: consider re-enabling the test if it can be made robust enough.
Currently, ocamlc.byte and ocamlc.opt (flambda2) sometimes generate equivalent
cmi files whose contents differ because of sharing; the resulting difference
is propagated through digests to the bytecode executables.
let bytecode_programs_comparison_tool =
make_bytecode_programs_comparison_tool in
compare_programs
Ocaml_backends.Bytecode bytecode_programs_comparison_tool log env
*)
Result.pass_with_reason "comparing of bytecode programs is disabled", env
let compare_bytecode_programs =
native_action
(Actions.make
~name:"compare-bytecode-programs"
~description:"Compare the bytecode programs generated by ocamlc.byte and \
ocamlc.opt"
~does_something:true
compare_bytecode_programs_code)
let compare_binary_files =
native_action
(Actions.make
~name:"compare-binary-files"
~description:"Compare the native programs generated by ocamlopt.byte and \
ocamlopt.opt"
~does_something:true
(compare_programs Ocaml_backends.Native native_programs_comparison_tool))
let compile_module compiler compilername compileroutput log env
(module_basename, module_filetype) =
let backend = compiler#target in
let filename =
Ocaml_filetypes.make_filename (module_basename, module_filetype) in
let expected_exit_status =
Ocaml_tools.expected_exit_status env (compiler :> Ocaml_tools.tool) in
let what = Printf.sprintf "%s for file %s (expected exit status: %d)"
(Ocaml_filetypes.action_of_filetype module_filetype) filename
(expected_exit_status) in
let compile_commandline input_file output_file optional_flags =
let compile = "-c " ^ input_file in
let output = match output_file with
| None -> ""
| Some file -> "-o " ^ file in
[
compilername;
Ocaml_flags.stdlib;
flags env;
backend_flags env backend;
optional_flags;
compile;
output;
] in
let exec commandline =
Printf.fprintf log "%s\n%!" what;
let exit_status =
Actions_helpers.run_cmd
~stdin_variable: Ocaml_variables.compiler_stdin
~stdout_variable:compileroutput
~stderr_variable:compileroutput
~append:true log env commandline in
if exit_status=expected_exit_status
then (Result.pass, env)
else begin
let reason =
(Actions_helpers.mkreason
what (String.concat " " commandline) exit_status) in
(Result.fail_with_reason reason, env)
end in