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