diff options
| author | Jakub Beránek <jakub.beranek@vsb.cz> | 2023-10-05 10:47:59 +0200 |
|---|---|---|
| committer | Jakub Beránek <jakub.beranek@vsb.cz> | 2023-10-05 15:40:34 +0200 |
| commit | eddbd7c6a0f45578e89c8f509bfff0c048a580d0 (patch) | |
| tree | 85a767cb4af1a3c08373c3ddef451b8614f47f98 | |
| parent | c373a05bdbc4cd0b140ae3c08122e3d597869c94 (diff) | |
| download | rust-eddbd7c6a0f45578e89c8f509bfff0c048a580d0.tar.gz rust-eddbd7c6a0f45578e89c8f509bfff0c048a580d0.zip | |
Pass host flags to `rustc` shim using prefixed env. vars
| -rw-r--r-- | src/bootstrap/bin/rustc.rs | 19 | ||||
| -rw-r--r-- | src/bootstrap/builder.rs | 41 |
2 files changed, 42 insertions, 18 deletions
diff --git a/src/bootstrap/bin/rustc.rs b/src/bootstrap/bin/rustc.rs index 20cd63b966b..119000b0935 100644 --- a/src/bootstrap/bin/rustc.rs +++ b/src/bootstrap/bin/rustc.rs @@ -111,20 +111,11 @@ fn main() { // FIXME(rust-lang/cargo#5754) we shouldn't be using special env vars // here, but rather Cargo should know what flags to pass rustc itself. - // Override linker if necessary. - if let Ok(host_linker) = env::var("RUSTC_HOST_LINKER") { - cmd.arg(format!("-Clinker={host_linker}")); - } - if env::var_os("RUSTC_HOST_FUSE_LD_LLD").is_some() { - cmd.arg("-Clink-args=-fuse-ld=lld"); - } - - if let Ok(s) = env::var("RUSTC_HOST_CRT_STATIC") { - if s == "true" { - cmd.arg("-C").arg("target-feature=+crt-static"); - } - if s == "false" { - cmd.arg("-C").arg("target-feature=-crt-static"); + // Find any host flags that were passed by bootstrap. + // The flags are stored in a RUSTC_HOST_FLAGS variable, separated by spaces. + if let Ok(flags) = std::env::var("RUSTC_HOST_FLAGS") { + for flag in flags.split(' ') { + cmd.arg(flag); } } diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index 7decfb00dac..a3e1a53b5de 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -1265,6 +1265,8 @@ impl<'a> Builder<'a> { let mut cargo = self.bare_cargo(compiler, mode, target, cmd); let out_dir = self.stage_out(compiler, mode); + let mut hostflags = HostFlags::default(); + // Codegen backends are not yet tracked by -Zbinary-dep-depinfo, // so we need to explicitly clear out if they've been updated. for backend in self.codegen_backends(compiler) { @@ -1652,10 +1654,10 @@ impl<'a> Builder<'a> { } if let Some(host_linker) = self.linker(compiler.host) { - cargo.env("RUSTC_HOST_LINKER", host_linker); + hostflags.flag(format!("-Clinker={}", host_linker.display())); } if self.is_fuse_ld_lld(compiler.host) { - cargo.env("RUSTC_HOST_FUSE_LD_LLD", "1"); + hostflags.flag("-Clink-args=-fuse-ld=lld"); } if let Some(target_linker) = self.linker(target) { @@ -1739,7 +1741,8 @@ impl<'a> Builder<'a> { } if let Some(x) = self.crt_static(compiler.host) { - cargo.env("RUSTC_HOST_CRT_STATIC", x.to_string()); + let sign = if x { "+" } else { "-" }; + hostflags.flag(format!("-Ctarget-feature={sign}crt-static")); } if let Some(map_to) = self.build.debuginfo_map_to(GitRepo::Rustc) { @@ -2051,7 +2054,7 @@ impl<'a> Builder<'a> { cargo.env("RUSTFLAGS", &rustc_args.join(" ")); } - Cargo { command: cargo, rustflags, rustdocflags, allow_features } + Cargo { command: cargo, rustflags, rustdocflags, hostflags, allow_features } } /// Ensure that a given step is built, returning its output. This will @@ -2229,11 +2232,36 @@ impl Rustflags { } } +/// Flags that are passed to the `rustc` shim binary. +/// These flags will only be applied when compiling host code, i.e. when +/// `--target` is unset. +#[derive(Debug, Default)] +pub struct HostFlags { + rustc: Vec<String>, +} + +impl HostFlags { + const SEPARATOR: &'static str = " "; + + /// Adds a host rustc flag. + fn flag<S: Into<String>>(&mut self, flag: S) { + let value = flag.into().trim().to_string(); + assert!(!value.contains(Self::SEPARATOR)); + self.rustc.push(value); + } + + /// Encodes all the flags into a single string. + fn encode(self) -> String { + self.rustc.join(Self::SEPARATOR) + } +} + #[derive(Debug)] pub struct Cargo { command: Command, rustflags: Rustflags, rustdocflags: Rustflags, + hostflags: HostFlags, allow_features: String, } @@ -2305,6 +2333,11 @@ impl From<Cargo> for Command { cargo.command.env("RUSTDOCFLAGS", rustdocflags); } + let encoded_hostflags = cargo.hostflags.encode(); + if !encoded_hostflags.is_empty() { + cargo.command.env("RUSTC_HOST_FLAGS", encoded_hostflags); + } + if !cargo.allow_features.is_empty() { cargo.command.env("RUSTC_ALLOW_FEATURES", cargo.allow_features); } |
