Skip to content

Commit e1856e2

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

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 16 additions & 22 deletions
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);
@@ -668,13 +662,13 @@ where
668662
search_path
669663
}
670664

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

@@ -692,8 +686,13 @@ fn prepare_rustc(
692686
}
693687

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

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

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

951945
let bcx = cx.bcx;
@@ -977,7 +971,7 @@ fn build_base_args(
977971

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

0 commit comments

Comments
 (0)