1 use super::build_sysroot;
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;
10 use std::path::{Path, PathBuf};
11 use std::process::Command;
15 func: &'static dyn Fn(&TestRunner),
19 const fn new(config: &'static str, func: &'static dyn Fn(&TestRunner)) -> Self {
24 const NO_SYSROOT_SUITE: &[TestCase] = &[
25 TestCase::new("build.mini_core", &|runner| {
27 "example/mini_core.rs",
33 &runner.target_triple,
36 TestCase::new("build.example", &|runner| {
42 &runner.target_triple,
45 TestCase::new("jit.mini_core_hello_world", &|runner| {
46 let mut jit_cmd = runner.rustc_command([
48 "-Cllvm-args=mode=jit",
50 "example/mini_core_hello_world.rs",
56 jit_cmd.env("CG_CLIF_JIT_ARGS", "abc bcd");
57 spawn_and_wait(jit_cmd);
59 eprintln!("[JIT-lazy] mini_core_hello_world");
60 let mut jit_cmd = runner.rustc_command([
62 "-Cllvm-args=mode=jit-lazy",
64 "example/mini_core_hello_world.rs",
70 jit_cmd.env("CG_CLIF_JIT_ARGS", "abc bcd");
71 spawn_and_wait(jit_cmd);
73 TestCase::new("aot.mini_core_hello_world", &|runner| {
75 "example/mini_core_hello_world.rs",
77 "mini_core_hello_world",
82 &runner.target_triple,
84 runner.run_out_command("mini_core_hello_world", ["abc", "bcd"]);
88 const BASE_SYSROOT_SUITE: &[TestCase] = &[
89 TestCase::new("aot.arbitrary_self_types_pointers_and_wrappers", &|runner| {
91 "example/arbitrary_self_types_pointers_and_wrappers.rs",
93 "arbitrary_self_types_pointers_and_wrappers",
97 &runner.target_triple,
99 runner.run_out_command("arbitrary_self_types_pointers_and_wrappers", []);
101 TestCase::new("aot.issue_91827_extern_types", &|runner| {
103 "example/issue-91827-extern-types.rs",
105 "issue_91827_extern_types",
109 &runner.target_triple,
111 runner.run_out_command("issue_91827_extern_types", []);
113 TestCase::new("build.alloc_system", &|runner| {
115 "example/alloc_system.rs",
119 &runner.target_triple,
122 TestCase::new("aot.alloc_example", &|runner| {
124 "example/alloc_example.rs",
128 &runner.target_triple,
130 runner.run_out_command("alloc_example", []);
132 TestCase::new("jit.std_example", &|runner| {
134 "-Zunstable-options",
135 "-Cllvm-args=mode=jit",
137 "example/std_example.rs",
142 eprintln!("[JIT-lazy] std_example");
144 "-Zunstable-options",
145 "-Cllvm-args=mode=jit-lazy",
147 "example/std_example.rs",
152 TestCase::new("aot.std_example", &|runner| {
154 "example/std_example.rs",
158 &runner.target_triple,
160 runner.run_out_command("std_example", ["arg"]);
162 TestCase::new("aot.dst_field_align", &|runner| {
164 "example/dst-field-align.rs",
170 &runner.target_triple,
172 runner.run_out_command("dst_field_align", []);
174 TestCase::new("aot.subslice-patterns-const-eval", &|runner| {
176 "example/subslice-patterns-const-eval.rs",
181 &runner.target_triple,
183 runner.run_out_command("subslice-patterns-const-eval", []);
185 TestCase::new("aot.track-caller-attribute", &|runner| {
187 "example/track-caller-attribute.rs",
192 &runner.target_triple,
194 runner.run_out_command("track-caller-attribute", []);
196 TestCase::new("aot.float-minmax-pass", &|runner| {
198 "example/float-minmax-pass.rs",
203 &runner.target_triple,
205 runner.run_out_command("float-minmax-pass", []);
207 TestCase::new("aot.mod_bench", &|runner| {
209 "example/mod_bench.rs",
213 &runner.target_triple,
215 runner.run_out_command("mod_bench", []);
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", []);
224 if runner.host_triple == runner.target_triple {
225 eprintln!("[TEST] rust-random/rand");
226 runner.run_cargo("test", ["--workspace"]);
228 eprintln!("[AOT] rust-random/rand");
229 runner.run_cargo("build", ["--workspace", "--tests"]);
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();
237 if runner.host_triple == runner.target_triple {
238 eprintln!("[BENCH COMPILE] ebobby/simple-raytracer");
239 let prepare = runner.cargo_command("clean", []);
241 let llvm_build_cmd = cargo_command("cargo", "build", None, Path::new("."));
243 let cargo_clif = runner
247 .join(get_wrapper_file_name("cargo-clif", "bin"));
248 let clif_build_cmd = cargo_command(cargo_clif, "build", None, Path::new("."));
251 hyperfine_command(1, run_runs, Some(prepare), llvm_build_cmd, clif_build_cmd);
253 spawn_and_wait(bench_compile);
255 eprintln!("[BENCH RUN] ebobby/simple-raytracer");
256 fs::copy(PathBuf::from("./target/debug/main"), PathBuf::from("raytracer_cg_clif"))
259 let bench_run = hyperfine_command(
263 Command::new("./raytracer_cg_llvm"),
264 Command::new("./raytracer_cg_clif"),
266 spawn_and_wait(bench_run);
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)");
276 TestCase::new("test.libcore", &|runner| {
278 std::env::current_dir()
280 .join("build_sysroot")
286 runner.run_cargo("clean", []);
288 if runner.host_triple == runner.target_triple {
289 runner.run_cargo("test", []);
291 eprintln!("Cross-Compiling: Not running tests");
292 runner.run_cargo("build", ["--tests"]);
297 TestCase::new("test.regex-shootout-regex-dna", &|runner| {
298 runner.in_dir(prepare::REGEX.source_dir(), |runner| {
299 runner.run_cargo("clean", []);
301 // newer aho_corasick versions throw a deprecation warning
302 let lint_rust_flags = format!("{} --cap-lints warn", runner.rust_flags);
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);
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);
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();
317 let output = spawn_and_wait_with_input(run_cmd, input);
318 // Make sure `[codegen mono items] start` doesn't poison the diff
321 .filter(|line| !line.contains("codegen mono items"))
322 .chain(Some("")) // This just adds the trailing newline
323 .collect::<Vec<&str>>()
326 let output_matches = expected.lines().eq(output.lines());
328 let res_path = PathBuf::from("res.txt");
329 fs::write(&res_path, &output).unwrap();
332 println!("Output files don't match!");
333 println!("Expected Output:\n{}", expected);
334 println!("Actual Output:\n{}", output);
336 let mut diff = Command::new("diff");
339 diff.arg(expected_path);
340 spawn_and_wait(diff);
343 std::process::exit(1);
348 TestCase::new("test.regex", &|runner| {
349 runner.in_dir(prepare::REGEX.source_dir(), |runner| {
350 runner.run_cargo("clean", []);
352 // newer aho_corasick versions throw a deprecation warning
353 let lint_rust_flags = format!("{} --cap-lints warn", runner.rust_flags);
355 if runner.host_triple == runner.target_triple {
356 let mut run_cmd = runner.cargo_command(
361 "--exclude-should-panic",
364 "-Zunstable-options",
368 run_cmd.env("RUSTFLAGS", lint_rust_flags);
369 spawn_and_wait(run_cmd);
371 eprintln!("Cross-Compiling: Not running tests");
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);
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]);
384 if runner.host_triple == runner.target_triple {
385 runner.run_cargo("test", ["-q"]);
391 pub(crate) fn run_tests(
393 sysroot_kind: SysrootKind,
395 cg_clif_dylib: &Path,
399 let runner = TestRunner::new(host_triple.to_string(), target_triple.to_string());
401 if config::get_bool("testsuite.no_sysroot") {
402 build_sysroot::build_sysroot(
411 let _ = fs::remove_dir_all(Path::new("target").join("out"));
412 runner.run_testsuite(NO_SYSROOT_SUITE);
414 eprintln!("[SKIP] no_sysroot tests");
417 let run_base_sysroot = config::get_bool("testsuite.base_sysroot");
418 let run_extended_sysroot = config::get_bool("testsuite.extended_sysroot");
420 if run_base_sysroot || run_extended_sysroot {
421 build_sysroot::build_sysroot(
431 if run_base_sysroot {
432 runner.run_testsuite(BASE_SYSROOT_SUITE);
434 eprintln!("[SKIP] base_sysroot tests");
437 if run_extended_sysroot {
438 runner.run_testsuite(EXTENDED_SYSROOT_SUITE);
440 eprintln!("[SKIP] extended_sysroot tests");
449 run_wrapper: Vec<String>,
451 target_triple: String,
455 pub fn new(host_triple: String, target_triple: String) -> Self {
456 let root_dir = env::current_dir().unwrap();
458 let mut out_dir = root_dir.clone();
459 out_dir.push("target");
462 let is_native = host_triple == target_triple;
464 target_triple.contains("x86_64") && is_native && !host_triple.contains("windows");
466 let mut rust_flags = env::var("RUSTFLAGS").ok().unwrap_or("".to_string());
467 let mut run_wrapper = Vec::new();
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"];
476 "x86_64-pc-windows-gnu" => {
477 // We are cross-compiling for Windows. Run tests in wine.
478 run_wrapper = vec!["wine"];
481 println!("Unknown non-native platform");
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);
496 run_wrapper: run_wrapper.iter().map(|s| s.to_string()).collect(),
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";
508 if !config::get_bool(config) || (is_jit_test && !self.jit_supported) {
509 eprintln!("[{tag}] {testname} (skipped)");
512 eprintln!("[{tag}] {testname}");
519 fn in_dir(&self, new: impl AsRef<Path>, callback: impl FnOnce(&TestRunner)) {
520 let current = env::current_dir().unwrap();
522 env::set_current_dir(new).unwrap();
524 env::set_current_dir(current).unwrap();
527 fn rustc_command<I, S>(&self, args: I) -> Command
529 I: IntoIterator<Item = S>,
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"));
536 let mut cmd = Command::new(rustc_clif);
537 cmd.args(self.rust_flags.split_whitespace());
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");
547 fn run_rustc<I, S>(&self, args: I)
549 I: IntoIterator<Item = S>,
552 spawn_and_wait(self.rustc_command(args));
555 fn run_out_command<'a, I>(&self, name: &str, args: I)
557 I: IntoIterator<Item = &'a str>,
559 let mut full_cmd = vec![];
561 // Prepend the RUN_WRAPPER's
562 if !self.run_wrapper.is_empty() {
563 full_cmd.extend(self.run_wrapper.iter().cloned());
567 let mut out_path = self.out_dir.clone();
569 out_path.to_str().unwrap().to_string()
572 for arg in args.into_iter() {
573 full_cmd.push(arg.to_string());
576 let mut cmd_iter = full_cmd.into_iter();
577 let first = cmd_iter.next().unwrap();
579 let mut cmd = Command::new(first);
585 fn cargo_command<'a, I>(&self, subcommand: &str, args: I) -> Command
587 I: IntoIterator<Item = &'a str>,
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"));
593 let mut cmd = cargo_command(
596 if subcommand == "clean" { None } else { Some(&self.target_triple) },
600 cmd.env("RUSTFLAGS", &self.rust_flags);
604 fn run_cargo<'a, I>(&self, subcommand: &str, args: I)
606 I: IntoIterator<Item = &'a str>,
608 spawn_and_wait(self.cargo_command(subcommand, args));