Skip to content

Commit 8926897

Browse files
committed
feat(trim-paths): rustc invocation integration
1 parent 92179d7 commit 8926897

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ use crate::util::errors::{CargoResult, VerboseError};
9494
use crate::util::interning::InternedString;
9595
use crate::util::machine_message::{self, Message};
9696
use crate::util::toml::TomlDebugInfo;
97+
use crate::util::toml::TomlTrimPaths;
9798
use crate::util::{add_path_args, internal, iter_join_onto, profile};
9899
use cargo_util::{paths, ProcessBuilder, ProcessError};
99100
use rustfix::diagnostics::Applicability;
@@ -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 let Some(trim_paths) = trim_paths {
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,57 @@ 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+
trim_paths: &TomlTrimPaths,
1185+
) -> CargoResult<()> {
1186+
if trim_paths.is_none() {
1187+
return Ok(());
1188+
}
1189+
1190+
// feature gate was checked during mainfest/config parsing.
1191+
cmd.arg("-Zunstable-options");
1192+
cmd.arg(format!("-Zremap-path-scope={trim_paths}"));
1193+
1194+
let sysroot_remap = {
1195+
let sysroot = &cx.bcx.target_data.info(unit.kind).sysroot_target_libdir;
1196+
let commit_hash = &cx.bcx.rustc().commit_hash;
1197+
let mut remap = OsString::from("--remap-path-prefix=");
1198+
remap.push(sysroot);
1199+
remap.push("=");
1200+
remap.push("/rustc/");
1201+
remap.push(commit_hash);
1202+
remap
1203+
};
1204+
cmd.arg(sysroot_remap);
1205+
1206+
let package_remap = {
1207+
let pkg_root = unit.pkg.root();
1208+
let ws_root = cx.bcx.ws.root();
1209+
let mut remap = OsString::from("--remap-path-prefix=");
1210+
remap.push(pkg_root);
1211+
remap.push("=");
1212+
if pkg_root.starts_with(ws_root) {
1213+
// empty to remap to relative paths.
1214+
} else {
1215+
remap.push(unit.pkg.name());
1216+
remap.push("-");
1217+
remap.push(unit.pkg.version().to_string());
1218+
}
1219+
remap
1220+
};
1221+
cmd.arg(package_remap);
1222+
1223+
Ok(())
1224+
}
1225+
11691226
/// Generates the `--check-cfg` arguments for the `unit`.
11701227
/// See unstable feature [`check-cfg`].
11711228
///

0 commit comments

Comments
 (0)