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 6, 2023
1 parent 20d99d9 commit b396cb1
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 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,62 @@ 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().all(|s| matches!(s, ProfileTrimPaths::None)) {
return Ok(());
}

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

let scopes = scopes.iter().fold(String::new(), |mut ret, s| {
ret.push_str(s.as_str());
ret
});
cmd.arg(format!("-Zremap-path-scope={scopes}"));

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 mut remap = OsString::from("--remap-path-prefix=");
let pkg_root = unit.pkg.root();
let ws_root = cx.bcx.ws.root();
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 b396cb1

Please sign in to comment.