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