]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/build_system/tests.rs
Merge commit '7d53619064ab7045c383644cb445052d2a3d46db' into sync_cg_clif-2023-02-09
[rust.git] / compiler / rustc_codegen_cranelift / build_system / tests.rs
1 use super::build_sysroot::{self, SYSROOT_SRC};
2 use super::config;
3 use super::path::{Dirs, RelPath};
4 use super::prepare::GitRepo;
5 use super::rustc_info::get_host_triple;
6 use super::utils::{spawn_and_wait, spawn_and_wait_with_input, CargoProject, Compiler};
7 use super::SysrootKind;
8 use std::env;
9 use std::ffi::OsStr;
10 use std::fs;
11 use std::path::Path;
12 use std::process::Command;
13
14 static BUILD_EXAMPLE_OUT_DIR: RelPath = RelPath::BUILD.join("example");
15
16 struct TestCase {
17     config: &'static str,
18     cmd: TestCaseCmd,
19 }
20
21 enum TestCaseCmd {
22     Custom { func: &'static dyn Fn(&TestRunner) },
23     BuildLib { source: &'static str, crate_types: &'static str },
24     BuildBinAndRun { source: &'static str, args: &'static [&'static str] },
25     JitBin { source: &'static str, args: &'static str },
26 }
27
28 impl TestCase {
29     // FIXME reduce usage of custom test case commands
30     const fn custom(config: &'static str, func: &'static dyn Fn(&TestRunner)) -> Self {
31         Self { config, cmd: TestCaseCmd::Custom { func } }
32     }
33
34     const fn build_lib(
35         config: &'static str,
36         source: &'static str,
37         crate_types: &'static str,
38     ) -> Self {
39         Self { config, cmd: TestCaseCmd::BuildLib { source, crate_types } }
40     }
41
42     const fn build_bin_and_run(
43         config: &'static str,
44         source: &'static str,
45         args: &'static [&'static str],
46     ) -> Self {
47         Self { config, cmd: TestCaseCmd::BuildBinAndRun { source, args } }
48     }
49
50     const fn jit_bin(config: &'static str, source: &'static str, args: &'static str) -> Self {
51         Self { config, cmd: TestCaseCmd::JitBin { source, args } }
52     }
53 }
54
55 const NO_SYSROOT_SUITE: &[TestCase] = &[
56     TestCase::build_lib("build.mini_core", "example/mini_core.rs", "lib,dylib"),
57     TestCase::build_lib("build.example", "example/example.rs", "lib"),
58     TestCase::jit_bin("jit.mini_core_hello_world", "example/mini_core_hello_world.rs", "abc bcd"),
59     TestCase::build_bin_and_run(
60         "aot.mini_core_hello_world",
61         "example/mini_core_hello_world.rs",
62         &["abc", "bcd"],
63     ),
64 ];
65
66 const BASE_SYSROOT_SUITE: &[TestCase] = &[
67     TestCase::build_bin_and_run(
68         "aot.arbitrary_self_types_pointers_and_wrappers",
69         "example/arbitrary_self_types_pointers_and_wrappers.rs",
70         &[],
71     ),
72     TestCase::build_bin_and_run(
73         "aot.issue_91827_extern_types",
74         "example/issue-91827-extern-types.rs",
75         &[],
76     ),
77     TestCase::build_lib("build.alloc_system", "example/alloc_system.rs", "lib"),
78     TestCase::build_bin_and_run("aot.alloc_example", "example/alloc_example.rs", &[]),
79     TestCase::jit_bin("jit.std_example", "example/std_example.rs", ""),
80     TestCase::build_bin_and_run("aot.std_example", "example/std_example.rs", &["arg"]),
81     TestCase::build_bin_and_run("aot.dst_field_align", "example/dst-field-align.rs", &[]),
82     TestCase::build_bin_and_run(
83         "aot.subslice-patterns-const-eval",
84         "example/subslice-patterns-const-eval.rs",
85         &[],
86     ),
87     TestCase::build_bin_and_run(
88         "aot.track-caller-attribute",
89         "example/track-caller-attribute.rs",
90         &[],
91     ),
92     TestCase::build_bin_and_run("aot.float-minmax-pass", "example/float-minmax-pass.rs", &[]),
93     TestCase::build_bin_and_run("aot.mod_bench", "example/mod_bench.rs", &[]),
94     TestCase::build_bin_and_run("aot.issue-72793", "example/issue-72793.rs", &[]),
95 ];
96
97 pub(crate) static RAND_REPO: GitRepo =
98     GitRepo::github("rust-random", "rand", "0f933f9c7176e53b2a3c7952ded484e1783f0bf1", "rand");
99
100 pub(crate) static RAND: CargoProject = CargoProject::new(&RAND_REPO.source_dir(), "rand");
101
102 pub(crate) static REGEX_REPO: GitRepo =
103     GitRepo::github("rust-lang", "regex", "341f207c1071f7290e3f228c710817c280c8dca1", "regex");
104
105 pub(crate) static REGEX: CargoProject = CargoProject::new(&REGEX_REPO.source_dir(), "regex");
106
107 pub(crate) static PORTABLE_SIMD_REPO: GitRepo = GitRepo::github(
108     "rust-lang",
109     "portable-simd",
110     "582239ac3b32007613df04d7ffa78dc30f4c5645",
111     "portable-simd",
112 );
113
114 pub(crate) static PORTABLE_SIMD: CargoProject =
115     CargoProject::new(&PORTABLE_SIMD_REPO.source_dir(), "portable_simd");
116
117 pub(crate) static LIBCORE_TESTS: CargoProject =
118     CargoProject::new(&SYSROOT_SRC.join("library/core/tests"), "core_tests");
119
120 const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[
121     TestCase::custom("test.rust-random/rand", &|runner| {
122         RAND.clean(&runner.dirs);
123
124         if runner.is_native {
125             eprintln!("[TEST] rust-random/rand");
126             let mut test_cmd = RAND.test(&runner.target_compiler, &runner.dirs);
127             test_cmd.arg("--workspace");
128             spawn_and_wait(test_cmd);
129         } else {
130             eprintln!("[AOT] rust-random/rand");
131             let mut build_cmd = RAND.build(&runner.target_compiler, &runner.dirs);
132             build_cmd.arg("--workspace").arg("--tests");
133             spawn_and_wait(build_cmd);
134         }
135     }),
136     TestCase::custom("test.libcore", &|runner| {
137         LIBCORE_TESTS.clean(&runner.dirs);
138
139         if runner.is_native {
140             spawn_and_wait(LIBCORE_TESTS.test(&runner.target_compiler, &runner.dirs));
141         } else {
142             eprintln!("Cross-Compiling: Not running tests");
143             let mut build_cmd = LIBCORE_TESTS.build(&runner.target_compiler, &runner.dirs);
144             build_cmd.arg("--tests");
145             spawn_and_wait(build_cmd);
146         }
147     }),
148     TestCase::custom("test.regex-shootout-regex-dna", &|runner| {
149         REGEX.clean(&runner.dirs);
150
151         // newer aho_corasick versions throw a deprecation warning
152         let lint_rust_flags = format!("{} --cap-lints warn", runner.target_compiler.rustflags);
153
154         let mut build_cmd = REGEX.build(&runner.target_compiler, &runner.dirs);
155         build_cmd.arg("--example").arg("shootout-regex-dna");
156         build_cmd.env("RUSTFLAGS", lint_rust_flags.clone());
157         spawn_and_wait(build_cmd);
158
159         if runner.is_native {
160             let mut run_cmd = REGEX.run(&runner.target_compiler, &runner.dirs);
161             run_cmd.arg("--example").arg("shootout-regex-dna");
162             run_cmd.env("RUSTFLAGS", lint_rust_flags);
163
164             let input = fs::read_to_string(
165                 REGEX.source_dir(&runner.dirs).join("examples").join("regexdna-input.txt"),
166             )
167             .unwrap();
168             let expected = fs::read_to_string(
169                 REGEX.source_dir(&runner.dirs).join("examples").join("regexdna-output.txt"),
170             )
171             .unwrap();
172
173             let output = spawn_and_wait_with_input(run_cmd, input);
174             // Make sure `[codegen mono items] start` doesn't poison the diff
175             let output = output
176                 .lines()
177                 .filter(|line| !line.contains("codegen mono items"))
178                 .chain(Some("")) // This just adds the trailing newline
179                 .collect::<Vec<&str>>()
180                 .join("\r\n");
181
182             let output_matches = expected.lines().eq(output.lines());
183             if !output_matches {
184                 println!("Output files don't match!");
185                 println!("Expected Output:\n{}", expected);
186                 println!("Actual Output:\n{}", output);
187
188                 std::process::exit(1);
189             }
190         }
191     }),
192     TestCase::custom("test.regex", &|runner| {
193         REGEX.clean(&runner.dirs);
194
195         // newer aho_corasick versions throw a deprecation warning
196         let lint_rust_flags = format!("{} --cap-lints warn", runner.target_compiler.rustflags);
197
198         if runner.is_native {
199             let mut run_cmd = REGEX.test(&runner.target_compiler, &runner.dirs);
200             run_cmd.args([
201                 "--tests",
202                 "--",
203                 "--exclude-should-panic",
204                 "--test-threads",
205                 "1",
206                 "-Zunstable-options",
207                 "-q",
208             ]);
209             run_cmd.env("RUSTFLAGS", lint_rust_flags);
210             spawn_and_wait(run_cmd);
211         } else {
212             eprintln!("Cross-Compiling: Not running tests");
213             let mut build_cmd = REGEX.build(&runner.target_compiler, &runner.dirs);
214             build_cmd.arg("--tests");
215             build_cmd.env("RUSTFLAGS", lint_rust_flags.clone());
216             spawn_and_wait(build_cmd);
217         }
218     }),
219     TestCase::custom("test.portable-simd", &|runner| {
220         PORTABLE_SIMD.clean(&runner.dirs);
221
222         let mut build_cmd = PORTABLE_SIMD.build(&runner.target_compiler, &runner.dirs);
223         build_cmd.arg("--all-targets");
224         spawn_and_wait(build_cmd);
225
226         if runner.is_native {
227             let mut test_cmd = PORTABLE_SIMD.test(&runner.target_compiler, &runner.dirs);
228             test_cmd.arg("-q");
229             spawn_and_wait(test_cmd);
230         }
231     }),
232 ];
233
234 pub(crate) fn run_tests(
235     dirs: &Dirs,
236     channel: &str,
237     sysroot_kind: SysrootKind,
238     cg_clif_dylib: &Path,
239     bootstrap_host_compiler: &Compiler,
240     target_triple: String,
241 ) {
242     if config::get_bool("testsuite.no_sysroot") {
243         let target_compiler = build_sysroot::build_sysroot(
244             dirs,
245             channel,
246             SysrootKind::None,
247             cg_clif_dylib,
248             bootstrap_host_compiler,
249             target_triple.clone(),
250         );
251
252         let runner =
253             TestRunner::new(dirs.clone(), target_compiler, get_host_triple() == target_triple);
254
255         BUILD_EXAMPLE_OUT_DIR.ensure_fresh(dirs);
256         runner.run_testsuite(NO_SYSROOT_SUITE);
257     } else {
258         eprintln!("[SKIP] no_sysroot tests");
259     }
260
261     let run_base_sysroot = config::get_bool("testsuite.base_sysroot");
262     let run_extended_sysroot = config::get_bool("testsuite.extended_sysroot");
263
264     if run_base_sysroot || run_extended_sysroot {
265         let target_compiler = build_sysroot::build_sysroot(
266             dirs,
267             channel,
268             sysroot_kind,
269             cg_clif_dylib,
270             bootstrap_host_compiler,
271             target_triple.clone(),
272         );
273
274         let runner =
275             TestRunner::new(dirs.clone(), target_compiler, get_host_triple() == target_triple);
276
277         if run_base_sysroot {
278             runner.run_testsuite(BASE_SYSROOT_SUITE);
279         } else {
280             eprintln!("[SKIP] base_sysroot tests");
281         }
282
283         if run_extended_sysroot {
284             runner.run_testsuite(EXTENDED_SYSROOT_SUITE);
285         } else {
286             eprintln!("[SKIP] extended_sysroot tests");
287         }
288     }
289 }
290
291 struct TestRunner {
292     is_native: bool,
293     jit_supported: bool,
294     dirs: Dirs,
295     target_compiler: Compiler,
296 }
297
298 impl TestRunner {
299     pub fn new(dirs: Dirs, mut target_compiler: Compiler, is_native: bool) -> Self {
300         if let Ok(rustflags) = env::var("RUSTFLAGS") {
301             target_compiler.rustflags.push(' ');
302             target_compiler.rustflags.push_str(&rustflags);
303         }
304         if let Ok(rustdocflags) = env::var("RUSTDOCFLAGS") {
305             target_compiler.rustdocflags.push(' ');
306             target_compiler.rustdocflags.push_str(&rustdocflags);
307         }
308
309         // FIXME fix `#[linkage = "extern_weak"]` without this
310         if target_compiler.triple.contains("darwin") {
311             target_compiler.rustflags.push_str(" -Clink-arg=-undefined -Clink-arg=dynamic_lookup");
312         }
313
314         let jit_supported = is_native
315             && target_compiler.triple.contains("x86_64")
316             && !target_compiler.triple.contains("windows");
317
318         Self { is_native, jit_supported, dirs, target_compiler }
319     }
320
321     pub fn run_testsuite(&self, tests: &[TestCase]) {
322         for TestCase { config, cmd } in tests {
323             let (tag, testname) = config.split_once('.').unwrap();
324             let tag = tag.to_uppercase();
325             let is_jit_test = tag == "JIT";
326
327             if !config::get_bool(config) || (is_jit_test && !self.jit_supported) {
328                 eprintln!("[{tag}] {testname} (skipped)");
329                 continue;
330             } else {
331                 eprintln!("[{tag}] {testname}");
332             }
333
334             match *cmd {
335                 TestCaseCmd::Custom { func } => func(self),
336                 TestCaseCmd::BuildLib { source, crate_types } => {
337                     self.run_rustc([source, "--crate-type", crate_types]);
338                 }
339                 TestCaseCmd::BuildBinAndRun { source, args } => {
340                     self.run_rustc([source]);
341                     self.run_out_command(
342                         source.split('/').last().unwrap().split('.').next().unwrap(),
343                         args,
344                     );
345                 }
346                 TestCaseCmd::JitBin { source, args } => {
347                     let mut jit_cmd = self.rustc_command([
348                         "-Zunstable-options",
349                         "-Cllvm-args=mode=jit",
350                         "-Cprefer-dynamic",
351                         source,
352                         "--cfg",
353                         "jit",
354                     ]);
355                     if !args.is_empty() {
356                         jit_cmd.env("CG_CLIF_JIT_ARGS", args);
357                     }
358                     spawn_and_wait(jit_cmd);
359
360                     eprintln!("[JIT-lazy] {testname}");
361                     let mut jit_cmd = self.rustc_command([
362                         "-Zunstable-options",
363                         "-Cllvm-args=mode=jit-lazy",
364                         "-Cprefer-dynamic",
365                         source,
366                         "--cfg",
367                         "jit",
368                     ]);
369                     if !args.is_empty() {
370                         jit_cmd.env("CG_CLIF_JIT_ARGS", args);
371                     }
372                     spawn_and_wait(jit_cmd);
373                 }
374             }
375         }
376     }
377
378     #[must_use]
379     fn rustc_command<I, S>(&self, args: I) -> Command
380     where
381         I: IntoIterator<Item = S>,
382         S: AsRef<OsStr>,
383     {
384         let mut cmd = Command::new(&self.target_compiler.rustc);
385         cmd.args(self.target_compiler.rustflags.split_whitespace());
386         cmd.arg("-L");
387         cmd.arg(format!("crate={}", BUILD_EXAMPLE_OUT_DIR.to_path(&self.dirs).display()));
388         cmd.arg("--out-dir");
389         cmd.arg(format!("{}", BUILD_EXAMPLE_OUT_DIR.to_path(&self.dirs).display()));
390         cmd.arg("-Cdebuginfo=2");
391         cmd.arg("--target");
392         cmd.arg(&self.target_compiler.triple);
393         cmd.arg("-Cpanic=abort");
394         cmd.args(args);
395         cmd
396     }
397
398     fn run_rustc<I, S>(&self, args: I)
399     where
400         I: IntoIterator<Item = S>,
401         S: AsRef<OsStr>,
402     {
403         spawn_and_wait(self.rustc_command(args));
404     }
405
406     fn run_out_command<'a>(&self, name: &str, args: &[&str]) {
407         let mut full_cmd = vec![];
408
409         // Prepend the RUN_WRAPPER's
410         if !self.target_compiler.runner.is_empty() {
411             full_cmd.extend(self.target_compiler.runner.iter().cloned());
412         }
413
414         full_cmd.push(
415             BUILD_EXAMPLE_OUT_DIR.to_path(&self.dirs).join(name).to_str().unwrap().to_string(),
416         );
417
418         for arg in args {
419             full_cmd.push(arg.to_string());
420         }
421
422         let mut cmd_iter = full_cmd.into_iter();
423         let first = cmd_iter.next().unwrap();
424
425         let mut cmd = Command::new(first);
426         cmd.args(cmd_iter);
427
428         spawn_and_wait(cmd);
429     }
430 }