@@ -94,6 +94,7 @@ use crate::util::errors::{CargoResult, VerboseError};
94
94
use crate :: util:: interning:: InternedString ;
95
95
use crate :: util:: machine_message:: { self , Message } ;
96
96
use crate :: util:: toml:: TomlDebugInfo ;
97
+ use crate :: util:: toml:: TomlTrimPaths ;
97
98
use crate :: util:: { add_path_args, internal, iter_join_onto, profile} ;
98
99
use cargo_util:: { paths, ProcessBuilder , ProcessError } ;
99
100
use rustfix:: diagnostics:: Applicability ;
@@ -954,6 +955,7 @@ fn build_base_args(cx: &Context<'_, '_>, cmd: &mut ProcessBuilder, unit: &Unit)
954
955
incremental,
955
956
strip,
956
957
rustflags : profile_rustflags,
958
+ trim_paths,
957
959
..
958
960
} = unit. profile . clone ( ) ;
959
961
let test = unit. mode . is_any_test ( ) ;
@@ -1032,6 +1034,10 @@ fn build_base_args(cx: &Context<'_, '_>, cmd: &mut ProcessBuilder, unit: &Unit)
1032
1034
}
1033
1035
}
1034
1036
1037
+ if let Some ( trim_paths) = trim_paths {
1038
+ trim_paths_args ( cmd, cx, unit, & trim_paths) ?;
1039
+ }
1040
+
1035
1041
cmd. args ( unit. pkg . manifest ( ) . lint_rustflags ( ) ) ;
1036
1042
cmd. args ( & profile_rustflags) ;
1037
1043
if let Some ( args) = cx. bcx . extra_args_for ( unit) {
@@ -1166,6 +1172,57 @@ fn features_args(unit: &Unit) -> Vec<OsString> {
1166
1172
args
1167
1173
}
1168
1174
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
+
1169
1226
/// Generates the `--check-cfg` arguments for the `unit`.
1170
1227
/// See unstable feature [`check-cfg`].
1171
1228
///
0 commit comments