Skip to content

Commit c6f6e51

Browse files
committed
bootstrap: remap compiler vs non-compiler sources differently
- Map compiler sources (corresponding to `rustc-dev` dist component) with `/rustc-dev/{hash}`. - Map non-compiler sources (corresponding to `rust-src` dist component or other non-compiler sources) with `/rustc/{hash}`. This allows the compiler to have the possibility of opportunistically reverse the mapping. This is because - `rust-src` unpacks sources to a path like `$sysroot/lib/rustlib/src/rust`, whereas - `rustc-dev` unpacks sources to a path like `$sysroot/lib/rustlib/rustc-src/rust` (Notice the `src` vs `rustc-src` difference.)
1 parent e0d014a commit c6f6e51

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::utils::build_stamp;
1111
use crate::utils::helpers::{self, LldThreads, check_cfg_arg, linker_args, linker_flags};
1212
use crate::{
1313
BootstrapCommand, CLang, Compiler, Config, DocTests, DryRun, EXTRA_CHECK_CFGS, GitRepo, Mode,
14-
TargetSelection, command, prepare_behaviour_dump_dir, t,
14+
RemapScheme, TargetSelection, command, prepare_behaviour_dump_dir, t,
1515
};
1616

1717
/// Represents flag values in `String` form with whitespace delimiter to pass it to the compiler
@@ -920,15 +920,33 @@ impl Builder<'_> {
920920
hostflags.arg(format!("-Ctarget-feature={sign}crt-static"));
921921
}
922922

923-
if let Some(map_to) = self.build.debuginfo_map_to(GitRepo::Rustc) {
923+
// `RUSTC_DEBUGINFO_MAP` is used to pass through to the underlying rustc
924+
// `--remap-path-prefix`. This needs to be unconditionally present and use the
925+
// `/rustc/{hash}` `rust-src` (`NonCompiler`) scheme.
926+
if let Some(map_to) = self.build.debuginfo_map_to(GitRepo::Rustc, RemapScheme::NonCompiler)
927+
{
924928
let map = format!("{}={}", self.build.src.display(), map_to);
925929
cargo.env("RUSTC_DEBUGINFO_MAP", map);
930+
}
931+
932+
// `rustc` needs to know the remapping scheme, in order to know how to reverse it (unremap)
933+
// later. Two env vars are set and made available to the compiler
934+
//
935+
// - `CFG_VIRTUAL_RUST_SOURCE_BASE_DIR`: `rust-src` remap scheme
936+
// - `CFG_VIRTUAL_RUSTC_DEV_SOURCE_BASE_DIR`: `rustc-dev` remap scheme
937+
//
938+
// Keep this scheme in sync with `rustc_metadata::rmeta::decoder`'s
939+
// `try_to_translate_virtual_to_real`.
926940

927-
// `rustc` needs to know the virtual `/rustc/$hash` we're mapping to,
928-
// in order to opportunistically reverse it later.
941+
if let Some(map_to) = self.build.debuginfo_map_to(GitRepo::Rustc, RemapScheme::NonCompiler)
942+
{
929943
cargo.env("CFG_VIRTUAL_RUST_SOURCE_BASE_DIR", map_to);
930944
}
931945

946+
if let Some(map_to) = self.build.debuginfo_map_to(GitRepo::Rustc, RemapScheme::Compiler) {
947+
cargo.env("CFG_VIRTUAL_RUSTC_DEV_SOURCE_BASE_DIR", map_to);
948+
}
949+
932950
if self.config.rust_remap_debuginfo {
933951
let mut env_var = OsString::new();
934952
if let Some(vendor) = self.build.vendored_crates_path() {

src/bootstrap/src/lib.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,16 @@ impl Mode {
272272
}
273273
}
274274

275+
/// When `rust.rust_remap_debuginfo` is requested, the compiler needs to know how to
276+
/// opportunistically unremap compiler vs non-compiler sources. We use two schemes,
277+
/// [`RemapScheme::Compiler`] and [`RemapScheme::NonCompiler`].
278+
pub enum RemapScheme {
279+
/// The [`RemapScheme::Compiler`] scheme will remap to `/rustc-dev/{hash}`.
280+
Compiler,
281+
/// The [`RemapScheme::NonCompiler`] scheme will remap to `/rustc/{hash}`.
282+
NonCompiler,
283+
}
284+
275285
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
276286
pub enum CLang {
277287
C,
@@ -1210,15 +1220,32 @@ Executed at: {executed_at}"#,
12101220
})
12111221
}
12121222

1213-
fn debuginfo_map_to(&self, which: GitRepo) -> Option<String> {
1223+
fn debuginfo_map_to(&self, which: GitRepo, remap_scheme: RemapScheme) -> Option<String> {
12141224
if !self.config.rust_remap_debuginfo {
12151225
return None;
12161226
}
12171227

12181228
match which {
12191229
GitRepo::Rustc => {
12201230
let sha = self.rust_sha().unwrap_or(&self.version);
1221-
Some(format!("/rustc/{sha}"))
1231+
1232+
match remap_scheme {
1233+
RemapScheme::Compiler => {
1234+
// For compiler sources, remap via `/rustc-dev/{sha}` to allow
1235+
// distinguishing between compiler sources vs library sources, since
1236+
// `rustc-dev` dist component places them under
1237+
// `$sysroot/lib/rustlib/rustc-src/rust` as opposed to `rust-src`'s
1238+
// `$sysroot/lib/rustlib/src/rust`.
1239+
//
1240+
// Keep this scheme in sync with `rustc_metadata::rmeta::decoder`'s
1241+
// `try_to_translate_virtual_to_real`.
1242+
Some(format!("/rustc-dev/{sha}"))
1243+
}
1244+
RemapScheme::NonCompiler => {
1245+
// For non-compiler sources, use `/rustc/{sha}` remapping scheme.
1246+
Some(format!("/rustc/{sha}"))
1247+
}
1248+
}
12221249
}
12231250
GitRepo::Llvm => Some(String::from("/rustc/llvm")),
12241251
}
@@ -1285,7 +1312,7 @@ Executed at: {executed_at}"#,
12851312
base.push("-fno-omit-frame-pointer".into());
12861313
}
12871314

1288-
if let Some(map_to) = self.debuginfo_map_to(which) {
1315+
if let Some(map_to) = self.debuginfo_map_to(which, RemapScheme::NonCompiler) {
12891316
let map = format!("{}={}", self.src.display(), map_to);
12901317
let cc = self.cc(target);
12911318
if cc.ends_with("clang") || cc.ends_with("gcc") {

0 commit comments

Comments
 (0)