Skip to content

Commit 0389edc

Browse files
committed
impl centralized RustcWrapper logic
1 parent e2f2c8d commit 0389edc

File tree

11 files changed

+111
-64
lines changed

11 files changed

+111
-64
lines changed

src/bin/cargo/cli.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Run with 'cargo -Z [FLAG] [SUBCOMMAND]'"
4848
}
4949

5050
if let Some(code) = args.value_of("explain") {
51-
let mut procss = config.rustc(None)?.process();
51+
let mut procss = config.load_global_rustc(None)?.process();
5252
procss.arg("--explain").arg(code).exec()?;
5353
return Ok(());
5454
}
@@ -236,4 +236,3 @@ See 'cargo help <command>' for more information on a specific command.\n",
236236
)
237237
.subcommands(commands::builtin())
238238
}
239-

src/bin/cargo/commands/clippy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ You can use tool lints to allow or deny lints from your code, eg.:
5656
}
5757

5858
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
59-
config.set_clippy_override(true);
6059
let ws = args.workspace(config)?;
6160

6261
let mode = CompileMode::Check { test: false };
63-
let compile_opts = args.compile_options(config, mode, Some(&ws))?;
62+
let mut compile_opts = args.compile_options(config, mode, Some(&ws))?;
63+
compile_opts.build_config.set_clippy_override(true);
6464

6565
if !config.cli_unstable().unstable_options {
6666
return Err(failure::format_err!(

src/cargo/core/compiler/build_config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub struct BuildConfig {
3030
/// Extra args to inject into rustc commands.
3131
pub extra_rustc_args: Vec<String>,
3232
pub rustfix_diagnostic_server: RefCell<Option<RustfixDiagnosticServer>>,
33+
/// Use `clippy-driver` instead of `rustc`
34+
pub clippy_override: bool,
3335
}
3436

3537
impl BuildConfig {
@@ -102,6 +104,7 @@ impl BuildConfig {
102104
extra_rustc_env: Vec::new(),
103105
extra_rustc_args: Vec::new(),
104106
rustfix_diagnostic_server: RefCell::new(None),
107+
clippy_override: false,
105108
})
106109
}
107110

@@ -112,6 +115,11 @@ impl BuildConfig {
112115
pub fn test(&self) -> bool {
113116
self.mode == CompileMode::Test || self.mode == CompileMode::Bench
114117
}
118+
119+
/// Sets the clippy override. If this is true, clippy-driver is invoked instead of rustc.
120+
pub fn set_clippy_override(&mut self, val: bool) {
121+
self.clippy_override = val;
122+
}
115123
}
116124

117125
#[derive(Clone, Copy, Debug, PartialEq, Eq)]

src/cargo/core/compiler/build_context/mod.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use crate::core::profiles::Profiles;
99
use crate::core::{Dependency, Workspace};
1010
use crate::core::{PackageId, PackageSet, Resolve};
1111
use crate::util::errors::CargoResult;
12+
use crate::util::paths;
13+
use crate::util::rustc::RustcWrapper;
1214
use crate::util::{profile, Cfg, CfgExpr, Config, Rustc};
1315

1416
use super::{BuildConfig, BuildOutput, Kind, Unit};
@@ -50,7 +52,30 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
5052
profiles: &'a Profiles,
5153
extra_compiler_args: HashMap<Unit<'a>, Vec<String>>,
5254
) -> CargoResult<BuildContext<'a, 'cfg>> {
53-
let rustc = config.rustc(Some(ws))?;
55+
let mut rustc = config.load_global_rustc(Some(ws))?;
56+
57+
if build_config.clippy_override {
58+
let tool = config.get_tool("clippy-driver")?;
59+
let tool = paths::resolve_executable(&tool).map_err(|e| {
60+
let rustup_in_path = config
61+
.get_tool("rustup")
62+
.and_then(|tool| paths::resolve_executable(&tool))
63+
.is_ok();
64+
let has_rustup_env = std::env::var("RUSTUP_TOOLCHAIN").is_ok();
65+
if rustup_in_path || has_rustup_env {
66+
failure::format_err!("{}: please run `rustup component add clippy`", e)
67+
} else {
68+
failure::format_err!("{}: please install clippy", e)
69+
}
70+
})?;
71+
rustc.push_wrapper(RustcWrapper::new(tool));
72+
} else if build_config.cargo_as_rustc_wrapper {
73+
let mut wrapper = RustcWrapper::new(env::current_exe()?);
74+
let prog = rustc.path.as_os_str().to_owned();
75+
wrapper.env("RUSTC", prog);
76+
rustc.push_wrapper(wrapper);
77+
}
78+
5479
let host_config = TargetConfig::new(config, &rustc.host)?;
5580
let target_config = match build_config.requested_target.as_ref() {
5681
Some(triple) => TargetConfig::new(config, triple)?,

src/cargo/core/compiler/compilation.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,8 @@ pub struct Compilation<'cfg> {
7575

7676
impl<'cfg> Compilation<'cfg> {
7777
pub fn new<'a>(bcx: &BuildContext<'a, 'cfg>) -> CargoResult<Compilation<'cfg>> {
78-
// If we're using cargo as a rustc wrapper then we're in a situation
79-
// like `cargo fix`. For now just disregard the `RUSTC_WRAPPER` env var
80-
// (which is typically set to `sccache` for now). Eventually we'll
81-
// probably want to implement `RUSTC_WRAPPER` for `cargo fix`, but we'll
82-
// leave that open as a bug for now.
83-
let mut rustc = if bcx.build_config.cargo_as_rustc_wrapper {
84-
let mut rustc = bcx.rustc.process_no_wrapper();
85-
let prog = rustc.get_program().to_owned();
86-
rustc.env("RUSTC", prog);
87-
rustc.program(env::current_exe()?);
88-
rustc
89-
} else {
90-
bcx.rustc.process()
91-
};
78+
let mut rustc = bcx.rustc.process();
79+
9280
if bcx.config.extra_verbose() {
9381
rustc.display_env_vars();
9482
}

src/cargo/core/compiler/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@ use log::debug;
2222
use same_file::is_same_file;
2323
use serde::Serialize;
2424

25-
use crate::core::manifest::TargetSourcePath;
26-
use crate::core::profiles::{Lto, PanicStrategy, Profile};
27-
use crate::core::{PackageId, Target};
28-
use crate::util::errors::{CargoResult, CargoResultExt, Internal, ProcessError};
29-
use crate::util::paths;
30-
use crate::util::{self, machine_message, process, Freshness, ProcessBuilder};
31-
use crate::util::{internal, join_paths, profile};
3225
pub use self::build_config::{BuildConfig, CompileMode, MessageFormat};
3326
pub use self::build_context::{BuildContext, FileFlavor, TargetConfig, TargetInfo};
3427
use self::build_plan::BuildPlan;
@@ -39,6 +32,13 @@ use self::job::{Job, Work};
3932
use self::job_queue::JobQueue;
4033
pub use self::layout::is_bad_artifact_name;
4134
use self::output_depinfo::output_depinfo;
35+
use crate::core::manifest::TargetSourcePath;
36+
use crate::core::profiles::{Lto, PanicStrategy, Profile};
37+
use crate::core::{PackageId, Target};
38+
use crate::util::errors::{CargoResult, CargoResultExt, Internal, ProcessError};
39+
use crate::util::paths;
40+
use crate::util::{self, machine_message, process, Freshness, ProcessBuilder};
41+
use crate::util::{internal, join_paths, profile};
4242

4343
/// Indicates whether an object is for the host architcture or the target architecture.
4444
///

src/cargo/ops/cargo_fetch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn fetch<'a>(
2121
let jobs = Some(1);
2222
let config = ws.config();
2323
let build_config = BuildConfig::new(config, jobs, &options.target, CompileMode::Build)?;
24-
let rustc = config.rustc(Some(ws))?;
24+
let rustc = config.load_global_rustc(Some(ws))?;
2525
let target_info =
2626
TargetInfo::new(config, &build_config.requested_target, &rustc, Kind::Target)?;
2727
{

src/cargo/util/config.rs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ pub struct Config {
7474
env: HashMap<String, String>,
7575
/// Profiles loaded from config.
7676
profiles: LazyCell<ConfigProfiles>,
77-
/// Use `clippy-driver` instead of `rustc`
78-
clippy_override: bool,
7977
}
8078

8179
impl Config {
@@ -131,7 +129,6 @@ impl Config {
131129
target_dir: None,
132130
env,
133131
profiles: LazyCell::new(),
134-
clippy_override: false,
135132
}
136133
}
137134

@@ -193,36 +190,14 @@ impl Config {
193190
.map(AsRef::as_ref)
194191
}
195192

196-
/// Sets the clippy override. If this is true, clippy-driver is invoked instead of rustc.
197-
pub fn set_clippy_override(&mut self, val: bool) {
198-
self.clippy_override = val;
199-
}
200-
201193
/// Gets the path to the `rustc` executable.
202-
pub fn rustc(&self, ws: Option<&Workspace<'_>>) -> CargoResult<Rustc> {
194+
pub fn load_global_rustc(&self, ws: Option<&Workspace<'_>>) -> CargoResult<Rustc> {
203195
let cache_location = ws.map(|ws| {
204196
ws.target_dir()
205197
.join(".rustc_info.json")
206198
.into_path_unlocked()
207199
});
208-
let wrapper = if self.clippy_override {
209-
let tool = self.get_tool("clippy-driver")?;
210-
let tool = paths::resolve_executable(&tool).map_err(|e| {
211-
let rustup_in_path = self
212-
.get_tool("rustup")
213-
.and_then(|tool| paths::resolve_executable(&tool))
214-
.is_ok();
215-
let has_rustup_env = std::env::var("RUSTUP_TOOLCHAIN").is_ok();
216-
if rustup_in_path || has_rustup_env {
217-
failure::format_err!("{}: please run `rustup component add clippy`", e)
218-
} else {
219-
failure::format_err!("{}: please install clippy", e)
220-
}
221-
})?;
222-
Some(tool)
223-
} else {
224-
self.maybe_get_tool("rustc-wrapper")?
225-
};
200+
let wrapper = self.maybe_get_tool("rustc-wrapper")?;
226201
Rustc::new(
227202
self.get_tool("rustc")?,
228203
wrapper,
@@ -782,7 +757,7 @@ impl Config {
782757

783758
/// Looks for a path for `tool` in an environment variable or config path, defaulting to `tool`
784759
/// as a path.
785-
fn get_tool(&self, tool: &str) -> CargoResult<PathBuf> {
760+
pub fn get_tool(&self, tool: &str) -> CargoResult<PathBuf> {
786761
self.maybe_get_tool(tool)
787762
.map(|t| t.unwrap_or_else(|| PathBuf::from(tool)))
788763
}

src/cargo/util/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub mod process_builder;
4747
pub mod profile;
4848
mod progress;
4949
mod read2;
50-
mod rustc;
50+
pub mod rustc;
5151
mod sha256;
5252
pub mod to_semver;
5353
pub mod to_url;

src/cargo/util/process_builder.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ impl ProcessBuilder {
8080

8181
/// (chainable) Replaces the args list with the given `args`.
8282
pub fn args_replace<T: AsRef<OsStr>>(&mut self, args: &[T]) -> &mut ProcessBuilder {
83-
self.args = args
84-
.iter()
85-
.map(|t| t.as_ref().to_os_string())
86-
.collect();
83+
self.args = args.iter().map(|t| t.as_ref().to_os_string()).collect();
8784
self
8885
}
8986

0 commit comments

Comments
 (0)