@@ -197,6 +197,7 @@ pub struct Tool {
197197    cc_wrapper_args :  Vec < OsString > , 
198198    args :  Vec < OsString > , 
199199    env :  Vec < ( OsString ,  OsString ) > , 
200+     env_remove :  Vec < OsString > , 
200201    family :  ToolFamily , 
201202    cuda :  bool , 
202203    removed_args :  Vec < OsString > , 
@@ -1516,12 +1517,8 @@ impl Build {
15161517        let  ( mut  cmd,  name)  = if  is_assembler_msvc { 
15171518            self . msvc_macro_assembler ( ) ?
15181519        }  else  { 
1519-             let  mut  cmd = compiler. to_command ( ) ; 
1520-             for  & ( ref  a,  ref  b)  in  self . env . iter ( )  { 
1521-                 cmd. env ( a,  b) ; 
1522-             } 
15231520            ( 
1524-                 cmd , 
1521+                 compiler . to_command ( ) , 
15251522                compiler
15261523                    . path 
15271524                    . file_name ( ) 
@@ -1552,9 +1549,6 @@ impl Build {
15521549            cmd. arg ( "--" ) ; 
15531550        } 
15541551        cmd. arg ( & obj. src ) ; 
1555-         if  cfg ! ( target_os = "macos" )  { 
1556-             self . fix_env_for_apple_os ( & mut  cmd) ?; 
1557-         } 
15581552
15591553        Ok ( ( cmd,  name) ) 
15601554    } 
@@ -1563,9 +1557,7 @@ impl Build {
15631557pub  fn  try_expand ( & self )  -> Result < Vec < u8 > ,  Error >  { 
15641558        let  compiler = self . try_get_compiler ( ) ?; 
15651559        let  mut  cmd = compiler. to_command ( ) ; 
1566-         for  & ( ref  a,  ref  b)  in  self . env . iter ( )  { 
1567-             cmd. env ( a,  b) ; 
1568-         } 
1560+ 
15691561        cmd. arg ( "-E" ) ; 
15701562
15711563        assert ! ( 
@@ -1711,6 +1703,19 @@ impl Build {
17111703            cmd. push_cc_arg ( warnings_to_errors_flag) ; 
17121704        } 
17131705
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+ 
17141719        Ok ( cmd) 
17151720    } 
17161721
@@ -3343,9 +3348,11 @@ impl Build {
33433348        } 
33443349    } 
33453350
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 >  { 
33473352        let  target = self . get_target ( ) ?; 
33483353        let  host = self . get_host ( ) ?; 
3354+         let  mut  env = HashMap :: new ( ) ; 
3355+ 
33493356        if  host. contains ( "apple-darwin" )  && target. contains ( "apple-darwin" )  { 
33503357            // If, for example, `cargo` runs during the build of an XCode project, then `SDKROOT` environment variable 
33513358            // would represent the current target, and this is the problem for us, if we want to compile something 
@@ -3357,15 +3364,16 @@ impl Build {
33573364            if  let  Ok ( sdkroot)  = env:: var ( "SDKROOT" )  { 
33583365                if  !sdkroot. contains ( "MacOSX" )  { 
33593366                    let  macos_sdk = self . apple_sdk_root ( "macosx" ) ?; 
3360-                     cmd . env ( "SDKROOT" ,   macos_sdk) ; 
3367+                     env. insert ( "SDKROOT" . into ( ) ,   Some ( macos_sdk) ) ; 
33613368                } 
33623369            } 
33633370            // Additionally, `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at 
33643371            // "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld", 
33653372            // 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 ) ; 
33673374        } 
3368-         Ok ( ( ) ) 
3375+ 
3376+         Ok ( env) 
33693377    } 
33703378
33713379    fn  apple_sdk_root ( & self ,  sdk :  & str )  -> Result < OsString ,  Error >  { 
@@ -3431,6 +3439,7 @@ impl Tool {
34313439            cc_wrapper_args :  Vec :: new ( ) , 
34323440            args :  Vec :: new ( ) , 
34333441            env :  Vec :: new ( ) , 
3442+             env_remove :  Vec :: new ( ) , 
34343443            family :  family, 
34353444            cuda :  false , 
34363445            removed_args :  Vec :: new ( ) , 
@@ -3462,6 +3471,7 @@ impl Tool {
34623471            cc_wrapper_args :  Vec :: new ( ) , 
34633472            args :  Vec :: new ( ) , 
34643473            env :  Vec :: new ( ) , 
3474+             env_remove :  Vec :: new ( ) , 
34653475            family :  family, 
34663476            cuda :  cuda, 
34673477            removed_args :  Vec :: new ( ) , 
@@ -3550,9 +3560,14 @@ impl Tool {
35503560            . collect :: < Vec < _ > > ( ) ; 
35513561        cmd. args ( & value) ; 
35523562
3553-         for  & ( ref  k ,   ref  v)  in  self . env . iter ( )  { 
3563+         for  ( k ,  v)  in  self . env . iter ( )  { 
35543564            cmd. env ( k,  v) ; 
35553565        } 
3566+ 
3567+         for  k in  self . env_remove . iter ( )  { 
3568+             cmd. env_remove ( k) ; 
3569+         } 
3570+ 
35563571        cmd
35573572    } 
35583573
@@ -3572,12 +3587,16 @@ impl Tool {
35723587
35733588    /// Returns the set of environment variables needed for this compiler to 
35743589/// operate. 
3575- /// 
3576- /// This is typically only used for MSVC compilers currently. 
35773590pub  fn  env ( & self )  -> & [ ( OsString ,  OsString ) ]  { 
35783591        & self . env 
35793592    } 
35803593
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+ 
35813600    /// Returns the compiler command in format of CC environment variable. 
35823601/// Or empty string if CC env was not present 
35833602/// 
0 commit comments