@@ -894,6 +894,7 @@ impl Build {
894
894
///
895
895
/// This will return a result instead of panicing; see compile() for the complete description.
896
896
pub fn try_compile ( & self , output : & str ) -> Result < ( ) , Error > {
897
+ self . fix_env_variabes ( ) ?;
897
898
let ( lib_name, gnu_lib_name) = if output. starts_with ( "lib" ) && output. ends_with ( ".a" ) {
898
899
( & output[ 3 ..output. len ( ) - 2 ] , output. to_owned ( ) )
899
900
} else {
@@ -2467,6 +2468,28 @@ impl Build {
2467
2468
println ! ( "{}" , s) ;
2468
2469
}
2469
2470
}
2471
+
2472
+ fn fix_env_variabes ( & self ) -> Result < ( ) , Error > {
2473
+ let target = self . get_target ( ) ?;
2474
+ let host = self . get_host ( ) ?;
2475
+ if host. contains ( "apple-darwin" ) && target. contains ( "apple-darwin" ) {
2476
+ // Replace the `SDKROOT` environment variable if it's clearly set for the wrong platform, which
2477
+ // may occur when we're linking a custom build script while targeting iOS for example.
2478
+ // Removing is not enough, because of cc from XCode can not find include files in such case
2479
+ if let Ok ( sdkroot) = env:: var ( "SDKROOT" ) {
2480
+ if sdkroot. contains ( "iPhone" ) {
2481
+ env:: set_var ( "SDKROOT" , "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk" ) ;
2482
+ }
2483
+ }
2484
+ // Additionally, `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at
2485
+ // "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld",
2486
+ // although this is apparently ignored when using the linker at "/usr/bin/ld".
2487
+ env:: remove_var ( "IPHONEOS_DEPLOYMENT_TARGET" ) ;
2488
+ Ok ( ( ) )
2489
+ } else {
2490
+ Ok ( ( ) )
2491
+ }
2492
+ }
2470
2493
}
2471
2494
2472
2495
impl Default for Build {
0 commit comments