forked from rust-lang/rustc_codegen_gcc
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.rs
1242 lines (1149 loc) · 44.3 KB
/
test.rs
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
use crate::build;
use crate::config::{Channel, ConfigInfo};
use crate::utils::{
create_dir, get_sysroot_dir, get_toolchain, git_clone, git_clone_root_dir, remove_file,
run_command, run_command_with_env, run_command_with_output_and_env, rustc_version_info,
split_args, walk_dir,
};
use std::collections::HashMap;
use std::ffi::OsStr;
use std::fs::{remove_dir_all, File};
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::str::FromStr;
type Env = HashMap<String, String>;
type Runner = fn(&Env, &TestArg) -> Result<(), String>;
type Runners = HashMap<&'static str, (&'static str, Runner)>;
fn get_runners() -> Runners {
let mut runners = HashMap::new();
runners.insert("--test-rustc", ("Run all rustc tests", test_rustc as Runner));
runners
.insert("--test-successful-rustc", ("Run successful rustc tests", test_successful_rustc));
runners.insert(
"--test-failing-ui-pattern-tests",
("Run failing ui pattern tests", test_failing_ui_pattern_tests),
);
runners.insert("--test-failing-rustc", ("Run failing rustc tests", test_failing_rustc));
runners.insert("--projects", ("Run the tests of popular crates", test_projects));
runners.insert("--test-libcore", ("Run libcore tests", test_libcore));
runners.insert("--clean", ("Empty cargo target directory", clean));
runners.insert("--build-sysroot", ("Build sysroot", build_sysroot));
runners.insert("--std-tests", ("Run std tests", std_tests));
runners.insert("--asm-tests", ("Run asm tests", asm_tests));
runners.insert("--extended-tests", ("Run extended sysroot tests", extended_sysroot_tests));
runners.insert("--extended-rand-tests", ("Run extended rand tests", extended_rand_tests));
runners.insert(
"--extended-regex-example-tests",
("Run extended regex example tests", extended_regex_example_tests),
);
runners.insert("--extended-regex-tests", ("Run extended regex tests", extended_regex_tests));
runners.insert("--mini-tests", ("Run mini tests", mini_tests));
runners
}
fn get_number_after_arg(
args: &mut impl Iterator<Item = String>,
option: &str,
) -> Result<usize, String> {
match args.next() {
Some(nb) if !nb.is_empty() => match usize::from_str(&nb) {
Ok(nb) => Ok(nb),
Err(_) => Err(format!("Expected a number after `{}`, found `{}`", option, nb)),
},
_ => Err(format!("Expected a number after `{}`, found nothing", option)),
}
}
fn show_usage() {
println!(
r#"
`test` command help:
--release : Build codegen in release mode
--sysroot-panic-abort : Build the sysroot without unwinding support.
--features [arg] : Add a new feature [arg]
--use-system-gcc : Use system installed libgccjit
--build-only : Only build rustc_codegen_gcc then exits
--nb-parts : Used to split rustc_tests (for CI needs)
--current-part : Used with `--nb-parts`, allows you to specify which parts to test"#
);
ConfigInfo::show_usage();
for (option, (doc, _)) in get_runners() {
// FIXME: Instead of using the hard-coded `23` value, better to compute it instead.
let needed_spaces = 23_usize.saturating_sub(option.len());
let spaces: String = std::iter::repeat(' ').take(needed_spaces).collect();
println!(" {}{}: {}", option, spaces, doc);
}
println!(" --help : Show this help");
}
#[derive(Default, Debug)]
struct TestArg {
build_only: bool,
use_system_gcc: bool,
runners: Vec<String>,
flags: Vec<String>,
nb_parts: Option<usize>,
current_part: Option<usize>,
sysroot_panic_abort: bool,
config_info: ConfigInfo,
}
impl TestArg {
fn new() -> Result<Option<Self>, String> {
let mut test_arg = Self::default();
// We skip binary name and the `test` command.
let mut args = std::env::args().skip(2);
let runners = get_runners();
while let Some(arg) = args.next() {
match arg.as_str() {
"--features" => match args.next() {
Some(feature) if !feature.is_empty() => {
test_arg.flags.extend_from_slice(&["--features".into(), feature]);
}
_ => {
return Err("Expected an argument after `--features`, found nothing".into())
}
},
"--use-system-gcc" => {
println!("Using system GCC");
test_arg.use_system_gcc = true;
}
"--build-only" => test_arg.build_only = true,
"--nb-parts" => {
test_arg.nb_parts = Some(get_number_after_arg(&mut args, "--nb-parts")?);
}
"--current-part" => {
test_arg.current_part =
Some(get_number_after_arg(&mut args, "--current-part")?);
}
"--sysroot-panic-abort" => {
test_arg.sysroot_panic_abort = true;
}
"--help" => {
show_usage();
return Ok(None);
}
x if runners.contains_key(x)
&& !test_arg.runners.iter().any(|runner| runner == x) =>
{
test_arg.runners.push(x.into());
}
arg => {
if !test_arg.config_info.parse_argument(arg, &mut args)? {
return Err(format!("Unknown option {}", arg));
}
}
}
}
match (test_arg.current_part, test_arg.nb_parts) {
(Some(_), Some(_)) | (None, None) => {}
_ => {
return Err(
"If either `--current-part` or `--nb-parts` is specified, the other one \
needs to be specified as well!"
.to_string(),
);
}
}
if test_arg.config_info.no_default_features {
test_arg.flags.push("--no-default-features".into());
}
Ok(Some(test_arg))
}
pub fn is_using_gcc_master_branch(&self) -> bool {
!self.config_info.no_default_features
}
}
fn build_if_no_backend(env: &Env, args: &TestArg) -> Result<(), String> {
if args.config_info.backend.is_some() {
return Ok(());
}
let mut command: Vec<&dyn AsRef<OsStr>> = vec![&"cargo", &"rustc"];
let mut tmp_env;
let env = if args.config_info.channel == Channel::Release {
tmp_env = env.clone();
tmp_env.insert("CARGO_INCREMENTAL".to_string(), "1".to_string());
command.push(&"--release");
&tmp_env
} else {
&env
};
for flag in args.flags.iter() {
command.push(flag);
}
run_command_with_output_and_env(&command, None, Some(env))
}
fn clean(_env: &Env, args: &TestArg) -> Result<(), String> {
let _ = std::fs::remove_dir_all(&args.config_info.cargo_target_dir);
let path = Path::new(&args.config_info.cargo_target_dir).join("gccjit");
create_dir(&path)
}
fn mini_tests(env: &Env, args: &TestArg) -> Result<(), String> {
// FIXME: create a function "display_if_not_quiet" or something along the line.
println!("[BUILD] mini_core");
let crate_types = if args.config_info.host_triple != args.config_info.target_triple {
"lib"
} else {
"lib,dylib"
}
.to_string();
let mut command = args.config_info.rustc_command_vec();
command.extend_from_slice(&[
&"example/mini_core.rs",
&"--crate-name",
&"mini_core",
&"--crate-type",
&crate_types,
&"--target",
&args.config_info.target_triple,
]);
run_command_with_output_and_env(&command, None, Some(&env))?;
// FIXME: create a function "display_if_not_quiet" or something along the line.
println!("[BUILD] example");
let mut command = args.config_info.rustc_command_vec();
command.extend_from_slice(&[
&"example/example.rs",
&"--crate-type",
&"lib",
&"--target",
&args.config_info.target_triple,
]);
run_command_with_output_and_env(&command, None, Some(&env))?;
// FIXME: create a function "display_if_not_quiet" or something along the line.
println!("[AOT] mini_core_hello_world");
let mut command = args.config_info.rustc_command_vec();
command.extend_from_slice(&[
&"example/mini_core_hello_world.rs",
&"--crate-name",
&"mini_core_hello_world",
&"--crate-type",
&"bin",
&"-g",
&"--target",
&args.config_info.target_triple,
]);
run_command_with_output_and_env(&command, None, Some(&env))?;
let command: &[&dyn AsRef<OsStr>] = &[
&Path::new(&args.config_info.cargo_target_dir).join("mini_core_hello_world"),
&"abc",
&"bcd",
];
maybe_run_command_in_vm(&command, env, args)?;
Ok(())
}
fn build_sysroot(env: &Env, args: &TestArg) -> Result<(), String> {
// FIXME: create a function "display_if_not_quiet" or something along the line.
println!("[BUILD] sysroot");
build::build_sysroot(env, &args.config_info)?;
Ok(())
}
// TODO(GuillaumeGomez): when rewriting in Rust, refactor with the code in tests/lang_tests_common.rs if possible.
fn maybe_run_command_in_vm(
command: &[&dyn AsRef<OsStr>],
env: &Env,
args: &TestArg,
) -> Result<(), String> {
if !args.config_info.run_in_vm {
run_command_with_output_and_env(command, None, Some(env))?;
return Ok(());
}
let vm_parent_dir = match env.get("CG_GCC_VM_DIR") {
Some(dir) if !dir.is_empty() => PathBuf::from(dir.clone()),
_ => std::env::current_dir().unwrap(),
};
let vm_dir = "vm";
let exe_to_run = command.first().unwrap();
let exe = Path::new(&exe_to_run);
let exe_filename = exe.file_name().unwrap();
let vm_home_dir = vm_parent_dir.join(vm_dir).join("home");
let vm_exe_path = vm_home_dir.join(exe_filename);
let inside_vm_exe_path = Path::new("/home").join(exe_filename);
let sudo_command: &[&dyn AsRef<OsStr>] = &[&"sudo", &"cp", &exe, &vm_exe_path];
run_command_with_env(sudo_command, None, Some(env))?;
let mut vm_command: Vec<&dyn AsRef<OsStr>> =
vec![&"sudo", &"chroot", &vm_dir, &"qemu-m68k-static", &inside_vm_exe_path];
vm_command.extend_from_slice(command);
run_command_with_output_and_env(&vm_command, Some(&vm_parent_dir), Some(env))?;
Ok(())
}
fn std_tests(env: &Env, args: &TestArg) -> Result<(), String> {
let cargo_target_dir = Path::new(&args.config_info.cargo_target_dir);
// FIXME: create a function "display_if_not_quiet" or something along the line.
println!("[AOT] arbitrary_self_types_pointers_and_wrappers");
let mut command = args.config_info.rustc_command_vec();
command.extend_from_slice(&[
&"example/arbitrary_self_types_pointers_and_wrappers.rs",
&"--crate-name",
&"arbitrary_self_types_pointers_and_wrappers",
&"--crate-type",
&"bin",
&"--target",
&args.config_info.target_triple,
]);
run_command_with_env(&command, None, Some(env))?;
maybe_run_command_in_vm(
&[&cargo_target_dir.join("arbitrary_self_types_pointers_and_wrappers")],
env,
args,
)?;
// FIXME: create a function "display_if_not_quiet" or something along the line.
println!("[AOT] alloc_system");
let mut command = args.config_info.rustc_command_vec();
command.extend_from_slice(&[
&"example/alloc_system.rs",
&"--crate-type",
&"lib",
&"--target",
&args.config_info.target_triple,
]);
if args.is_using_gcc_master_branch() {
command.extend_from_slice(&[&"--cfg", &"feature=\"master\""]);
}
run_command_with_env(&command, None, Some(env))?;
// FIXME: doesn't work on m68k.
if args.config_info.host_triple == args.config_info.target_triple {
// FIXME: create a function "display_if_not_quiet" or something along the line.
println!("[AOT] alloc_example");
let mut command = args.config_info.rustc_command_vec();
command.extend_from_slice(&[
&"example/alloc_example.rs",
&"--crate-type",
&"bin",
&"--target",
&args.config_info.target_triple,
]);
run_command_with_env(&command, None, Some(env))?;
maybe_run_command_in_vm(&[&cargo_target_dir.join("alloc_example")], env, args)?;
}
// FIXME: create a function "display_if_not_quiet" or something along the line.
println!("[AOT] dst_field_align");
// FIXME(antoyo): Re-add -Zmir-opt-level=2 once rust-lang/rust#67529 is fixed.
let mut command = args.config_info.rustc_command_vec();
command.extend_from_slice(&[
&"example/dst-field-align.rs",
&"--crate-name",
&"dst_field_align",
&"--crate-type",
&"bin",
&"--target",
&args.config_info.target_triple,
]);
run_command_with_env(&command, None, Some(env))?;
maybe_run_command_in_vm(&[&cargo_target_dir.join("dst_field_align")], env, args)?;
// FIXME: create a function "display_if_not_quiet" or something along the line.
println!("[AOT] std_example");
let mut command = args.config_info.rustc_command_vec();
command.extend_from_slice(&[
&"example/std_example.rs",
&"--crate-type",
&"bin",
&"--target",
&args.config_info.target_triple,
]);
if args.is_using_gcc_master_branch() {
command.extend_from_slice(&[&"--cfg", &"feature=\"master\""]);
}
run_command_with_env(&command, None, Some(env))?;
maybe_run_command_in_vm(
&[&cargo_target_dir.join("std_example"), &"--target", &args.config_info.target_triple],
env,
args,
)?;
let test_flags = if let Some(test_flags) = env.get("TEST_FLAGS") {
split_args(test_flags)?
} else {
Vec::new()
};
// FIXME: create a function "display_if_not_quiet" or something along the line.
println!("[AOT] subslice-patterns-const-eval");
let mut command = args.config_info.rustc_command_vec();
command.extend_from_slice(&[
&"example/subslice-patterns-const-eval.rs",
&"--crate-type",
&"bin",
&"--target",
&args.config_info.target_triple,
]);
for test_flag in &test_flags {
command.push(test_flag);
}
run_command_with_env(&command, None, Some(env))?;
maybe_run_command_in_vm(&[&cargo_target_dir.join("subslice-patterns-const-eval")], env, args)?;
// FIXME: create a function "display_if_not_quiet" or something along the line.
println!("[AOT] track-caller-attribute");
let mut command = args.config_info.rustc_command_vec();
command.extend_from_slice(&[
&"example/track-caller-attribute.rs",
&"--crate-type",
&"bin",
&"--target",
&args.config_info.target_triple,
]);
for test_flag in &test_flags {
command.push(test_flag);
}
run_command_with_env(&command, None, Some(env))?;
maybe_run_command_in_vm(&[&cargo_target_dir.join("track-caller-attribute")], env, args)?;
// FIXME: create a function "display_if_not_quiet" or something along the line.
println!("[AOT] mod_bench");
let mut command = args.config_info.rustc_command_vec();
command.extend_from_slice(&[
&"example/mod_bench.rs",
&"--crate-type",
&"bin",
&"--target",
&args.config_info.target_triple,
]);
run_command_with_env(&command, None, Some(env))?;
// FIXME: the compiled binary is not run.
Ok(())
}
fn setup_rustc(env: &mut Env, args: &TestArg) -> Result<PathBuf, String> {
let toolchain = format!(
"+{channel}-{host}",
channel = get_toolchain()?, // May also include date
host = args.config_info.host_triple
);
let rust_dir_path = Path::new(crate::BUILD_DIR).join("rust");
// If the repository was already cloned, command will fail, so doesn't matter.
let _ = git_clone("https://github.com/rust-lang/rust.git", Some(&rust_dir_path), false);
let rust_dir: Option<&Path> = Some(&rust_dir_path);
run_command(&[&"git", &"checkout", &"--", &"tests/"], rust_dir)?;
run_command_with_output_and_env(&[&"git", &"fetch"], rust_dir, Some(env))?;
let rustc_commit = match rustc_version_info(env.get("RUSTC").map(|s| s.as_str()))?.commit_hash {
Some(commit_hash) => commit_hash,
None => return Err("Couldn't retrieve rustc commit hash".to_string()),
};
if rustc_commit != "unknown" {
run_command_with_output_and_env(
&[&"git", &"checkout", &rustc_commit],
rust_dir,
Some(env),
)?;
} else {
run_command_with_output_and_env(&[&"git", &"checkout"], rust_dir, Some(env))?;
}
let cargo = String::from_utf8(
run_command_with_env(&[&"rustup", &"which", &"cargo"], rust_dir, Some(env))?.stdout,
)
.map_err(|error| format!("Failed to retrieve cargo path: {:?}", error))
.and_then(|cargo| {
let cargo = cargo.trim().to_owned();
if cargo.is_empty() {
Err(format!("`cargo` path is empty"))
} else {
Ok(cargo)
}
})?;
let rustc = String::from_utf8(
run_command_with_env(&[&"rustup", &toolchain, &"which", &"rustc"], rust_dir, Some(env))?
.stdout,
)
.map_err(|error| format!("Failed to retrieve rustc path: {:?}", error))
.and_then(|rustc| {
let rustc = rustc.trim().to_owned();
if rustc.is_empty() {
Err(format!("`rustc` path is empty"))
} else {
Ok(rustc)
}
})?;
let llvm_filecheck = match run_command_with_env(
&[
&"bash",
&"-c",
&"which FileCheck-10 || \
which FileCheck-11 || \
which FileCheck-12 || \
which FileCheck-13 || \
which FileCheck-14",
],
rust_dir,
Some(env),
) {
Ok(cmd) => String::from_utf8_lossy(&cmd.stdout).to_string(),
Err(_) => {
eprintln!("Failed to retrieve LLVM FileCheck, ignoring...");
String::new()
}
};
let file_path = rust_dir_path.join("config.toml");
std::fs::write(
&file_path,
&format!(
r#"change-id = 115898
[rust]
codegen-backends = []
deny-warnings = false
verbose-tests = true
[build]
cargo = "{cargo}"
local-rebuild = true
rustc = "{rustc}"
[target.x86_64-unknown-linux-gnu]
llvm-filecheck = "{llvm_filecheck}"
[llvm]
download-ci-llvm = false
"#,
cargo = cargo,
rustc = rustc,
llvm_filecheck = llvm_filecheck.trim(),
),
)
.map_err(|error| format!("Failed to write into `{}`: {:?}", file_path.display(), error))?;
Ok(rust_dir_path)
}
fn asm_tests(env: &Env, args: &TestArg) -> Result<(), String> {
let mut env = env.clone();
let rust_dir = setup_rustc(&mut env, args)?;
// FIXME: create a function "display_if_not_quiet" or something along the line.
println!("[TEST] rustc asm test suite");
env.insert("COMPILETEST_FORCE_STAGE0".to_string(), "1".to_string());
let extra =
if args.is_using_gcc_master_branch() { "" } else { " -Csymbol-mangling-version=v0" };
let rustc_args = &format!(
r#"-Zpanic-abort-tests \
-Zcodegen-backend="{pwd}/target/{channel}/librustc_codegen_gcc.{dylib_ext}" \
--sysroot "{sysroot_dir}" -Cpanic=abort{extra}"#,
pwd = std::env::current_dir()
.map_err(|error| format!("`current_dir` failed: {:?}", error))?
.display(),
channel = args.config_info.channel.as_str(),
dylib_ext = args.config_info.dylib_ext,
sysroot_dir = args.config_info.sysroot_path,
extra = extra,
);
run_command_with_env(
&[
&"./x.py",
&"test",
&"--run",
&"always",
&"--stage",
&"0",
&"tests/assembly/asm",
&"--rustc-args",
&rustc_args,
],
Some(&rust_dir),
Some(&env),
)?;
Ok(())
}
fn run_cargo_command(
command: &[&dyn AsRef<OsStr>],
cwd: Option<&Path>,
env: &Env,
args: &TestArg,
) -> Result<(), String> {
run_cargo_command_with_callback(command, cwd, env, args, |cargo_command, cwd, env| {
run_command_with_output_and_env(cargo_command, cwd, Some(env))?;
Ok(())
})
}
fn run_cargo_command_with_callback<F>(
command: &[&dyn AsRef<OsStr>],
cwd: Option<&Path>,
env: &Env,
args: &TestArg,
callback: F,
) -> Result<(), String>
where
F: Fn(&[&dyn AsRef<OsStr>], Option<&Path>, &Env) -> Result<(), String>,
{
let toolchain = get_toolchain()?;
let toolchain_arg = format!("+{}", toolchain);
let rustc_version = String::from_utf8(
run_command_with_env(&[&args.config_info.rustc_command[0], &"-V"], cwd, Some(env))?.stdout,
)
.map_err(|error| format!("Failed to retrieve rustc version: {:?}", error))?;
let rustc_toolchain_version = String::from_utf8(
run_command_with_env(
&[&args.config_info.rustc_command[0], &toolchain_arg, &"-V"],
cwd,
Some(env),
)?
.stdout,
)
.map_err(|error| format!("Failed to retrieve rustc +toolchain version: {:?}", error))?;
if rustc_version != rustc_toolchain_version {
eprintln!(
"rustc_codegen_gcc is built for `{}` but the default rustc version is `{}`.",
rustc_toolchain_version, rustc_version,
);
eprintln!("Using `{}`.", rustc_toolchain_version);
}
let mut env = env.clone();
let rustflags = env.get("RUSTFLAGS").cloned().unwrap_or_default();
env.insert("RUSTDOCFLAGS".to_string(), rustflags);
let mut cargo_command: Vec<&dyn AsRef<OsStr>> = vec![&"cargo", &toolchain_arg];
cargo_command.extend_from_slice(&command);
callback(&cargo_command, cwd, &env)
}
// FIXME(antoyo): linker gives multiple definitions error on Linux
// echo "[BUILD] sysroot in release mode"
// ./build_sysroot/build_sysroot.sh --release
fn test_projects(env: &Env, args: &TestArg) -> Result<(), String> {
let projects = [
//"https://gitlab.gnome.org/GNOME/librsvg", // FIXME: doesn't compile in the CI since the
// version of cairo and other libraries is too old.
"https://github.com/rust-random/getrandom",
"https://github.com/BurntSushi/memchr",
"https://github.com/dtolnay/itoa",
"https://github.com/rust-lang/cfg-if",
"https://github.com/rust-lang-nursery/lazy-static.rs",
//"https://github.com/marshallpierce/rust-base64", // FIXME: one test is OOM-killed.
// TODO: ignore the base64 test that is OOM-killed.
"https://github.com/time-rs/time",
"https://github.com/rust-lang/log",
"https://github.com/bitflags/bitflags",
//"https://github.com/serde-rs/serde", // FIXME: one test fails.
//"https://github.com/rayon-rs/rayon", // TODO: very slow, only run on master?
//"https://github.com/rust-lang/cargo", // TODO: very slow, only run on master?
];
let mut env = env.clone();
let rustflags =
format!("{} --cap-lints allow", env.get("RUSTFLAGS").cloned().unwrap_or_default());
env.insert("RUSTFLAGS".to_string(), rustflags);
let run_tests = |projects_path, iter: &mut dyn Iterator<Item = &&str>| -> Result<(), String> {
for project in iter {
let clone_result = git_clone_root_dir(project, projects_path, true)?;
let repo_path = Path::new(&clone_result.repo_dir);
run_cargo_command(&[&"build", &"--release"], Some(repo_path), &env, args)?;
run_cargo_command(&[&"test"], Some(repo_path), &env, args)?;
}
Ok(())
};
let projects_path = Path::new("projects");
create_dir(projects_path)?;
let nb_parts = args.nb_parts.unwrap_or(0);
if nb_parts > 0 {
// We increment the number of tests by one because if this is an odd number, we would skip
// one test.
let count = projects.len() / nb_parts + 1;
let current_part = args.current_part.unwrap();
let start = current_part * count;
// We remove the projects we don't want to test.
run_tests(projects_path, &mut projects.iter().skip(start).take(count))?;
} else {
run_tests(projects_path, &mut projects.iter())?;
}
Ok(())
}
fn test_libcore(env: &Env, args: &TestArg) -> Result<(), String> {
// FIXME: create a function "display_if_not_quiet" or something along the line.
println!("[TEST] libcore");
let path = get_sysroot_dir().join("sysroot_src/library/core/tests");
let _ = remove_dir_all(path.join("target"));
run_cargo_command(&[&"test"], Some(&path), env, args)?;
Ok(())
}
// echo "[BENCH COMPILE] mod_bench"
//
// COMPILE_MOD_BENCH_INLINE="$RUSTC example/mod_bench.rs --crate-type bin -Zmir-opt-level=3 -O --crate-name mod_bench_inline"
// COMPILE_MOD_BENCH_LLVM_0="rustc example/mod_bench.rs --crate-type bin -Copt-level=0 -o $cargo_target_dir/mod_bench_llvm_0 -Cpanic=abort"
// COMPILE_MOD_BENCH_LLVM_1="rustc example/mod_bench.rs --crate-type bin -Copt-level=1 -o $cargo_target_dir/mod_bench_llvm_1 -Cpanic=abort"
// COMPILE_MOD_BENCH_LLVM_2="rustc example/mod_bench.rs --crate-type bin -Copt-level=2 -o $cargo_target_dir/mod_bench_llvm_2 -Cpanic=abort"
// COMPILE_MOD_BENCH_LLVM_3="rustc example/mod_bench.rs --crate-type bin -Copt-level=3 -o $cargo_target_dir/mod_bench_llvm_3 -Cpanic=abort"
//
// Use 100 runs, because a single compilations doesn't take more than ~150ms, so it isn't very slow
// hyperfine --runs ${COMPILE_RUNS:-100} "$COMPILE_MOD_BENCH_INLINE" "$COMPILE_MOD_BENCH_LLVM_0" "$COMPILE_MOD_BENCH_LLVM_1" "$COMPILE_MOD_BENCH_LLVM_2" "$COMPILE_MOD_BENCH_LLVM_3"
// echo "[BENCH RUN] mod_bench"
// hyperfine --runs ${RUN_RUNS:-10} $cargo_target_dir/mod_bench{,_inline} $cargo_target_dir/mod_bench_llvm_*
fn extended_rand_tests(env: &Env, args: &TestArg) -> Result<(), String> {
if !args.is_using_gcc_master_branch() {
println!("Not using GCC master branch. Skipping `extended_rand_tests`.");
return Ok(());
}
let mut env = env.clone();
// newer aho_corasick versions throw a deprecation warning
let rustflags =
format!("{} --cap-lints warn", env.get("RUSTFLAGS").cloned().unwrap_or_default());
env.insert("RUSTFLAGS".to_string(), rustflags);
let path = Path::new(crate::BUILD_DIR).join("rand");
run_cargo_command(&[&"clean"], Some(&path), &env, args)?;
// FIXME: create a function "display_if_not_quiet" or something along the line.
println!("[TEST] rust-random/rand");
run_cargo_command(&[&"test", &"--workspace"], Some(&path), &env, args)?;
Ok(())
}
fn extended_regex_example_tests(env: &Env, args: &TestArg) -> Result<(), String> {
if !args.is_using_gcc_master_branch() {
println!("Not using GCC master branch. Skipping `extended_regex_example_tests`.");
return Ok(());
}
let path = Path::new(crate::BUILD_DIR).join("regex");
run_cargo_command(&[&"clean"], Some(&path), env, args)?;
// FIXME: create a function "display_if_not_quiet" or something along the line.
println!("[TEST] rust-lang/regex example shootout-regex-dna");
let mut env = env.clone();
// newer aho_corasick versions throw a deprecation warning
let rustflags =
format!("{} --cap-lints warn", env.get("RUSTFLAGS").cloned().unwrap_or_default());
env.insert("RUSTFLAGS".to_string(), rustflags);
// Make sure `[codegen mono items] start` doesn't poison the diff
run_cargo_command(&[&"build", &"--example", &"shootout-regex-dna"], Some(&path), &env, args)?;
run_cargo_command_with_callback(
&[&"run", &"--example", &"shootout-regex-dna"],
Some(&path),
&env,
args,
|cargo_command, cwd, env| {
// FIXME: rewrite this with `child.stdin.write_all()` because
// `examples/regexdna-input.txt` is very small.
let mut command: Vec<&dyn AsRef<OsStr>> = vec![&"bash", &"-c"];
let cargo_args =
cargo_command.iter().map(|s| s.as_ref().to_str().unwrap()).collect::<Vec<_>>();
let bash_command = format!(
"cat examples/regexdna-input.txt | {} | grep -v 'Spawned thread' > res.txt",
cargo_args.join(" "),
);
command.push(&bash_command);
run_command_with_output_and_env(&command, cwd, Some(env))?;
run_command_with_output_and_env(
&[&"diff", &"-u", &"res.txt", &"examples/regexdna-output.txt"],
cwd,
Some(env),
)?;
Ok(())
},
)?;
Ok(())
}
fn extended_regex_tests(env: &Env, args: &TestArg) -> Result<(), String> {
if !args.is_using_gcc_master_branch() {
println!("Not using GCC master branch. Skipping `extended_regex_tests`.");
return Ok(());
}
// FIXME: create a function "display_if_not_quiet" or something along the line.
println!("[TEST] rust-lang/regex tests");
let mut env = env.clone();
// newer aho_corasick versions throw a deprecation warning
let rustflags =
format!("{} --cap-lints warn", env.get("RUSTFLAGS").cloned().unwrap_or_default());
env.insert("RUSTFLAGS".to_string(), rustflags);
let path = Path::new(crate::BUILD_DIR).join("regex");
run_cargo_command(
&[
&"test",
&"--tests",
&"--",
// FIXME: try removing `--exclude-should-panic` argument
&"--exclude-should-panic",
&"--test-threads",
&"1",
&"-Zunstable-options",
&"-q",
],
Some(&path),
&env,
args,
)?;
Ok(())
}
fn extended_sysroot_tests(env: &Env, args: &TestArg) -> Result<(), String> {
// pushd simple-raytracer
// echo "[BENCH COMPILE] ebobby/simple-raytracer"
// hyperfine --runs "${RUN_RUNS:-10}" --warmup 1 --prepare "cargo clean" \
// "RUSTC=rustc RUSTFLAGS='' cargo build" \
// "../y.sh cargo build"
// echo "[BENCH RUN] ebobby/simple-raytracer"
// cp ./target/debug/main ./raytracer_cg_gcc
// hyperfine --runs "${RUN_RUNS:-10}" ./raytracer_cg_llvm ./raytracer_cg_gcc
// popd
extended_rand_tests(env, args)?;
extended_regex_example_tests(env, args)?;
extended_regex_tests(env, args)?;
Ok(())
}
fn valid_ui_error_pattern_test(file: &str) -> bool {
// contains //~ERROR, but shouldn't be removed
[
"issues/auxiliary/issue-3136-a.rs",
"type-alias-impl-trait/auxiliary/cross_crate_ice.rs",
"type-alias-impl-trait/auxiliary/cross_crate_ice2.rs",
"macros/rfc-2011-nicer-assert-messages/auxiliary/common.rs",
"imports/ambiguous-1.rs",
"imports/ambiguous-4-extern.rs",
"entry-point/auxiliary/bad_main_functions.rs",
]
.iter()
.any(|to_ignore| file.ends_with(to_ignore))
}
#[rustfmt::skip]
fn contains_ui_error_patterns(file_path: &Path) -> Result<bool, String> {
// Tests generating errors.
let file = File::open(file_path)
.map_err(|error| format!("Failed to read `{}`: {:?}", file_path.display(), error))?;
for line in BufReader::new(file).lines().filter_map(|line| line.ok()) {
let line = line.trim();
if line.is_empty() {
continue;
}
if [
"//@ error-pattern:",
"//@ build-fail",
"//@ run-fail",
"-Cllvm-args",
"//~",
"thread",
]
.iter()
.any(|check| line.contains(check))
{
return Ok(true);
}
if line.contains("//[") && line.contains("]~") {
return Ok(true);
}
}
if file_path.display().to_string().contains("ambiguous-4-extern.rs") {
eprintln!("nothing found for {file_path:?}");
}
Ok(false)
}
// # Parameters
//
// * `env`: An environment variable that provides context for the function.
// * `args`: The arguments passed to the test. This could include things like the flags, config etc.
// * `prepare_files_callback`: A callback function that prepares the files needed for the test. Its used to remove/retain tests giving Error to run various rust test suits.
// * `run_error_pattern_test`: A boolean that determines whether to run only error pattern tests.
// * `test_type`: A string that indicates the type of the test being run.
//
fn test_rustc_inner<F>(
env: &Env,
args: &TestArg,
prepare_files_callback: F,
run_error_pattern_test: bool,
test_type: &str,
) -> Result<(), String>
where
F: Fn(&Path) -> Result<bool, String>,
{
// FIXME: create a function "display_if_not_quiet" or something along the line.
println!("[TEST] rust-lang/rust");
let mut env = env.clone();
let rust_path = setup_rustc(&mut env, args)?;
if !prepare_files_callback(&rust_path)? {
// FIXME: create a function "display_if_not_quiet" or something along the line.
println!("Keeping all {} tests", test_type);
}
if test_type == "ui" {
if run_error_pattern_test {
// After we removed the error tests that are known to panic with rustc_codegen_gcc, we now remove the passing tests since this runs the error tests.
walk_dir(
rust_path.join("tests/ui"),
&mut |_dir| Ok(()),
&mut |file_path| {
if contains_ui_error_patterns(file_path)? {
Ok(())
} else {
remove_file(file_path).map_err(|e| e.to_string())
}
},
true,
)?;
} else {
walk_dir(
rust_path.join("tests/ui"),
&mut |dir| {
let dir_name = dir.file_name().and_then(|name| name.to_str()).unwrap_or("");
if [
"abi",
"extern",
"unsized-locals",
"proc-macro",
"threads-sendsync",
"borrowck",
"test-attrs",
]
.iter()
.any(|name| *name == dir_name)
{
std::fs::remove_dir_all(dir).map_err(|error| {
format!("Failed to remove folder `{}`: {:?}", dir.display(), error)
})?;
}
Ok(())
},
&mut |_| Ok(()),
false,
)?;
// These two functions are used to remove files that are known to not be working currently
// with the GCC backend to reduce noise.
fn dir_handling(dir: &Path) -> Result<(), String> {
if dir.file_name().map(|name| name == "auxiliary").unwrap_or(true) {
return Ok(());
}
walk_dir(dir, &mut dir_handling, &mut file_handling, false)
}
fn file_handling(file_path: &Path) -> Result<(), String> {
if !file_path.extension().map(|extension| extension == "rs").unwrap_or(false) {
return Ok(());
}
let path_str = file_path.display().to_string().replace("\\", "/");
if valid_ui_error_pattern_test(&path_str) {
return Ok(());
} else if contains_ui_error_patterns(file_path)? {
return remove_file(&file_path);
}
Ok(())
}
walk_dir(rust_path.join("tests/ui"), &mut dir_handling, &mut file_handling, false)?;
}
let nb_parts = args.nb_parts.unwrap_or(0);
if nb_parts > 0 {
let current_part = args.current_part.unwrap();
// FIXME: create a function "display_if_not_quiet" or something along the line.
println!(
"Splitting ui_test into {} parts (and running part {})",
nb_parts, current_part
);
let out = String::from_utf8(
run_command(
&[
&"find",
&"tests/ui",
&"-type",
&"f",
&"-name",
&"*.rs",
&"-not",
&"-path",
&"*/auxiliary/*",
],
Some(&rust_path),
)?
.stdout,
)
.map_err(|error| format!("Failed to retrieve output of find command: {:?}", error))?;
let mut files = out
.split('\n')
.map(|line| line.trim())
.filter(|line| !line.is_empty())
.collect::<Vec<_>>();
// To ensure it'll be always the same sub files, we sort the content.
files.sort();
// We increment the number of tests by one because if this is an odd number, we would skip
// one test.
let count = files.len() / nb_parts + 1;
// We remove the files we don't want to test.
let start = current_part * count;
for path in files.iter().skip(start).take(count) {
remove_file(&rust_path.join(path))?;