Skip to content

Commit 46b48f6

Browse files
committed
implement primary unit rustc
1 parent abd9149 commit 46b48f6

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

src/cargo/core/compiler/build_config.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::cell::RefCell;
2-
use std::path::Path;
2+
use std::path::{Path, PathBuf};
33

44
use serde::ser;
55

@@ -26,6 +26,8 @@ pub struct BuildConfig {
2626
pub build_plan: bool,
2727
/// An optional wrapper, if any, used to wrap rustc invocations
2828
pub rustc_wrapper: Option<ProcessBuilder>,
29+
/// An optional override of the rustc path for primary units only
30+
pub primary_unit_rustc: Option<PathBuf>,
2931
pub rustfix_diagnostic_server: RefCell<Option<RustfixDiagnosticServer>>,
3032
/// Whether or not Cargo should cache compiler output on disk.
3133
cache_messages: bool,
@@ -99,6 +101,7 @@ impl BuildConfig {
99101
force_rebuild: false,
100102
build_plan: false,
101103
rustc_wrapper: None,
104+
primary_unit_rustc: None,
102105
rustfix_diagnostic_server: RefCell::new(None),
103106
cache_messages: config.cli_unstable().cache_messages,
104107
})

src/cargo/core/compiler/compilation.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ pub struct Compilation<'cfg> {
6969

7070
config: &'cfg Config,
7171
rustc_process: ProcessBuilder,
72+
primary_unit_rustc_process: Option<ProcessBuilder>,
7273

7374
target_runner: Option<(PathBuf, Vec<String>)>,
7475
}
@@ -77,8 +78,17 @@ impl<'cfg> Compilation<'cfg> {
7778
pub fn new<'a>(bcx: &BuildContext<'a, 'cfg>) -> CargoResult<Compilation<'cfg>> {
7879
let mut rustc = bcx.rustc.process();
7980

81+
let mut primary_unit_rustc_process = bcx
82+
.build_config
83+
.primary_unit_rustc
84+
.as_ref()
85+
.map(|primary_unit_rustc| bcx.rustc.process_with(primary_unit_rustc));
86+
8087
if bcx.config.extra_verbose() {
8188
rustc.display_env_vars();
89+
primary_unit_rustc_process.as_mut().map(|rustc| {
90+
rustc.display_env_vars();
91+
});
8292
}
8393

8494
Ok(Compilation {
@@ -97,15 +107,29 @@ impl<'cfg> Compilation<'cfg> {
97107
rustdocflags: HashMap::new(),
98108
config: bcx.config,
99109
rustc_process: rustc,
110+
primary_unit_rustc_process,
100111
host: bcx.host_triple().to_string(),
101112
target: bcx.target_triple().to_string(),
102113
target_runner: target_runner(bcx)?,
103114
})
104115
}
105116

106117
/// See `process`.
107-
pub fn rustc_process(&self, pkg: &Package, target: &Target) -> CargoResult<ProcessBuilder> {
108-
let mut p = self.fill_env(self.rustc_process.clone(), pkg, true)?;
118+
pub fn rustc_process(
119+
&self,
120+
pkg: &Package,
121+
target: &Target,
122+
is_primary: bool,
123+
) -> CargoResult<ProcessBuilder> {
124+
let rustc = if is_primary {
125+
self.primary_unit_rustc_process
126+
.clone()
127+
.unwrap_or_else(|| self.rustc_process.clone())
128+
} else {
129+
self.rustc_process.clone()
130+
};
131+
132+
let mut p = self.fill_env(rustc, pkg, true)?;
109133
if target.edition() != Edition::Edition2015 {
110134
p.arg(format!("--edition={}", target.edition()));
111135
}

src/cargo/core/compiler/mod.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,6 @@ fn rustc<'a, 'cfg>(
180180
exec: &Arc<dyn Executor>,
181181
) -> CargoResult<Work> {
182182
let mut rustc = prepare_rustc(cx, &unit.target.rustc_crate_types(), unit)?;
183-
if cx.is_primary_package(unit) {
184-
rustc.env("CARGO_PRIMARY_PACKAGE", "1");
185-
}
186183
let build_plan = cx.bcx.build_config.build_plan;
187184

188185
let name = unit.pkg.name().to_string();
@@ -593,7 +590,15 @@ fn prepare_rustc<'a, 'cfg>(
593590
crate_types: &[&str],
594591
unit: &Unit<'a>,
595592
) -> CargoResult<ProcessBuilder> {
596-
let mut base = cx.compilation.rustc_process(unit.pkg, unit.target)?;
593+
let is_primary = cx.is_primary_package(unit);
594+
let mut base = cx
595+
.compilation
596+
.rustc_process(unit.pkg, unit.target, is_primary)?;
597+
598+
if is_primary {
599+
base.env("CARGO_PRIMARY_PACKAGE", "1");
600+
}
601+
597602
base.inherit_jobserver(&cx.jobserver);
598603
build_base_args(cx, &mut base, unit, crate_types)?;
599604
build_deps_args(&mut base, cx, unit)?;

src/cargo/util/rustc.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ impl Rustc {
6666
})
6767
}
6868

69+
/// Gets a process builder set up to use the found rustc version, with a wrapper if `Some`.
70+
pub fn process_with(&self, path: impl AsRef<Path>) -> ProcessBuilder {
71+
match self.wrapper {
72+
Some(ref wrapper) if !wrapper.get_program().is_empty() => {
73+
let mut cmd = wrapper.clone();
74+
cmd.arg(path.as_ref());
75+
cmd
76+
}
77+
_ => util::process(path.as_ref()),
78+
}
79+
}
80+
6981
/// Gets a process builder set up to use the found rustc version, with a wrapper if `Some`.
7082
pub fn process(&self) -> ProcessBuilder {
7183
match self.wrapper {

0 commit comments

Comments
 (0)