test.rs 62.3 KB
Newer Older
1
//! Implementation of the test-related targets of the build system.
2 3 4 5
//!
//! This file implements the various regression test suites that we execute on
//! our CI.

6
use std::env;
7
use std::ffi::OsString;
U
Ulrik Sverdrup 已提交
8
use std::fmt;
9
use std::fs;
10
use std::iter;
S
Santiago Pastorino 已提交
11 12
use std::path::{Path, PathBuf};
use std::process::Command;
13

14
use build_helper::{self, output, t};
15

L
ljedrz 已提交
16 17 18 19 20 21
use crate::builder::{Builder, Compiler, Kind, RunConfig, ShouldRun, Step};
use crate::cache::{Interned, INTERNER};
use crate::compile;
use crate::dist;
use crate::flags::Subcommand;
use crate::native;
M
Mark Rousskov 已提交
22
use crate::tool::{self, SourceType, Tool};
L
ljedrz 已提交
23 24 25
use crate::toolstate::ToolState;
use crate::util::{self, dylib_path, dylib_path_var};
use crate::Crate as CargoCrate;
M
Mark Rousskov 已提交
26
use crate::{envify, DocTests, GitRepo, Mode};
27

M
Mark Simulacrum 已提交
28
const ADB_TEST_DIR: &str = "/data/tmp/work";
29

U
Ulrik Sverdrup 已提交
30
/// The two modes of the test runner; tests or benchmarks.
K
kennytm 已提交
31
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, PartialOrd, Ord)]
U
Ulrik Sverdrup 已提交
32
pub enum TestKind {
A
Alexander Regueiro 已提交
33
    /// Run `cargo test`.
U
Ulrik Sverdrup 已提交
34
    Test,
A
Alexander Regueiro 已提交
35
    /// Run `cargo bench`.
U
Ulrik Sverdrup 已提交
36 37 38
    Bench,
}

39 40 41 42 43
impl From<Kind> for TestKind {
    fn from(kind: Kind) -> Self {
        match kind {
            Kind::Test => TestKind::Test,
            Kind::Bench => TestKind::Bench,
S
Santiago Pastorino 已提交
44
            _ => panic!("unexpected kind in crate: {:?}", kind),
45 46 47 48
        }
    }
}

U
Ulrik Sverdrup 已提交
49 50 51 52 53 54 55 56 57 58 59
impl TestKind {
    // Return the cargo subcommand for this test kind
    fn subcommand(self) -> &'static str {
        match self {
            TestKind::Test => "test",
            TestKind::Bench => "bench",
        }
    }
}

impl fmt::Display for TestKind {
T
Taiki Endo 已提交
60
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
U
Ulrik Sverdrup 已提交
61 62 63 64 65 66 67
        f.write_str(match *self {
            TestKind::Test => "Testing",
            TestKind::Bench => "Benchmarking",
        })
    }
}

T
Taiki Endo 已提交
68
fn try_run(builder: &Builder<'_>, cmd: &mut Command) -> bool {
69 70 71
    if !builder.fail_fast {
        if !builder.try_run(cmd) {
            let mut failures = builder.delayed_failures.borrow_mut();
72
            failures.push(format!("{:?}", cmd));
O
Oliver Schneider 已提交
73
            return false;
74 75
        }
    } else {
76
        builder.run(cmd);
77
    }
O
Oliver Schneider 已提交
78
    true
79 80
}

T
Taiki Endo 已提交
81
fn try_run_quiet(builder: &Builder<'_>, cmd: &mut Command) -> bool {
82 83 84
    if !builder.fail_fast {
        if !builder.try_run_quiet(cmd) {
            let mut failures = builder.delayed_failures.borrow_mut();
85
            failures.push(format!("{:?}", cmd));
86
            return false;
87 88
        }
    } else {
89
        builder.run_quiet(cmd);
90
    }
91
    true
92 93
}

94 95 96
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Linkcheck {
    host: Interned<String>,
97 98
}

99
impl Step for Linkcheck {
100
    type Output = ();
101 102
    const ONLY_HOSTS: bool = true;
    const DEFAULT: bool = true;
103 104 105 106 107

    /// Runs the `linkchecker` tool as compiled in `stage` by the `host` compiler.
    ///
    /// This tool in `src/tools` will verify the validity of all our links in the
    /// documentation to ensure we don't have a bunch of dead ones.
T
Taiki Endo 已提交
108
    fn run(self, builder: &Builder<'_>) {
109 110
        let host = self.host;

111
        builder.info(&format!("Linkcheck ({})", host));
112 113

        builder.default_doc(None);
114

115
        let _time = util::timeit(&builder);
S
Santiago Pastorino 已提交
116 117
        try_run(
            builder,
M
Mark Rousskov 已提交
118
            builder.tool_cmd(Tool::Linkchecker).arg(builder.out.join(host).join("doc")),
S
Santiago Pastorino 已提交
119
        );
120
    }
121

T
Taiki Endo 已提交
122
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
123
        let builder = run.builder;
M
Mark Rousskov 已提交
124
        run.path("src/tools/linkchecker").default_condition(builder.config.docs)
125 126
    }

T
Taiki Endo 已提交
127
    fn make_run(run: RunConfig<'_>) {
128
        run.builder.ensure(Linkcheck { host: run.target });
129
    }
130
}
131

132 133
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Cargotest {
134
    stage: u32,
135
    host: Interned<String>,
136
}
137

138
impl Step for Cargotest {
139
    type Output = ();
140
    const ONLY_HOSTS: bool = true;
141

T
Taiki Endo 已提交
142
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
143
        run.path("src/tools/cargotest")
144 145
    }

T
Taiki Endo 已提交
146
    fn make_run(run: RunConfig<'_>) {
M
Mark Rousskov 已提交
147
        run.builder.ensure(Cargotest { stage: run.builder.top_stage, host: run.target });
148 149
    }

150 151 152 153
    /// Runs the `cargotest` tool as compiled in `stage` by the `host` compiler.
    ///
    /// This tool in `src/tools` will check out a few Rust projects and run `cargo
    /// test` to ensure that we don't regress the test suites there.
T
Taiki Endo 已提交
154
    fn run(self, builder: &Builder<'_>) {
155
        let compiler = builder.compiler(self.stage, self.host);
M
Mark Rousskov 已提交
156
        builder.ensure(compile::Rustc { compiler, target: compiler.host });
157 158 159 160

        // Note that this is a short, cryptic, and not scoped directory name. This
        // is currently to minimize the length of path on Windows where we otherwise
        // quickly run into path name limit constraints.
161
        let out_dir = builder.out.join("ct");
162 163
        t!(fs::create_dir_all(&out_dir));

164
        let _time = util::timeit(&builder);
165
        let mut cmd = builder.tool_cmd(Tool::CargoTest);
S
Santiago Pastorino 已提交
166 167 168 169 170
        try_run(
            builder,
            cmd.arg(&builder.initial_cargo)
                .arg(&out_dir)
                .env("RUSTC", builder.rustc(compiler))
M
Mark Rousskov 已提交
171
                .env("RUSTDOC", builder.rustdoc(compiler)),
S
Santiago Pastorino 已提交
172
        );
173
    }
174 175
}

176 177
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Cargo {
178
    stage: u32,
179
    host: Interned<String>,
180 181
}

182
impl Step for Cargo {
183
    type Output = ();
184 185
    const ONLY_HOSTS: bool = true;

T
Taiki Endo 已提交
186
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
187
        run.path("src/tools/cargo")
188 189
    }

T
Taiki Endo 已提交
190
    fn make_run(run: RunConfig<'_>) {
M
Mark Rousskov 已提交
191
        run.builder.ensure(Cargo { stage: run.builder.top_stage, host: run.target });
192
    }
193 194

    /// Runs `cargo test` for `cargo` packaged with Rust.
195 196
    fn run(self, builder: &Builder<'_>) {
        let compiler = builder.compiler(self.stage, self.host);
197

M
Mark Rousskov 已提交
198 199 200
        builder.ensure(tool::Cargo { compiler, target: self.host });
        let mut cargo = tool::prepare_tool_cargo(
            builder,
S
Santiago Pastorino 已提交
201
            compiler,
M
Mark Rousskov 已提交
202 203 204 205 206 207 208
            Mode::ToolRustc,
            self.host,
            "test",
            "src/tools/cargo",
            SourceType::Submodule,
            &[],
        );
209

210
        if !builder.fail_fast {
211 212
            cargo.arg("--no-fail-fast");
        }
213

214 215 216
        // Don't run cross-compile tests, we may not have cross-compiled libstd libs
        // available.
        cargo.env("CFG_DISABLE_CROSS_TESTS", "1");
217 218
        // Disable a test that has issues with mingw.
        cargo.env("CARGO_TEST_DISABLE_GIT_CLI", "1");
219 220 221
        // Forcibly disable tests using nightly features since any changes to
        // those features won't be able to land.
        cargo.env("CARGO_TEST_DISABLE_NIGHTLY", "1");
222

223 224
        cargo.env("PATH", &path_for_cargo(builder, compiler));

225
        try_run(builder, &mut cargo.into());
226
    }
N
Nick Cameron 已提交
227 228
}

M
Mark Simulacrum 已提交
229 230
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Rls {
231
    stage: u32,
M
Mark Simulacrum 已提交
232
    host: Interned<String>,
233
}
N
Nick Cameron 已提交
234

M
Mark Simulacrum 已提交
235
impl Step for Rls {
236
    type Output = ();
M
Mark Simulacrum 已提交
237 238
    const ONLY_HOSTS: bool = true;

T
Taiki Endo 已提交
239
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
240
        run.path("src/tools/rls")
M
Mark Simulacrum 已提交
241 242
    }

T
Taiki Endo 已提交
243
    fn make_run(run: RunConfig<'_>) {
M
Mark Rousskov 已提交
244
        run.builder.ensure(Rls { stage: run.builder.top_stage, host: run.target });
M
Mark Simulacrum 已提交
245
    }
N
Nick Cameron 已提交
246

247
    /// Runs `cargo test` for the rls.
T
Taiki Endo 已提交
248
    fn run(self, builder: &Builder<'_>) {
249 250
        let stage = self.stage;
        let host = self.host;
M
Mark Simulacrum 已提交
251
        let compiler = builder.compiler(stage, host);
N
Nick Cameron 已提交
252

M
Mark Rousskov 已提交
253 254
        let build_result =
            builder.ensure(tool::Rls { compiler, target: self.host, extra_features: Vec::new() });
255 256 257 258 259
        if build_result.is_none() {
            eprintln!("failed to test rls: could not build");
            return;
        }

M
Mark Rousskov 已提交
260 261 262 263 264 265 266 267 268 269
        let mut cargo = tool::prepare_tool_cargo(
            builder,
            compiler,
            Mode::ToolRustc,
            host,
            "test",
            "src/tools/rls",
            SourceType::Submodule,
            &[],
        );
N
Nick Cameron 已提交
270

M
Mark Simulacrum 已提交
271
        builder.add_rustc_lib_path(compiler, &mut cargo);
M
Mark Rousskov 已提交
272
        cargo.arg("--").args(builder.config.cmd.test_args());
273

274
        if try_run(builder, &mut cargo.into()) {
275
            builder.save_toolstate("rls", ToolState::TestPass);
O
Oliver Schneider 已提交
276
        }
277
    }
N
Nick Cameron 已提交
278 279
}

N
Nick Cameron 已提交
280 281 282 283 284 285 286 287 288 289
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Rustfmt {
    stage: u32,
    host: Interned<String>,
}

impl Step for Rustfmt {
    type Output = ();
    const ONLY_HOSTS: bool = true;

T
Taiki Endo 已提交
290
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
N
Nick Cameron 已提交
291 292 293
        run.path("src/tools/rustfmt")
    }

T
Taiki Endo 已提交
294
    fn make_run(run: RunConfig<'_>) {
M
Mark Rousskov 已提交
295
        run.builder.ensure(Rustfmt { stage: run.builder.top_stage, host: run.target });
N
Nick Cameron 已提交
296 297 298
    }

    /// Runs `cargo test` for rustfmt.
T
Taiki Endo 已提交
299
    fn run(self, builder: &Builder<'_>) {
N
Nick Cameron 已提交
300 301 302 303
        let stage = self.stage;
        let host = self.host;
        let compiler = builder.compiler(stage, host);

304 305 306 307 308 309 310 311 312 313
        let build_result = builder.ensure(tool::Rustfmt {
            compiler,
            target: self.host,
            extra_features: Vec::new(),
        });
        if build_result.is_none() {
            eprintln!("failed to test rustfmt: could not build");
            return;
        }

M
Mark Rousskov 已提交
314 315 316 317 318 319 320 321 322 323
        let mut cargo = tool::prepare_tool_cargo(
            builder,
            compiler,
            Mode::ToolRustc,
            host,
            "test",
            "src/tools/rustfmt",
            SourceType::Submodule,
            &[],
        );
N
Nick Cameron 已提交
324

N
Nick Cameron 已提交
325 326 327
        let dir = testdir(builder, compiler.host);
        t!(fs::create_dir_all(&dir));
        cargo.env("RUSTFMT_TEST_DIR", dir);
N
Nick Cameron 已提交
328 329 330

        builder.add_rustc_lib_path(compiler, &mut cargo);

331
        if try_run(builder, &mut cargo.into()) {
332
            builder.save_toolstate("rustfmt", ToolState::TestPass);
O
Oliver Schneider 已提交
333
        }
N
Nick Cameron 已提交
334 335
    }
}
O
Oliver Schneider 已提交
336 337

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
338
pub struct Miri {
O
Oliver Schneider 已提交
339
    stage: u32,
340 341 342 343 344 345 346
    host: Interned<String>,
}

impl Step for Miri {
    type Output = ();
    const ONLY_HOSTS: bool = true;

T
Taiki Endo 已提交
347
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
R
Ralf Jung 已提交
348
        run.path("src/tools/miri")
349 350
    }

T
Taiki Endo 已提交
351
    fn make_run(run: RunConfig<'_>) {
M
Mark Rousskov 已提交
352
        run.builder.ensure(Miri { stage: run.builder.top_stage, host: run.target });
353 354 355
    }

    /// Runs `cargo test` for miri.
T
Taiki Endo 已提交
356
    fn run(self, builder: &Builder<'_>) {
O
Oliver Schneider 已提交
357
        let stage = self.stage;
358
        let host = self.host;
O
Oliver Schneider 已提交
359
        let compiler = builder.compiler(stage, host);
360

M
Mark Rousskov 已提交
361 362
        let miri =
            builder.ensure(tool::Miri { compiler, target: self.host, extra_features: Vec::new() });
363
        if let Some(miri) = miri {
R
Ralf Jung 已提交
364
            let mut cargo = builder.cargo(compiler, Mode::ToolRustc, host, "install");
365 366 367 368 369 370 371 372 373
            cargo.arg("xargo");
            // Configure `cargo install` path. cargo adds a `bin/`.
            cargo.env("CARGO_INSTALL_ROOT", &builder.out);

            let mut cargo = Command::from(cargo);
            if !try_run(builder, &mut cargo) {
                return;
            }

374 375 376 377 378 379 380 381 382 383 384
            // # Run `cargo miri setup`.
            let mut cargo = tool::prepare_tool_cargo(
                builder,
                compiler,
                Mode::ToolRustc,
                host,
                "run",
                "src/tools/miri",
                SourceType::Submodule,
                &[],
            );
M
Mark Rousskov 已提交
385
            cargo.arg("--bin").arg("cargo-miri").arg("--").arg("miri").arg("setup");
386 387 388

            // Tell `cargo miri setup` where to find the sources.
            cargo.env("XARGO_RUST_SRC", builder.src.join("src"));
389 390
            // Debug things.
            cargo.env("RUST_BACKTRACE", "1");
391
            // Let cargo-miri know where xargo ended up.
392
            cargo.env("XARGO", builder.out.join("bin").join("xargo"));
393

394
            let mut cargo = Command::from(cargo);
395 396 397 398 399 400 401 402 403
            if !try_run(builder, &mut cargo) {
                return;
            }

            // # Determine where Miri put its sysroot.
            // To this end, we run `cargo miri setup --env` and capture the output.
            // (We do this separately from the above so that when the setup actually
            // happens we get some output.)
            // We re-use the `cargo` from above.
R
Ralf Jung 已提交
404
            cargo.arg("--print-sysroot");
405 406 407 408 409

            // FIXME: Is there a way in which we can re-use the usual `run` helpers?
            let miri_sysroot = if builder.config.dry_run {
                String::new()
            } else {
410
                builder.verbose(&format!("running: {:?}", cargo));
M
Mark Rousskov 已提交
411 412
                let out = cargo
                    .output()
413 414
                    .expect("We already ran `cargo miri setup` before and that worked");
                assert!(out.status.success(), "`cargo miri setup` returned with non-0 exit code");
R
Ralf Jung 已提交
415
                // Output is "<sysroot>\n".
416 417
                let stdout = String::from_utf8(out.stdout)
                    .expect("`cargo miri setup` stdout is not valid UTF-8");
R
Ralf Jung 已提交
418 419
                let sysroot = stdout.trim_end();
                builder.verbose(&format!("`cargo miri setup --print-sysroot` said: {:?}", sysroot));
420 421 422 423 424 425 426 427 428 429 430 431 432 433
                sysroot.to_owned()
            };

            // # Run `cargo test`.
            let mut cargo = tool::prepare_tool_cargo(
                builder,
                compiler,
                Mode::ToolRustc,
                host,
                "test",
                "src/tools/miri",
                SourceType::Submodule,
                &[],
            );
O
Oliver Schneider 已提交
434 435

            // miri tests need to know about the stage sysroot
436
            cargo.env("MIRI_SYSROOT", miri_sysroot);
O
Oliver Schneider 已提交
437 438 439 440 441 442
            cargo.env("RUSTC_TEST_SUITE", builder.rustc(compiler));
            cargo.env("RUSTC_LIB_PATH", builder.rustc_libdir(compiler));
            cargo.env("MIRI_PATH", miri);

            builder.add_rustc_lib_path(compiler, &mut cargo);

443
            if !try_run(builder, &mut cargo.into()) {
444
                return;
O
Oliver Schneider 已提交
445
            }
446 447 448

            // # Done!
            builder.save_toolstate("miri", ToolState::TestPass);
O
Oliver Schneider 已提交
449 450 451
        } else {
            eprintln!("failed to test miri: could not build");
        }
452 453 454
    }
}

455 456 457 458 459 460 461 462
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct CompiletestTest {
    host: Interned<String>,
}

impl Step for CompiletestTest {
    type Output = ();

T
Taiki Endo 已提交
463
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
464 465 466
        run.path("src/tools/compiletest")
    }

T
Taiki Endo 已提交
467
    fn make_run(run: RunConfig<'_>) {
M
Mark Rousskov 已提交
468
        run.builder.ensure(CompiletestTest { host: run.target });
469 470 471
    }

    /// Runs `cargo test` for compiletest.
T
Taiki Endo 已提交
472
    fn run(self, builder: &Builder<'_>) {
473
        let host = self.host;
474
        let compiler = builder.compiler(0, host);
475

M
Mark Rousskov 已提交
476 477 478 479 480 481 482 483 484 485
        let cargo = tool::prepare_tool_cargo(
            builder,
            compiler,
            Mode::ToolBootstrap,
            host,
            "test",
            "src/tools/compiletest",
            SourceType::InTree,
            &[],
        );
486

487
        try_run(builder, &mut cargo.into());
488 489 490
    }
}

491 492
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Clippy {
O
Oliver Schneider 已提交
493
    stage: u32,
494 495 496 497 498 499 500 501
    host: Interned<String>,
}

impl Step for Clippy {
    type Output = ();
    const ONLY_HOSTS: bool = true;
    const DEFAULT: bool = false;

T
Taiki Endo 已提交
502
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
503 504 505
        run.path("src/tools/clippy")
    }

T
Taiki Endo 已提交
506
    fn make_run(run: RunConfig<'_>) {
M
Mark Rousskov 已提交
507
        run.builder.ensure(Clippy { stage: run.builder.top_stage, host: run.target });
508 509 510
    }

    /// Runs `cargo test` for clippy.
T
Taiki Endo 已提交
511
    fn run(self, builder: &Builder<'_>) {
O
Oliver Schneider 已提交
512
        let stage = self.stage;
513
        let host = self.host;
O
Oliver Schneider 已提交
514
        let compiler = builder.compiler(stage, host);
515

516 517 518 519 520 521
        let clippy = builder.ensure(tool::Clippy {
            compiler,
            target: self.host,
            extra_features: Vec::new(),
        });
        if let Some(clippy) = clippy {
M
Mark Rousskov 已提交
522 523 524 525 526 527 528 529 530 531
            let mut cargo = tool::prepare_tool_cargo(
                builder,
                compiler,
                Mode::ToolRustc,
                host,
                "test",
                "src/tools/clippy",
                SourceType::Submodule,
                &[],
            );
O
Oliver Schneider 已提交
532 533 534 535 536

            // clippy tests need to know about the stage sysroot
            cargo.env("SYSROOT", builder.sysroot(compiler));
            cargo.env("RUSTC_TEST_SUITE", builder.rustc(compiler));
            cargo.env("RUSTC_LIB_PATH", builder.rustc_libdir(compiler));
M
Mark Rousskov 已提交
537
            let host_libs = builder.stage_out(compiler, Mode::ToolRustc).join(builder.cargo_dir());
M
Manish Goregaokar 已提交
538 539 540 541
            let target_libs = builder
                .stage_out(compiler, Mode::ToolRustc)
                .join(&self.host)
                .join(builder.cargo_dir());
O
Oliver Schneider 已提交
542
            cargo.env("HOST_LIBS", host_libs);
M
Manish Goregaokar 已提交
543
            cargo.env("TARGET_LIBS", target_libs);
O
Oliver Schneider 已提交
544 545 546 547 548
            // clippy tests need to find the driver
            cargo.env("CLIPPY_DRIVER_PATH", clippy);

            builder.add_rustc_lib_path(compiler, &mut cargo);

549
            if try_run(builder, &mut cargo.into()) {
550
                builder.save_toolstate("clippy-driver", ToolState::TestPass);
O
Oliver Schneider 已提交
551 552 553 554
            }
        } else {
            eprintln!("failed to test clippy: could not build");
        }
555 556
    }
}
N
Nick Cameron 已提交
557

558 559 560 561 562 563 564 565
fn path_for_cargo(builder: &Builder<'_>, compiler: Compiler) -> OsString {
    // Configure PATH to find the right rustc. NB. we have to use PATH
    // and not RUSTC because the Cargo test suite has tests that will
    // fail if rustc is not spelled `rustc`.
    let path = builder.sysroot(compiler).join("bin");
    let old_path = env::var_os("PATH").unwrap_or_default();
    env::join_paths(iter::once(path).chain(env::split_paths(&old_path))).expect("")
}
566

G
Guillaume Gomez 已提交
567 568 569 570 571 572 573 574 575 576
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct RustdocTheme {
    pub compiler: Compiler,
}

impl Step for RustdocTheme {
    type Output = ();
    const DEFAULT: bool = true;
    const ONLY_HOSTS: bool = true;

T
Taiki Endo 已提交
577
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
G
Guillaume Gomez 已提交
578 579 580
        run.path("src/tools/rustdoc-themes")
    }

T
Taiki Endo 已提交
581
    fn make_run(run: RunConfig<'_>) {
G
Guillaume Gomez 已提交
582 583
        let compiler = run.builder.compiler(run.builder.top_stage, run.host);

584
        run.builder.ensure(RustdocTheme { compiler });
G
Guillaume Gomez 已提交
585 586
    }

T
Taiki Endo 已提交
587
    fn run(self, builder: &Builder<'_>) {
588
        let rustdoc = builder.out.join("bootstrap/debug/rustdoc");
G
Guillaume Gomez 已提交
589 590
        let mut cmd = builder.tool_cmd(Tool::RustdocTheme);
        cmd.arg(rustdoc.to_str().unwrap())
M
Mark Rousskov 已提交
591
            .arg(builder.src.join("src/librustdoc/html/static/themes").to_str().unwrap())
S
Santiago Pastorino 已提交
592 593
            .env("RUSTC_STAGE", self.compiler.stage.to_string())
            .env("RUSTC_SYSROOT", builder.sysroot(self.compiler))
M
Mark Rousskov 已提交
594
            .env("RUSTDOC_LIBDIR", builder.sysroot_libdir(self.compiler, self.compiler.host))
S
Santiago Pastorino 已提交
595
            .env("CFG_RELEASE_CHANNEL", &builder.config.channel)
M
Mark Rousskov 已提交
596
            .env("RUSTDOC_REAL", builder.rustdoc(self.compiler))
S
Santiago Pastorino 已提交
597 598
            .env("RUSTDOC_CRATE_VERSION", builder.rust_version())
            .env("RUSTC_BOOTSTRAP", "1");
599
        if let Some(linker) = builder.linker(self.compiler.host) {
G
Guillaume Gomez 已提交
600 601
            cmd.env("RUSTC_TARGET_LINKER", linker);
        }
602
        try_run(builder, &mut cmd);
G
Guillaume Gomez 已提交
603 604 605
    }
}

G
Guillaume Gomez 已提交
606
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
G
Guillaume Gomez 已提交
607
pub struct RustdocJSStd {
G
Guillaume Gomez 已提交
608
    pub host: Interned<String>,
609
    pub target: Interned<String>,
G
Guillaume Gomez 已提交
610 611
}

G
Guillaume Gomez 已提交
612
impl Step for RustdocJSStd {
613
    type Output = ();
G
Guillaume Gomez 已提交
614 615 616
    const DEFAULT: bool = true;
    const ONLY_HOSTS: bool = true;

T
Taiki Endo 已提交
617
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
G
Guillaume Gomez 已提交
618
        run.path("src/test/rustdoc-js-std")
G
Guillaume Gomez 已提交
619 620
    }

T
Taiki Endo 已提交
621
    fn make_run(run: RunConfig<'_>) {
M
Mark Rousskov 已提交
622
        run.builder.ensure(RustdocJSStd { host: run.host, target: run.target });
G
Guillaume Gomez 已提交
623 624
    }

T
Taiki Endo 已提交
625
    fn run(self, builder: &Builder<'_>) {
626 627
        if let Some(ref nodejs) = builder.config.nodejs {
            let mut command = Command::new(nodejs);
G
Guillaume Gomez 已提交
628
            command.args(&["src/tools/rustdoc-js-std/tester.js", &*self.host]);
M
Mark Rousskov 已提交
629
            builder.ensure(crate::doc::Std { target: self.target, stage: builder.top_stage });
630 631
            builder.run(&mut command);
        } else {
M
Mark Rousskov 已提交
632
            builder.info("No nodejs found, skipping \"src/test/rustdoc-js-std\" tests");
633
        }
G
Guillaume Gomez 已提交
634 635 636
    }
}

G
Guillaume Gomez 已提交
637 638 639 640 641 642 643 644 645 646 647 648
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct RustdocJSNotStd {
    pub host: Interned<String>,
    pub target: Interned<String>,
    pub compiler: Compiler,
}

impl Step for RustdocJSNotStd {
    type Output = ();
    const DEFAULT: bool = true;
    const ONLY_HOSTS: bool = true;

G
Guillaume Gomez 已提交
649
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
G
Guillaume Gomez 已提交
650
        run.path("src/test/rustdoc-js")
G
Guillaume Gomez 已提交
651 652
    }

G
Guillaume Gomez 已提交
653
    fn make_run(run: RunConfig<'_>) {
G
Guillaume Gomez 已提交
654
        let compiler = run.builder.compiler(run.builder.top_stage, run.host);
M
Mark Rousskov 已提交
655
        run.builder.ensure(RustdocJSNotStd { host: run.host, target: run.target, compiler });
G
Guillaume Gomez 已提交
656 657
    }

G
Guillaume Gomez 已提交
658
    fn run(self, builder: &Builder<'_>) {
659 660 661
        if builder.config.nodejs.is_some() {
            builder.ensure(Compiletest {
                compiler: self.compiler,
G
Guillaume Gomez 已提交
662
                target: self.target,
663 664 665 666
                mode: "js-doc-test",
                suite: "rustdoc-js",
                path: None,
                compare_mode: None,
G
Guillaume Gomez 已提交
667 668
            });
        } else {
M
Mark Rousskov 已提交
669
            builder.info("No nodejs found, skipping \"src/test/rustdoc-js\" tests");
G
Guillaume Gomez 已提交
670 671 672 673
        }
    }
}

674 675 676 677 678 679 680 681 682 683 684 685
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct RustdocUi {
    pub host: Interned<String>,
    pub target: Interned<String>,
    pub compiler: Compiler,
}

impl Step for RustdocUi {
    type Output = ();
    const DEFAULT: bool = true;
    const ONLY_HOSTS: bool = true;

T
Taiki Endo 已提交
686
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
687 688 689
        run.path("src/test/rustdoc-ui")
    }

T
Taiki Endo 已提交
690
    fn make_run(run: RunConfig<'_>) {
691
        let compiler = run.builder.compiler(run.builder.top_stage, run.host);
M
Mark Rousskov 已提交
692
        run.builder.ensure(RustdocUi { host: run.host, target: run.target, compiler });
693 694
    }

T
Taiki Endo 已提交
695
    fn run(self, builder: &Builder<'_>) {
696 697 698 699 700
        builder.ensure(Compiletest {
            compiler: self.compiler,
            target: self.target,
            mode: "ui",
            suite: "rustdoc-ui",
G
Guillaume Gomez 已提交
701
            path: Some("src/test/rustdoc-ui"),
702
            compare_mode: None,
703 704 705 706
        })
    }
}

707
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
M
Mark Simulacrum 已提交
708
pub struct Tidy;
709

710
impl Step for Tidy {
711
    type Output = ();
712 713
    const DEFAULT: bool = true;
    const ONLY_HOSTS: bool = true;
714

M
Mark Simulacrum 已提交
715
    /// Runs the `tidy` tool.
716 717 718 719
    ///
    /// This tool in `src/tools` checks up on various bits and pieces of style and
    /// otherwise just implements a few lint-like checks that are specific to the
    /// compiler itself.
720 721 722
    ///
    /// Once tidy passes, this step also runs `fmt --check` if tests are being run
    /// for the `dev` or `nightly` channels.
T
Taiki Endo 已提交
723
    fn run(self, builder: &Builder<'_>) {
724
        let mut cmd = builder.tool_cmd(Tool::Tidy);
725 726 727
        cmd.arg(builder.src.join("src"));
        cmd.arg(&builder.initial_cargo);
        if !builder.config.vendor {
728 729
            cmd.arg("--no-vendor");
        }
M
Mark Rousskov 已提交
730 731
        if builder.is_verbose() {
            cmd.arg("--verbose");
732
        }
733

734
        builder.info("tidy check");
735
        try_run(builder, &mut cmd);
736 737 738

        if builder.config.channel == "dev" || builder.config.channel == "nightly" {
            builder.info("fmt check");
739
            crate::format::format(&builder.build, !builder.config.cmd.bless());
740
        }
741
    }
742

T
Taiki Endo 已提交
743
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
744
        run.path("src/tools/tidy")
745 746
    }

T
Taiki Endo 已提交
747
    fn make_run(run: RunConfig<'_>) {
M
Mark Simulacrum 已提交
748
        run.builder.ensure(Tidy);
749
    }
750
}
751

T
Taiki Endo 已提交
752
fn testdir(builder: &Builder<'_>, host: Interned<String>) -> PathBuf {
753
    builder.out.join(host).join("test")
754 755
}

756 757 758
macro_rules! default_test {
    ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr }) => {
        test!($name { path: $path, mode: $mode, suite: $suite, default: true, host: false });
M
Mark Rousskov 已提交
759
    };
760 761
}

762 763 764
macro_rules! default_test_with_compare_mode {
    ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr,
                   compare_mode: $compare_mode:expr }) => {
M
Mark Rousskov 已提交
765 766 767 768 769 770 771 772 773
        test_with_compare_mode!($name {
            path: $path,
            mode: $mode,
            suite: $suite,
            default: true,
            host: false,
            compare_mode: $compare_mode
        });
    };
774 775
}

776 777 778
macro_rules! host_test {
    ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr }) => {
        test!($name { path: $path, mode: $mode, suite: $suite, default: true, host: true });
M
Mark Rousskov 已提交
779
    };
780 781
}

782
macro_rules! test {
783 784
    ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr, default: $default:expr,
                   host: $host:expr }) => {
M
Mark Rousskov 已提交
785 786 787 788 789 790 791 792 793
        test_definitions!($name {
            path: $path,
            mode: $mode,
            suite: $suite,
            default: $default,
            host: $host,
            compare_mode: None
        });
    };
794 795 796 797 798
}

macro_rules! test_with_compare_mode {
    ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr, default: $default:expr,
                   host: $host:expr, compare_mode: $compare_mode:expr }) => {
M
Mark Rousskov 已提交
799 800 801 802 803 804 805 806 807
        test_definitions!($name {
            path: $path,
            mode: $mode,
            suite: $suite,
            default: $default,
            host: $host,
            compare_mode: Some($compare_mode)
        });
    };
808 809 810
}

macro_rules! test_definitions {
811 812 813 814 815
    ($name:ident {
        path: $path:expr,
        mode: $mode:expr,
        suite: $suite:expr,
        default: $default:expr,
816 817
        host: $host:expr,
        compare_mode: $compare_mode:expr
818 819 820 821 822
    }) => {
        #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
        pub struct $name {
            pub compiler: Compiler,
            pub target: Interned<String>,
823 824
        }

825 826 827 828
        impl Step for $name {
            type Output = ();
            const DEFAULT: bool = $default;
            const ONLY_HOSTS: bool = $host;
829

T
Taiki Endo 已提交
830
            fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
831
                run.suite_path($path)
832
            }
833

T
Taiki Endo 已提交
834
            fn make_run(run: RunConfig<'_>) {
835
                let compiler = run.builder.compiler(run.builder.top_stage, run.host);
836

M
Mark Rousskov 已提交
837
                run.builder.ensure($name { compiler, target: run.target });
838
            }
839

T
Taiki Endo 已提交
840
            fn run(self, builder: &Builder<'_>) {
841 842 843 844 845
                builder.ensure(Compiletest {
                    compiler: self.compiler,
                    target: self.target,
                    mode: $mode,
                    suite: $suite,
846
                    path: Some($path),
847
                    compare_mode: $compare_mode,
848 849 850
                })
            }
        }
M
Mark Rousskov 已提交
851
    };
852 853
}

854
default_test_with_compare_mode!(Ui {
855 856
    path: "src/test/ui",
    mode: "ui",
857 858
    suite: "ui",
    compare_mode: "nll"
859 860 861 862 863 864 865 866
});

default_test!(CompileFail {
    path: "src/test/compile-fail",
    mode: "compile-fail",
    suite: "compile-fail"
});

M
Mark Rousskov 已提交
867
default_test!(RunFail { path: "src/test/run-fail", mode: "run-fail", suite: "run-fail" });
868 869 870 871 872 873 874

default_test!(RunPassValgrind {
    path: "src/test/run-pass-valgrind",
    mode: "run-pass-valgrind",
    suite: "run-pass-valgrind"
});

M
Mark Rousskov 已提交
875
default_test!(MirOpt { path: "src/test/mir-opt", mode: "mir-opt", suite: "mir-opt" });
876

M
Mark Rousskov 已提交
877
default_test!(Codegen { path: "src/test/codegen", mode: "codegen", suite: "codegen" });
878 879 880 881 882 883 884 885 886 887 888 889 890

default_test!(CodegenUnits {
    path: "src/test/codegen-units",
    mode: "codegen-units",
    suite: "codegen-units"
});

default_test!(Incremental {
    path: "src/test/incremental",
    mode: "incremental",
    suite: "incremental"
});

M
Mark Rousskov 已提交
891
default_test!(Debuginfo { path: "src/test/debuginfo", mode: "debuginfo", suite: "debuginfo" });
892

M
Mark Rousskov 已提交
893
host_test!(UiFullDeps { path: "src/test/ui-fulldeps", mode: "ui", suite: "ui-fulldeps" });
894

M
Mark Rousskov 已提交
895
host_test!(Rustdoc { path: "src/test/rustdoc", mode: "rustdoc", suite: "rustdoc" });
896

M
Mark Rousskov 已提交
897
host_test!(Pretty { path: "src/test/pretty", mode: "pretty", suite: "pretty" });
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
test!(RunFailPretty {
    path: "src/test/run-fail/pretty",
    mode: "pretty",
    suite: "run-fail",
    default: false,
    host: true
});
test!(RunPassValgrindPretty {
    path: "src/test/run-pass-valgrind/pretty",
    mode: "pretty",
    suite: "run-pass-valgrind",
    default: false,
    host: true
});

M
Mark Rousskov 已提交
913
default_test!(RunMake { path: "src/test/run-make", mode: "run-make", suite: "run-make" });
914

915 916 917 918 919 920
host_test!(RunMakeFullDeps {
    path: "src/test/run-make-fulldeps",
    mode: "run-make",
    suite: "run-make-fulldeps"
});

M
Mark Rousskov 已提交
921
default_test!(Assembly { path: "src/test/assembly", mode: "assembly", suite: "assembly" });
D
Denys Zariaiev 已提交
922

923 924 925 926 927 928
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
struct Compiletest {
    compiler: Compiler,
    target: Interned<String>,
    mode: &'static str,
    suite: &'static str,
929
    path: Option<&'static str>,
930
    compare_mode: Option<&'static str>,
931 932 933 934 935
}

impl Step for Compiletest {
    type Output = ();

T
Taiki Endo 已提交
936
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
937 938 939
        run.never()
    }

940 941 942 943 944
    /// Executes the `compiletest` tool to run a suite of tests.
    ///
    /// Compiles all tests with `compiler` for `target` with the specified
    /// compiletest `mode` and `suite` arguments. For example `mode` can be
    /// "run-pass" or `suite` can be something like `debuginfo`.
T
Taiki Endo 已提交
945
    fn run(self, builder: &Builder<'_>) {
946 947 948 949
        let compiler = self.compiler;
        let target = self.target;
        let mode = self.mode;
        let suite = self.suite;
950

951 952 953
        // Path for test suite
        let suite_path = self.path.unwrap_or("");

954
        // Skip codegen tests if they aren't enabled in configuration.
955
        if !builder.config.codegen_tests && suite == "codegen" {
956 957 958 959
            return;
        }

        if suite == "debuginfo" {
M
Mark Rousskov 已提交
960 961
            builder
                .ensure(dist::DebuggerScripts { sysroot: builder.sysroot(compiler), host: target });
962 963
        }

964
        if suite.ends_with("fulldeps") {
965 966 967
            builder.ensure(compile::Rustc { compiler, target });
        }

968 969 970
        builder.ensure(compile::Std { compiler, target });
        // ensure that `libproc_macro` is available on the host.
        builder.ensure(compile::Std { compiler, target: compiler.host });
971

972 973
        // Also provide `rust_test_helpers` for the host.
        builder.ensure(native::TestHelpers { target: compiler.host });
974

975 976
        // As well as the target, except for plain wasm32, which can't build it
        if !target.contains("wasm32") || target.contains("emscripten") {
977 978
            builder.ensure(native::TestHelpers { target });
        }
979

980
        builder.ensure(RemoteCopyLibs { compiler, target });
981 982

        let mut cmd = builder.tool_cmd(Tool::Compiletest);
983 984 985 986

        // compiletest currently has... a lot of arguments, so let's just pass all
        // of them!

M
Mark Rousskov 已提交
987 988
        cmd.arg("--compile-lib-path").arg(builder.rustc_libdir(compiler));
        cmd.arg("--run-lib-path").arg(builder.sysroot_libdir(compiler, target));
989
        cmd.arg("--rustc-path").arg(builder.rustc(compiler));
990

991
        let is_rustdoc = suite.ends_with("rustdoc-ui") || suite.ends_with("rustdoc-js");
G
Guillaume Gomez 已提交
992

993
        // Avoid depending on rustdoc when we don't need it.
S
Santiago Pastorino 已提交
994 995
        if mode == "rustdoc"
            || (mode == "run-make" && suite.ends_with("fulldeps"))
996 997
            || (mode == "ui" && is_rustdoc)
            || mode == "js-doc-test"
S
Santiago Pastorino 已提交
998
        {
M
Mark Rousskov 已提交
999
            cmd.arg("--rustdoc-path").arg(builder.rustdoc(compiler));
1000 1001
        }

M
Mark Rousskov 已提交
1002 1003 1004
        cmd.arg("--src-base").arg(builder.src.join("src/test").join(suite));
        cmd.arg("--build-base").arg(testdir(builder, compiler.host).join(suite));
        cmd.arg("--stage-id").arg(format!("stage{}-{}", compiler.stage, target));
1005 1006
        cmd.arg("--mode").arg(mode);
        cmd.arg("--target").arg(target);
1007
        cmd.arg("--host").arg(&*compiler.host);
M
Mark Rousskov 已提交
1008
        cmd.arg("--llvm-filecheck").arg(builder.llvm_filecheck(builder.config.build));
1009

1010
        if builder.config.cmd.bless() {
1011 1012 1013
            cmd.arg("--bless");
        }

M
Mark Rousskov 已提交
1014 1015 1016 1017
        let compare_mode =
            builder.config.cmd.compare_mode().or_else(|| {
                if builder.config.test_compare_mode { self.compare_mode } else { None }
            });
S
Santiago Pastorino 已提交
1018

1019 1020 1021 1022 1023
        if let Some(ref pass) = builder.config.cmd.pass() {
            cmd.arg("--pass");
            cmd.arg(pass);
        }

1024
        if let Some(ref nodejs) = builder.config.nodejs {
1025 1026
            cmd.arg("--nodejs").arg(nodejs);
        }
1027

M
Mark Rousskov 已提交
1028
        let mut flags = if is_rustdoc { Vec::new() } else { vec!["-Crpath".to_string()] };
1029
        if !is_rustdoc {
1030
            if builder.config.rust_optimize_tests {
G
Guillaume Gomez 已提交
1031 1032
                flags.push("-O".to_string());
            }
1033
        }
1034
        flags.push(format!("-Cdebuginfo={}", builder.config.rust_debuginfo_level_tests));
G
Guillaume Gomez 已提交
1035
        flags.push("-Zunstable-options".to_string());
1036
        flags.push(builder.config.cmd.rustc_args().join(" "));
1037

1038
        if let Some(linker) = builder.linker(target) {
O
Oliver Schneider 已提交
1039 1040 1041
            cmd.arg("--linker").arg(linker);
        }

1042
        let mut hostflags = flags.clone();
M
Mark Rousskov 已提交
1043
        hostflags.push(format!("-Lnative={}", builder.test_helpers_out(compiler.host).display()));
1044 1045
        cmd.arg("--host-rustcflags").arg(hostflags.join(" "));

S
Shotaro Yamada 已提交
1046
        let mut targetflags = flags;
M
Mark Rousskov 已提交
1047
        targetflags.push(format!("-Lnative={}", builder.test_helpers_out(target).display()));
1048 1049
        cmd.arg("--target-rustcflags").arg(targetflags.join(" "));

1050
        cmd.arg("--docck-python").arg(builder.python());
1051

1052
        if builder.config.build.ends_with("apple-darwin") {
1053 1054 1055 1056 1057
            // Force /usr/bin/python on macOS for LLDB tests because we're loading the
            // LLDB plugin's compiled module which only works with the system python
            // (namely not Homebrew-installed python)
            cmd.arg("--lldb-python").arg("/usr/bin/python");
        } else {
1058
            cmd.arg("--lldb-python").arg(builder.python());
1059
        }
1060

1061
        if let Some(ref gdb) = builder.config.gdb {
1062 1063
            cmd.arg("--gdb").arg(gdb);
        }
1064 1065 1066 1067

        let run = |cmd: &mut Command| {
            cmd.output().map(|output| {
                String::from_utf8_lossy(&output.stdout)
M
Mark Rousskov 已提交
1068 1069 1070 1071
                    .lines()
                    .next()
                    .unwrap_or_else(|| panic!("{:?} failed {:?}", cmd, output))
                    .to_string()
1072 1073
            })
        };
1074
        let lldb_exe = if builder.config.lldb_enabled {
1075
            // Test against the lldb that was just built.
1076
            builder.llvm_out(target).join("bin").join("lldb")
1077 1078 1079 1080 1081 1082
        } else {
            PathBuf::from("lldb")
        };
        let lldb_version = Command::new(&lldb_exe)
            .arg("--version")
            .output()
M
Mark Rousskov 已提交
1083
            .map(|output| String::from_utf8_lossy(&output.stdout).to_string())
1084 1085
            .ok();
        if let Some(ref vers) = lldb_version {
1086
            cmd.arg("--lldb-version").arg(vers);
1087 1088 1089 1090
            let lldb_python_dir = run(Command::new(&lldb_exe).arg("-P")).ok();
            if let Some(ref dir) = lldb_python_dir {
                cmd.arg("--lldb-python-dir").arg(dir);
            }
1091
        }
1092

1093 1094 1095
        if util::forcing_clang_based_tests() {
            let clang_exe = builder.llvm_out(target).join("bin").join("clang");
            cmd.arg("--run-clang-based-tests-with").arg(clang_exe);
1096 1097
        }

1098 1099
        // Get paths from cmd args
        let paths = match &builder.config.cmd {
S
Santiago Pastorino 已提交
1100 1101
            Subcommand::Test { ref paths, .. } => &paths[..],
            _ => &[],
1102 1103 1104
        };

        // Get test-args by striping suite path
S
Santiago Pastorino 已提交
1105 1106
        let mut test_args: Vec<&str> = paths
            .iter()
M
Mark Rousskov 已提交
1107 1108 1109
            .map(|p| match p.strip_prefix(".") {
                Ok(path) => path,
                Err(_) => p,
1110
            })
1111 1112
            .filter(|p| p.starts_with(suite_path) && (p.is_dir() || p.is_file()))
            .filter_map(|p| {
V
varkor 已提交
1113 1114 1115 1116 1117 1118
                // Since test suite paths are themselves directories, if we don't
                // specify a directory or file, we'll get an empty string here
                // (the result of the test suite directory without its suite prefix).
                // Therefore, we need to filter these out, as only the first --test-args
                // flag is respected, so providing an empty --test-args conflicts with
                // any following it.
1119 1120 1121 1122 1123
                match p.strip_prefix(suite_path).ok().and_then(|p| p.to_str()) {
                    Some(s) if s != "" => Some(s),
                    _ => None,
                }
            })
S
Santiago Pastorino 已提交
1124
            .collect();
1125 1126 1127 1128

        test_args.append(&mut builder.config.cmd.test_args());

        cmd.args(&test_args);
1129

1130
        if builder.is_verbose() {
1131 1132
            cmd.arg("--verbose");
        }
1133

O
Oliver Schneider 已提交
1134
        if !builder.config.verbose_tests {
1135 1136
            cmd.arg("--quiet");
        }
1137

B
bjorn3 已提交
1138
        if builder.config.llvm_enabled() {
M
Mark Rousskov 已提交
1139
            let llvm_config = builder.ensure(native::Llvm { target: builder.config.build });
1140
            if !builder.config.dry_run {
1141 1142 1143
                let llvm_version = output(Command::new(&llvm_config).arg("--version"));
                cmd.arg("--llvm-version").arg(llvm_version);
            }
1144
            if !builder.is_rust_llvm(target) {
B
bjorn3 已提交
1145 1146 1147 1148 1149
                cmd.arg("--system-llvm");
            }

            // Only pass correct values for these flags for the `run-make` suite as it
            // requires that a C++ compiler was configured which isn't always the case.
E
Eric Huss 已提交
1150
            if !builder.config.dry_run && suite == "run-make-fulldeps" {
B
bjorn3 已提交
1151 1152
                let llvm_components = output(Command::new(&llvm_config).arg("--components"));
                let llvm_cxxflags = output(Command::new(&llvm_config).arg("--cxxflags"));
S
Santiago Pastorino 已提交
1153 1154 1155 1156 1157
                cmd.arg("--cc")
                    .arg(builder.cc(target))
                    .arg("--cxx")
                    .arg(builder.cxx(target).unwrap())
                    .arg("--cflags")
1158
                    .arg(builder.cflags(target, GitRepo::Rustc).join(" "))
S
Santiago Pastorino 已提交
1159 1160 1161 1162
                    .arg("--llvm-components")
                    .arg(llvm_components.trim())
                    .arg("--llvm-cxxflags")
                    .arg(llvm_cxxflags.trim());
1163
                if let Some(ar) = builder.ar(target) {
O
Oliver Schneider 已提交
1164 1165
                    cmd.arg("--ar").arg(ar);
                }
1166 1167 1168

                // The llvm/bin directory contains many useful cross-platform
                // tools. Pass the path to run-make tests so they can use them.
M
Mark Rousskov 已提交
1169 1170
                let llvm_bin_path = llvm_config
                    .parent()
1171 1172 1173
                    .expect("Expected llvm-config to be contained in directory");
                assert!(llvm_bin_path.is_dir());
                cmd.arg("--llvm-bin-dir").arg(llvm_bin_path);
1174 1175 1176

                // If LLD is available, add it to the PATH
                if builder.config.lld_enabled {
M
Mark Rousskov 已提交
1177 1178
                    let lld_install_root =
                        builder.ensure(native::Lld { target: builder.config.build });
1179 1180 1181 1182

                    let lld_bin_path = lld_install_root.join("bin");

                    let old_path = env::var_os("PATH").unwrap_or_default();
M
Mark Rousskov 已提交
1183 1184 1185 1186
                    let new_path = env::join_paths(
                        std::iter::once(lld_bin_path).chain(env::split_paths(&old_path)),
                    )
                    .expect("Could not add LLD bin path to PATH");
1187 1188
                    cmd.env("PATH", new_path);
                }
B
bjorn3 已提交
1189 1190 1191
            }
        }

E
Eric Huss 已提交
1192
        if suite != "run-make-fulldeps" {
S
Santiago Pastorino 已提交
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
            cmd.arg("--cc")
                .arg("")
                .arg("--cxx")
                .arg("")
                .arg("--cflags")
                .arg("")
                .arg("--llvm-components")
                .arg("")
                .arg("--llvm-cxxflags")
                .arg("");
1203
        }
1204

1205
        if builder.remote_tested(target) {
M
Mark Rousskov 已提交
1206
            cmd.arg("--remote-test-client").arg(builder.tool_exe(Tool::RemoteTestClient));
1207
        }
1208

1209 1210 1211 1212 1213 1214
        // Running a C compiler on MSVC requires a few env vars to be set, to be
        // sure to set them here.
        //
        // Note that if we encounter `PATH` we make sure to append to our own `PATH`
        // rather than stomp over it.
        if target.contains("msvc") {
1215
            for &(ref k, ref v) in builder.cc[&target].env() {
1216 1217 1218
                if k != "PATH" {
                    cmd.env(k, v);
                }
1219 1220
            }
        }
1221
        cmd.env("RUSTC_BOOTSTRAP", "1");
1222
        builder.add_rust_test_threads(&mut cmd);
1223

1224
        if builder.config.sanitizers {
1225
            cmd.env("RUSTC_SANITIZER_SUPPORT", "1");
1226
        }
1227

1228
        if builder.config.profiler {
1229
            cmd.env("RUSTC_PROFILER_SUPPORT", "1");
1230
        }
1231

M
Mark Rousskov 已提交
1232 1233 1234 1235
        let tmp = builder.out.join("tmp");
        std::fs::create_dir_all(&tmp).unwrap();
        cmd.env("RUST_TEST_TMPDIR", tmp);

1236 1237 1238 1239 1240
        cmd.arg("--adb-path").arg("adb");
        cmd.arg("--adb-test-dir").arg(ADB_TEST_DIR);
        if target.contains("android") {
            // Assume that cc for this target comes from the android sysroot
            cmd.arg("--android-cross-path")
S
Santiago Pastorino 已提交
1241
                .arg(builder.cc(target).parent().unwrap().parent().unwrap());
1242 1243 1244
        } else {
            cmd.arg("--android-cross-path").arg("");
        }
1245

1246 1247 1248 1249
        if builder.config.cmd.rustfix_coverage() {
            cmd.arg("--rustfix-coverage");
        }

1250
        builder.ci_env.force_coloring_in_ci(&mut cmd);
1251

S
Santiago Pastorino 已提交
1252 1253 1254 1255
        builder.info(&format!(
            "Check compiletest suite={} mode={} ({} -> {})",
            suite, mode, &compiler.host, target
        ));
1256 1257
        let _time = util::timeit(&builder);
        try_run(builder, &mut cmd);
1258 1259 1260

        if let Some(compare_mode) = compare_mode {
            cmd.arg("--compare-mode").arg(compare_mode);
S
Santiago Pastorino 已提交
1261 1262 1263 1264
            builder.info(&format!(
                "Check compiletest suite={} mode={} compare_mode={} ({} -> {})",
                suite, mode, compare_mode, &compiler.host, target
            ));
1265 1266 1267
            let _time = util::timeit(&builder);
            try_run(builder, &mut cmd);
        }
1268
    }
1269
}
1270

1271
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1272
struct DocTest {
1273
    compiler: Compiler,
1274 1275 1276
    path: &'static str,
    name: &'static str,
    is_ext_doc: bool,
1277 1278
}

1279
impl Step for DocTest {
1280 1281
    type Output = ();
    const ONLY_HOSTS: bool = true;
1282

T
Taiki Endo 已提交
1283
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1284
        run.never()
1285
    }
M
Mark Simulacrum 已提交
1286

A
Alexander Regueiro 已提交
1287
    /// Runs `rustdoc --test` for all documentation in `src/doc`.
1288
    ///
1289
    /// This will run all tests in our markdown documentation (e.g., the book)
1290 1291
    /// located in `src/doc`. The `rustdoc` that's run is the one that sits next to
    /// `compiler`.
T
Taiki Endo 已提交
1292
    fn run(self, builder: &Builder<'_>) {
1293
        let compiler = self.compiler;
1294

M
Mark Rousskov 已提交
1295
        builder.ensure(compile::Std { compiler, target: compiler.host });
1296

1297 1298
        // Do a breadth-first traversal of the `src/doc` directory and just run
        // tests for all files that end in `*.md`
1299 1300
        let mut stack = vec![builder.src.join(self.path)];
        let _time = util::timeit(&builder);
1301

1302
        let mut files = Vec::new();
1303 1304 1305
        while let Some(p) = stack.pop() {
            if p.is_dir() {
                stack.extend(t!(p.read_dir()).map(|p| t!(p).path()));
S
Santiago Pastorino 已提交
1306
                continue;
1307 1308 1309 1310 1311 1312 1313
            }

            if p.extension().and_then(|s| s.to_str()) != Some("md") {
                continue;
            }

            // The nostarch directory in the book is for no starch, and so isn't
1314
            // guaranteed to builder. We don't care if it doesn't build, so skip it.
1315 1316 1317 1318
            if p.to_str().map_or(false, |p| p.contains("nostarch")) {
                continue;
            }

1319 1320 1321 1322 1323
            files.push(p);
        }

        files.sort();

1324
        let mut toolstate = ToolState::TestPass;
1325
        for file in files {
1326 1327
            if !markdown_test(builder, compiler, &file) {
                toolstate = ToolState::TestFail;
1328
            }
1329
        }
1330 1331 1332
        if self.is_ext_doc {
            builder.save_toolstate(self.name, toolstate);
        }
1333 1334 1335
    }
}

1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
macro_rules! test_book {
    ($($name:ident, $path:expr, $book_name:expr, default=$default:expr;)+) => {
        $(
            #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
            pub struct $name {
                compiler: Compiler,
            }

            impl Step for $name {
                type Output = ();
                const DEFAULT: bool = $default;
                const ONLY_HOSTS: bool = true;

T
Taiki Endo 已提交
1349
                fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1350 1351 1352
                    run.path($path)
                }

T
Taiki Endo 已提交
1353
                fn make_run(run: RunConfig<'_>) {
1354 1355 1356 1357 1358
                    run.builder.ensure($name {
                        compiler: run.builder.compiler(run.builder.top_stage, run.host),
                    });
                }

T
Taiki Endo 已提交
1359
                fn run(self, builder: &Builder<'_>) {
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
                    builder.ensure(DocTest {
                        compiler: self.compiler,
                        path: $path,
                        name: $book_name,
                        is_ext_doc: !$default,
                    });
                }
            }
        )+
    }
}

test_book!(
    Nomicon, "src/doc/nomicon", "nomicon", default=false;
    Reference, "src/doc/reference", "reference", default=false;
    RustdocBook, "src/doc/rustdoc", "rustdoc", default=true;
1376
    RustcBook, "src/doc/rustc", "rustc", default=true;
1377
    RustByExample, "src/doc/rust-by-example", "rust-by-example", default=false;
1378
    EmbeddedBook, "src/doc/embedded-book", "embedded-book", default=false;
1379 1380
    TheBook, "src/doc/book", "book", default=false;
    UnstableBook, "src/doc/unstable-book", "unstable-book", default=true;
E
Eric Huss 已提交
1381
    EditionGuide, "src/doc/edition-guide", "edition-guide", default=false;
1382 1383
);

1384 1385 1386
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct ErrorIndex {
    compiler: Compiler,
1387
}
1388

1389
impl Step for ErrorIndex {
1390
    type Output = ();
1391 1392 1393
    const DEFAULT: bool = true;
    const ONLY_HOSTS: bool = true;

T
Taiki Endo 已提交
1394
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1395
        run.path("src/tools/error_index_generator")
1396 1397
    }

T
Taiki Endo 已提交
1398
    fn make_run(run: RunConfig<'_>) {
M
Mark Rousskov 已提交
1399 1400
        run.builder
            .ensure(ErrorIndex { compiler: run.builder.compiler(run.builder.top_stage, run.host) });
1401
    }
1402

A
Alexander Regueiro 已提交
1403
    /// Runs the error index generator tool to execute the tests located in the error
1404 1405 1406 1407 1408
    /// index.
    ///
    /// The `error_index_generator` tool lives in `src/tools` and is used to
    /// generate a markdown file from the error indexes of the code base which is
    /// then passed to `rustdoc --test`.
T
Taiki Endo 已提交
1409
    fn run(self, builder: &Builder<'_>) {
1410 1411
        let compiler = self.compiler;

M
Mark Rousskov 已提交
1412
        builder.ensure(compile::Std { compiler, target: compiler.host });
1413

1414
        let dir = testdir(builder, compiler.host);
1415 1416 1417
        t!(fs::create_dir_all(&dir));
        let output = dir.join("error-index.md");

1418 1419 1420 1421
        let mut tool = tool::ErrorIndex::command(
            builder,
            builder.compiler(compiler.stage, builder.config.build),
        );
M
Mark Rousskov 已提交
1422
        tool.arg("markdown").arg(&output).env("CFG_BUILD", &builder.config.build);
1423

1424 1425
        builder.info(&format!("Testing error-index stage{}", compiler.stage));
        let _time = util::timeit(&builder);
1426
        builder.run_quiet(&mut tool);
1427
        markdown_test(builder, compiler, &output);
1428
    }
1429 1430
}

T
Taiki Endo 已提交
1431
fn markdown_test(builder: &Builder<'_>, compiler: Compiler, markdown: &Path) -> bool {
1432 1433
    match fs::read_to_string(markdown) {
        Ok(contents) => {
1434 1435 1436 1437
            if !contents.contains("```") {
                return true;
            }
        }
S
Santiago Pastorino 已提交
1438
        Err(_) => {}
1439 1440
    }

1441
    builder.info(&format!("doc tests for: {}", markdown.display()));
M
Mark Rousskov 已提交
1442
    let mut cmd = builder.rustdoc_cmd(compiler);
1443
    builder.add_rust_test_threads(&mut cmd);
1444 1445
    cmd.arg("--test");
    cmd.arg(markdown);
1446
    cmd.env("RUSTC_BOOTSTRAP", "1");
1447

1448
    let test_args = builder.config.cmd.test_args().join(" ");
1449 1450
    cmd.arg("--test-args").arg(test_args);

O
Oliver Schneider 已提交
1451
    if builder.config.verbose_tests {
1452
        try_run(builder, &mut cmd)
O
Oliver Schneider 已提交
1453 1454
    } else {
        try_run_quiet(builder, &mut cmd)
1455
    }
1456
}
1457

1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct RustcGuide;

impl Step for RustcGuide {
    type Output = ();
    const DEFAULT: bool = false;
    const ONLY_HOSTS: bool = true;

    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
        run.path("src/doc/rustc-guide")
    }

    fn make_run(run: RunConfig<'_>) {
        run.builder.ensure(RustcGuide);
    }

    fn run(self, builder: &Builder<'_>) {
        let src = builder.src.join("src/doc/rustc-guide");
        let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
E
Eric Huss 已提交
1477 1478 1479 1480 1481 1482
        let toolstate = if try_run(builder, rustbook_cmd.arg("linkcheck").arg(&src)) {
            ToolState::TestPass
        } else {
            ToolState::TestFail
        };
        builder.save_toolstate("rustc-guide", toolstate);
1483 1484 1485
    }
}

1486
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
M
Mark Simulacrum 已提交
1487
pub struct CrateLibrustc {
1488 1489
    compiler: Compiler,
    target: Interned<String>,
1490
    test_kind: TestKind,
1491
    krate: Interned<String>,
1492 1493
}

M
Mark Simulacrum 已提交
1494
impl Step for CrateLibrustc {
1495 1496 1497 1498
    type Output = ();
    const DEFAULT: bool = true;
    const ONLY_HOSTS: bool = true;

T
Taiki Endo 已提交
1499
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1500
        run.krate("rustc-main")
1501 1502
    }

T
Taiki Endo 已提交
1503
    fn make_run(run: RunConfig<'_>) {
1504 1505
        let builder = run.builder;
        let compiler = builder.compiler(builder.top_stage, run.host);
1506

1507 1508
        for krate in builder.in_tree_crates("rustc-main") {
            if run.path.ends_with(&krate.path) {
1509
                let test_kind = builder.kind.into();
1510

1511 1512 1513 1514 1515 1516
                builder.ensure(CrateLibrustc {
                    compiler,
                    target: run.target,
                    test_kind,
                    krate: krate.name,
                });
1517 1518 1519 1520
            }
        }
    }

T
Taiki Endo 已提交
1521
    fn run(self, builder: &Builder<'_>) {
M
Mark Simulacrum 已提交
1522
        builder.ensure(Crate {
1523 1524
            compiler: self.compiler,
            target: self.target,
C
Collins Abitekaniza 已提交
1525
            mode: Mode::Rustc,
1526 1527 1528 1529 1530 1531
            test_kind: self.test_kind,
            krate: self.krate,
        });
    }
}

1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct CrateNotDefault {
    compiler: Compiler,
    target: Interned<String>,
    test_kind: TestKind,
    krate: &'static str,
}

impl Step for CrateNotDefault {
    type Output = ();

T
Taiki Endo 已提交
1543
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1544
        run.path("src/librustc_asan")
1545 1546 1547 1548 1549
            .path("src/librustc_lsan")
            .path("src/librustc_msan")
            .path("src/librustc_tsan")
    }

T
Taiki Endo 已提交
1550
    fn make_run(run: RunConfig<'_>) {
1551 1552 1553
        let builder = run.builder;
        let compiler = builder.compiler(builder.top_stage, run.host);

1554
        let test_kind = builder.kind.into();
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569

        builder.ensure(CrateNotDefault {
            compiler,
            target: run.target,
            test_kind,
            krate: match run.path {
                _ if run.path.ends_with("src/librustc_asan") => "rustc_asan",
                _ if run.path.ends_with("src/librustc_lsan") => "rustc_lsan",
                _ if run.path.ends_with("src/librustc_msan") => "rustc_msan",
                _ if run.path.ends_with("src/librustc_tsan") => "rustc_tsan",
                _ => panic!("unexpected path {:?}", run.path),
            },
        });
    }

T
Taiki Endo 已提交
1570
    fn run(self, builder: &Builder<'_>) {
1571 1572 1573
        builder.ensure(Crate {
            compiler: self.compiler,
            target: self.target,
C
Collins Abitekaniza 已提交
1574
            mode: Mode::Std,
1575 1576 1577 1578 1579 1580
            test_kind: self.test_kind,
            krate: INTERNER.intern_str(self.krate),
        });
    }
}

K
kennytm 已提交
1581
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
M
Mark Simulacrum 已提交
1582
pub struct Crate {
K
kennytm 已提交
1583 1584 1585 1586 1587
    pub compiler: Compiler,
    pub target: Interned<String>,
    pub mode: Mode,
    pub test_kind: TestKind,
    pub krate: Interned<String>,
1588
}
1589

M
Mark Simulacrum 已提交
1590
impl Step for Crate {
1591
    type Output = ();
1592 1593
    const DEFAULT: bool = true;

T
Taiki Endo 已提交
1594
    fn should_run(mut run: ShouldRun<'_>) -> ShouldRun<'_> {
1595
        let builder = run.builder;
1596
        for krate in run.builder.in_tree_crates("test") {
1597
            if !(krate.name.starts_with("rustc_") && krate.name.ends_with("san")) {
1598 1599 1600 1601
                run = run.path(krate.local_path(&builder).to_str().unwrap());
            }
        }
        run
1602 1603
    }

T
Taiki Endo 已提交
1604
    fn make_run(run: RunConfig<'_>) {
1605 1606
        let builder = run.builder;
        let compiler = builder.compiler(builder.top_stage, run.host);
1607

1608
        let make = |mode: Mode, krate: &CargoCrate| {
1609
            let test_kind = builder.kind.into();
1610

M
Mark Simulacrum 已提交
1611
            builder.ensure(Crate {
1612 1613
                compiler,
                target: run.target,
1614 1615
                mode,
                test_kind,
1616
                krate: krate.name,
1617 1618 1619
            });
        };

1620 1621
        for krate in builder.in_tree_crates("test") {
            if run.path.ends_with(&krate.local_path(&builder)) {
1622
                make(Mode::Std, krate);
1623 1624 1625
            }
        }
    }
1626

A
Alexander Regueiro 已提交
1627
    /// Runs all unit tests plus documentation tests for a given crate defined
1628
    /// by a `Cargo.toml` (single manifest)
1629 1630 1631 1632 1633 1634
    ///
    /// This is what runs tests for crates like the standard library, compiler, etc.
    /// It essentially is the driver for running `cargo test`.
    ///
    /// Currently this runs all tests for a DAG by passing a bunch of `-p foo`
    /// arguments, and those arguments are discovered from `cargo metadata`.
T
Taiki Endo 已提交
1635
    fn run(self, builder: &Builder<'_>) {
1636 1637 1638 1639 1640 1641
        let compiler = self.compiler;
        let target = self.target;
        let mode = self.mode;
        let test_kind = self.test_kind;
        let krate = self.krate;

1642
        builder.ensure(compile::Std { compiler, target });
1643
        builder.ensure(RemoteCopyLibs { compiler, target });
A
Alex Crichton 已提交
1644

1645 1646 1647 1648 1649
        // If we're not doing a full bootstrap but we're testing a stage2
        // version of libstd, then what we're actually testing is the libstd
        // produced in stage1. Reflect that here by updating the compiler that
        // we're working with automatically.
        let compiler = builder.compiler_for(compiler.stage, compiler.host, target);
A
Alex Crichton 已提交
1650 1651

        let mut cargo = builder.cargo(compiler, mode, target, test_kind.subcommand());
1652
        match mode {
C
Collins Abitekaniza 已提交
1653
            Mode::Std => {
1654
                compile::std_cargo(builder, target, &mut cargo);
1655
            }
C
Collins Abitekaniza 已提交
1656
            Mode::Rustc => {
1657
                builder.ensure(compile::Rustc { compiler, target });
1658
                compile::rustc_cargo(builder, &mut cargo, target);
1659 1660 1661 1662 1663 1664 1665 1666 1667
            }
            _ => panic!("can only test libraries"),
        };

        // Build up the base `cargo test` command.
        //
        // Pass in some standard flags then iterate over the graph we've discovered
        // in `cargo metadata` with the maps above and figure out what `-p`
        // arguments need to get passed.
1668
        if test_kind.subcommand() == "test" && !builder.fail_fast {
1669 1670
            cargo.arg("--no-fail-fast");
        }
K
kennytm 已提交
1671
        match builder.doc_tests {
K
kennytm 已提交
1672
            DocTests::Only => {
K
kennytm 已提交
1673 1674
                cargo.arg("--doc");
            }
K
kennytm 已提交
1675
            DocTests::No => {
K
kennytm 已提交
1676 1677
                cargo.args(&["--lib", "--bins", "--examples", "--tests", "--benches"]);
            }
K
kennytm 已提交
1678
            DocTests::Yes => {}
1679
        }
1680

1681
        cargo.arg("-p").arg(krate);
1682

1683 1684 1685 1686 1687 1688
        // The tests are going to run with the *target* libraries, so we need to
        // ensure that those libraries show up in the LD_LIBRARY_PATH equivalent.
        //
        // Note that to run the compiler we need to run with the *host* libraries,
        // but our wrapper scripts arrange for that to be the case anyway.
        let mut dylib_path = dylib_path();
1689
        dylib_path.insert(0, PathBuf::from(&*builder.sysroot_libdir(compiler, target)));
1690 1691 1692
        cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());

        cargo.arg("--");
1693
        cargo.args(&builder.config.cmd.test_args());
1694

O
Oliver Schneider 已提交
1695
        if !builder.config.verbose_tests {
1696 1697
            cargo.arg("--quiet");
        }
1698

1699
        if target.contains("emscripten") {
S
Santiago Pastorino 已提交
1700 1701
            cargo.env(
                format!("CARGO_TARGET_{}_RUNNER", envify(&target)),
M
Mark Rousskov 已提交
1702
                builder.config.nodejs.as_ref().expect("nodejs not configured"),
S
Santiago Pastorino 已提交
1703
            );
O
Oliver Schneider 已提交
1704
        } else if target.starts_with("wasm32") {
M
Mark Rousskov 已提交
1705 1706 1707
            let node = builder.config.nodejs.as_ref().expect("nodejs not configured");
            let runner =
                format!("{} {}/src/etc/wasm32-shim.js", node.display(), builder.src.display());
O
Oliver Schneider 已提交
1708
            cargo.env(format!("CARGO_TARGET_{}_RUNNER", envify(&target)), &runner);
1709
        } else if builder.remote_tested(target) {
S
Santiago Pastorino 已提交
1710 1711 1712 1713
            cargo.env(
                format!("CARGO_TARGET_{}_RUNNER", envify(&target)),
                format!("{} run", builder.tool_exe(Tool::RemoteTestClient).display()),
            );
1714
        }
1715

S
Santiago Pastorino 已提交
1716 1717 1718 1719
        builder.info(&format!(
            "{} {} stage{} ({} -> {})",
            test_kind, krate, compiler.stage, &compiler.host, target
        ));
1720
        let _time = util::timeit(&builder);
1721
        try_run(builder, &mut cargo.into());
1722 1723
    }
}
1724

M
Mark Simulacrum 已提交
1725
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1726
pub struct CrateRustdoc {
M
Mark Simulacrum 已提交
1727 1728 1729 1730
    host: Interned<String>,
    test_kind: TestKind,
}

1731
impl Step for CrateRustdoc {
M
Mark Simulacrum 已提交
1732 1733 1734 1735
    type Output = ();
    const DEFAULT: bool = true;
    const ONLY_HOSTS: bool = true;

T
Taiki Endo 已提交
1736
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1737
        run.paths(&["src/librustdoc", "src/tools/rustdoc"])
M
Mark Simulacrum 已提交
1738 1739
    }

T
Taiki Endo 已提交
1740
    fn make_run(run: RunConfig<'_>) {
M
Mark Simulacrum 已提交
1741 1742
        let builder = run.builder;

1743
        let test_kind = builder.kind.into();
M
Mark Simulacrum 已提交
1744

M
Mark Rousskov 已提交
1745
        builder.ensure(CrateRustdoc { host: run.host, test_kind });
M
Mark Simulacrum 已提交
1746 1747
    }

T
Taiki Endo 已提交
1748
    fn run(self, builder: &Builder<'_>) {
M
Mark Simulacrum 已提交
1749 1750 1751 1752
        let test_kind = self.test_kind;

        let compiler = builder.compiler(builder.top_stage, self.host);
        let target = compiler.host;
1753
        builder.ensure(compile::Rustc { compiler, target });
M
Mark Simulacrum 已提交
1754

M
Mark Rousskov 已提交
1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
        let mut cargo = tool::prepare_tool_cargo(
            builder,
            compiler,
            Mode::ToolRustc,
            target,
            test_kind.subcommand(),
            "src/tools/rustdoc",
            SourceType::InTree,
            &[],
        );
1765
        if test_kind.subcommand() == "test" && !builder.fail_fast {
M
Mark Simulacrum 已提交
1766 1767 1768 1769 1770 1771
            cargo.arg("--no-fail-fast");
        }

        cargo.arg("-p").arg("rustdoc:0.0.0");

        cargo.arg("--");
1772
        cargo.args(&builder.config.cmd.test_args());
M
Mark Simulacrum 已提交
1773

1774 1775 1776 1777
        if self.host.contains("musl") {
            cargo.arg("'-Ctarget-feature=-crt-static'");
        }

O
Oliver Schneider 已提交
1778
        if !builder.config.verbose_tests {
M
Mark Simulacrum 已提交
1779 1780 1781
            cargo.arg("--quiet");
        }

S
Santiago Pastorino 已提交
1782 1783 1784 1785
        builder.info(&format!(
            "{} rustdoc stage{} ({} -> {})",
            test_kind, compiler.stage, &compiler.host, target
        ));
1786
        let _time = util::timeit(&builder);
M
Mark Simulacrum 已提交
1787

1788
        try_run(builder, &mut cargo.into());
M
Mark Simulacrum 已提交
1789 1790 1791
    }
}

1792 1793 1794 1795 1796
/// Some test suites are run inside emulators or on remote devices, and most
/// of our test binaries are linked dynamically which means we need to ship
/// the standard library and such to the emulator ahead of time. This step
/// represents this and is a dependency of all test suites.
///
A
Alexander Regueiro 已提交
1797
/// Most of the time this is a no-op. For some steps such as shipping data to
1798 1799 1800
/// QEMU we have to build our own tools so we've got conditional dependencies
/// on those programs as well. Note that the remote test client is built for
/// the build target (us) and the server is built for the target.
1801 1802 1803 1804
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct RemoteCopyLibs {
    compiler: Compiler,
    target: Interned<String>,
1805
}
1806

1807
impl Step for RemoteCopyLibs {
1808
    type Output = ();
1809

T
Taiki Endo 已提交
1810
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1811
        run.never()
1812 1813
    }

T
Taiki Endo 已提交
1814
    fn run(self, builder: &Builder<'_>) {
1815 1816
        let compiler = self.compiler;
        let target = self.target;
1817
        if !builder.remote_tested(target) {
S
Santiago Pastorino 已提交
1818
            return;
1819 1820
        }

1821
        builder.ensure(compile::Std { compiler, target });
1822

1823 1824
        builder.info(&format!("REMOTE copy libs to emulator ({})", target));
        t!(fs::create_dir_all(builder.out.join("tmp")));
1825

M
Mark Rousskov 已提交
1826 1827
        let server =
            builder.ensure(tool::RemoteTestServer { compiler: compiler.with_stage(0), target });
1828 1829

        // Spawn the emulator and wait for it to come online
1830
        let tool = builder.tool_exe(Tool::RemoteTestClient);
1831
        let mut cmd = Command::new(&tool);
M
Mark Rousskov 已提交
1832
        cmd.arg("spawn-emulator").arg(target).arg(&server).arg(builder.out.join("tmp"));
1833
        if let Some(rootfs) = builder.qemu_rootfs(target) {
1834 1835
            cmd.arg(rootfs);
        }
1836
        builder.run(&mut cmd);
1837 1838

        // Push all our dylibs to the emulator
1839
        for f in t!(builder.sysroot_libdir(compiler, target).read_dir()) {
1840 1841 1842
            let f = t!(f);
            let name = f.file_name().into_string().unwrap();
            if util::is_dylib(&name) {
S
Santiago Pastorino 已提交
1843
                builder.run(Command::new(&tool).arg("push").arg(f.path()));
1844
            }
1845 1846 1847 1848
        }
    }
}

1849
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1850
pub struct Distcheck;
A
Alex Crichton 已提交
1851

1852
impl Step for Distcheck {
1853 1854
    type Output = ();

T
Taiki Endo 已提交
1855
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1856
        run.path("distcheck")
1857 1858
    }

T
Taiki Endo 已提交
1859
    fn make_run(run: RunConfig<'_>) {
M
Mark Simulacrum 已提交
1860 1861 1862
        run.builder.ensure(Distcheck);
    }

A
Alexander Regueiro 已提交
1863
    /// Runs "distcheck", a 'make check' from a tarball
T
Taiki Endo 已提交
1864
    fn run(self, builder: &Builder<'_>) {
1865
        builder.info("Distcheck");
1866
        let dir = builder.out.join("tmp").join("distcheck");
1867 1868 1869
        let _ = fs::remove_dir_all(&dir);
        t!(fs::create_dir_all(&dir));

M
Mark Simulacrum 已提交
1870 1871 1872 1873
        // Guarantee that these are built before we begin running.
        builder.ensure(dist::PlainSourceTarball);
        builder.ensure(dist::Src);

1874 1875
        let mut cmd = Command::new("tar");
        cmd.arg("-xzf")
S
Santiago Pastorino 已提交
1876 1877 1878
            .arg(builder.ensure(dist::PlainSourceTarball))
            .arg("--strip-components=1")
            .current_dir(&dir);
1879
        builder.run(&mut cmd);
S
Santiago Pastorino 已提交
1880 1881 1882 1883 1884 1885 1886
        builder.run(
            Command::new("./configure")
                .args(&builder.config.configure_args)
                .arg("--enable-vendor")
                .current_dir(&dir),
        );
        builder.run(
M
Mark Rousskov 已提交
1887
            Command::new(build_helper::make(&builder.config.build)).arg("check").current_dir(&dir),
S
Santiago Pastorino 已提交
1888
        );
1889 1890

        // Now make sure that rust-src has all of libstd's dependencies
1891
        builder.info("Distcheck rust-src");
1892
        let dir = builder.out.join("tmp").join("distcheck-src");
1893 1894 1895 1896 1897
        let _ = fs::remove_dir_all(&dir);
        t!(fs::create_dir_all(&dir));

        let mut cmd = Command::new("tar");
        cmd.arg("-xzf")
S
Santiago Pastorino 已提交
1898 1899 1900
            .arg(builder.ensure(dist::Src))
            .arg("--strip-components=1")
            .current_dir(&dir);
1901
        builder.run(&mut cmd);
1902 1903

        let toml = dir.join("rust-src/lib/rustlib/src/rust/src/libstd/Cargo.toml");
S
Santiago Pastorino 已提交
1904 1905 1906 1907 1908 1909 1910
        builder.run(
            Command::new(&builder.initial_cargo)
                .arg("generate-lockfile")
                .arg("--manifest-path")
                .arg(&toml)
                .current_dir(&dir),
        );
1911
    }
A
Alex Crichton 已提交
1912
}
1913

1914
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1915 1916
pub struct Bootstrap;

1917
impl Step for Bootstrap {
1918
    type Output = ();
1919 1920
    const DEFAULT: bool = true;
    const ONLY_HOSTS: bool = true;
1921

A
Alexander Regueiro 已提交
1922
    /// Tests the build system itself.
T
Taiki Endo 已提交
1923
    fn run(self, builder: &Builder<'_>) {
1924
        let mut cmd = Command::new(&builder.initial_cargo);
1925
        cmd.arg("test")
S
Santiago Pastorino 已提交
1926 1927 1928 1929 1930
            .current_dir(builder.src.join("src/bootstrap"))
            .env("RUSTFLAGS", "-Cdebuginfo=2")
            .env("CARGO_TARGET_DIR", builder.out.join("bootstrap"))
            .env("RUSTC_BOOTSTRAP", "1")
            .env("RUSTC", &builder.initial_rustc);
1931 1932 1933 1934 1935 1936
        if let Some(flags) = option_env!("RUSTFLAGS") {
            // Use the same rustc flags for testing as for "normal" compilation,
            // so that Cargo doesn’t recompile the entire dependency graph every time:
            // https://github.com/rust-lang/rust/issues/49215
            cmd.env("RUSTFLAGS", flags);
        }
1937
        if !builder.fail_fast {
1938 1939
            cmd.arg("--no-fail-fast");
        }
1940
        cmd.arg("--").args(&builder.config.cmd.test_args());
1941 1942 1943
        // rustbuild tests are racy on directory creation so just run them one at a time.
        // Since there's not many this shouldn't be a problem.
        cmd.arg("--test-threads=1");
1944
        try_run(builder, &mut cmd);
1945
    }
1946

T
Taiki Endo 已提交
1947
    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1948
        run.path("src/bootstrap")
1949 1950
    }

T
Taiki Endo 已提交
1951
    fn make_run(run: RunConfig<'_>) {
1952
        run.builder.ensure(Bootstrap);
1953
    }
1954
}