@@ -93,6 +93,7 @@ use crate::core::{Feature, PackageId, Target, Verbosity};
9393use crate :: util:: errors:: { CargoResult , VerboseError } ;
9494use crate :: util:: interning:: InternedString ;
9595use crate :: util:: machine_message:: { self , Message } ;
96+ use crate :: util:: toml:: ProfileTrimPaths ;
9697use crate :: util:: toml:: TomlDebugInfo ;
9798use crate :: util:: { add_path_args, internal, iter_join_onto, profile} ;
9899use 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,63 @@ 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 ( ) . any ( |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 mut remap_path_scope = String :: from ( "-Zremap-path-scope=" ) ;
1194+ for scope in scopes. iter ( ) {
1195+ remap_path_scope. push_str ( scope. as_str ( ) ) ;
1196+ remap_path_scope. push ( ',' ) ;
1197+ }
1198+ cmd. arg ( remap_path_scope. trim_end_matches ( ',' ) ) ;
1199+
1200+ let sysroot_remap = {
1201+ let sysroot = & cx. bcx . target_data . info ( unit. kind ) . sysroot_target_libdir ;
1202+ let commit_hash = & cx. bcx . rustc ( ) . commit_hash ;
1203+ let mut remap = OsString :: from ( "--remap-path-prefix=" ) ;
1204+ remap. push ( sysroot) ;
1205+ remap. push ( "=" ) ;
1206+ remap. push ( "/rustc/" ) ;
1207+ remap. push ( commit_hash) ;
1208+ remap
1209+ } ;
1210+ cmd. arg ( sysroot_remap) ;
1211+
1212+ let package_remap = {
1213+ let pkg_root = unit. pkg . root ( ) ;
1214+ let ws_root = cx. bcx . ws . root ( ) ;
1215+ let mut remap = OsString :: from ( "--remap-path-prefix=" ) ;
1216+ remap. push ( pkg_root) ;
1217+ remap. push ( "=" ) ;
1218+ if pkg_root. starts_with ( ws_root) {
1219+ // empty to remap to relative paths.
1220+ } else {
1221+ remap. push ( unit. pkg . name ( ) ) ;
1222+ remap. push ( "-" ) ;
1223+ remap. push ( unit. pkg . version ( ) . to_string ( ) ) ;
1224+ }
1225+ remap
1226+ } ;
1227+ cmd. arg ( package_remap) ;
1228+
1229+ Ok ( ( ) )
1230+ }
1231+
11691232/// Generates the `--check-cfg` arguments for the `unit`.
11701233/// See unstable feature [`check-cfg`].
11711234///
0 commit comments