lib.rs 56.1 KB
Newer Older
1
//! Implementation of rustbuild, the Rust build system.
2
//!
3 4 5
//! This module, and its descendants, are the implementation of the Rust build
//! system. Most of this build system is backed by Cargo but the outer layer
//! here serves as the ability to orchestrate calling Cargo, sequencing Cargo
6
//! builds, building artifacts like LLVM, etc. The goals of rustbuild are:
7
//!
8 9 10 11 12 13 14 15
//! * To be an easily understandable, easily extensible, and maintainable build
//!   system.
//! * Leverage standard tools in the Rust ecosystem to build the compiler, aka
//!   crates.io and Cargo.
//! * A standard interface to build across all platforms, including MSVC
//!
//! ## Architecture
//!
16 17 18 19 20 21
//! The build system defers most of the complicated logic managing invocations
//! of rustc and rustdoc to Cargo itself. However, moving through various stages
//! and copying artifacts is still necessary for it to do. Each time rustbuild
//! is invoked, it will iterate through the list of predefined steps and execute
//! each serially in turn if it matches the paths passed or is a default rule.
//! For each step rustbuild relies on the step internally being incremental and
22 23 24 25 26 27
//! parallel. Note, though, that the `-j` parameter to rustbuild gets forwarded
//! to appropriate test harnesses and such.
//!
//! Most of the "meaty" steps that matter are backed by Cargo, which does indeed
//! have its own parallelism and incremental management. Later steps, like
//! tests, aren't incremental and simply run the entire suite currently.
28 29
//! However, compiletest itself tries to avoid running tests when the artifacts
//! that are involved (mainly the compiler) haven't changed.
30
//!
31
//! When you execute `x.py build`, the steps executed are:
32 33
//!
//! * First, the python script is run. This will automatically download the
34
//!   stage0 rustc and cargo according to `src/stage0.json`, or use the cached
35 36 37 38
//!   versions if they're available. These are then used to compile rustbuild
//!   itself (using Cargo). Finally, control is then transferred to rustbuild.
//!
//! * Rustbuild takes over, performs sanity checks, probes the environment,
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
//!   reads configuration, and starts executing steps as it reads the command
//!   line arguments (paths) or going through the default rules.
//!
//!   The build output will be something like the following:
//!
//!   Building stage0 std artifacts
//!   Copying stage0 std
//!   Building stage0 test artifacts
//!   Copying stage0 test
//!   Building stage0 compiler artifacts
//!   Copying stage0 rustc
//!   Assembling stage1 compiler
//!   Building stage1 std artifacts
//!   Copying stage1 std
//!   Building stage1 test artifacts
//!   Copying stage1 test
//!   Building stage1 compiler artifacts
//!   Copying stage1 rustc
//!   Assembling stage2 compiler
//!   Uplifting stage1 std
//!   Uplifting stage1 test
//!   Uplifting stage1 rustc
//!
//! Let's disect that a little:
//!
//! ## Building stage0 {std,test,compiler} artifacts
//!
//! These steps use the provided (downloaded, usually) compiler to compile the
//! local Rust source into libraries we can use.
//!
//! ## Copying stage0 {std,test,rustc}
//!
//! This copies the build output from Cargo into
A
Alexander Regueiro 已提交
72
//! `build/$HOST/stage0-sysroot/lib/rustlib/$ARCH/lib`. FIXME: this step's
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
//! documentation should be expanded -- the information already here may be
//! incorrect.
//!
//! ## Assembling stage1 compiler
//!
//! This copies the libraries we built in "building stage0 ... artifacts" into
//! the stage1 compiler's lib directory. These are the host libraries that the
//! compiler itself uses to run. These aren't actually used by artifacts the new
//! compiler generates. This step also copies the rustc and rustdoc binaries we
//! generated into build/$HOST/stage/bin.
//!
//! The stage1/bin/rustc is a fully functional compiler, but it doesn't yet have
//! any libraries to link built binaries or libraries to. The next 3 steps will
//! provide those libraries for it; they are mostly equivalent to constructing
//! the stage1/bin compiler so we don't go through them individually.
//!
89
//! ## Uplifting stage1 {std,test,rustc}
90 91 92 93 94 95 96
//!
//! This step copies the libraries from the stage1 compiler sysroot into the
//! stage2 compiler. This is done to avoid rebuilding the compiler; libraries
//! we'd build in this step should be identical (in function, if not necessarily
//! identical on disk) so there's no need to recompile the compiler again. Note
//! that if you want to, you can enable the full-bootstrap option to change this
//! behavior.
97 98 99 100 101 102 103 104
//!
//! Each step is driven by a separate Cargo project and rustbuild orchestrates
//! copying files between steps and otherwise preparing for Cargo to run.
//!
//! ## Further information
//!
//! More documentation can be found in each respective module below, and you can
//! also check out the `src/bootstrap/README.md` file for more information.
105

M
Mark Rousskov 已提交
106 107
use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet};
A
Alex Crichton 已提交
108
use std::env;
109
use std::fs::{self, File};
M
Mark Rousskov 已提交
110
use std::path::{Path, PathBuf};
111
use std::process::{self, Command};
112
use std::str;
113

T
Tom Tromey 已提交
114 115 116 117 118
#[cfg(unix)]
use std::os::unix::fs::symlink as symlink_file;
#[cfg(windows)]
use std::os::windows::fs::symlink_file;

M
Mark Rousskov 已提交
119
use build_helper::{mtime, output, run, run_suppressed, t, try_run, try_run_suppressed};
120
use filetime::FileTime;
121

B
bjorn3 已提交
122
use crate::builder::Kind;
123
use crate::config::{LlvmLibunwind, TargetSelection};
124
use crate::util::{exe, libdir, CiEnv};
125

M
Mark Rousskov 已提交
126 127
mod builder;
mod cache;
A
Alex Crichton 已提交
128
mod cc_detect;
129 130 131 132 133 134 135 136
mod channel;
mod check;
mod clean;
mod compile;
mod config;
mod dist;
mod doc;
mod flags;
M
Mark Rousskov 已提交
137
mod format;
138
mod install;
M
Mark Rousskov 已提交
139
mod metadata;
140
mod native;
141
mod run;
142
mod sanity;
J
Joshua Nelson 已提交
143
mod setup;
144
mod tarball;
M
Mark Rousskov 已提交
145
mod test;
146
mod tool;
147
mod toolstate;
M
Mark Rousskov 已提交
148
pub mod util;
149 150 151 152

#[cfg(windows)]
mod job;

153
#[cfg(all(unix, not(target_os = "haiku")))]
154
mod job {
L
ljedrz 已提交
155
    pub unsafe fn setup(build: &mut crate::Build) {
156
        if build.config.low_priority {
Q
QuietMisdreavus 已提交
157
            libc::setpriority(libc::PRIO_PGRP as _, 0, 10);
158 159 160 161
        }
    }
}

162
#[cfg(any(target_os = "haiku", target_os = "hermit", not(any(unix, windows))))]
163
mod job {
M
Mark Rousskov 已提交
164
    pub unsafe fn setup(_build: &mut crate::Build) {}
165 166
}

M
Mark Rousskov 已提交
167
use crate::cache::{Interned, INTERNER};
L
ljedrz 已提交
168
pub use crate::config::Config;
J
Joshua Nelson 已提交
169
pub use crate::flags::Subcommand;
170

J
Jorge Aparicio 已提交
171
const LLVM_TOOLS: &[&str] = &[
D
dalance 已提交
172 173 174 175
    "llvm-cov",      // used to generate coverage report
    "llvm-nm",       // used to inspect binaries; it shows symbol names, their sizes and visibility
    "llvm-objcopy",  // used to transform ELFs into binary format which flashing tools consume
    "llvm-objdump",  // used to disassemble programs
J
Jorge Aparicio 已提交
176
    "llvm-profdata", // used to inspect and merge files generated by profiles
D
dalance 已提交
177 178 179 180
    "llvm-readobj",  // used to get information from ELFs/objects that the other tools don't provide
    "llvm-size",     // used to prints the size of the linker sections of a program
    "llvm-strip",    // used to discard symbols from binary files to reduce their size
    "llvm-ar",       // used for creating and modifying archive files
Z
Zachary Catlin 已提交
181
    "llvm-as",       // used to convert LLVM assembly to LLVM bitcode
D
dalance 已提交
182 183 184
    "llvm-dis",      // used to disassemble LLVM bitcode
    "llc",           // used to compile LLVM bytecode
    "opt",           // used to optimize LLVM bytecode
J
Jorge Aparicio 已提交
185
];
186

187 188
pub const VERSION: usize = 2;

189 190 191 192 193
/// A structure representing a Rust compiler.
///
/// Each compiler has a `stage` that it is associated with and a `host` that
/// corresponds to the platform the compiler runs on. This structure is used as
/// a parameter to many methods below.
M
Mark Simulacrum 已提交
194
#[derive(Eq, PartialOrd, Ord, PartialEq, Clone, Copy, Hash, Debug)]
195
pub struct Compiler {
196
    stage: u32,
197
    host: TargetSelection,
198 199
}

K
kennytm 已提交
200
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
K
kennytm 已提交
201
pub enum DocTests {
A
Alexander Regueiro 已提交
202
    /// Run normal tests and doc tests (default).
K
kennytm 已提交
203
    Yes,
A
Alexander Regueiro 已提交
204
    /// Do not run any doc tests.
K
kennytm 已提交
205
    No,
A
Alexander Regueiro 已提交
206
    /// Only run doc tests.
K
kennytm 已提交
207 208 209
    Only,
}

210 211 212 213 214
pub enum GitRepo {
    Rustc,
    Llvm,
}

215 216 217 218 219 220 221 222 223 224 225
/// Global configuration for the build system.
///
/// This structure transitively contains all configuration for the build system.
/// All filesystem-encoded configuration is in `config`, all flags are in
/// `flags`, and then parsed or probed information is listed in the keys below.
///
/// This structure is a parameter of almost all methods in the build system,
/// although most functions are implemented as free functions rather than
/// methods specifically on this structure itself (to make it easier to
/// organize).
pub struct Build {
A
Alexander Regueiro 已提交
226
    /// User-specified configuration from `config.toml`.
227 228
    config: Config,

229 230 231
    // Version information
    version: String,

A
Alexander Regueiro 已提交
232
    // Properties derived from the above configuration
233 234
    src: PathBuf,
    out: PathBuf,
235 236
    rust_info: channel::GitInfo,
    cargo_info: channel::GitInfo,
A
Alex Crichton 已提交
237
    rls_info: channel::GitInfo,
A
Aleksey Kladov 已提交
238
    rust_analyzer_info: channel::GitInfo,
239
    clippy_info: channel::GitInfo,
240
    miri_info: channel::GitInfo,
N
Nick Cameron 已提交
241
    rustfmt_info: channel::GitInfo,
242
    in_tree_llvm_info: channel::GitInfo,
243
    local_rebuild: bool,
244
    fail_fast: bool,
K
kennytm 已提交
245
    doc_tests: DocTests,
M
Mark Simulacrum 已提交
246
    verbosity: usize,
247

A
Alexander Regueiro 已提交
248
    // Targets for which to build
249 250 251
    build: TargetSelection,
    hosts: Vec<TargetSelection>,
    targets: Vec<TargetSelection>,
252

253
    // Stage 0 (downloaded) compiler, lld and cargo or their local rust equivalents
254 255
    initial_rustc: PathBuf,
    initial_cargo: PathBuf,
256
    initial_lld: PathBuf,
O
O01eg 已提交
257
    initial_libdir: PathBuf,
258

259
    // Runtime state filled in later on
260
    // C/C++ compilers and archiver for all targets
261 262 263 264
    cc: HashMap<TargetSelection, cc::Tool>,
    cxx: HashMap<TargetSelection, cc::Tool>,
    ar: HashMap<TargetSelection, PathBuf>,
    ranlib: HashMap<TargetSelection, PathBuf>,
A
Alexander Regueiro 已提交
265
    // Miscellaneous
266
    crates: HashMap<Interned<String>, Crate>,
267
    is_sudo: bool,
268
    ci_env: CiEnv,
269
    delayed_failures: RefCell<Vec<String>>,
270
    prerelease_version: Cell<Option<u32>>,
M
Mark Rousskov 已提交
271
    tool_artifacts:
272
        RefCell<HashMap<TargetSelection, HashMap<String, (&'static str, PathBuf, Vec<String>)>>>,
273 274 275 276
}

#[derive(Debug)]
struct Crate {
277
    name: Interned<String>,
278
    deps: HashSet<Interned<String>>,
279
    path: PathBuf,
280 281
}

282 283 284 285 286 287
impl Crate {
    fn local_path(&self, build: &Build) -> PathBuf {
        self.path.strip_prefix(&build.config.src).unwrap().into()
    }
}

288 289 290 291 292 293 294 295 296 297 298
/// When building Rust various objects are handled differently.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum DependencyType {
    /// Libraries originating from proc-macros.
    Host,
    /// Typical Rust libraries.
    Target,
    /// Non Rust libraries and objects shipped to ease usage of certain targets.
    TargetSelfContained,
}

299 300 301 302
/// The various "modes" of invoking Cargo.
///
/// These entries currently correspond to the various output directories of the
/// build system, with each mod generating output in a different directory.
K
kennytm 已提交
303
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
304
pub enum Mode {
M
Mark Simulacrum 已提交
305
    /// Build the standard library, placing output in the "stageN-std" directory.
C
Collins Abitekaniza 已提交
306
    Std,
307

308
    /// Build librustc, and compiler libraries, placing output in the "stageN-rustc" directory.
C
Collins Abitekaniza 已提交
309
    Rustc,
310

311 312 313
    /// Build a codegen backend for rustc, placing the output in the "stageN-codegen" directory.
    Codegen,

314 315 316 317
    /// Build a tool, placing output in the "stage0-bootstrap-tools"
    /// directory. This is for miscellaneous sets of tools that are built
    /// using the bootstrap stage0 compiler in its entirety (target libraries
    /// and all). Typically these tools compile with stable Rust.
318 319
    ToolBootstrap,

320 321 322
    /// Build a tool which uses the locally built std, placing output in the
    /// "stageN-tools" directory. Its usage is quite rare, mainly used by
    /// compiletest which needs libtest.
A
Alex Crichton 已提交
323
    ToolStd,
324 325 326 327 328

    /// Build a tool which uses the locally built rustc and the target std,
    /// placing the output in the "stageN-tools" directory. This is used for
    /// anything that needs a fully functional rustc, such as rustdoc, clippy,
    /// cargo, rls, rustfmt, miri, etc.
C
Collins Abitekaniza 已提交
329
    ToolRustc,
330 331
}

C
Collins Abitekaniza 已提交
332 333
impl Mode {
    pub fn is_tool(&self) -> bool {
334
        matches!(self, Mode::ToolBootstrap | Mode::ToolRustc | Mode::ToolStd)
C
Collins Abitekaniza 已提交
335
    }
336 337 338 339

    pub fn must_support_dlopen(&self) -> bool {
        matches!(self, Mode::Std | Mode::Codegen)
    }
C
Collins Abitekaniza 已提交
340 341
}

342 343 344 345 346
pub enum CLang {
    C,
    Cxx,
}

347 348 349 350 351
impl Build {
    /// Creates a new set of build configuration from the `flags` on the command
    /// line and the filesystem `config`.
    ///
    /// By default all build output will be placed in the current directory.
M
Mark Simulacrum 已提交
352 353
    pub fn new(config: Config) -> Build {
        let src = config.src.clone();
354
        let out = config.out.clone();
355

356
        let is_sudo = match env::var_os("SUDO_USER") {
M
Mark Rousskov 已提交
357 358 359 360
            Some(sudo_user) => match env::var_os("USER") {
                Some(user) => user != sudo_user,
                None => false,
            },
361 362
            None => false,
        };
J
Josh Stone 已提交
363 364 365 366 367

        let ignore_git = config.ignore_git;
        let rust_info = channel::GitInfo::new(ignore_git, &src);
        let cargo_info = channel::GitInfo::new(ignore_git, &src.join("src/tools/cargo"));
        let rls_info = channel::GitInfo::new(ignore_git, &src.join("src/tools/rls"));
A
Aleksey Kladov 已提交
368 369
        let rust_analyzer_info =
            channel::GitInfo::new(ignore_git, &src.join("src/tools/rust-analyzer"));
J
Josh Stone 已提交
370 371 372 373 374 375
        let clippy_info = channel::GitInfo::new(ignore_git, &src.join("src/tools/clippy"));
        let miri_info = channel::GitInfo::new(ignore_git, &src.join("src/tools/miri"));
        let rustfmt_info = channel::GitInfo::new(ignore_git, &src.join("src/tools/rustfmt"));

        // we always try to use git for LLVM builds
        let in_tree_llvm_info = channel::GitInfo::new(false, &src.join("src/llvm-project"));
376

O
O01eg 已提交
377 378
        let initial_target_libdir_str = if config.dry_run {
            "/dummy/lib/path/to/lib/".to_string()
379 380 381 382
        } else {
            output(
                Command::new(&config.initial_rustc)
                    .arg("--target")
383
                    .arg(config.build.rustc_target_arg())
384 385 386 387
                    .arg("--print")
                    .arg("target-libdir"),
            )
        };
O
O01eg 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
        let initial_target_dir = Path::new(&initial_target_libdir_str).parent().unwrap();
        let initial_lld = initial_target_dir.join("bin").join("rust-lld");

        let initial_sysroot = if config.dry_run {
            "/dummy".to_string()
        } else {
            output(Command::new(&config.initial_rustc).arg("--print").arg("sysroot"))
        };
        let initial_libdir = initial_target_dir
            .parent()
            .unwrap()
            .parent()
            .unwrap()
            .strip_prefix(initial_sysroot.trim())
            .unwrap()
            .to_path_buf();
404

405 406 407 408
        let version = std::fs::read_to_string(src.join("src").join("version"))
            .expect("failed to read src/version");
        let version = version.trim();

409
        let mut build = Build {
410 411
            initial_rustc: config.initial_rustc.clone(),
            initial_cargo: config.initial_cargo.clone(),
412
            initial_lld,
O
O01eg 已提交
413
            initial_libdir,
414
            local_rebuild: config.local_rebuild,
M
Mark Simulacrum 已提交
415
            fail_fast: config.cmd.fail_fast(),
416
            doc_tests: config.cmd.doc_tests(),
M
Mark Simulacrum 已提交
417
            verbosity: config.verbose,
418

M
Mark Simulacrum 已提交
419 420 421
            build: config.build,
            hosts: config.hosts.clone(),
            targets: config.targets.clone(),
422

423
            config,
424
            version: version.to_string(),
425 426
            src,
            out,
427

428 429 430
            rust_info,
            cargo_info,
            rls_info,
A
Aleksey Kladov 已提交
431
            rust_analyzer_info,
432
            clippy_info,
433
            miri_info,
N
Nick Cameron 已提交
434
            rustfmt_info,
435
            in_tree_llvm_info,
436 437
            cc: HashMap::new(),
            cxx: HashMap::new(),
438
            ar: HashMap::new(),
439
            ranlib: HashMap::new(),
440
            crates: HashMap::new(),
441
            is_sudo,
442
            ci_env: CiEnv::current(),
443
            delayed_failures: RefCell::new(Vec::new()),
444
            prerelease_version: Cell::new(None),
445
            tool_artifacts: Default::default(),
446 447 448 449
        };

        build.verbose("finding compilers");
        cc_detect::find(&mut build);
450 451 452 453 454 455 456
        // When running `setup`, the profile is about to change, so any requirements we have now may
        // be different on the next invocation. Don't check for them until the next time x.py is
        // run. This is ok because `setup` never runs any build commands, so it won't fail if commands are missing.
        if !matches!(build.config.cmd, Subcommand::Setup { .. }) {
            build.verbose("running sanity check");
            sanity::check(&mut build);
        }
M
Mark Simulacrum 已提交
457 458 459

        // If local-rust is the same major.minor as the current version, then force a
        // local-rebuild
M
Mark Rousskov 已提交
460 461
        let local_version_verbose =
            output(Command::new(&build.initial_rustc).arg("--version").arg("--verbose"));
M
Mark Simulacrum 已提交
462
        let local_release = local_version_verbose
M
Mark Rousskov 已提交
463
            .lines()
L
Lzu Tao 已提交
464
            .filter_map(|x| x.strip_prefix("release:"))
M
Mark Rousskov 已提交
465 466 467
            .next()
            .unwrap()
            .trim();
468
        if local_release.split('.').take(2).eq(version.split('.').take(2)) {
M
Mark Simulacrum 已提交
469 470
            build.verbose(&format!("auto-detected local-rebuild {}", local_release));
            build.local_rebuild = true;
471
        }
M
Mark Simulacrum 已提交
472

473 474 475 476
        build.verbose("learning about cargo");
        metadata::build(&mut build);

        build
477 478
    }

479 480 481 482 483 484 485 486 487
    // modified from `check_submodule` and `update_submodule` in bootstrap.py
    /// Given a path to the directory of a submodule, update it.
    ///
    /// `relative_path` should be relative to the root of the git repository, not an absolute path.
    pub(crate) fn update_submodule(&self, relative_path: &Path) {
        fn dir_is_empty(dir: &Path) -> bool {
            t!(std::fs::read_dir(dir)).next().is_none()
        }

488
        if !self.config.submodules(&self.rust_info) {
489 490 491 492 493 494 495
            return;
        }

        let absolute_path = self.config.src.join(relative_path);

        // NOTE: The check for the empty directory is here because when running x.py the first time,
        // the submodule won't be checked out. Check it out now so we can build it.
496
        if !channel::GitInfo::new(false, &absolute_path).is_git() && !dir_is_empty(&absolute_path) {
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
            return;
        }

        // check_submodule
        if self.config.fast_submodules {
            let checked_out_hash = output(
                Command::new("git").args(&["rev-parse", "HEAD"]).current_dir(&absolute_path),
            );
            // update_submodules
            let recorded = output(
                Command::new("git")
                    .args(&["ls-tree", "HEAD"])
                    .arg(relative_path)
                    .current_dir(&self.config.src),
            );
            let actual_hash = recorded
                .split_whitespace()
                .nth(2)
                .unwrap_or_else(|| panic!("unexpected output `{}`", recorded));

            // update_submodule
            if actual_hash == checked_out_hash.trim_end() {
                // already checked out
                return;
            }
        }

        println!("Updating submodule {}", relative_path.display());
        self.run(
            Command::new("git")
                .args(&["submodule", "-q", "sync"])
                .arg(relative_path)
                .current_dir(&self.config.src),
        );

        // Try passing `--progress` to start, then run git again without if that fails.
        let update = |progress: bool| {
            let mut git = Command::new("git");
535
            git.args(&["submodule", "update", "--init", "--recursive", "--depth=1"]);
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
            if progress {
                git.arg("--progress");
            }
            git.arg(relative_path).current_dir(&self.config.src);
            git
        };
        // NOTE: doesn't use `try_run` because this shouldn't print an error if it fails.
        if !update(true).status().map_or(false, |status| status.success()) {
            self.run(&mut update(false));
        }

        self.run(Command::new("git").args(&["reset", "-q", "--hard"]).current_dir(&absolute_path));
        self.run(Command::new("git").args(&["clean", "-qdfx"]).current_dir(absolute_path));
    }

    /// If any submodule has been initialized already, sync it unconditionally.
    /// This avoids contributors checking in a submodule change by accident.
    pub fn maybe_update_submodules(&self) {
        // WARNING: keep this in sync with the submodules hard-coded in bootstrap.py
        const BOOTSTRAP_SUBMODULES: &[&str] = &[
            "src/tools/rust-installer",
            "src/tools/cargo",
            "src/tools/rls",
            "src/tools/miri",
            "library/backtrace",
            "library/stdarch",
        ];
        // Avoid running git when there isn't a git checkout.
564
        if !self.config.submodules(&self.rust_info) {
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
            return;
        }
        let output = output(
            Command::new("git")
                .args(&["config", "--file"])
                .arg(&self.config.src.join(".gitmodules"))
                .args(&["--get-regexp", "path"]),
        );
        for line in output.lines() {
            // Look for `submodule.$name.path = $path`
            // Sample output: `submodule.src/rust-installer.path src/tools/rust-installer`
            let submodule = Path::new(line.splitn(2, ' ').nth(1).unwrap());
            // avoid updating submodules twice
            if !BOOTSTRAP_SUBMODULES.iter().any(|&p| Path::new(p) == submodule)
                && channel::GitInfo::new(false, submodule).is_git()
            {
                self.update_submodule(submodule);
            }
        }
    }

586 587 588
    /// Executes the entire build, as configured by the flags and configuration.
    pub fn build(&mut self) {
        unsafe {
589
            job::setup(self);
590 591
        }

592
        self.maybe_update_submodules();
593

594 595
        if let Subcommand::Format { check, paths } = &self.config.cmd {
            return format::format(self, *check, &paths);
A
Adam Perry 已提交
596 597
        }

T
Tommy Ip 已提交
598 599
        if let Subcommand::Clean { all } = self.config.cmd {
            return clean::clean(self, all);
600 601
        }

A
Antoine Martin 已提交
602 603
        if let Subcommand::Setup { profile } = &self.config.cmd {
            return setup::setup(&self.config.src, *profile);
J
Joshua Nelson 已提交
604 605
        }

606 607 608 609 610 611
        {
            let builder = builder::Builder::new(&self);
            if let Some(path) = builder.paths.get(0) {
                if path == Path::new("nonexistent/path/to/trigger/cargo/metadata") {
                    return;
                }
612 613
            }
        }
614 615

        if !self.config.dry_run {
616
            {
617 618
                self.config.dry_run = true;
                let builder = builder::Builder::new(&self);
619 620
                builder.execute_cli();
            }
621 622
            self.config.dry_run = false;
            let builder = builder::Builder::new(&self);
623
            builder.execute_cli();
624 625
        } else {
            let builder = builder::Builder::new(&self);
626
            builder.execute_cli();
627
        }
628 629 630 631 632 633 634 635 636 637

        // Check for postponed failures from `test --no-fail-fast`.
        let failures = self.delayed_failures.borrow();
        if failures.len() > 0 {
            println!("\n{} command(s) did not execute successfully:\n", failures.len());
            for failure in failures.iter() {
                println!("  - {}\n", failure);
            }
            process::exit(1);
        }
638 639 640 641 642
    }

    /// Clear out `dir` if `input` is newer.
    ///
    /// After this executes, it will also ensure that `dir` exists.
643
    fn clear_if_dirty(&self, dir: &Path, input: &Path) -> bool {
644
        let stamp = dir.join(".stamp");
645
        let mut cleared = false;
646 647 648
        if mtime(&stamp) < mtime(input) {
            self.verbose(&format!("Dirty - {}", dir.display()));
            let _ = fs::remove_dir_all(dir);
649
            cleared = true;
650
        } else if stamp.exists() {
651
            return cleared;
652 653 654
        }
        t!(fs::create_dir_all(dir));
        t!(File::create(stamp));
655
        cleared
656 657
    }

A
Alexander Regueiro 已提交
658
    /// Gets the space-separated set of activated features for the standard
659
    /// library.
660
    fn std_features(&self, target: TargetSelection) -> String {
661
        let mut features = "panic-unwind".to_string();
J
Jorge Aparicio 已提交
662

663
        match self.config.llvm_libunwind {
664 665 666
            LlvmLibunwind::InTree => features.push_str(" llvm-libunwind"),
            LlvmLibunwind::System => features.push_str(" system-llvm-libunwind"),
            LlvmLibunwind::No => {}
667
        }
668 669 670
        if self.config.backtrace {
            features.push_str(" backtrace");
        }
671
        if self.config.profiler_enabled(target) {
672 673
            features.push_str(" profiler");
        }
M
Mark Simulacrum 已提交
674
        features
675 676
    }

A
Alexander Regueiro 已提交
677
    /// Gets the space-separated set of activated features for the compiler.
B
bjorn3 已提交
678
    fn rustc_features(&self, kind: Kind) -> String {
679 680 681 682
        let mut features = String::new();
        if self.config.jemalloc {
            features.push_str("jemalloc");
        }
B
bjorn3 已提交
683
        if self.config.llvm_enabled() || kind == Kind::Check {
684 685
            features.push_str(" llvm");
        }
G
Gus Wynn 已提交
686 687 688 689 690 691 692

        // If debug logging is on, then we want the default for tracing:
        // https://github.com/tokio-rs/tracing/blob/3dd5c03d907afdf2c39444a29931833335171554/tracing/src/level_filters.rs#L26
        // which is everything (including debug/trace/etc.)
        // if its unset, if debug_assertions is on, then debug_logging will also be on
        // as well as tracing *ignoring* this feature when debug_assertions is on
        if !self.config.rust_debug_logging {
G
Gus Wynn 已提交
693
            features.push_str(" max_level_info");
G
Gus Wynn 已提交
694 695
        }

696
        features
697 698 699 700 701
    }

    /// Component directory that Cargo will produce output into (e.g.
    /// release/debug)
    fn cargo_dir(&self) -> &'static str {
M
Mark Rousskov 已提交
702
        if self.config.rust_optimize { "release" } else { "debug" }
703 704
    }

705
    fn tools_dir(&self, compiler: Compiler) -> PathBuf {
706 707 708 709
        let out = self
            .out
            .join(&*compiler.host.triple)
            .join(format!("stage{}-tools-bin", compiler.stage));
710 711 712 713
        t!(fs::create_dir_all(&out));
        out
    }

714 715 716 717
    /// Returns the root directory for all output generated in a particular
    /// stage when running with a particular host compiler.
    ///
    /// The mode indicates what the root directory is for.
718
    fn stage_out(&self, compiler: Compiler, mode: Mode) -> PathBuf {
719
        let suffix = match mode {
C
Collins Abitekaniza 已提交
720 721
            Mode::Std => "-std",
            Mode::Rustc => "-rustc",
722
            Mode::Codegen => "-codegen",
723
            Mode::ToolBootstrap => "-bootstrap-tools",
724
            Mode::ToolStd | Mode::ToolRustc => "-tools",
725
        };
726
        self.out.join(&*compiler.host.triple).join(format!("stage{}{}", compiler.stage, suffix))
727 728 729
    }

    /// Returns the root output directory for all Cargo output in a given stage,
F
Fourchaux 已提交
730
    /// running a particular compiler, whether or not we're building the
731
    /// standard library, and targeting the specified architecture.
732 733
    fn cargo_out(&self, compiler: Compiler, mode: Mode, target: TargetSelection) -> PathBuf {
        self.stage_out(compiler, mode).join(&*target.triple).join(self.cargo_dir())
734 735 736 737 738 739
    }

    /// Root output directory for LLVM compiled for `target`
    ///
    /// Note that if LLVM is configured externally then the directory returned
    /// will likely be empty.
740 741
    fn llvm_out(&self, target: TargetSelection) -> PathBuf {
        self.out.join(&*target.triple).join("llvm")
742 743
    }

744 745
    fn lld_out(&self, target: TargetSelection) -> PathBuf {
        self.out.join(&*target.triple).join("lld")
746 747
    }

748
    /// Output directory for all documentation for a target
749 750
    fn doc_out(&self, target: TargetSelection) -> PathBuf {
        self.out.join(&*target.triple).join("doc")
751 752
    }

G
Guillaume Gomez 已提交
753 754 755 756
    fn test_out(&self, target: TargetSelection) -> PathBuf {
        self.out.join(&*target.triple).join("test")
    }

757
    /// Output directory for all documentation for a target
758 759
    fn compiler_doc_out(&self, target: TargetSelection) -> PathBuf {
        self.out.join(&*target.triple).join("compiler-doc")
760 761
    }

762
    /// Output directory for some generated md crate documentation for a target (temporary)
763 764
    fn md_doc_out(&self, target: TargetSelection) -> Interned<PathBuf> {
        INTERNER.intern_path(self.out.join(&*target.triple).join("md-doc"))
765 766
    }

A
Alexander Regueiro 已提交
767
    /// Returns `true` if no custom `llvm-config` is set for the specified target.
768 769
    ///
    /// If no custom `llvm-config` was specified then Rust's llvm will be used.
770
    fn is_rust_llvm(&self, target: TargetSelection) -> bool {
771 772 773 774
        if self.config.llvm_from_ci && target == self.config.build {
            return true;
        }

775
        match self.config.target_config.get(&target) {
776
            Some(ref c) => c.llvm_config.is_none(),
M
Mark Rousskov 已提交
777
            None => true,
778 779 780
        }
    }

781
    /// Returns the path to `FileCheck` binary for the specified target
782
    fn llvm_filecheck(&self, target: TargetSelection) -> PathBuf {
783
        let target_config = self.config.target_config.get(&target);
784 785 786
        if let Some(s) = target_config.and_then(|c| c.llvm_filecheck.as_ref()) {
            s.to_path_buf()
        } else if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) {
S
Seo Sanghyeon 已提交
787
            let llvm_bindir = output(Command::new(s).arg("--bindir"));
788
            let filecheck = Path::new(llvm_bindir.trim()).join(exe("FileCheck", target));
789 790 791 792 793 794
            if filecheck.exists() {
                filecheck
            } else {
                // On Fedora the system LLVM installs FileCheck in the
                // llvm subdirectory of the libdir.
                let llvm_libdir = output(Command::new(s).arg("--libdir"));
M
Mark Rousskov 已提交
795
                let lib_filecheck =
796
                    Path::new(llvm_libdir.trim()).join("llvm").join(exe("FileCheck", target));
797 798 799 800 801 802 803 804 805
                if lib_filecheck.exists() {
                    lib_filecheck
                } else {
                    // Return the most normal file name, even though
                    // it doesn't exist, so that any error message
                    // refers to that.
                    filecheck
                }
            }
806
        } else {
807
            let base = self.llvm_out(self.config.build).join("build");
808
            let base = if !self.ninja() && self.config.build.contains("msvc") {
J
John Kåre Alsaker 已提交
809 810 811 812 813 814 815 816 817
                if self.config.llvm_optimize {
                    if self.config.llvm_release_debuginfo {
                        base.join("RelWithDebInfo")
                    } else {
                        base.join("Release")
                    }
                } else {
                    base.join("Debug")
                }
818
            } else {
J
John Kåre Alsaker 已提交
819 820
                base
            };
821
            base.join("bin").join(exe("FileCheck", target))
822 823 824
        }
    }

825
    /// Directory for libraries built from C/C++ code and shared between stages.
826 827
    fn native_dir(&self, target: TargetSelection) -> PathBuf {
        self.out.join(&*target.triple).join("native")
828 829
    }

830 831
    /// Root output directory for rust_test_helpers library compiled for
    /// `target`
832
    fn test_helpers_out(&self, target: TargetSelection) -> PathBuf {
833
        self.native_dir(target).join("rust-test-helpers")
834 835
    }

836 837 838 839 840 841 842
    /// Adds the `RUST_TEST_THREADS` env var if necessary
    fn add_rust_test_threads(&self, cmd: &mut Command) {
        if env::var_os("RUST_TEST_THREADS").is_none() {
            cmd.env("RUST_TEST_THREADS", self.jobs().to_string());
        }
    }

843 844
    /// Returns the libdir of the snapshot compiler.
    fn rustc_snapshot_libdir(&self) -> PathBuf {
845
        self.rustc_snapshot_sysroot().join(libdir(self.config.build))
846 847 848 849
    }

    /// Returns the sysroot of the snapshot compiler.
    fn rustc_snapshot_sysroot(&self) -> &Path {
850
        self.initial_rustc.parent().unwrap().parent().unwrap()
851 852 853 854
    }

    /// Runs a command, printing out nice contextual information if it fails.
    fn run(&self, cmd: &mut Command) {
M
Mark Rousskov 已提交
855 856 857
        if self.config.dry_run {
            return;
        }
858
        self.verbose(&format!("running: {:?}", cmd));
859
        run(cmd, self.is_verbose())
860 861
    }

862 863
    /// Runs a command, printing out nice contextual information if it fails.
    fn run_quiet(&self, cmd: &mut Command) {
M
Mark Rousskov 已提交
864 865 866
        if self.config.dry_run {
            return;
        }
867
        self.verbose(&format!("running: {:?}", cmd));
868
        run_suppressed(cmd)
869 870
    }

871 872 873 874
    /// Runs a command, printing out nice contextual information if it fails.
    /// Exits if the command failed to execute at all, otherwise returns its
    /// `status.success()`.
    fn try_run(&self, cmd: &mut Command) -> bool {
M
Mark Rousskov 已提交
875 876 877
        if self.config.dry_run {
            return true;
        }
878
        self.verbose(&format!("running: {:?}", cmd));
879
        try_run(cmd, self.is_verbose())
880 881 882 883 884 885
    }

    /// Runs a command, printing out nice contextual information if it fails.
    /// Exits if the command failed to execute at all, otherwise returns its
    /// `status.success()`.
    fn try_run_quiet(&self, cmd: &mut Command) -> bool {
M
Mark Rousskov 已提交
886 887 888
        if self.config.dry_run {
            return true;
        }
889
        self.verbose(&format!("running: {:?}", cmd));
890
        try_run_suppressed(cmd)
891 892
    }

M
Mark Simulacrum 已提交
893 894 895 896
    pub fn is_verbose(&self) -> bool {
        self.verbosity > 0
    }

897 898
    /// Prints a message if this build is configured in verbose mode.
    fn verbose(&self, msg: &str) {
M
Mark Simulacrum 已提交
899
        if self.is_verbose() {
900 901 902 903
            println!("{}", msg);
        }
    }

904 905 906 907 908 909 910 911 912 913 914
    pub fn is_verbose_than(&self, level: usize) -> bool {
        self.verbosity > level
    }

    /// Prints a message if this build is configured in more verbose mode than `level`.
    fn verbose_than(&self, level: usize, msg: &str) {
        if self.is_verbose_than(level) {
            println!("{}", msg);
        }
    }

915
    fn info(&self, msg: &str) {
M
Mark Rousskov 已提交
916 917 918
        if self.config.dry_run {
            return;
        }
919 920 921
        println!("{}", msg);
    }

922 923 924
    /// Returns the number of parallel jobs that have been configured for this
    /// build.
    fn jobs(&self) -> u32 {
M
Mark Simulacrum 已提交
925
        self.config.jobs.unwrap_or_else(|| num_cpus::get() as u32)
926 927
    }

928
    fn debuginfo_map_to(&self, which: GitRepo) -> Option<String> {
929
        if !self.config.rust_remap_debuginfo {
M
Mark Rousskov 已提交
930
            return None;
931 932
        }

933
        match which {
934
            GitRepo::Rustc => {
935
                let sha = self.rust_sha().unwrap_or(&self.version);
936
                Some(format!("/rustc/{}", sha))
937
            }
938 939
            GitRepo::Llvm => Some(String::from("/rustc/llvm")),
        }
940 941
    }

942
    /// Returns the path to the C compiler for the target specified.
943
    fn cc(&self, target: TargetSelection) -> &Path {
944
        self.cc[&target].path()
945 946 947 948
    }

    /// Returns a list of flags to pass to the C compiler for the target
    /// specified.
949 950 951 952 953 954
    fn cflags(&self, target: TargetSelection, which: GitRepo, c: CLang) -> Vec<String> {
        let base = match c {
            CLang::C => &self.cc[&target],
            CLang::Cxx => &self.cxx[&target],
        };

955
        // Filter out -O and /O (the optimization flags) that we picked up from
A
Alex Crichton 已提交
956
        // cc-rs because the build scripts will determine that for themselves.
957
        let mut base = base
M
Mark Rousskov 已提交
958 959 960 961 962
            .args()
            .iter()
            .map(|s| s.to_string_lossy().into_owned())
            .filter(|s| !s.starts_with("-O") && !s.starts_with("/O"))
            .collect::<Vec<String>>();
963

964
        // If we're compiling on macOS then we add a few unconditional flags
965 966
        // indicating that we want libc++ (more filled out than libstdc++) and
        // we want to compile for 10.7. This way we can ensure that
967
        // LLVM/etc are all properly compiled.
968 969 970
        if target.contains("apple-darwin") {
            base.push("-stdlib=libc++".into());
        }
971 972

        // Work around an apparently bad MinGW / GCC optimization,
S
Smitty 已提交
973
        // See: https://lists.llvm.org/pipermail/cfe-dev/2016-December/051980.html
974
        // See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78936
975
        if &*target.triple == "i686-pc-windows-gnu" {
976 977
            base.push("-fno-omit-frame-pointer".into());
        }
978

979 980
        if let Some(map_to) = self.debuginfo_map_to(which) {
            let map = format!("{}={}", self.src.display(), map_to);
M
Mark Rousskov 已提交
981
            let cc = self.cc(target);
982
            if cc.ends_with("clang") || cc.ends_with("gcc") {
983
                base.push(format!("-fdebug-prefix-map={}", map));
984 985
            } else if cc.ends_with("clang-cl.exe") {
                base.push("-Xclang".into());
986
                base.push(format!("-fdebug-prefix-map={}", map));
987 988
            }
        }
M
Mark Simulacrum 已提交
989
        base
990 991 992
    }

    /// Returns the path to the `ar` archive utility for the target specified.
993
    fn ar(&self, target: TargetSelection) -> Option<&Path> {
994 995 996
        self.ar.get(&target).map(|p| &**p)
    }

997
    /// Returns the path to the `ranlib` utility for the target specified.
998
    fn ranlib(&self, target: TargetSelection) -> Option<&Path> {
999 1000 1001
        self.ranlib.get(&target).map(|p| &**p)
    }

1002
    /// Returns the path to the C++ compiler for the target specified.
1003
    fn cxx(&self, target: TargetSelection) -> Result<&Path, String> {
1004
        match self.cxx.get(&target) {
1005
            Some(p) => Ok(p.path()),
M
Mark Rousskov 已提交
1006 1007 1008
            None => {
                Err(format!("target `{}` is not configured as a host, only as a target", target))
            }
1009
        }
1010 1011
    }

1012
    /// Returns the path to the linker for the given target if it needs to be overridden.
1013
    fn linker(&self, target: TargetSelection) -> Option<&Path> {
M
Mark Rousskov 已提交
1014 1015
        if let Some(linker) = self.config.target_config.get(&target).and_then(|c| c.linker.as_ref())
        {
1016
            Some(linker)
P
Pang, Baoshan 已提交
1017 1018 1019 1020
        } else if target.contains("vxworks") {
            // need to use CXX compiler as linker to resolve the exception functions
            // that are only existed in CXX libraries
            Some(self.cxx[&target].path())
M
Mark Rousskov 已提交
1021
        } else if target != self.config.build
1022
            && util::use_host_linker(target)
M
Mark Rousskov 已提交
1023 1024
            && !target.contains("msvc")
        {
1025
            Some(self.cc(target))
1026
        } else if self.config.use_lld && !self.is_fuse_ld_lld(target) && self.build == target {
1027
            Some(&self.initial_lld)
1028 1029
        } else {
            None
1030
        }
A
Alex Crichton 已提交
1031
    }
1032

1033 1034 1035 1036 1037 1038
    // LLD is used through `-fuse-ld=lld` rather than directly.
    // Only MSVC targets use LLD directly at the moment.
    fn is_fuse_ld_lld(&self, target: TargetSelection) -> bool {
        self.config.use_lld && !target.contains("msvc")
    }

1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
    fn lld_flags(&self, target: TargetSelection) -> impl Iterator<Item = String> {
        let mut options = [None, None];

        if self.config.use_lld {
            if self.is_fuse_ld_lld(target) {
                options[0] = Some("-Clink-arg=-fuse-ld=lld".to_string());
            }

            let threads = if target.contains("windows") { "/threads:1" } else { "--threads=1" };
            options[1] = Some(format!("-Clink-arg=-Wl,{}", threads));
        }

1051
        IntoIterator::into_iter(options).flatten()
1052 1053
    }

1054
    /// Returns if this target should statically link the C runtime, if specified
1055
    fn crt_static(&self, target: TargetSelection) -> Option<bool> {
1056 1057 1058
        if target.contains("pc-windows-msvc") {
            Some(true)
        } else {
M
Mark Rousskov 已提交
1059
            self.config.target_config.get(&target).and_then(|t| t.crt_static)
1060
        }
1061 1062
    }

1063
    /// Returns the "musl root" for this `target`, if defined
1064
    fn musl_root(&self, target: TargetSelection) -> Option<&Path> {
M
Mark Rousskov 已提交
1065 1066 1067
        self.config
            .target_config
            .get(&target)
1068
            .and_then(|t| t.musl_root.as_ref())
1069
            .or_else(|| self.config.musl_root.as_ref())
1070 1071
            .map(|p| &**p)
    }
1072

1073
    /// Returns the "musl libdir" for this `target`.
1074
    fn musl_libdir(&self, target: TargetSelection) -> Option<PathBuf> {
1075 1076 1077 1078 1079 1080 1081
        let t = self.config.target_config.get(&target)?;
        if let libdir @ Some(_) = &t.musl_libdir {
            return libdir.clone();
        }
        self.musl_root(target).map(|root| root.join("lib"))
    }

1082
    /// Returns the sysroot for the wasi target, if defined
1083
    fn wasi_root(&self, target: TargetSelection) -> Option<&Path> {
M
Mark Rousskov 已提交
1084
        self.config.target_config.get(&target).and_then(|t| t.wasi_root.as_ref()).map(|p| &**p)
1085 1086
    }

A
Alexander Regueiro 已提交
1087
    /// Returns `true` if this is a no-std `target`, if defined
1088
    fn no_std(&self, target: TargetSelection) -> Option<bool> {
M
Mark Rousskov 已提交
1089
        self.config.target_config.get(&target).map(|t| t.no_std)
1090 1091
    }

A
Alexander Regueiro 已提交
1092
    /// Returns `true` if the target will be tested using the `remote-test-client`
1093
    /// and `remote-test-server` binaries.
1094
    fn remote_tested(&self, target: TargetSelection) -> bool {
M
Mark Rousskov 已提交
1095 1096 1097
        self.qemu_rootfs(target).is_some()
            || target.contains("android")
            || env::var_os("TEST_DEVICE_ADDR").is_some()
1098 1099
    }

1100 1101 1102 1103 1104
    /// Returns the root of the "rootfs" image that this target will be using,
    /// if one was configured.
    ///
    /// If `Some` is returned then that means that tests for this target are
    /// emulated with QEMU and binaries will need to be shipped to the emulator.
1105
    fn qemu_rootfs(&self, target: TargetSelection) -> Option<&Path> {
M
Mark Rousskov 已提交
1106
        self.config.target_config.get(&target).and_then(|t| t.qemu_rootfs.as_ref()).map(|p| &**p)
1107 1108
    }

1109 1110 1111 1112
    /// Path to the python interpreter to use
    fn python(&self) -> &Path {
        self.config.python.as_ref().unwrap()
    }
1113

1114 1115 1116 1117 1118
    /// Temporary directory that extended error information is emitted to.
    fn extended_error_dir(&self) -> PathBuf {
        self.out.join("tmp/extended-error-metadata")
    }

1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
    /// Tests whether the `compiler` compiling for `target` should be forced to
    /// use a stage1 compiler instead.
    ///
    /// Currently, by default, the build system does not perform a "full
    /// bootstrap" by default where we compile the compiler three times.
    /// Instead, we compile the compiler two times. The final stage (stage2)
    /// just copies the libraries from the previous stage, which is what this
    /// method detects.
    ///
    /// Here we return `true` if:
    ///
    /// * The build isn't performing a full bootstrap
    /// * The `compiler` is in the final stage, 2
    /// * We're not cross-compiling, so the artifacts are already available in
    ///   stage1
    ///
    /// When all of these conditions are met the build will lift artifacts from
    /// the previous stage forward.
1137
    fn force_use_stage1(&self, compiler: Compiler, target: TargetSelection) -> bool {
M
Mark Rousskov 已提交
1138 1139 1140
        !self.config.full_bootstrap
            && compiler.stage >= 2
            && (self.hosts.iter().any(|h| *h == target) || target == self.build)
1141
    }
1142 1143 1144 1145 1146 1147 1148 1149 1150

    /// Given `num` in the form "a.b.c" return a "release string" which
    /// describes the release version number.
    ///
    /// For example on nightly this returns "a.b.c-nightly", on beta it returns
    /// "a.b.c-beta.1" and on stable it just returns "a.b.c".
    fn release(&self, num: &str) -> String {
        match &self.config.channel[..] {
            "stable" => num.to_string(),
M
Mark Rousskov 已提交
1151
            "beta" => {
1152
                if self.rust_info.is_git() && !self.config.ignore_git {
M
Mark Rousskov 已提交
1153 1154 1155 1156 1157
                    format!("{}-beta.{}", num, self.beta_prerelease_version())
                } else {
                    format!("{}-beta", num)
                }
            }
1158 1159 1160 1161 1162
            "nightly" => format!("{}-nightly", num),
            _ => format!("{}-dev", num),
        }
    }

1163 1164
    fn beta_prerelease_version(&self) -> u32 {
        if let Some(s) = self.prerelease_version.get() {
M
Mark Rousskov 已提交
1165
            return s;
1166 1167
        }

1168 1169 1170
        // Figure out how many merge commits happened since we branched off master.
        // That's our beta number!
        // (Note that we use a `..` range, not the `...` symmetric difference.)
1171 1172 1173 1174 1175
        let count = output(
            Command::new("git")
                .arg("rev-list")
                .arg("--count")
                .arg("--merges")
1176
                .arg("refs/remotes/origin/master..HEAD")
1177 1178 1179 1180 1181 1182 1183
                .current_dir(&self.src),
        );
        let n = count.trim().parse().unwrap();
        self.prerelease_version.set(Some(n));
        n
    }

1184 1185
    /// Returns the value of `release` above for Rust itself.
    fn rust_release(&self) -> String {
1186
        self.release(&self.version)
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
    }

    /// Returns the "package version" for a component given the `num` release
    /// number.
    ///
    /// The package version is typically what shows up in the names of tarballs.
    /// For channels like beta/nightly it's just the channel name, otherwise
    /// it's the `num` provided.
    fn package_vers(&self, num: &str) -> String {
        match &self.config.channel[..] {
            "stable" => num.to_string(),
            "beta" => "beta".to_string(),
            "nightly" => "nightly".to_string(),
            _ => format!("{}-dev", num),
        }
    }

    /// Returns the value of `package_vers` above for Rust itself.
    fn rust_package_vers(&self) -> String {
1206
        self.package_vers(&self.version)
1207 1208
    }

1209
    fn llvm_link_tools_dynamically(&self, target: TargetSelection) -> bool {
1210
        target.contains("linux-gnu") || target.contains("apple-darwin")
1211 1212
    }

1213 1214 1215 1216 1217 1218
    /// Returns the `version` string associated with this compiler for Rust
    /// itself.
    ///
    /// Note that this is a descriptive string which includes the commit date,
    /// sha, version, etc.
    fn rust_version(&self) -> String {
1219 1220 1221 1222
        let mut version = self.rust_info.version(self, &self.version);
        if let Some(ref s) = self.config.description {
            version.push_str(" (");
            version.push_str(s);
M
Matthias Krüger 已提交
1223
            version.push(')');
1224 1225
        }
        version
1226 1227
    }

A
Alexander Regueiro 已提交
1228
    /// Returns the full commit hash.
1229 1230 1231 1232
    fn rust_sha(&self) -> Option<&str> {
        self.rust_info.sha()
    }

N
Nick Cameron 已提交
1233 1234
    /// Returns the `a.b.c` version that the given package is at.
    fn release_num(&self, package: &str) -> String {
A
Alex Crichton 已提交
1235
        let toml_file_name = self.src.join(&format!("src/tools/{}/Cargo.toml", package));
1236
        let toml = t!(fs::read_to_string(&toml_file_name));
1237
        for line in toml.lines() {
L
Lzu Tao 已提交
1238 1239 1240 1241
            if let Some(stripped) =
                line.strip_prefix("version = \"").and_then(|s| s.strip_suffix("\""))
            {
                return stripped.to_owned();
1242 1243 1244
            }
        }

N
Nick Cameron 已提交
1245
        panic!("failed to find version in {}'s Cargo.toml", package)
1246 1247
    }

A
Alexander Regueiro 已提交
1248
    /// Returns `true` if unstable features should be enabled for the compiler
1249 1250 1251 1252 1253 1254 1255
    /// we're building.
    fn unstable_features(&self) -> bool {
        match &self.config.channel[..] {
            "stable" | "beta" => false,
            "nightly" | _ => true,
        }
    }
1256

1257 1258 1259
    /// Returns a Vec of all the dependencies of the given root crate,
    /// including transitive dependencies and the root itself. Only includes
    /// "local" crates (those in the local source tree, not from a registry).
1260
    fn in_tree_crates(&self, root: &str, target: Option<TargetSelection>) -> Vec<&Crate> {
1261
        let mut ret = Vec::new();
1262
        let mut list = vec![INTERNER.intern_str(root)];
1263 1264
        let mut visited = HashSet::new();
        while let Some(krate) = list.pop() {
1265
            let krate = &self.crates[&krate];
1266
            ret.push(krate);
1267
            for dep in &krate.deps {
1268 1269 1270 1271
                if !self.crates.contains_key(dep) {
                    // Ignore non-workspace members.
                    continue;
                }
1272 1273 1274 1275 1276 1277 1278 1279 1280
                // Don't include optional deps if their features are not
                // enabled. Ideally this would be computed from `cargo
                // metadata --features …`, but that is somewhat slow. Just
                // skip `build_helper` since there aren't any operations we
                // want to perform on it. In the future, we may want to
                // consider just filtering all build and dev dependencies in
                // metadata::build.
                if visited.insert(dep)
                    && dep != "build_helper"
1281 1282 1283
                    && (dep != "profiler_builtins"
                        || target
                            .map(|t| self.config.profiler_enabled(t))
M
Matthias Krüger 已提交
1284
                            .unwrap_or_else(|| self.config.any_profiler_enabled()))
1285 1286
                    && (dep != "rustc_codegen_llvm" || self.config.llvm_enabled())
                {
1287
                    list.push(*dep);
1288 1289 1290 1291 1292
                }
            }
        }
        ret
    }
1293

1294
    fn read_stamp_file(&self, stamp: &Path) -> Vec<(PathBuf, DependencyType)> {
1295 1296 1297 1298 1299
        if self.config.dry_run {
            return Vec::new();
        }

        let mut paths = Vec::new();
1300
        let contents = t!(fs::read(stamp), &stamp);
1301 1302 1303 1304
        // This is the method we use for extracting paths from the stamp file passed to us. See
        // run_cargo for more information (in compile.rs).
        for part in contents.split(|b| *b == 0) {
            if part.is_empty() {
M
Mark Rousskov 已提交
1305
                continue;
1306
            }
1307 1308 1309 1310 1311 1312
            let dependency_type = match part[0] as char {
                'h' => DependencyType::Host,
                's' => DependencyType::TargetSelfContained,
                't' => DependencyType::Target,
                _ => unreachable!(),
            };
J
John Kåre Alsaker 已提交
1313
            let path = PathBuf::from(t!(str::from_utf8(&part[1..])));
1314
            paths.push((path, dependency_type));
1315 1316 1317 1318 1319 1320
        }
        paths
    }

    /// Copies a file from `src` to `dst`
    pub fn copy(&self, src: &Path, dst: &Path) {
M
Mark Rousskov 已提交
1321 1322 1323
        if self.config.dry_run {
            return;
        }
1324
        self.verbose_than(1, &format!("Copy {:?} to {:?}", src, dst));
M
Mark Rousskov 已提交
1325 1326 1327
        if src == dst {
            return;
        }
1328
        let _ = fs::remove_file(&dst);
T
Tom Tromey 已提交
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
        let metadata = t!(src.symlink_metadata());
        if metadata.file_type().is_symlink() {
            let link = t!(fs::read_link(src));
            t!(symlink_file(link, dst));
        } else if let Ok(()) = fs::hard_link(src, dst) {
            // Attempt to "easy copy" by creating a hard link
            // (symlinks don't work on windows), but if that fails
            // just fall back to a slow `copy` operation.
        } else {
            if let Err(e) = fs::copy(src, dst) {
M
Mark Rousskov 已提交
1339
                panic!("failed to copy `{}` to `{}`: {}", src.display(), dst.display(), e)
T
Tom Tromey 已提交
1340 1341 1342 1343 1344
            }
            t!(fs::set_permissions(dst, metadata.permissions()));
            let atime = FileTime::from_last_access_time(&metadata);
            let mtime = FileTime::from_last_modification_time(&metadata);
            t!(filetime::set_file_times(dst, atime, mtime));
1345 1346 1347 1348 1349 1350
        }
    }

    /// Copies the `src` directory recursively to `dst`. Both are assumed to exist
    /// when this function is called.
    pub fn cp_r(&self, src: &Path, dst: &Path) {
M
Mark Rousskov 已提交
1351 1352 1353
        if self.config.dry_run {
            return;
        }
1354
        for f in self.read_dir(src) {
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
            let path = f.path();
            let name = path.file_name().unwrap();
            let dst = dst.join(name);
            if t!(f.file_type()).is_dir() {
                t!(fs::create_dir_all(&dst));
                self.cp_r(&path, &dst);
            } else {
                let _ = fs::remove_file(&dst);
                self.copy(&path, &dst);
            }
        }
    }

    /// Copies the `src` directory recursively to `dst`. Both are assumed to exist
    /// when this function is called. Unwanted files or directories can be skipped
    /// by returning `false` from the filter function.
1371
    pub fn cp_filtered(&self, src: &Path, dst: &Path, filter: &dyn Fn(&Path) -> bool) {
1372 1373 1374 1375 1376
        // Immediately recurse with an empty relative path
        self.recurse_(src, dst, Path::new(""), filter)
    }

    // Inner function does the actual work
1377
    fn recurse_(&self, src: &Path, dst: &Path, relative: &Path, filter: &dyn Fn(&Path) -> bool) {
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
        for f in self.read_dir(src) {
            let path = f.path();
            let name = path.file_name().unwrap();
            let dst = dst.join(name);
            let relative = relative.join(name);
            // Only copy file or directory if the filter function returns true
            if filter(&relative) {
                if t!(f.file_type()).is_dir() {
                    let _ = fs::remove_dir_all(&dst);
                    self.create_dir(&dst);
                    self.recurse_(&path, &dst, &relative, filter);
                } else {
                    let _ = fs::remove_file(&dst);
                    self.copy(&path, &dst);
                }
            }
        }
    }

    fn copy_to_folder(&self, src: &Path, dest_folder: &Path) {
        let file_name = src.file_name().unwrap();
        let dest = dest_folder.join(file_name);
        self.copy(src, &dest);
    }

    fn install(&self, src: &Path, dstdir: &Path, perms: u32) {
M
Mark Rousskov 已提交
1404 1405 1406
        if self.config.dry_run {
            return;
        }
1407
        let dst = dstdir.join(src.file_name().unwrap());
O
O01eg 已提交
1408
        self.verbose_than(1, &format!("Install {:?} to {:?}", src, dst));
1409 1410 1411
        t!(fs::create_dir_all(dstdir));
        drop(fs::remove_file(&dst));
        {
1412 1413 1414
            if !src.exists() {
                panic!("Error: File \"{}\" not found!", src.display());
            }
1415 1416
            let metadata = t!(src.symlink_metadata());
            if let Err(e) = fs::copy(&src, &dst) {
M
Mark Rousskov 已提交
1417
                panic!("failed to copy `{}` to `{}`: {}", src.display(), dst.display(), e)
1418 1419 1420 1421 1422
            }
            t!(fs::set_permissions(&dst, metadata.permissions()));
            let atime = FileTime::from_last_access_time(&metadata);
            let mtime = FileTime::from_last_modification_time(&metadata);
            t!(filetime::set_file_times(&dst, atime, mtime));
1423 1424 1425 1426 1427
        }
        chmod(&dst, perms);
    }

    fn create(&self, path: &Path, s: &str) {
M
Mark Rousskov 已提交
1428 1429 1430
        if self.config.dry_run {
            return;
        }
1431 1432 1433 1434
        t!(fs::write(path, s));
    }

    fn read(&self, path: &Path) -> String {
M
Mark Rousskov 已提交
1435 1436 1437
        if self.config.dry_run {
            return String::new();
        }
1438
        t!(fs::read_to_string(path))
1439 1440 1441
    }

    fn create_dir(&self, dir: &Path) {
M
Mark Rousskov 已提交
1442 1443 1444
        if self.config.dry_run {
            return;
        }
1445 1446 1447 1448
        t!(fs::create_dir_all(dir))
    }

    fn remove_dir(&self, dir: &Path) {
M
Mark Rousskov 已提交
1449 1450 1451
        if self.config.dry_run {
            return;
        }
1452 1453 1454
        t!(fs::remove_dir_all(dir))
    }

M
Mark Rousskov 已提交
1455
    fn read_dir(&self, dir: &Path) -> impl Iterator<Item = fs::DirEntry> {
1456 1457 1458 1459 1460 1461 1462
        let iter = match fs::read_dir(dir) {
            Ok(v) => v,
            Err(_) if self.config.dry_run => return vec![].into_iter(),
            Err(err) => panic!("could not read dir {:?}: {:?}", dir, err),
        };
        iter.map(|e| t!(e)).collect::<Vec<_>>().into_iter()
    }
1463 1464

    fn remove(&self, f: &Path) {
M
Mark Rousskov 已提交
1465 1466 1467
        if self.config.dry_run {
            return;
        }
1468 1469
        fs::remove_file(f).unwrap_or_else(|_| panic!("failed to remove {:?}", f));
    }
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483

    /// Returns if config.ninja is enabled, and checks for ninja existence,
    /// exiting with a nicer error message if not.
    fn ninja(&self) -> bool {
        let mut cmd_finder = crate::sanity::Finder::new();

        if self.config.ninja_in_file {
            // Some Linux distros rename `ninja` to `ninja-build`.
            // CMake can work with either binary name.
            if cmd_finder.maybe_have("ninja-build").is_none()
                && cmd_finder.maybe_have("ninja").is_none()
            {
                eprintln!(
                    "
1484 1485 1486 1487
Couldn't find required command: ninja (or ninja-build)

You should install ninja as described at
<https://github.com/ninja-build/ninja/wiki/Pre-built-Ninja-packages>,
1488 1489
or set `ninja = false` in the `[llvm]` section of `config.toml`.
Alternatively, set `download-ci-llvm = true` in that `[llvm]` section
1490
to download LLVM rather than building it.
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
"
                );
                std::process::exit(1);
            }
        }

        // If ninja isn't enabled but we're building for MSVC then we try
        // doubly hard to enable it. It was realized in #43767 that the msbuild
        // CMake generator for MSVC doesn't respect configuration options like
        // disabling LLVM assertions, which can often be quite important!
        //
        // In these cases we automatically enable Ninja if we find it in the
        // environment.
        if !self.config.ninja_in_file && self.config.build.contains("msvc") {
            if cmd_finder.maybe_have("ninja").is_some() {
                return true;
            }
        }

        self.config.ninja_in_file
    }
1512 1513 1514 1515 1516 1517
}

#[cfg(unix)]
fn chmod(path: &Path, perms: u32) {
    use std::os::unix::fs::*;
    t!(fs::set_permissions(path, fs::Permissions::from_mode(perms)));
A
Alex Crichton 已提交
1518
}
1519 1520 1521
#[cfg(windows)]
fn chmod(_path: &Path, _perms: u32) {}

1522
impl Compiler {
1523
    pub fn with_stage(mut self, stage: u32) -> Compiler {
1524 1525
        self.stage = stage;
        self
1526 1527
    }

A
Alexander Regueiro 已提交
1528
    /// Returns `true` if this is a snapshot compiler for `build`'s configuration
1529
    pub fn is_snapshot(&self, build: &Build) -> bool {
1530
        self.stage == 0 && self.host == build.build
1531
    }
1532

1533 1534 1535 1536
    /// Returns if this compiler should be treated as a final stage one in the
    /// current build session.
    /// This takes into account whether we're performing a full bootstrap or
    /// not; don't directly compare the stage with `2`!
1537
    pub fn is_final_stage(&self, build: &Build) -> bool {
1538 1539 1540
        let final_stage = if build.config.full_bootstrap { 2 } else { 1 };
        self.stage >= final_stage
    }
A
Alex Crichton 已提交
1541
}
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551

fn envify(s: &str) -> String {
    s.chars()
        .map(|c| match c {
            '-' => '_',
            c => c,
        })
        .flat_map(|c| c.to_uppercase())
        .collect()
}