Skip to content

Commit b682cbb

Browse files
committed
refactor: consolidate rustc process building in one place
Make it clear by separating static and dynamic rustflags.
1 parent 6d3224f commit b682cbb

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

src/cargo/core/compiler/mod.rs

+16-22
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,12 @@ fn make_failed_scrape_diagnostic(
249249

250250
/// Creates a unit of work invoking `rustc` for building the `unit`.
251251
fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> CargoResult<Work> {
252-
let mut rustc = prepare_rustc(cx, &unit.target.rustc_crate_types(), unit)?;
252+
let mut rustc = prepare_rustc(cx, unit)?;
253253
let build_plan = cx.bcx.build_config.build_plan;
254254

255255
let name = unit.pkg.name().to_string();
256256
let buildkey = unit.buildkey();
257257

258-
add_cap_lints(cx.bcx, unit, &mut rustc);
259-
260258
let outputs = cx.outputs(unit)?;
261259
let root = cx.files().out_dir(unit);
262260

@@ -282,10 +280,6 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
282280
let rustc_dep_info_loc = root.join(dep_info_name);
283281
let dep_info_loc = fingerprint::dep_info_loc(cx, unit);
284282

285-
rustc.args(cx.bcx.rustflags_args(unit));
286-
if cx.bcx.config.cli_unstable().binary_dep_depinfo {
287-
rustc.arg("-Z").arg("binary-dep-depinfo");
288-
}
289283
let mut output_options = OutputOptions::new(cx, unit);
290284
let package_id = unit.pkg.package_id();
291285
let target = Target::clone(&unit.target);
@@ -669,13 +663,13 @@ where
669663
search_path
670664
}
671665

672-
// TODO: do we really need this as a separate function?
673-
// Maybe we should reorganize `rustc` fn to make it more traceable and readable.
674-
fn prepare_rustc(
675-
cx: &Context<'_, '_>,
676-
crate_types: &[CrateType],
677-
unit: &Unit,
678-
) -> CargoResult<ProcessBuilder> {
666+
/// Prepares flags and environments we can compute for a `rustc` invocation
667+
/// before the job queue starts compiling any unit.
668+
///
669+
/// This builds a static view of the invocation. Flags depending on the
670+
/// completion of other units will be added later in runtime, such as flags
671+
/// from build scripts.
672+
fn prepare_rustc(cx: &Context<'_, '_>, unit: &Unit) -> CargoResult<ProcessBuilder> {
679673
let is_primary = cx.is_primary_package(unit);
680674
let is_workspace = cx.bcx.ws.is_member(&unit.pkg);
681675

@@ -693,8 +687,13 @@ fn prepare_rustc(
693687
}
694688

695689
base.inherit_jobserver(&cx.jobserver);
696-
build_base_args(cx, &mut base, unit, crate_types)?;
690+
build_base_args(cx, &mut base, unit)?;
697691
build_deps_args(&mut base, cx, unit)?;
692+
add_cap_lints(cx.bcx, unit, &mut base);
693+
base.args(cx.bcx.rustflags_args(unit));
694+
if cx.bcx.config.cli_unstable().binary_dep_depinfo {
695+
base.arg("-Z").arg("binary-dep-depinfo");
696+
}
698697
Ok(base)
699698
}
700699

@@ -941,12 +940,7 @@ fn add_error_format_and_color(cx: &Context<'_, '_>, cmd: &mut ProcessBuilder) {
941940
}
942941

943942
/// Adds essential rustc flags and environment variables to the command to execute.
944-
fn build_base_args(
945-
cx: &Context<'_, '_>,
946-
cmd: &mut ProcessBuilder,
947-
unit: &Unit,
948-
crate_types: &[CrateType],
949-
) -> CargoResult<()> {
943+
fn build_base_args(cx: &Context<'_, '_>, cmd: &mut ProcessBuilder, unit: &Unit) -> CargoResult<()> {
950944
assert!(!unit.mode.is_run_custom_build());
951945

952946
let bcx = cx.bcx;
@@ -978,7 +972,7 @@ fn build_base_args(
978972

979973
let mut contains_dy_lib = false;
980974
if !test {
981-
for crate_type in crate_types {
975+
for crate_type in &unit.target.rustc_crate_types() {
982976
cmd.arg("--crate-type").arg(crate_type.as_str());
983977
contains_dy_lib |= crate_type == &CrateType::Dylib;
984978
}

0 commit comments

Comments
 (0)