From f6c87aba328f5d648786237da70fd227642b9c20 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 15 Aug 2019 13:42:39 -0700 Subject: [PATCH] bootstrap: Add a helper for managing RUSTFLAGS Most of `bootstrap/bin/rustc.rs` doesn't need to exist with the advent of `RUSTFLAGS` (yes this is super old) so this starts by refactoring a bit to make it easier locally in the `Builder::cargo` method to append to `RUSTFLAGS` that gets down to rustc. --- src/bootstrap/builder.rs | 51 ++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index b7873fd1d35..399130d6755 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -860,28 +860,16 @@ impl<'a> Builder<'a> { stage = compiler.stage; } - let mut extra_args = String::new(); + let mut rustflags = Rustflags::new(); + rustflags.env(&format!("RUSTFLAGS_STAGE_{}", stage)); if stage != 0 { - let s = env::var("RUSTFLAGS_NOT_BOOTSTRAP").unwrap_or_default(); - extra_args.push_str(&s); + rustflags.env("RUSTFLAGS_NOT_BOOTSTRAP"); } else { - let s = env::var("RUSTFLAGS_BOOTSTRAP").unwrap_or_default(); - extra_args.push_str(&s); + rustflags.env("RUSTFLAGS_BOOTSTRAP"); } if cmd == "clippy" { - extra_args.push_str("-Zforce-unstable-if-unmarked"); - } - - if !extra_args.is_empty() { - cargo.env( - "RUSTFLAGS", - format!( - "{} {}", - env::var("RUSTFLAGS").unwrap_or_default(), - extra_args - ), - ); + rustflags.arg("-Zforce-unstable-if-unmarked"); } let want_rustdoc = self.doc_tests != DocTests::No; @@ -1171,6 +1159,8 @@ impl<'a> Builder<'a> { self.ci_env.force_coloring_in_ci(&mut cargo); + cargo.env("RUSTFLAGS", &rustflags.0); + cargo } @@ -1271,3 +1261,30 @@ impl<'a> Builder<'a> { #[cfg(test)] mod tests; + +struct Rustflags(String); + +impl Rustflags { + fn new() -> Rustflags { + let mut ret = Rustflags(String::new()); + ret.env("RUSTFLAGS"); + return ret; + } + + fn env(&mut self, env: &str) { + if let Ok(s) = env::var(env) { + for part in s.split_whitespace() { + self.arg(part); + } + } + } + + fn arg(&mut self, arg: &str) -> &mut Self { + assert_eq!(arg.split_whitespace().count(), 1); + if self.0.len() > 0 { + self.0.push_str(" "); + } + self.0.push_str(arg); + self + } +}