Skip to content

Commit a831673

Browse files
authored
Unrolled build for #141751
Rollup merge of #141751 - jieyouxu:remap, r=Kobzol Remap compiler vs non-compiler sources differently (bootstrap side) See [#t-compiler/help > Span pointing to wrong file location (`rustc-dev` component)](https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/Span.20pointing.20to.20wrong.20file.20location.20.28.60rustc-dev.60.20component.29/with/521087083). The path remapping and unremapping for compiler sources (distributed via `rustc-dev` dist component) is broken because bootstrap currently remaps all sources unconditionally (if remapping is enabled) to the `/rustc/{hash}` form. However, the `rustc-dev` dist component (compiler sources) and `rust-src` dist component (library sources) unpacks differently: - `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`[^note], meaning that the compiler need to unremap them differently. But the same remapping means that the compiler has no way to distinguish between compiler and non-compiler (esp. standard library) sources. To remedy this, this PR adopts the approach of: - remapping compiler sources (corresponding to `rustc-dev` dist component) with `/rustc-dev/{hash}` (this is `RemapScheme::Compiler`), and - remapping non-compiler sources (corresponding to `rust-src` dist component or other non-compiler sources) with `/rustc/{hash}` (this is `RemapScheme::NonCompiler`). A different remapping allows the compiler to reverse the remapping differently. This PR implements the bootstrap side. A follow-up compiler-side change is needed to implement the unremapping change to address the reported issue completely. This PR introduces another env var `CFG_VIRTUAL_RUSTC_DEV_SOURCE_BASE_DIR` that is made available to the compiler when building compiler sources to know what the remap scheme for `rustc-dev` (`RemapScheme::Compiler`) is. Compiler sources are built with the compiler remapping scheme. As far as I know, this change should not introduce new regressions, because the compiler source unremapping (through `rustc-dev`) is already broken. [^note]: (Notice the `src` vs `rustc-src` difference.)
2 parents b6685d7 + 8976a6e commit a831673

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,
@@ -1217,15 +1227,32 @@ Executed at: {executed_at}"#,
12171227
})
12181228
}
12191229

1220-
fn debuginfo_map_to(&self, which: GitRepo) -> Option<String> {
1230+
fn debuginfo_map_to(&self, which: GitRepo, remap_scheme: RemapScheme) -> Option<String> {
12211231
if !self.config.rust_remap_debuginfo {
12221232
return None;
12231233
}
12241234

12251235
match which {
12261236
GitRepo::Rustc => {
12271237
let sha = self.rust_sha().unwrap_or(&self.version);
1228-
Some(format!("/rustc/{sha}"))
1238+
1239+
match remap_scheme {
1240+
RemapScheme::Compiler => {
1241+
// For compiler sources, remap via `/rustc-dev/{sha}` to allow
1242+
// distinguishing between compiler sources vs library sources, since
1243+
// `rustc-dev` dist component places them under
1244+
// `$sysroot/lib/rustlib/rustc-src/rust` as opposed to `rust-src`'s
1245+
// `$sysroot/lib/rustlib/src/rust`.
1246+
//
1247+
// Keep this scheme in sync with `rustc_metadata::rmeta::decoder`'s
1248+
// `try_to_translate_virtual_to_real`.
1249+
Some(format!("/rustc-dev/{sha}"))
1250+
}
1251+
RemapScheme::NonCompiler => {
1252+
// For non-compiler sources, use `/rustc/{sha}` remapping scheme.
1253+
Some(format!("/rustc/{sha}"))
1254+
}
1255+
}
12291256
}
12301257
GitRepo::Llvm => Some(String::from("/rustc/llvm")),
12311258
}
@@ -1292,7 +1319,7 @@ Executed at: {executed_at}"#,
12921319
base.push("-fno-omit-frame-pointer".into());
12931320
}
12941321

1295-
if let Some(map_to) = self.debuginfo_map_to(which) {
1322+
if let Some(map_to) = self.debuginfo_map_to(which, RemapScheme::NonCompiler) {
12961323
let map = format!("{}={}", self.src.display(), map_to);
12971324
let cc = self.cc(target);
12981325
if cc.ends_with("clang") || cc.ends_with("gcc") {

0 commit comments

Comments
 (0)