Skip to content

Commit 547c942

Browse files
committed
bootstrap: move debug-prefix-map C flags to LLVM CMake build
The `-fdebug-prefix-map` flags in `cc_unhandled_cflags` served three kinds of consumers. * The cc-rs-driven C/C++ builds inside cargo: They now inherit the same remap pairs from cargo trim-paths so passing the flag through `CFLAGS` there is redundant. * The CMake-driven LLVM build: This is the one we need the remaps. * The remaining callers (`compiler_file` probing, cc detection, test fixtures): They never produce distributed artifacts.
1 parent fd1afdd commit 547c942

6 files changed

Lines changed: 59 additions & 72 deletions

File tree

src/bootstrap/src/core/build_steps/compile.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ use crate::utils::helpers::{
3737
self, exe, get_clang_cl_resource_dir, is_debug_info, is_dylib, symlink_dir, t, up_to_date,
3838
};
3939
use crate::{
40-
CLang, CodegenBackendKind, Compiler, DependencyType, FileType, GitRepo, LLVM_TOOLS, Mode,
41-
debug, trace,
40+
CLang, CodegenBackendKind, Compiler, DependencyType, FileType, LLVM_TOOLS, Mode, debug, trace,
4241
};
4342

4443
/// Build a standard library for the given `target` using the given `build_compiler`.
@@ -1898,7 +1897,7 @@ pub fn compiler_file(
18981897
}
18991898
let mut cmd = command(compiler);
19001899
cmd.args(builder.cc_handled_cflags(target, c));
1901-
cmd.args(builder.cc_unhandled_cflags(target, GitRepo::Rustc, c));
1900+
cmd.args(builder.cc_unhandled_cflags(target, c));
19021901
cmd.arg(format!("-print-file-name={file}"));
19031902
let out = cmd.run_capture_stdout(builder).stdout();
19041903
PathBuf::from(out.trim())

src/bootstrap/src/core/build_steps/llvm.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::utils::exec::command;
2626
use crate::utils::helpers::{
2727
self, exe, get_clang_cl_resource_dir, libdir, t, unhashed_basename, up_to_date,
2828
};
29-
use crate::{CLang, GitRepo, trace};
29+
use crate::{CLang, trace};
3030

3131
/// Result of building or downloading LLVM artifacts.
3232
#[derive(Clone)]
@@ -663,6 +663,27 @@ fn check_llvm_version(builder: &Builder<'_>, llvm_config: &Path) {
663663
panic!("\n\nbad LLVM version: {version}, need >=21\n\n")
664664
}
665665

666+
/// C/C++ debug info remap flags for LLVM build.
667+
///
668+
/// The remap is observable when LLVM is compiled with debug info,
669+
/// for example, with `llvm.release-debuginfo = true`.
670+
fn debuginfo_map_cflags(builder: &Builder<'_>, target: TargetSelection) -> Vec<String> {
671+
if !builder.config.rust_remap_debuginfo {
672+
return Vec::new();
673+
}
674+
675+
let mut flags = Vec::new();
676+
let map = format!("{}=/rustc/llvm", builder.src.display());
677+
let cc = builder.cc_tool(target);
678+
if cc.is_like_clang() || cc.is_like_gnu() {
679+
flags.push(format!("-fdebug-prefix-map={map}"));
680+
} else if cc.is_like_clang_cl() {
681+
flags.push("-Xclang".into());
682+
flags.push(format!("-fdebug-prefix-map={map}"));
683+
}
684+
flags
685+
}
686+
666687
fn configure_cmake(
667688
builder: &Builder<'_>,
668689
target: TargetSelection,
@@ -827,7 +848,8 @@ fn configure_cmake(
827848
for flag in builder
828849
.cc_handled_cflags(target, CLang::C)
829850
.into_iter()
830-
.chain(builder.cc_unhandled_cflags(target, GitRepo::Llvm, CLang::C))
851+
.chain(builder.cc_unhandled_cflags(target, CLang::C))
852+
.chain(debuginfo_map_cflags(builder, target))
831853
.filter(|flag| !suppressed_compiler_flag_prefixes.iter().any(|p| flag.starts_with(p)))
832854
{
833855
cflags.push(" ");
@@ -848,7 +870,8 @@ fn configure_cmake(
848870
for flag in builder
849871
.cc_handled_cflags(target, CLang::Cxx)
850872
.into_iter()
851-
.chain(builder.cc_unhandled_cflags(target, GitRepo::Llvm, CLang::Cxx))
873+
.chain(builder.cc_unhandled_cflags(target, CLang::Cxx))
874+
.chain(debuginfo_map_cflags(builder, target))
852875
.filter(|flag| {
853876
!suppressed_compiler_flag_prefixes
854877
.iter()

src/bootstrap/src/core/build_steps/test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::utils::helpers::{
4545
up_to_date,
4646
};
4747
use crate::utils::render_tests::{add_flags_and_try_run_tests, try_run_tests};
48-
use crate::{CLang, CodegenBackendKind, GitRepo, Mode, TestTarget, envify};
48+
use crate::{CLang, CodegenBackendKind, Mode, TestTarget, envify};
4949

5050
mod compiletest;
5151
pub mod failed_tests;
@@ -2781,9 +2781,9 @@ Please disable assertions with `rust.debug-assertions = false`.
27812781
// requires that a C++ compiler was configured which isn't always the case.
27822782
if !builder.config.dry_run() && mode == CompiletestMode::RunMake {
27832783
let mut cflags = builder.cc_handled_cflags(target, CLang::C);
2784-
cflags.extend(builder.cc_unhandled_cflags(target, GitRepo::Rustc, CLang::C));
2784+
cflags.extend(builder.cc_unhandled_cflags(target, CLang::C));
27852785
let mut cxxflags = builder.cc_handled_cflags(target, CLang::Cxx);
2786-
cxxflags.extend(builder.cc_unhandled_cflags(target, GitRepo::Rustc, CLang::Cxx));
2786+
cxxflags.extend(builder.cc_unhandled_cflags(target, CLang::Cxx));
27872787
cmd.arg("--cc")
27882788
.arg(builder.cc(target))
27892789
.arg("--cxx")

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ use crate::core::config::{CompressDebuginfo, Config, DryRun, SplitDebuginfo, Tar
1111
use crate::utils::build_stamp;
1212
use crate::utils::exec::{BootstrapCommand, command};
1313
use crate::utils::helpers::{self, LldThreads, check_cfg_arg, linker_flags, t};
14-
use crate::{
15-
CLang, Compiler, EXTRA_CHECK_CFGS, GitRepo, Mode, RemapScheme, prepare_behaviour_dump_dir,
16-
};
14+
use crate::{CLang, Compiler, EXTRA_CHECK_CFGS, Mode, RemapScheme, prepare_behaviour_dump_dir};
1715

1816
/// Represents flag values in `String` form with a `\x1f` delimiter to pass to the compiler later.
1917
///
@@ -431,8 +429,7 @@ impl Cargo {
431429

432430
// Extend `CXXFLAGS_$TARGET` with our extra flags.
433431
let env = format!("CFLAGS_{triple_underscored}");
434-
let mut cflags =
435-
builder.cc_unhandled_cflags(target, GitRepo::Rustc, CLang::C).join(" ");
432+
let mut cflags = builder.cc_unhandled_cflags(target, CLang::C).join(" ");
436433
if let Ok(var) = std::env::var(&env) {
437434
cflags.push(' ');
438435
cflags.push_str(&var);
@@ -452,8 +449,7 @@ impl Cargo {
452449

453450
// Extend `CXXFLAGS_$TARGET` with our extra flags.
454451
let env = format!("CXXFLAGS_{triple_underscored}");
455-
let mut cxxflags =
456-
builder.cc_unhandled_cflags(target, GitRepo::Rustc, CLang::Cxx).join(" ");
452+
let mut cxxflags = builder.cc_unhandled_cflags(target, CLang::Cxx).join(" ");
457453
if let Ok(var) = std::env::var(&env) {
458454
cxxflags.push(' ');
459455
cxxflags.push_str(&var);
@@ -1116,16 +1112,12 @@ impl Builder<'_> {
11161112

11171113
match mode {
11181114
Mode::Rustc | Mode::Codegen => {
1119-
if let Some(ref map_to) =
1120-
self.build.debuginfo_map_to(GitRepo::Rustc, RemapScheme::NonCompiler)
1121-
{
1115+
if let Some(ref map_to) = self.build.debuginfo_map_to(RemapScheme::NonCompiler) {
11221116
// Tell the compiler which prefix was used for remapping the standard library
11231117
cargo.env("CFG_VIRTUAL_RUST_SOURCE_BASE_DIR", map_to);
11241118
}
11251119

1126-
if let Some(ref map_to) =
1127-
self.build.debuginfo_map_to(GitRepo::Rustc, RemapScheme::Compiler)
1128-
{
1120+
if let Some(ref map_to) = self.build.debuginfo_map_to(RemapScheme::Compiler) {
11291121
// Tell the compiler which prefix was used for remapping the compiler it-self
11301122
cargo.env("CFG_VIRTUAL_RUSTC_DEV_SOURCE_BASE_DIR", map_to);
11311123

@@ -1137,9 +1129,7 @@ impl Builder<'_> {
11371129
| Mode::ToolRustcPrivate
11381130
| Mode::ToolStd
11391131
| Mode::ToolTarget => {
1140-
if let Some(ref map_to) =
1141-
self.build.debuginfo_map_to(GitRepo::Rustc, RemapScheme::NonCompiler)
1142-
{
1132+
if let Some(ref map_to) = self.build.debuginfo_map_to(RemapScheme::NonCompiler) {
11431133
trim_paths(&mut cargo, map_to);
11441134
}
11451135
}

src/bootstrap/src/lib.rs

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,6 @@ impl TestTarget {
186186
}
187187
}
188188

189-
pub enum GitRepo {
190-
Rustc,
191-
Llvm,
192-
}
193-
194189
/// Global configuration for the build system.
195190
///
196191
/// This structure transitively contains all configuration for the build system.
@@ -1167,34 +1162,29 @@ impl Build {
11671162
})
11681163
}
11691164

1170-
fn debuginfo_map_to(&self, which: GitRepo, remap_scheme: RemapScheme) -> Option<String> {
1165+
fn debuginfo_map_to(&self, remap_scheme: RemapScheme) -> Option<String> {
11711166
if !self.config.rust_remap_debuginfo {
11721167
return None;
11731168
}
11741169

1175-
match which {
1176-
GitRepo::Rustc => {
1177-
let sha = self.rust_sha().unwrap_or(&self.version);
1178-
1179-
match remap_scheme {
1180-
RemapScheme::Compiler => {
1181-
// For compiler sources, remap via `/rustc-dev/{sha}` to allow
1182-
// distinguishing between compiler sources vs library sources, since
1183-
// `rustc-dev` dist component places them under
1184-
// `$sysroot/lib/rustlib/rustc-src/rust` as opposed to `rust-src`'s
1185-
// `$sysroot/lib/rustlib/src/rust`.
1186-
//
1187-
// Keep this scheme in sync with `rustc_metadata::rmeta::decoder`'s
1188-
// `try_to_translate_virtual_to_real`.
1189-
Some(format!("/rustc-dev/{sha}"))
1190-
}
1191-
RemapScheme::NonCompiler => {
1192-
// For non-compiler sources, use `/rustc/{sha}` remapping scheme.
1193-
Some(format!("/rustc/{sha}"))
1194-
}
1195-
}
1170+
let sha = self.rust_sha().unwrap_or(&self.version);
1171+
1172+
match remap_scheme {
1173+
RemapScheme::Compiler => {
1174+
// For compiler sources, remap via `/rustc-dev/{sha}` to allow
1175+
// distinguishing between compiler sources vs library sources, since
1176+
// `rustc-dev` dist component places them under
1177+
// `$sysroot/lib/rustlib/rustc-src/rust` as opposed to `rust-src`'s
1178+
// `$sysroot/lib/rustlib/src/rust`.
1179+
//
1180+
// Keep this scheme in sync with `rustc_metadata::rmeta::decoder`'s
1181+
// `try_to_translate_virtual_to_real`.
1182+
Some(format!("/rustc-dev/{sha}"))
1183+
}
1184+
RemapScheme::NonCompiler => {
1185+
// For non-compiler sources, use `/rustc/{sha}` remapping scheme.
1186+
Some(format!("/rustc/{sha}"))
11961187
}
1197-
GitRepo::Llvm => Some(String::from("/rustc/llvm")),
11981188
}
11991189
}
12001190

@@ -1237,12 +1227,7 @@ impl Build {
12371227
}
12381228

12391229
/// Returns extra C flags that `cc-rs` doesn't handle.
1240-
fn cc_unhandled_cflags(
1241-
&self,
1242-
target: TargetSelection,
1243-
which: GitRepo,
1244-
c: CLang,
1245-
) -> Vec<String> {
1230+
fn cc_unhandled_cflags(&self, target: TargetSelection, c: CLang) -> Vec<String> {
12461231
let mut base = Vec::new();
12471232

12481233
// If we're compiling C++ on macOS then we add a flag indicating that
@@ -1259,16 +1244,6 @@ impl Build {
12591244
base.push("-fno-omit-frame-pointer".into());
12601245
}
12611246

1262-
if let Some(map_to) = self.debuginfo_map_to(which, RemapScheme::NonCompiler) {
1263-
let map = format!("{}={}", self.src.display(), map_to);
1264-
let cc = self.cc_tool(target);
1265-
if cc.is_like_clang() || cc.is_like_gnu() {
1266-
base.push(format!("-fdebug-prefix-map={map}"));
1267-
} else if cc.is_like_clang_cl() {
1268-
base.push("-Xclang".into());
1269-
base.push(format!("-fdebug-prefix-map={map}"));
1270-
}
1271-
}
12721247
base
12731248
}
12741249

src/bootstrap/src/utils/cc_detect.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use std::path::{Path, PathBuf};
2727

2828
use crate::core::config::{CompressDebuginfo, Subcommand, TargetSelection};
2929
use crate::utils::exec::{BootstrapCommand, command};
30-
use crate::{Build, CLang, GitRepo};
30+
use crate::{Build, CLang};
3131

3232
/// Creates and configures a new [`cc::Build`] instance for the given target.
3333
fn new_cc_build(build: &Build, target: TargetSelection) -> cc::Build {
@@ -124,7 +124,7 @@ pub fn fill_target_compiler(build: &mut Build, target: TargetSelection) {
124124

125125
build.cc.insert(target, compiler.clone());
126126
let mut cflags = build.cc_handled_cflags(target, CLang::C);
127-
cflags.extend(build.cc_unhandled_cflags(target, GitRepo::Rustc, CLang::C));
127+
cflags.extend(build.cc_unhandled_cflags(target, CLang::C));
128128

129129
// If we use llvm-libunwind, we will need a C++ compiler as well for all targets
130130
// We'll need one anyways if the target triple is also a host triple
@@ -151,7 +151,7 @@ pub fn fill_target_compiler(build: &mut Build, target: TargetSelection) {
151151
build.do_if_verbose(|| println!("CFLAGS_{} = {cflags:?}", target.triple));
152152
if let Ok(cxx) = build.cxx(target) {
153153
let mut cxxflags = build.cc_handled_cflags(target, CLang::Cxx);
154-
cxxflags.extend(build.cc_unhandled_cflags(target, GitRepo::Rustc, CLang::Cxx));
154+
cxxflags.extend(build.cc_unhandled_cflags(target, CLang::Cxx));
155155
build.do_if_verbose(|| println!("CXX_{} = {cxx:?}", target.triple));
156156
build.do_if_verbose(|| println!("CXXFLAGS_{} = {cxxflags:?}", target.triple));
157157
}

0 commit comments

Comments
 (0)