1 #![feature(slice_concat_ext)]
3 extern crate compiletest_rs as compiletest;
6 use std::slice::SliceConcatExt;
7 use std::path::{PathBuf, Path};
11 macro_rules! eprintln {
13 let stderr = std::io::stderr();
14 writeln!(stderr.lock(), $($arg)*).unwrap();
18 fn miri_path() -> PathBuf {
19 if rustc_test_suite().is_some() {
20 PathBuf::from(option_env!("MIRI_PATH").unwrap())
22 PathBuf::from(concat!("target/", env!("PROFILE"), "/miri"))
26 fn rustc_test_suite() -> Option<PathBuf> {
27 option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
30 fn rustc_lib_path() -> PathBuf {
31 option_env!("RUSTC_LIB_PATH").unwrap().into()
34 fn compile_fail(sysroot: &Path, path: &str, target: &str, host: &str, fullmir: bool) {
36 "## Running compile-fail tests in {} against miri for target {}",
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();
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() {
53 // skip fullmir on nonhost
56 let sysroot = Path::new(&std::env::var("HOME").unwrap())
59 config.target_rustcflags = Some(format!("--sysroot {}", sysroot.to_str().unwrap()));
60 config.src_base = PathBuf::from(path.to_string());
62 config.target_rustcflags = Some(format!("--sysroot {}", sysroot.to_str().unwrap()));
63 config.src_base = PathBuf::from(path.to_string());
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);
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()));
84 config.target_rustcflags = Some("-Dwarnings".to_owned());
86 config.host_rustcflags = Some("-Dwarnings".to_string());
87 compiletest::run_tests(&config);
90 fn miri_pass(path: &str, target: &str, host: &str, fullmir: bool, opt: bool) {
91 let opt_str = if opt { " with optimizations" } else { "" };
93 "## Running run-pass tests in {} against miri for target {}{}",
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();
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() {
114 // skip fullmir on nonhost
117 let sysroot = Path::new(&std::env::var("HOME").unwrap())
120 flags.push(format!("--sysroot {}", sysroot.to_str().unwrap()));
123 flags.push("-Zmir-opt-level=3".to_owned());
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());
130 flags.push("--miri_host_target".to_owned());
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);
139 fn is_target_dir<P: Into<PathBuf>>(path: P) -> bool {
140 let mut path = path.into();
142 path.metadata().map(|m| m.is_dir()).unwrap_or(false)
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()) {
152 let target = entry.file_name().into_string().unwrap();
157 fn get_sysroot() -> PathBuf {
158 let sysroot = std::env::var("MIRI_SYSROOT").unwrap_or_else(|_| {
159 let sysroot = std::process::Command::new("rustc")
163 .expect("rustc not found")
165 String::from_utf8(sysroot).expect("sysroot is not utf8")
167 PathBuf::from(sysroot.trim())
170 fn get_host() -> String {
171 let host = std::process::Command::new("rustc")
174 .expect("rustc not found for -vV")
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",
180 let host = host.split('\n').next().expect("no \n after host");
184 fn run_pass_miri(opt: bool) {
185 let sysroot = get_sysroot();
186 let host = get_host();
188 for_all_targets(&sysroot, |target| {
189 miri_pass("tests/run-pass", &target, &host, false, opt);
191 miri_pass("tests/run-pass-fullmir", &host, &host, true, opt);
195 fn run_pass_miri_noopt() {
196 run_pass_miri(false);
200 fn run_pass_miri_opt() {
205 fn run_pass_rustc() {
206 run_pass("tests/run-pass");
207 run_pass("tests/run-pass-fullmir");
211 fn compile_fail_miri() {
212 let sysroot = get_sysroot();
213 let host = get_host();
215 for_all_targets(&sysroot, |target| {
216 compile_fail(&sysroot, "tests/compile-fail", &target, &host, false);
218 compile_fail(&sysroot, "tests/compile-fail-fullmir", &host, &host, true);