]> git.lizzy.rs Git - rust.git/blob - tests/compiletest.rs
Merge branch 'master' into tests
[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 use std::env;
9
10 macro_rules! eprintln {
11     ($($arg:tt)*) => {
12         let stderr = std::io::stderr();
13         writeln!(stderr.lock(), $($arg)*).unwrap();
14     }
15 }
16
17 fn miri_path() -> PathBuf {
18     if rustc_test_suite().is_some() {
19         PathBuf::from(option_env!("MIRI_PATH").unwrap())
20     } else {
21         PathBuf::from(concat!("target/", env!("PROFILE"), "/miri"))
22     }
23 }
24
25 fn rustc_test_suite() -> Option<PathBuf> {
26     option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
27 }
28
29 fn rustc_lib_path() -> PathBuf {
30     option_env!("RUSTC_LIB_PATH").unwrap().into()
31 }
32
33 fn compile_fail(sysroot: &Path, path: &str, target: &str, host: &str, fullmir: bool) {
34     eprintln!(
35         "## Running compile-fail tests in {} against miri for target {}",
36         path,
37         target
38     );
39     let mut config = compiletest::Config::default().tempdir();
40     config.mode = "compile-fail".parse().expect("Invalid mode");
41     config.rustc_path = miri_path();
42     let mut flags = Vec::new();
43     if rustc_test_suite().is_some() {
44         config.run_lib_path = rustc_lib_path();
45         config.compile_lib_path = rustc_lib_path();
46     }
47     // if we are building as part of the rustc test suite, we already have fullmir for everything
48     if fullmir && rustc_test_suite().is_none() {
49         if host != target {
50             // skip fullmir on nonhost
51             return;
52         }
53         let sysroot = Path::new(&std::env::var("HOME").unwrap())
54             .join(".xargo")
55             .join("HOST");
56         config.target_rustcflags = Some(format!("--sysroot {}", sysroot.to_str().unwrap()));
57         config.src_base = PathBuf::from(path.to_string());
58     } else {
59         config.target_rustcflags = Some(format!("--sysroot {}", sysroot.to_str().unwrap()));
60         config.src_base = PathBuf::from(path.to_string());
61     }
62     flags.push("-Zmir-emit-validate=1".to_owned());
63     config.target_rustcflags = Some(flags.join(" "));
64     config.target = target.to_owned();
65     compiletest::run_tests(&config);
66 }
67
68 fn run_pass(path: &str) {
69     eprintln!("## Running run-pass tests in {} against rustc", path);
70     let mut config = compiletest::Config::default().tempdir();
71     config.mode = "run-pass".parse().expect("Invalid mode");
72     config.src_base = PathBuf::from(path);
73     if let Some(rustc_path) = rustc_test_suite() {
74         config.rustc_path = rustc_path;
75         config.run_lib_path = rustc_lib_path();
76         config.compile_lib_path = rustc_lib_path();
77         config.target_rustcflags = Some(format!("-Dwarnings --sysroot {}", get_sysroot().display()));
78     } else {
79         config.target_rustcflags = Some("-Dwarnings".to_owned());
80     }
81     config.host_rustcflags = Some("-Dwarnings".to_string());
82     compiletest::run_tests(&config);
83 }
84
85 fn miri_pass(path: &str, target: &str, host: &str, fullmir: bool, opt: bool) {
86     let opt_str = if opt { " with optimizations" } else { "" };
87     eprintln!(
88         "## Running run-pass tests in {} against miri for target {}{}",
89         path,
90         target,
91         opt_str
92     );
93     let mut config = compiletest::Config::default().tempdir();
94     config.mode = "ui".parse().expect("Invalid mode");
95     config.src_base = PathBuf::from(path);
96     config.target = target.to_owned();
97     config.host = host.to_owned();
98     config.rustc_path = miri_path();
99     if rustc_test_suite().is_some() {
100         config.run_lib_path = rustc_lib_path();
101         config.compile_lib_path = rustc_lib_path();
102     }
103     let mut flags = Vec::new();
104     // Control miri logging. This is okay despite concurrent test execution as all tests
105     // will set this env var to the same value.
106     env::set_var("MIRI_LOG", "warn");
107     // if we are building as part of the rustc test suite, we already have fullmir for everything
108     if fullmir && rustc_test_suite().is_none() {
109         if host != target {
110             // skip fullmir on nonhost
111             return;
112         }
113         let sysroot = Path::new(&std::env::var("HOME").unwrap())
114             .join(".xargo")
115             .join("HOST");
116         flags.push(format!("--sysroot {}", sysroot.to_str().unwrap()));
117     }
118     if opt {
119         flags.push("-Zmir-opt-level=3".to_owned());
120     } else {
121         flags.push("-Zmir-opt-level=0".to_owned());
122         // For now, only validate without optimizations.  Inlining breaks validation.
123         flags.push("-Zmir-emit-validate=1".to_owned());
124     }
125     if target == host {
126         flags.push("--miri_host_target".to_owned());
127     }
128     config.target_rustcflags = Some(flags.join(" "));
129     compiletest::run_tests(&config);
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 rustc = rustc_test_suite().unwrap_or(PathBuf::from("rustc"));
165     println!("using rustc at {}", rustc.display());
166     let host = std::process::Command::new(rustc)
167         .arg("-vV")
168         .output()
169         .expect("rustc not found for -vV")
170         .stdout;
171     let host = std::str::from_utf8(&host).expect("sysroot is not utf8");
172     let host = host.split("\nhost: ").nth(1).expect(
173         "no host: part in rustc -vV",
174     );
175     let host = host.split('\n').next().expect("no \n after host");
176     String::from(host)
177 }
178
179 fn run_pass_miri(opt: bool) {
180     let sysroot = get_sysroot();
181     let host = get_host();
182
183     for_all_targets(&sysroot, |target| {
184         miri_pass("tests/run-pass", &target, &host, false, opt);
185     });
186     miri_pass("tests/run-pass-fullmir", &host, &host, true, opt);
187 }
188
189 #[test]
190 fn run_pass_miri_noopt() {
191     run_pass_miri(false);
192 }
193
194 #[test]
195 fn run_pass_miri_opt() {
196     // FIXME: Disabled for now, as the optimizer is pretty broken and crashes...
197     //run_pass_miri(true);
198 }
199
200 #[test]
201 fn run_pass_rustc() {
202     run_pass("tests/run-pass");
203     run_pass("tests/run-pass-fullmir");
204 }
205
206 #[test]
207 fn compile_fail_miri() {
208     let sysroot = get_sysroot();
209     let host = get_host();
210
211     for_all_targets(&sysroot, |target| {
212         compile_fail(&sysroot, "tests/compile-fail", &target, &host, false);
213     });
214     compile_fail(&sysroot, "tests/compile-fail-fullmir", &host, &host, true);
215 }