diff --git a/src/cargo/core/compiler/build_context/mod.rs b/src/cargo/core/compiler/build_context/mod.rs index aca9a8ad25e..d72cf454519 100644 --- a/src/cargo/core/compiler/build_context/mod.rs +++ b/src/cargo/core/compiler/build_context/mod.rs @@ -8,9 +8,8 @@ use log::debug; use crate::core::profiles::Profiles; use crate::core::{Dependency, Workspace}; use crate::core::{PackageId, PackageSet, Resolve}; +use crate::util; use crate::util::errors::CargoResult; -use crate::util::paths; -use crate::util::rustc::RustcWrapper; use crate::util::{profile, Cfg, CfgExpr, Config, Rustc}; use super::{BuildConfig, BuildOutput, Kind, Unit}; @@ -55,26 +54,16 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> { let mut rustc = config.load_global_rustc(Some(ws))?; if build_config.clippy_override { - let tool = config.get_tool("clippy-driver")?; - let tool = paths::resolve_executable(&tool).map_err(|e| { - let rustup_in_path = config - .get_tool("rustup") - .and_then(|tool| paths::resolve_executable(&tool)) - .is_ok(); - let has_rustup_env = std::env::var("RUSTUP_TOOLCHAIN").is_ok(); - if rustup_in_path || has_rustup_env { - failure::format_err!("{}: please run `rustup component add clippy`", e) - } else { - failure::format_err!("{}: please install clippy", e) - } - })?; - rustc.push_wrapper(RustcWrapper::new(tool)); + rustc.set_wrapper(util::process("clippy-driver")); } else if build_config.cargo_as_rustc_wrapper { - let mut wrapper = RustcWrapper::new(env::current_exe()?); + let mut wrapper = util::process(env::current_exe()?); for (k, v) in build_config.extra_rustc_env.iter() { wrapper.env(k, v); } - rustc.push_wrapper(wrapper); + for arg in build_config.extra_rustc_args.iter() { + wrapper.arg(arg); + } + rustc.set_wrapper(wrapper); } let host_config = TargetConfig::new(config, &rustc.host)?; diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index 0f162b72bc0..13d5d96be57 100644 --- a/src/cargo/core/compiler/compilation.rs +++ b/src/cargo/core/compiler/compilation.rs @@ -80,9 +80,6 @@ impl<'cfg> Compilation<'cfg> { if bcx.config.extra_verbose() { rustc.display_env_vars(); } - for arg in bcx.build_config.extra_rustc_args.iter() { - rustc.arg(arg); - } let srv = bcx.build_config.rustfix_diagnostic_server.borrow(); if let Some(server) = &*srv { server.configure(&mut rustc); diff --git a/src/cargo/util/rustc.rs b/src/cargo/util/rustc.rs index ec275fdfcf8..3a3451317ef 100644 --- a/src/cargo/util/rustc.rs +++ b/src/cargo/util/rustc.rs @@ -2,7 +2,6 @@ use std::collections::hash_map::{Entry, HashMap}; use std::env; -use std::ffi::{OsStr, OsString}; use std::hash::{Hash, Hasher, SipHasher}; use std::path::{Path, PathBuf}; use std::process::Stdio; @@ -21,7 +20,7 @@ pub struct Rustc { pub path: PathBuf, /// An optional program that will be passed the path of the rust exe as its first argument, and /// rustc args following this. - pub wrapper: Option, + pub wrapper: Option, /// Verbose version information (the output of `rustc -vV`) pub verbose_version: String, /// The host triple (arch-platform-OS), this comes from verbose_version. @@ -29,56 +28,6 @@ pub struct Rustc { cache: Mutex, } -/// Information on the `rustc` wrapper -#[derive(Debug)] -pub struct RustcWrapper { - path: PathBuf, - env: HashMap>, -} - -impl RustcWrapper { - pub fn new>(path: T) -> RustcWrapper { - RustcWrapper { - path: path.into(), - env: HashMap::new(), - } - } - - /// (chainable) Sets an environment variable for the process. - pub fn env>(&mut self, key: &str, val: T) -> &mut RustcWrapper { - self.env - .insert(key.to_string(), Some(val.as_ref().to_os_string())); - self - } - - /// (chainable) Unsets an environment variable for the process. - pub fn env_remove(&mut self, key: &str) -> &mut RustcWrapper { - self.env.insert(key.to_string(), None); - self - } - - pub fn process(&self) -> ProcessBuilder { - let mut cmd = util::process(&self.path); - - for (k, v) in &self.env { - match *v { - Some(ref v) => { - cmd.env(k, v); - } - None => { - cmd.env_remove(k); - } - } - } - - cmd - } - - pub fn is_empty(&self) -> bool { - self.path.as_os_str().is_empty() - } -} - impl Rustc { /// Runs the compiler at `path` to learn various pieces of information about /// it, with an optional wrapper. @@ -110,7 +59,7 @@ impl Rustc { Ok(Rustc { path, - wrapper: wrapper.map(RustcWrapper::new), + wrapper: wrapper.map(util::process), verbose_version, host, cache: Mutex::new(cache), @@ -120,8 +69,8 @@ impl Rustc { /// Gets a process builder set up to use the found rustc version, with a wrapper if `Some`. pub fn process(&self) -> ProcessBuilder { match self.wrapper { - Some(ref wrapper) if !wrapper.is_empty() => { - let mut cmd = wrapper.process(); + Some(ref wrapper) if !wrapper.get_program().is_empty() => { + let mut cmd = wrapper.clone(); cmd.arg(&self.path); cmd } @@ -141,8 +90,8 @@ impl Rustc { self.cache.lock().unwrap().cached_success(cmd) } - pub fn push_wrapper>>(&mut self, wrapper: T) { - self.wrapper = wrapper.into(); + pub fn set_wrapper(&mut self, wrapper: ProcessBuilder) { + self.wrapper = Some(wrapper); } }