]> git.lizzy.rs Git - rust.git/blob - tests/compiletest.rs
Merge pull request #333 from solson/rust_ci
[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
9 macro_rules! eprintln {
10     ($($arg:tt)*) => {
11         let stderr = std::io::stderr();
12         writeln!(stderr.lock(), $($arg)*).unwrap();
13     }
14 }
15
16 fn miri_path() -> PathBuf {
17     if rustc_test_suite().is_some() {
18         PathBuf::from(option_env!("MIRI_PATH").unwrap())
19     } else {
20         PathBuf::from(concat!("target/", env!("PROFILE"), "/miri"))
21     }
22 }
23
24 fn rustc_test_suite() -> Option<PathBuf> {
25     option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
26 }
27
28 fn rustc_lib_path() -> PathBuf {
29     option_env!("RUSTC_LIB_PATH").unwrap().into()
30 }
31
32 fn compile_fail(sysroot: &Path, path: &str, target: &str, host: &str, fullmir: bool) {
33     eprintln!(
34         "## Running compile-fail tests in {} against miri for target {}",
35         path,
36         target
37     );
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();
45     }
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() {
48         if host != target {
49             // skip fullmir on nonhost
50             return;
51         }
52         let sysroot = Path::new(&std::env::var("HOME").unwrap())
53             .join(".xargo")
54             .join("HOST");
55         config.target_rustcflags = Some(format!("--sysroot {}", sysroot.to_str().unwrap()));
56         config.src_base = PathBuf::from(path.to_string());
57     } else {
58         config.target_rustcflags = Some(format!("--sysroot {}", sysroot.to_str().unwrap()));
59         config.src_base = PathBuf::from(path.to_string());
60     }
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);
65 }
66
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()));
77     } else {
78         config.target_rustcflags = Some("-Dwarnings".to_owned());
79     }
80     config.host_rustcflags = Some("-Dwarnings".to_string());
81     compiletest::run_tests(&config);
82 }
83
84 fn miri_pass(path: &str, target: &str, host: &str, fullmir: bool, opt: bool) {
85     let opt_str = if opt { " with optimizations" } else { "" };
86     eprintln!(
87         "## Running run-pass tests in {} against miri for target {}{}",
88         path,
89         target,
90         opt_str
91     );
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();
101     }
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() {
105         if host != target {
106             // skip fullmir on nonhost
107             return;
108         }
109         let sysroot = Path::new(&std::env::var("HOME").unwrap())
110             .join(".xargo")
111             .join("HOST");
112         flags.push(format!("--sysroot {}", sysroot.to_str().unwrap()));
113     }
114     if opt {
115         flags.push("-Zmir-opt-level=3".to_owned());
116     } else {
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());
120     }
121     if target == host {
122         flags.push("--miri_host_target".to_owned());
123     }
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);
129 }
130
131 fn is_target_dir<P: Into<PathBuf>>(path: P) -> bool {
132     let mut path = path.into();
133     path.push("lib");
134     path.metadata().map(|m| m.is_dir()).unwrap_or(false)
135 }
136
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()) {
142             continue;
143         }
144         let target = entry.file_name().into_string().unwrap();
145         f(target);
146     }
147 }
148
149 fn get_sysroot() -> PathBuf {
150     let sysroot = std::env::var("MIRI_SYSROOT").unwrap_or_else(|_| {
151         let sysroot = std::process::Command::new("rustc")
152             .arg("--print")
153             .arg("sysroot")
154             .output()
155             .expect("rustc not found")
156             .stdout;
157         String::from_utf8(sysroot).expect("sysroot is not utf8")
158     });
159     PathBuf::from(sysroot.trim())
160 }
161
162 fn get_host() -> String {
163     let rustc = rustc_test_suite().unwrap_or(PathBuf::from("rustc"));
164     println!("using rustc at {}", rustc.display());
165     let host = std::process::Command::new(rustc)
166         .arg("-vV")
167         .output()
168         .expect("rustc not found for -vV")
169         .stdout;
170     let host = std::str::from_utf8(&host).expect("sysroot is not utf8");
171     let host = host.split("\nhost: ").nth(1).expect(
172         "no host: part in rustc -vV",
173     );
174     let host = host.split('\n').next().expect("no \n after host");
175     String::from(host)
176 }
177
178 fn run_pass_miri(opt: bool) {
179     let sysroot = get_sysroot();
180     let host = get_host();
181
182     for_all_targets(&sysroot, |target| {
183         miri_pass("tests/run-pass", &target, &host, false, opt);
184     });
185     miri_pass("tests/run-pass-fullmir", &host, &host, true, opt);
186 }
187
188 #[test]
189 fn run_pass_miri_noopt() {
190     run_pass_miri(false);
191 }
192
193 #[test]
194 fn run_pass_miri_opt() {
195     // FIXME: Disabled for now, as the optimizer is pretty broken and crashes...
196     //run_pass_miri(true);
197 }
198
199 #[test]
200 fn run_pass_rustc() {
201     run_pass("tests/run-pass");
202     run_pass("tests/run-pass-fullmir");
203 }
204
205 #[test]
206 fn compile_fail_miri() {
207     let sysroot = get_sysroot();
208     let host = get_host();
209
210     for_all_targets(&sysroot, |target| {
211         compile_fail(&sysroot, "tests/compile-fail", &target, &host, false);
212     });
213     compile_fail(&sysroot, "tests/compile-fail-fullmir", &host, &host, true);
214 }