Rollup merge of #148911 - karolzwolak:bootstrap-rustflags-precedence, r=Kobzol

Make flags from `*FLAGS*` (such as `RUSTFLAGS`) env. vars. have precedence over flags set by bootstrap

Before this PR extra flags from env variables like `RUSTFLAGS` had lower precedence than bootstrap flags. This PR changes that, and tus makes overriding flags passed to the compiler easier and more reliable.
This is technically a breaking change — but it only affects people using these env variables.

This change was [discussed on zulip](https://rust-lang.zulipchat.com/#narrow/channel/326414-t-infra.2Fbootstrap/topic/Precedence.20of.20RUSTFLAGS.20in.20bootstrap/with/554903280) by members of bootstrap team.

r? `@Kobzol`
This commit is contained in:
Matthias Krüger 2025-11-16 20:40:23 +01:00 committed by GitHub
commit 8c5606ecbc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 40 additions and 25 deletions

View file

@ -25,9 +25,7 @@ struct Rustflags(String, TargetSelection);
impl Rustflags {
fn new(target: TargetSelection) -> Rustflags {
let mut ret = Rustflags(String::new(), target);
ret.propagate_cargo_env("RUSTFLAGS");
ret
Rustflags(String::new(), target)
}
/// By default, cargo will pick up on various variables in the environment. However, bootstrap
@ -60,6 +58,16 @@ impl Rustflags {
self.0.push_str(arg);
self
}
fn propagate_rustflag_envs(&mut self, build_compiler_stage: u32) {
self.propagate_cargo_env("RUSTFLAGS");
if build_compiler_stage != 0 {
self.env("RUSTFLAGS_NOT_BOOTSTRAP");
} else {
self.env("RUSTFLAGS_BOOTSTRAP");
self.arg("--cfg=bootstrap");
}
}
}
/// Flags that are passed to the `rustc` shim binary. These flags will only be applied when
@ -96,6 +104,7 @@ pub struct Cargo {
hostflags: HostFlags,
allow_features: String,
release_build: bool,
build_compiler_stage: u32,
}
impl Cargo {
@ -394,6 +403,28 @@ impl From<Cargo> for BootstrapCommand {
cargo.args.insert(0, "--release".into());
}
// Propagate the envs here at the very end to make sure they override any previously set flags.
cargo.rustflags.propagate_rustflag_envs(cargo.build_compiler_stage);
cargo.rustdocflags.propagate_rustflag_envs(cargo.build_compiler_stage);
cargo.rustdocflags.propagate_cargo_env("RUSTDOCFLAGS");
if cargo.build_compiler_stage == 0 {
cargo.rustdocflags.env("RUSTDOCFLAGS_BOOTSTRAP");
if let Ok(s) = env::var("CARGOFLAGS_BOOTSTRAP") {
cargo.args(s.split_whitespace());
}
} else {
cargo.rustdocflags.env("RUSTDOCFLAGS_NOT_BOOTSTRAP");
if let Ok(s) = env::var("CARGOFLAGS_NOT_BOOTSTRAP") {
cargo.args(s.split_whitespace());
}
}
if let Ok(s) = env::var("CARGOFLAGS") {
cargo.args(s.split_whitespace());
}
cargo.command.args(cargo.args);
let rustflags = &cargo.rustflags.0;
@ -601,18 +632,6 @@ impl Builder<'_> {
}
let mut rustflags = Rustflags::new(target);
if build_compiler_stage != 0 {
if let Ok(s) = env::var("CARGOFLAGS_NOT_BOOTSTRAP") {
cargo.args(s.split_whitespace());
}
rustflags.env("RUSTFLAGS_NOT_BOOTSTRAP");
} else {
if let Ok(s) = env::var("CARGOFLAGS_BOOTSTRAP") {
cargo.args(s.split_whitespace());
}
rustflags.env("RUSTFLAGS_BOOTSTRAP");
rustflags.arg("--cfg=bootstrap");
}
if cmd_kind == Kind::Clippy {
// clippy overwrites sysroot if we pass it to cargo.
@ -711,16 +730,6 @@ impl Builder<'_> {
// but this breaks CI. At the very least, stage0 `rustdoc` needs `--cfg bootstrap`. See
// #71458.
let mut rustdocflags = rustflags.clone();
rustdocflags.propagate_cargo_env("RUSTDOCFLAGS");
if build_compiler_stage == 0 {
rustdocflags.env("RUSTDOCFLAGS_BOOTSTRAP");
} else {
rustdocflags.env("RUSTDOCFLAGS_NOT_BOOTSTRAP");
}
if let Ok(s) = env::var("CARGOFLAGS") {
cargo.args(s.split_whitespace());
}
match mode {
Mode::Std | Mode::ToolBootstrap | Mode::ToolStd | Mode::ToolTarget => {}
@ -1374,6 +1383,7 @@ impl Builder<'_> {
hostflags,
allow_features,
release_build,
build_compiler_stage,
}
}
}

View file

@ -581,4 +581,9 @@ pub const CONFIG_CHANGE_HISTORY: &[ChangeInfo] = &[
severity: ChangeSeverity::Info,
summary: "The `build.python` option is now respected on macOS (previously ignored and forced to be /usr/bin/python3).",
},
ChangeInfo {
change_id: 148911,
severity: ChangeSeverity::Warning,
summary: "Flags from `*FLAGS*` (such as `RUSTFLAGS`) env. vars. now have precedence over rustflags set by bootstrap. Before, it was the other way around.",
},
];