Remap both absolute and relative paths when building rustc and std

This commit is contained in:
Urgau 2025-12-22 17:41:13 +01:00
parent 1b8ee46683
commit 4d839da22b
3 changed files with 81 additions and 5 deletions

View file

@ -168,8 +168,11 @@ fn main() {
}
}
if let Ok(map) = env::var("RUSTC_DEBUGINFO_MAP") {
cmd.arg("--remap-path-prefix").arg(&map);
// The remap flags for the compiler and standard library sources.
if let Ok(maps) = env::var("RUSTC_DEBUGINFO_MAP") {
for map in maps.split('\t') {
cmd.arg("--remap-path-prefix").arg(map);
}
}
// The remap flags for Cargo registry sources need to be passed after the remapping for the
// Rust source code directory, to handle cases when $CARGO_HOME is inside the source directory.

View file

@ -1025,15 +1025,26 @@ impl Builder<'_> {
if let Some(ref map_to) =
self.build.debuginfo_map_to(GitRepo::Rustc, RemapScheme::NonCompiler)
{
// Tell the compiler which prefix was used for remapping the standard library
cargo.env("CFG_VIRTUAL_RUST_SOURCE_BASE_DIR", map_to);
}
if let Some(ref map_to) =
self.build.debuginfo_map_to(GitRepo::Rustc, RemapScheme::Compiler)
{
// When building compiler sources, we want to apply the compiler remap scheme.
cargo.env("RUSTC_DEBUGINFO_MAP", format!("compiler/={map_to}/compiler"));
// Tell the compiler which prefix was used for remapping the compiler it-self
cargo.env("CFG_VIRTUAL_RUSTC_DEV_SOURCE_BASE_DIR", map_to);
// When building compiler sources, we want to apply the compiler remap scheme.
let map = [
// Cargo use relative paths for workspace members, so let's remap those.
format!("compiler/={map_to}/compiler"),
// rustc creates absolute paths (in part bc of the `rust-src` unremap
// and for working directory) so let's remap the build directory as well.
format!("{}={map_to}", self.build.src.display()),
]
.join("\t");
cargo.env("RUSTC_DEBUGINFO_MAP", map);
}
}
Mode::Std
@ -1044,7 +1055,16 @@ impl Builder<'_> {
if let Some(ref map_to) =
self.build.debuginfo_map_to(GitRepo::Rustc, RemapScheme::NonCompiler)
{
cargo.env("RUSTC_DEBUGINFO_MAP", format!("library/={map_to}/library"));
// When building the standard library sources, we want to apply the std remap scheme.
let map = [
// Cargo use relative paths for workspace members, so let's remap those.
format!("library/={map_to}/library"),
// rustc creates absolute paths (in part bc of the `rust-src` unremap
// and for working directory) so let's remap the build directory as well.
format!("{}={map_to}", self.build.src.display()),
]
.join("\t");
cargo.env("RUSTC_DEBUGINFO_MAP", map);
}
}
}

View file

@ -0,0 +1,53 @@
// This test makes sure that we do not leak paths to the checkout
// (ie. /checkout in CI) in the distributed `libstd` debuginfo.
//
// This test only runs on Linux and dist builder (or with `rust.remap-debuginfo = true`
// set in your `bootstrap.toml`).
//@ needs-std-remap-debuginfo
//@ only-linux
use std::path::PathBuf;
use run_make_support::{llvm_dwarfdump, rfs, rustc, shallow_find_files, source_root};
fn main() {
// Find the target libdir for the current target
let target_libdir = {
let output = rustc().print("target-libdir").run();
let stdout = output.stdout_utf8();
let path = PathBuf::from(stdout.trim());
// Assert that the target-libdir path exists
assert!(path.exists(), "target-libdir: {path:?} does not exists");
path
};
// Find all the `libstd-.*.rlib` files under the libdir
let libstd_rlibs = shallow_find_files(&target_libdir, |p| {
if let Some(filename) = p.file_name()
&& let filename = filename.to_string_lossy()
{
filename.starts_with("libstd-") && filename.ends_with(".rlib")
} else {
false
}
});
// Assert that there is only one rlib for the `libstd`
let [libstd_rlib] = &libstd_rlibs[..] else {
unreachable!("multiple libstd rlib: {libstd_rlibs:?} in {target_libdir:?}");
};
// Symlink the libstd rlib here to avoid absolute paths from llvm-dwarfdump own output
// and not from the debuginfo it-self
rfs::symlink_file(libstd_rlib, "libstd.rlib");
// Check that there is only `/rustc/` paths and no `/checkout`, `/home`, or whatever
llvm_dwarfdump()
.input("libstd.rlib")
.run()
.assert_stdout_contains("/rustc/")
.assert_stdout_not_contains(source_root().to_string_lossy());
}