test.rs 61.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

11
//! Implementation of the test-related targets of the build system.
12 13 14 15
//!
//! This file implements the various regression test suites that we execute on
//! our CI.

16
use std::env;
N
Nick Cameron 已提交
17
use std::ffi::OsString;
U
Ulrik Sverdrup 已提交
18
use std::fmt;
19
use std::fs;
S
Santiago Pastorino 已提交
20 21 22
use std::iter;
use std::path::{Path, PathBuf};
use std::process::Command;
23

24
use build_helper::{self, output};
25

S
Santiago Pastorino 已提交
26 27
use builder::{Builder, Compiler, Kind, RunConfig, ShouldRun, Step};
use cache::{Interned, INTERNER};
28
use compile;
A
Alex Crichton 已提交
29
use dist;
S
Santiago Pastorino 已提交
30
use flags::Subcommand;
31
use native;
32
use tool::{self, Tool, SourceType};
33
use toolstate::ToolState;
S
Santiago Pastorino 已提交
34 35
use util::{self, dylib_path, dylib_path_var};
use Crate as CargoCrate;
36
use {DocTests, Mode, GitRepo};
37

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

U
Ulrik Sverdrup 已提交
40
/// The two modes of the test runner; tests or benchmarks.
K
kennytm 已提交
41
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, PartialOrd, Ord)]
U
Ulrik Sverdrup 已提交
42 43 44 45 46 47 48
pub enum TestKind {
    /// Run `cargo test`
    Test,
    /// Run `cargo bench`
    Bench,
}

49 50 51 52 53
impl From<Kind> for TestKind {
    fn from(kind: Kind) -> Self {
        match kind {
            Kind::Test => TestKind::Test,
            Kind::Bench => TestKind::Bench,
S
Santiago Pastorino 已提交
54
            _ => panic!("unexpected kind in crate: {:?}", kind),
55 56 57 58
        }
    }
}

U
Ulrik Sverdrup 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
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 {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(match *self {
            TestKind::Test => "Testing",
            TestKind::Bench => "Benchmarking",
        })
    }
}

78 79 80 81
fn try_run(builder: &Builder, cmd: &mut Command) -> bool {
    if !builder.fail_fast {
        if !builder.try_run(cmd) {
            let mut failures = builder.delayed_failures.borrow_mut();
82
            failures.push(format!("{:?}", cmd));
O
Oliver Schneider 已提交
83
            return false;
84 85
        }
    } else {
86
        builder.run(cmd);
87
    }
O
Oliver Schneider 已提交
88
    true
89 90
}

91 92 93 94
fn try_run_quiet(builder: &Builder, cmd: &mut Command) -> bool {
    if !builder.fail_fast {
        if !builder.try_run_quiet(cmd) {
            let mut failures = builder.delayed_failures.borrow_mut();
95
            failures.push(format!("{:?}", cmd));
96
            return false;
97 98
        }
    } else {
99
        builder.run_quiet(cmd);
100
    }
101
    true
102 103
}

104 105 106
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Linkcheck {
    host: Interned<String>,
107 108
}

109
impl Step for Linkcheck {
110
    type Output = ();
111 112
    const ONLY_HOSTS: bool = true;
    const DEFAULT: bool = true;
113 114 115 116 117 118 119 120

    /// 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.
    fn run(self, builder: &Builder) {
        let host = self.host;

121
        builder.info(&format!("Linkcheck ({})", host));
122 123

        builder.default_doc(None);
124

125
        let _time = util::timeit(&builder);
S
Santiago Pastorino 已提交
126 127 128 129 130 131
        try_run(
            builder,
            builder
                .tool_cmd(Tool::Linkchecker)
                .arg(builder.out.join(host).join("doc")),
        );
132
    }
133

134
    fn should_run(run: ShouldRun) -> ShouldRun {
135
        let builder = run.builder;
S
Santiago Pastorino 已提交
136 137
        run.path("src/tools/linkchecker")
            .default_condition(builder.config.docs)
138 139
    }

140
    fn make_run(run: RunConfig) {
141
        run.builder.ensure(Linkcheck { host: run.target });
142
    }
143
}
144

145 146
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Cargotest {
147
    stage: u32,
148
    host: Interned<String>,
149
}
150

151
impl Step for Cargotest {
152
    type Output = ();
153
    const ONLY_HOSTS: bool = true;
154

155 156
    fn should_run(run: ShouldRun) -> ShouldRun {
        run.path("src/tools/cargotest")
157 158
    }

159 160 161
    fn make_run(run: RunConfig) {
        run.builder.ensure(Cargotest {
            stage: run.builder.top_stage,
162
            host: run.target,
163 164 165
        });
    }

166 167 168 169 170
    /// 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.
    fn run(self, builder: &Builder) {
171
        let compiler = builder.compiler(self.stage, self.host);
S
Santiago Pastorino 已提交
172 173 174 175
        builder.ensure(compile::Rustc {
            compiler,
            target: compiler.host,
        });
176 177 178 179

        // 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.
180
        let out_dir = builder.out.join("ct");
181 182
        t!(fs::create_dir_all(&out_dir));

183
        let _time = util::timeit(&builder);
184
        let mut cmd = builder.tool_cmd(Tool::CargoTest);
S
Santiago Pastorino 已提交
185 186 187 188 189 190 191
        try_run(
            builder,
            cmd.arg(&builder.initial_cargo)
                .arg(&out_dir)
                .env("RUSTC", builder.rustc(compiler))
                .env("RUSTDOC", builder.rustdoc(compiler.host)),
        );
192
    }
193 194
}

195 196
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Cargo {
197
    stage: u32,
198
    host: Interned<String>,
199 200
}

201
impl Step for Cargo {
202
    type Output = ();
203 204
    const ONLY_HOSTS: bool = true;

205 206
    fn should_run(run: ShouldRun) -> ShouldRun {
        run.path("src/tools/cargo")
207 208
    }

209 210 211 212
    fn make_run(run: RunConfig) {
        run.builder.ensure(Cargo {
            stage: run.builder.top_stage,
            host: run.target,
213 214
        });
    }
215 216 217

    /// Runs `cargo test` for `cargo` packaged with Rust.
    fn run(self, builder: &Builder) {
218
        let compiler = builder.compiler(self.stage, self.host);
219

S
Santiago Pastorino 已提交
220 221 222 223
        builder.ensure(tool::Cargo {
            compiler,
            target: self.host,
        });
224 225 226 227 228 229
        let mut cargo = tool::prepare_tool_cargo(builder,
                                                 compiler,
                                                 Mode::ToolRustc,
                                                 self.host,
                                                 "test",
                                                 "src/tools/cargo",
230 231
                                                 SourceType::Submodule,
                                                 &[]);
232

233
        if !builder.fail_fast {
234 235
            cargo.arg("--no-fail-fast");
        }
236

237 238 239
        // Don't run cross-compile tests, we may not have cross-compiled libstd libs
        // available.
        cargo.env("CFG_DISABLE_CROSS_TESTS", "1");
240 241
        // Disable a test that has issues with mingw.
        cargo.env("CARGO_TEST_DISABLE_GIT_CLI", "1");
242

S
Santiago Pastorino 已提交
243 244 245 246
        try_run(
            builder,
            cargo.env("PATH", &path_for_cargo(builder, compiler)),
        );
247
    }
N
Nick Cameron 已提交
248 249
}

M
Mark Simulacrum 已提交
250 251
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Rls {
252
    stage: u32,
M
Mark Simulacrum 已提交
253
    host: Interned<String>,
254
}
N
Nick Cameron 已提交
255

M
Mark Simulacrum 已提交
256
impl Step for Rls {
257
    type Output = ();
M
Mark Simulacrum 已提交
258 259
    const ONLY_HOSTS: bool = true;

260 261
    fn should_run(run: ShouldRun) -> ShouldRun {
        run.path("src/tools/rls")
M
Mark Simulacrum 已提交
262 263
    }

264 265 266 267
    fn make_run(run: RunConfig) {
        run.builder.ensure(Rls {
            stage: run.builder.top_stage,
            host: run.target,
M
Mark Simulacrum 已提交
268 269
        });
    }
N
Nick Cameron 已提交
270

271 272 273 274
    /// Runs `cargo test` for the rls.
    fn run(self, builder: &Builder) {
        let stage = self.stage;
        let host = self.host;
M
Mark Simulacrum 已提交
275
        let compiler = builder.compiler(stage, host);
N
Nick Cameron 已提交
276

277 278 279 280 281 282 283 284 285 286
        let build_result = builder.ensure(tool::Rls {
            compiler,
            target: self.host,
            extra_features: Vec::new(),
        });
        if build_result.is_none() {
            eprintln!("failed to test rls: could not build");
            return;
        }

C
Collins Abitekaniza 已提交
287 288
        let mut cargo = tool::prepare_tool_cargo(builder,
                                                 compiler,
289
                                                 Mode::ToolRustc,
C
Collins Abitekaniza 已提交
290 291
                                                 host,
                                                 "test",
292
                                                 "src/tools/rls",
293 294
                                                 SourceType::Submodule,
                                                 &[]);
N
Nick Cameron 已提交
295

296 297 298 299 300 301 302
        // Copy `src/tools/rls/test_data` to a writable drive.
        let test_workspace_path = builder.out.join("rls-test-data");
        let test_data_path = test_workspace_path.join("test_data");
        builder.create_dir(&test_data_path);
        builder.cp_r(&builder.src.join("src/tools/rls/test_data"), &test_data_path);
        cargo.env("RLS_TEST_WORKSPACE_DIR", test_workspace_path);

M
Mark Simulacrum 已提交
303
        builder.add_rustc_lib_path(compiler, &mut cargo);
A
Alex Crichton 已提交
304 305
        cargo.arg("--")
            .args(builder.config.cmd.test_args());
306

307 308
        if try_run(builder, &mut cargo) {
            builder.save_toolstate("rls", ToolState::TestPass);
O
Oliver Schneider 已提交
309
        }
310
    }
N
Nick Cameron 已提交
311 312
}

N
Nick Cameron 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
#[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;

    fn should_run(run: ShouldRun) -> ShouldRun {
        run.path("src/tools/rustfmt")
    }

    fn make_run(run: RunConfig) {
        run.builder.ensure(Rustfmt {
            stage: run.builder.top_stage,
            host: run.target,
        });
    }

    /// Runs `cargo test` for rustfmt.
    fn run(self, builder: &Builder) {
        let stage = self.stage;
        let host = self.host;
        let compiler = builder.compiler(stage, host);

340 341 342 343 344 345 346 347 348 349
        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;
        }

C
Collins Abitekaniza 已提交
350 351
        let mut cargo = tool::prepare_tool_cargo(builder,
                                                 compiler,
352
                                                 Mode::ToolRustc,
C
Collins Abitekaniza 已提交
353 354
                                                 host,
                                                 "test",
355
                                                 "src/tools/rustfmt",
356 357
                                                 SourceType::Submodule,
                                                 &[]);
N
Nick Cameron 已提交
358

N
Nick Cameron 已提交
359 360 361
        let dir = testdir(builder, compiler.host);
        t!(fs::create_dir_all(&dir));
        cargo.env("RUSTFMT_TEST_DIR", dir);
N
Nick Cameron 已提交
362 363 364

        builder.add_rustc_lib_path(compiler, &mut cargo);

365 366
        if try_run(builder, &mut cargo) {
            builder.save_toolstate("rustfmt", ToolState::TestPass);
O
Oliver Schneider 已提交
367
        }
N
Nick Cameron 已提交
368 369
    }
}
O
Oliver Schneider 已提交
370 371

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
372
pub struct Miri {
O
Oliver Schneider 已提交
373
    stage: u32,
374 375 376 377 378 379 380 381 382
    host: Interned<String>,
}

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

    fn should_run(run: ShouldRun) -> ShouldRun {
383
        let test_miri = run.builder.config.test_miri;
384 385 386 387 388
        run.path("src/tools/miri").default_condition(test_miri)
    }

    fn make_run(run: RunConfig) {
        run.builder.ensure(Miri {
O
Oliver Schneider 已提交
389
            stage: run.builder.top_stage,
390 391 392 393 394 395
            host: run.target,
        });
    }

    /// Runs `cargo test` for miri.
    fn run(self, builder: &Builder) {
O
Oliver Schneider 已提交
396
        let stage = self.stage;
397
        let host = self.host;
O
Oliver Schneider 已提交
398
        let compiler = builder.compiler(stage, host);
399

400 401 402 403 404 405
        let miri = builder.ensure(tool::Miri {
            compiler,
            target: self.host,
            extra_features: Vec::new(),
        });
        if let Some(miri) = miri {
406 407 408 409 410 411
            let mut cargo = tool::prepare_tool_cargo(builder,
                                                 compiler,
                                                 Mode::ToolRustc,
                                                 host,
                                                 "test",
                                                 "src/tools/miri",
412 413
                                                 SourceType::Submodule,
                                                 &[]);
O
Oliver Schneider 已提交
414 415 416 417 418 419 420 421 422

            // miri tests need to know about the stage sysroot
            cargo.env("MIRI_SYSROOT", builder.sysroot(compiler));
            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);

423 424
            if try_run(builder, &mut cargo) {
                builder.save_toolstate("miri", ToolState::TestPass);
O
Oliver Schneider 已提交
425 426 427 428
            }
        } else {
            eprintln!("failed to test miri: could not build");
        }
429 430 431
    }
}

432 433
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Clippy {
O
Oliver Schneider 已提交
434
    stage: u32,
435 436 437 438 439 440 441 442 443 444 445 446 447 448
    host: Interned<String>,
}

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

    fn should_run(run: ShouldRun) -> ShouldRun {
        run.path("src/tools/clippy")
    }

    fn make_run(run: RunConfig) {
        run.builder.ensure(Clippy {
O
Oliver Schneider 已提交
449
            stage: run.builder.top_stage,
450 451 452 453 454 455
            host: run.target,
        });
    }

    /// Runs `cargo test` for clippy.
    fn run(self, builder: &Builder) {
O
Oliver Schneider 已提交
456
        let stage = self.stage;
457
        let host = self.host;
O
Oliver Schneider 已提交
458
        let compiler = builder.compiler(stage, host);
459

460 461 462 463 464 465
        let clippy = builder.ensure(tool::Clippy {
            compiler,
            target: self.host,
            extra_features: Vec::new(),
        });
        if let Some(clippy) = clippy {
466 467 468 469 470 471
            let mut cargo = tool::prepare_tool_cargo(builder,
                                                 compiler,
                                                 Mode::ToolRustc,
                                                 host,
                                                 "test",
                                                 "src/tools/clippy",
472 473
                                                 SourceType::Submodule,
                                                 &[]);
O
Oliver Schneider 已提交
474 475 476 477 478

            // 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));
S
Santiago Pastorino 已提交
479
            let host_libs = builder
C
Collins Abitekaniza 已提交
480
                .stage_out(compiler, Mode::ToolRustc)
S
Santiago Pastorino 已提交
481
                .join(builder.cargo_dir());
O
Oliver Schneider 已提交
482 483 484 485 486 487
            cargo.env("HOST_LIBS", host_libs);
            // clippy tests need to find the driver
            cargo.env("CLIPPY_DRIVER_PATH", clippy);

            builder.add_rustc_lib_path(compiler, &mut cargo);

488 489
            if try_run(builder, &mut cargo) {
                builder.save_toolstate("clippy-driver", ToolState::TestPass);
O
Oliver Schneider 已提交
490 491 492 493
            }
        } else {
            eprintln!("failed to test clippy: could not build");
        }
494 495
    }
}
N
Nick Cameron 已提交
496

M
Mark Simulacrum 已提交
497
fn path_for_cargo(builder: &Builder, compiler: Compiler) -> OsString {
N
Nick Cameron 已提交
498 499 500
    // 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`.
M
Mark Simulacrum 已提交
501
    let path = builder.sysroot(compiler).join("bin");
N
Nick Cameron 已提交
502 503
    let old_path = env::var_os("PATH").unwrap_or_default();
    env::join_paths(iter::once(path).chain(env::split_paths(&old_path))).expect("")
504
}
505

G
Guillaume Gomez 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
#[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;

    fn should_run(run: ShouldRun) -> ShouldRun {
        run.path("src/tools/rustdoc-themes")
    }

    fn make_run(run: RunConfig) {
        let compiler = run.builder.compiler(run.builder.top_stage, run.host);

523
        run.builder.ensure(RustdocTheme { compiler });
G
Guillaume Gomez 已提交
524 525 526
    }

    fn run(self, builder: &Builder) {
527
        let rustdoc = builder.out.join("bootstrap/debug/rustdoc");
G
Guillaume Gomez 已提交
528 529
        let mut cmd = builder.tool_cmd(Tool::RustdocTheme);
        cmd.arg(rustdoc.to_str().unwrap())
S
Santiago Pastorino 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
            .arg(
                builder
                    .src
                    .join("src/librustdoc/html/static/themes")
                    .to_str()
                    .unwrap(),
            )
            .env("RUSTC_STAGE", self.compiler.stage.to_string())
            .env("RUSTC_SYSROOT", builder.sysroot(self.compiler))
            .env(
                "RUSTDOC_LIBDIR",
                builder.sysroot_libdir(self.compiler, self.compiler.host),
            )
            .env("CFG_RELEASE_CHANNEL", &builder.config.channel)
            .env("RUSTDOC_REAL", builder.rustdoc(self.compiler.host))
            .env("RUSTDOC_CRATE_VERSION", builder.rust_version())
            .env("RUSTC_BOOTSTRAP", "1");
547
        if let Some(linker) = builder.linker(self.compiler.host) {
G
Guillaume Gomez 已提交
548 549
            cmd.env("RUSTC_TARGET_LINKER", linker);
        }
550
        try_run(builder, &mut cmd);
G
Guillaume Gomez 已提交
551 552 553
    }
}

G
Guillaume Gomez 已提交
554 555 556
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct RustdocJS {
    pub host: Interned<String>,
557
    pub target: Interned<String>,
G
Guillaume Gomez 已提交
558 559 560
}

impl Step for RustdocJS {
561
    type Output = ();
G
Guillaume Gomez 已提交
562 563 564 565
    const DEFAULT: bool = true;
    const ONLY_HOSTS: bool = true;

    fn should_run(run: ShouldRun) -> ShouldRun {
566
        run.path("src/test/rustdoc-js")
G
Guillaume Gomez 已提交
567 568 569 570 571
    }

    fn make_run(run: RunConfig) {
        run.builder.ensure(RustdocJS {
            host: run.host,
572
            target: run.target,
G
Guillaume Gomez 已提交
573 574 575
        });
    }

576
    fn run(self, builder: &Builder) {
577 578 579 580 581 582 583 584 585
        if let Some(ref nodejs) = builder.config.nodejs {
            let mut command = Command::new(nodejs);
            command.args(&["src/tools/rustdoc-js/tester.js", &*self.host]);
            builder.ensure(::doc::Std {
                target: self.target,
                stage: builder.top_stage,
            });
            builder.run(&mut command);
        } else {
586
            builder.info(
S
Santiago Pastorino 已提交
587
                "No nodejs found, skipping \"src/test/rustdoc-js\" tests"
588
            );
589
        }
G
Guillaume Gomez 已提交
590 591 592
    }
}

593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
#[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;

    fn should_run(run: ShouldRun) -> ShouldRun {
        run.path("src/test/rustdoc-ui")
    }

    fn make_run(run: RunConfig) {
        let compiler = run.builder.compiler(run.builder.top_stage, run.host);
        run.builder.ensure(RustdocUi {
            host: run.host,
            target: run.target,
            compiler,
        });
    }

    fn run(self, builder: &Builder) {
        builder.ensure(Compiletest {
            compiler: self.compiler,
            target: self.target,
            mode: "ui",
            suite: "rustdoc-ui",
624
            path: None,
625
            compare_mode: None,
626 627 628 629
        })
    }
}

630
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
M
Mark Simulacrum 已提交
631
pub struct Tidy;
632

633
impl Step for Tidy {
634
    type Output = ();
635 636
    const DEFAULT: bool = true;
    const ONLY_HOSTS: bool = true;
637

M
Mark Simulacrum 已提交
638
    /// Runs the `tidy` tool.
639 640 641 642 643
    ///
    /// 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.
    fn run(self, builder: &Builder) {
644
        let mut cmd = builder.tool_cmd(Tool::Tidy);
645 646 647
        cmd.arg(builder.src.join("src"));
        cmd.arg(&builder.initial_cargo);
        if !builder.config.vendor {
648 649
            cmd.arg("--no-vendor");
        }
O
Oliver Schneider 已提交
650
        if !builder.config.verbose_tests {
651 652
            cmd.arg("--quiet");
        }
653

654
        let _folder = builder.fold_output(|| "tidy");
655
        builder.info("tidy check");
656
        try_run(builder, &mut cmd);
657
    }
658

659 660
    fn should_run(run: ShouldRun) -> ShouldRun {
        run.path("src/tools/tidy")
661 662
    }

663
    fn make_run(run: RunConfig) {
M
Mark Simulacrum 已提交
664
        run.builder.ensure(Tidy);
665
    }
666
}
667

668 669
fn testdir(builder: &Builder, host: Interned<String>) -> PathBuf {
    builder.out.join(host).join("test")
670 671
}

672 673 674 675
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 });
    }
676 677
}

678 679 680 681 682 683 684 685
macro_rules! default_test_with_compare_mode {
    ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr,
                   compare_mode: $compare_mode:expr }) => {
        test_with_compare_mode!($name { path: $path, mode: $mode, suite: $suite, default: true,
                                        host: false, compare_mode: $compare_mode });
    }
}

686 687 688 689
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 });
    }
690 691
}

692
macro_rules! test {
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
    ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr, default: $default:expr,
                   host: $host:expr }) => {
        test_definitions!($name { path: $path, mode: $mode, suite: $suite, default: $default,
                                  host: $host, compare_mode: None });
    }
}

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 }) => {
        test_definitions!($name { path: $path, mode: $mode, suite: $suite, default: $default,
                                  host: $host, compare_mode: Some($compare_mode) });
    }
}

macro_rules! test_definitions {
709 710 711 712 713
    ($name:ident {
        path: $path:expr,
        mode: $mode:expr,
        suite: $suite:expr,
        default: $default:expr,
714 715
        host: $host:expr,
        compare_mode: $compare_mode:expr
716 717 718 719 720
    }) => {
        #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
        pub struct $name {
            pub compiler: Compiler,
            pub target: Interned<String>,
721 722
        }

723 724 725 726
        impl Step for $name {
            type Output = ();
            const DEFAULT: bool = $default;
            const ONLY_HOSTS: bool = $host;
727

728
            fn should_run(run: ShouldRun) -> ShouldRun {
729
                run.suite_path($path)
730
            }
731

732 733
            fn make_run(run: RunConfig) {
                let compiler = run.builder.compiler(run.builder.top_stage, run.host);
734

735
                run.builder.ensure($name {
736
                    compiler,
737
                    target: run.target,
738 739
                });
            }
740

741 742 743 744 745 746
            fn run(self, builder: &Builder) {
                builder.ensure(Compiletest {
                    compiler: self.compiler,
                    target: self.target,
                    mode: $mode,
                    suite: $suite,
747
                    path: Some($path),
748
                    compare_mode: $compare_mode,
749 750 751
                })
            }
        }
752 753 754
    }
}

755
default_test_with_compare_mode!(Ui {
756 757
    path: "src/test/ui",
    mode: "ui",
758 759
    suite: "ui",
    compare_mode: "nll"
760 761
});

762
default_test_with_compare_mode!(RunPass {
763 764
    path: "src/test/run-pass",
    mode: "run-pass",
765 766
    suite: "run-pass",
    compare_mode: "nll"
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
});

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

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

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

default_test!(MirOpt {
    path: "src/test/mir-opt",
    mode: "mir-opt",
    suite: "mir-opt"
});

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

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"
});

default_test!(Debuginfo {
    path: "src/test/debuginfo",
T
Tom Tromey 已提交
813
    mode: "debuginfo",
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
    suite: "debuginfo"
});

host_test!(UiFullDeps {
    path: "src/test/ui-fulldeps",
    mode: "ui",
    suite: "ui-fulldeps"
});

host_test!(RunPassFullDeps {
    path: "src/test/run-pass-fulldeps",
    mode: "run-pass",
    suite: "run-pass-fulldeps"
});

host_test!(RunFailFullDeps {
    path: "src/test/run-fail-fulldeps",
    mode: "run-fail",
    suite: "run-fail-fulldeps"
});

host_test!(Rustdoc {
    path: "src/test/rustdoc",
    mode: "rustdoc",
    suite: "rustdoc"
});

test!(Pretty {
    path: "src/test/pretty",
    mode: "pretty",
    suite: "pretty",
    default: false,
    host: true
});
test!(RunPassPretty {
    path: "src/test/run-pass/pretty",
    mode: "pretty",
    suite: "run-pass",
    default: false,
    host: true
});
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
});
test!(RunPassFullDepsPretty {
    path: "src/test/run-pass-fulldeps/pretty",
    mode: "pretty",
    suite: "run-pass-fulldeps",
    default: false,
    host: true
});
test!(RunFailFullDepsPretty {
    path: "src/test/run-fail-fulldeps/pretty",
    mode: "pretty",
    suite: "run-fail-fulldeps",
    default: false,
    host: true
});

E
Eric Huss 已提交
884
default_test!(RunMake {
885 886 887 888 889
    path: "src/test/run-make",
    mode: "run-make",
    suite: "run-make"
});

890 891 892 893 894 895
host_test!(RunMakeFullDeps {
    path: "src/test/run-make-fulldeps",
    mode: "run-make",
    suite: "run-make-fulldeps"
});

896 897 898 899 900 901
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
struct Compiletest {
    compiler: Compiler,
    target: Interned<String>,
    mode: &'static str,
    suite: &'static str,
902
    path: Option<&'static str>,
903
    compare_mode: Option<&'static str>,
904 905 906 907 908 909 910 911 912
}

impl Step for Compiletest {
    type Output = ();

    fn should_run(run: ShouldRun) -> ShouldRun {
        run.never()
    }

913 914 915 916 917 918 919 920 921 922
    /// 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`.
    fn run(self, builder: &Builder) {
        let compiler = self.compiler;
        let target = self.target;
        let mode = self.mode;
        let suite = self.suite;
923

924 925 926
        // Path for test suite
        let suite_path = self.path.unwrap_or("");

927
        // Skip codegen tests if they aren't enabled in configuration.
928
        if !builder.config.codegen_tests && suite == "codegen" {
929 930 931 932
            return;
        }

        if suite == "debuginfo" {
933
            // Skip debuginfo tests on MSVC
934
            if builder.config.build.contains("msvc") {
935 936 937
                return;
            }

T
Tom Tromey 已提交
938 939 940 941 942
            if mode == "debuginfo" {
                return builder.ensure(Compiletest {
                    mode: "debuginfo-both",
                    ..self
                });
943 944 945
            }

            builder.ensure(dist::DebuggerScripts {
946
                sysroot: builder.sysroot(compiler),
S
Santiago Pastorino 已提交
947
                host: target,
948 949 950 951 952 953
            });
        }

        if suite.ends_with("fulldeps") ||
            // FIXME: Does pretty need librustc compiled? Note that there are
            // fulldeps test suites with mode = pretty as well.
954
            mode == "pretty"
S
Santiago Pastorino 已提交
955
        {
956 957 958
            builder.ensure(compile::Rustc { compiler, target });
        }

959 960 961 962
        if builder.no_std(target) == Some(true) {
            // the `test` doesn't compile for no-std targets
            builder.ensure(compile::Std { compiler, target });
        } else {
H
Hideki Sekine 已提交
963 964
            builder.ensure(compile::Test { compiler, target });
        }
965 966

        if builder.no_std(target) == Some(true) {
967
            // for no_std run-make (e.g., thumb*),
968 969 970 971
            // we need a host compiler which is called by cargo.
            builder.ensure(compile::Std { compiler, target: compiler.host });
        }

972 973
        // HACK(eddyb) ensure that `libproc_macro` is available on the host.
        builder.ensure(compile::Test { compiler, target: compiler.host });
974 975
        // Also provide `rust_test_helpers` for the host.
        builder.ensure(native::TestHelpers { target: compiler.host });
976

977
        builder.ensure(native::TestHelpers { target });
978
        builder.ensure(RemoteCopyLibs { compiler, target });
979 980

        let mut cmd = builder.tool_cmd(Tool::Compiletest);
981 982 983 984

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

S
Santiago Pastorino 已提交
985 986 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

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

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

S
Santiago Pastorino 已提交
1002 1003 1004 1005 1006 1007
        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));
1008 1009
        cmd.arg("--mode").arg(mode);
        cmd.arg("--target").arg(target);
1010
        cmd.arg("--host").arg(&*compiler.host);
S
Santiago Pastorino 已提交
1011 1012
        cmd.arg("--llvm-filecheck")
            .arg(builder.llvm_filecheck(builder.config.build));
1013

1014
        if builder.config.cmd.bless() {
1015 1016 1017
            cmd.arg("--bless");
        }

1018 1019 1020 1021 1022 1023 1024
        let compare_mode = builder.config.cmd.compare_mode().or_else(|| {
            if builder.config.test_compare_mode {
                self.compare_mode
            } else {
                None
            }
        });
S
Santiago Pastorino 已提交
1025

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

G
Guillaume Gomez 已提交
1030 1031 1032 1033 1034 1035
        let mut flags = if is_rustdoc_ui {
            Vec::new()
        } else {
            vec!["-Crpath".to_string()]
        };
        if !is_rustdoc_ui {
1036
            if builder.config.rust_optimize_tests {
G
Guillaume Gomez 已提交
1037 1038
                flags.push("-O".to_string());
            }
1039
            if builder.config.rust_debuginfo_tests {
G
Guillaume Gomez 已提交
1040 1041
                flags.push("-g".to_string());
            }
1042
        }
G
Guillaume Gomez 已提交
1043
        flags.push("-Zunstable-options".to_string());
1044
        flags.push(builder.config.cmd.rustc_args().join(" "));
1045

1046
        if let Some(linker) = builder.linker(target) {
O
Oliver Schneider 已提交
1047 1048 1049
            cmd.arg("--linker").arg(linker);
        }

1050 1051 1052 1053 1054
        let mut hostflags = flags.clone();
        hostflags.push(format!(
            "-Lnative={}",
            builder.test_helpers_out(compiler.host).display()
        ));
1055 1056
        cmd.arg("--host-rustcflags").arg(hostflags.join(" "));

S
Shotaro Yamada 已提交
1057
        let mut targetflags = flags;
S
Santiago Pastorino 已提交
1058 1059 1060 1061
        targetflags.push(format!(
            "-Lnative={}",
            builder.test_helpers_out(target).display()
        ));
1062 1063
        cmd.arg("--target-rustcflags").arg(targetflags.join(" "));

1064
        cmd.arg("--docck-python").arg(builder.python());
1065

1066
        if builder.config.build.ends_with("apple-darwin") {
1067 1068 1069 1070 1071
            // 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 {
1072
            cmd.arg("--lldb-python").arg(builder.python());
1073
        }
1074

1075
        if let Some(ref gdb) = builder.config.gdb {
1076 1077
            cmd.arg("--gdb").arg(gdb);
        }
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100

        let run = |cmd: &mut Command| {
            cmd.output().map(|output| {
                String::from_utf8_lossy(&output.stdout)
                    .lines().next().unwrap_or_else(|| {
                        panic!("{:?} failed {:?}", cmd, output)
                    }).to_string()
            })
        };
        let lldb_exe = if builder.config.lldb_enabled && !target.contains("emscripten") {
            // Test against the lldb that was just built.
            builder.llvm_out(target)
                .join("bin")
                .join("lldb")
        } else {
            PathBuf::from("lldb")
        };
        let lldb_version = Command::new(&lldb_exe)
            .arg("--version")
            .output()
            .map(|output| { String::from_utf8_lossy(&output.stdout).to_string() })
            .ok();
        if let Some(ref vers) = lldb_version {
1101
            cmd.arg("--lldb-version").arg(vers);
1102 1103 1104 1105
            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);
            }
1106
        }
1107

1108 1109
        // Get paths from cmd args
        let paths = match &builder.config.cmd {
S
Santiago Pastorino 已提交
1110 1111
            Subcommand::Test { ref paths, .. } => &paths[..],
            _ => &[],
1112 1113 1114
        };

        // Get test-args by striping suite path
S
Santiago Pastorino 已提交
1115 1116
        let mut test_args: Vec<&str> = paths
            .iter()
1117 1118 1119 1120 1121 1122
            .map(|p| {
                match p.strip_prefix(".") {
                    Ok(path) => path,
                    Err(_) => p,
                }
            })
S
Santiago Pastorino 已提交
1123 1124 1125
            .filter(|p| p.starts_with(suite_path) && p.is_file())
            .map(|p| p.strip_prefix(suite_path).unwrap().to_str().unwrap())
            .collect();
1126 1127 1128 1129

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

        cmd.args(&test_args);
1130

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

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

1139
        if builder.config.llvm_enabled {
1140
            let llvm_config = builder.ensure(native::Llvm {
1141
                target: builder.config.build,
1142 1143
                emscripten: false,
            });
1144
            if !builder.config.dry_run {
1145 1146 1147
                let llvm_version = output(Command::new(&llvm_config).arg("--version"));
                cmd.arg("--llvm-version").arg(llvm_version);
            }
1148
            if !builder.is_rust_llvm(target) {
B
bjorn3 已提交
1149 1150 1151 1152 1153
                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 已提交
1154
            if !builder.config.dry_run && suite == "run-make-fulldeps" {
B
bjorn3 已提交
1155 1156
                let llvm_components = output(Command::new(&llvm_config).arg("--components"));
                let llvm_cxxflags = output(Command::new(&llvm_config).arg("--cxxflags"));
S
Santiago Pastorino 已提交
1157 1158 1159 1160 1161
                cmd.arg("--cc")
                    .arg(builder.cc(target))
                    .arg("--cxx")
                    .arg(builder.cxx(target).unwrap())
                    .arg("--cflags")
1162
                    .arg(builder.cflags(target, GitRepo::Rustc).join(" "))
S
Santiago Pastorino 已提交
1163 1164 1165 1166
                    .arg("--llvm-components")
                    .arg(llvm_components.trim())
                    .arg("--llvm-cxxflags")
                    .arg(llvm_cxxflags.trim());
1167
                if let Some(ar) = builder.ar(target) {
O
Oliver Schneider 已提交
1168 1169
                    cmd.arg("--ar").arg(ar);
                }
B
bjorn3 已提交
1170 1171
            }
        }
E
Eric Huss 已提交
1172
        if suite == "run-make-fulldeps" && !builder.config.llvm_enabled {
1173
            builder.info(
S
Santiago Pastorino 已提交
1174
                "Ignoring run-make test suite as they generally don't work without LLVM"
1175
            );
B
bjorn3 已提交
1176 1177 1178
            return;
        }

E
Eric Huss 已提交
1179
        if suite != "run-make-fulldeps" {
S
Santiago Pastorino 已提交
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
            cmd.arg("--cc")
                .arg("")
                .arg("--cxx")
                .arg("")
                .arg("--cflags")
                .arg("")
                .arg("--llvm-components")
                .arg("")
                .arg("--llvm-cxxflags")
                .arg("");
1190
        }
1191

1192
        if builder.remote_tested(target) {
S
Santiago Pastorino 已提交
1193 1194
            cmd.arg("--remote-test-client")
                .arg(builder.tool_exe(Tool::RemoteTestClient));
1195
        }
1196

1197 1198 1199 1200 1201 1202
        // 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") {
1203
            for &(ref k, ref v) in builder.cc[&target].env() {
1204 1205 1206
                if k != "PATH" {
                    cmd.env(k, v);
                }
1207 1208
            }
        }
1209
        cmd.env("RUSTC_BOOTSTRAP", "1");
1210
        builder.add_rust_test_threads(&mut cmd);
1211

1212
        if builder.config.sanitizers {
1213 1214
            cmd.env("SANITIZER_SUPPORT", "1");
        }
1215

1216
        if builder.config.profiler {
1217 1218
            cmd.env("PROFILER_SUPPORT", "1");
        }
1219

1220
        cmd.env("RUST_TEST_TMPDIR", builder.out.join("tmp"));
1221

1222 1223 1224 1225 1226
        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 已提交
1227
                .arg(builder.cc(target).parent().unwrap().parent().unwrap());
1228 1229 1230
        } else {
            cmd.arg("--android-cross-path").arg("");
        }
1231

1232
        builder.ci_env.force_coloring_in_ci(&mut cmd);
1233

1234
        let _folder = builder.fold_output(|| format!("test_{}", suite));
S
Santiago Pastorino 已提交
1235 1236 1237 1238
        builder.info(&format!(
            "Check compiletest suite={} mode={} ({} -> {})",
            suite, mode, &compiler.host, target
        ));
1239 1240
        let _time = util::timeit(&builder);
        try_run(builder, &mut cmd);
1241 1242 1243 1244

        if let Some(compare_mode) = compare_mode {
            cmd.arg("--compare-mode").arg(compare_mode);
            let _folder = builder.fold_output(|| format!("test_{}_{}", suite, compare_mode));
S
Santiago Pastorino 已提交
1245 1246 1247 1248
            builder.info(&format!(
                "Check compiletest suite={} mode={} compare_mode={} ({} -> {})",
                suite, mode, compare_mode, &compiler.host, target
            ));
1249 1250 1251
            let _time = util::timeit(&builder);
            try_run(builder, &mut cmd);
        }
1252
    }
1253
}
1254

1255
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1256
struct DocTest {
1257
    compiler: Compiler,
1258 1259 1260
    path: &'static str,
    name: &'static str,
    is_ext_doc: bool,
1261 1262
}

1263
impl Step for DocTest {
1264 1265
    type Output = ();
    const ONLY_HOSTS: bool = true;
1266

1267
    fn should_run(run: ShouldRun) -> ShouldRun {
1268
        run.never()
1269
    }
M
Mark Simulacrum 已提交
1270

1271 1272
    /// Run `rustdoc --test` for all documentation in `src/doc`.
    ///
1273
    /// This will run all tests in our markdown documentation (e.g., the book)
1274 1275 1276 1277
    /// located in `src/doc`. The `rustdoc` that's run is the one that sits next to
    /// `compiler`.
    fn run(self, builder: &Builder) {
        let compiler = self.compiler;
1278

S
Santiago Pastorino 已提交
1279 1280 1281 1282
        builder.ensure(compile::Test {
            compiler,
            target: compiler.host,
        });
1283

1284 1285
        // Do a breadth-first traversal of the `src/doc` directory and just run
        // tests for all files that end in `*.md`
1286 1287 1288
        let mut stack = vec![builder.src.join(self.path)];
        let _time = util::timeit(&builder);
        let _folder = builder.fold_output(|| format!("test_{}", self.name));
1289

1290
        let mut files = Vec::new();
1291 1292 1293
        while let Some(p) = stack.pop() {
            if p.is_dir() {
                stack.extend(t!(p.read_dir()).map(|p| t!(p).path()));
S
Santiago Pastorino 已提交
1294
                continue;
1295 1296 1297 1298 1299 1300 1301
            }

            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
1302
            // guaranteed to builder. We don't care if it doesn't build, so skip it.
1303 1304 1305 1306
            if p.to_str().map_or(false, |p| p.contains("nostarch")) {
                continue;
            }

1307 1308 1309 1310 1311
            files.push(p);
        }

        files.sort();

1312
        let mut toolstate = ToolState::TestPass;
1313
        for file in files {
1314 1315
            if !markdown_test(builder, compiler, &file) {
                toolstate = ToolState::TestFail;
1316
            }
1317
        }
1318 1319 1320
        if self.is_ext_doc {
            builder.save_toolstate(self.name, toolstate);
        }
1321 1322 1323
    }
}

1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
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;

                fn should_run(run: ShouldRun) -> ShouldRun {
                    run.path($path)
                }

                fn make_run(run: RunConfig) {
                    run.builder.ensure($name {
                        compiler: run.builder.compiler(run.builder.top_stage, run.host),
                    });
                }

                fn run(self, builder: &Builder) {
                    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;
1364
    RustcBook, "src/doc/rustc", "rustc", default=true;
1365 1366 1367 1368 1369
    RustByExample, "src/doc/rust-by-example", "rust-by-example", default=false;
    TheBook, "src/doc/book", "book", default=false;
    UnstableBook, "src/doc/unstable-book", "unstable-book", default=true;
);

1370 1371 1372
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct ErrorIndex {
    compiler: Compiler,
1373
}
1374

1375
impl Step for ErrorIndex {
1376
    type Output = ();
1377 1378 1379
    const DEFAULT: bool = true;
    const ONLY_HOSTS: bool = true;

1380 1381
    fn should_run(run: ShouldRun) -> ShouldRun {
        run.path("src/tools/error_index_generator")
1382 1383
    }

1384 1385 1386
    fn make_run(run: RunConfig) {
        run.builder.ensure(ErrorIndex {
            compiler: run.builder.compiler(run.builder.top_stage, run.host),
1387 1388
        });
    }
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398

    /// Run the error index generator tool to execute the tests located in the error
    /// 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`.
    fn run(self, builder: &Builder) {
        let compiler = self.compiler;

S
Santiago Pastorino 已提交
1399 1400 1401 1402
        builder.ensure(compile::Std {
            compiler,
            target: compiler.host,
        });
1403

1404
        let dir = testdir(builder, compiler.host);
1405 1406 1407
        t!(fs::create_dir_all(&dir));
        let output = dir.join("error-index.md");

1408 1409 1410
        let mut tool = builder.tool_cmd(Tool::ErrorIndex);
        tool.arg("markdown")
            .arg(&output)
1411 1412
            .env("CFG_BUILD", &builder.config.build)
            .env("RUSTC_ERROR_METADATA_DST", builder.extended_error_dir());
1413

1414 1415 1416 1417
        let _folder = builder.fold_output(|| "test_error_index");
        builder.info(&format!("Testing error-index stage{}", compiler.stage));
        let _time = util::timeit(&builder);
        builder.run(&mut tool);
1418
        markdown_test(builder, compiler, &output);
1419
    }
1420 1421
}

1422
fn markdown_test(builder: &Builder, compiler: Compiler, markdown: &Path) -> bool {
1423 1424
    match fs::read_to_string(markdown) {
        Ok(contents) => {
1425 1426 1427 1428
            if !contents.contains("```") {
                return true;
            }
        }
S
Santiago Pastorino 已提交
1429
        Err(_) => {}
1430 1431
    }

1432
    builder.info(&format!("doc tests for: {}", markdown.display()));
1433
    let mut cmd = builder.rustdoc_cmd(compiler.host);
1434
    builder.add_rust_test_threads(&mut cmd);
1435 1436
    cmd.arg("--test");
    cmd.arg(markdown);
1437
    cmd.env("RUSTC_BOOTSTRAP", "1");
1438

1439
    let test_args = builder.config.cmd.test_args().join(" ");
1440 1441
    cmd.arg("--test-args").arg(test_args);

O
Oliver Schneider 已提交
1442
    if builder.config.verbose_tests {
1443
        try_run(builder, &mut cmd)
O
Oliver Schneider 已提交
1444 1445
    } else {
        try_run_quiet(builder, &mut cmd)
1446
    }
1447
}
1448

1449
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
M
Mark Simulacrum 已提交
1450
pub struct CrateLibrustc {
1451 1452
    compiler: Compiler,
    target: Interned<String>,
1453
    test_kind: TestKind,
1454
    krate: Interned<String>,
1455 1456
}

M
Mark Simulacrum 已提交
1457
impl Step for CrateLibrustc {
1458 1459 1460 1461
    type Output = ();
    const DEFAULT: bool = true;
    const ONLY_HOSTS: bool = true;

1462 1463
    fn should_run(run: ShouldRun) -> ShouldRun {
        run.krate("rustc-main")
1464 1465
    }

1466 1467 1468
    fn make_run(run: RunConfig) {
        let builder = run.builder;
        let compiler = builder.compiler(builder.top_stage, run.host);
1469

1470 1471
        for krate in builder.in_tree_crates("rustc-main") {
            if run.path.ends_with(&krate.path) {
1472
                let test_kind = builder.kind.into();
1473

1474 1475 1476 1477 1478 1479
                builder.ensure(CrateLibrustc {
                    compiler,
                    target: run.target,
                    test_kind,
                    krate: krate.name,
                });
1480 1481 1482 1483 1484
            }
        }
    }

    fn run(self, builder: &Builder) {
M
Mark Simulacrum 已提交
1485
        builder.ensure(Crate {
1486 1487
            compiler: self.compiler,
            target: self.target,
C
Collins Abitekaniza 已提交
1488
            mode: Mode::Rustc,
1489 1490 1491 1492 1493 1494
            test_kind: self.test_kind,
            krate: self.krate,
        });
    }
}

1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
#[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 = ();

    fn should_run(run: ShouldRun) -> ShouldRun {
1507
        run.path("src/librustc_asan")
1508 1509 1510 1511 1512 1513 1514 1515 1516
            .path("src/librustc_lsan")
            .path("src/librustc_msan")
            .path("src/librustc_tsan")
    }

    fn make_run(run: RunConfig) {
        let builder = run.builder;
        let compiler = builder.compiler(builder.top_stage, run.host);

1517
        let test_kind = builder.kind.into();
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536

        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),
            },
        });
    }

    fn run(self, builder: &Builder) {
        builder.ensure(Crate {
            compiler: self.compiler,
            target: self.target,
C
Collins Abitekaniza 已提交
1537
            mode: Mode::Std,
1538 1539 1540 1541 1542 1543
            test_kind: self.test_kind,
            krate: INTERNER.intern_str(self.krate),
        });
    }
}

K
kennytm 已提交
1544
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
M
Mark Simulacrum 已提交
1545
pub struct Crate {
K
kennytm 已提交
1546 1547 1548 1549 1550
    pub compiler: Compiler,
    pub target: Interned<String>,
    pub mode: Mode,
    pub test_kind: TestKind,
    pub krate: Interned<String>,
1551
}
1552

M
Mark Simulacrum 已提交
1553
impl Step for Crate {
1554
    type Output = ();
1555 1556
    const DEFAULT: bool = true;

1557 1558 1559 1560
    fn should_run(mut run: ShouldRun) -> ShouldRun {
        let builder = run.builder;
        run = run.krate("test");
        for krate in run.builder.in_tree_crates("std") {
S
Santiago Pastorino 已提交
1561 1562 1563 1564
            if krate.is_local(&run.builder)
                && !(krate.name.starts_with("rustc_") && krate.name.ends_with("san"))
                && krate.name != "dlmalloc"
            {
1565 1566 1567 1568
                run = run.path(krate.local_path(&builder).to_str().unwrap());
            }
        }
        run
1569 1570
    }

1571 1572 1573
    fn make_run(run: RunConfig) {
        let builder = run.builder;
        let compiler = builder.compiler(builder.top_stage, run.host);
1574

1575
        let make = |mode: Mode, krate: &CargoCrate| {
1576
            let test_kind = builder.kind.into();
1577

M
Mark Simulacrum 已提交
1578
            builder.ensure(Crate {
1579 1580
                compiler,
                target: run.target,
1581 1582
                mode,
                test_kind,
1583
                krate: krate.name,
1584 1585 1586
            });
        };

1587 1588
        for krate in builder.in_tree_crates("std") {
            if run.path.ends_with(&krate.local_path(&builder)) {
C
Collins Abitekaniza 已提交
1589
                make(Mode::Std, krate);
1590
            }
1591 1592 1593
        }
        for krate in builder.in_tree_crates("test") {
            if run.path.ends_with(&krate.local_path(&builder)) {
C
Collins Abitekaniza 已提交
1594
                make(Mode::Test, krate);
1595 1596 1597
            }
        }
    }
1598

1599 1600
    /// Run all unit tests plus documentation tests for a given crate defined
    /// by a `Cargo.toml` (single manifest)
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
    ///
    /// 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`.
    fn run(self, builder: &Builder) {
        let compiler = self.compiler;
        let target = self.target;
        let mode = self.mode;
        let test_kind = self.test_kind;
        let krate = self.krate;

1614 1615
        builder.ensure(compile::Test { compiler, target });
        builder.ensure(RemoteCopyLibs { compiler, target });
A
Alex Crichton 已提交
1616 1617 1618 1619 1620

        // 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.
1621
        let compiler = if builder.force_use_stage1(compiler, target) {
A
Alex Crichton 已提交
1622 1623 1624 1625 1626 1627
            builder.compiler(1, compiler.host)
        } else {
            compiler.clone()
        };

        let mut cargo = builder.cargo(compiler, mode, target, test_kind.subcommand());
1628
        match mode {
C
Collins Abitekaniza 已提交
1629
            Mode::Std => {
1630
                compile::std_cargo(builder, &compiler, target, &mut cargo);
1631
            }
C
Collins Abitekaniza 已提交
1632
            Mode::Test => {
1633
                compile::test_cargo(builder, &compiler, target, &mut cargo);
1634
            }
C
Collins Abitekaniza 已提交
1635
            Mode::Rustc => {
1636
                builder.ensure(compile::Rustc { compiler, target });
1637
                compile::rustc_cargo(builder, &mut cargo);
1638 1639 1640 1641 1642 1643 1644 1645 1646
            }
            _ => 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.
1647
        if test_kind.subcommand() == "test" && !builder.fail_fast {
1648 1649
            cargo.arg("--no-fail-fast");
        }
K
kennytm 已提交
1650
        match builder.doc_tests {
K
kennytm 已提交
1651
            DocTests::Only => {
K
kennytm 已提交
1652 1653
                cargo.arg("--doc");
            }
K
kennytm 已提交
1654
            DocTests::No => {
K
kennytm 已提交
1655 1656
                cargo.args(&["--lib", "--bins", "--examples", "--tests", "--benches"]);
            }
K
kennytm 已提交
1657
            DocTests::Yes => {}
1658
        }
1659

1660
        cargo.arg("-p").arg(krate);
1661

1662 1663 1664 1665 1666 1667
        // 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();
1668
        dylib_path.insert(0, PathBuf::from(&*builder.sysroot_libdir(compiler, target)));
1669 1670 1671
        cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());

        cargo.arg("--");
1672
        cargo.args(&builder.config.cmd.test_args());
1673

O
Oliver Schneider 已提交
1674
        if !builder.config.verbose_tests {
1675 1676
            cargo.arg("--quiet");
        }
1677

1678
        if target.contains("emscripten") {
S
Santiago Pastorino 已提交
1679 1680 1681 1682 1683 1684 1685 1686
            cargo.env(
                format!("CARGO_TARGET_{}_RUNNER", envify(&target)),
                builder
                    .config
                    .nodejs
                    .as_ref()
                    .expect("nodejs not configured"),
            );
O
Oliver Schneider 已提交
1687
        } else if target.starts_with("wasm32") {
1688 1689 1690
            // Warn about running tests without the `wasm_syscall` feature enabled.
            // The javascript shim implements the syscall interface so that test
            // output can be correctly reported.
1691
            if !builder.config.wasm_syscall {
1692
                builder.info(
S
Santiago Pastorino 已提交
1693 1694
                    "Libstd was built without `wasm_syscall` feature enabled: \
                     test output may not be visible."
1695
                );
1696 1697
            }

O
Oliver Schneider 已提交
1698 1699 1700 1701
            // On the wasm32-unknown-unknown target we're using LTO which is
            // incompatible with `-C prefer-dynamic`, so disable that here
            cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");

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

1720
        let _folder = builder.fold_output(|| {
S
Santiago Pastorino 已提交
1721 1722 1723 1724 1725 1726
            format!(
                "{}_stage{}-{}",
                test_kind.subcommand(),
                compiler.stage,
                krate
            )
1727
        });
S
Santiago Pastorino 已提交
1728 1729 1730 1731
        builder.info(&format!(
            "{} {} stage{} ({} -> {})",
            test_kind, krate, compiler.stage, &compiler.host, target
        ));
1732 1733
        let _time = util::timeit(&builder);
        try_run(builder, &mut cargo);
1734 1735
    }
}
1736

M
Mark Simulacrum 已提交
1737
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1738
pub struct CrateRustdoc {
M
Mark Simulacrum 已提交
1739 1740 1741 1742
    host: Interned<String>,
    test_kind: TestKind,
}

1743
impl Step for CrateRustdoc {
M
Mark Simulacrum 已提交
1744 1745 1746 1747 1748
    type Output = ();
    const DEFAULT: bool = true;
    const ONLY_HOSTS: bool = true;

    fn should_run(run: ShouldRun) -> ShouldRun {
1749
        run.paths(&["src/librustdoc", "src/tools/rustdoc"])
M
Mark Simulacrum 已提交
1750 1751 1752 1753 1754
    }

    fn make_run(run: RunConfig) {
        let builder = run.builder;

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

1757
        builder.ensure(CrateRustdoc {
M
Mark Simulacrum 已提交
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
            host: run.host,
            test_kind,
        });
    }

    fn run(self, builder: &Builder) {
        let test_kind = self.test_kind;

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

C
Collins Abitekaniza 已提交
1770 1771
        let mut cargo = tool::prepare_tool_cargo(builder,
                                                 compiler,
C
Collins Abitekaniza 已提交
1772
                                                 Mode::ToolRustc,
C
Collins Abitekaniza 已提交
1773 1774
                                                 target,
                                                 test_kind.subcommand(),
1775
                                                 "src/tools/rustdoc",
1776 1777
                                                 SourceType::InTree,
                                                 &[]);
1778
        if test_kind.subcommand() == "test" && !builder.fail_fast {
M
Mark Simulacrum 已提交
1779 1780 1781 1782 1783 1784
            cargo.arg("--no-fail-fast");
        }

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

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

O
Oliver Schneider 已提交
1787
        if !builder.config.verbose_tests {
M
Mark Simulacrum 已提交
1788 1789 1790
            cargo.arg("--quiet");
        }

S
Santiago Pastorino 已提交
1791 1792 1793 1794 1795 1796
        let _folder = builder
            .fold_output(|| format!("{}_stage{}-rustdoc", test_kind.subcommand(), compiler.stage));
        builder.info(&format!(
            "{} rustdoc stage{} ({} -> {})",
            test_kind, compiler.stage, &compiler.host, target
        ));
1797
        let _time = util::timeit(&builder);
M
Mark Simulacrum 已提交
1798

1799
        try_run(builder, &mut cargo);
M
Mark Simulacrum 已提交
1800 1801 1802
    }
}

1803
fn envify(s: &str) -> String {
S
Santiago Pastorino 已提交
1804 1805
    s.chars()
        .map(|c| match c {
1806 1807
            '-' => '_',
            c => c,
S
Santiago Pastorino 已提交
1808 1809 1810
        })
        .flat_map(|c| c.to_uppercase())
        .collect()
1811 1812
}

1813 1814 1815 1816 1817 1818 1819 1820 1821
/// 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.
///
/// Most of the time this is a noop. For some steps such as shipping data to
/// 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.
1822 1823 1824 1825
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct RemoteCopyLibs {
    compiler: Compiler,
    target: Interned<String>,
1826
}
1827

1828
impl Step for RemoteCopyLibs {
1829
    type Output = ();
1830

1831 1832
    fn should_run(run: ShouldRun) -> ShouldRun {
        run.never()
1833 1834
    }

1835 1836 1837
    fn run(self, builder: &Builder) {
        let compiler = self.compiler;
        let target = self.target;
1838
        if !builder.remote_tested(target) {
S
Santiago Pastorino 已提交
1839
            return;
1840 1841
        }

1842 1843
        builder.ensure(compile::Test { compiler, target });

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

1847 1848 1849 1850
        let server = builder.ensure(tool::RemoteTestServer {
            compiler: compiler.with_stage(0),
            target,
        });
1851 1852

        // Spawn the emulator and wait for it to come online
1853
        let tool = builder.tool_exe(Tool::RemoteTestClient);
1854 1855
        let mut cmd = Command::new(&tool);
        cmd.arg("spawn-emulator")
S
Santiago Pastorino 已提交
1856 1857 1858
            .arg(target)
            .arg(&server)
            .arg(builder.out.join("tmp"));
1859
        if let Some(rootfs) = builder.qemu_rootfs(target) {
1860 1861
            cmd.arg(rootfs);
        }
1862
        builder.run(&mut cmd);
1863 1864

        // Push all our dylibs to the emulator
1865
        for f in t!(builder.sysroot_libdir(compiler, target).read_dir()) {
1866 1867 1868
            let f = t!(f);
            let name = f.file_name().into_string().unwrap();
            if util::is_dylib(&name) {
S
Santiago Pastorino 已提交
1869
                builder.run(Command::new(&tool).arg("push").arg(f.path()));
1870
            }
1871 1872 1873 1874
        }
    }
}

1875
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1876
pub struct Distcheck;
A
Alex Crichton 已提交
1877

1878
impl Step for Distcheck {
1879 1880
    type Output = ();

1881 1882
    fn should_run(run: ShouldRun) -> ShouldRun {
        run.path("distcheck")
1883 1884
    }

M
Mark Simulacrum 已提交
1885 1886 1887 1888
    fn make_run(run: RunConfig) {
        run.builder.ensure(Distcheck);
    }

1889 1890
    /// Run "distcheck", a 'make check' from a tarball
    fn run(self, builder: &Builder) {
1891
        builder.info("Distcheck");
1892
        let dir = builder.out.join("tmp").join("distcheck");
1893 1894 1895
        let _ = fs::remove_dir_all(&dir);
        t!(fs::create_dir_all(&dir));

M
Mark Simulacrum 已提交
1896 1897 1898 1899
        // Guarantee that these are built before we begin running.
        builder.ensure(dist::PlainSourceTarball);
        builder.ensure(dist::Src);

1900 1901
        let mut cmd = Command::new("tar");
        cmd.arg("-xzf")
S
Santiago Pastorino 已提交
1902 1903 1904
            .arg(builder.ensure(dist::PlainSourceTarball))
            .arg("--strip-components=1")
            .current_dir(&dir);
1905
        builder.run(&mut cmd);
S
Santiago Pastorino 已提交
1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916
        builder.run(
            Command::new("./configure")
                .args(&builder.config.configure_args)
                .arg("--enable-vendor")
                .current_dir(&dir),
        );
        builder.run(
            Command::new(build_helper::make(&builder.config.build))
                .arg("check")
                .current_dir(&dir),
        );
1917 1918

        // Now make sure that rust-src has all of libstd's dependencies
1919
        builder.info("Distcheck rust-src");
1920
        let dir = builder.out.join("tmp").join("distcheck-src");
1921 1922 1923 1924 1925
        let _ = fs::remove_dir_all(&dir);
        t!(fs::create_dir_all(&dir));

        let mut cmd = Command::new("tar");
        cmd.arg("-xzf")
S
Santiago Pastorino 已提交
1926 1927 1928
            .arg(builder.ensure(dist::Src))
            .arg("--strip-components=1")
            .current_dir(&dir);
1929
        builder.run(&mut cmd);
1930 1931

        let toml = dir.join("rust-src/lib/rustlib/src/rust/src/libstd/Cargo.toml");
S
Santiago Pastorino 已提交
1932 1933 1934 1935 1936 1937 1938
        builder.run(
            Command::new(&builder.initial_cargo)
                .arg("generate-lockfile")
                .arg("--manifest-path")
                .arg(&toml)
                .current_dir(&dir),
        );
1939
    }
A
Alex Crichton 已提交
1940
}
1941

1942
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1943 1944
pub struct Bootstrap;

1945
impl Step for Bootstrap {
1946
    type Output = ();
1947 1948
    const DEFAULT: bool = true;
    const ONLY_HOSTS: bool = true;
1949 1950 1951

    /// Test the build system itself
    fn run(self, builder: &Builder) {
1952
        let mut cmd = Command::new(&builder.initial_cargo);
1953
        cmd.arg("test")
S
Santiago Pastorino 已提交
1954 1955 1956 1957 1958
            .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);
1959 1960 1961 1962 1963 1964
        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);
        }
1965
        if !builder.fail_fast {
1966 1967
            cmd.arg("--no-fail-fast");
        }
1968
        cmd.arg("--").args(&builder.config.cmd.test_args());
1969 1970 1971
        // 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");
1972
        try_run(builder, &mut cmd);
1973
    }
1974

1975 1976
    fn should_run(run: ShouldRun) -> ShouldRun {
        run.path("src/bootstrap")
1977 1978
    }

1979 1980
    fn make_run(run: RunConfig) {
        run.builder.ensure(Bootstrap);
1981
    }
1982
}