Skip to content

Commit

Permalink
build: add_inherited_rustflags: Apply code review suggestions
Browse files Browse the repository at this point in the history
Co-authored-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>
  • Loading branch information
mrkajetanp and NobodyXu authored Nov 27, 2024
1 parent fff1a82 commit f438b0d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
47 changes: 21 additions & 26 deletions src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ impl RustcCodegenFlags {
// Parse flags obtained from CARGO_ENCODED_RUSTFLAGS
pub(crate) fn parse(rustflags_env: &str) -> Result<RustcCodegenFlags, Error> {
fn is_flag_prefix(flag: &str) -> bool {
matches!(
flag,
"-Z" | "-C"
| "--codegen"
| "-L"
| "-l"
| "-o"
| "-W"
| "--warn"
| "-A"
| "--allow"
| "-D"
| "--deny"
| "-F"
| "--forbid"
)
[
"-Z",
"-C",
"--codegen",
"-L",
"-l",
"-o",
"-W",
"--warn",
"-A",
"--allow",
"-D",
"--deny",
"-F",
"--forbid",
].contains(flag)
}

fn handle_flag_prefix<'a>(prev: &'a str, curr: &'a str) -> Cow<'a, str> {
Expand All @@ -64,17 +64,12 @@ impl RustcCodegenFlags {

let mut prev_prefix = None;
for curr in rustflags_env.split("\u{1f}") {
let prev = if let Some(prev) = prev_prefix {
prev_prefix = None;
prev
} else {
let prev = prev_prefix.take().unwrap_or("");
if prev.is_empty() && is_flag_prefix(curr) {
// Do not process prefixes on their own
if is_flag_prefix(curr) {
prev_prefix = Some(curr);
continue;
}
""
};
prev_prefix = Some(curr);
continue;
}

let rustc_flag = handle_flag_prefix(prev, curr);
codegen_flags.set_rustc_flag(&rustc_flag)?;
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2399,9 +2399,7 @@ impl Build {

let env = env_os.to_string_lossy();
let codegen_flags = RustcCodegenFlags::parse(&env)?;
let mut cc_flags_args: Vec<OsString> = vec![];
codegen_flags.cc_flags(self, path, *family, target, &mut cc_flags_args);
args.extend(cc_flags_args);
codegen_flags.cc_flags(self, path, *family, target, &mut args);
Ok(())
}

Expand Down

0 comments on commit f438b0d

Please sign in to comment.