Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a3fd45d

Browse files
committedOct 5, 2023
Pass host flags to rustc shim using prefixed env. vars
1 parent c373a05 commit a3fd45d

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed
 

‎src/bootstrap/bin/rustc.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,13 @@ fn main() {
111111
// FIXME(rust-lang/cargo#5754) we shouldn't be using special env vars
112112
// here, but rather Cargo should know what flags to pass rustc itself.
113113

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");
125-
}
126-
if s == "false" {
127-
cmd.arg("-C").arg("target-feature=-crt-static");
114+
// Find flags with host commands passed by bootstrap and use them.
115+
// These flags are encoded in environment variables with the RUSTC_HOST_CMD_
116+
// prefix.
117+
for (key, val) in std::env::vars_os() {
118+
let key: &std::path::Path = key.as_ref();
119+
if key.starts_with("RUSTC_HOST_CMD_") {
120+
cmd.arg(val);
128121
}
129122
}
130123

‎src/bootstrap/builder.rs

+28-4
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,8 @@ impl<'a> Builder<'a> {
12651265
let mut cargo = self.bare_cargo(compiler, mode, target, cmd);
12661266
let out_dir = self.stage_out(compiler, mode);
12671267

1268+
let mut hostflags = HostFlags::default();
1269+
12681270
// Codegen backends are not yet tracked by -Zbinary-dep-depinfo,
12691271
// so we need to explicitly clear out if they've been updated.
12701272
for backend in self.codegen_backends(compiler) {
@@ -1652,10 +1654,10 @@ impl<'a> Builder<'a> {
16521654
}
16531655

16541656
if let Some(host_linker) = self.linker(compiler.host) {
1655-
cargo.env("RUSTC_HOST_LINKER", host_linker);
1657+
hostflags.flag(format!("-Clinker={}", host_linker.display()));
16561658
}
16571659
if self.is_fuse_ld_lld(compiler.host) {
1658-
cargo.env("RUSTC_HOST_FUSE_LD_LLD", "1");
1660+
hostflags.flag("-Clink-args=-fuse-ld=lld");
16591661
}
16601662

16611663
if let Some(target_linker) = self.linker(target) {
@@ -1739,7 +1741,8 @@ impl<'a> Builder<'a> {
17391741
}
17401742

17411743
if let Some(x) = self.crt_static(compiler.host) {
1742-
cargo.env("RUSTC_HOST_CRT_STATIC", x.to_string());
1744+
let sign = if x { "+" } else { "-" };
1745+
hostflags.flag(format!("-Ctarget-feature={sign}crt-static"));
17431746
}
17441747

17451748
if let Some(map_to) = self.build.debuginfo_map_to(GitRepo::Rustc) {
@@ -2051,7 +2054,7 @@ impl<'a> Builder<'a> {
20512054
cargo.env("RUSTFLAGS", &rustc_args.join(" "));
20522055
}
20532056

2054-
Cargo { command: cargo, rustflags, rustdocflags, allow_features }
2057+
Cargo { command: cargo, rustflags, rustdocflags, hostflags, allow_features }
20552058
}
20562059

20572060
/// Ensure that a given step is built, returning its output. This will
@@ -2229,11 +2232,27 @@ impl Rustflags {
22292232
}
22302233
}
22312234

2235+
/// Flags that are passed to the `rustc` and `rustdoc` shim binaries.
2236+
/// These flags will only be applied when compiling host code, i.e. when
2237+
/// `--target` is unset.
2238+
#[derive(Debug, Default)]
2239+
pub struct HostFlags {
2240+
rustc: Vec<String>,
2241+
}
2242+
2243+
impl HostFlags {
2244+
/// Adds a host rustc flag.
2245+
fn flag<S: Into<String>>(&mut self, flag: S) {
2246+
self.rustc.push(flag.into());
2247+
}
2248+
}
2249+
22322250
#[derive(Debug)]
22332251
pub struct Cargo {
22342252
command: Command,
22352253
rustflags: Rustflags,
22362254
rustdocflags: Rustflags,
2255+
hostflags: HostFlags,
22372256
allow_features: String,
22382257
}
22392258

@@ -2305,6 +2324,11 @@ impl From<Cargo> for Command {
23052324
cargo.command.env("RUSTDOCFLAGS", rustdocflags);
23062325
}
23072326

2327+
let hostflags = cargo.hostflags;
2328+
for (index, flag) in hostflags.rustc.into_iter().enumerate() {
2329+
cargo.command.env(format!("RUSTC_HOST_CMD_{index}"), flag);
2330+
}
2331+
23082332
if !cargo.allow_features.is_empty() {
23092333
cargo.command.env("RUSTC_ALLOW_FEATURES", cargo.allow_features);
23102334
}

0 commit comments

Comments
 (0)
Failed to load comments.