From 8926897333e827741447e05587d30aaa5c8117c0 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Wed, 6 Sep 2023 21:59:01 +0800 Subject: [PATCH] feat(trim-paths): rustc invocation integration --- src/cargo/core/compiler/mod.rs | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index ab49e65e4f61..562561916c23 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -94,6 +94,7 @@ use crate::util::errors::{CargoResult, VerboseError}; use crate::util::interning::InternedString; use crate::util::machine_message::{self, Message}; use crate::util::toml::TomlDebugInfo; +use crate::util::toml::TomlTrimPaths; use crate::util::{add_path_args, internal, iter_join_onto, profile}; use cargo_util::{paths, ProcessBuilder, ProcessError}; use rustfix::diagnostics::Applicability; @@ -954,6 +955,7 @@ fn build_base_args(cx: &Context<'_, '_>, cmd: &mut ProcessBuilder, unit: &Unit) incremental, strip, rustflags: profile_rustflags, + trim_paths, .. } = unit.profile.clone(); let test = unit.mode.is_any_test(); @@ -1032,6 +1034,10 @@ fn build_base_args(cx: &Context<'_, '_>, cmd: &mut ProcessBuilder, unit: &Unit) } } + if let Some(trim_paths) = trim_paths { + trim_paths_args(cmd, cx, unit, &trim_paths)?; + } + cmd.args(unit.pkg.manifest().lint_rustflags()); cmd.args(&profile_rustflags); if let Some(args) = cx.bcx.extra_args_for(unit) { @@ -1166,6 +1172,57 @@ fn features_args(unit: &Unit) -> Vec { args } +/// Generates the `--remap-path-scope` and `--remap-path-prefix` for [RFC 3127]. +/// See also unstable feature [`-Ztrim-paths`]. +/// +/// [RFC 3127]: https://rust-lang.github.io/rfcs/3127-trim-paths.html +/// [`-Ztrim-paths`]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#profile-trim-paths-option +fn trim_paths_args( + cmd: &mut ProcessBuilder, + cx: &Context<'_, '_>, + unit: &Unit, + trim_paths: &TomlTrimPaths, +) -> CargoResult<()> { + if trim_paths.is_none() { + return Ok(()); + } + + // feature gate was checked during mainfest/config parsing. + cmd.arg("-Zunstable-options"); + cmd.arg(format!("-Zremap-path-scope={trim_paths}")); + + let sysroot_remap = { + let sysroot = &cx.bcx.target_data.info(unit.kind).sysroot_target_libdir; + let commit_hash = &cx.bcx.rustc().commit_hash; + let mut remap = OsString::from("--remap-path-prefix="); + remap.push(sysroot); + remap.push("="); + remap.push("/rustc/"); + remap.push(commit_hash); + remap + }; + cmd.arg(sysroot_remap); + + let package_remap = { + let pkg_root = unit.pkg.root(); + let ws_root = cx.bcx.ws.root(); + let mut remap = OsString::from("--remap-path-prefix="); + remap.push(pkg_root); + remap.push("="); + if pkg_root.starts_with(ws_root) { + // empty to remap to relative paths. + } else { + remap.push(unit.pkg.name()); + remap.push("-"); + remap.push(unit.pkg.version().to_string()); + } + remap + }; + cmd.arg(package_remap); + + Ok(()) +} + /// Generates the `--check-cfg` arguments for the `unit`. /// See unstable feature [`check-cfg`]. ///