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