]> git.lizzy.rs Git - rust.git/blob - tests/compiletest.rs
Merge remote-tracking branch 'origin/master' into memory
[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     config.target_rustcflags = Some(flags.join(" "));
130     // don't actually execute the final binary, it might be for other targets and we only care
131     // about running miri, not the binary.
132     config.runtool = Some("echo \"\" || ".to_owned());
133     if target == host {
134         std::env::set_var("MIRI_HOST_TARGET", "yes");
135     }
136     compiletest::run_tests(&config);
137     std::env::set_var("MIRI_HOST_TARGET", "");
138 }
139
140 fn is_target_dir<P: Into<PathBuf>>(path: P) -> bool {
141     let mut path = path.into();
142     path.push("lib");
143     path.metadata().map(|m| m.is_dir()).unwrap_or(false)
144 }
145
146 fn for_all_targets<F: FnMut(String)>(sysroot: &Path, mut f: F) {
147     let target_dir = sysroot.join("lib").join("rustlib");
148     for entry in std::fs::read_dir(target_dir).expect("invalid sysroot") {
149         let entry = entry.unwrap();
150         if !is_target_dir(entry.path()) {
151             continue;
152         }
153         let target = entry.file_name().into_string().unwrap();
154         f(target);
155     }
156 }
157
158 fn get_sysroot() -> PathBuf {
159     let sysroot = std::env::var("MIRI_SYSROOT").unwrap_or_else(|_| {
160         let sysroot = std::process::Command::new("rustc")
161             .arg("--print")
162             .arg("sysroot")
163             .output()
164             .expect("rustc not found")
165             .stdout;
166         String::from_utf8(sysroot).expect("sysroot is not utf8")
167     });
168     PathBuf::from(sysroot.trim())
169 }
170
171 fn get_host() -> String {
172     let host = std::process::Command::new("rustc")
173         .arg("-vV")
174         .output()
175         .expect("rustc not found for -vV")
176         .stdout;
177     let host = std::str::from_utf8(&host).expect("sysroot is not utf8");
178     let host = host.split("\nhost: ").nth(1).expect(
179         "no host: part in rustc -vV",
180     );
181     let host = host.split('\n').next().expect("no \n after host");
182     String::from(host)
183 }
184
185 fn run_pass_miri(opt: bool) {
186     let sysroot = get_sysroot();
187     let host = get_host();
188
189     for_all_targets(&sysroot, |target| {
190         miri_pass("tests/run-pass", &target, &host, false, opt);
191     });
192     miri_pass("tests/run-pass-fullmir", &host, &host, true, opt);
193 }
194
195 #[test]
196 fn run_pass_miri_noopt() {
197     run_pass_miri(false);
198 }
199
200 #[test]
201 fn run_pass_miri_opt() {
202     run_pass_miri(true);
203 }
204
205 #[test]
206 fn run_pass_rustc() {
207     run_pass("tests/run-pass");
208     run_pass("tests/run-pass-fullmir");
209 }
210
211 #[test]
212 fn compile_fail_miri() {
213     let sysroot = get_sysroot();
214     let host = get_host();
215
216     for_all_targets(&sysroot, |target| {
217         compile_fail(&sysroot, "tests/compile-fail", &target, &host, false);
218     });
219     compile_fail(&sysroot, "tests/compile-fail-fullmir", &host, &host, true);
220 }