@@ -196,6 +196,7 @@ pub struct Tool {
196
196
cc_wrapper_args : Vec < OsString > ,
197
197
args : Vec < OsString > ,
198
198
env : Vec < ( OsString , OsString ) > ,
199
+ env_remove : Vec < OsString > ,
199
200
family : ToolFamily ,
200
201
cuda : bool ,
201
202
removed_args : Vec < OsString > ,
@@ -1482,12 +1483,8 @@ impl Build {
1482
1483
let ( mut cmd, name) = if msvc && asm_ext == Some ( AsmFileExt :: DotAsm ) {
1483
1484
self . msvc_macro_assembler ( ) ?
1484
1485
} else {
1485
- let mut cmd = compiler. to_command ( ) ;
1486
- for & ( ref a, ref b) in self . env . iter ( ) {
1487
- cmd. env ( a, b) ;
1488
- }
1489
1486
(
1490
- cmd ,
1487
+ compiler . to_command ( ) ,
1491
1488
compiler
1492
1489
. path
1493
1490
. file_name ( )
@@ -1518,9 +1515,6 @@ impl Build {
1518
1515
cmd. arg ( "--" ) ;
1519
1516
}
1520
1517
cmd. arg ( & obj. src ) ;
1521
- if cfg ! ( target_os = "macos" ) {
1522
- self . fix_env_for_apple_os ( & mut cmd) ?;
1523
- }
1524
1518
1525
1519
Ok ( ( cmd, name) )
1526
1520
}
@@ -1529,9 +1523,7 @@ impl Build {
1529
1523
pub fn try_expand ( & self ) -> Result < Vec < u8 > , Error > {
1530
1524
let compiler = self . try_get_compiler ( ) ?;
1531
1525
let mut cmd = compiler. to_command ( ) ;
1532
- for & ( ref a, ref b) in self . env . iter ( ) {
1533
- cmd. env ( a, b) ;
1534
- }
1526
+
1535
1527
cmd. arg ( "-E" ) ;
1536
1528
1537
1529
assert ! (
@@ -1669,6 +1661,19 @@ impl Build {
1669
1661
cmd. push_cc_arg ( warnings_to_errors_flag) ;
1670
1662
}
1671
1663
1664
+ for ( k, v) in self . env . iter ( ) {
1665
+ cmd. env . push ( ( k. to_os_string ( ) , v. to_os_string ( ) ) ) ;
1666
+ }
1667
+
1668
+ if cfg ! ( target_os = "macos" ) {
1669
+ for ( k, v) in self . fixed_env_for_apple_os ( ) ? {
1670
+ match v {
1671
+ Some ( v) => cmd. env . push ( ( k, v) ) ,
1672
+ None => cmd. env_remove . push ( k) ,
1673
+ }
1674
+ }
1675
+ }
1676
+
1672
1677
Ok ( cmd)
1673
1678
}
1674
1679
@@ -3301,9 +3306,11 @@ impl Build {
3301
3306
}
3302
3307
}
3303
3308
3304
- fn fix_env_for_apple_os ( & self , cmd : & mut Command ) -> Result < ( ) , Error > {
3309
+ fn fixed_env_for_apple_os ( & self ) -> Result < HashMap < OsString , Option < OsString > > , Error > {
3305
3310
let target = self . get_target ( ) ?;
3306
3311
let host = self . get_host ( ) ?;
3312
+ let mut env = HashMap :: new ( ) ;
3313
+
3307
3314
if host. contains ( "apple-darwin" ) && target. contains ( "apple-darwin" ) {
3308
3315
// If, for example, `cargo` runs during the build of an XCode project, then `SDKROOT` environment variable
3309
3316
// would represent the current target, and this is the problem for us, if we want to compile something
@@ -3315,15 +3322,16 @@ impl Build {
3315
3322
if let Ok ( sdkroot) = env:: var ( "SDKROOT" ) {
3316
3323
if !sdkroot. contains ( "MacOSX" ) {
3317
3324
let macos_sdk = self . apple_sdk_root ( "macosx" ) ?;
3318
- cmd . env ( "SDKROOT" , macos_sdk) ;
3325
+ env. insert ( "SDKROOT" . into ( ) , Some ( macos_sdk) ) ;
3319
3326
}
3320
3327
}
3321
3328
// Additionally, `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at
3322
3329
// "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld",
3323
3330
// although this is apparently ignored when using the linker at "/usr/bin/ld".
3324
- cmd . env_remove ( "IPHONEOS_DEPLOYMENT_TARGET" ) ;
3331
+ env . insert ( "IPHONEOS_DEPLOYMENT_TARGET" . into ( ) , None ) ;
3325
3332
}
3326
- Ok ( ( ) )
3333
+
3334
+ Ok ( env)
3327
3335
}
3328
3336
3329
3337
fn apple_sdk_root ( & self , sdk : & str ) -> Result < OsString , Error > {
@@ -3389,6 +3397,7 @@ impl Tool {
3389
3397
cc_wrapper_args : Vec :: new ( ) ,
3390
3398
args : Vec :: new ( ) ,
3391
3399
env : Vec :: new ( ) ,
3400
+ env_remove : Vec :: new ( ) ,
3392
3401
family : family,
3393
3402
cuda : false ,
3394
3403
removed_args : Vec :: new ( ) ,
@@ -3420,6 +3429,7 @@ impl Tool {
3420
3429
cc_wrapper_args : Vec :: new ( ) ,
3421
3430
args : Vec :: new ( ) ,
3422
3431
env : Vec :: new ( ) ,
3432
+ env_remove : Vec :: new ( ) ,
3423
3433
family : family,
3424
3434
cuda : cuda,
3425
3435
removed_args : Vec :: new ( ) ,
@@ -3508,9 +3518,14 @@ impl Tool {
3508
3518
. collect :: < Vec < _ > > ( ) ;
3509
3519
cmd. args ( & value) ;
3510
3520
3511
- for & ( ref k , ref v) in self . env . iter ( ) {
3521
+ for ( k , v) in self . env . iter ( ) {
3512
3522
cmd. env ( k, v) ;
3513
3523
}
3524
+
3525
+ for k in self . env_remove . iter ( ) {
3526
+ cmd. env_remove ( k) ;
3527
+ }
3528
+
3514
3529
cmd
3515
3530
}
3516
3531
@@ -3530,12 +3545,16 @@ impl Tool {
3530
3545
3531
3546
/// Returns the set of environment variables needed for this compiler to
3532
3547
/// operate.
3533
- ///
3534
- /// This is typically only used for MSVC compilers currently.
3535
3548
pub fn env ( & self ) -> & [ ( OsString , OsString ) ] {
3536
3549
& self . env
3537
3550
}
3538
3551
3552
+ /// Returns the set of environment variables needed to be removed for this
3553
+ /// compiler to operate.
3554
+ pub fn env_remove ( & self ) -> & [ OsString ] {
3555
+ & self . env_remove
3556
+ }
3557
+
3539
3558
/// Returns the compiler command in format of CC environment variable.
3540
3559
/// Or empty string if CC env was not present
3541
3560
///
0 commit comments