Skip to content

Commit 0a752c6

Browse files
committed
refactor: extract remap rules into functions
1 parent ec05ed9 commit 0a752c6

File tree

1 file changed

+64
-61
lines changed

1 file changed

+64
-61
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 64 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,73 +1242,76 @@ fn trim_paths_args(
12421242
cmd.arg("-Zunstable-options");
12431243
cmd.arg(format!("-Zremap-path-scope={trim_paths}"));
12441244

1245-
let sysroot_remap = {
1246-
let sysroot = &build_runner.bcx.target_data.info(unit.kind).sysroot;
1247-
let mut remap = OsString::from("--remap-path-prefix=");
1248-
remap.push(sysroot);
1249-
remap.push("/lib/rustlib/src/rust"); // See also `detect_sysroot_src_path()`.
1250-
remap.push("=");
1251-
remap.push("/rustc/");
1252-
// This remap logic aligns with rustc:
1253-
// <https://github.com/rust-lang/rust/blob/c2ef3516/src/bootstrap/src/lib.rs#L1113-L1116>
1254-
if let Some(commit_hash) = build_runner.bcx.rustc().commit_hash.as_ref() {
1255-
remap.push(commit_hash);
1256-
} else {
1257-
remap.push(build_runner.bcx.rustc().version.to_string());
1258-
}
1259-
remap
1260-
};
1261-
let package_remap = {
1262-
let pkg_root = unit.pkg.root();
1263-
let ws_root = build_runner.bcx.ws.root();
1264-
let mut remap = OsString::from("--remap-path-prefix=");
1265-
// Remap rules for dependencies
1266-
//
1267-
// * Git dependencies: remove ~/.cargo/git/checkouts prefix.
1268-
// * Registry dependencies: remove ~/.cargo/registry/src prefix.
1269-
// * Others (e.g. path dependencies):
1270-
// * relative paths to workspace root if inside the workspace directory.
1271-
// * otherwise remapped to `<pkg>-<version>`.
1272-
let source_id = unit.pkg.package_id().source_id();
1273-
if source_id.is_git() {
1274-
remap.push(
1275-
build_runner
1276-
.bcx
1277-
.gctx
1278-
.git_checkouts_path()
1279-
.as_path_unlocked(),
1280-
);
1281-
remap.push("=");
1282-
} else if source_id.is_registry() {
1283-
remap.push(
1284-
build_runner
1285-
.bcx
1286-
.gctx
1287-
.registry_source_path()
1288-
.as_path_unlocked(),
1289-
);
1290-
remap.push("=");
1291-
} else if pkg_root.strip_prefix(ws_root).is_ok() {
1292-
remap.push(ws_root);
1293-
remap.push("=."); // remap to relative rustc work dir explicitly
1294-
} else {
1295-
remap.push(pkg_root);
1296-
remap.push("=");
1297-
remap.push(unit.pkg.name());
1298-
remap.push("-");
1299-
remap.push(unit.pkg.version().to_string());
1300-
}
1301-
remap
1302-
};
1303-
13041245
// Order of `--remap-path-prefix` flags is important for `-Zbuild-std`.
13051246
// We want to show `/rustc/<hash>/library/std` instead of `std-0.0.0`.
1306-
cmd.arg(package_remap);
1307-
cmd.arg(sysroot_remap);
1247+
cmd.arg(package_remap(build_runner, unit));
1248+
cmd.arg(sysroot_remap(build_runner, unit));
13081249

13091250
Ok(())
13101251
}
13111252

1253+
/// Path prefix remap rules for sysroot.
1254+
///
1255+
/// This remap logic aligns with rustc:
1256+
/// <https://github.com/rust-lang/rust/blob/c2ef3516/src/bootstrap/src/lib.rs#L1113-L1116>
1257+
fn sysroot_remap(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> OsString {
1258+
let sysroot = &build_runner.bcx.target_data.info(unit.kind).sysroot;
1259+
let mut remap = OsString::from("--remap-path-prefix=");
1260+
remap.push(sysroot);
1261+
remap.push("/lib/rustlib/src/rust"); // See also `detect_sysroot_src_path()`.
1262+
remap.push("=");
1263+
remap.push("/rustc/");
1264+
if let Some(commit_hash) = build_runner.bcx.rustc().commit_hash.as_ref() {
1265+
remap.push(commit_hash);
1266+
} else {
1267+
remap.push(build_runner.bcx.rustc().version.to_string());
1268+
}
1269+
remap
1270+
}
1271+
1272+
/// Path prefix remap rules for dependencies.
1273+
///
1274+
/// * Git dependencies: remove `~/.cargo/git/checkouts` prefix.
1275+
/// * Registry dependencies: remove `~/.cargo/registry/src` prefix.
1276+
/// * Others (e.g. path dependencies):
1277+
/// * relative paths to workspace root if inside the workspace directory.
1278+
/// * otherwise remapped to `<pkg>-<version>`.
1279+
fn package_remap(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> OsString {
1280+
let pkg_root = unit.pkg.root();
1281+
let ws_root = build_runner.bcx.ws.root();
1282+
let mut remap = OsString::from("--remap-path-prefix=");
1283+
let source_id = unit.pkg.package_id().source_id();
1284+
if source_id.is_git() {
1285+
remap.push(
1286+
build_runner
1287+
.bcx
1288+
.gctx
1289+
.git_checkouts_path()
1290+
.as_path_unlocked(),
1291+
);
1292+
remap.push("=");
1293+
} else if source_id.is_registry() {
1294+
remap.push(
1295+
build_runner
1296+
.bcx
1297+
.gctx
1298+
.registry_source_path()
1299+
.as_path_unlocked(),
1300+
);
1301+
remap.push("=");
1302+
} else if pkg_root.strip_prefix(ws_root).is_ok() {
1303+
remap.push(ws_root);
1304+
remap.push("=."); // remap to relative rustc work dir explicitly
1305+
} else {
1306+
remap.push(pkg_root);
1307+
remap.push("=");
1308+
remap.push(unit.pkg.name());
1309+
remap.push("-");
1310+
remap.push(unit.pkg.version().to_string());
1311+
}
1312+
remap
1313+
}
1314+
13121315
/// Generates the `--check-cfg` arguments for the `unit`.
13131316
fn check_cfg_args(unit: &Unit) -> CargoResult<Vec<OsString>> {
13141317
// The routine below generates the --check-cfg arguments. Our goals here are to

0 commit comments

Comments
 (0)