Skip to content

Commit 6d88dd7

Browse files
committed
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 have a side effect of upgrading the clang version used on rustc CI. (rust-lang/rust#98746) Ideally, the prefered 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 prefered DWARF version (rust-lang/rust#98350)
1 parent 53272c5 commit 6d88dd7

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/lib.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,17 @@ enum ToolFamily {
212212

213213
impl ToolFamily {
214214
/// What the flag to request debug info for this family of tools look like
215-
fn add_debug_flags(&self, cmd: &mut Tool) {
215+
fn add_debug_flags(&self, cmd: &mut Tool, dwarf_version: Option<u32>) {
216216
match *self {
217217
ToolFamily::Msvc { .. } => {
218218
cmd.push_cc_arg("-Z7".into());
219219
}
220220
ToolFamily::Gnu | ToolFamily::Clang => {
221-
cmd.push_cc_arg("-g".into());
221+
cmd.push_cc_arg(
222+
dwarf_version
223+
.map_or_else(|| "-g".into(), |v| format!("-gdwarf-{}", v))
224+
.into(),
225+
);
222226
}
223227
}
224228
}
@@ -1555,7 +1559,7 @@ impl Build {
15551559
cmd.args.push("-G".into());
15561560
}
15571561
let family = cmd.family;
1558-
family.add_debug_flags(cmd);
1562+
family.add_debug_flags(cmd, self.get_dwarf_version());
15591563
}
15601564

15611565
if self.get_force_frame_pointer() {
@@ -2771,6 +2775,23 @@ impl Build {
27712775
})
27722776
}
27732777

2778+
fn get_dwarf_version(&self) -> Option<u32> {
2779+
let target = self.get_target().ok()?;
2780+
if target.contains("android")
2781+
|| target.contains("apple")
2782+
|| target.contains("dragonfly")
2783+
|| target.contains("freebsd")
2784+
|| target.contains("netbsd")
2785+
|| target.contains("openbsd")
2786+
{
2787+
Some(2)
2788+
} else if target.contains("linux") {
2789+
Some(4)
2790+
} else {
2791+
None
2792+
}
2793+
}
2794+
27742795
fn get_force_frame_pointer(&self) -> bool {
27752796
self.force_frame_pointer.unwrap_or_else(|| self.get_debug())
27762797
}

tests/test.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn gnu_smoke() {
2020
test.cmd(0)
2121
.must_have("-O2")
2222
.must_have("foo.c")
23-
.must_not_have("-g")
23+
.must_not_have("-gdwarf-4")
2424
.must_have("-c")
2525
.must_have("-ffunction-sections")
2626
.must_have("-fdata-sections");
@@ -52,19 +52,34 @@ fn gnu_opt_level_s() {
5252
.must_not_have("-Oz");
5353
}
5454

55+
#[test]
56+
fn gnu_debug() {
57+
let test = Test::gnu();
58+
test.gcc().debug(true).file("foo.c").compile("foo");
59+
test.cmd(0).must_have("-gdwarf-4");
60+
61+
let test = Test::gnu();
62+
test.gcc()
63+
.target("x86_64-apple-darwin")
64+
.debug(true)
65+
.file("foo.c")
66+
.compile("foo");
67+
test.cmd(0).must_have("-gdwarf-2");
68+
}
69+
5570
#[test]
5671
fn gnu_debug_fp_auto() {
5772
let test = Test::gnu();
5873
test.gcc().debug(true).file("foo.c").compile("foo");
59-
test.cmd(0).must_have("-g");
74+
test.cmd(0).must_have("-gdwarf-4");
6075
test.cmd(0).must_have("-fno-omit-frame-pointer");
6176
}
6277

6378
#[test]
6479
fn gnu_debug_fp() {
6580
let test = Test::gnu();
6681
test.gcc().debug(true).file("foo.c").compile("foo");
67-
test.cmd(0).must_have("-g");
82+
test.cmd(0).must_have("-gdwarf-4");
6883
test.cmd(0).must_have("-fno-omit-frame-pointer");
6984
}
7085

@@ -78,7 +93,7 @@ fn gnu_debug_nofp() {
7893
.force_frame_pointer(false)
7994
.file("foo.c")
8095
.compile("foo");
81-
test.cmd(0).must_have("-g");
96+
test.cmd(0).must_have("-gdwarf-4");
8297
test.cmd(0).must_not_have("-fno-omit-frame-pointer");
8398

8499
let test = Test::gnu();
@@ -87,7 +102,7 @@ fn gnu_debug_nofp() {
87102
.debug(true)
88103
.file("foo.c")
89104
.compile("foo");
90-
test.cmd(0).must_have("-g");
105+
test.cmd(0).must_have("-gdwarf-4");
91106
test.cmd(0).must_not_have("-fno-omit-frame-pointer");
92107
}
93108

0 commit comments

Comments
 (0)