]>
Commit | Line | Data |
---|---|---|
54a0048b SL |
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 | ||
a7813a04 XL |
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. | |
54a0048b | 15 | |
a7813a04 XL |
16 | use std::env; |
17 | use std::fs::{self, File}; | |
18 | use std::io::prelude::*; | |
19 | use std::path::{PathBuf, Path}; | |
20 | use std::process::Command; | |
21 | ||
22 | use build_helper::output; | |
a7813a04 | 23 | |
5bcae85e SL |
24 | use {Build, Compiler, Mode}; |
25 | use util::{self, dylib_path, dylib_path_var}; | |
3157f602 XL |
26 | |
27 | const ADB_TEST_DIR: &'static str = "/data/tmp"; | |
a7813a04 XL |
28 | |
29 | /// Runs the `linkchecker` tool as compiled in `stage` by the `host` compiler. | |
30 | /// | |
31 | /// This tool in `src/tools` will verify the validity of all our links in the | |
32 | /// documentation to ensure we don't have a bunch of dead ones. | |
54a0048b SL |
33 | pub fn linkcheck(build: &Build, stage: u32, host: &str) { |
34 | println!("Linkcheck stage{} ({})", stage, host); | |
35 | let compiler = Compiler::new(stage, host); | |
36 | build.run(build.tool_cmd(&compiler, "linkchecker") | |
37 | .arg(build.out.join(host).join("doc"))); | |
38 | } | |
39 | ||
a7813a04 XL |
40 | /// Runs the `cargotest` tool as compiled in `stage` by the `host` compiler. |
41 | /// | |
42 | /// This tool in `src/tools` will check out a few Rust projects and run `cargo | |
43 | /// test` to ensure that we don't regress the test suites there. | |
54a0048b | 44 | pub fn cargotest(build: &Build, stage: u32, host: &str) { |
54a0048b SL |
45 | let ref compiler = Compiler::new(stage, host); |
46 | ||
47 | // Configure PATH to find the right rustc. NB. we have to use PATH | |
48 | // and not RUSTC because the Cargo test suite has tests that will | |
49 | // fail if rustc is not spelled `rustc`. | |
50 | let path = build.sysroot(compiler).join("bin"); | |
51 | let old_path = ::std::env::var("PATH").expect(""); | |
52 | let sep = if cfg!(windows) { ";" } else {":" }; | |
53 | let ref newpath = format!("{}{}{}", path.display(), sep, old_path); | |
54 | ||
a7813a04 XL |
55 | // Note that this is a short, cryptic, and not scoped directory name. This |
56 | // is currently to minimize the length of path on Windows where we otherwise | |
57 | // quickly run into path name limit constraints. | |
58 | let out_dir = build.out.join("ct"); | |
59 | t!(fs::create_dir_all(&out_dir)); | |
60 | ||
54a0048b | 61 | build.run(build.tool_cmd(compiler, "cargotest") |
a7813a04 XL |
62 | .env("PATH", newpath) |
63 | .arg(&build.cargo) | |
64 | .arg(&out_dir)); | |
65 | } | |
66 | ||
67 | /// Runs the `tidy` tool as compiled in `stage` by the `host` compiler. | |
68 | /// | |
69 | /// This tool in `src/tools` checks up on various bits and pieces of style and | |
70 | /// otherwise just implements a few lint-like checks that are specific to the | |
71 | /// compiler itself. | |
72 | pub fn tidy(build: &Build, stage: u32, host: &str) { | |
73 | println!("tidy check stage{} ({})", stage, host); | |
74 | let compiler = Compiler::new(stage, host); | |
75 | build.run(build.tool_cmd(&compiler, "tidy") | |
76 | .arg(build.src.join("src"))); | |
77 | } | |
78 | ||
79 | fn testdir(build: &Build, host: &str) -> PathBuf { | |
80 | build.out.join(host).join("test") | |
81 | } | |
82 | ||
83 | /// Executes the `compiletest` tool to run a suite of tests. | |
84 | /// | |
85 | /// Compiles all tests with `compiler` for `target` with the specified | |
86 | /// compiletest `mode` and `suite` arguments. For example `mode` can be | |
87 | /// "run-pass" or `suite` can be something like `debuginfo`. | |
88 | pub fn compiletest(build: &Build, | |
89 | compiler: &Compiler, | |
90 | target: &str, | |
91 | mode: &str, | |
92 | suite: &str) { | |
3157f602 | 93 | println!("Check compiletest {} ({} -> {})", suite, compiler.host, target); |
a7813a04 XL |
94 | let mut cmd = build.tool_cmd(compiler, "compiletest"); |
95 | ||
96 | // compiletest currently has... a lot of arguments, so let's just pass all | |
97 | // of them! | |
98 | ||
99 | cmd.arg("--compile-lib-path").arg(build.rustc_libdir(compiler)); | |
100 | cmd.arg("--run-lib-path").arg(build.sysroot_libdir(compiler, target)); | |
101 | cmd.arg("--rustc-path").arg(build.compiler_path(compiler)); | |
102 | cmd.arg("--rustdoc-path").arg(build.rustdoc(compiler)); | |
103 | cmd.arg("--src-base").arg(build.src.join("src/test").join(suite)); | |
104 | cmd.arg("--build-base").arg(testdir(build, compiler.host).join(suite)); | |
105 | cmd.arg("--stage-id").arg(format!("stage{}-{}", compiler.stage, target)); | |
106 | cmd.arg("--mode").arg(mode); | |
107 | cmd.arg("--target").arg(target); | |
108 | cmd.arg("--host").arg(compiler.host); | |
109 | cmd.arg("--llvm-filecheck").arg(build.llvm_filecheck(&build.config.build)); | |
110 | ||
3157f602 | 111 | let mut flags = vec!["-Crpath".to_string()]; |
a7813a04 | 112 | if build.config.rust_optimize_tests { |
3157f602 | 113 | flags.push("-O".to_string()); |
a7813a04 XL |
114 | } |
115 | if build.config.rust_debuginfo_tests { | |
3157f602 | 116 | flags.push("-g".to_string()); |
a7813a04 XL |
117 | } |
118 | ||
3157f602 XL |
119 | let mut hostflags = build.rustc_flags(&compiler.host); |
120 | hostflags.extend(flags.clone()); | |
121 | cmd.arg("--host-rustcflags").arg(hostflags.join(" ")); | |
a7813a04 | 122 | |
3157f602 XL |
123 | let mut targetflags = build.rustc_flags(&target); |
124 | targetflags.extend(flags); | |
125 | targetflags.push(format!("-Lnative={}", | |
126 | build.test_helpers_out(target).display())); | |
127 | cmd.arg("--target-rustcflags").arg(targetflags.join(" ")); | |
a7813a04 XL |
128 | |
129 | // FIXME: CFG_PYTHON should probably be detected more robustly elsewhere | |
130 | let python_default = "python"; | |
131 | cmd.arg("--docck-python").arg(python_default); | |
132 | ||
133 | if build.config.build.ends_with("apple-darwin") { | |
134 | // Force /usr/bin/python on OSX for LLDB tests because we're loading the | |
135 | // LLDB plugin's compiled module which only works with the system python | |
136 | // (namely not Homebrew-installed python) | |
137 | cmd.arg("--lldb-python").arg("/usr/bin/python"); | |
138 | } else { | |
139 | cmd.arg("--lldb-python").arg(python_default); | |
140 | } | |
141 | ||
142 | if let Some(ref vers) = build.gdb_version { | |
143 | cmd.arg("--gdb-version").arg(vers); | |
144 | } | |
145 | if let Some(ref vers) = build.lldb_version { | |
146 | cmd.arg("--lldb-version").arg(vers); | |
147 | } | |
148 | if let Some(ref dir) = build.lldb_python_dir { | |
149 | cmd.arg("--lldb-python-dir").arg(dir); | |
150 | } | |
9e0c209e SL |
151 | let llvm_config = build.llvm_config(target); |
152 | let llvm_version = output(Command::new(&llvm_config).arg("--version")); | |
153 | cmd.arg("--llvm-version").arg(llvm_version); | |
a7813a04 XL |
154 | |
155 | cmd.args(&build.flags.args); | |
156 | ||
157 | if build.config.verbose || build.flags.verbose { | |
158 | cmd.arg("--verbose"); | |
159 | } | |
160 | ||
161 | // Only pass correct values for these flags for the `run-make` suite as it | |
162 | // requires that a C++ compiler was configured which isn't always the case. | |
163 | if suite == "run-make" { | |
a7813a04 XL |
164 | let llvm_components = output(Command::new(&llvm_config).arg("--components")); |
165 | let llvm_cxxflags = output(Command::new(&llvm_config).arg("--cxxflags")); | |
166 | cmd.arg("--cc").arg(build.cc(target)) | |
167 | .arg("--cxx").arg(build.cxx(target)) | |
168 | .arg("--cflags").arg(build.cflags(target).join(" ")) | |
169 | .arg("--llvm-components").arg(llvm_components.trim()) | |
170 | .arg("--llvm-cxxflags").arg(llvm_cxxflags.trim()); | |
171 | } else { | |
172 | cmd.arg("--cc").arg("") | |
173 | .arg("--cxx").arg("") | |
174 | .arg("--cflags").arg("") | |
175 | .arg("--llvm-components").arg("") | |
176 | .arg("--llvm-cxxflags").arg(""); | |
177 | } | |
178 | ||
179 | // Running a C compiler on MSVC requires a few env vars to be set, to be | |
180 | // sure to set them here. | |
181 | if target.contains("msvc") { | |
182 | for &(ref k, ref v) in build.cc[target].0.env() { | |
183 | if k != "PATH" { | |
184 | cmd.env(k, v); | |
185 | } | |
186 | } | |
187 | } | |
9e0c209e | 188 | build.add_bootstrap_key(&mut cmd); |
a7813a04 | 189 | |
3157f602 XL |
190 | cmd.arg("--adb-path").arg("adb"); |
191 | cmd.arg("--adb-test-dir").arg(ADB_TEST_DIR); | |
192 | if target.contains("android") { | |
193 | // Assume that cc for this target comes from the android sysroot | |
194 | cmd.arg("--android-cross-path") | |
195 | .arg(build.cc(target).parent().unwrap().parent().unwrap()); | |
196 | } else { | |
197 | cmd.arg("--android-cross-path").arg(""); | |
198 | } | |
199 | ||
a7813a04 XL |
200 | build.run(&mut cmd); |
201 | } | |
202 | ||
203 | /// Run `rustdoc --test` for all documentation in `src/doc`. | |
204 | /// | |
205 | /// This will run all tests in our markdown documentation (e.g. the book) | |
206 | /// located in `src/doc`. The `rustdoc` that's run is the one that sits next to | |
207 | /// `compiler`. | |
208 | pub fn docs(build: &Build, compiler: &Compiler) { | |
209 | // Do a breadth-first traversal of the `src/doc` directory and just run | |
210 | // tests for all files that end in `*.md` | |
211 | let mut stack = vec![build.src.join("src/doc")]; | |
212 | ||
213 | while let Some(p) = stack.pop() { | |
214 | if p.is_dir() { | |
215 | stack.extend(t!(p.read_dir()).map(|p| t!(p).path())); | |
216 | continue | |
217 | } | |
218 | ||
219 | if p.extension().and_then(|s| s.to_str()) != Some("md") { | |
220 | continue | |
221 | } | |
222 | ||
223 | println!("doc tests for: {}", p.display()); | |
224 | markdown_test(build, compiler, &p); | |
225 | } | |
226 | } | |
227 | ||
228 | /// Run the error index generator tool to execute the tests located in the error | |
229 | /// index. | |
230 | /// | |
231 | /// The `error_index_generator` tool lives in `src/tools` and is used to | |
232 | /// generate a markdown file from the error indexes of the code base which is | |
233 | /// then passed to `rustdoc --test`. | |
234 | pub fn error_index(build: &Build, compiler: &Compiler) { | |
235 | println!("Testing error-index stage{}", compiler.stage); | |
236 | ||
237 | let output = testdir(build, compiler.host).join("error-index.md"); | |
238 | build.run(build.tool_cmd(compiler, "error_index_generator") | |
239 | .arg("markdown") | |
240 | .arg(&output) | |
241 | .env("CFG_BUILD", &build.config.build)); | |
242 | ||
243 | markdown_test(build, compiler, &output); | |
244 | } | |
245 | ||
246 | fn markdown_test(build: &Build, compiler: &Compiler, markdown: &Path) { | |
247 | let mut cmd = Command::new(build.rustdoc(compiler)); | |
248 | build.add_rustc_lib_path(compiler, &mut cmd); | |
249 | cmd.arg("--test"); | |
250 | cmd.arg(markdown); | |
251 | cmd.arg("--test-args").arg(build.flags.args.join(" ")); | |
252 | build.run(&mut cmd); | |
253 | } | |
254 | ||
255 | /// Run all unit tests plus documentation tests for an entire crate DAG defined | |
256 | /// by a `Cargo.toml` | |
257 | /// | |
258 | /// This is what runs tests for crates like the standard library, compiler, etc. | |
259 | /// It essentially is the driver for running `cargo test`. | |
260 | /// | |
261 | /// Currently this runs all tests for a DAG by passing a bunch of `-p foo` | |
262 | /// arguments, and those arguments are discovered from `Cargo.lock`. | |
263 | pub fn krate(build: &Build, | |
264 | compiler: &Compiler, | |
265 | target: &str, | |
266 | mode: Mode) { | |
267 | let (name, path, features) = match mode { | |
268 | Mode::Libstd => ("libstd", "src/rustc/std_shim", build.std_features()), | |
269 | Mode::Libtest => ("libtest", "src/rustc/test_shim", String::new()), | |
270 | Mode::Librustc => ("librustc", "src/rustc", build.rustc_features()), | |
271 | _ => panic!("can only test libraries"), | |
272 | }; | |
273 | println!("Testing {} stage{} ({} -> {})", name, compiler.stage, | |
274 | compiler.host, target); | |
275 | ||
276 | // Build up the base `cargo test` command. | |
277 | let mut cargo = build.cargo(compiler, mode, target, "test"); | |
278 | cargo.arg("--manifest-path") | |
279 | .arg(build.src.join(path).join("Cargo.toml")) | |
280 | .arg("--features").arg(features); | |
281 | ||
282 | // Generate a list of `-p` arguments to pass to the `cargo test` invocation | |
283 | // by crawling the corresponding Cargo.lock file. | |
284 | let lockfile = build.src.join(path).join("Cargo.lock"); | |
285 | let mut contents = String::new(); | |
286 | t!(t!(File::open(&lockfile)).read_to_string(&mut contents)); | |
287 | let mut lines = contents.lines(); | |
288 | while let Some(line) = lines.next() { | |
289 | let prefix = "name = \""; | |
290 | if !line.starts_with(prefix) { | |
291 | continue | |
292 | } | |
293 | lines.next(); // skip `version = ...` | |
294 | ||
295 | // skip crates.io or otherwise non-path crates | |
296 | if let Some(line) = lines.next() { | |
297 | if line.starts_with("source") { | |
298 | continue | |
299 | } | |
300 | } | |
301 | ||
302 | let crate_name = &line[prefix.len()..line.len() - 1]; | |
303 | ||
304 | // Right now jemalloc is our only target-specific crate in the sense | |
305 | // that it's not present on all platforms. Custom skip it here for now, | |
306 | // but if we add more this probably wants to get more generalized. | |
307 | if crate_name.contains("jemalloc") { | |
308 | continue | |
309 | } | |
310 | ||
311 | cargo.arg("-p").arg(crate_name); | |
312 | } | |
313 | ||
314 | // The tests are going to run with the *target* libraries, so we need to | |
315 | // ensure that those libraries show up in the LD_LIBRARY_PATH equivalent. | |
316 | // | |
317 | // Note that to run the compiler we need to run with the *host* libraries, | |
318 | // but our wrapper scripts arrange for that to be the case anyway. | |
319 | let mut dylib_path = dylib_path(); | |
320 | dylib_path.insert(0, build.sysroot_libdir(compiler, target)); | |
321 | cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap()); | |
a7813a04 | 322 | |
3157f602 XL |
323 | if target.contains("android") { |
324 | build.run(cargo.arg("--no-run")); | |
325 | krate_android(build, compiler, target, mode); | |
326 | } else { | |
327 | cargo.args(&build.flags.args); | |
328 | build.run(&mut cargo); | |
329 | } | |
330 | } | |
331 | ||
332 | fn krate_android(build: &Build, | |
333 | compiler: &Compiler, | |
334 | target: &str, | |
335 | mode: Mode) { | |
336 | let mut tests = Vec::new(); | |
337 | let out_dir = build.cargo_out(compiler, mode, target); | |
338 | find_tests(&out_dir, target, &mut tests); | |
339 | find_tests(&out_dir.join("deps"), target, &mut tests); | |
340 | ||
341 | for test in tests { | |
342 | build.run(Command::new("adb").arg("push").arg(&test).arg(ADB_TEST_DIR)); | |
343 | ||
344 | let test_file_name = test.file_name().unwrap().to_string_lossy(); | |
345 | let log = format!("{}/check-stage{}-T-{}-H-{}-{}.log", | |
346 | ADB_TEST_DIR, | |
347 | compiler.stage, | |
348 | target, | |
349 | compiler.host, | |
350 | test_file_name); | |
351 | let program = format!("(cd {dir}; \ | |
352 | LD_LIBRARY_PATH=./{target} ./{test} \ | |
353 | --logfile {log} \ | |
354 | {args})", | |
355 | dir = ADB_TEST_DIR, | |
356 | target = target, | |
357 | test = test_file_name, | |
358 | log = log, | |
359 | args = build.flags.args.join(" ")); | |
360 | ||
361 | let output = output(Command::new("adb").arg("shell").arg(&program)); | |
362 | println!("{}", output); | |
363 | build.run(Command::new("adb") | |
364 | .arg("pull") | |
365 | .arg(&log) | |
366 | .arg(build.out.join("tmp"))); | |
367 | build.run(Command::new("adb").arg("shell").arg("rm").arg(&log)); | |
368 | if !output.contains("result: ok") { | |
369 | panic!("some tests failed"); | |
370 | } | |
371 | } | |
372 | } | |
373 | ||
374 | fn find_tests(dir: &Path, | |
375 | target: &str, | |
376 | dst: &mut Vec<PathBuf>) { | |
377 | for e in t!(dir.read_dir()).map(|e| t!(e)) { | |
378 | let file_type = t!(e.file_type()); | |
379 | if !file_type.is_file() { | |
380 | continue | |
381 | } | |
382 | let filename = e.file_name().into_string().unwrap(); | |
383 | if (target.contains("windows") && filename.ends_with(".exe")) || | |
384 | (!target.contains("windows") && !filename.contains(".")) { | |
385 | dst.push(e.path()); | |
386 | } | |
387 | } | |
388 | } | |
389 | ||
390 | pub fn android_copy_libs(build: &Build, | |
391 | compiler: &Compiler, | |
392 | target: &str) { | |
393 | println!("Android copy libs to emulator ({})", target); | |
394 | build.run(Command::new("adb").arg("remount")); | |
395 | build.run(Command::new("adb").args(&["shell", "rm", "-r", ADB_TEST_DIR])); | |
396 | build.run(Command::new("adb").args(&["shell", "mkdir", ADB_TEST_DIR])); | |
397 | build.run(Command::new("adb") | |
398 | .arg("push") | |
399 | .arg(build.src.join("src/etc/adb_run_wrapper.sh")) | |
400 | .arg(ADB_TEST_DIR)); | |
401 | ||
402 | let target_dir = format!("{}/{}", ADB_TEST_DIR, target); | |
403 | build.run(Command::new("adb").args(&["shell", "mkdir", &target_dir[..]])); | |
404 | ||
405 | for f in t!(build.sysroot_libdir(compiler, target).read_dir()) { | |
406 | let f = t!(f); | |
407 | let name = f.file_name().into_string().unwrap(); | |
408 | if util::is_dylib(&name) { | |
409 | build.run(Command::new("adb") | |
410 | .arg("push") | |
411 | .arg(f.path()) | |
412 | .arg(&target_dir)); | |
413 | } | |
414 | } | |
54a0048b | 415 | } |