]> git.lizzy.rs Git - rust.git/blob - build_system/tests.rs
Move all downloaded repos to the downloads/ dir
[rust.git] / build_system / tests.rs
1 use super::build_sysroot;
2 use super::config;
3 use super::prepare;
4 use super::rustc_info::get_wrapper_file_name;
5 use super::utils::{cargo_command, hyperfine_command, spawn_and_wait, spawn_and_wait_with_input};
6 use build_system::SysrootKind;
7 use std::env;
8 use std::ffi::OsStr;
9 use std::fs;
10 use std::path::{Path, PathBuf};
11 use std::process::Command;
12
13 struct TestCase {
14     config: &'static str,
15     func: &'static dyn Fn(&TestRunner),
16 }
17
18 impl TestCase {
19     const fn new(config: &'static str, func: &'static dyn Fn(&TestRunner)) -> Self {
20         Self { config, func }
21     }
22 }
23
24 const NO_SYSROOT_SUITE: &[TestCase] = &[
25     TestCase::new("build.mini_core", &|runner| {
26         runner.run_rustc([
27             "example/mini_core.rs",
28             "--crate-name",
29             "mini_core",
30             "--crate-type",
31             "lib,dylib",
32             "--target",
33             &runner.target_triple,
34         ]);
35     }),
36     TestCase::new("build.example", &|runner| {
37         runner.run_rustc([
38             "example/example.rs",
39             "--crate-type",
40             "lib",
41             "--target",
42             &runner.target_triple,
43         ]);
44     }),
45     TestCase::new("jit.mini_core_hello_world", &|runner| {
46         let mut jit_cmd = runner.rustc_command([
47             "-Zunstable-options",
48             "-Cllvm-args=mode=jit",
49             "-Cprefer-dynamic",
50             "example/mini_core_hello_world.rs",
51             "--cfg",
52             "jit",
53             "--target",
54             &runner.host_triple,
55         ]);
56         jit_cmd.env("CG_CLIF_JIT_ARGS", "abc bcd");
57         spawn_and_wait(jit_cmd);
58
59         eprintln!("[JIT-lazy] mini_core_hello_world");
60         let mut jit_cmd = runner.rustc_command([
61             "-Zunstable-options",
62             "-Cllvm-args=mode=jit-lazy",
63             "-Cprefer-dynamic",
64             "example/mini_core_hello_world.rs",
65             "--cfg",
66             "jit",
67             "--target",
68             &runner.host_triple,
69         ]);
70         jit_cmd.env("CG_CLIF_JIT_ARGS", "abc bcd");
71         spawn_and_wait(jit_cmd);
72     }),
73     TestCase::new("aot.mini_core_hello_world", &|runner| {
74         runner.run_rustc([
75             "example/mini_core_hello_world.rs",
76             "--crate-name",
77             "mini_core_hello_world",
78             "--crate-type",
79             "bin",
80             "-g",
81             "--target",
82             &runner.target_triple,
83         ]);
84         runner.run_out_command("mini_core_hello_world", ["abc", "bcd"]);
85     }),
86 ];
87
88 const BASE_SYSROOT_SUITE: &[TestCase] = &[
89     TestCase::new("aot.arbitrary_self_types_pointers_and_wrappers", &|runner| {
90         runner.run_rustc([
91             "example/arbitrary_self_types_pointers_and_wrappers.rs",
92             "--crate-name",
93             "arbitrary_self_types_pointers_and_wrappers",
94             "--crate-type",
95             "bin",
96             "--target",
97             &runner.target_triple,
98         ]);
99         runner.run_out_command("arbitrary_self_types_pointers_and_wrappers", []);
100     }),
101     TestCase::new("aot.issue_91827_extern_types", &|runner| {
102         runner.run_rustc([
103             "example/issue-91827-extern-types.rs",
104             "--crate-name",
105             "issue_91827_extern_types",
106             "--crate-type",
107             "bin",
108             "--target",
109             &runner.target_triple,
110         ]);
111         runner.run_out_command("issue_91827_extern_types", []);
112     }),
113     TestCase::new("build.alloc_system", &|runner| {
114         runner.run_rustc([
115             "example/alloc_system.rs",
116             "--crate-type",
117             "lib",
118             "--target",
119             &runner.target_triple,
120         ]);
121     }),
122     TestCase::new("aot.alloc_example", &|runner| {
123         runner.run_rustc([
124             "example/alloc_example.rs",
125             "--crate-type",
126             "bin",
127             "--target",
128             &runner.target_triple,
129         ]);
130         runner.run_out_command("alloc_example", []);
131     }),
132     TestCase::new("jit.std_example", &|runner| {
133         runner.run_rustc([
134             "-Zunstable-options",
135             "-Cllvm-args=mode=jit",
136             "-Cprefer-dynamic",
137             "example/std_example.rs",
138             "--target",
139             &runner.host_triple,
140         ]);
141
142         eprintln!("[JIT-lazy] std_example");
143         runner.run_rustc([
144             "-Zunstable-options",
145             "-Cllvm-args=mode=jit-lazy",
146             "-Cprefer-dynamic",
147             "example/std_example.rs",
148             "--target",
149             &runner.host_triple,
150         ]);
151     }),
152     TestCase::new("aot.std_example", &|runner| {
153         runner.run_rustc([
154             "example/std_example.rs",
155             "--crate-type",
156             "bin",
157             "--target",
158             &runner.target_triple,
159         ]);
160         runner.run_out_command("std_example", ["arg"]);
161     }),
162     TestCase::new("aot.dst_field_align", &|runner| {
163         runner.run_rustc([
164             "example/dst-field-align.rs",
165             "--crate-name",
166             "dst_field_align",
167             "--crate-type",
168             "bin",
169             "--target",
170             &runner.target_triple,
171         ]);
172         runner.run_out_command("dst_field_align", []);
173     }),
174     TestCase::new("aot.subslice-patterns-const-eval", &|runner| {
175         runner.run_rustc([
176             "example/subslice-patterns-const-eval.rs",
177             "--crate-type",
178             "bin",
179             "-Cpanic=abort",
180             "--target",
181             &runner.target_triple,
182         ]);
183         runner.run_out_command("subslice-patterns-const-eval", []);
184     }),
185     TestCase::new("aot.track-caller-attribute", &|runner| {
186         runner.run_rustc([
187             "example/track-caller-attribute.rs",
188             "--crate-type",
189             "bin",
190             "-Cpanic=abort",
191             "--target",
192             &runner.target_triple,
193         ]);
194         runner.run_out_command("track-caller-attribute", []);
195     }),
196     TestCase::new("aot.float-minmax-pass", &|runner| {
197         runner.run_rustc([
198             "example/float-minmax-pass.rs",
199             "--crate-type",
200             "bin",
201             "-Cpanic=abort",
202             "--target",
203             &runner.target_triple,
204         ]);
205         runner.run_out_command("float-minmax-pass", []);
206     }),
207     TestCase::new("aot.mod_bench", &|runner| {
208         runner.run_rustc([
209             "example/mod_bench.rs",
210             "--crate-type",
211             "bin",
212             "--target",
213             &runner.target_triple,
214         ]);
215         runner.run_out_command("mod_bench", []);
216     }),
217 ];
218
219 const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[
220     TestCase::new("test.rust-random/rand", &|runner| {
221         runner.in_dir(prepare::RAND.source_dir(), |runner| {
222             runner.run_cargo("clean", []);
223
224             if runner.host_triple == runner.target_triple {
225                 eprintln!("[TEST] rust-random/rand");
226                 runner.run_cargo("test", ["--workspace"]);
227             } else {
228                 eprintln!("[AOT] rust-random/rand");
229                 runner.run_cargo("build", ["--workspace", "--tests"]);
230             }
231         });
232     }),
233     TestCase::new("bench.simple-raytracer", &|runner| {
234         runner.in_dir(prepare::SIMPLE_RAYTRACER.source_dir(), |runner| {
235             let run_runs = env::var("RUN_RUNS").unwrap_or("10".to_string()).parse().unwrap();
236
237             if runner.host_triple == runner.target_triple {
238                 eprintln!("[BENCH COMPILE] ebobby/simple-raytracer");
239                 let prepare = runner.cargo_command("clean", []);
240
241                 let llvm_build_cmd = cargo_command("cargo", "build", None, Path::new("."));
242
243                 let cargo_clif = runner
244                     .root_dir
245                     .clone()
246                     .join("build")
247                     .join(get_wrapper_file_name("cargo-clif", "bin"));
248                 let clif_build_cmd = cargo_command(cargo_clif, "build", None, Path::new("."));
249
250                 let bench_compile =
251                     hyperfine_command(1, run_runs, Some(prepare), llvm_build_cmd, clif_build_cmd);
252
253                 spawn_and_wait(bench_compile);
254
255                 eprintln!("[BENCH RUN] ebobby/simple-raytracer");
256                 fs::copy(PathBuf::from("./target/debug/main"), PathBuf::from("raytracer_cg_clif"))
257                     .unwrap();
258
259                 let bench_run = hyperfine_command(
260                     0,
261                     run_runs,
262                     None,
263                     Command::new("./raytracer_cg_llvm"),
264                     Command::new("./raytracer_cg_clif"),
265                 );
266                 spawn_and_wait(bench_run);
267             } else {
268                 runner.run_cargo("clean", []);
269                 eprintln!("[BENCH COMPILE] ebobby/simple-raytracer (skipped)");
270                 eprintln!("[COMPILE] ebobby/simple-raytracer");
271                 runner.run_cargo("build", []);
272                 eprintln!("[BENCH RUN] ebobby/simple-raytracer (skipped)");
273             }
274         });
275     }),
276     TestCase::new("test.libcore", &|runner| {
277         runner.in_dir(
278             std::env::current_dir()
279                 .unwrap()
280                 .join("build_sysroot")
281                 .join("sysroot_src")
282                 .join("library")
283                 .join("core")
284                 .join("tests"),
285             |runner| {
286                 runner.run_cargo("clean", []);
287
288                 if runner.host_triple == runner.target_triple {
289                     runner.run_cargo("test", []);
290                 } else {
291                     eprintln!("Cross-Compiling: Not running tests");
292                     runner.run_cargo("build", ["--tests"]);
293                 }
294             },
295         );
296     }),
297     TestCase::new("test.regex-shootout-regex-dna", &|runner| {
298         runner.in_dir(prepare::REGEX.source_dir(), |runner| {
299             runner.run_cargo("clean", []);
300
301             // newer aho_corasick versions throw a deprecation warning
302             let lint_rust_flags = format!("{} --cap-lints warn", runner.rust_flags);
303
304             let mut build_cmd = runner.cargo_command("build", ["--example", "shootout-regex-dna"]);
305             build_cmd.env("RUSTFLAGS", lint_rust_flags.clone());
306             spawn_and_wait(build_cmd);
307
308             if runner.host_triple == runner.target_triple {
309                 let mut run_cmd = runner.cargo_command("run", ["--example", "shootout-regex-dna"]);
310                 run_cmd.env("RUSTFLAGS", lint_rust_flags);
311
312                 let input =
313                     fs::read_to_string(PathBuf::from("examples/regexdna-input.txt")).unwrap();
314                 let expected_path = PathBuf::from("examples/regexdna-output.txt");
315                 let expected = fs::read_to_string(&expected_path).unwrap();
316
317                 let output = spawn_and_wait_with_input(run_cmd, input);
318                 // Make sure `[codegen mono items] start` doesn't poison the diff
319                 let output = output
320                     .lines()
321                     .filter(|line| !line.contains("codegen mono items"))
322                     .chain(Some("")) // This just adds the trailing newline
323                     .collect::<Vec<&str>>()
324                     .join("\r\n");
325
326                 let output_matches = expected.lines().eq(output.lines());
327                 if !output_matches {
328                     let res_path = PathBuf::from("res.txt");
329                     fs::write(&res_path, &output).unwrap();
330
331                     if cfg!(windows) {
332                         println!("Output files don't match!");
333                         println!("Expected Output:\n{}", expected);
334                         println!("Actual Output:\n{}", output);
335                     } else {
336                         let mut diff = Command::new("diff");
337                         diff.arg("-u");
338                         diff.arg(res_path);
339                         diff.arg(expected_path);
340                         spawn_and_wait(diff);
341                     }
342
343                     std::process::exit(1);
344                 }
345             }
346         });
347     }),
348     TestCase::new("test.regex", &|runner| {
349         runner.in_dir(prepare::REGEX.source_dir(), |runner| {
350             runner.run_cargo("clean", []);
351
352             // newer aho_corasick versions throw a deprecation warning
353             let lint_rust_flags = format!("{} --cap-lints warn", runner.rust_flags);
354
355             if runner.host_triple == runner.target_triple {
356                 let mut run_cmd = runner.cargo_command(
357                     "test",
358                     [
359                         "--tests",
360                         "--",
361                         "--exclude-should-panic",
362                         "--test-threads",
363                         "1",
364                         "-Zunstable-options",
365                         "-q",
366                     ],
367                 );
368                 run_cmd.env("RUSTFLAGS", lint_rust_flags);
369                 spawn_and_wait(run_cmd);
370             } else {
371                 eprintln!("Cross-Compiling: Not running tests");
372                 let mut build_cmd =
373                     runner.cargo_command("build", ["--tests", "--target", &runner.target_triple]);
374                 build_cmd.env("RUSTFLAGS", lint_rust_flags.clone());
375                 spawn_and_wait(build_cmd);
376             }
377         });
378     }),
379     TestCase::new("test.portable-simd", &|runner| {
380         runner.in_dir(prepare::PORTABLE_SIMD.source_dir(), |runner| {
381             runner.run_cargo("clean", []);
382             runner.run_cargo("build", ["--all-targets", "--target", &runner.target_triple]);
383
384             if runner.host_triple == runner.target_triple {
385                 runner.run_cargo("test", ["-q"]);
386             }
387         });
388     }),
389 ];
390
391 pub(crate) fn run_tests(
392     channel: &str,
393     sysroot_kind: SysrootKind,
394     target_dir: &Path,
395     cg_clif_dylib: &Path,
396     host_triple: &str,
397     target_triple: &str,
398 ) {
399     let runner = TestRunner::new(host_triple.to_string(), target_triple.to_string());
400
401     if config::get_bool("testsuite.no_sysroot") {
402         build_sysroot::build_sysroot(
403             channel,
404             SysrootKind::None,
405             &target_dir,
406             cg_clif_dylib,
407             &host_triple,
408             &target_triple,
409         );
410
411         let _ = fs::remove_dir_all(Path::new("target").join("out"));
412         runner.run_testsuite(NO_SYSROOT_SUITE);
413     } else {
414         eprintln!("[SKIP] no_sysroot tests");
415     }
416
417     let run_base_sysroot = config::get_bool("testsuite.base_sysroot");
418     let run_extended_sysroot = config::get_bool("testsuite.extended_sysroot");
419
420     if run_base_sysroot || run_extended_sysroot {
421         build_sysroot::build_sysroot(
422             channel,
423             sysroot_kind,
424             &target_dir,
425             cg_clif_dylib,
426             &host_triple,
427             &target_triple,
428         );
429     }
430
431     if run_base_sysroot {
432         runner.run_testsuite(BASE_SYSROOT_SUITE);
433     } else {
434         eprintln!("[SKIP] base_sysroot tests");
435     }
436
437     if run_extended_sysroot {
438         runner.run_testsuite(EXTENDED_SYSROOT_SUITE);
439     } else {
440         eprintln!("[SKIP] extended_sysroot tests");
441     }
442 }
443
444 struct TestRunner {
445     root_dir: PathBuf,
446     out_dir: PathBuf,
447     jit_supported: bool,
448     rust_flags: String,
449     run_wrapper: Vec<String>,
450     host_triple: String,
451     target_triple: String,
452 }
453
454 impl TestRunner {
455     pub fn new(host_triple: String, target_triple: String) -> Self {
456         let root_dir = env::current_dir().unwrap();
457
458         let mut out_dir = root_dir.clone();
459         out_dir.push("target");
460         out_dir.push("out");
461
462         let is_native = host_triple == target_triple;
463         let jit_supported =
464             target_triple.contains("x86_64") && is_native && !host_triple.contains("windows");
465
466         let mut rust_flags = env::var("RUSTFLAGS").ok().unwrap_or("".to_string());
467         let mut run_wrapper = Vec::new();
468
469         if !is_native {
470             match target_triple.as_str() {
471                 "aarch64-unknown-linux-gnu" => {
472                     // We are cross-compiling for aarch64. Use the correct linker and run tests in qemu.
473                     rust_flags = format!("-Clinker=aarch64-linux-gnu-gcc{}", rust_flags);
474                     run_wrapper = vec!["qemu-aarch64", "-L", "/usr/aarch64-linux-gnu"];
475                 }
476                 "x86_64-pc-windows-gnu" => {
477                     // We are cross-compiling for Windows. Run tests in wine.
478                     run_wrapper = vec!["wine"];
479                 }
480                 _ => {
481                     println!("Unknown non-native platform");
482                 }
483             }
484         }
485
486         // FIXME fix `#[linkage = "extern_weak"]` without this
487         if host_triple.contains("darwin") {
488             rust_flags = format!("{} -Clink-arg=-undefined -Clink-arg=dynamic_lookup", rust_flags);
489         }
490
491         Self {
492             root_dir,
493             out_dir,
494             jit_supported,
495             rust_flags,
496             run_wrapper: run_wrapper.iter().map(|s| s.to_string()).collect(),
497             host_triple,
498             target_triple,
499         }
500     }
501
502     pub fn run_testsuite(&self, tests: &[TestCase]) {
503         for &TestCase { config, func } in tests {
504             let (tag, testname) = config.split_once('.').unwrap();
505             let tag = tag.to_uppercase();
506             let is_jit_test = tag == "JIT";
507
508             if !config::get_bool(config) || (is_jit_test && !self.jit_supported) {
509                 eprintln!("[{tag}] {testname} (skipped)");
510                 continue;
511             } else {
512                 eprintln!("[{tag}] {testname}");
513             }
514
515             func(self);
516         }
517     }
518
519     fn in_dir(&self, new: impl AsRef<Path>, callback: impl FnOnce(&TestRunner)) {
520         let current = env::current_dir().unwrap();
521
522         env::set_current_dir(new).unwrap();
523         callback(self);
524         env::set_current_dir(current).unwrap();
525     }
526
527     fn rustc_command<I, S>(&self, args: I) -> Command
528     where
529         I: IntoIterator<Item = S>,
530         S: AsRef<OsStr>,
531     {
532         let mut rustc_clif = self.root_dir.clone();
533         rustc_clif.push("build");
534         rustc_clif.push(get_wrapper_file_name("rustc-clif", "bin"));
535
536         let mut cmd = Command::new(rustc_clif);
537         cmd.args(self.rust_flags.split_whitespace());
538         cmd.arg("-L");
539         cmd.arg(format!("crate={}", self.out_dir.display()));
540         cmd.arg("--out-dir");
541         cmd.arg(format!("{}", self.out_dir.display()));
542         cmd.arg("-Cdebuginfo=2");
543         cmd.args(args);
544         cmd
545     }
546
547     fn run_rustc<I, S>(&self, args: I)
548     where
549         I: IntoIterator<Item = S>,
550         S: AsRef<OsStr>,
551     {
552         spawn_and_wait(self.rustc_command(args));
553     }
554
555     fn run_out_command<'a, I>(&self, name: &str, args: I)
556     where
557         I: IntoIterator<Item = &'a str>,
558     {
559         let mut full_cmd = vec![];
560
561         // Prepend the RUN_WRAPPER's
562         if !self.run_wrapper.is_empty() {
563             full_cmd.extend(self.run_wrapper.iter().cloned());
564         }
565
566         full_cmd.push({
567             let mut out_path = self.out_dir.clone();
568             out_path.push(name);
569             out_path.to_str().unwrap().to_string()
570         });
571
572         for arg in args.into_iter() {
573             full_cmd.push(arg.to_string());
574         }
575
576         let mut cmd_iter = full_cmd.into_iter();
577         let first = cmd_iter.next().unwrap();
578
579         let mut cmd = Command::new(first);
580         cmd.args(cmd_iter);
581
582         spawn_and_wait(cmd);
583     }
584
585     fn cargo_command<'a, I>(&self, subcommand: &str, args: I) -> Command
586     where
587         I: IntoIterator<Item = &'a str>,
588     {
589         let mut cargo_clif = self.root_dir.clone();
590         cargo_clif.push("build");
591         cargo_clif.push(get_wrapper_file_name("cargo-clif", "bin"));
592
593         let mut cmd = cargo_command(
594             cargo_clif,
595             subcommand,
596             if subcommand == "clean" { None } else { Some(&self.target_triple) },
597             Path::new("."),
598         );
599         cmd.args(args);
600         cmd.env("RUSTFLAGS", &self.rust_flags);
601         cmd
602     }
603
604     fn run_cargo<'a, I>(&self, subcommand: &str, args: I)
605     where
606         I: IntoIterator<Item = &'a str>,
607     {
608         spawn_and_wait(self.cargo_command(subcommand, args));
609     }
610 }