Skip to content

Commit 8976a6e

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 8976a6e

File tree

2 files changed

+70
-10
lines changed

2 files changed

+70
-10
lines changed

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

Lines changed: 40 additions & 7 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,13 +920,46 @@ 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) {
924-
let map = format!("{}={}", self.build.src.display(), map_to);
925-
cargo.env("RUSTC_DEBUGINFO_MAP", map);
923+
// `rustc` needs to know the remapping scheme, in order to know how to reverse it (unremap)
924+
// later. Two env vars are set and made available to the compiler
925+
//
926+
// - `CFG_VIRTUAL_RUST_SOURCE_BASE_DIR`: `rust-src` remap scheme (`NonCompiler`)
927+
// - `CFG_VIRTUAL_RUSTC_DEV_SOURCE_BASE_DIR`: `rustc-dev` remap scheme (`Compiler`)
928+
//
929+
// Keep this scheme in sync with `rustc_metadata::rmeta::decoder`'s
930+
// `try_to_translate_virtual_to_real`.
931+
//
932+
// `RUSTC_DEBUGINFO_MAP` is used to pass through to the underlying rustc
933+
// `--remap-path-prefix`.
934+
match mode {
935+
Mode::Rustc | Mode::Codegen => {
936+
if let Some(ref map_to) =
937+
self.build.debuginfo_map_to(GitRepo::Rustc, RemapScheme::NonCompiler)
938+
{
939+
cargo.env("CFG_VIRTUAL_RUST_SOURCE_BASE_DIR", map_to);
940+
}
926941

927-
// `rustc` needs to know the virtual `/rustc/$hash` we're mapping to,
928-
// in order to opportunistically reverse it later.
929-
cargo.env("CFG_VIRTUAL_RUST_SOURCE_BASE_DIR", map_to);
942+
if let Some(ref map_to) =
943+
self.build.debuginfo_map_to(GitRepo::Rustc, RemapScheme::Compiler)
944+
{
945+
// When building compiler sources, we want to apply the compiler remap scheme.
946+
cargo.env(
947+
"RUSTC_DEBUGINFO_MAP",
948+
format!("{}={}", self.build.src.display(), map_to),
949+
);
950+
cargo.env("CFG_VIRTUAL_RUSTC_DEV_SOURCE_BASE_DIR", map_to);
951+
}
952+
}
953+
Mode::Std | Mode::ToolBootstrap | Mode::ToolRustc | Mode::ToolStd => {
954+
if let Some(ref map_to) =
955+
self.build.debuginfo_map_to(GitRepo::Rustc, RemapScheme::NonCompiler)
956+
{
957+
cargo.env(
958+
"RUSTC_DEBUGINFO_MAP",
959+
format!("{}={}", self.build.src.display(), map_to),
960+
);
961+
}
962+
}
930963
}
931964

932965
if self.config.rust_remap_debuginfo {

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)