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 | bb9062a | 2016-04-29 21:23:15 | [diff] [blame] | 16 | use std::env; |
| 17 | use std::fs::{self, File}; |
| 18 | use std::io::prelude::*; |
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 | bb9062a | 2016-04-29 21:23:15 | [diff] [blame] | 23 | use bootstrap::{dylib_path, dylib_path_var}; |
Alex Crichton | 126e09e | 2016-04-14 22:51:03 | [diff] [blame] | 24 | |
Alex Crichton | bb9062a | 2016-04-29 21:23:15 | [diff] [blame] | 25 | use build::{Build, Compiler, Mode}; |
Alex Crichton | defd1b3 | 2016-03-08 07:15:55 | [diff] [blame] | 26 | |
Alex Crichton | f72bfe6 | 2016-05-02 22:16:15 | [diff] [blame] | 27 | /// Runs the `linkchecker` tool as compiled in `stage` by the `host` compiler. |
| 28 | /// |
| 29 | /// This tool in `src/tools` will verify the validity of all our links in the |
| 30 | /// documentation to ensure we don't have a bunch of dead ones. |
Alex Crichton | defd1b3 | 2016-03-08 07:15:55 | [diff] [blame] | 31 | pub fn linkcheck(build: &Build, stage: u32, host: &str) { |
| 32 | println!("Linkcheck stage{} ({})", stage, host); |
| 33 | let compiler = Compiler::new(stage, host); |
Alex Crichton | 4a917e0 | 2016-03-12 00:21:05 | [diff] [blame] | 34 | build.run(build.tool_cmd(&compiler, "linkchecker") |
| 35 | .arg(build.out.join(host).join("doc"))); |
Alex Crichton | defd1b3 | 2016-03-08 07:15:55 | [diff] [blame] | 36 | } |
Brian Anderson | 3a790ac | 2016-03-18 20:54:31 | [diff] [blame] | 37 | |
Alex Crichton | f72bfe6 | 2016-05-02 22:16:15 | [diff] [blame] | 38 | /// Runs the `cargotest` tool as compiled in `stage` by the `host` compiler. |
| 39 | /// |
| 40 | /// This tool in `src/tools` will check out a few Rust projects and run `cargo |
| 41 | /// test` to ensure that we don't regress the test suites there. |
Brian Anderson | 3a790ac | 2016-03-18 20:54:31 | [diff] [blame] | 42 | pub fn cargotest(build: &Build, stage: u32, host: &str) { |
| 43 | let ref compiler = Compiler::new(stage, host); |
Brian Anderson | 8019922 | 2016-04-06 18:03:42 | [diff] [blame] | 44 | |
| 45 | // Configure PATH to find the right rustc. NB. we have to use PATH |
| 46 | // and not RUSTC because the Cargo test suite has tests that will |
| 47 | // fail if rustc is not spelled `rustc`. |
| 48 | let path = build.sysroot(compiler).join("bin"); |
| 49 | let old_path = ::std::env::var("PATH").expect(""); |
| 50 | let sep = if cfg!(windows) { ";" } else {":" }; |
| 51 | let ref newpath = format!("{}{}{}", path.display(), sep, old_path); |
| 52 | |
Alex Crichton | 73c2d2a | 2016-04-14 21:27:51 | [diff] [blame] | 53 | // Note that this is a short, cryptic, and not scoped directory name. This |
| 54 | // is currently to minimize the length of path on Windows where we otherwise |
| 55 | // quickly run into path name limit constraints. |
| 56 | let out_dir = build.out.join("ct"); |
| 57 | t!(fs::create_dir_all(&out_dir)); |
| 58 | |
Brian Anderson | 3a790ac | 2016-03-18 20:54:31 | [diff] [blame] | 59 | build.run(build.tool_cmd(compiler, "cargotest") |
Alex Crichton | 73c2d2a | 2016-04-14 21:27:51 | [diff] [blame] | 60 | .env("PATH", newpath) |
| 61 | .arg(&build.cargo) |
| 62 | .arg(&out_dir)); |
Brian Anderson | 3a790ac | 2016-03-18 20:54:31 | [diff] [blame] | 63 | } |
Alex Crichton | 9dd3c54 | 2016-03-29 20:14:52 | [diff] [blame] | 64 | |
Alex Crichton | f72bfe6 | 2016-05-02 22:16:15 | [diff] [blame] | 65 | /// Runs the `tidy` tool as compiled in `stage` by the `host` compiler. |
| 66 | /// |
| 67 | /// This tool in `src/tools` checks up on various bits and pieces of style and |
| 68 | /// otherwise just implements a few lint-like checks that are specific to the |
| 69 | /// compiler itself. |
Alex Crichton | 9dd3c54 | 2016-03-29 20:14:52 | [diff] [blame] | 70 | pub fn tidy(build: &Build, stage: u32, host: &str) { |
| 71 | println!("tidy check stage{} ({})", stage, host); |
| 72 | let compiler = Compiler::new(stage, host); |
| 73 | build.run(build.tool_cmd(&compiler, "tidy") |
| 74 | .arg(build.src.join("src"))); |
| 75 | } |
Alex Crichton | b325baf | 2016-04-05 18:34:23 | [diff] [blame] | 76 | |
| 77 | fn testdir(build: &Build, host: &str) -> PathBuf { |
| 78 | build.out.join(host).join("test") |
| 79 | } |
| 80 | |
Alex Crichton | f72bfe6 | 2016-05-02 22:16:15 | [diff] [blame] | 81 | /// Executes the `compiletest` tool to run a suite of tests. |
| 82 | /// |
| 83 | /// Compiles all tests with `compiler` for `target` with the specified |
| 84 | /// compiletest `mode` and `suite` arguments. For example `mode` can be |
| 85 | /// "run-pass" or `suite` can be something like `debuginfo`. |
Alex Crichton | b325baf | 2016-04-05 18:34:23 | [diff] [blame] | 86 | pub fn compiletest(build: &Build, |
| 87 | compiler: &Compiler, |
| 88 | target: &str, |
| 89 | mode: &str, |
| 90 | suite: &str) { |
| 91 | let mut cmd = build.tool_cmd(compiler, "compiletest"); |
| 92 | |
Alex Crichton | f72bfe6 | 2016-05-02 22:16:15 | [diff] [blame] | 93 | // compiletest currently has... a lot of arguments, so let's just pass all |
| 94 | // of them! |
| 95 | |
Alex Crichton | b325baf | 2016-04-05 18:34:23 | [diff] [blame] | 96 | cmd.arg("--compile-lib-path").arg(build.rustc_libdir(compiler)); |
| 97 | cmd.arg("--run-lib-path").arg(build.sysroot_libdir(compiler, target)); |
| 98 | cmd.arg("--rustc-path").arg(build.compiler_path(compiler)); |
| 99 | cmd.arg("--rustdoc-path").arg(build.rustdoc(compiler)); |
| 100 | cmd.arg("--src-base").arg(build.src.join("src/test").join(suite)); |
Alex Crichton | b325baf | 2016-04-05 18:34:23 | [diff] [blame] | 101 | cmd.arg("--build-base").arg(testdir(build, compiler.host).join(suite)); |
| 102 | cmd.arg("--stage-id").arg(format!("stage{}-{}", compiler.stage, target)); |
| 103 | cmd.arg("--mode").arg(mode); |
| 104 | cmd.arg("--target").arg(target); |
| 105 | cmd.arg("--host").arg(compiler.host); |
| 106 | cmd.arg("--llvm-filecheck").arg(build.llvm_filecheck(&build.config.build)); |
| 107 | |
Alex Crichton | f4e4ec7 | 2016-05-13 22:26:41 | [diff] [blame^] | 108 | let mut flags = format!("-Crpath"); |
| 109 | if build.config.rust_optimize_tests { |
| 110 | flags.push_str(" -O"); |
| 111 | } |
| 112 | if build.config.rust_debuginfo_tests { |
| 113 | flags.push_str(" -g"); |
| 114 | } |
| 115 | |
| 116 | cmd.arg("--host-rustcflags").arg(&flags); |
| 117 | |
Alex Crichton | b325baf | 2016-04-05 18:34:23 | [diff] [blame] | 118 | let linkflag = format!("-Lnative={}", build.test_helpers_out(target).display()); |
Alex Crichton | f4e4ec7 | 2016-05-13 22:26:41 | [diff] [blame^] | 119 | cmd.arg("--target-rustcflags").arg(format!("{} {}", flags, linkflag)); |
Alex Crichton | b325baf | 2016-04-05 18:34:23 | [diff] [blame] | 120 | |
| 121 | // FIXME: needs android support |
| 122 | cmd.arg("--android-cross-path").arg(""); |
Alex Crichton | cbe6292 | 2016-04-19 16:44:19 | [diff] [blame] | 123 | |
Alex Crichton | b325baf | 2016-04-05 18:34:23 | [diff] [blame] | 124 | // FIXME: CFG_PYTHON should probably be detected more robustly elsewhere |
Alex Crichton | cbe6292 | 2016-04-19 16:44:19 | [diff] [blame] | 125 | let python_default = "python"; |
| 126 | cmd.arg("--docck-python").arg(python_default); |
| 127 | |
| 128 | if build.config.build.ends_with("apple-darwin") { |
| 129 | // Force /usr/bin/python on OSX for LLDB tests because we're loading the |
| 130 | // LLDB plugin's compiled module which only works with the system python |
| 131 | // (namely not Homebrew-installed python) |
| 132 | cmd.arg("--lldb-python").arg("/usr/bin/python"); |
| 133 | } else { |
| 134 | cmd.arg("--lldb-python").arg(python_default); |
| 135 | } |
Alex Crichton | b325baf | 2016-04-05 18:34:23 | [diff] [blame] | 136 | |
| 137 | if let Some(ref vers) = build.gdb_version { |
| 138 | cmd.arg("--gdb-version").arg(vers); |
| 139 | } |
| 140 | if let Some(ref vers) = build.lldb_version { |
| 141 | cmd.arg("--lldb-version").arg(vers); |
| 142 | } |
| 143 | if let Some(ref dir) = build.lldb_python_dir { |
| 144 | cmd.arg("--lldb-python-dir").arg(dir); |
| 145 | } |
| 146 | |
| 147 | cmd.args(&build.flags.args); |
| 148 | |
| 149 | if build.config.verbose || build.flags.verbose { |
| 150 | cmd.arg("--verbose"); |
| 151 | } |
| 152 | |
Alex Crichton | f72bfe6 | 2016-05-02 22:16:15 | [diff] [blame] | 153 | // Only pass correct values for these flags for the `run-make` suite as it |
| 154 | // requires that a C++ compiler was configured which isn't always the case. |
Alex Crichton | 126e09e | 2016-04-14 22:51:03 | [diff] [blame] | 155 | if suite == "run-make" { |
| 156 | let llvm_config = build.llvm_config(target); |
| 157 | let llvm_components = output(Command::new(&llvm_config).arg("--components")); |
| 158 | let llvm_cxxflags = output(Command::new(&llvm_config).arg("--cxxflags")); |
| 159 | cmd.arg("--cc").arg(build.cc(target)) |
| 160 | .arg("--cxx").arg(build.cxx(target)) |
| 161 | .arg("--cflags").arg(build.cflags(target).join(" ")) |
| 162 | .arg("--llvm-components").arg(llvm_components.trim()) |
| 163 | .arg("--llvm-cxxflags").arg(llvm_cxxflags.trim()); |
| 164 | } else { |
| 165 | cmd.arg("--cc").arg("") |
| 166 | .arg("--cxx").arg("") |
| 167 | .arg("--cflags").arg("") |
| 168 | .arg("--llvm-components").arg("") |
| 169 | .arg("--llvm-cxxflags").arg(""); |
| 170 | } |
| 171 | |
| 172 | // Running a C compiler on MSVC requires a few env vars to be set, to be |
| 173 | // sure to set them here. |
| 174 | if target.contains("msvc") { |
| 175 | for &(ref k, ref v) in build.cc[target].0.env() { |
| 176 | if k != "PATH" { |
| 177 | cmd.env(k, v); |
| 178 | } |
| 179 | } |
| 180 | } |
Alex Crichton | f72bfe6 | 2016-05-02 22:16:15 | [diff] [blame] | 181 | build.add_bootstrap_key(compiler, &mut cmd); |
Alex Crichton | 126e09e | 2016-04-14 22:51:03 | [diff] [blame] | 182 | |
Alex Crichton | b325baf | 2016-04-05 18:34:23 | [diff] [blame] | 183 | build.run(&mut cmd); |
| 184 | } |
Alex Crichton | ede8944 | 2016-04-15 01:00:35 | [diff] [blame] | 185 | |
Alex Crichton | f72bfe6 | 2016-05-02 22:16:15 | [diff] [blame] | 186 | /// Run `rustdoc --test` for all documentation in `src/doc`. |
| 187 | /// |
| 188 | /// This will run all tests in our markdown documentation (e.g. the book) |
| 189 | /// located in `src/doc`. The `rustdoc` that's run is the one that sits next to |
| 190 | /// `compiler`. |
Alex Crichton | ede8944 | 2016-04-15 01:00:35 | [diff] [blame] | 191 | pub fn docs(build: &Build, compiler: &Compiler) { |
Alex Crichton | f72bfe6 | 2016-05-02 22:16:15 | [diff] [blame] | 192 | // Do a breadth-first traversal of the `src/doc` directory and just run |
| 193 | // tests for all files that end in `*.md` |
Alex Crichton | ede8944 | 2016-04-15 01:00:35 | [diff] [blame] | 194 | let mut stack = vec![build.src.join("src/doc")]; |
| 195 | |
| 196 | while let Some(p) = stack.pop() { |
| 197 | if p.is_dir() { |
| 198 | stack.extend(t!(p.read_dir()).map(|p| t!(p).path())); |
| 199 | continue |
| 200 | } |
| 201 | |
| 202 | if p.extension().and_then(|s| s.to_str()) != Some("md") { |
| 203 | continue |
| 204 | } |
| 205 | |
| 206 | println!("doc tests for: {}", p.display()); |
| 207 | markdown_test(build, compiler, &p); |
| 208 | } |
| 209 | } |
| 210 | |
Alex Crichton | f72bfe6 | 2016-05-02 22:16:15 | [diff] [blame] | 211 | /// Run the error index generator tool to execute the tests located in the error |
| 212 | /// index. |
| 213 | /// |
| 214 | /// The `error_index_generator` tool lives in `src/tools` and is used to |
| 215 | /// generate a markdown file from the error indexes of the code base which is |
| 216 | /// then passed to `rustdoc --test`. |
Alex Crichton | ede8944 | 2016-04-15 01:00:35 | [diff] [blame] | 217 | pub fn error_index(build: &Build, compiler: &Compiler) { |
| 218 | println!("Testing error-index stage{}", compiler.stage); |
| 219 | |
| 220 | let output = testdir(build, compiler.host).join("error-index.md"); |
| 221 | build.run(build.tool_cmd(compiler, "error_index_generator") |
| 222 | .arg("markdown") |
| 223 | .arg(&output) |
| 224 | .env("CFG_BUILD", &build.config.build)); |
| 225 | |
| 226 | markdown_test(build, compiler, &output); |
| 227 | } |
| 228 | |
| 229 | fn markdown_test(build: &Build, compiler: &Compiler, markdown: &Path) { |
| 230 | let mut cmd = Command::new(build.rustdoc(compiler)); |
| 231 | build.add_rustc_lib_path(compiler, &mut cmd); |
| 232 | cmd.arg("--test"); |
| 233 | cmd.arg(markdown); |
| 234 | cmd.arg("--test-args").arg(build.flags.args.join(" ")); |
| 235 | build.run(&mut cmd); |
| 236 | } |
Alex Crichton | bb9062a | 2016-04-29 21:23:15 | [diff] [blame] | 237 | |
| 238 | /// Run all unit tests plus documentation tests for an entire crate DAG defined |
| 239 | /// by a `Cargo.toml` |
| 240 | /// |
| 241 | /// This is what runs tests for crates like the standard library, compiler, etc. |
| 242 | /// It essentially is the driver for running `cargo test`. |
| 243 | /// |
| 244 | /// Currently this runs all tests for a DAG by passing a bunch of `-p foo` |
| 245 | /// arguments, and those arguments are discovered from `Cargo.lock`. |
| 246 | pub fn krate(build: &Build, |
| 247 | compiler: &Compiler, |
| 248 | target: &str, |
| 249 | mode: Mode) { |
| 250 | let (name, path, features) = match mode { |
| 251 | Mode::Libstd => ("libstd", "src/rustc/std_shim", build.std_features()), |
| 252 | Mode::Libtest => ("libtest", "src/rustc/test_shim", String::new()), |
| 253 | Mode::Librustc => ("librustc", "src/rustc", build.rustc_features()), |
| 254 | _ => panic!("can only test libraries"), |
| 255 | }; |
| 256 | println!("Testing {} stage{} ({} -> {})", name, compiler.stage, |
| 257 | compiler.host, target); |
| 258 | |
| 259 | // Build up the base `cargo test` command. |
| 260 | let mut cargo = build.cargo(compiler, mode, target, "test"); |
| 261 | cargo.arg("--manifest-path") |
| 262 | .arg(build.src.join(path).join("Cargo.toml")) |
| 263 | .arg("--features").arg(features); |
| 264 | |
| 265 | // Generate a list of `-p` arguments to pass to the `cargo test` invocation |
| 266 | // by crawling the corresponding Cargo.lock file. |
| 267 | let lockfile = build.src.join(path).join("Cargo.lock"); |
| 268 | let mut contents = String::new(); |
| 269 | t!(t!(File::open(&lockfile)).read_to_string(&mut contents)); |
| 270 | let mut lines = contents.lines(); |
| 271 | while let Some(line) = lines.next() { |
| 272 | let prefix = "name = \""; |
| 273 | if !line.starts_with(prefix) { |
| 274 | continue |
| 275 | } |
| 276 | lines.next(); // skip `version = ...` |
| 277 | |
| 278 | // skip crates.io or otherwise non-path crates |
| 279 | if let Some(line) = lines.next() { |
| 280 | if line.starts_with("source") { |
| 281 | continue |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | let crate_name = &line[prefix.len()..line.len() - 1]; |
| 286 | |
| 287 | // Right now jemalloc is our only target-specific crate in the sense |
| 288 | // that it's not present on all platforms. Custom skip it here for now, |
| 289 | // but if we add more this probably wants to get more generalized. |
| 290 | if crate_name.contains("jemalloc") { |
| 291 | continue |
| 292 | } |
| 293 | |
| 294 | cargo.arg("-p").arg(crate_name); |
| 295 | } |
| 296 | |
| 297 | // The tests are going to run with the *target* libraries, so we need to |
| 298 | // ensure that those libraries show up in the LD_LIBRARY_PATH equivalent. |
| 299 | // |
| 300 | // Note that to run the compiler we need to run with the *host* libraries, |
| 301 | // but our wrapper scripts arrange for that to be the case anyway. |
| 302 | let mut dylib_path = dylib_path(); |
| 303 | dylib_path.insert(0, build.sysroot_libdir(compiler, target)); |
| 304 | cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap()); |
| 305 | cargo.args(&build.flags.args); |
| 306 | |
| 307 | build.run(&mut cargo); |
| 308 | } |