Alex Crichton | defd1b3 | 2016-03-08 07:15:55 | [diff] [blame] | 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 | |
Alex Crichton | 0e272de | 2016-11-16 20:31:19 | [diff] [blame] | 11 | //! Implementation of the test-related targets of the build system. |
Alex Crichton | f72bfe6 | 2016-05-02 22:16:15 | [diff] [blame] | 12 | //! |
| 13 | //! This file implements the various regression test suites that we execute on |
| 14 | //! our CI. |
| 15 | |
Sébastien Marie | a7d9025 | 2016-12-17 19:09:23 | [diff] [blame] | 16 | extern crate build_helper; |
| 17 | |
Alex Crichton | a270b80 | 2016-10-21 20:18:09 | [diff] [blame] | 18 | use std::collections::HashSet; |
Alex Crichton | bb9062a | 2016-04-29 21:23:15 | [diff] [blame] | 19 | use std::env; |
Ulrik Sverdrup | b1566ba | 2016-11-25 21:13:59 | [diff] [blame] | 20 | use std::fmt; |
Alex Crichton | 147e2da | 2016-10-07 06:30:38 | [diff] [blame] | 21 | use std::fs; |
Alex Crichton | ede8944 | 2016-04-15 01:00:35 | [diff] [blame] | 22 | use std::path::{PathBuf, Path}; |
| 23 | use std::process::Command; |
Alex Crichton | 73c2d2a | 2016-04-14 21:27:51 | [diff] [blame] | 24 | |
Alex Crichton | 126e09e | 2016-04-14 22:51:03 | [diff] [blame] | 25 | use build_helper::output; |
| 26 | |
Alex Crichton | 48a07bf | 2016-07-06 04:58:20 | [diff] [blame] | 27 | use {Build, Compiler, Mode}; |
Alex Crichton | d38db82 | 2016-12-09 01:13:55 | [diff] [blame] | 28 | use dist; |
Alex Crichton | 48a07bf | 2016-07-06 04:58:20 | [diff] [blame] | 29 | use util::{self, dylib_path, dylib_path_var}; |
Alex Crichton | 39a5d3f | 2016-06-28 20:31:30 | [diff] [blame] | 30 | |
| 31 | const ADB_TEST_DIR: &'static str = "/data/tmp"; |
Alex Crichton | defd1b3 | 2016-03-08 07:15:55 | [diff] [blame] | 32 | |
Ulrik Sverdrup | b1566ba | 2016-11-25 21:13:59 | [diff] [blame] | 33 | /// The two modes of the test runner; tests or benchmarks. |
| 34 | #[derive(Copy, Clone)] |
| 35 | pub enum TestKind { |
| 36 | /// Run `cargo test` |
| 37 | Test, |
| 38 | /// Run `cargo bench` |
| 39 | Bench, |
| 40 | } |
| 41 | |
| 42 | impl TestKind { |
| 43 | // Return the cargo subcommand for this test kind |
| 44 | fn subcommand(self) -> &'static str { |
| 45 | match self { |
| 46 | TestKind::Test => "test", |
| 47 | TestKind::Bench => "bench", |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 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", |
| 57 | }) |
| 58 | } |
| 59 | } |
| 60 | |
Alex Crichton | f72bfe6 | 2016-05-02 22:16:15 | [diff] [blame] | 61 | /// Runs the `linkchecker` tool as compiled in `stage` by the `host` compiler. |
| 62 | /// |
| 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. |
Alex Crichton | 254876e | 2016-12-28 23:01:21 | [diff] [blame] | 65 | pub fn linkcheck(build: &Build, host: &str) { |
| 66 | println!("Linkcheck ({})", host); |
| 67 | let compiler = Compiler::new(0, host); |
Alex Crichton | 0e272de | 2016-11-16 20:31:19 | [diff] [blame] | 68 | |
| 69 | let _time = util::timeit(); |
Alex Crichton | 4a917e0 | 2016-03-12 00:21:05 | [diff] [blame] | 70 | build.run(build.tool_cmd(&compiler, "linkchecker") |
| 71 | .arg(build.out.join(host).join("doc"))); |
Alex Crichton | defd1b3 | 2016-03-08 07:15:55 | [diff] [blame] | 72 | } |
Brian Anderson | 3a790ac | 2016-03-18 20:54:31 | [diff] [blame] | 73 | |
Alex Crichton | f72bfe6 | 2016-05-02 22:16:15 | [diff] [blame] | 74 | /// Runs the `cargotest` tool as compiled in `stage` by the `host` compiler. |
| 75 | /// |
| 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. |
Brian Anderson | 3a790ac | 2016-03-18 20:54:31 | [diff] [blame] | 78 | pub fn cargotest(build: &Build, stage: u32, host: &str) { |
| 79 | let ref compiler = Compiler::new(stage, host); |
Brian Anderson | 8019922 | 2016-04-06 18:03:42 | [diff] [blame] | 80 | |
| 81 | // Configure PATH to find the right rustc. NB. we have to use PATH |
| 82 | // and not RUSTC because the Cargo test suite has tests that will |
| 83 | // fail if rustc is not spelled `rustc`. |
| 84 | let path = build.sysroot(compiler).join("bin"); |
| 85 | let old_path = ::std::env::var("PATH").expect(""); |
| 86 | let sep = if cfg!(windows) { ";" } else {":" }; |
| 87 | let ref newpath = format!("{}{}{}", path.display(), sep, old_path); |
| 88 | |
Alex Crichton | 73c2d2a | 2016-04-14 21:27:51 | [diff] [blame] | 89 | // Note that this is a short, cryptic, and not scoped directory name. This |
| 90 | // is currently to minimize the length of path on Windows where we otherwise |
| 91 | // quickly run into path name limit constraints. |
| 92 | let out_dir = build.out.join("ct"); |
| 93 | t!(fs::create_dir_all(&out_dir)); |
| 94 | |
Alex Crichton | 0e272de | 2016-11-16 20:31:19 | [diff] [blame] | 95 | let _time = util::timeit(); |
Alex Crichton | 254876e | 2016-12-28 23:01:21 | [diff] [blame] | 96 | let mut cmd = Command::new(build.tool(&Compiler::new(0, host), "cargotest")); |
| 97 | build.prepare_tool_cmd(compiler, &mut cmd); |
| 98 | build.run(cmd.env("PATH", newpath) |
| 99 | .arg(&build.cargo) |
| 100 | .arg(&out_dir)); |
Brian Anderson | 3a790ac | 2016-03-18 20:54:31 | [diff] [blame] | 101 | } |
Alex Crichton | 9dd3c54 | 2016-03-29 20:14:52 | [diff] [blame] | 102 | |
Alex Crichton | f72bfe6 | 2016-05-02 22:16:15 | [diff] [blame] | 103 | /// Runs the `tidy` tool as compiled in `stage` by the `host` compiler. |
| 104 | /// |
| 105 | /// This tool in `src/tools` checks up on various bits and pieces of style and |
| 106 | /// otherwise just implements a few lint-like checks that are specific to the |
| 107 | /// compiler itself. |
Alex Crichton | 254876e | 2016-12-28 23:01:21 | [diff] [blame] | 108 | pub fn tidy(build: &Build, host: &str) { |
| 109 | println!("tidy check ({})", host); |
| 110 | let compiler = Compiler::new(0, host); |
Alex Crichton | 9dd3c54 | 2016-03-29 20:14:52 | [diff] [blame] | 111 | build.run(build.tool_cmd(&compiler, "tidy") |
| 112 | .arg(build.src.join("src"))); |
| 113 | } |
Alex Crichton | b325baf | 2016-04-05 18:34:23 | [diff] [blame] | 114 | |
| 115 | fn testdir(build: &Build, host: &str) -> PathBuf { |
| 116 | build.out.join(host).join("test") |
| 117 | } |
| 118 | |
Alex Crichton | f72bfe6 | 2016-05-02 22:16:15 | [diff] [blame] | 119 | /// Executes the `compiletest` tool to run a suite of tests. |
| 120 | /// |
| 121 | /// Compiles all tests with `compiler` for `target` with the specified |
| 122 | /// compiletest `mode` and `suite` arguments. For example `mode` can be |
| 123 | /// "run-pass" or `suite` can be something like `debuginfo`. |
Alex Crichton | b325baf | 2016-04-05 18:34:23 | [diff] [blame] | 124 | pub fn compiletest(build: &Build, |
| 125 | compiler: &Compiler, |
| 126 | target: &str, |
| 127 | mode: &str, |
| 128 | suite: &str) { |
Alex Crichton | 0e272de | 2016-11-16 20:31:19 | [diff] [blame] | 129 | println!("Check compiletest suite={} mode={} ({} -> {})", |
| 130 | suite, mode, compiler.host, target); |
Alex Crichton | 254876e | 2016-12-28 23:01:21 | [diff] [blame] | 131 | let mut cmd = Command::new(build.tool(&Compiler::new(0, compiler.host), |
| 132 | "compiletest")); |
| 133 | build.prepare_tool_cmd(compiler, &mut cmd); |
Alex Crichton | b325baf | 2016-04-05 18:34:23 | [diff] [blame] | 134 | |
Alex Crichton | f72bfe6 | 2016-05-02 22:16:15 | [diff] [blame] | 135 | // compiletest currently has... a lot of arguments, so let's just pass all |
| 136 | // of them! |
| 137 | |
Alex Crichton | b325baf | 2016-04-05 18:34:23 | [diff] [blame] | 138 | cmd.arg("--compile-lib-path").arg(build.rustc_libdir(compiler)); |
| 139 | cmd.arg("--run-lib-path").arg(build.sysroot_libdir(compiler, target)); |
| 140 | cmd.arg("--rustc-path").arg(build.compiler_path(compiler)); |
| 141 | cmd.arg("--rustdoc-path").arg(build.rustdoc(compiler)); |
| 142 | cmd.arg("--src-base").arg(build.src.join("src/test").join(suite)); |
Alex Crichton | b325baf | 2016-04-05 18:34:23 | [diff] [blame] | 143 | cmd.arg("--build-base").arg(testdir(build, compiler.host).join(suite)); |
| 144 | cmd.arg("--stage-id").arg(format!("stage{}-{}", compiler.stage, target)); |
| 145 | cmd.arg("--mode").arg(mode); |
| 146 | cmd.arg("--target").arg(target); |
| 147 | cmd.arg("--host").arg(compiler.host); |
| 148 | cmd.arg("--llvm-filecheck").arg(build.llvm_filecheck(&build.config.build)); |
| 149 | |
Brian Anderson | 8401e37 | 2016-09-15 19:42:26 | [diff] [blame] | 150 | if let Some(nodejs) = build.config.nodejs.as_ref() { |
| 151 | cmd.arg("--nodejs").arg(nodejs); |
| 152 | } |
| 153 | |
Alex Crichton | 39a5d3f | 2016-06-28 20:31:30 | [diff] [blame] | 154 | let mut flags = vec!["-Crpath".to_string()]; |
Alex Crichton | f4e4ec7 | 2016-05-13 22:26:41 | [diff] [blame] | 155 | if build.config.rust_optimize_tests { |
Alex Crichton | 39a5d3f | 2016-06-28 20:31:30 | [diff] [blame] | 156 | flags.push("-O".to_string()); |
Alex Crichton | f4e4ec7 | 2016-05-13 22:26:41 | [diff] [blame] | 157 | } |
| 158 | if build.config.rust_debuginfo_tests { |
Alex Crichton | 39a5d3f | 2016-06-28 20:31:30 | [diff] [blame] | 159 | flags.push("-g".to_string()); |
Alex Crichton | f4e4ec7 | 2016-05-13 22:26:41 | [diff] [blame] | 160 | } |
| 161 | |
Alex Crichton | 39a5d3f | 2016-06-28 20:31:30 | [diff] [blame] | 162 | let mut hostflags = build.rustc_flags(&compiler.host); |
| 163 | hostflags.extend(flags.clone()); |
| 164 | cmd.arg("--host-rustcflags").arg(hostflags.join(" ")); |
Alex Crichton | f4e4ec7 | 2016-05-13 22:26:41 | [diff] [blame] | 165 | |
Alex Crichton | 39a5d3f | 2016-06-28 20:31:30 | [diff] [blame] | 166 | let mut targetflags = build.rustc_flags(&target); |
| 167 | targetflags.extend(flags); |
| 168 | targetflags.push(format!("-Lnative={}", |
| 169 | build.test_helpers_out(target).display())); |
| 170 | cmd.arg("--target-rustcflags").arg(targetflags.join(" ")); |
Alex Crichton | cbe6292 | 2016-04-19 16:44:19 | [diff] [blame] | 171 | |
Alex Crichton | 5f626138 | 2016-11-14 16:04:39 | [diff] [blame] | 172 | cmd.arg("--docck-python").arg(build.python()); |
Alex Crichton | cbe6292 | 2016-04-19 16:44:19 | [diff] [blame] | 173 | |
| 174 | if build.config.build.ends_with("apple-darwin") { |
| 175 | // Force /usr/bin/python on OSX for LLDB tests because we're loading the |
| 176 | // LLDB plugin's compiled module which only works with the system python |
| 177 | // (namely not Homebrew-installed python) |
| 178 | cmd.arg("--lldb-python").arg("/usr/bin/python"); |
| 179 | } else { |
Alex Crichton | 5f626138 | 2016-11-14 16:04:39 | [diff] [blame] | 180 | cmd.arg("--lldb-python").arg(build.python()); |
Alex Crichton | cbe6292 | 2016-04-19 16:44:19 | [diff] [blame] | 181 | } |
Alex Crichton | b325baf | 2016-04-05 18:34:23 | [diff] [blame] | 182 | |
Tim Neumann | dce4600 | 2016-10-29 18:11:53 | [diff] [blame] | 183 | if let Some(ref gdb) = build.config.gdb { |
| 184 | cmd.arg("--gdb").arg(gdb); |
Alex Crichton | b325baf | 2016-04-05 18:34:23 | [diff] [blame] | 185 | } |
| 186 | if let Some(ref vers) = build.lldb_version { |
| 187 | cmd.arg("--lldb-version").arg(vers); |
| 188 | } |
| 189 | if let Some(ref dir) = build.lldb_python_dir { |
| 190 | cmd.arg("--lldb-python-dir").arg(dir); |
| 191 | } |
Alex Crichton | 96283fc | 2016-09-01 17:52:44 | [diff] [blame] | 192 | let llvm_config = build.llvm_config(target); |
| 193 | let llvm_version = output(Command::new(&llvm_config).arg("--version")); |
| 194 | cmd.arg("--llvm-version").arg(llvm_version); |
Alex Crichton | b325baf | 2016-04-05 18:34:23 | [diff] [blame] | 195 | |
Alex Crichton | a270b80 | 2016-10-21 20:18:09 | [diff] [blame] | 196 | cmd.args(&build.flags.cmd.test_args()); |
Alex Crichton | b325baf | 2016-04-05 18:34:23 | [diff] [blame] | 197 | |
Niko Matsakis | 83453bc | 2016-11-16 23:02:56 | [diff] [blame] | 198 | if build.config.verbose() || build.flags.verbose() { |
Alex Crichton | b325baf | 2016-04-05 18:34:23 | [diff] [blame] | 199 | cmd.arg("--verbose"); |
| 200 | } |
| 201 | |
Corey Farwell | c8c6d2c | 2016-10-30 01:58:52 | [diff] [blame] | 202 | if build.config.quiet_tests { |
| 203 | cmd.arg("--quiet"); |
| 204 | } |
| 205 | |
Alex Crichton | f72bfe6 | 2016-05-02 22:16:15 | [diff] [blame] | 206 | // Only pass correct values for these flags for the `run-make` suite as it |
| 207 | // requires that a C++ compiler was configured which isn't always the case. |
Alex Crichton | 126e09e | 2016-04-14 22:51:03 | [diff] [blame] | 208 | if suite == "run-make" { |
Alex Crichton | 126e09e | 2016-04-14 22:51:03 | [diff] [blame] | 209 | let llvm_components = output(Command::new(&llvm_config).arg("--components")); |
| 210 | let llvm_cxxflags = output(Command::new(&llvm_config).arg("--cxxflags")); |
| 211 | cmd.arg("--cc").arg(build.cc(target)) |
| 212 | .arg("--cxx").arg(build.cxx(target)) |
| 213 | .arg("--cflags").arg(build.cflags(target).join(" ")) |
| 214 | .arg("--llvm-components").arg(llvm_components.trim()) |
| 215 | .arg("--llvm-cxxflags").arg(llvm_cxxflags.trim()); |
| 216 | } else { |
| 217 | cmd.arg("--cc").arg("") |
| 218 | .arg("--cxx").arg("") |
| 219 | .arg("--cflags").arg("") |
| 220 | .arg("--llvm-components").arg("") |
| 221 | .arg("--llvm-cxxflags").arg(""); |
| 222 | } |
| 223 | |
| 224 | // Running a C compiler on MSVC requires a few env vars to be set, to be |
| 225 | // sure to set them here. |
Alex Crichton | 0e272de | 2016-11-16 20:31:19 | [diff] [blame] | 226 | // |
| 227 | // Note that if we encounter `PATH` we make sure to append to our own `PATH` |
| 228 | // rather than stomp over it. |
Alex Crichton | 126e09e | 2016-04-14 22:51:03 | [diff] [blame] | 229 | if target.contains("msvc") { |
| 230 | for &(ref k, ref v) in build.cc[target].0.env() { |
| 231 | if k != "PATH" { |
| 232 | cmd.env(k, v); |
| 233 | } |
| 234 | } |
| 235 | } |
Alex Crichton | 2186660 | 2016-11-16 17:19:02 | [diff] [blame] | 236 | cmd.env("RUSTC_BOOTSTRAP", "1"); |
Alex Crichton | 0e272de | 2016-11-16 20:31:19 | [diff] [blame] | 237 | build.add_rust_test_threads(&mut cmd); |
Alex Crichton | 126e09e | 2016-04-14 22:51:03 | [diff] [blame] | 238 | |
Jorge Aparicio | 775a936 | 2017-02-03 23:58:47 | [diff] [blame^] | 239 | if build.config.sanitizers { |
| 240 | cmd.env("SANITIZER_SUPPORT", "1"); |
| 241 | } |
| 242 | |
Alex Crichton | 39a5d3f | 2016-06-28 20:31:30 | [diff] [blame] | 243 | cmd.arg("--adb-path").arg("adb"); |
| 244 | cmd.arg("--adb-test-dir").arg(ADB_TEST_DIR); |
| 245 | if target.contains("android") { |
| 246 | // Assume that cc for this target comes from the android sysroot |
| 247 | cmd.arg("--android-cross-path") |
| 248 | .arg(build.cc(target).parent().unwrap().parent().unwrap()); |
| 249 | } else { |
| 250 | cmd.arg("--android-cross-path").arg(""); |
| 251 | } |
| 252 | |
Alex Crichton | 0e272de | 2016-11-16 20:31:19 | [diff] [blame] | 253 | let _time = util::timeit(); |
Alex Crichton | b325baf | 2016-04-05 18:34:23 | [diff] [blame] | 254 | build.run(&mut cmd); |
| 255 | } |
Alex Crichton | ede8944 | 2016-04-15 01:00:35 | [diff] [blame] | 256 | |
Alex Crichton | f72bfe6 | 2016-05-02 22:16:15 | [diff] [blame] | 257 | /// Run `rustdoc --test` for all documentation in `src/doc`. |
| 258 | /// |
| 259 | /// This will run all tests in our markdown documentation (e.g. the book) |
| 260 | /// located in `src/doc`. The `rustdoc` that's run is the one that sits next to |
| 261 | /// `compiler`. |
Alex Crichton | ede8944 | 2016-04-15 01:00:35 | [diff] [blame] | 262 | pub fn docs(build: &Build, compiler: &Compiler) { |
Alex Crichton | f72bfe6 | 2016-05-02 22:16:15 | [diff] [blame] | 263 | // Do a breadth-first traversal of the `src/doc` directory and just run |
| 264 | // tests for all files that end in `*.md` |
Alex Crichton | ede8944 | 2016-04-15 01:00:35 | [diff] [blame] | 265 | let mut stack = vec![build.src.join("src/doc")]; |
Alex Crichton | 0e272de | 2016-11-16 20:31:19 | [diff] [blame] | 266 | let _time = util::timeit(); |
Alex Crichton | ede8944 | 2016-04-15 01:00:35 | [diff] [blame] | 267 | |
| 268 | while let Some(p) = stack.pop() { |
| 269 | if p.is_dir() { |
| 270 | stack.extend(t!(p.read_dir()).map(|p| t!(p).path())); |
| 271 | continue |
| 272 | } |
| 273 | |
| 274 | if p.extension().and_then(|s| s.to_str()) != Some("md") { |
| 275 | continue |
| 276 | } |
| 277 | |
| 278 | println!("doc tests for: {}", p.display()); |
| 279 | markdown_test(build, compiler, &p); |
| 280 | } |
| 281 | } |
| 282 | |
Alex Crichton | f72bfe6 | 2016-05-02 22:16:15 | [diff] [blame] | 283 | /// Run the error index generator tool to execute the tests located in the error |
| 284 | /// index. |
| 285 | /// |
| 286 | /// The `error_index_generator` tool lives in `src/tools` and is used to |
| 287 | /// generate a markdown file from the error indexes of the code base which is |
| 288 | /// then passed to `rustdoc --test`. |
Alex Crichton | ede8944 | 2016-04-15 01:00:35 | [diff] [blame] | 289 | pub fn error_index(build: &Build, compiler: &Compiler) { |
| 290 | println!("Testing error-index stage{}", compiler.stage); |
| 291 | |
Alex Crichton | 860c6ab | 2016-11-08 17:59:46 | [diff] [blame] | 292 | let dir = testdir(build, compiler.host); |
| 293 | t!(fs::create_dir_all(&dir)); |
| 294 | let output = dir.join("error-index.md"); |
Alex Crichton | 0e272de | 2016-11-16 20:31:19 | [diff] [blame] | 295 | |
| 296 | let _time = util::timeit(); |
Alex Crichton | 254876e | 2016-12-28 23:01:21 | [diff] [blame] | 297 | build.run(build.tool_cmd(&Compiler::new(0, compiler.host), |
| 298 | "error_index_generator") |
Alex Crichton | ede8944 | 2016-04-15 01:00:35 | [diff] [blame] | 299 | .arg("markdown") |
| 300 | .arg(&output) |
| 301 | .env("CFG_BUILD", &build.config.build)); |
| 302 | |
| 303 | markdown_test(build, compiler, &output); |
| 304 | } |
| 305 | |
| 306 | fn markdown_test(build: &Build, compiler: &Compiler, markdown: &Path) { |
| 307 | let mut cmd = Command::new(build.rustdoc(compiler)); |
| 308 | build.add_rustc_lib_path(compiler, &mut cmd); |
Alex Crichton | 0e272de | 2016-11-16 20:31:19 | [diff] [blame] | 309 | build.add_rust_test_threads(&mut cmd); |
Alex Crichton | ede8944 | 2016-04-15 01:00:35 | [diff] [blame] | 310 | cmd.arg("--test"); |
| 311 | cmd.arg(markdown); |
Alex Crichton | 6f62fae | 2016-12-12 17:03:35 | [diff] [blame] | 312 | cmd.env("RUSTC_BOOTSTRAP", "1"); |
Corey Farwell | c8c6d2c | 2016-10-30 01:58:52 | [diff] [blame] | 313 | |
Alex Crichton | a270b80 | 2016-10-21 20:18:09 | [diff] [blame] | 314 | let mut test_args = build.flags.cmd.test_args().join(" "); |
Corey Farwell | c8c6d2c | 2016-10-30 01:58:52 | [diff] [blame] | 315 | if build.config.quiet_tests { |
| 316 | test_args.push_str(" --quiet"); |
| 317 | } |
| 318 | cmd.arg("--test-args").arg(test_args); |
| 319 | |
Alex Crichton | ede8944 | 2016-04-15 01:00:35 | [diff] [blame] | 320 | build.run(&mut cmd); |
| 321 | } |
Alex Crichton | bb9062a | 2016-04-29 21:23:15 | [diff] [blame] | 322 | |
| 323 | /// Run all unit tests plus documentation tests for an entire crate DAG defined |
| 324 | /// by a `Cargo.toml` |
| 325 | /// |
| 326 | /// This is what runs tests for crates like the standard library, compiler, etc. |
| 327 | /// It essentially is the driver for running `cargo test`. |
| 328 | /// |
| 329 | /// Currently this runs all tests for a DAG by passing a bunch of `-p foo` |
Alex Crichton | 147e2da | 2016-10-07 06:30:38 | [diff] [blame] | 330 | /// arguments, and those arguments are discovered from `cargo metadata`. |
Alex Crichton | bb9062a | 2016-04-29 21:23:15 | [diff] [blame] | 331 | pub fn krate(build: &Build, |
| 332 | compiler: &Compiler, |
| 333 | target: &str, |
Alex Crichton | a270b80 | 2016-10-21 20:18:09 | [diff] [blame] | 334 | mode: Mode, |
Ulrik Sverdrup | b1566ba | 2016-11-25 21:13:59 | [diff] [blame] | 335 | test_kind: TestKind, |
Alex Crichton | a270b80 | 2016-10-21 20:18:09 | [diff] [blame] | 336 | krate: Option<&str>) { |
Alex Crichton | 147e2da | 2016-10-07 06:30:38 | [diff] [blame] | 337 | let (name, path, features, root) = match mode { |
Ahmed Charles | 9ca382f | 2016-09-02 08:55:29 | [diff] [blame] | 338 | Mode::Libstd => { |
Jorge Aparicio | 775a936 | 2017-02-03 23:58:47 | [diff] [blame^] | 339 | ("libstd", "src/rustc/std_shim", build.std_features(), "std_shim") |
Ahmed Charles | 9ca382f | 2016-09-02 08:55:29 | [diff] [blame] | 340 | } |
| 341 | Mode::Libtest => { |
Alex Crichton | 147e2da | 2016-10-07 06:30:38 | [diff] [blame] | 342 | ("libtest", "src/rustc/test_shim", String::new(), "test_shim") |
Ahmed Charles | 9ca382f | 2016-09-02 08:55:29 | [diff] [blame] | 343 | } |
| 344 | Mode::Librustc => { |
Alex Crichton | 147e2da | 2016-10-07 06:30:38 | [diff] [blame] | 345 | ("librustc", "src/rustc", build.rustc_features(), "rustc-main") |
Ahmed Charles | 9ca382f | 2016-09-02 08:55:29 | [diff] [blame] | 346 | } |
Alex Crichton | bb9062a | 2016-04-29 21:23:15 | [diff] [blame] | 347 | _ => panic!("can only test libraries"), |
| 348 | }; |
Ulrik Sverdrup | b1566ba | 2016-11-25 21:13:59 | [diff] [blame] | 349 | println!("{} {} stage{} ({} -> {})", test_kind, name, compiler.stage, |
Alex Crichton | bb9062a | 2016-04-29 21:23:15 | [diff] [blame] | 350 | compiler.host, target); |
| 351 | |
Alex Crichton | 7046fea | 2016-12-25 23:20:33 | [diff] [blame] | 352 | // If we're not doing a full bootstrap but we're testing a stage2 version of |
| 353 | // libstd, then what we're actually testing is the libstd produced in |
| 354 | // stage1. Reflect that here by updating the compiler that we're working |
| 355 | // with automatically. |
| 356 | let compiler = if build.force_use_stage1(compiler, target) { |
| 357 | Compiler::new(1, compiler.host) |
| 358 | } else { |
| 359 | compiler.clone() |
| 360 | }; |
| 361 | |
Alex Crichton | bb9062a | 2016-04-29 21:23:15 | [diff] [blame] | 362 | // Build up the base `cargo test` command. |
Alex Crichton | 147e2da | 2016-10-07 06:30:38 | [diff] [blame] | 363 | // |
| 364 | // Pass in some standard flags then iterate over the graph we've discovered |
| 365 | // in `cargo metadata` with the maps above and figure out what `-p` |
| 366 | // arguments need to get passed. |
Alex Crichton | 7046fea | 2016-12-25 23:20:33 | [diff] [blame] | 367 | let mut cargo = build.cargo(&compiler, mode, target, test_kind.subcommand()); |
Alex Crichton | bb9062a | 2016-04-29 21:23:15 | [diff] [blame] | 368 | cargo.arg("--manifest-path") |
| 369 | .arg(build.src.join(path).join("Cargo.toml")) |
| 370 | .arg("--features").arg(features); |
| 371 | |
Alex Crichton | a270b80 | 2016-10-21 20:18:09 | [diff] [blame] | 372 | match krate { |
| 373 | Some(krate) => { |
| 374 | cargo.arg("-p").arg(krate); |
Alex Crichton | bb9062a | 2016-04-29 21:23:15 | [diff] [blame] | 375 | } |
Alex Crichton | a270b80 | 2016-10-21 20:18:09 | [diff] [blame] | 376 | None => { |
| 377 | let mut visited = HashSet::new(); |
| 378 | let mut next = vec![root]; |
| 379 | while let Some(name) = next.pop() { |
Alex Crichton | 36a926a | 2017-01-13 23:11:34 | [diff] [blame] | 380 | // Right now jemalloc is our only target-specific crate in the |
| 381 | // sense that it's not present on all platforms. Custom skip it |
| 382 | // here for now, but if we add more this probably wants to get |
| 383 | // more generalized. |
| 384 | // |
| 385 | // Also skip `build_helper` as it's not compiled normally for |
| 386 | // target during the bootstrap and it's just meant to be a |
| 387 | // helper crate, not tested. If it leaks through then it ends up |
| 388 | // messing with various mtime calculations and such. |
| 389 | if !name.contains("jemalloc") && name != "build_helper" { |
Vadim Petrochenkov | b4abb72 | 2017-02-02 20:55:42 | [diff] [blame] | 390 | cargo.arg("-p").arg(&format!("{}:0.0.0", name)); |
Alex Crichton | a270b80 | 2016-10-21 20:18:09 | [diff] [blame] | 391 | } |
| 392 | for dep in build.crates[name].deps.iter() { |
| 393 | if visited.insert(dep) { |
| 394 | next.push(dep); |
| 395 | } |
| 396 | } |
Alex Crichton | bb9062a | 2016-04-29 21:23:15 | [diff] [blame] | 397 | } |
| 398 | } |
Alex Crichton | bb9062a | 2016-04-29 21:23:15 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | // The tests are going to run with the *target* libraries, so we need to |
| 402 | // ensure that those libraries show up in the LD_LIBRARY_PATH equivalent. |
| 403 | // |
| 404 | // Note that to run the compiler we need to run with the *host* libraries, |
| 405 | // but our wrapper scripts arrange for that to be the case anyway. |
| 406 | let mut dylib_path = dylib_path(); |
Alex Crichton | 7046fea | 2016-12-25 23:20:33 | [diff] [blame] | 407 | dylib_path.insert(0, build.sysroot_libdir(&compiler, target)); |
Alex Crichton | bb9062a | 2016-04-29 21:23:15 | [diff] [blame] | 408 | cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap()); |
Alex Crichton | bb9062a | 2016-04-29 21:23:15 | [diff] [blame] | 409 | |
Alex Crichton | 0e272de | 2016-11-16 20:31:19 | [diff] [blame] | 410 | if target.contains("android") { |
| 411 | cargo.arg("--no-run"); |
| 412 | } else if target.contains("emscripten") { |
| 413 | cargo.arg("--no-run"); |
| 414 | } |
| 415 | |
| 416 | cargo.arg("--"); |
| 417 | |
Corey Farwell | c8c6d2c | 2016-10-30 01:58:52 | [diff] [blame] | 418 | if build.config.quiet_tests { |
Corey Farwell | c8c6d2c | 2016-10-30 01:58:52 | [diff] [blame] | 419 | cargo.arg("--quiet"); |
| 420 | } |
| 421 | |
Alex Crichton | 0e272de | 2016-11-16 20:31:19 | [diff] [blame] | 422 | let _time = util::timeit(); |
| 423 | |
Alex Crichton | 39a5d3f | 2016-06-28 20:31:30 | [diff] [blame] | 424 | if target.contains("android") { |
Alex Crichton | 0e272de | 2016-11-16 20:31:19 | [diff] [blame] | 425 | build.run(&mut cargo); |
Alex Crichton | 7046fea | 2016-12-25 23:20:33 | [diff] [blame] | 426 | krate_android(build, &compiler, target, mode); |
Brian Anderson | b8b50f0 | 2016-09-06 00:41:50 | [diff] [blame] | 427 | } else if target.contains("emscripten") { |
Alex Crichton | 0e272de | 2016-11-16 20:31:19 | [diff] [blame] | 428 | build.run(&mut cargo); |
Alex Crichton | 7046fea | 2016-12-25 23:20:33 | [diff] [blame] | 429 | krate_emscripten(build, &compiler, target, mode); |
Alex Crichton | 39a5d3f | 2016-06-28 20:31:30 | [diff] [blame] | 430 | } else { |
Alex Crichton | a270b80 | 2016-10-21 20:18:09 | [diff] [blame] | 431 | cargo.args(&build.flags.cmd.test_args()); |
Alex Crichton | 39a5d3f | 2016-06-28 20:31:30 | [diff] [blame] | 432 | build.run(&mut cargo); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | fn krate_android(build: &Build, |
| 437 | compiler: &Compiler, |
| 438 | target: &str, |
| 439 | mode: Mode) { |
| 440 | let mut tests = Vec::new(); |
| 441 | let out_dir = build.cargo_out(compiler, mode, target); |
| 442 | find_tests(&out_dir, target, &mut tests); |
| 443 | find_tests(&out_dir.join("deps"), target, &mut tests); |
| 444 | |
| 445 | for test in tests { |
| 446 | build.run(Command::new("adb").arg("push").arg(&test).arg(ADB_TEST_DIR)); |
| 447 | |
| 448 | let test_file_name = test.file_name().unwrap().to_string_lossy(); |
| 449 | let log = format!("{}/check-stage{}-T-{}-H-{}-{}.log", |
| 450 | ADB_TEST_DIR, |
| 451 | compiler.stage, |
| 452 | target, |
| 453 | compiler.host, |
| 454 | test_file_name); |
Alex Crichton | 0e272de | 2016-11-16 20:31:19 | [diff] [blame] | 455 | let quiet = if build.config.quiet_tests { "--quiet" } else { "" }; |
Alex Crichton | 39a5d3f | 2016-06-28 20:31:30 | [diff] [blame] | 456 | let program = format!("(cd {dir}; \ |
| 457 | LD_LIBRARY_PATH=./{target} ./{test} \ |
| 458 | --logfile {log} \ |
Alex Crichton | 0e272de | 2016-11-16 20:31:19 | [diff] [blame] | 459 | {quiet} \ |
Alex Crichton | 39a5d3f | 2016-06-28 20:31:30 | [diff] [blame] | 460 | {args})", |
| 461 | dir = ADB_TEST_DIR, |
| 462 | target = target, |
| 463 | test = test_file_name, |
| 464 | log = log, |
Alex Crichton | 0e272de | 2016-11-16 20:31:19 | [diff] [blame] | 465 | quiet = quiet, |
Alex Crichton | a270b80 | 2016-10-21 20:18:09 | [diff] [blame] | 466 | args = build.flags.cmd.test_args().join(" ")); |
Alex Crichton | 39a5d3f | 2016-06-28 20:31:30 | [diff] [blame] | 467 | |
| 468 | let output = output(Command::new("adb").arg("shell").arg(&program)); |
| 469 | println!("{}", output); |
Alex Crichton | 3187672 | 2017-01-01 01:42:40 | [diff] [blame] | 470 | |
| 471 | t!(fs::create_dir_all(build.out.join("tmp"))); |
Alex Crichton | 39a5d3f | 2016-06-28 20:31:30 | [diff] [blame] | 472 | build.run(Command::new("adb") |
| 473 | .arg("pull") |
| 474 | .arg(&log) |
| 475 | .arg(build.out.join("tmp"))); |
| 476 | build.run(Command::new("adb").arg("shell").arg("rm").arg(&log)); |
| 477 | if !output.contains("result: ok") { |
| 478 | panic!("some tests failed"); |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
Brian Anderson | b8b50f0 | 2016-09-06 00:41:50 | [diff] [blame] | 483 | fn krate_emscripten(build: &Build, |
| 484 | compiler: &Compiler, |
| 485 | target: &str, |
| 486 | mode: Mode) { |
Ross Schulman | ad9184c | 2016-09-05 23:56:48 | [diff] [blame] | 487 | let mut tests = Vec::new(); |
| 488 | let out_dir = build.cargo_out(compiler, mode, target); |
| 489 | find_tests(&out_dir, target, &mut tests); |
| 490 | find_tests(&out_dir.join("deps"), target, &mut tests); |
| 491 | |
| 492 | for test in tests { |
| 493 | let test_file_name = test.to_string_lossy().into_owned(); |
Brian Anderson | fcd3279 | 2016-09-06 20:36:14 | [diff] [blame] | 494 | println!("running {}", test_file_name); |
Brian Anderson | 8401e37 | 2016-09-15 19:42:26 | [diff] [blame] | 495 | let nodejs = build.config.nodejs.as_ref().expect("nodejs not configured"); |
Alex Crichton | 0e272de | 2016-11-16 20:31:19 | [diff] [blame] | 496 | let mut cmd = Command::new(nodejs); |
Alex Crichton | 03fb5ad | 2016-12-08 01:26:48 | [diff] [blame] | 497 | cmd.arg(&test_file_name); |
Alex Crichton | 0e272de | 2016-11-16 20:31:19 | [diff] [blame] | 498 | if build.config.quiet_tests { |
| 499 | cmd.arg("--quiet"); |
| 500 | } |
| 501 | build.run(&mut cmd); |
Ross Schulman | ad9184c | 2016-09-05 23:56:48 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
| 505 | |
Alex Crichton | 39a5d3f | 2016-06-28 20:31:30 | [diff] [blame] | 506 | fn find_tests(dir: &Path, |
| 507 | target: &str, |
| 508 | dst: &mut Vec<PathBuf>) { |
| 509 | for e in t!(dir.read_dir()).map(|e| t!(e)) { |
| 510 | let file_type = t!(e.file_type()); |
| 511 | if !file_type.is_file() { |
| 512 | continue |
| 513 | } |
| 514 | let filename = e.file_name().into_string().unwrap(); |
| 515 | if (target.contains("windows") && filename.ends_with(".exe")) || |
Ross Schulman | ad9184c | 2016-09-05 23:56:48 | [diff] [blame] | 516 | (!target.contains("windows") && !filename.contains(".")) || |
Brian Anderson | fcd3279 | 2016-09-06 20:36:14 | [diff] [blame] | 517 | (target.contains("emscripten") && filename.contains(".js")){ |
Alex Crichton | 39a5d3f | 2016-06-28 20:31:30 | [diff] [blame] | 518 | dst.push(e.path()); |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | pub fn android_copy_libs(build: &Build, |
| 524 | compiler: &Compiler, |
| 525 | target: &str) { |
Alex Crichton | cf8fde1 | 2016-12-31 02:54:05 | [diff] [blame] | 526 | if !target.contains("android") { |
| 527 | return |
| 528 | } |
| 529 | |
Alex Crichton | 39a5d3f | 2016-06-28 20:31:30 | [diff] [blame] | 530 | println!("Android copy libs to emulator ({})", target); |
Alex Crichton | 3187672 | 2017-01-01 01:42:40 | [diff] [blame] | 531 | build.run(Command::new("adb").arg("wait-for-device")); |
Alex Crichton | 39a5d3f | 2016-06-28 20:31:30 | [diff] [blame] | 532 | build.run(Command::new("adb").arg("remount")); |
| 533 | build.run(Command::new("adb").args(&["shell", "rm", "-r", ADB_TEST_DIR])); |
| 534 | build.run(Command::new("adb").args(&["shell", "mkdir", ADB_TEST_DIR])); |
| 535 | build.run(Command::new("adb") |
| 536 | .arg("push") |
| 537 | .arg(build.src.join("src/etc/adb_run_wrapper.sh")) |
| 538 | .arg(ADB_TEST_DIR)); |
| 539 | |
| 540 | let target_dir = format!("{}/{}", ADB_TEST_DIR, target); |
| 541 | build.run(Command::new("adb").args(&["shell", "mkdir", &target_dir[..]])); |
| 542 | |
| 543 | for f in t!(build.sysroot_libdir(compiler, target).read_dir()) { |
| 544 | let f = t!(f); |
| 545 | let name = f.file_name().into_string().unwrap(); |
| 546 | if util::is_dylib(&name) { |
| 547 | build.run(Command::new("adb") |
| 548 | .arg("push") |
| 549 | .arg(f.path()) |
| 550 | .arg(&target_dir)); |
| 551 | } |
| 552 | } |
Alex Crichton | bb9062a | 2016-04-29 21:23:15 | [diff] [blame] | 553 | } |
Alex Crichton | d38db82 | 2016-12-09 01:13:55 | [diff] [blame] | 554 | |
| 555 | /// Run "distcheck", a 'make check' from a tarball |
| 556 | pub fn distcheck(build: &Build) { |
| 557 | if build.config.build != "x86_64-unknown-linux-gnu" { |
| 558 | return |
| 559 | } |
| 560 | if !build.config.host.iter().any(|s| s == "x86_64-unknown-linux-gnu") { |
| 561 | return |
| 562 | } |
| 563 | if !build.config.target.iter().any(|s| s == "x86_64-unknown-linux-gnu") { |
| 564 | return |
| 565 | } |
| 566 | |
| 567 | let dir = build.out.join("tmp").join("distcheck"); |
| 568 | let _ = fs::remove_dir_all(&dir); |
| 569 | t!(fs::create_dir_all(&dir)); |
| 570 | |
| 571 | let mut cmd = Command::new("tar"); |
| 572 | cmd.arg("-xzf") |
| 573 | .arg(dist::rust_src_location(build)) |
| 574 | .arg("--strip-components=1") |
| 575 | .current_dir(&dir); |
| 576 | build.run(&mut cmd); |
| 577 | build.run(Command::new("./configure") |
Alex Crichton | 4781eb3 | 2016-12-30 17:26:25 | [diff] [blame] | 578 | .args(&build.config.configure_args) |
Alex Crichton | d38db82 | 2016-12-09 01:13:55 | [diff] [blame] | 579 | .current_dir(&dir)); |
Sébastien Marie | a7d9025 | 2016-12-17 19:09:23 | [diff] [blame] | 580 | build.run(Command::new(build_helper::make(&build.config.build)) |
Alex Crichton | d38db82 | 2016-12-09 01:13:55 | [diff] [blame] | 581 | .arg("check") |
| 582 | .current_dir(&dir)); |
| 583 | } |
Alex Crichton | 1a040b3 | 2016-12-31 03:50:57 | [diff] [blame] | 584 | |
| 585 | /// Test the build system itself |
| 586 | pub fn bootstrap(build: &Build) { |
| 587 | let mut cmd = Command::new(&build.cargo); |
| 588 | cmd.arg("test") |
| 589 | .current_dir(build.src.join("src/bootstrap")) |
| 590 | .env("CARGO_TARGET_DIR", build.out.join("bootstrap")) |
| 591 | .env("RUSTC", &build.rustc); |
| 592 | cmd.arg("--").args(&build.flags.cmd.test_args()); |
| 593 | build.run(&mut cmd); |
| 594 | } |