@@ -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
2324impl < ' 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 }
0 commit comments