]> git.lizzy.rs Git - rust.git/blob - tests/compiletest.rs
move tests with MIR-opt to their own function we we can run them separately
[rust.git] / tests / compiletest.rs
1 #![feature(slice_concat_ext)]
2
3 extern crate compiletest_rs as compiletest;
4
5 use std::slice::SliceConcatExt;
6 use std::path::{PathBuf, Path};
7 use std::io::Write;
8
9 macro_rules! eprintln {
10     ($($arg:tt)*) => {
11         let stderr = std::io::stderr();
12         writeln!(stderr.lock(), $($arg)*).unwrap();
13     }
14 }
15
16 fn miri_path() -> PathBuf {
17     if rustc_test_suite().is_some() {
18         PathBuf::from(option_env!("MIRI_PATH").unwrap())
19     } else {
20         PathBuf::from(concat!("target/", env!("PROFILE"), "/miri"))
21     }
22 }
23
24 fn rustc_test_suite() -> Option<PathBuf> {
25     option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
26 }
27
28 fn rustc_lib_path() -> PathBuf {
29     option_env!("RUSTC_LIB_PATH").unwrap().into()
30 }
31
32 fn compile_fail(sysroot: &Path, path: &str, target: &str, host: &str, fullmir: bool) {
33     eprintln!(
34         "## Running compile-fail tests in {} against miri for target {}",
35         path,
36         target
37     );
38     let mut config = compiletest::default_config();
39     config.mode = "compile-fail".parse().expect("Invalid mode");
40     config.rustc_path = miri_path();
41     let mut flags = Vec::new();
42     if rustc_test_suite().is_some() {
43         config.run_lib_path = rustc_lib_path();
44         config.compile_lib_path = rustc_lib_path();
45     }
46     // if we are building as part of the rustc test suite, we already have fullmir for everything
47     if fullmir && rustc_test_suite().is_none() {
48         if host != target {
49             // skip fullmir on nonhost
50             return;
51         }
52         let sysroot = Path::new(&std::env::var("HOME").unwrap())
53             .join(".xargo")
54             .join("HOST");
55         config.target_rustcflags = Some(format!("--sysroot {}", sysroot.to_str().unwrap()));
56         config.src_base = PathBuf::from(path.to_string());
57     } else {
58         config.target_rustcflags = Some(format!("--sysroot {}", sysroot.to_str().unwrap()));
59         config.src_base = PathBuf::from(path.to_string());
60     }
61     flags.push("-Zmir-emit-validate=1".to_owned());
62     config.target_rustcflags = Some(flags.join(" "));
63     config.target = target.to_owned();
64     compiletest::run_tests(&config);
65 }
66
67 fn run_pass(path: &str) {
68     eprintln!("## Running run-pass tests in {} against rustc", path);
69     let mut config = compiletest::default_config();
70     config.mode = "run-pass".parse().expect("Invalid mode");
71     config.src_base = PathBuf::from(path);
72     if let Some(rustc_path) = rustc_test_suite() {
73         config.rustc_path = rustc_path;
74         config.run_lib_path = rustc_lib_path();
75         config.compile_lib_path = rustc_lib_path();
76         config.target_rustcflags = Some(format!("-Dwarnings --sysroot {}", get_sysroot().display()));
77     } else {
78         config.target_rustcflags = Some("-Dwarnings".to_owned());
79     }
80     config.host_rustcflags = Some("-Dwarnings".to_string());
81     compiletest::run_tests(&config);
82 }
83
84 fn miri_pass(path: &str, target: &str, host: &str, fullmir: bool, opt: bool) {
85     let opt_str = if opt { " with optimizations" } else { "" };
86     eprintln!(
87         "## Running run-pass tests in {} against miri for target {}{}",
88         path,
89         target,
90         opt_str
91     );
92     let mut config = compiletest::default_config();
93     config.mode = "mir-opt".parse().expect("Invalid mode");
94     config.src_base = PathBuf::from(path);
95     config.target = target.to_owned();
96     config.host = host.to_owned();
97     config.rustc_path = miri_path();
98     if rustc_test_suite().is_some() {
99         config.run_lib_path = rustc_lib_path();
100         config.compile_lib_path = rustc_lib_path();
101     }
102     let mut flags = Vec::new();
103     // if we are building as part of the rustc test suite, we already have fullmir for everything
104     if fullmir && rustc_test_suite().is_none() {
105         if host != target {
106             // skip fullmir on nonhost
107             return;
108         }
109         let sysroot = Path::new(&std::env::var("HOME").unwrap())
110             .join(".xargo")
111             .join("HOST");
112         flags.push(format!("--sysroot {}", sysroot.to_str().unwrap()));
113     }
114     if opt {
115         flags.push("-Zmir-opt-level=3".to_owned());
116     } else {
117         flags.push("-Zmir-opt-level=0".to_owned());
118         // For now, only validate without optimizations.  Inlining breaks validation.
119         flags.push("-Zmir-emit-validate=1".to_owned());
120     }
121     config.target_rustcflags = Some(flags.join(" "));
122     // don't actually execute the final binary, it might be for other targets and we only care
123     // about running miri, not the binary.
124     config.runtool = Some("echo \"\" || ".to_owned());
125     if target == host {
126         std::env::set_var("MIRI_HOST_TARGET", "yes");
127     }
128     compiletest::run_tests(&config);
129     std::env::set_var("MIRI_HOST_TARGET", "");
130 }
131
132 fn is_target_dir<P: Into<PathBuf>>(path: P) -> bool {
133     let mut path = path.into();
134     path.push("lib");
135     path.metadata().map(|m| m.is_dir()).unwrap_or(false)
136 }
137
138 fn for_all_targets<F: FnMut(String)>(sysroot: &Path, mut f: F) {
139     let target_dir = sysroot.join("lib").join("rustlib");
140     for entry in std::fs::read_dir(target_dir).expect("invalid sysroot") {
141         let entry = entry.unwrap();
142         if !is_target_dir(entry.path()) {
143             continue;
144         }
145         let target = entry.file_name().into_string().unwrap();
146         f(target);
147     }
148 }
149
150 fn get_sysroot() -> PathBuf {
151     let sysroot = std::env::var("MIRI_SYSROOT").unwrap_or_else(|_| {
152         let sysroot = std::process::Command::new("rustc")
153             .arg("--print")
154             .arg("sysroot")
155             .output()
156             .expect("rustc not found")
157             .stdout;
158         String::from_utf8(sysroot).expect("sysroot is not utf8")
159     });
160     PathBuf::from(sysroot.trim())
161 }
162
163 fn get_host() -> String {
164     let host = std::process::Command::new("rustc")
165         .arg("-vV")
166         .output()
167         .expect("rustc not found for -vV")
168         .stdout;
169     let host = std::str::from_utf8(&host).expect("sysroot is not utf8");
170     let host = host.split("\nhost: ").nth(1).expect(
171         "no host: part in rustc -vV",
172     );
173     let host = host.split('\n').next().expect("no \n after host");
174     String::from(host)
175 }
176
177 fn run_pass_miri(opt: bool) {
178     let sysroot = get_sysroot();
179     let host = get_host();
180
181     for_all_targets(&sysroot, |target| {
182         miri_pass("tests/run-pass", &target, &host, false, opt);
183     });
184     miri_pass("tests/run-pass-fullmir", &host, &host, true, opt);
185 }
186
187 #[test]
188 fn run_pass_miri_noopt() {
189     run_pass_miri(false);
190 }
191
192 #[test]
193 fn run_pass_miri_opt() {
194     run_pass_miri(true);
195 }
196
197 #[test]
198 fn run_pass_rustc() {
199     run_pass("tests/run-pass");
200     run_pass("tests/run-pass-fullmir");
201 }
202
203 #[test]
204 fn compile_fail_miri() {
205     let sysroot = get_sysroot();
206     let host = get_host();
207
208     for_all_targets(&sysroot, |target| {
209         compile_fail(&sysroot, "tests/compile-fail", &target, &host, false);
210     });
211     compile_fail(&sysroot, "tests/compile-fail-fullmir", &host, &host, true);
212 }