Skip to content

Commit 8fac353

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 8fac353

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/flags.rs

+13
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<&'a str>,
2122
}
2223

2324
impl<'this> RustcCodegenFlags<'this> {
@@ -148,6 +149,11 @@ impl<'this> RustcCodegenFlags<'this> {
148149
self.branch_protection =
149150
Some(flag_ok_or(value, "-Zbranch-protection must have a value")?);
150151
}
152+
// https://doc.rust-lang.org/beta/unstable-book/compiler-flags/dwarf-version.html
153+
// FIXME: Drop the -Z variant and update the doc link once the option is stablized
154+
"-Zdwarf-version" | "-Cdwarf-version" => {
155+
self.dwarf_version = Some(flag_ok_or(value, "-Zdwarf-version must have a value")?);
156+
}
151157
_ => {}
152158
}
153159
Ok(())
@@ -250,6 +256,11 @@ impl<'this> RustcCodegenFlags<'this> {
250256
};
251257
push_if_supported(cc_flag.into());
252258
}
259+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-gdwarf-2
260+
// https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html#index-gdwarf
261+
if let Some(value) = self.dwarf_version {
262+
push_if_supported(format!("-gdwarf-{value}").into());
263+
}
253264
}
254265

255266
// Compiler-exclusive flags
@@ -390,6 +401,7 @@ mod tests {
390401
"-Crelocation-model=pic",
391402
"-Csoft-float=yes",
392403
"-Zbranch-protection=bti,pac-ret,leaf",
404+
"-Zdwarf-version=5",
393405
// Set flags we don't recognise but rustc supports next
394406
// rustc flags
395407
"--cfg",
@@ -496,6 +508,7 @@ mod tests {
496508
relocation_model: Some("pic"),
497509
soft_float: Some(true),
498510
branch_protection: Some("bti,pac-ret,leaf"),
511+
dwarf_version: Some("5"),
499512
},
500513
);
501514
}

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)