@@ -94,6 +94,7 @@ use crate::util::errors::{CargoResult, VerboseError};
9494use crate :: util:: interning:: InternedString ;
9595use crate :: util:: machine_message:: { self , Message } ;
9696use crate :: util:: toml:: TomlDebugInfo ;
97+ use crate :: util:: toml:: TomlTrimPaths ;
9798use crate :: util:: { add_path_args, internal, iter_join_onto, profile} ;
9899use cargo_util:: { paths, ProcessBuilder , ProcessError } ;
99100use rustfix:: diagnostics:: Applicability ;
@@ -950,6 +951,7 @@ fn build_base_args(cx: &Context<'_, '_>, cmd: &mut ProcessBuilder, unit: &Unit)
950951 incremental,
951952 strip,
952953 rustflags : profile_rustflags,
954+ trim_paths,
953955 ..
954956 } = unit. profile . clone ( ) ;
955957 let test = unit. mode . is_any_test ( ) ;
@@ -1028,6 +1030,10 @@ fn build_base_args(cx: &Context<'_, '_>, cmd: &mut ProcessBuilder, unit: &Unit)
10281030 }
10291031 }
10301032
1033+ if let Some ( trim_paths) = trim_paths {
1034+ trim_paths_args ( cmd, cx, unit, & trim_paths) ?;
1035+ }
1036+
10311037 cmd. args ( unit. pkg . manifest ( ) . lint_rustflags ( ) ) ;
10321038 cmd. args ( & profile_rustflags) ;
10331039 if let Some ( args) = cx. bcx . extra_args_for ( unit) {
@@ -1162,6 +1168,74 @@ fn features_args(unit: &Unit) -> Vec<OsString> {
11621168 args
11631169}
11641170
1171+ /// Generates the `--remap-path-scope` and `--remap-path-prefix` for [RFC 3127].
1172+ /// See also unstable feature [`-Ztrim-paths`].
1173+ ///
1174+ /// [RFC 3127]: https://rust-lang.github.io/rfcs/3127-trim-paths.html
1175+ /// [`-Ztrim-paths`]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#profile-trim-paths-option
1176+ fn trim_paths_args (
1177+ cmd : & mut ProcessBuilder ,
1178+ cx : & Context < ' _ , ' _ > ,
1179+ unit : & Unit ,
1180+ trim_paths : & TomlTrimPaths ,
1181+ ) -> CargoResult < ( ) > {
1182+ if trim_paths. is_none ( ) {
1183+ return Ok ( ( ) ) ;
1184+ }
1185+
1186+ // feature gate was checked during mainfest/config parsing.
1187+ cmd. arg ( "-Zunstable-options" ) ;
1188+ cmd. arg ( format ! ( "-Zremap-path-scope={trim_paths}" ) ) ;
1189+
1190+ let sysroot_remap = {
1191+ let sysroot = & cx. bcx . target_data . info ( unit. kind ) . sysroot ;
1192+ let mut remap = OsString :: from ( "--remap-path-prefix=" ) ;
1193+ remap. push ( sysroot) ;
1194+ remap. push ( "/lib/rustlib/src/rust" ) ; // See also `detect_sysroot_src_path()`.
1195+ remap. push ( "=" ) ;
1196+ remap. push ( "/rustc/" ) ;
1197+ if let Some ( commit_hash) = cx. bcx . rustc ( ) . commit_hash . as_ref ( ) {
1198+ remap. push ( commit_hash) ;
1199+ } else {
1200+ // This fallback aligns with rustc:
1201+ // <https://github.com/rust-lang/rust/blob/c2ef3516/src/bootstrap/src/lib.rs#L1113-L1116>
1202+ remap. push ( cx. bcx . rustc ( ) . version . to_string ( ) ) ;
1203+ }
1204+ remap
1205+ } ;
1206+ cmd. arg ( sysroot_remap) ;
1207+
1208+ let package_remap = {
1209+ let pkg_root = unit. pkg . root ( ) ;
1210+ let ws_root = cx. bcx . ws . root ( ) ;
1211+ let is_local = unit. pkg . package_id ( ) . source_id ( ) . is_path ( ) ;
1212+ let mut remap = OsString :: from ( "--remap-path-prefix=" ) ;
1213+ // Remapped to path relative to workspace root:
1214+ //
1215+ // * path dependencies under workspace root directory
1216+ //
1217+ // Remapped to `<pkg>-<version>`
1218+ //
1219+ // * registry dependencies
1220+ // * git dependencies
1221+ // * path dependencies outside workspace root directory
1222+ if is_local && pkg_root. strip_prefix ( ws_root) . is_ok ( ) {
1223+ remap. push ( ws_root) ;
1224+ remap. push ( "=" ) ; // empty to remap to relative paths.
1225+ } else {
1226+ remap. push ( pkg_root) ;
1227+ remap. push ( "=" ) ;
1228+ remap. push ( unit. pkg . name ( ) ) ;
1229+ remap. push ( "-" ) ;
1230+ remap. push ( unit. pkg . version ( ) . to_string ( ) ) ;
1231+ }
1232+ remap
1233+ } ;
1234+ cmd. arg ( package_remap) ;
1235+
1236+ Ok ( ( ) )
1237+ }
1238+
11651239/// Generates the `--check-cfg` arguments for the `unit`.
11661240/// See unstable feature [`check-cfg`].
11671241///
0 commit comments