Skip to content

Commit 894fae4

Browse files
committed
Add support for setting -gdwarf-{version} based on RUSTFLAGS
Detect if `-Zdwarf-version` (which will probably be stabilized soon as `-Cdwarf-version`) was passed in RUSTFLAGS and set the corresponding Clang/GCC option to the same value.
1 parent 4acb868 commit 894fae4

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/flags.rs

+20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub(crate) struct RustcCodegenFlags<'a> {
1818
force_frame_pointers: Option<bool>,
1919
no_redzone: Option<bool>,
2020
soft_float: Option<bool>,
21+
dwarf_version: Option<u32>,
2122
}
2223

2324
impl<'this> RustcCodegenFlags<'this> {
@@ -86,6 +87,10 @@ impl<'this> RustcCodegenFlags<'this> {
8687
}
8788
}
8889

90+
fn arg_to_u32(arg: impl AsRef<str>) -> Option<u32> {
91+
arg.as_ref().parse().ok()
92+
}
93+
8994
let (flag, value) = if let Some((flag, value)) = flag.split_once('=') {
9095
(flag, Some(value))
9196
} else {
@@ -148,6 +153,14 @@ impl<'this> RustcCodegenFlags<'this> {
148153
self.branch_protection =
149154
Some(flag_ok_or(value, "-Zbranch-protection must have a value")?);
150155
}
156+
// https://doc.rust-lang.org/beta/unstable-book/compiler-flags/dwarf-version.html
157+
// FIXME: Drop the -Z variant and update the doc link once the option is stablized
158+
"-Zdwarf-version" | "-Cdwarf-version" => {
159+
self.dwarf_version = Some(value.and_then(arg_to_u32).ok_or(Error::new(
160+
ErrorKind::InvalidFlag,
161+
"-Zdwarf-version must have a value",
162+
))?);
163+
}
151164
_ => {}
152165
}
153166
Ok(())
@@ -250,6 +263,11 @@ impl<'this> RustcCodegenFlags<'this> {
250263
};
251264
push_if_supported(cc_flag.into());
252265
}
266+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-gdwarf-2
267+
// https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html#index-gdwarf
268+
if let Some(value) = self.dwarf_version {
269+
push_if_supported(format!("-gdwarf-{value}").into());
270+
}
253271
}
254272

255273
// Compiler-exclusive flags
@@ -390,6 +408,7 @@ mod tests {
390408
"-Crelocation-model=pic",
391409
"-Csoft-float=yes",
392410
"-Zbranch-protection=bti,pac-ret,leaf",
411+
"-Zdwarf-version=5",
393412
// Set flags we don't recognise but rustc supports next
394413
// rustc flags
395414
"--cfg",
@@ -496,6 +515,7 @@ mod tests {
496515
relocation_model: Some("pic"),
497516
soft_float: Some(true),
498517
branch_protection: Some("bti,pac-ret,leaf"),
518+
dwarf_version: Some(5),
499519
},
500520
);
501521
}

tests/rustflags.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ fn inherits_rustflags() {
1919
// Correctly inherits flags from rustc
2020
std::env::set_var(
2121
"CARGO_ENCODED_RUSTFLAGS",
22-
"-Cforce-frame-pointers=true\u{1f}-Ccode-model=small\u{1f}-Csoft-float",
22+
"-Cforce-frame-pointers=true\u{1f}-Ccode-model=small\u{1f}-Csoft-float\u{1f}-Cdwarf-version=5",
2323
);
2424
let test = Test::gnu();
2525
test.gcc().file("foo.c").compile("foo");
2626
test.cmd(0)
2727
.must_have("-fno-omit-frame-pointer")
2828
.must_have("-mcmodel=small")
29-
.must_have("-msoft-float");
29+
.must_have("-msoft-float")
30+
.must_have("-gdwarf-5");
3031
}

0 commit comments

Comments
 (0)