Skip to content

Commit 889bd0a

Browse files
authored
Unrolled build for #148911
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`
2 parents cc57d9a + afa24cd commit 889bd0a

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

src/bootstrap/src/core/builder/cargo.rs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ struct Rustflags(String, TargetSelection);
2525

2626
impl Rustflags {
2727
fn new(target: TargetSelection) -> Rustflags {
28-
let mut ret = Rustflags(String::new(), target);
29-
ret.propagate_cargo_env("RUSTFLAGS");
30-
ret
28+
Rustflags(String::new(), target)
3129
}
3230

3331
/// By default, cargo will pick up on various variables in the environment. However, bootstrap
@@ -60,6 +58,16 @@ impl Rustflags {
6058
self.0.push_str(arg);
6159
self
6260
}
61+
62+
fn propagate_rustflag_envs(&mut self, build_compiler_stage: u32) {
63+
self.propagate_cargo_env("RUSTFLAGS");
64+
if build_compiler_stage != 0 {
65+
self.env("RUSTFLAGS_NOT_BOOTSTRAP");
66+
} else {
67+
self.env("RUSTFLAGS_BOOTSTRAP");
68+
self.arg("--cfg=bootstrap");
69+
}
70+
}
6371
}
6472

6573
/// Flags that are passed to the `rustc` shim binary. These flags will only be applied when
@@ -96,6 +104,7 @@ pub struct Cargo {
96104
hostflags: HostFlags,
97105
allow_features: String,
98106
release_build: bool,
107+
build_compiler_stage: u32,
99108
}
100109

101110
impl Cargo {
@@ -394,6 +403,28 @@ impl From<Cargo> for BootstrapCommand {
394403
cargo.args.insert(0, "--release".into());
395404
}
396405

406+
// Propagate the envs here at the very end to make sure they override any previously set flags.
407+
cargo.rustflags.propagate_rustflag_envs(cargo.build_compiler_stage);
408+
cargo.rustdocflags.propagate_rustflag_envs(cargo.build_compiler_stage);
409+
410+
cargo.rustdocflags.propagate_cargo_env("RUSTDOCFLAGS");
411+
412+
if cargo.build_compiler_stage == 0 {
413+
cargo.rustdocflags.env("RUSTDOCFLAGS_BOOTSTRAP");
414+
if let Ok(s) = env::var("CARGOFLAGS_BOOTSTRAP") {
415+
cargo.args(s.split_whitespace());
416+
}
417+
} else {
418+
cargo.rustdocflags.env("RUSTDOCFLAGS_NOT_BOOTSTRAP");
419+
if let Ok(s) = env::var("CARGOFLAGS_NOT_BOOTSTRAP") {
420+
cargo.args(s.split_whitespace());
421+
}
422+
}
423+
424+
if let Ok(s) = env::var("CARGOFLAGS") {
425+
cargo.args(s.split_whitespace());
426+
}
427+
397428
cargo.command.args(cargo.args);
398429

399430
let rustflags = &cargo.rustflags.0;
@@ -601,18 +632,6 @@ impl Builder<'_> {
601632
}
602633

603634
let mut rustflags = Rustflags::new(target);
604-
if build_compiler_stage != 0 {
605-
if let Ok(s) = env::var("CARGOFLAGS_NOT_BOOTSTRAP") {
606-
cargo.args(s.split_whitespace());
607-
}
608-
rustflags.env("RUSTFLAGS_NOT_BOOTSTRAP");
609-
} else {
610-
if let Ok(s) = env::var("CARGOFLAGS_BOOTSTRAP") {
611-
cargo.args(s.split_whitespace());
612-
}
613-
rustflags.env("RUSTFLAGS_BOOTSTRAP");
614-
rustflags.arg("--cfg=bootstrap");
615-
}
616635

617636
if cmd_kind == Kind::Clippy {
618637
// clippy overwrites sysroot if we pass it to cargo.
@@ -711,16 +730,6 @@ impl Builder<'_> {
711730
// but this breaks CI. At the very least, stage0 `rustdoc` needs `--cfg bootstrap`. See
712731
// #71458.
713732
let mut rustdocflags = rustflags.clone();
714-
rustdocflags.propagate_cargo_env("RUSTDOCFLAGS");
715-
if build_compiler_stage == 0 {
716-
rustdocflags.env("RUSTDOCFLAGS_BOOTSTRAP");
717-
} else {
718-
rustdocflags.env("RUSTDOCFLAGS_NOT_BOOTSTRAP");
719-
}
720-
721-
if let Ok(s) = env::var("CARGOFLAGS") {
722-
cargo.args(s.split_whitespace());
723-
}
724733

725734
match mode {
726735
Mode::Std | Mode::ToolBootstrap | Mode::ToolStd | Mode::ToolTarget => {}
@@ -1374,6 +1383,7 @@ impl Builder<'_> {
13741383
hostflags,
13751384
allow_features,
13761385
release_build,
1386+
build_compiler_stage,
13771387
}
13781388
}
13791389
}

src/bootstrap/src/utils/change_tracker.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,4 +581,9 @@ pub const CONFIG_CHANGE_HISTORY: &[ChangeInfo] = &[
581581
severity: ChangeSeverity::Info,
582582
summary: "The `build.python` option is now respected on macOS (previously ignored and forced to be /usr/bin/python3).",
583583
},
584+
ChangeInfo {
585+
change_id: 148911,
586+
severity: ChangeSeverity::Warning,
587+
summary: "Flags from `*FLAGS*` (such as `RUSTFLAGS`) env. vars. now have precedence over rustflags set by bootstrap. Before, it was the other way around.",
588+
},
584589
];

0 commit comments

Comments
 (0)