Rollup merge of #128269 - onur-ozkan:improve-cargo-invocations, r=Mark-Simulacrum

improve cargo invocations on bootstrap

Fixes few of the `FIXME`s on cargo invocations and should be considered as blocker for https://github.com/rust-lang/rust/issues/128180.
This commit is contained in:
Matthias Krüger 2024-07-29 07:11:16 +02:00 committed by GitHub
commit 9b82536776
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 151 additions and 117 deletions

View File

@ -12,16 +12,6 @@ use crate::core::builder::{
use crate::core::config::TargetSelection; use crate::core::config::TargetSelection;
use crate::{Compiler, Mode, Subcommand}; use crate::{Compiler, Mode, Subcommand};
pub fn cargo_subcommand(kind: Kind) -> &'static str {
match kind {
Kind::Check
// We ensure check steps for both std and rustc from build_steps/clippy, so handle `Kind::Clippy` as well.
| Kind::Clippy => "check",
Kind::Fix => "fix",
_ => unreachable!(),
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Std { pub struct Std {
pub target: TargetSelection, pub target: TargetSelection,
@ -31,11 +21,22 @@ pub struct Std {
/// ///
/// [`compile::Rustc`]: crate::core::build_steps::compile::Rustc /// [`compile::Rustc`]: crate::core::build_steps::compile::Rustc
crates: Vec<String>, crates: Vec<String>,
/// Override `Builder::kind` on cargo invocations.
///
/// By default, `Builder::kind` is propagated as the subcommand to the cargo invocations.
/// However, there are cases when this is not desirable. For example, when running `x clippy $tool_name`,
/// passing `Builder::kind` to cargo invocations would run clippy on the entire compiler and library,
/// which is not useful if we only want to lint a few crates with specific rules.
override_build_kind: Option<Kind>,
} }
impl Std { impl Std {
pub fn new(target: TargetSelection) -> Self { pub fn new(target: TargetSelection) -> Self {
Self { target, crates: vec![] } Self::new_with_build_kind(target, None)
}
pub fn new_with_build_kind(target: TargetSelection, kind: Option<Kind>) -> Self {
Self { target, crates: vec![], override_build_kind: kind }
} }
} }
@ -49,7 +50,7 @@ impl Step for Std {
fn make_run(run: RunConfig<'_>) { fn make_run(run: RunConfig<'_>) {
let crates = run.make_run_crates(Alias::Library); let crates = run.make_run_crates(Alias::Library);
run.builder.ensure(Std { target: run.target, crates }); run.builder.ensure(Std { target: run.target, crates, override_build_kind: None });
} }
fn run(self, builder: &Builder<'_>) { fn run(self, builder: &Builder<'_>) {
@ -64,7 +65,7 @@ impl Step for Std {
Mode::Std, Mode::Std,
SourceType::InTree, SourceType::InTree,
target, target,
cargo_subcommand(builder.kind), self.override_build_kind.unwrap_or(builder.kind),
); );
std_cargo(builder, target, compiler.stage, &mut cargo); std_cargo(builder, target, compiler.stage, &mut cargo);
@ -118,7 +119,7 @@ impl Step for Std {
Mode::Std, Mode::Std,
SourceType::InTree, SourceType::InTree,
target, target,
cargo_subcommand(builder.kind), self.override_build_kind.unwrap_or(builder.kind),
); );
// If we're not in stage 0, tests and examples will fail to compile // If we're not in stage 0, tests and examples will fail to compile
@ -159,16 +160,31 @@ pub struct Rustc {
/// ///
/// [`compile::Rustc`]: crate::core::build_steps::compile::Rustc /// [`compile::Rustc`]: crate::core::build_steps::compile::Rustc
crates: Vec<String>, crates: Vec<String>,
/// Override `Builder::kind` on cargo invocations.
///
/// By default, `Builder::kind` is propagated as the subcommand to the cargo invocations.
/// However, there are cases when this is not desirable. For example, when running `x clippy $tool_name`,
/// passing `Builder::kind` to cargo invocations would run clippy on the entire compiler and library,
/// which is not useful if we only want to lint a few crates with specific rules.
override_build_kind: Option<Kind>,
} }
impl Rustc { impl Rustc {
pub fn new(target: TargetSelection, builder: &Builder<'_>) -> Self { pub fn new(target: TargetSelection, builder: &Builder<'_>) -> Self {
Self::new_with_build_kind(target, builder, None)
}
pub fn new_with_build_kind(
target: TargetSelection,
builder: &Builder<'_>,
kind: Option<Kind>,
) -> Self {
let crates = builder let crates = builder
.in_tree_crates("rustc-main", Some(target)) .in_tree_crates("rustc-main", Some(target))
.into_iter() .into_iter()
.map(|krate| krate.name.to_string()) .map(|krate| krate.name.to_string())
.collect(); .collect();
Self { target, crates } Self { target, crates, override_build_kind: kind }
} }
} }
@ -183,7 +199,7 @@ impl Step for Rustc {
fn make_run(run: RunConfig<'_>) { fn make_run(run: RunConfig<'_>) {
let crates = run.make_run_crates(Alias::Compiler); let crates = run.make_run_crates(Alias::Compiler);
run.builder.ensure(Rustc { target: run.target, crates }); run.builder.ensure(Rustc { target: run.target, crates, override_build_kind: None });
} }
/// Builds the compiler. /// Builds the compiler.
@ -204,7 +220,7 @@ impl Step for Rustc {
builder.ensure(crate::core::build_steps::compile::Std::new(compiler, compiler.host)); builder.ensure(crate::core::build_steps::compile::Std::new(compiler, compiler.host));
builder.ensure(crate::core::build_steps::compile::Std::new(compiler, target)); builder.ensure(crate::core::build_steps::compile::Std::new(compiler, target));
} else { } else {
builder.ensure(Std::new(target)); builder.ensure(Std::new_with_build_kind(target, self.override_build_kind));
} }
let mut cargo = builder::Cargo::new( let mut cargo = builder::Cargo::new(
@ -213,7 +229,7 @@ impl Step for Rustc {
Mode::Rustc, Mode::Rustc,
SourceType::InTree, SourceType::InTree,
target, target,
cargo_subcommand(builder.kind), self.override_build_kind.unwrap_or(builder.kind),
); );
rustc_cargo(builder, &mut cargo, target, &compiler); rustc_cargo(builder, &mut cargo, target, &compiler);
@ -291,7 +307,7 @@ impl Step for CodegenBackend {
Mode::Codegen, Mode::Codegen,
SourceType::InTree, SourceType::InTree,
target, target,
cargo_subcommand(builder.kind), builder.kind,
); );
cargo cargo
@ -349,7 +365,7 @@ impl Step for RustAnalyzer {
compiler, compiler,
Mode::ToolRustc, Mode::ToolRustc,
target, target,
cargo_subcommand(builder.kind), builder.kind,
"src/tools/rust-analyzer", "src/tools/rust-analyzer",
SourceType::InTree, SourceType::InTree,
&["in-rust-tree".to_owned()], &["in-rust-tree".to_owned()],
@ -417,7 +433,7 @@ macro_rules! tool_check_step {
compiler, compiler,
Mode::ToolRustc, Mode::ToolRustc,
target, target,
cargo_subcommand(builder.kind), builder.kind,
$path, $path,
$source_type, $source_type,
&[], &[],

View File

@ -11,7 +11,7 @@ use std::path::Path;
use crate::core::builder::{crate_description, Builder, RunConfig, ShouldRun, Step}; use crate::core::builder::{crate_description, Builder, RunConfig, ShouldRun, Step};
use crate::utils::helpers::t; use crate::utils::helpers::t;
use crate::{Build, Compiler, Mode, Subcommand}; use crate::{Build, Compiler, Kind, Mode, Subcommand};
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CleanAll {} pub struct CleanAll {}
@ -66,7 +66,7 @@ macro_rules! clean_crate_tree {
fn run(self, builder: &Builder<'_>) -> Self::Output { fn run(self, builder: &Builder<'_>) -> Self::Output {
let compiler = self.compiler; let compiler = self.compiler;
let target = compiler.host; let target = compiler.host;
let mut cargo = builder.bare_cargo(compiler, $mode, target, "clean"); let mut cargo = builder.bare_cargo(compiler, $mode, target, Kind::Clean);
// Since https://github.com/rust-lang/rust/pull/111076 enables // Since https://github.com/rust-lang/rust/pull/111076 enables
// unstable cargo feature (`public-dependency`), we need to ensure // unstable cargo feature (`public-dependency`), we need to ensure

View File

@ -116,8 +116,14 @@ impl Step for Std {
let target = self.target; let target = self.target;
let compiler = builder.compiler(builder.top_stage, builder.config.build); let compiler = builder.compiler(builder.top_stage, builder.config.build);
let mut cargo = let mut cargo = builder::Cargo::new(
builder::Cargo::new(builder, compiler, Mode::Std, SourceType::InTree, target, "clippy"); builder,
compiler,
Mode::Std,
SourceType::InTree,
target,
Kind::Clippy,
);
std_cargo(builder, target, compiler.stage, &mut cargo); std_cargo(builder, target, compiler.stage, &mut cargo);
@ -178,7 +184,7 @@ impl Step for Rustc {
builder.ensure(compile::Std::new(compiler, compiler.host)); builder.ensure(compile::Std::new(compiler, compiler.host));
builder.ensure(compile::Std::new(compiler, target)); builder.ensure(compile::Std::new(compiler, target));
} else { } else {
builder.ensure(check::Std::new(target)); builder.ensure(check::Std::new_with_build_kind(target, Some(Kind::Check)));
} }
let mut cargo = builder::Cargo::new( let mut cargo = builder::Cargo::new(
@ -187,7 +193,7 @@ impl Step for Rustc {
Mode::Rustc, Mode::Rustc,
SourceType::InTree, SourceType::InTree,
target, target,
"clippy", Kind::Clippy,
); );
rustc_cargo(builder, &mut cargo, target, &compiler); rustc_cargo(builder, &mut cargo, target, &compiler);
@ -245,14 +251,14 @@ macro_rules! lint_any {
let compiler = builder.compiler(builder.top_stage, builder.config.build); let compiler = builder.compiler(builder.top_stage, builder.config.build);
let target = self.target; let target = self.target;
builder.ensure(check::Rustc::new(target, builder)); builder.ensure(check::Rustc::new_with_build_kind(target, builder, Some(Kind::Check)));
let cargo = prepare_tool_cargo( let cargo = prepare_tool_cargo(
builder, builder,
compiler, compiler,
Mode::ToolRustc, Mode::ToolRustc,
target, target,
"clippy", Kind::Clippy,
$path, $path,
SourceType::InTree, SourceType::InTree,
&[], &[],

View File

@ -247,7 +247,7 @@ impl Step for Std {
Mode::Std, Mode::Std,
SourceType::InTree, SourceType::InTree,
target, target,
"check", Kind::Check,
); );
cargo.rustflag("-Zalways-encode-mir"); cargo.rustflag("-Zalways-encode-mir");
cargo.arg("--manifest-path").arg(builder.src.join("library/sysroot/Cargo.toml")); cargo.arg("--manifest-path").arg(builder.src.join("library/sysroot/Cargo.toml"));
@ -259,7 +259,7 @@ impl Step for Std {
Mode::Std, Mode::Std,
SourceType::InTree, SourceType::InTree,
target, target,
"build", Kind::Build,
); );
std_cargo(builder, target, compiler.stage, &mut cargo); std_cargo(builder, target, compiler.stage, &mut cargo);
for krate in &*self.crates { for krate in &*self.crates {
@ -919,7 +919,7 @@ impl Step for Rustc {
Mode::Rustc, Mode::Rustc,
SourceType::InTree, SourceType::InTree,
target, target,
"build", Kind::Build,
); );
rustc_cargo(builder, &mut cargo, target, &compiler); rustc_cargo(builder, &mut cargo, target, &compiler);
@ -1359,7 +1359,7 @@ impl Step for CodegenBackend {
Mode::Codegen, Mode::Codegen,
SourceType::InTree, SourceType::InTree,
target, target,
"build", Kind::Build,
); );
cargo cargo
.arg("--manifest-path") .arg("--manifest-path")

View File

@ -714,7 +714,7 @@ fn doc_std(
let out_dir = target_dir.join(target.triple).join("doc"); let out_dir = target_dir.join(target.triple).join("doc");
let mut cargo = let mut cargo =
builder::Cargo::new(builder, compiler, Mode::Std, SourceType::InTree, target, "doc"); builder::Cargo::new(builder, compiler, Mode::Std, SourceType::InTree, target, Kind::Doc);
compile::std_cargo(builder, target, compiler.stage, &mut cargo); compile::std_cargo(builder, target, compiler.stage, &mut cargo);
cargo cargo
@ -816,8 +816,14 @@ impl Step for Rustc {
); );
// Build cargo command. // Build cargo command.
let mut cargo = let mut cargo = builder::Cargo::new(
builder::Cargo::new(builder, compiler, Mode::Rustc, SourceType::InTree, target, "doc"); builder,
compiler,
Mode::Rustc,
SourceType::InTree,
target,
Kind::Doc,
);
cargo.rustdocflag("--document-private-items"); cargo.rustdocflag("--document-private-items");
// Since we always pass --document-private-items, there's no need to warn about linking to private items. // Since we always pass --document-private-items, there's no need to warn about linking to private items.
@ -964,7 +970,7 @@ macro_rules! tool_doc {
compiler, compiler,
Mode::ToolRustc, Mode::ToolRustc,
target, target,
"doc", Kind::Doc,
$path, $path,
source_type, source_type,
&[], &[],

View File

@ -8,7 +8,7 @@ use std::path::PathBuf;
use crate::core::build_steps::dist::distdir; use crate::core::build_steps::dist::distdir;
use crate::core::build_steps::test; use crate::core::build_steps::test;
use crate::core::build_steps::tool::{self, SourceType, Tool}; use crate::core::build_steps::tool::{self, SourceType, Tool};
use crate::core::builder::{Builder, RunConfig, ShouldRun, Step}; use crate::core::builder::{Builder, Kind, RunConfig, ShouldRun, Step};
use crate::core::config::flags::get_completion; use crate::core::config::flags::get_completion;
use crate::core::config::TargetSelection; use crate::core::config::TargetSelection;
use crate::utils::exec::command; use crate::utils::exec::command;
@ -142,7 +142,7 @@ impl Step for Miri {
host_compiler, host_compiler,
Mode::ToolRustc, Mode::ToolRustc,
host, host,
"run", Kind::Run,
"src/tools/miri", "src/tools/miri",
SourceType::InTree, SourceType::InTree,
&[], &[],

View File

@ -68,7 +68,7 @@ impl Step for CrateBootstrap {
compiler, compiler,
Mode::ToolBootstrap, Mode::ToolBootstrap,
bootstrap_host, bootstrap_host,
"test", Kind::Test,
path, path,
SourceType::InTree, SourceType::InTree,
&[], &[],
@ -119,7 +119,7 @@ You can skip linkcheck with --skip src/tools/linkchecker"
compiler, compiler,
Mode::ToolBootstrap, Mode::ToolBootstrap,
bootstrap_host, bootstrap_host,
"test", Kind::Test,
"src/tools/linkchecker", "src/tools/linkchecker",
SourceType::InTree, SourceType::InTree,
&[], &[],
@ -284,7 +284,7 @@ impl Step for Cargo {
compiler, compiler,
Mode::ToolRustc, Mode::ToolRustc,
self.host, self.host,
"test", Kind::Test,
"src/tools/cargo", "src/tools/cargo",
SourceType::Submodule, SourceType::Submodule,
&[], &[],
@ -355,7 +355,7 @@ impl Step for RustAnalyzer {
compiler, compiler,
Mode::ToolRustc, Mode::ToolRustc,
host, host,
"test", Kind::Test,
crate_path, crate_path,
SourceType::InTree, SourceType::InTree,
&["in-rust-tree".to_owned()], &["in-rust-tree".to_owned()],
@ -407,7 +407,7 @@ impl Step for Rustfmt {
compiler, compiler,
Mode::ToolRustc, Mode::ToolRustc,
host, host,
"test", Kind::Test,
"src/tools/rustfmt", "src/tools/rustfmt",
SourceType::InTree, SourceType::InTree,
&[], &[],
@ -442,7 +442,7 @@ impl Miri {
Mode::Std, Mode::Std,
SourceType::Submodule, SourceType::Submodule,
target, target,
"miri-setup", Kind::MiriSetup,
); );
// Tell `cargo miri setup` where to find the sources. // Tell `cargo miri setup` where to find the sources.
@ -527,7 +527,7 @@ impl Step for Miri {
host_compiler, host_compiler,
Mode::ToolRustc, Mode::ToolRustc,
host, host,
"test", Kind::Test,
"src/tools/miri", "src/tools/miri",
SourceType::InTree, SourceType::InTree,
&[], &[],
@ -617,7 +617,7 @@ impl Step for CargoMiri {
compiler, compiler,
Mode::ToolStd, // it's unclear what to use here, we're not building anything just doing a smoke test! Mode::ToolStd, // it's unclear what to use here, we're not building anything just doing a smoke test!
target, target,
"miri-test", Kind::MiriTest,
"src/tools/miri/test-cargo-miri", "src/tools/miri/test-cargo-miri",
SourceType::Submodule, SourceType::Submodule,
&[], &[],
@ -677,7 +677,7 @@ impl Step for CompiletestTest {
// when std sources change. // when std sources change.
Mode::ToolStd, Mode::ToolStd,
host, host,
"test", Kind::Test,
"src/tools/compiletest", "src/tools/compiletest",
SourceType::InTree, SourceType::InTree,
&[], &[],
@ -727,7 +727,7 @@ impl Step for Clippy {
compiler, compiler,
Mode::ToolRustc, Mode::ToolRustc,
host, host,
"test", Kind::Test,
"src/tools/clippy", "src/tools/clippy",
SourceType::InTree, SourceType::InTree,
&[], &[],
@ -1277,7 +1277,7 @@ impl Step for RunMakeSupport {
self.compiler, self.compiler,
Mode::ToolStd, Mode::ToolStd,
self.target, self.target,
"build", Kind::Build,
"src/tools/run-make-support", "src/tools/run-make-support",
SourceType::InTree, SourceType::InTree,
&[], &[],
@ -1321,7 +1321,7 @@ impl Step for CrateRunMakeSupport {
compiler, compiler,
Mode::ToolBootstrap, Mode::ToolBootstrap,
host, host,
"test", Kind::Test,
"src/tools/run-make-support", "src/tools/run-make-support",
SourceType::InTree, SourceType::InTree,
&[], &[],
@ -1367,7 +1367,7 @@ impl Step for CrateBuildHelper {
compiler, compiler,
Mode::ToolBootstrap, Mode::ToolBootstrap,
host, host,
"test", Kind::Test,
"src/tools/build_helper", "src/tools/build_helper",
SourceType::InTree, SourceType::InTree,
&[], &[],
@ -2631,7 +2631,7 @@ impl Step for Crate {
mode, mode,
SourceType::InTree, SourceType::InTree,
target, target,
"miri-test", Kind::MiriTest,
); );
// This hack helps bootstrap run standard library tests in Miri. The issue is as // This hack helps bootstrap run standard library tests in Miri. The issue is as
// follows: when running `cargo miri test` on libcore, cargo builds a local copy of core // follows: when running `cargo miri test` on libcore, cargo builds a local copy of core
@ -2654,14 +2654,7 @@ impl Step for Crate {
} }
// Build `cargo test` command // Build `cargo test` command
builder::Cargo::new( builder::Cargo::new(builder, compiler, mode, SourceType::InTree, target, builder.kind)
builder,
compiler,
mode,
SourceType::InTree,
target,
builder.kind.as_str(),
)
}; };
match mode { match mode {
@ -2753,7 +2746,7 @@ impl Step for CrateRustdoc {
compiler, compiler,
Mode::ToolRustc, Mode::ToolRustc,
target, target,
builder.kind.as_str(), builder.kind,
"src/tools/rustdoc", "src/tools/rustdoc",
SourceType::InTree, SourceType::InTree,
&[], &[],
@ -2845,7 +2838,7 @@ impl Step for CrateRustdocJsonTypes {
compiler, compiler,
Mode::ToolRustc, Mode::ToolRustc,
target, target,
builder.kind.as_str(), builder.kind,
"src/rustdoc-json-types", "src/rustdoc-json-types",
SourceType::InTree, SourceType::InTree,
&[], &[],
@ -3079,7 +3072,7 @@ impl Step for TierCheck {
self.compiler, self.compiler,
Mode::ToolStd, Mode::ToolStd,
self.compiler.host, self.compiler.host,
"run", Kind::Run,
"src/tools/tier-check", "src/tools/tier-check",
SourceType::InTree, SourceType::InTree,
&[], &[],
@ -3151,7 +3144,7 @@ impl Step for RustInstaller {
compiler, compiler,
Mode::ToolBootstrap, Mode::ToolBootstrap,
bootstrap_host, bootstrap_host,
"test", Kind::Test,
"src/tools/rust-installer", "src/tools/rust-installer",
SourceType::InTree, SourceType::InTree,
&[], &[],
@ -3321,7 +3314,7 @@ impl Step for CodegenCranelift {
Mode::Codegen, // Must be codegen to ensure dlopen on compiled dylibs works Mode::Codegen, // Must be codegen to ensure dlopen on compiled dylibs works
SourceType::InTree, SourceType::InTree,
target, target,
"run", Kind::Run,
); );
cargo.current_dir(&builder.src.join("compiler/rustc_codegen_cranelift")); cargo.current_dir(&builder.src.join("compiler/rustc_codegen_cranelift"));
@ -3453,7 +3446,7 @@ impl Step for CodegenGCC {
Mode::Codegen, // Must be codegen to ensure dlopen on compiled dylibs works Mode::Codegen, // Must be codegen to ensure dlopen on compiled dylibs works
SourceType::InTree, SourceType::InTree,
target, target,
"run", Kind::Run,
); );
cargo.current_dir(&builder.src.join("compiler/rustc_codegen_gcc")); cargo.current_dir(&builder.src.join("compiler/rustc_codegen_gcc"));
@ -3541,7 +3534,7 @@ impl Step for TestFloatParse {
compiler, compiler,
Mode::ToolStd, Mode::ToolStd,
bootstrap_host, bootstrap_host,
"test", Kind::Test,
path, path,
SourceType::InTree, SourceType::InTree,
&[], &[],
@ -3564,7 +3557,7 @@ impl Step for TestFloatParse {
compiler, compiler,
Mode::ToolStd, Mode::ToolStd,
bootstrap_host, bootstrap_host,
"run", Kind::Run,
path, path,
SourceType::InTree, SourceType::InTree,
&[], &[],

View File

@ -90,7 +90,7 @@ impl Step for ToolBuild {
compiler, compiler,
self.mode, self.mode,
target, target,
"build", Kind::Build,
path, path,
self.source_type, self.source_type,
&self.extra_features, &self.extra_features,
@ -136,12 +136,12 @@ pub fn prepare_tool_cargo(
compiler: Compiler, compiler: Compiler,
mode: Mode, mode: Mode,
target: TargetSelection, target: TargetSelection,
command: &'static str, cmd_kind: Kind,
path: &str, path: &str,
source_type: SourceType, source_type: SourceType,
extra_features: &[String], extra_features: &[String],
) -> CargoCommand { ) -> CargoCommand {
let mut cargo = builder::Cargo::new(builder, compiler, mode, source_type, target, command); let mut cargo = builder::Cargo::new(builder, compiler, mode, source_type, target, cmd_kind);
let dir = builder.src.join(path); let dir = builder.src.join(path);
cargo.arg("--manifest-path").arg(dir.join("Cargo.toml")); cargo.arg("--manifest-path").arg(dir.join("Cargo.toml"));
@ -646,7 +646,7 @@ impl Step for Rustdoc {
build_compiler, build_compiler,
Mode::ToolRustc, Mode::ToolRustc,
target, target,
"build", Kind::Build,
"src/tools/rustdoc", "src/tools/rustdoc",
SourceType::InTree, SourceType::InTree,
features.as_slice(), features.as_slice(),
@ -905,7 +905,7 @@ impl Step for LlvmBitcodeLinker {
self.compiler, self.compiler,
Mode::ToolRustc, Mode::ToolRustc,
self.target, self.target,
"build", Kind::Build,
"src/tools/llvm-bitcode-linker", "src/tools/llvm-bitcode-linker",
SourceType::InTree, SourceType::InTree,
&self.extra_features, &self.extra_features,

View File

@ -689,7 +689,7 @@ impl<'a> ShouldRun<'a> {
} }
} }
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] #[derive(Debug, Copy, Clone, Eq, Hash, PartialEq, PartialOrd, Ord, ValueEnum)]
pub enum Kind { pub enum Kind {
#[value(alias = "b")] #[value(alias = "b")]
Build, Build,
@ -701,6 +701,8 @@ pub enum Kind {
#[value(alias = "t")] #[value(alias = "t")]
Test, Test,
Miri, Miri,
MiriSetup,
MiriTest,
Bench, Bench,
#[value(alias = "d")] #[value(alias = "d")]
Doc, Doc,
@ -725,6 +727,8 @@ impl Kind {
Kind::Format => "fmt", Kind::Format => "fmt",
Kind::Test => "test", Kind::Test => "test",
Kind::Miri => "miri", Kind::Miri => "miri",
Kind::MiriSetup => panic!("`as_str` is not supported for `Kind::MiriSetup`."),
Kind::MiriTest => panic!("`as_str` is not supported for `Kind::MiriTest`."),
Kind::Bench => "bench", Kind::Bench => "bench",
Kind::Doc => "doc", Kind::Doc => "doc",
Kind::Clean => "clean", Kind::Clean => "clean",
@ -1000,6 +1004,7 @@ impl<'a> Builder<'a> {
Kind::Vendor => describe!(vendor::Vendor), Kind::Vendor => describe!(vendor::Vendor),
// special-cased in Build::build() // special-cased in Build::build()
Kind::Format | Kind::Suggest | Kind::Perf => vec![], Kind::Format | Kind::Suggest | Kind::Perf => vec![],
Kind::MiriTest | Kind::MiriSetup => unreachable!(),
} }
} }
@ -1386,23 +1391,30 @@ impl<'a> Builder<'a> {
compiler: Compiler, compiler: Compiler,
mode: Mode, mode: Mode,
target: TargetSelection, target: TargetSelection,
cmd: &str, // FIXME make this properly typed cmd_kind: Kind,
) -> BootstrapCommand { ) -> BootstrapCommand {
let mut cargo; let mut cargo = match cmd_kind {
if cmd == "clippy" { Kind::Clippy => {
cargo = self.cargo_clippy_cmd(compiler); let mut cargo = self.cargo_clippy_cmd(compiler);
cargo.arg(cmd); cargo.arg(cmd_kind.as_str());
} else if let Some(subcmd) = cmd.strip_prefix("miri") { cargo
// Command must be "miri-X". }
let subcmd = subcmd Kind::MiriSetup => {
.strip_prefix('-') let mut cargo = self.cargo_miri_cmd(compiler);
.unwrap_or_else(|| panic!("expected `miri-$subcommand`, but got {}", cmd)); cargo.arg("miri").arg("setup");
cargo = self.cargo_miri_cmd(compiler); cargo
cargo.arg("miri").arg(subcmd); }
} else { Kind::MiriTest => {
cargo = command(&self.initial_cargo); let mut cargo = self.cargo_miri_cmd(compiler);
cargo.arg(cmd); cargo.arg("miri").arg("test");
} cargo
}
_ => {
let mut cargo = command(&self.initial_cargo);
cargo.arg(cmd_kind.as_str());
cargo
}
};
// Run cargo from the source root so it can find .cargo/config. // Run cargo from the source root so it can find .cargo/config.
// This matters when using vendoring and the working directory is outside the repository. // This matters when using vendoring and the working directory is outside the repository.
@ -1431,7 +1443,7 @@ impl<'a> Builder<'a> {
Color::Auto => {} // nothing to do Color::Auto => {} // nothing to do
} }
if cmd != "install" { if cmd_kind != Kind::Install {
cargo.arg("--target").arg(target.rustc_target_arg()); cargo.arg("--target").arg(target.rustc_target_arg());
} else { } else {
assert_eq!(target, compiler.host); assert_eq!(target, compiler.host);
@ -1440,8 +1452,11 @@ impl<'a> Builder<'a> {
if self.config.rust_optimize.is_release() { if self.config.rust_optimize.is_release() {
// FIXME: cargo bench/install do not accept `--release` // FIXME: cargo bench/install do not accept `--release`
// and miri doesn't want it // and miri doesn't want it
if cmd != "bench" && cmd != "install" && !cmd.starts_with("miri-") { match cmd_kind {
cargo.arg("--release"); Kind::Bench | Kind::Install | Kind::Miri | Kind::MiriSetup | Kind::MiriTest => {}
_ => {
cargo.arg("--release");
}
} }
} }
@ -1464,9 +1479,9 @@ impl<'a> Builder<'a> {
mode: Mode, mode: Mode,
source_type: SourceType, source_type: SourceType,
target: TargetSelection, target: TargetSelection,
cmd: &str, // FIXME make this properly typed cmd_kind: Kind,
) -> Cargo { ) -> Cargo {
let mut cargo = self.bare_cargo(compiler, mode, target, cmd); let mut cargo = self.bare_cargo(compiler, mode, target, cmd_kind);
let out_dir = self.stage_out(compiler, mode); let out_dir = self.stage_out(compiler, mode);
let mut hostflags = HostFlags::default(); let mut hostflags = HostFlags::default();
@ -1477,7 +1492,7 @@ impl<'a> Builder<'a> {
self.clear_if_dirty(&out_dir, &backend); self.clear_if_dirty(&out_dir, &backend);
} }
if cmd == "doc" || cmd == "rustdoc" { if cmd_kind == Kind::Doc {
let my_out = match mode { let my_out = match mode {
// This is the intended out directory for compiler documentation. // This is the intended out directory for compiler documentation.
Mode::Rustc | Mode::ToolRustc => self.compiler_doc_out(target), Mode::Rustc | Mode::ToolRustc => self.compiler_doc_out(target),
@ -1508,7 +1523,7 @@ impl<'a> Builder<'a> {
// Set a flag for `check`/`clippy`/`fix`, so that certain build // Set a flag for `check`/`clippy`/`fix`, so that certain build
// scripts can do less work (i.e. not building/requiring LLVM). // scripts can do less work (i.e. not building/requiring LLVM).
if cmd == "check" || cmd == "clippy" || cmd == "fix" { if matches!(cmd_kind, Kind::Check | Kind::Clippy | Kind::Fix) {
// If we've not yet built LLVM, or it's stale, then bust // If we've not yet built LLVM, or it's stale, then bust
// the rustc_llvm cache. That will always work, even though it // the rustc_llvm cache. That will always work, even though it
// may mean that on the next non-check build we'll need to rebuild // may mean that on the next non-check build we'll need to rebuild
@ -1558,7 +1573,7 @@ impl<'a> Builder<'a> {
rustflags.arg("--cfg=bootstrap"); rustflags.arg("--cfg=bootstrap");
} }
if cmd == "clippy" { if cmd_kind == Kind::Clippy {
// clippy overwrites sysroot if we pass it to cargo. // clippy overwrites sysroot if we pass it to cargo.
// Pass it directly to clippy instead. // Pass it directly to clippy instead.
// NOTE: this can't be fixed in clippy because we explicitly don't set `RUSTC`, // NOTE: this can't be fixed in clippy because we explicitly don't set `RUSTC`,
@ -1654,7 +1669,7 @@ impl<'a> Builder<'a> {
Mode::Std | Mode::ToolBootstrap | Mode::ToolStd => {} Mode::Std | Mode::ToolBootstrap | Mode::ToolStd => {}
Mode::Rustc | Mode::Codegen | Mode::ToolRustc => { Mode::Rustc | Mode::Codegen | Mode::ToolRustc => {
// Build proc macros both for the host and the target // Build proc macros both for the host and the target
if target != compiler.host && cmd != "check" { if target != compiler.host && cmd_kind != Kind::Check {
cargo.arg("-Zdual-proc-macros"); cargo.arg("-Zdual-proc-macros");
rustflags.arg("-Zdual-proc-macros"); rustflags.arg("-Zdual-proc-macros");
} }
@ -1739,7 +1754,7 @@ impl<'a> Builder<'a> {
} }
cargo.env("__CARGO_DEFAULT_LIB_METADATA", &metadata); cargo.env("__CARGO_DEFAULT_LIB_METADATA", &metadata);
if cmd == "clippy" { if cmd_kind == Kind::Clippy {
rustflags.arg("-Zforce-unstable-if-unmarked"); rustflags.arg("-Zforce-unstable-if-unmarked");
} }
@ -1755,10 +1770,15 @@ impl<'a> Builder<'a> {
// //
// Only clear out the directory if we're compiling std; otherwise, we // Only clear out the directory if we're compiling std; otherwise, we
// should let Cargo take care of things for us (via depdep info) // should let Cargo take care of things for us (via depdep info)
if !self.config.dry_run() && mode == Mode::Std && cmd == "build" { if !self.config.dry_run() && mode == Mode::Std && cmd_kind == Kind::Build {
self.clear_if_dirty(&out_dir, &self.rustc(compiler)); self.clear_if_dirty(&out_dir, &self.rustc(compiler));
} }
let rustdoc_path = match cmd_kind {
Kind::Doc | Kind::Test | Kind::MiriTest => self.rustdoc(compiler),
_ => PathBuf::from("/path/to/nowhere/rustdoc/not/required"),
};
// Customize the compiler we're running. Specify the compiler to cargo // Customize the compiler we're running. Specify the compiler to cargo
// as our shim and then pass it some various options used to configure // as our shim and then pass it some various options used to configure
// how the actual compiler itself is called. // how the actual compiler itself is called.
@ -1772,15 +1792,7 @@ impl<'a> Builder<'a> {
.env("RUSTC_SYSROOT", sysroot) .env("RUSTC_SYSROOT", sysroot)
.env("RUSTC_LIBDIR", libdir) .env("RUSTC_LIBDIR", libdir)
.env("RUSTDOC", self.bootstrap_out.join("rustdoc")) .env("RUSTDOC", self.bootstrap_out.join("rustdoc"))
.env( .env("RUSTDOC_REAL", rustdoc_path)
"RUSTDOC_REAL",
// Make sure to handle both `test` and `miri-test` commands.
if cmd == "doc" || cmd == "rustdoc" || (cmd.ends_with("test") && want_rustdoc) {
self.rustdoc(compiler)
} else {
PathBuf::from("/path/to/nowhere/rustdoc/not/required")
},
)
.env("RUSTC_ERROR_METADATA_DST", self.extended_error_dir()) .env("RUSTC_ERROR_METADATA_DST", self.extended_error_dir())
.env("RUSTC_BREAK_ON_ICE", "1"); .env("RUSTC_BREAK_ON_ICE", "1");
@ -1799,7 +1811,7 @@ impl<'a> Builder<'a> {
} }
// If this is for `miri-test`, prepare the sysroots. // If this is for `miri-test`, prepare the sysroots.
if cmd == "miri-test" { if cmd_kind == Kind::MiriTest {
self.ensure(compile::Std::new(compiler, compiler.host)); self.ensure(compile::Std::new(compiler, compiler.host));
let host_sysroot = self.sysroot(compiler); let host_sysroot = self.sysroot(compiler);
let miri_sysroot = test::Miri::build_miri_sysroot(self, compiler, target); let miri_sysroot = test::Miri::build_miri_sysroot(self, compiler, target);
@ -1813,7 +1825,8 @@ impl<'a> Builder<'a> {
rustflags.arg(&format!("-Zstack-protector={stack_protector}")); rustflags.arg(&format!("-Zstack-protector={stack_protector}"));
} }
if !(["build", "check", "clippy", "fix", "rustc"].contains(&cmd)) && want_rustdoc { if !matches!(cmd_kind, Kind::Build | Kind::Check | Kind::Clippy | Kind::Fix) && want_rustdoc
{
cargo.env("RUSTDOC_LIBDIR", self.rustc_libdir(compiler)); cargo.env("RUSTDOC_LIBDIR", self.rustc_libdir(compiler));
} }
@ -2430,9 +2443,9 @@ impl Cargo {
mode: Mode, mode: Mode,
source_type: SourceType, source_type: SourceType,
target: TargetSelection, target: TargetSelection,
cmd: &str, // FIXME make this properly typed cmd_kind: Kind,
) -> Cargo { ) -> Cargo {
let mut cargo = builder.cargo(compiler, mode, source_type, target, cmd); let mut cargo = builder.cargo(compiler, mode, source_type, target, cmd_kind);
cargo.configure_linker(builder); cargo.configure_linker(builder);
cargo cargo
} }
@ -2448,9 +2461,9 @@ impl Cargo {
mode: Mode, mode: Mode,
source_type: SourceType, source_type: SourceType,
target: TargetSelection, target: TargetSelection,
cmd: &str, // FIXME make this properly typed cmd_kind: Kind,
) -> Cargo { ) -> Cargo {
builder.cargo(compiler, mode, source_type, target, cmd) builder.cargo(compiler, mode, source_type, target, cmd_kind)
} }
pub fn rustdocflag(&mut self, arg: &str) -> &mut Cargo { pub fn rustdocflag(&mut self, arg: &str) -> &mut Cargo {