@@ -197,6 +197,7 @@ pub struct Tool {
197
197
cc_wrapper_args : Vec < OsString > ,
198
198
args : Vec < OsString > ,
199
199
env : Vec < ( OsString , OsString ) > ,
200
+ env_remove : Vec < OsString > ,
200
201
family : ToolFamily ,
201
202
cuda : bool ,
202
203
removed_args : Vec < OsString > ,
@@ -1516,12 +1517,8 @@ impl Build {
1516
1517
let ( mut cmd, name) = if is_assembler_msvc {
1517
1518
self . msvc_macro_assembler ( ) ?
1518
1519
} else {
1519
- let mut cmd = compiler. to_command ( ) ;
1520
- for & ( ref a, ref b) in self . env . iter ( ) {
1521
- cmd. env ( a, b) ;
1522
- }
1523
1520
(
1524
- cmd ,
1521
+ compiler . to_command ( ) ,
1525
1522
compiler
1526
1523
. path
1527
1524
. file_name ( )
@@ -1552,9 +1549,6 @@ impl Build {
1552
1549
cmd. arg ( "--" ) ;
1553
1550
}
1554
1551
cmd. arg ( & obj. src ) ;
1555
- if cfg ! ( target_os = "macos" ) {
1556
- self . fix_env_for_apple_os ( & mut cmd) ?;
1557
- }
1558
1552
1559
1553
Ok ( ( cmd, name) )
1560
1554
}
@@ -1563,9 +1557,7 @@ impl Build {
1563
1557
pub fn try_expand ( & self ) -> Result < Vec < u8 > , Error > {
1564
1558
let compiler = self . try_get_compiler ( ) ?;
1565
1559
let mut cmd = compiler. to_command ( ) ;
1566
- for & ( ref a, ref b) in self . env . iter ( ) {
1567
- cmd. env ( a, b) ;
1568
- }
1560
+
1569
1561
cmd. arg ( "-E" ) ;
1570
1562
1571
1563
assert ! (
@@ -1711,6 +1703,19 @@ impl Build {
1711
1703
cmd. push_cc_arg ( warnings_to_errors_flag) ;
1712
1704
}
1713
1705
1706
+ for ( k, v) in self . env . iter ( ) {
1707
+ cmd. env . push ( ( k. to_os_string ( ) , v. to_os_string ( ) ) ) ;
1708
+ }
1709
+
1710
+ if cfg ! ( target_os = "macos" ) {
1711
+ for ( k, v) in self . fixed_env_for_apple_os ( ) ? {
1712
+ match v {
1713
+ Some ( v) => cmd. env . push ( ( k, v) ) ,
1714
+ None => cmd. env_remove . push ( k) ,
1715
+ }
1716
+ }
1717
+ }
1718
+
1714
1719
Ok ( cmd)
1715
1720
}
1716
1721
@@ -3343,9 +3348,11 @@ impl Build {
3343
3348
}
3344
3349
}
3345
3350
3346
- fn fix_env_for_apple_os ( & self , cmd : & mut Command ) -> Result < ( ) , Error > {
3351
+ fn fixed_env_for_apple_os ( & self ) -> Result < HashMap < OsString , Option < OsString > > , Error > {
3347
3352
let target = self . get_target ( ) ?;
3348
3353
let host = self . get_host ( ) ?;
3354
+ let mut env = HashMap :: new ( ) ;
3355
+
3349
3356
if host. contains ( "apple-darwin" ) && target. contains ( "apple-darwin" ) {
3350
3357
// If, for example, `cargo` runs during the build of an XCode project, then `SDKROOT` environment variable
3351
3358
// would represent the current target, and this is the problem for us, if we want to compile something
@@ -3357,15 +3364,16 @@ impl Build {
3357
3364
if let Ok ( sdkroot) = env:: var ( "SDKROOT" ) {
3358
3365
if !sdkroot. contains ( "MacOSX" ) {
3359
3366
let macos_sdk = self . apple_sdk_root ( "macosx" ) ?;
3360
- cmd . env ( "SDKROOT" , macos_sdk) ;
3367
+ env. insert ( "SDKROOT" . into ( ) , Some ( macos_sdk) ) ;
3361
3368
}
3362
3369
}
3363
3370
// Additionally, `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at
3364
3371
// "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld",
3365
3372
// although this is apparently ignored when using the linker at "/usr/bin/ld".
3366
- cmd . env_remove ( "IPHONEOS_DEPLOYMENT_TARGET" ) ;
3373
+ env . insert ( "IPHONEOS_DEPLOYMENT_TARGET" . into ( ) , None ) ;
3367
3374
}
3368
- Ok ( ( ) )
3375
+
3376
+ Ok ( env)
3369
3377
}
3370
3378
3371
3379
fn apple_sdk_root ( & self , sdk : & str ) -> Result < OsString , Error > {
@@ -3431,6 +3439,7 @@ impl Tool {
3431
3439
cc_wrapper_args : Vec :: new ( ) ,
3432
3440
args : Vec :: new ( ) ,
3433
3441
env : Vec :: new ( ) ,
3442
+ env_remove : Vec :: new ( ) ,
3434
3443
family : family,
3435
3444
cuda : false ,
3436
3445
removed_args : Vec :: new ( ) ,
@@ -3462,6 +3471,7 @@ impl Tool {
3462
3471
cc_wrapper_args : Vec :: new ( ) ,
3463
3472
args : Vec :: new ( ) ,
3464
3473
env : Vec :: new ( ) ,
3474
+ env_remove : Vec :: new ( ) ,
3465
3475
family : family,
3466
3476
cuda : cuda,
3467
3477
removed_args : Vec :: new ( ) ,
@@ -3550,9 +3560,14 @@ impl Tool {
3550
3560
. collect :: < Vec < _ > > ( ) ;
3551
3561
cmd. args ( & value) ;
3552
3562
3553
- for & ( ref k , ref v) in self . env . iter ( ) {
3563
+ for ( k , v) in self . env . iter ( ) {
3554
3564
cmd. env ( k, v) ;
3555
3565
}
3566
+
3567
+ for k in self . env_remove . iter ( ) {
3568
+ cmd. env_remove ( k) ;
3569
+ }
3570
+
3556
3571
cmd
3557
3572
}
3558
3573
@@ -3572,12 +3587,16 @@ impl Tool {
3572
3587
3573
3588
/// Returns the set of environment variables needed for this compiler to
3574
3589
/// operate.
3575
- ///
3576
- /// This is typically only used for MSVC compilers currently.
3577
3590
pub fn env ( & self ) -> & [ ( OsString , OsString ) ] {
3578
3591
& self . env
3579
3592
}
3580
3593
3594
+ /// Returns the set of environment variables needed to be removed for this
3595
+ /// compiler to operate.
3596
+ pub fn env_remove ( & self ) -> & [ OsString ] {
3597
+ & self . env_remove
3598
+ }
3599
+
3581
3600
/// Returns the compiler command in format of CC environment variable.
3582
3601
/// Or empty string if CC env was not present
3583
3602
///
0 commit comments