]> git.proxmox.com Git - rustc.git/blob - src/bootstrap/build/check.rs
Imported Upstream version 1.11.0+dfsg1
[rustc.git] / src / bootstrap / build / check.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Implementation of the various `check-*` targets of the build system.
12 //!
13 //! This file implements the various regression test suites that we execute on
14 //! our CI.
15
16 use std::env;
17 use std::fs::{self, File};
18 use std::io::prelude::*;
19 use std::path::{PathBuf, Path};
20 use std::process::Command;
21
22 use build_helper::output;
23 use bootstrap::{dylib_path, dylib_path_var};
24
25 use build::{Build, Compiler, Mode};
26 use build::util;
27
28 const ADB_TEST_DIR: &'static str = "/data/tmp";
29
30 /// Runs the `linkchecker` tool as compiled in `stage` by the `host` compiler.
31 ///
32 /// This tool in `src/tools` will verify the validity of all our links in the
33 /// documentation to ensure we don't have a bunch of dead ones.
34 pub fn linkcheck(build: &Build, stage: u32, host: &str) {
35 println!("Linkcheck stage{} ({})", stage, host);
36 let compiler = Compiler::new(stage, host);
37 build.run(build.tool_cmd(&compiler, "linkchecker")
38 .arg(build.out.join(host).join("doc")));
39 }
40
41 /// Runs the `cargotest` tool as compiled in `stage` by the `host` compiler.
42 ///
43 /// This tool in `src/tools` will check out a few Rust projects and run `cargo
44 /// test` to ensure that we don't regress the test suites there.
45 pub fn cargotest(build: &Build, stage: u32, host: &str) {
46 let ref compiler = Compiler::new(stage, host);
47
48 // Configure PATH to find the right rustc. NB. we have to use PATH
49 // and not RUSTC because the Cargo test suite has tests that will
50 // fail if rustc is not spelled `rustc`.
51 let path = build.sysroot(compiler).join("bin");
52 let old_path = ::std::env::var("PATH").expect("");
53 let sep = if cfg!(windows) { ";" } else {":" };
54 let ref newpath = format!("{}{}{}", path.display(), sep, old_path);
55
56 // Note that this is a short, cryptic, and not scoped directory name. This
57 // is currently to minimize the length of path on Windows where we otherwise
58 // quickly run into path name limit constraints.
59 let out_dir = build.out.join("ct");
60 t!(fs::create_dir_all(&out_dir));
61
62 build.run(build.tool_cmd(compiler, "cargotest")
63 .env("PATH", newpath)
64 .arg(&build.cargo)
65 .arg(&out_dir));
66 }
67
68 /// Runs the `tidy` tool as compiled in `stage` by the `host` compiler.
69 ///
70 /// This tool in `src/tools` checks up on various bits and pieces of style and
71 /// otherwise just implements a few lint-like checks that are specific to the
72 /// compiler itself.
73 pub fn tidy(build: &Build, stage: u32, host: &str) {
74 println!("tidy check stage{} ({})", stage, host);
75 let compiler = Compiler::new(stage, host);
76 build.run(build.tool_cmd(&compiler, "tidy")
77 .arg(build.src.join("src")));
78 }
79
80 fn testdir(build: &Build, host: &str) -> PathBuf {
81 build.out.join(host).join("test")
82 }
83
84 /// Executes the `compiletest` tool to run a suite of tests.
85 ///
86 /// Compiles all tests with `compiler` for `target` with the specified
87 /// compiletest `mode` and `suite` arguments. For example `mode` can be
88 /// "run-pass" or `suite` can be something like `debuginfo`.
89 pub fn compiletest(build: &Build,
90 compiler: &Compiler,
91 target: &str,
92 mode: &str,
93 suite: &str) {
94 println!("Check compiletest {} ({} -> {})", suite, compiler.host, target);
95 let mut cmd = build.tool_cmd(compiler, "compiletest");
96
97 // compiletest currently has... a lot of arguments, so let's just pass all
98 // of them!
99
100 cmd.arg("--compile-lib-path").arg(build.rustc_libdir(compiler));
101 cmd.arg("--run-lib-path").arg(build.sysroot_libdir(compiler, target));
102 cmd.arg("--rustc-path").arg(build.compiler_path(compiler));
103 cmd.arg("--rustdoc-path").arg(build.rustdoc(compiler));
104 cmd.arg("--src-base").arg(build.src.join("src/test").join(suite));
105 cmd.arg("--build-base").arg(testdir(build, compiler.host).join(suite));
106 cmd.arg("--stage-id").arg(format!("stage{}-{}", compiler.stage, target));
107 cmd.arg("--mode").arg(mode);
108 cmd.arg("--target").arg(target);
109 cmd.arg("--host").arg(compiler.host);
110 cmd.arg("--llvm-filecheck").arg(build.llvm_filecheck(&build.config.build));
111
112 let mut flags = vec!["-Crpath".to_string()];
113 if build.config.rust_optimize_tests {
114 flags.push("-O".to_string());
115 }
116 if build.config.rust_debuginfo_tests {
117 flags.push("-g".to_string());
118 }
119
120 let mut hostflags = build.rustc_flags(&compiler.host);
121 hostflags.extend(flags.clone());
122 cmd.arg("--host-rustcflags").arg(hostflags.join(" "));
123
124 let mut targetflags = build.rustc_flags(&target);
125 targetflags.extend(flags);
126 targetflags.push(format!("-Lnative={}",
127 build.test_helpers_out(target).display()));
128 cmd.arg("--target-rustcflags").arg(targetflags.join(" "));
129
130 // FIXME: CFG_PYTHON should probably be detected more robustly elsewhere
131 let python_default = "python";
132 cmd.arg("--docck-python").arg(python_default);
133
134 if build.config.build.ends_with("apple-darwin") {
135 // Force /usr/bin/python on OSX for LLDB tests because we're loading the
136 // LLDB plugin's compiled module which only works with the system python
137 // (namely not Homebrew-installed python)
138 cmd.arg("--lldb-python").arg("/usr/bin/python");
139 } else {
140 cmd.arg("--lldb-python").arg(python_default);
141 }
142
143 if let Some(ref vers) = build.gdb_version {
144 cmd.arg("--gdb-version").arg(vers);
145 }
146 if let Some(ref vers) = build.lldb_version {
147 cmd.arg("--lldb-version").arg(vers);
148 }
149 if let Some(ref dir) = build.lldb_python_dir {
150 cmd.arg("--lldb-python-dir").arg(dir);
151 }
152
153 cmd.args(&build.flags.args);
154
155 if build.config.verbose || build.flags.verbose {
156 cmd.arg("--verbose");
157 }
158
159 // Only pass correct values for these flags for the `run-make` suite as it
160 // requires that a C++ compiler was configured which isn't always the case.
161 if suite == "run-make" {
162 let llvm_config = build.llvm_config(target);
163 let llvm_components = output(Command::new(&llvm_config).arg("--components"));
164 let llvm_cxxflags = output(Command::new(&llvm_config).arg("--cxxflags"));
165 cmd.arg("--cc").arg(build.cc(target))
166 .arg("--cxx").arg(build.cxx(target))
167 .arg("--cflags").arg(build.cflags(target).join(" "))
168 .arg("--llvm-components").arg(llvm_components.trim())
169 .arg("--llvm-cxxflags").arg(llvm_cxxflags.trim());
170 } else {
171 cmd.arg("--cc").arg("")
172 .arg("--cxx").arg("")
173 .arg("--cflags").arg("")
174 .arg("--llvm-components").arg("")
175 .arg("--llvm-cxxflags").arg("");
176 }
177
178 // Running a C compiler on MSVC requires a few env vars to be set, to be
179 // sure to set them here.
180 if target.contains("msvc") {
181 for &(ref k, ref v) in build.cc[target].0.env() {
182 if k != "PATH" {
183 cmd.env(k, v);
184 }
185 }
186 }
187 build.add_bootstrap_key(compiler, &mut cmd);
188
189 cmd.arg("--adb-path").arg("adb");
190 cmd.arg("--adb-test-dir").arg(ADB_TEST_DIR);
191 if target.contains("android") {
192 // Assume that cc for this target comes from the android sysroot
193 cmd.arg("--android-cross-path")
194 .arg(build.cc(target).parent().unwrap().parent().unwrap());
195 } else {
196 cmd.arg("--android-cross-path").arg("");
197 }
198
199 build.run(&mut cmd);
200 }
201
202 /// Run `rustdoc --test` for all documentation in `src/doc`.
203 ///
204 /// This will run all tests in our markdown documentation (e.g. the book)
205 /// located in `src/doc`. The `rustdoc` that's run is the one that sits next to
206 /// `compiler`.
207 pub fn docs(build: &Build, compiler: &Compiler) {
208 // Do a breadth-first traversal of the `src/doc` directory and just run
209 // tests for all files that end in `*.md`
210 let mut stack = vec![build.src.join("src/doc")];
211
212 while let Some(p) = stack.pop() {
213 if p.is_dir() {
214 stack.extend(t!(p.read_dir()).map(|p| t!(p).path()));
215 continue
216 }
217
218 if p.extension().and_then(|s| s.to_str()) != Some("md") {
219 continue
220 }
221
222 println!("doc tests for: {}", p.display());
223 markdown_test(build, compiler, &p);
224 }
225 }
226
227 /// Run the error index generator tool to execute the tests located in the error
228 /// index.
229 ///
230 /// The `error_index_generator` tool lives in `src/tools` and is used to
231 /// generate a markdown file from the error indexes of the code base which is
232 /// then passed to `rustdoc --test`.
233 pub fn error_index(build: &Build, compiler: &Compiler) {
234 println!("Testing error-index stage{}", compiler.stage);
235
236 let output = testdir(build, compiler.host).join("error-index.md");
237 build.run(build.tool_cmd(compiler, "error_index_generator")
238 .arg("markdown")
239 .arg(&output)
240 .env("CFG_BUILD", &build.config.build));
241
242 markdown_test(build, compiler, &output);
243 }
244
245 fn markdown_test(build: &Build, compiler: &Compiler, markdown: &Path) {
246 let mut cmd = Command::new(build.rustdoc(compiler));
247 build.add_rustc_lib_path(compiler, &mut cmd);
248 cmd.arg("--test");
249 cmd.arg(markdown);
250 cmd.arg("--test-args").arg(build.flags.args.join(" "));
251 build.run(&mut cmd);
252 }
253
254 /// Run all unit tests plus documentation tests for an entire crate DAG defined
255 /// by a `Cargo.toml`
256 ///
257 /// This is what runs tests for crates like the standard library, compiler, etc.
258 /// It essentially is the driver for running `cargo test`.
259 ///
260 /// Currently this runs all tests for a DAG by passing a bunch of `-p foo`
261 /// arguments, and those arguments are discovered from `Cargo.lock`.
262 pub fn krate(build: &Build,
263 compiler: &Compiler,
264 target: &str,
265 mode: Mode) {
266 let (name, path, features) = match mode {
267 Mode::Libstd => ("libstd", "src/rustc/std_shim", build.std_features()),
268 Mode::Libtest => ("libtest", "src/rustc/test_shim", String::new()),
269 Mode::Librustc => ("librustc", "src/rustc", build.rustc_features()),
270 _ => panic!("can only test libraries"),
271 };
272 println!("Testing {} stage{} ({} -> {})", name, compiler.stage,
273 compiler.host, target);
274
275 // Build up the base `cargo test` command.
276 let mut cargo = build.cargo(compiler, mode, target, "test");
277 cargo.arg("--manifest-path")
278 .arg(build.src.join(path).join("Cargo.toml"))
279 .arg("--features").arg(features);
280
281 // Generate a list of `-p` arguments to pass to the `cargo test` invocation
282 // by crawling the corresponding Cargo.lock file.
283 let lockfile = build.src.join(path).join("Cargo.lock");
284 let mut contents = String::new();
285 t!(t!(File::open(&lockfile)).read_to_string(&mut contents));
286 let mut lines = contents.lines();
287 while let Some(line) = lines.next() {
288 let prefix = "name = \"";
289 if !line.starts_with(prefix) {
290 continue
291 }
292 lines.next(); // skip `version = ...`
293
294 // skip crates.io or otherwise non-path crates
295 if let Some(line) = lines.next() {
296 if line.starts_with("source") {
297 continue
298 }
299 }
300
301 let crate_name = &line[prefix.len()..line.len() - 1];
302
303 // Right now jemalloc is our only target-specific crate in the sense
304 // that it's not present on all platforms. Custom skip it here for now,
305 // but if we add more this probably wants to get more generalized.
306 if crate_name.contains("jemalloc") {
307 continue
308 }
309
310 cargo.arg("-p").arg(crate_name);
311 }
312
313 // The tests are going to run with the *target* libraries, so we need to
314 // ensure that those libraries show up in the LD_LIBRARY_PATH equivalent.
315 //
316 // Note that to run the compiler we need to run with the *host* libraries,
317 // but our wrapper scripts arrange for that to be the case anyway.
318 let mut dylib_path = dylib_path();
319 dylib_path.insert(0, build.sysroot_libdir(compiler, target));
320 cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
321
322 if target.contains("android") {
323 build.run(cargo.arg("--no-run"));
324 krate_android(build, compiler, target, mode);
325 } else {
326 cargo.args(&build.flags.args);
327 build.run(&mut cargo);
328 }
329 }
330
331 fn krate_android(build: &Build,
332 compiler: &Compiler,
333 target: &str,
334 mode: Mode) {
335 let mut tests = Vec::new();
336 let out_dir = build.cargo_out(compiler, mode, target);
337 find_tests(&out_dir, target, &mut tests);
338 find_tests(&out_dir.join("deps"), target, &mut tests);
339
340 for test in tests {
341 build.run(Command::new("adb").arg("push").arg(&test).arg(ADB_TEST_DIR));
342
343 let test_file_name = test.file_name().unwrap().to_string_lossy();
344 let log = format!("{}/check-stage{}-T-{}-H-{}-{}.log",
345 ADB_TEST_DIR,
346 compiler.stage,
347 target,
348 compiler.host,
349 test_file_name);
350 let program = format!("(cd {dir}; \
351 LD_LIBRARY_PATH=./{target} ./{test} \
352 --logfile {log} \
353 {args})",
354 dir = ADB_TEST_DIR,
355 target = target,
356 test = test_file_name,
357 log = log,
358 args = build.flags.args.join(" "));
359
360 let output = output(Command::new("adb").arg("shell").arg(&program));
361 println!("{}", output);
362 build.run(Command::new("adb")
363 .arg("pull")
364 .arg(&log)
365 .arg(build.out.join("tmp")));
366 build.run(Command::new("adb").arg("shell").arg("rm").arg(&log));
367 if !output.contains("result: ok") {
368 panic!("some tests failed");
369 }
370 }
371 }
372
373 fn find_tests(dir: &Path,
374 target: &str,
375 dst: &mut Vec<PathBuf>) {
376 for e in t!(dir.read_dir()).map(|e| t!(e)) {
377 let file_type = t!(e.file_type());
378 if !file_type.is_file() {
379 continue
380 }
381 let filename = e.file_name().into_string().unwrap();
382 if (target.contains("windows") && filename.ends_with(".exe")) ||
383 (!target.contains("windows") && !filename.contains(".")) {
384 dst.push(e.path());
385 }
386 }
387 }
388
389 pub fn android_copy_libs(build: &Build,
390 compiler: &Compiler,
391 target: &str) {
392 println!("Android copy libs to emulator ({})", target);
393 build.run(Command::new("adb").arg("remount"));
394 build.run(Command::new("adb").args(&["shell", "rm", "-r", ADB_TEST_DIR]));
395 build.run(Command::new("adb").args(&["shell", "mkdir", ADB_TEST_DIR]));
396 build.run(Command::new("adb")
397 .arg("push")
398 .arg(build.src.join("src/etc/adb_run_wrapper.sh"))
399 .arg(ADB_TEST_DIR));
400
401 let target_dir = format!("{}/{}", ADB_TEST_DIR, target);
402 build.run(Command::new("adb").args(&["shell", "mkdir", &target_dir[..]]));
403
404 for f in t!(build.sysroot_libdir(compiler, target).read_dir()) {
405 let f = t!(f);
406 let name = f.file_name().into_string().unwrap();
407 if util::is_dylib(&name) {
408 build.run(Command::new("adb")
409 .arg("push")
410 .arg(f.path())
411 .arg(&target_dir));
412 }
413 }
414 }