blob: 154d9556fd7ba2331e248ab1e9c20c7ca72757f7 [file] [log] [blame]
Alex Crichtondefd1b32016-03-08 07:15:551// 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 Crichtonf72bfe62016-05-02 22:16:1511//! 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 Crichtonbb9062a2016-04-29 21:23:1516use std::env;
17use std::fs::{self, File};
18use std::io::prelude::*;
Alex Crichtonede89442016-04-15 01:00:3519use std::path::{PathBuf, Path};
20use std::process::Command;
Alex Crichton73c2d2a2016-04-14 21:27:5121
Alex Crichton126e09e2016-04-14 22:51:0322use build_helper::output;
Alex Crichtonbb9062a2016-04-29 21:23:1523use bootstrap::{dylib_path, dylib_path_var};
Alex Crichton126e09e2016-04-14 22:51:0324
Alex Crichtonbb9062a2016-04-29 21:23:1525use build::{Build, Compiler, Mode};
Alex Crichtondefd1b32016-03-08 07:15:5526
Alex Crichtonf72bfe62016-05-02 22:16:1527/// 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 Crichtondefd1b32016-03-08 07:15:5531pub fn linkcheck(build: &Build, stage: u32, host: &str) {
32 println!("Linkcheck stage{} ({})", stage, host);
33 let compiler = Compiler::new(stage, host);
Alex Crichton4a917e02016-03-12 00:21:0534 build.run(build.tool_cmd(&compiler, "linkchecker")
35 .arg(build.out.join(host).join("doc")));
Alex Crichtondefd1b32016-03-08 07:15:5536}
Brian Anderson3a790ac2016-03-18 20:54:3137
Alex Crichtonf72bfe62016-05-02 22:16:1538/// 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 Anderson3a790ac2016-03-18 20:54:3142pub fn cargotest(build: &Build, stage: u32, host: &str) {
43 let ref compiler = Compiler::new(stage, host);
Brian Anderson80199222016-04-06 18:03:4244
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 Crichton73c2d2a2016-04-14 21:27:5153 // 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 Anderson3a790ac2016-03-18 20:54:3159 build.run(build.tool_cmd(compiler, "cargotest")
Alex Crichton73c2d2a2016-04-14 21:27:5160 .env("PATH", newpath)
61 .arg(&build.cargo)
62 .arg(&out_dir));
Brian Anderson3a790ac2016-03-18 20:54:3163}
Alex Crichton9dd3c542016-03-29 20:14:5264
Alex Crichtonf72bfe62016-05-02 22:16:1565/// 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 Crichton9dd3c542016-03-29 20:14:5270pub 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 Crichtonb325baf2016-04-05 18:34:2376
77fn testdir(build: &Build, host: &str) -> PathBuf {
78 build.out.join(host).join("test")
79}
80
Alex Crichtonf72bfe62016-05-02 22:16:1581/// 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 Crichtonb325baf2016-04-05 18:34:2386pub 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 Crichtonf72bfe62016-05-02 22:16:1593 // compiletest currently has... a lot of arguments, so let's just pass all
94 // of them!
95
Alex Crichtonb325baf2016-04-05 18:34:2396 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 Crichtonb325baf2016-04-05 18:34:23101 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 Crichtonf4e4ec72016-05-13 22:26:41108 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 Crichtonb325baf2016-04-05 18:34:23118 let linkflag = format!("-Lnative={}", build.test_helpers_out(target).display());
Alex Crichtonf4e4ec72016-05-13 22:26:41119 cmd.arg("--target-rustcflags").arg(format!("{} {}", flags, linkflag));
Alex Crichtonb325baf2016-04-05 18:34:23120
121 // FIXME: needs android support
122 cmd.arg("--android-cross-path").arg("");
Alex Crichtoncbe62922016-04-19 16:44:19123
Alex Crichtonb325baf2016-04-05 18:34:23124 // FIXME: CFG_PYTHON should probably be detected more robustly elsewhere
Alex Crichtoncbe62922016-04-19 16:44:19125 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 Crichtonb325baf2016-04-05 18:34:23136
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 Crichtonf72bfe62016-05-02 22:16:15153 // 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 Crichton126e09e2016-04-14 22:51:03155 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 Crichtonf72bfe62016-05-02 22:16:15181 build.add_bootstrap_key(compiler, &mut cmd);
Alex Crichton126e09e2016-04-14 22:51:03182
Alex Crichtonb325baf2016-04-05 18:34:23183 build.run(&mut cmd);
184}
Alex Crichtonede89442016-04-15 01:00:35185
Alex Crichtonf72bfe62016-05-02 22:16:15186/// 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 Crichtonede89442016-04-15 01:00:35191pub fn docs(build: &Build, compiler: &Compiler) {
Alex Crichtonf72bfe62016-05-02 22:16:15192 // Do a breadth-first traversal of the `src/doc` directory and just run
193 // tests for all files that end in `*.md`
Alex Crichtonede89442016-04-15 01:00:35194 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 Crichtonf72bfe62016-05-02 22:16:15211/// 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 Crichtonede89442016-04-15 01:00:35217pub 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
229fn 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 Crichtonbb9062a2016-04-29 21:23:15237
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`.
246pub 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}