Skip to content

Commit b396cb1

Browse files
committed
feat(trim-paths): rustc invocation integration
1 parent 20d99d9 commit b396cb1

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ use crate::core::{Feature, PackageId, Target, Verbosity};
9393
use crate::util::errors::{CargoResult, VerboseError};
9494
use crate::util::interning::InternedString;
9595
use crate::util::machine_message::{self, Message};
96+
use crate::util::toml::ProfileTrimPaths;
9697
use crate::util::toml::TomlDebugInfo;
9798
use crate::util::{add_path_args, internal, iter_join_onto, profile};
9899
use cargo_util::{paths, ProcessBuilder, ProcessError};
@@ -954,6 +955,7 @@ fn build_base_args(cx: &Context<'_, '_>, cmd: &mut ProcessBuilder, unit: &Unit)
954955
incremental,
955956
strip,
956957
rustflags: profile_rustflags,
958+
trim_paths,
957959
..
958960
} = unit.profile.clone();
959961
let test = unit.mode.is_any_test();
@@ -1032,6 +1034,10 @@ fn build_base_args(cx: &Context<'_, '_>, cmd: &mut ProcessBuilder, unit: &Unit)
10321034
}
10331035
}
10341036

1037+
if !trim_paths.is_empty() {
1038+
trim_paths_args(cmd, cx, unit, &trim_paths)?;
1039+
}
1040+
10351041
cmd.args(unit.pkg.manifest().lint_rustflags());
10361042
cmd.args(&profile_rustflags);
10371043
if let Some(args) = cx.bcx.extra_args_for(unit) {
@@ -1166,6 +1172,62 @@ fn features_args(unit: &Unit) -> Vec<OsString> {
11661172
args
11671173
}
11681174

1175+
/// Generates the `--remap-path-scope` and `--remap-path-prefix` for [RFC 3127].
1176+
/// See also unstable feature [`-Ztrim-paths`].
1177+
///
1178+
/// [RFC 3127]: https://rust-lang.github.io/rfcs/3127-trim-paths.html
1179+
/// [`-Ztrim-paths`]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#profile-trim-paths-option
1180+
fn trim_paths_args(
1181+
cmd: &mut ProcessBuilder,
1182+
cx: &Context<'_, '_>,
1183+
unit: &Unit,
1184+
scopes: &[ProfileTrimPaths],
1185+
) -> CargoResult<()> {
1186+
if scopes.iter().all(|s| matches!(s, ProfileTrimPaths::None)) {
1187+
return Ok(());
1188+
}
1189+
1190+
// feature gate was checked during mainfest/config parsing.
1191+
cmd.arg("-Zunstable-options");
1192+
1193+
let scopes = scopes.iter().fold(String::new(), |mut ret, s| {
1194+
ret.push_str(s.as_str());
1195+
ret
1196+
});
1197+
cmd.arg(format!("-Zremap-path-scope={scopes}"));
1198+
1199+
let sysroot_remap = {
1200+
let sysroot = &cx.bcx.target_data.info(unit.kind).sysroot_target_libdir;
1201+
let commit_hash = &cx.bcx.rustc().commit_hash;
1202+
let mut remap = OsString::from("--remap-path-prefix=");
1203+
remap.push(sysroot);
1204+
remap.push("=");
1205+
remap.push("/rustc/");
1206+
remap.push(commit_hash);
1207+
remap
1208+
};
1209+
cmd.arg(sysroot_remap);
1210+
1211+
let package_remap = {
1212+
let mut remap = OsString::from("--remap-path-prefix=");
1213+
let pkg_root = unit.pkg.root();
1214+
let ws_root = cx.bcx.ws.root();
1215+
remap.push(pkg_root);
1216+
remap.push("=");
1217+
if pkg_root.starts_with(ws_root) {
1218+
// empty to remap to relative paths.
1219+
} else {
1220+
remap.push(unit.pkg.name());
1221+
remap.push("-");
1222+
remap.push(unit.pkg.version().to_string());
1223+
}
1224+
remap
1225+
};
1226+
cmd.arg(package_remap);
1227+
1228+
Ok(())
1229+
}
1230+
11691231
/// Generates the `--check-cfg` arguments for the `unit`.
11701232
/// See unstable feature [`check-cfg`].
11711233
///

0 commit comments

Comments
 (0)