]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/tests/lang_tests_common.rs
Rollup merge of #106739 - WaffleLapkin:astconv, r=estebank
[rust.git] / compiler / rustc_codegen_gcc / tests / lang_tests_common.rs
1 //! The common code for `tests/lang_tests_*.rs`
2 use std::{
3     env::{self, current_dir},
4     path::PathBuf,
5     process::Command,
6 };
7
8 use lang_tester::LangTester;
9 use tempfile::TempDir;
10
11 /// Controls the compile options (e.g., optimization level) used to compile
12 /// test code.
13 #[allow(dead_code)] // Each test crate picks one variant
14 pub enum Profile {
15     Debug,
16     Release,
17 }
18
19 pub fn main_inner(profile: Profile) {
20     let tempdir = TempDir::new().expect("temp dir");
21     let current_dir = current_dir().expect("current dir");
22     let current_dir = current_dir.to_str().expect("current dir").to_string();
23     let gcc_path = include_str!("../gcc_path");
24     let gcc_path = gcc_path.trim();
25     env::set_var("LD_LIBRARY_PATH", gcc_path);
26     LangTester::new()
27         .test_dir("tests/run")
28         .test_file_filter(|path| path.extension().expect("extension").to_str().expect("to_str") == "rs")
29         .test_extract(|source| {
30             let lines =
31                 source.lines()
32                     .skip_while(|l| !l.starts_with("//"))
33                     .take_while(|l| l.starts_with("//"))
34                     .map(|l| &l[2..])
35                     .collect::<Vec<_>>()
36                     .join("\n");
37             Some(lines)
38         })
39         .test_cmds(move |path| {
40             // Test command 1: Compile `x.rs` into `tempdir/x`.
41             let mut exe = PathBuf::new();
42             exe.push(&tempdir);
43             exe.push(path.file_stem().expect("file_stem"));
44             let mut compiler = Command::new("rustc");
45             compiler.args(&[
46                 &format!("-Zcodegen-backend={}/target/debug/librustc_codegen_gcc.so", current_dir),
47                 "--sysroot", &format!("{}/build_sysroot/sysroot/", current_dir),
48                 "-Zno-parallel-llvm",
49                 "-C", "panic=abort",
50                 "-C", "link-arg=-lc",
51                 "-o", exe.to_str().expect("to_str"),
52                 path.to_str().expect("to_str"),
53             ]);
54             match profile {
55                 Profile::Debug => {}
56                 Profile::Release => {
57                     compiler.args(&[
58                         "-C", "opt-level=3",
59                         "-C", "lto=no",
60                     ]);
61                 }
62             }
63             // Test command 2: run `tempdir/x`.
64             let runtime = Command::new(exe);
65             vec![("Compiler", compiler), ("Run-time", runtime)]
66         })
67         .run();
68 }