Use a DWARF version consistent with rustc

Rustc defaults to DWARF-2 on some targets, and DWARF-4 on others.
However using -g with the C compiler yields whatever default version the
C compiler prefers.

One side effect is that the DWARF debug info shipped in some libraries
with rustc itself (e.g. libcompiler_builtins and others) have recently
switched to DWARF-5 as a side effect of upgrading the clang version
used on rustc CI. (https://github.com/rust-lang/rust/issues/98746)

Ideally, the preferred DWARF version would be given by the rust compiler
and/or cargo, but that's not the case at the moment, so the next best
thing is something that aligns with the current defaults, although
work in under way to add a rustc flag that would allow to pick the
preferred DWARF version (https://github.com/rust-lang/rust/pull/98350)
This commit is contained in:
Mike Hommey
2022-07-07 13:49:21 +09:00
committed by Thom Chiovoloni
parent e84e342a36
commit 0d90b42e49
2 changed files with 46 additions and 8 deletions
+26 -3
View File
@@ -214,13 +214,17 @@ enum ToolFamily {
impl ToolFamily {
/// What the flag to request debug info for this family of tools look like
fn add_debug_flags(&self, cmd: &mut Tool) {
fn add_debug_flags(&self, cmd: &mut Tool, dwarf_version: Option<u32>) {
match *self {
ToolFamily::Msvc { .. } => {
cmd.push_cc_arg("-Z7".into());
}
ToolFamily::Gnu | ToolFamily::Clang => {
cmd.push_cc_arg("-g".into());
cmd.push_cc_arg(
dwarf_version
.map_or_else(|| "-g".into(), |v| format!("-gdwarf-{}", v))
.into(),
);
}
}
}
@@ -1589,7 +1593,7 @@ impl Build {
cmd.args.push("-G".into());
}
let family = cmd.family;
family.add_debug_flags(cmd);
family.add_debug_flags(cmd, self.get_dwarf_version());
}
if self.get_force_frame_pointer() {
@@ -2848,6 +2852,25 @@ impl Build {
})
}
fn get_dwarf_version(&self) -> Option<u32> {
// Tentatively matches the DWARF version defaults as of rustc 1.62.
let target = self.get_target().ok()?;
if target.contains("android")
|| target.contains("apple")
|| target.contains("dragonfly")
|| target.contains("freebsd")
|| target.contains("netbsd")
|| target.contains("openbsd")
|| target.contains("windows-gnu")
{
Some(2)
} else if target.contains("linux") {
Some(4)
} else {
None
}
}
fn get_force_frame_pointer(&self) -> bool {
self.force_frame_pointer.unwrap_or_else(|| self.get_debug())
}
+20 -5
View File
@@ -20,7 +20,7 @@ fn gnu_smoke() {
test.cmd(0)
.must_have("-O2")
.must_have("foo.c")
.must_not_have("-g")
.must_not_have("-gdwarf-4")
.must_have("-c")
.must_have("-ffunction-sections")
.must_have("-fdata-sections");
@@ -52,11 +52,26 @@ fn gnu_opt_level_s() {
.must_not_have("-Oz");
}
#[test]
fn gnu_debug() {
let test = Test::gnu();
test.gcc().debug(true).file("foo.c").compile("foo");
test.cmd(0).must_have("-gdwarf-4");
let test = Test::gnu();
test.gcc()
.target("x86_64-apple-darwin")
.debug(true)
.file("foo.c")
.compile("foo");
test.cmd(0).must_have("-gdwarf-2");
}
#[test]
fn gnu_debug_fp_auto() {
let test = Test::gnu();
test.gcc().debug(true).file("foo.c").compile("foo");
test.cmd(0).must_have("-g");
test.cmd(0).must_have("-gdwarf-4");
test.cmd(0).must_have("-fno-omit-frame-pointer");
}
@@ -64,7 +79,7 @@ fn gnu_debug_fp_auto() {
fn gnu_debug_fp() {
let test = Test::gnu();
test.gcc().debug(true).file("foo.c").compile("foo");
test.cmd(0).must_have("-g");
test.cmd(0).must_have("-gdwarf-4");
test.cmd(0).must_have("-fno-omit-frame-pointer");
}
@@ -78,7 +93,7 @@ fn gnu_debug_nofp() {
.force_frame_pointer(false)
.file("foo.c")
.compile("foo");
test.cmd(0).must_have("-g");
test.cmd(0).must_have("-gdwarf-4");
test.cmd(0).must_not_have("-fno-omit-frame-pointer");
let test = Test::gnu();
@@ -87,7 +102,7 @@ fn gnu_debug_nofp() {
.debug(true)
.file("foo.c")
.compile("foo");
test.cmd(0).must_have("-g");
test.cmd(0).must_have("-gdwarf-4");
test.cmd(0).must_not_have("-fno-omit-frame-pointer");
}