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 8, 2023
1 parent 9e60b16 commit 7558706
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ use crate::core::{Feature, PackageId, Target, Verbosity};
use crate::util::errors::{CargoResult, VerboseError};
use crate::util::interning::InternedString;
use crate::util::machine_message::{self, Message};
use crate::util::toml::ProfileTrimPaths;
use crate::util::toml::TomlDebugInfo;
use crate::util::{add_path_args, internal, iter_join_onto, profile};
use cargo_util::{paths, ProcessBuilder, ProcessError};
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 !trim_paths.is_empty() {
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,63 @@ 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,
scopes: &[ProfileTrimPaths],
) -> CargoResult<()> {
if scopes.iter().any(|s| matches!(s, ProfileTrimPaths::None)) {
return Ok(());
}

// feature gate was checked during mainfest/config parsing.
cmd.arg("-Zunstable-options");

let mut remap_path_scope = String::from("-Zremap-path-scope=");
for scope in scopes.iter() {
remap_path_scope.push_str(scope.as_str());
remap_path_scope.push(',');
}
cmd.arg(remap_path_scope.trim_end_matches(','));

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 7558706

Please sign in to comment.