Skip to content

Commit bd16ded

Browse files
committed
Remap both absolute and relative paths when building rustc and std
1 parent 1b8ee46 commit bd16ded

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

src/bootstrap/src/bin/rustc.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,11 @@ fn main() {
168168
}
169169
}
170170

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

src/bootstrap/src/core/builder/cargo.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,15 +1025,26 @@ impl Builder<'_> {
10251025
if let Some(ref map_to) =
10261026
self.build.debuginfo_map_to(GitRepo::Rustc, RemapScheme::NonCompiler)
10271027
{
1028+
// Tell the compiler which prefix was used for remapping the standard library
10281029
cargo.env("CFG_VIRTUAL_RUST_SOURCE_BASE_DIR", map_to);
10291030
}
10301031

10311032
if let Some(ref map_to) =
10321033
self.build.debuginfo_map_to(GitRepo::Rustc, RemapScheme::Compiler)
10331034
{
1034-
// When building compiler sources, we want to apply the compiler remap scheme.
1035-
cargo.env("RUSTC_DEBUGINFO_MAP", format!("compiler/={map_to}/compiler"));
1035+
// Tell the compiler which prefix was used for remapping the compiler it-self
10361036
cargo.env("CFG_VIRTUAL_RUSTC_DEV_SOURCE_BASE_DIR", map_to);
1037+
1038+
// When building compiler sources, we want to apply the compiler remap scheme.
1039+
let map = [
1040+
// Cargo use relative paths for workspace members, so let's remap those.
1041+
format!("compiler/={map_to}/compiler"),
1042+
// rustc creates absolute paths (in part bc of the `rust-src` unremap
1043+
// and for working directory) so let's remap the build directory as well.
1044+
format!("{}={map_to}", self.build.src.display()),
1045+
]
1046+
.join("\t");
1047+
cargo.env("RUSTC_DEBUGINFO_MAP", map);
10371048
}
10381049
}
10391050
Mode::Std
@@ -1044,7 +1055,16 @@ impl Builder<'_> {
10441055
if let Some(ref map_to) =
10451056
self.build.debuginfo_map_to(GitRepo::Rustc, RemapScheme::NonCompiler)
10461057
{
1047-
cargo.env("RUSTC_DEBUGINFO_MAP", format!("library/={map_to}/library"));
1058+
// When building the standard library sources, we want to apply the std remap scheme.
1059+
let map = [
1060+
// Cargo use relative paths for workspace members, so let's remap those.
1061+
format!("library/={map_to}/library"),
1062+
// rustc creates absolute paths (in part bc of the `rust-src` unremap
1063+
// and for working directory) so let's remap the build directory as well.
1064+
format!("{}={map_to}", self.build.src.display()),
1065+
]
1066+
.join("\t");
1067+
cargo.env("RUSTC_DEBUGINFO_MAP", map);
10481068
}
10491069
}
10501070
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// This test makes sure that we do not leak paths to the checkout
2+
// (ie. /checkout in CI) in the distributed `libstd` debuginfo.
3+
//
4+
// This test only runs on Linux and dist builder (or with `rust.remap-debuginfo = true`
5+
// set in your `bootstrap.toml`).
6+
7+
//@ needs-std-remap-debuginfo
8+
//@ only-linux
9+
10+
use std::path::PathBuf;
11+
12+
use run_make_support::{llvm_dwarfdump, rfs, rustc, shallow_find_files, source_root};
13+
14+
fn main() {
15+
// Find the target libdir for the current target
16+
let target_libdir = {
17+
let output = rustc().print("target-libdir").run();
18+
let stdout = output.stdout_utf8();
19+
let path = PathBuf::from(stdout.trim());
20+
21+
// Assert that the target-libdir path exists
22+
assert!(path.exists(), "target-libdir: {path:?} does not exists");
23+
24+
path
25+
};
26+
27+
// Find all the `libstd-.*.rlib` files under the libdir
28+
let libstd_rlibs = shallow_find_files(&target_libdir, |p| {
29+
if let Some(filename) = p.file_name()
30+
&& let filename = filename.to_string_lossy()
31+
{
32+
filename.starts_with("libstd-") && filename.ends_with(".rlib")
33+
} else {
34+
false
35+
}
36+
});
37+
38+
// Assert that there is only one rlib for the `libstd`
39+
let [libstd_rlib] = &libstd_rlibs[..] else {
40+
unreachable!("multiple libstd rlib: {libstd_rlibs:?} in {target_libdir:?}");
41+
};
42+
43+
// Symlink the libstd rlib here to avoid absolute paths from llvm-dwarfdump own output
44+
// and not from the debuginfo it-self
45+
rfs::symlink_file(libstd_rlib, "libstd.rlib");
46+
47+
// Check that there is only `/rustc/` paths and no `/checkout`, `/home`, or whatever
48+
llvm_dwarfdump()
49+
.input("libstd.rlib")
50+
.run()
51+
.assert_stdout_contains("/rustc/")
52+
.assert_stdout_not_contains(source_root().to_string_lossy());
53+
}

0 commit comments

Comments
 (0)