@@ -539,35 +539,38 @@ impl Zig {
539
539
}
540
540
}
541
541
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 < ( ) >
543
543
where
544
544
K : AsRef < OsStr > ,
545
545
V : AsRef < OsStr > ,
546
546
{
547
547
let env_key = rustflags_env. as_ref ( ) ;
548
548
let flag_str = new_flag. as_ref ( ) . to_string_lossy ( ) ;
549
549
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 ( ' ' ) ;
566
564
}
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) ;
570
571
}
572
+
573
+ Ok ( ( ) )
571
574
}
572
575
573
576
pub ( crate ) fn apply_command_env (
@@ -625,7 +628,7 @@ impl Zig {
625
628
// Use -Cdlltool=<path> rustc flag instead of environment variables
626
629
let rustflags_env = format ! ( "CARGO_TARGET_{}_RUSTFLAGS" , env_target. to_uppercase( ) ) ;
627
630
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 ) ? ;
629
632
}
630
633
631
634
// Only setup AR when explicitly asked to
0 commit comments