Skip to content

Commit

Permalink
feat(trim-paths): rustc invocation integration
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed Sep 12, 2023
1 parent 92179d7 commit 8926897
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -1166,6 +1172,57 @@ fn features_args(unit: &Unit) -> Vec<OsString> {
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`].
///
Expand Down

0 comments on commit 8926897

Please sign in to comment.