Skip to content

Commit

Permalink
Add support for setting -gdwarf-{version} based on RUSTFLAGS
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wesleywiser committed Feb 11, 2025
1 parent 4acb868 commit 894fae4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub(crate) struct RustcCodegenFlags<'a> {
force_frame_pointers: Option<bool>,
no_redzone: Option<bool>,
soft_float: Option<bool>,
dwarf_version: Option<u32>,
}

impl<'this> RustcCodegenFlags<'this> {
Expand Down Expand Up @@ -86,6 +87,10 @@ impl<'this> RustcCodegenFlags<'this> {
}
}

fn arg_to_u32(arg: impl AsRef<str>) -> Option<u32> {
arg.as_ref().parse().ok()
}

let (flag, value) = if let Some((flag, value)) = flag.split_once('=') {
(flag, Some(value))
} else {
Expand Down Expand Up @@ -148,6 +153,14 @@ impl<'this> RustcCodegenFlags<'this> {
self.branch_protection =
Some(flag_ok_or(value, "-Zbranch-protection must have a value")?);
}
// https://doc.rust-lang.org/beta/unstable-book/compiler-flags/dwarf-version.html
// FIXME: Drop the -Z variant and update the doc link once the option is stablized
"-Zdwarf-version" | "-Cdwarf-version" => {
self.dwarf_version = Some(value.and_then(arg_to_u32).ok_or(Error::new(
ErrorKind::InvalidFlag,
"-Zdwarf-version must have a value",
))?);
}
_ => {}
}
Ok(())
Expand Down Expand Up @@ -250,6 +263,11 @@ impl<'this> RustcCodegenFlags<'this> {
};
push_if_supported(cc_flag.into());
}
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-gdwarf-2
// https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html#index-gdwarf
if let Some(value) = self.dwarf_version {
push_if_supported(format!("-gdwarf-{value}").into());
}
}

// Compiler-exclusive flags
Expand Down Expand Up @@ -390,6 +408,7 @@ mod tests {
"-Crelocation-model=pic",
"-Csoft-float=yes",
"-Zbranch-protection=bti,pac-ret,leaf",
"-Zdwarf-version=5",
// Set flags we don't recognise but rustc supports next
// rustc flags
"--cfg",
Expand Down Expand Up @@ -496,6 +515,7 @@ mod tests {
relocation_model: Some("pic"),
soft_float: Some(true),
branch_protection: Some("bti,pac-ret,leaf"),
dwarf_version: Some(5),
},
);
}
Expand Down
5 changes: 3 additions & 2 deletions tests/rustflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ fn inherits_rustflags() {
// Correctly inherits flags from rustc
std::env::set_var(
"CARGO_ENCODED_RUSTFLAGS",
"-Cforce-frame-pointers=true\u{1f}-Ccode-model=small\u{1f}-Csoft-float",
"-Cforce-frame-pointers=true\u{1f}-Ccode-model=small\u{1f}-Csoft-float\u{1f}-Cdwarf-version=5",
);
let test = Test::gnu();
test.gcc().file("foo.c").compile("foo");
test.cmd(0)
.must_have("-fno-omit-frame-pointer")
.must_have("-mcmodel=small")
.must_have("-msoft-float");
.must_have("-msoft-float")
.must_have("-gdwarf-5");
}

0 comments on commit 894fae4

Please sign in to comment.