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