1 #![feature(slice_concat_ext)]
3 extern crate compiletest_rs as compiletest;
5 use std::slice::SliceConcatExt;
6 use std::path::{PathBuf, Path};
9 macro_rules! eprintln {
11 let stderr = std::io::stderr();
12 writeln!(stderr.lock(), $($arg)*).unwrap();
16 fn miri_path() -> PathBuf {
17 if rustc_test_suite().is_some() {
18 PathBuf::from(option_env!("MIRI_PATH").unwrap())
20 PathBuf::from(concat!("target/", env!("PROFILE"), "/miri"))
24 fn rustc_test_suite() -> Option<PathBuf> {
25 option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
28 fn rustc_lib_path() -> PathBuf {
29 option_env!("RUSTC_LIB_PATH").unwrap().into()
32 fn compile_fail(sysroot: &Path, path: &str, target: &str, host: &str, fullmir: bool) {
34 "## Running compile-fail tests in {} against miri for target {}",
38 let mut config = compiletest::Config::default().tempdir();
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();
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() {
49 // skip fullmir on nonhost
52 let sysroot = Path::new(&std::env::var("HOME").unwrap())
55 config.target_rustcflags = Some(format!("--sysroot {}", sysroot.to_str().unwrap()));
56 config.src_base = PathBuf::from(path.to_string());
58 config.target_rustcflags = Some(format!("--sysroot {}", sysroot.to_str().unwrap()));
59 config.src_base = PathBuf::from(path.to_string());
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);
67 fn run_pass(path: &str) {
68 eprintln!("## Running run-pass tests in {} against rustc", path);
69 let mut config = compiletest::Config::default().tempdir();
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()));
78 config.target_rustcflags = Some("-Dwarnings".to_owned());
80 config.host_rustcflags = Some("-Dwarnings".to_string());
81 compiletest::run_tests(&config);
84 fn miri_pass(path: &str, target: &str, host: &str, fullmir: bool, opt: bool) {
85 let opt_str = if opt { " with optimizations" } else { "" };
87 "## Running run-pass tests in {} against miri for target {}{}",
92 let mut config = compiletest::Config::default().tempdir();
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();
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() {
106 // skip fullmir on nonhost
109 let sysroot = Path::new(&std::env::var("HOME").unwrap())
112 flags.push(format!("--sysroot {}", sysroot.to_str().unwrap()));
115 flags.push("-Zmir-opt-level=3".to_owned());
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());
122 flags.push("--miri_host_target".to_owned());
124 config.target_rustcflags = Some(flags.join(" "));
125 // don't actually execute the final binary, it might be for other targets and we only care
126 // about running miri, not the binary.
127 config.runtool = Some("echo \"\" || ".to_owned());
128 compiletest::run_tests(&config);
131 fn is_target_dir<P: Into<PathBuf>>(path: P) -> bool {
132 let mut path = path.into();
134 path.metadata().map(|m| m.is_dir()).unwrap_or(false)
137 fn for_all_targets<F: FnMut(String)>(sysroot: &Path, mut f: F) {
138 let target_dir = sysroot.join("lib").join("rustlib");
139 for entry in std::fs::read_dir(target_dir).expect("invalid sysroot") {
140 let entry = entry.unwrap();
141 if !is_target_dir(entry.path()) {
144 let target = entry.file_name().into_string().unwrap();
149 fn get_sysroot() -> PathBuf {
150 let sysroot = std::env::var("MIRI_SYSROOT").unwrap_or_else(|_| {
151 let sysroot = std::process::Command::new("rustc")
155 .expect("rustc not found")
157 String::from_utf8(sysroot).expect("sysroot is not utf8")
159 PathBuf::from(sysroot.trim())
162 fn get_host() -> String {
163 let host = std::process::Command::new("rustc")
166 .expect("rustc not found for -vV")
168 let host = std::str::from_utf8(&host).expect("sysroot is not utf8");
169 let host = host.split("\nhost: ").nth(1).expect(
170 "no host: part in rustc -vV",
172 let host = host.split('\n').next().expect("no \n after host");
176 fn run_pass_miri(opt: bool) {
177 let sysroot = get_sysroot();
178 let host = get_host();
180 for_all_targets(&sysroot, |target| {
181 miri_pass("tests/run-pass", &target, &host, false, opt);
183 miri_pass("tests/run-pass-fullmir", &host, &host, true, opt);
187 fn run_pass_miri_noopt() {
188 run_pass_miri(false);
192 fn run_pass_miri_opt() {
197 fn run_pass_rustc() {
198 run_pass("tests/run-pass");
199 run_pass("tests/run-pass-fullmir");
203 fn compile_fail_miri() {
204 let sysroot = get_sysroot();
205 let host = get_host();
207 for_all_targets(&sysroot, |target| {
208 compile_fail(&sysroot, "tests/compile-fail", &target, &host, false);
210 compile_fail(&sysroot, "tests/compile-fail-fullmir", &host, &host, true);