]> git.lizzy.rs Git - rust.git/blob - tests/compiletest.rs
Merge remote-tracking branch 'origin/master' into rustup
[rust.git] / tests / compiletest.rs
1 #![feature(slice_concat_ext)]
2
3 extern crate compiletest_rs as compiletest;
4 extern crate colored;
5
6 use colored::*;
7
8 use std::slice::SliceConcatExt;
9 use std::path::{PathBuf, Path};
10 use std::io::Write;
11
12 macro_rules! eprintln {
13     ($($arg:tt)*) => {
14         let stderr = std::io::stderr();
15         writeln!(stderr.lock(), $($arg)*).unwrap();
16     }
17 }
18
19 fn miri_path() -> PathBuf {
20     if rustc_test_suite().is_some() {
21         PathBuf::from(option_env!("MIRI_PATH").unwrap())
22     } else {
23         PathBuf::from(concat!("target/", env!("PROFILE"), "/miri"))
24     }
25 }
26
27 fn rustc_test_suite() -> Option<PathBuf> {
28     option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
29 }
30
31 fn rustc_lib_path() -> PathBuf {
32     option_env!("RUSTC_LIB_PATH").unwrap().into()
33 }
34
35 fn have_fullmir() -> bool {
36     // We assume we have full MIR when MIRI_SYSROOT is set or when we are in rustc
37     std::env::var("MIRI_SYSROOT").is_ok() || rustc_test_suite().is_some()
38 }
39
40 fn compile_fail(sysroot: &Path, path: &str, target: &str, host: &str, need_fullmir: bool, opt: bool) {
41     if need_fullmir && !have_fullmir() {
42         eprintln!("{}", format!(
43             "## Skipping compile-fail tests in {} against miri for target {} due to missing mir",
44             path,
45             target
46         ).yellow().bold());
47         return;
48     }
49
50     let opt_str = if opt { " with optimizations" } else { "" };
51     eprintln!("{}", format!(
52         "## Running compile-fail tests in {} against miri for target {}{}",
53         path,
54         target,
55         opt_str
56     ).green().bold());
57
58     let mut flags = Vec::new();
59     flags.push(format!("--sysroot {}", sysroot.display()));
60     flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
61     flags.push("-Zmir-emit-validate=1".to_owned());
62     if opt {
63         // Optimizing too aggressivley makes UB detection harder, but test at least
64         // the default value.
65         flags.push("-Zmir-opt-level=1".to_owned());
66     } else {
67         flags.push("-Zmir-opt-level=0".to_owned());
68     }
69
70     let mut config = compiletest::Config::default().tempdir();
71     config.mode = "compile-fail".parse().expect("Invalid mode");
72     config.rustc_path = miri_path();
73     if rustc_test_suite().is_some() {
74         config.run_lib_path = rustc_lib_path();
75         config.compile_lib_path = rustc_lib_path();
76     }
77     config.src_base = PathBuf::from(path.to_string());
78     config.target_rustcflags = Some(flags.join(" "));
79     config.target = target.to_owned();
80     config.host = host.to_owned();
81     compiletest::run_tests(&config);
82 }
83
84 fn miri_pass(sysroot: &Path, path: &str, target: &str, host: &str, need_fullmir: bool, opt: bool) {
85     if need_fullmir && !have_fullmir() {
86         eprintln!("{}", format!(
87             "## Skipping run-pass tests in {} against miri for target {} due to missing mir",
88             path,
89             target
90         ).yellow().bold());
91         return;
92     }
93
94     let opt_str = if opt { " with optimizations" } else { "" };
95     eprintln!("{}", format!(
96         "## Running run-pass tests in {} against miri for target {}{}",
97         path,
98         target,
99         opt_str
100     ).green().bold());
101
102     let mut flags = Vec::new();
103     flags.push(format!("--sysroot {}", sysroot.display()));
104     flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
105     flags.push("-Zmir-emit-validate=1".to_owned());
106     if opt {
107         flags.push("-Zmir-opt-level=3".to_owned());
108     } else {
109         flags.push("-Zmir-opt-level=0".to_owned());
110     }
111
112     let mut config = compiletest::Config::default().tempdir();
113     config.mode = "ui".parse().expect("Invalid mode");
114     config.src_base = PathBuf::from(path);
115     config.target = target.to_owned();
116     config.host = host.to_owned();
117     config.rustc_path = miri_path();
118     if rustc_test_suite().is_some() {
119         config.run_lib_path = rustc_lib_path();
120         config.compile_lib_path = rustc_lib_path();
121     }
122     config.target_rustcflags = Some(flags.join(" "));
123     compiletest::run_tests(&config);
124 }
125
126 fn is_target_dir<P: Into<PathBuf>>(path: P) -> bool {
127     let mut path = path.into();
128     path.push("lib");
129     path.metadata().map(|m| m.is_dir()).unwrap_or(false)
130 }
131
132 fn for_all_targets<F: FnMut(String)>(sysroot: &Path, mut f: F) {
133     let target_dir = sysroot.join("lib").join("rustlib");
134     for entry in std::fs::read_dir(target_dir).expect("invalid sysroot") {
135         let entry = entry.unwrap();
136         if !is_target_dir(entry.path()) {
137             continue;
138         }
139         let target = entry.file_name().into_string().unwrap();
140         f(target);
141     }
142 }
143
144 fn get_sysroot() -> PathBuf {
145     let sysroot = std::env::var("MIRI_SYSROOT").unwrap_or_else(|_| {
146         let sysroot = std::process::Command::new("rustc")
147             .arg("--print")
148             .arg("sysroot")
149             .output()
150             .expect("rustc not found")
151             .stdout;
152         String::from_utf8(sysroot).expect("sysroot is not utf8")
153     });
154     PathBuf::from(sysroot.trim())
155 }
156
157 fn get_host() -> String {
158     let rustc = rustc_test_suite().unwrap_or(PathBuf::from("rustc"));
159     println!("using rustc at {}", rustc.display());
160     let host = std::process::Command::new(rustc)
161         .arg("-vV")
162         .output()
163         .expect("rustc not found for -vV")
164         .stdout;
165     let host = std::str::from_utf8(&host).expect("sysroot is not utf8");
166     let host = host.split("\nhost: ").nth(1).expect(
167         "no host: part in rustc -vV",
168     );
169     let host = host.split('\n').next().expect("no \n after host");
170     String::from(host)
171 }
172
173 fn run_pass_miri(opt: bool) {
174     let sysroot = get_sysroot();
175     let host = get_host();
176
177     for_all_targets(&sysroot, |target| {
178         miri_pass(&sysroot, "tests/run-pass", &target, &host, false, opt);
179     });
180     miri_pass(&sysroot, "tests/run-pass-fullmir", &host, &host, true, opt);
181 }
182
183 fn compile_fail_miri(opt: bool) {
184     let sysroot = get_sysroot();
185     let host = get_host();
186
187     // FIXME: run tests for other targets, too
188     compile_fail(&sysroot, "tests/compile-fail", &host, &host, false, opt);
189     compile_fail(&sysroot, "tests/compile-fail-fullmir", &host, &host, true, opt);
190 }
191
192 #[test]
193 fn test() {
194     // We put everything into a single test to avoid the parallelism `cargo test`
195     // introduces.  We still get parallelism within our tests because `compiletest`
196     // uses `libtest` which runs jobs in parallel.
197
198     run_pass_miri(false);
199     run_pass_miri(true);
200
201     compile_fail_miri(false);
202     compile_fail_miri(true);
203 }