Skip to content

Commit 9d1e4b7

Browse files
committed
Auto merge of #116448 - Kobzol:bootstrap-host-flags, r=onur-ozkan,petrochenkov
Pass rustc shim flags using environment variable This PR implements a generalized way of passing of host flags to the `rustc` shim in bootstrap, as proposed [here](#116278 (comment)). I tried to implement the bootstrap side using `OsString`, but then I realized that the shim code was using `env::var` before anyway, instead of `env::var_os`, so I just settled on a `String`. The shim side is still general and uses `env::vars_os` now. I'm not sure if we actually need to do something with the `rustdoc` shim. It *seems* to me that the env. vars passed to it (`RUSTDOC_LINKER`) and (`RUSTDOC_LLD_NO_THREADS`) could just be passed to cargo directly (or rather, the commands that they invoke in the shim could be passed directly). I'm not sure why are they set by the shim. r? `@onur-ozkan` CC `@petrochenkov`
2 parents 475c71d + 3f9ab7a commit 9d1e4b7

File tree

3 files changed

+57
-37
lines changed

3 files changed

+57
-37
lines changed

src/bootstrap/bin/_helper.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ fn parse_rustc_verbose() -> usize {
1414
/// Parses the value of the "RUSTC_STAGE" environment variable and returns it as a `String`.
1515
///
1616
/// If "RUSTC_STAGE" was not set, the program will be terminated with 101.
17+
#[allow(unused)]
1718
fn parse_rustc_stage() -> String {
1819
std::env::var("RUSTC_STAGE").unwrap_or_else(|_| {
1920
// Don't panic here; it's reasonable to try and run these shims directly. Give a helpful error instead.

src/bootstrap/bin/rustc.rs

+5-29
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ fn main() {
2727
let args = env::args_os().skip(1).collect::<Vec<_>>();
2828
let arg = |name| args.windows(2).find(|args| args[0] == name).and_then(|args| args[1].to_str());
2929

30-
let stage = parse_rustc_stage();
3130
let verbose = parse_rustc_verbose();
3231

3332
// Detect whether or not we're a build script depending on whether --target
@@ -108,36 +107,13 @@ fn main() {
108107
cmd.arg("-Ztls-model=initial-exec");
109108
}
110109
} else {
111-
// FIXME(rust-lang/cargo#5754) we shouldn't be using special env vars
112-
// here, but rather Cargo should know what flags to pass rustc itself.
113-
114-
// Override linker if necessary.
115-
if let Ok(host_linker) = env::var("RUSTC_HOST_LINKER") {
116-
cmd.arg(format!("-Clinker={host_linker}"));
117-
}
118-
if env::var_os("RUSTC_HOST_FUSE_LD_LLD").is_some() {
119-
cmd.arg("-Clink-args=-fuse-ld=lld");
120-
}
121-
122-
if let Ok(s) = env::var("RUSTC_HOST_CRT_STATIC") {
123-
if s == "true" {
124-
cmd.arg("-C").arg("target-feature=+crt-static");
110+
// Find any host flags that were passed by bootstrap.
111+
// The flags are stored in a RUSTC_HOST_FLAGS variable, separated by spaces.
112+
if let Ok(flags) = std::env::var("RUSTC_HOST_FLAGS") {
113+
for flag in flags.split(' ') {
114+
cmd.arg(flag);
125115
}
126-
if s == "false" {
127-
cmd.arg("-C").arg("target-feature=-crt-static");
128-
}
129-
}
130-
131-
// Cargo doesn't pass RUSTFLAGS to proc_macros:
132-
// https://github.com/rust-lang/cargo/issues/4423
133-
// Thus, if we are on stage 0, we explicitly set `--cfg=bootstrap`.
134-
// We also declare that the flag is expected, which we need to do to not
135-
// get warnings about it being unexpected.
136-
if stage == "0" {
137-
cmd.arg("--cfg=bootstrap");
138116
}
139-
cmd.arg("-Zunstable-options");
140-
cmd.arg("--check-cfg=values(bootstrap)");
141117
}
142118

143119
if let Ok(map) = env::var("RUSTC_DEBUGINFO_MAP") {

src/bootstrap/builder.rs

+51-8
Original file line numberDiff line numberDiff line change
@@ -1174,9 +1174,6 @@ impl<'a> Builder<'a> {
11741174
if let Some(linker) = self.linker(compiler.host) {
11751175
cmd.env("RUSTDOC_LINKER", linker);
11761176
}
1177-
if self.is_fuse_ld_lld(compiler.host) {
1178-
cmd.env("RUSTDOC_FUSE_LD_LLD", "1");
1179-
}
11801177
cmd
11811178
}
11821179

@@ -1268,6 +1265,8 @@ impl<'a> Builder<'a> {
12681265
let mut cargo = self.bare_cargo(compiler, mode, target, cmd);
12691266
let out_dir = self.stage_out(compiler, mode);
12701267

1268+
let mut hostflags = HostFlags::default();
1269+
12711270
// Codegen backends are not yet tracked by -Zbinary-dep-depinfo,
12721271
// so we need to explicitly clear out if they've been updated.
12731272
for backend in self.codegen_backends(compiler) {
@@ -1439,6 +1438,20 @@ impl<'a> Builder<'a> {
14391438
}
14401439
}
14411440

1441+
// FIXME(rust-lang/cargo#5754) we shouldn't be using special command arguments
1442+
// to the host invocation here, but rather Cargo should know what flags to pass rustc
1443+
// itself.
1444+
if stage == 0 {
1445+
hostflags.arg("--cfg=bootstrap");
1446+
}
1447+
// Cargo doesn't pass RUSTFLAGS to proc_macros:
1448+
// https://github.com/rust-lang/cargo/issues/4423
1449+
// Thus, if we are on stage 0, we explicitly set `--cfg=bootstrap`.
1450+
// We also declare that the flag is expected, which we need to do to not
1451+
// get warnings about it being unexpected.
1452+
hostflags.arg("-Zunstable-options");
1453+
hostflags.arg("--check-cfg=values(bootstrap)");
1454+
14421455
// FIXME: It might be better to use the same value for both `RUSTFLAGS` and `RUSTDOCFLAGS`,
14431456
// but this breaks CI. At the very least, stage0 `rustdoc` needs `--cfg bootstrap`. See
14441457
// #71458.
@@ -1655,11 +1668,10 @@ impl<'a> Builder<'a> {
16551668
}
16561669

16571670
if let Some(host_linker) = self.linker(compiler.host) {
1658-
cargo.env("RUSTC_HOST_LINKER", host_linker);
1671+
hostflags.arg(format!("-Clinker={}", host_linker.display()));
16591672
}
16601673
if self.is_fuse_ld_lld(compiler.host) {
1661-
cargo.env("RUSTC_HOST_FUSE_LD_LLD", "1");
1662-
cargo.env("RUSTDOC_FUSE_LD_LLD", "1");
1674+
hostflags.arg("-Clink-args=-fuse-ld=lld");
16631675
}
16641676

16651677
if let Some(target_linker) = self.linker(target) {
@@ -1743,7 +1755,8 @@ impl<'a> Builder<'a> {
17431755
}
17441756

17451757
if let Some(x) = self.crt_static(compiler.host) {
1746-
cargo.env("RUSTC_HOST_CRT_STATIC", x.to_string());
1758+
let sign = if x { "+" } else { "-" };
1759+
hostflags.arg(format!("-Ctarget-feature={sign}crt-static"));
17471760
}
17481761

17491762
if let Some(map_to) = self.build.debuginfo_map_to(GitRepo::Rustc) {
@@ -2055,7 +2068,7 @@ impl<'a> Builder<'a> {
20552068
cargo.env("RUSTFLAGS", &rustc_args.join(" "));
20562069
}
20572070

2058-
Cargo { command: cargo, rustflags, rustdocflags, allow_features }
2071+
Cargo { command: cargo, rustflags, rustdocflags, hostflags, allow_features }
20592072
}
20602073

20612074
/// Ensure that a given step is built, returning its output. This will
@@ -2233,11 +2246,36 @@ impl Rustflags {
22332246
}
22342247
}
22352248

2249+
/// Flags that are passed to the `rustc` shim binary.
2250+
/// These flags will only be applied when compiling host code, i.e. when
2251+
/// `--target` is unset.
2252+
#[derive(Debug, Default)]
2253+
pub struct HostFlags {
2254+
rustc: Vec<String>,
2255+
}
2256+
2257+
impl HostFlags {
2258+
const SEPARATOR: &'static str = " ";
2259+
2260+
/// Adds a host rustc flag.
2261+
fn arg<S: Into<String>>(&mut self, flag: S) {
2262+
let value = flag.into().trim().to_string();
2263+
assert!(!value.contains(Self::SEPARATOR));
2264+
self.rustc.push(value);
2265+
}
2266+
2267+
/// Encodes all the flags into a single string.
2268+
fn encode(self) -> String {
2269+
self.rustc.join(Self::SEPARATOR)
2270+
}
2271+
}
2272+
22362273
#[derive(Debug)]
22372274
pub struct Cargo {
22382275
command: Command,
22392276
rustflags: Rustflags,
22402277
rustdocflags: Rustflags,
2278+
hostflags: HostFlags,
22412279
allow_features: String,
22422280
}
22432281

@@ -2309,6 +2347,11 @@ impl From<Cargo> for Command {
23092347
cargo.command.env("RUSTDOCFLAGS", rustdocflags);
23102348
}
23112349

2350+
let encoded_hostflags = cargo.hostflags.encode();
2351+
if !encoded_hostflags.is_empty() {
2352+
cargo.command.env("RUSTC_HOST_FLAGS", encoded_hostflags);
2353+
}
2354+
23122355
if !cargo.allow_features.is_empty() {
23132356
cargo.command.env("RUSTC_ALLOW_FEATURES", cargo.allow_features);
23142357
}

0 commit comments

Comments
 (0)