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.
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.
11 //! Implementation of the test-related targets of the build system.
13 //! This file implements the various regression test suites that we execute on
16 extern crate build_helper;
18 use std::collections::HashSet;
22 use std::path::{PathBuf, Path};
23 use std::process::Command;
25 use build_helper::output;
27 use {Build, Compiler, Mode};
29 use util::{self, dylib_path, dylib_path_var, exe};
31 const ADB_TEST_DIR: &'static str = "/data/tmp/work";
33 /// The two modes of the test runner; tests or benchmarks.
34 #[derive(Copy, Clone)]
43 // Return the cargo subcommand for this test kind
44 fn subcommand(self) -> &'static str {
46 TestKind::Test => "test",
47 TestKind::Bench => "bench",
52 impl fmt::Display for TestKind {
53 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
54 f.write_str(match *self {
55 TestKind::Test => "Testing",
56 TestKind::Bench => "Benchmarking",
61 /// Runs the `linkchecker` tool as compiled in `stage` by the `host` compiler.
63 /// This tool in `src/tools` will verify the validity of all our links in the
64 /// documentation to ensure we don't have a bunch of dead ones.
65 pub fn linkcheck(build: &Build, host: &str) {
66 println!("Linkcheck ({})", host);
67 let compiler = Compiler::new(0, host);
69 let _time = util::timeit();
70 build.run(build.tool_cmd(&compiler, "linkchecker")
71 .arg(build.out.join(host).join("doc")));
74 /// Runs the `cargotest` tool as compiled in `stage` by the `host` compiler.
76 /// This tool in `src/tools` will check out a few Rust projects and run `cargo
77 /// test` to ensure that we don't regress the test suites there.
78 pub fn cargotest(build: &Build, stage: u32, host: &str) {
79 let ref compiler = Compiler::new(stage, host);
81 // Note that this is a short, cryptic, and not scoped directory name. This
82 // is currently to minimize the length of path on Windows where we otherwise
83 // quickly run into path name limit constraints.
84 let out_dir = build.out.join("ct");
85 t!(fs::create_dir_all(&out_dir));
87 let _time = util::timeit();
88 let mut cmd = Command::new(build.tool(&Compiler::new(0, host), "cargotest"));
89 build.prepare_tool_cmd(compiler, &mut cmd);
90 build.run(cmd.arg(&build.cargo)
92 .env("RUSTC", build.compiler_path(compiler))
93 .env("RUSTDOC", build.rustdoc(compiler)))
96 /// Runs `cargo test` for `cargo` packaged with Rust.
97 pub fn cargo(build: &Build, stage: u32, host: &str) {
98 let ref compiler = Compiler::new(stage, host);
100 // Configure PATH to find the right rustc. NB. we have to use PATH
101 // and not RUSTC because the Cargo test suite has tests that will
102 // fail if rustc is not spelled `rustc`.
103 let path = build.sysroot(compiler).join("bin");
104 let old_path = ::std::env::var("PATH").expect("");
105 let sep = if cfg!(windows) { ";" } else {":" };
106 let ref newpath = format!("{}{}{}", path.display(), sep, old_path);
108 let mut cargo = build.cargo(compiler, Mode::Tool, host, "test");
109 cargo.arg("--manifest-path").arg(build.src.join("src/tools/cargo/Cargo.toml"));
111 // Don't build tests dynamically, just a pain to work with
112 cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
114 // Don't run cross-compile tests, we may not have cross-compiled libstd libs
116 cargo.env("CFG_DISABLE_CROSS_TESTS", "1");
118 build.run(cargo.env("PATH", newpath));
121 /// Runs the `tidy` tool as compiled in `stage` by the `host` compiler.
123 /// This tool in `src/tools` checks up on various bits and pieces of style and
124 /// otherwise just implements a few lint-like checks that are specific to the
126 pub fn tidy(build: &Build, host: &str) {
127 let _folder = build.fold_output(|| "tidy");
128 println!("tidy check ({})", host);
129 let compiler = Compiler::new(0, host);
130 let mut cmd = build.tool_cmd(&compiler, "tidy");
131 cmd.arg(build.src.join("src"));
132 if !build.config.vendor {
133 cmd.arg("--no-vendor");
135 if build.config.quiet_tests {
141 fn testdir(build: &Build, host: &str) -> PathBuf {
142 build.out.join(host).join("test")
145 /// Executes the `compiletest` tool to run a suite of tests.
147 /// Compiles all tests with `compiler` for `target` with the specified
148 /// compiletest `mode` and `suite` arguments. For example `mode` can be
149 /// "run-pass" or `suite` can be something like `debuginfo`.
150 pub fn compiletest(build: &Build,
155 let _folder = build.fold_output(|| format!("test_{}", suite));
156 println!("Check compiletest suite={} mode={} ({} -> {})",
157 suite, mode, compiler.host, target);
158 let mut cmd = Command::new(build.tool(&Compiler::new(0, compiler.host),
160 build.prepare_tool_cmd(compiler, &mut cmd);
162 // compiletest currently has... a lot of arguments, so let's just pass all
165 cmd.arg("--compile-lib-path").arg(build.rustc_libdir(compiler));
166 cmd.arg("--run-lib-path").arg(build.sysroot_libdir(compiler, target));
167 cmd.arg("--rustc-path").arg(build.compiler_path(compiler));
168 cmd.arg("--rustdoc-path").arg(build.rustdoc(compiler));
169 cmd.arg("--src-base").arg(build.src.join("src/test").join(suite));
170 cmd.arg("--build-base").arg(testdir(build, compiler.host).join(suite));
171 cmd.arg("--stage-id").arg(format!("stage{}-{}", compiler.stage, target));
172 cmd.arg("--mode").arg(mode);
173 cmd.arg("--target").arg(target);
174 cmd.arg("--host").arg(compiler.host);
175 cmd.arg("--llvm-filecheck").arg(build.llvm_filecheck(&build.config.build));
177 if let Some(nodejs) = build.config.nodejs.as_ref() {
178 cmd.arg("--nodejs").arg(nodejs);
181 let mut flags = vec!["-Crpath".to_string()];
182 if build.config.rust_optimize_tests {
183 flags.push("-O".to_string());
185 if build.config.rust_debuginfo_tests {
186 flags.push("-g".to_string());
189 let mut hostflags = build.rustc_flags(&compiler.host);
190 hostflags.extend(flags.clone());
191 cmd.arg("--host-rustcflags").arg(hostflags.join(" "));
193 let mut targetflags = build.rustc_flags(&target);
194 targetflags.extend(flags);
195 targetflags.push(format!("-Lnative={}",
196 build.test_helpers_out(target).display()));
197 cmd.arg("--target-rustcflags").arg(targetflags.join(" "));
199 cmd.arg("--docck-python").arg(build.python());
201 if build.config.build.ends_with("apple-darwin") {
202 // Force /usr/bin/python on macOS for LLDB tests because we're loading the
203 // LLDB plugin's compiled module which only works with the system python
204 // (namely not Homebrew-installed python)
205 cmd.arg("--lldb-python").arg("/usr/bin/python");
207 cmd.arg("--lldb-python").arg(build.python());
210 if let Some(ref gdb) = build.config.gdb {
211 cmd.arg("--gdb").arg(gdb);
213 if let Some(ref vers) = build.lldb_version {
214 cmd.arg("--lldb-version").arg(vers);
216 if let Some(ref dir) = build.lldb_python_dir {
217 cmd.arg("--lldb-python-dir").arg(dir);
219 let llvm_config = build.llvm_config(target);
220 let llvm_version = output(Command::new(&llvm_config).arg("--version"));
221 cmd.arg("--llvm-version").arg(llvm_version);
223 cmd.args(&build.flags.cmd.test_args());
225 if build.config.verbose() || build.flags.verbose() {
226 cmd.arg("--verbose");
229 if build.config.quiet_tests {
233 // Only pass correct values for these flags for the `run-make` suite as it
234 // requires that a C++ compiler was configured which isn't always the case.
235 if suite == "run-make" {
236 let llvm_components = output(Command::new(&llvm_config).arg("--components"));
237 let llvm_cxxflags = output(Command::new(&llvm_config).arg("--cxxflags"));
238 cmd.arg("--cc").arg(build.cc(target))
239 .arg("--cxx").arg(build.cxx(target))
240 .arg("--cflags").arg(build.cflags(target).join(" "))
241 .arg("--llvm-components").arg(llvm_components.trim())
242 .arg("--llvm-cxxflags").arg(llvm_cxxflags.trim());
244 cmd.arg("--cc").arg("")
245 .arg("--cxx").arg("")
246 .arg("--cflags").arg("")
247 .arg("--llvm-components").arg("")
248 .arg("--llvm-cxxflags").arg("");
251 if build.remote_tested(target) {
252 cmd.arg("--remote-test-client")
253 .arg(build.tool(&Compiler::new(0, &build.config.build),
254 "remote-test-client"));
257 // Running a C compiler on MSVC requires a few env vars to be set, to be
258 // sure to set them here.
260 // Note that if we encounter `PATH` we make sure to append to our own `PATH`
261 // rather than stomp over it.
262 if target.contains("msvc") {
263 for &(ref k, ref v) in build.cc[target].0.env() {
269 cmd.env("RUSTC_BOOTSTRAP", "1");
270 build.add_rust_test_threads(&mut cmd);
272 if build.config.sanitizers {
273 cmd.env("SANITIZER_SUPPORT", "1");
276 cmd.arg("--adb-path").arg("adb");
277 cmd.arg("--adb-test-dir").arg(ADB_TEST_DIR);
278 if target.contains("android") {
279 // Assume that cc for this target comes from the android sysroot
280 cmd.arg("--android-cross-path")
281 .arg(build.cc(target).parent().unwrap().parent().unwrap());
283 cmd.arg("--android-cross-path").arg("");
286 build.ci_env.force_coloring_in_ci(&mut cmd);
288 let _time = util::timeit();
292 /// Run `rustdoc --test` for all documentation in `src/doc`.
294 /// This will run all tests in our markdown documentation (e.g. the book)
295 /// located in `src/doc`. The `rustdoc` that's run is the one that sits next to
297 pub fn docs(build: &Build, compiler: &Compiler) {
298 // Do a breadth-first traversal of the `src/doc` directory and just run
299 // tests for all files that end in `*.md`
300 let mut stack = vec![build.src.join("src/doc")];
301 let _time = util::timeit();
302 let _folder = build.fold_output(|| "test_docs");
304 while let Some(p) = stack.pop() {
306 stack.extend(t!(p.read_dir()).map(|p| t!(p).path()));
310 if p.extension().and_then(|s| s.to_str()) != Some("md") {
314 // The nostarch directory in the book is for no starch, and so isn't guaranteed to build.
315 // we don't care if it doesn't build, so skip it.
317 let path: &OsStr = p.as_ref();
318 if let Some(path) = path.to_str() {
319 if path.contains("nostarch") {
324 println!("doc tests for: {}", p.display());
325 markdown_test(build, compiler, &p);
329 /// Run the error index generator tool to execute the tests located in the error
332 /// The `error_index_generator` tool lives in `src/tools` and is used to
333 /// generate a markdown file from the error indexes of the code base which is
334 /// then passed to `rustdoc --test`.
335 pub fn error_index(build: &Build, compiler: &Compiler) {
336 let _folder = build.fold_output(|| "test_error_index");
337 println!("Testing error-index stage{}", compiler.stage);
339 let dir = testdir(build, compiler.host);
340 t!(fs::create_dir_all(&dir));
341 let output = dir.join("error-index.md");
343 let _time = util::timeit();
344 build.run(build.tool_cmd(&Compiler::new(0, compiler.host),
345 "error_index_generator")
348 .env("CFG_BUILD", &build.config.build));
350 markdown_test(build, compiler, &output);
353 fn markdown_test(build: &Build, compiler: &Compiler, markdown: &Path) {
354 let mut cmd = Command::new(build.rustdoc(compiler));
355 build.add_rustc_lib_path(compiler, &mut cmd);
356 build.add_rust_test_threads(&mut cmd);
359 cmd.env("RUSTC_BOOTSTRAP", "1");
361 let test_args = build.flags.cmd.test_args().join(" ");
362 cmd.arg("--test-args").arg(test_args);
364 if build.config.quiet_tests {
365 build.run_quiet(&mut cmd);
371 /// Run all unit tests plus documentation tests for an entire crate DAG defined
372 /// by a `Cargo.toml`
374 /// This is what runs tests for crates like the standard library, compiler, etc.
375 /// It essentially is the driver for running `cargo test`.
377 /// Currently this runs all tests for a DAG by passing a bunch of `-p foo`
378 /// arguments, and those arguments are discovered from `cargo metadata`.
379 pub fn krate(build: &Build,
384 krate: Option<&str>) {
385 let (name, path, features, root) = match mode {
387 ("libstd", "src/libstd", build.std_features(), "std")
390 ("libtest", "src/libtest", String::new(), "test")
393 ("librustc", "src/rustc", build.rustc_features(), "rustc-main")
395 _ => panic!("can only test libraries"),
397 let _folder = build.fold_output(|| {
398 format!("{}_stage{}-{}", test_kind.subcommand(), compiler.stage, name)
400 println!("{} {} stage{} ({} -> {})", test_kind, name, compiler.stage,
401 compiler.host, target);
403 // If we're not doing a full bootstrap but we're testing a stage2 version of
404 // libstd, then what we're actually testing is the libstd produced in
405 // stage1. Reflect that here by updating the compiler that we're working
406 // with automatically.
407 let compiler = if build.force_use_stage1(compiler, target) {
408 Compiler::new(1, compiler.host)
413 // Build up the base `cargo test` command.
415 // Pass in some standard flags then iterate over the graph we've discovered
416 // in `cargo metadata` with the maps above and figure out what `-p`
417 // arguments need to get passed.
418 let mut cargo = build.cargo(&compiler, mode, target, test_kind.subcommand());
419 cargo.arg("--manifest-path")
420 .arg(build.src.join(path).join("Cargo.toml"))
421 .arg("--features").arg(features);
425 cargo.arg("-p").arg(krate);
428 let mut visited = HashSet::new();
429 let mut next = vec![root];
430 while let Some(name) = next.pop() {
431 // Right now jemalloc is our only target-specific crate in the
432 // sense that it's not present on all platforms. Custom skip it
433 // here for now, but if we add more this probably wants to get
436 // Also skip `build_helper` as it's not compiled normally for
437 // target during the bootstrap and it's just meant to be a
438 // helper crate, not tested. If it leaks through then it ends up
439 // messing with various mtime calculations and such.
440 if !name.contains("jemalloc") && name != "build_helper" {
441 cargo.arg("-p").arg(&format!("{}:0.0.0", name));
443 for dep in build.crates[name].deps.iter() {
444 if visited.insert(dep) {
452 // The tests are going to run with the *target* libraries, so we need to
453 // ensure that those libraries show up in the LD_LIBRARY_PATH equivalent.
455 // Note that to run the compiler we need to run with the *host* libraries,
456 // but our wrapper scripts arrange for that to be the case anyway.
457 let mut dylib_path = dylib_path();
458 dylib_path.insert(0, build.sysroot_libdir(&compiler, target));
459 cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
461 if target.contains("emscripten") || build.remote_tested(target) {
462 cargo.arg("--no-run");
467 if build.config.quiet_tests {
468 cargo.arg("--quiet");
471 let _time = util::timeit();
473 if target.contains("emscripten") {
474 build.run(&mut cargo);
475 krate_emscripten(build, &compiler, target, mode);
476 } else if build.remote_tested(target) {
477 build.run(&mut cargo);
478 krate_remote(build, &compiler, target, mode);
480 cargo.args(&build.flags.cmd.test_args());
481 build.run(&mut cargo);
485 fn krate_emscripten(build: &Build,
489 let mut tests = Vec::new();
490 let out_dir = build.cargo_out(compiler, mode, target);
491 find_tests(&out_dir.join("deps"), target, &mut tests);
494 let test_file_name = test.to_string_lossy().into_owned();
495 println!("running {}", test_file_name);
496 let nodejs = build.config.nodejs.as_ref().expect("nodejs not configured");
497 let mut cmd = Command::new(nodejs);
498 cmd.arg(&test_file_name);
499 if build.config.quiet_tests {
506 fn krate_remote(build: &Build,
510 let mut tests = Vec::new();
511 let out_dir = build.cargo_out(compiler, mode, target);
512 find_tests(&out_dir.join("deps"), target, &mut tests);
514 let tool = build.tool(&Compiler::new(0, &build.config.build),
515 "remote-test-client");
517 let mut cmd = Command::new(&tool);
520 if build.config.quiet_tests {
523 cmd.args(&build.flags.cmd.test_args());
528 fn find_tests(dir: &Path,
530 dst: &mut Vec<PathBuf>) {
531 for e in t!(dir.read_dir()).map(|e| t!(e)) {
532 let file_type = t!(e.file_type());
533 if !file_type.is_file() {
536 let filename = e.file_name().into_string().unwrap();
537 if (target.contains("windows") && filename.ends_with(".exe")) ||
538 (!target.contains("windows") && !filename.contains(".")) ||
539 (target.contains("emscripten") && filename.ends_with(".js")) {
545 pub fn remote_copy_libs(build: &Build, compiler: &Compiler, target: &str) {
546 if !build.remote_tested(target) {
550 println!("REMOTE copy libs to emulator ({})", target);
551 t!(fs::create_dir_all(build.out.join("tmp")));
553 let server = build.cargo_out(compiler, Mode::Tool, target)
554 .join(exe("remote-test-server", target));
556 // Spawn the emulator and wait for it to come online
557 let tool = build.tool(&Compiler::new(0, &build.config.build),
558 "remote-test-client");
559 let mut cmd = Command::new(&tool);
560 cmd.arg("spawn-emulator")
563 .arg(build.out.join("tmp"));
564 if let Some(rootfs) = build.qemu_rootfs(target) {
569 // Push all our dylibs to the emulator
570 for f in t!(build.sysroot_libdir(compiler, target).read_dir()) {
572 let name = f.file_name().into_string().unwrap();
573 if util::is_dylib(&name) {
574 build.run(Command::new(&tool)
581 /// Run "distcheck", a 'make check' from a tarball
582 pub fn distcheck(build: &Build) {
583 if build.config.build != "x86_64-unknown-linux-gnu" {
586 if !build.config.host.iter().any(|s| s == "x86_64-unknown-linux-gnu") {
589 if !build.config.target.iter().any(|s| s == "x86_64-unknown-linux-gnu") {
593 println!("Distcheck");
594 let dir = build.out.join("tmp").join("distcheck");
595 let _ = fs::remove_dir_all(&dir);
596 t!(fs::create_dir_all(&dir));
598 let mut cmd = Command::new("tar");
600 .arg(dist::rust_src_location(build))
601 .arg("--strip-components=1")
604 build.run(Command::new("./configure")
605 .args(&build.config.configure_args)
606 .arg("--enable-vendor")
608 build.run(Command::new(build_helper::make(&build.config.build))
612 // Now make sure that rust-src has all of libstd's dependencies
613 println!("Distcheck rust-src");
614 let dir = build.out.join("tmp").join("distcheck-src");
615 let _ = fs::remove_dir_all(&dir);
616 t!(fs::create_dir_all(&dir));
618 let mut cmd = Command::new("tar");
620 .arg(dist::rust_src_installer(build))
621 .arg("--strip-components=1")
625 let toml = dir.join("rust-src/lib/rustlib/src/rust/src/libstd/Cargo.toml");
626 build.run(Command::new(&build.cargo)
627 .arg("generate-lockfile")
628 .arg("--manifest-path")
633 /// Test the build system itself
634 pub fn bootstrap(build: &Build) {
635 let mut cmd = Command::new(&build.cargo);
637 .current_dir(build.src.join("src/bootstrap"))
638 .env("CARGO_TARGET_DIR", build.out.join("bootstrap"))
639 .env("RUSTC", &build.rustc);
640 cmd.arg("--").args(&build.flags.cmd.test_args());