Skip to content

Commit f1397de

Browse files
Copilotmessense
andcommitted
Use cargo_config2 and rustflags crate for better rustflags handling
Co-authored-by: messense <[email protected]>
1 parent 132bc64 commit f1397de

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

src/zig.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -539,35 +539,38 @@ impl Zig {
539539
}
540540
}
541541

542-
fn add_rustflags<K, V>(command: &mut Command, rustflags_env: K, new_flag: V)
542+
fn add_rustflags<K, V>(command: &mut Command, rustflags_env: K, new_flag: V, target: &str) -> Result<()>
543543
where
544544
K: AsRef<OsStr>,
545545
V: AsRef<OsStr>,
546546
{
547547
let env_key = rustflags_env.as_ref();
548548
let flag_str = new_flag.as_ref().to_string_lossy();
549549

550-
// Check if RUSTFLAGS is already set in the command environment
551-
if let Some((_, existing_value)) = command.get_envs().find(|(key, _)| *key == env_key) {
552-
if let Some(existing) = existing_value {
553-
let existing_str = existing.to_string_lossy();
554-
if !existing_str.contains(&*flag_str) {
555-
let new_value = format!("{} {}", existing_str, flag_str);
556-
command.env(env_key, new_value);
557-
}
558-
} else {
559-
command.env(env_key, flag_str.as_ref());
560-
}
561-
} else if let Ok(existing) = env::var(env_key) {
562-
// Check if it's already set in the process environment
563-
if !existing.contains(&*flag_str) {
564-
let new_value = format!("{} {}", existing, flag_str);
565-
command.env(env_key, new_value);
550+
// Load existing rustflags from cargo config
551+
let cargo_config = cargo_config2::Config::load()?;
552+
let existing_flags = cargo_config.rustflags(target)?.unwrap_or_default();
553+
554+
// Get the encoded string representation of existing flags
555+
let mut combined_flags = String::new();
556+
if !existing_flags.flags.is_empty() {
557+
combined_flags = existing_flags.encode()?;
558+
}
559+
560+
// Check if our flag is already present
561+
if !combined_flags.contains(&*flag_str) {
562+
if !combined_flags.is_empty() {
563+
combined_flags.push(' ');
566564
}
567-
} else {
568-
// Not set anywhere, set it now
569-
command.env(env_key, flag_str.as_ref());
565+
combined_flags.push_str(&flag_str);
566+
}
567+
568+
// Set the combined flags
569+
if !combined_flags.is_empty() {
570+
command.env(env_key, combined_flags);
570571
}
572+
573+
Ok(())
571574
}
572575

573576
pub(crate) fn apply_command_env(
@@ -625,7 +628,7 @@ impl Zig {
625628
// Use -Cdlltool=<path> rustc flag instead of environment variables
626629
let rustflags_env = format!("CARGO_TARGET_{}_RUSTFLAGS", env_target.to_uppercase());
627630
let dlltool_flag = format!("-Cdlltool={}", zig_wrapper.dlltool.display());
628-
Self::add_rustflags(cmd, rustflags_env, dlltool_flag);
631+
Self::add_rustflags(cmd, rustflags_env, dlltool_flag, parsed_target)?;
629632
}
630633

631634
// Only setup AR when explicitly asked to

0 commit comments

Comments
 (0)