@@ -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 ;
@@ -950,6 +951,7 @@ fn build_base_args(cx: &Context<'_, '_>, cmd: &mut ProcessBuilder, unit: &Unit)
950
951
incremental,
951
952
strip,
952
953
rustflags : profile_rustflags,
954
+ trim_paths,
953
955
..
954
956
} = unit. profile . clone ( ) ;
955
957
let test = unit. mode . is_any_test ( ) ;
@@ -1028,6 +1030,10 @@ fn build_base_args(cx: &Context<'_, '_>, cmd: &mut ProcessBuilder, unit: &Unit)
1028
1030
}
1029
1031
}
1030
1032
1033
+ if let Some ( trim_paths) = trim_paths {
1034
+ trim_paths_args ( cmd, cx, unit, & trim_paths) ?;
1035
+ }
1036
+
1031
1037
cmd. args ( unit. pkg . manifest ( ) . lint_rustflags ( ) ) ;
1032
1038
cmd. args ( & profile_rustflags) ;
1033
1039
if let Some ( args) = cx. bcx . extra_args_for ( unit) {
@@ -1162,6 +1168,74 @@ fn features_args(unit: &Unit) -> Vec<OsString> {
1162
1168
args
1163
1169
}
1164
1170
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
+
1165
1239
/// Generates the `--check-cfg` arguments for the `unit`.
1166
1240
/// See unstable feature [`check-cfg`].
1167
1241
///
0 commit comments