@@ -1172,6 +1172,7 @@ impl Build {
1172
1172
cmd. arg ( "-c" ) ;
1173
1173
}
1174
1174
cmd. arg ( & obj. src ) ;
1175
+ self . fix_env_for_apple_os ( & mut cmd) ?;
1175
1176
1176
1177
run ( & mut cmd, & name) ?;
1177
1178
Ok ( ( ) )
@@ -1887,27 +1888,9 @@ impl Build {
1887
1888
} ;
1888
1889
1889
1890
self . print ( & format ! ( "Detecting iOS SDK path for {}" , sdk) ) ;
1890
- let sdk_path = self
1891
- . cmd ( "xcrun" )
1892
- . arg ( "--show-sdk-path" )
1893
- . arg ( "--sdk" )
1894
- . arg ( sdk)
1895
- . stderr ( Stdio :: inherit ( ) )
1896
- . output ( ) ?
1897
- . stdout ;
1898
-
1899
- let sdk_path = match String :: from_utf8 ( sdk_path) {
1900
- Ok ( p) => p,
1901
- Err ( _) => {
1902
- return Err ( Error :: new (
1903
- ErrorKind :: IOError ,
1904
- "Unable to determine iOS SDK path." ,
1905
- ) ) ;
1906
- }
1907
- } ;
1908
-
1891
+ let sdk_path = self . apple_sdk_root ( sdk) ?;
1909
1892
cmd. args . push ( "-isysroot" . into ( ) ) ;
1910
- cmd. args . push ( sdk_path. trim ( ) . into ( ) ) ;
1893
+ cmd. args . push ( sdk_path) ;
1911
1894
cmd. args . push ( "-fembed-bitcode" . into ( ) ) ;
1912
1895
/*
1913
1896
* TODO we probably ultimately want the -fembed-bitcode-marker flag
@@ -2467,6 +2450,54 @@ impl Build {
2467
2450
println ! ( "{}" , s) ;
2468
2451
}
2469
2452
}
2453
+
2454
+ #[ cfg( target_os = "macos" ) ]
2455
+ fn fix_env_for_apple_os ( & self , cmd : & mut Command ) -> Result < ( ) , Error > {
2456
+ let target = self . get_target ( ) ?;
2457
+ let host = self . get_host ( ) ?;
2458
+ if host. contains ( "apple-darwin" ) && target. contains ( "apple-darwin" ) {
2459
+ // Replace the `SDKROOT` environment variable if it's clearly set for the wrong platform, which
2460
+ // may occur when we're linking a custom build script while targeting iOS for example.
2461
+ // Removing is not enough, because of cc from XCode can not find include files in such case
2462
+ if let Ok ( sdkroot) = env:: var ( "SDKROOT" ) {
2463
+ if sdkroot. contains ( "iPhone" ) {
2464
+ let macos_sdk = self . apple_sdk_root ( "macosx" ) ?;
2465
+ cmd. env ( "SDKROOT" , macos_sdk) ;
2466
+ }
2467
+ }
2468
+ // Additionally, `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at
2469
+ // "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld",
2470
+ // although this is apparently ignored when using the linker at "/usr/bin/ld".
2471
+ cmd. env_remove ( "IPHONEOS_DEPLOYMENT_TARGET" ) ;
2472
+ }
2473
+ Ok ( ( ) )
2474
+ }
2475
+ #[ cfg( not( target_os = "macos" ) ) ]
2476
+ fn fix_env_for_apple_os ( & self , _cmd : & mut Command ) -> Result < ( ) , Error > {
2477
+ Ok ( ( ) )
2478
+ }
2479
+
2480
+ fn apple_sdk_root ( & self , sdk : & str ) -> Result < OsString , Error > {
2481
+ let sdk_path = self
2482
+ . cmd ( "xcrun" )
2483
+ . arg ( "--show-sdk-path" )
2484
+ . arg ( "--sdk" )
2485
+ . arg ( sdk)
2486
+ . stderr ( Stdio :: inherit ( ) )
2487
+ . output ( ) ?
2488
+ . stdout ;
2489
+
2490
+ let sdk_path = match String :: from_utf8 ( sdk_path) {
2491
+ Ok ( p) => p,
2492
+ Err ( _) => {
2493
+ return Err ( Error :: new (
2494
+ ErrorKind :: IOError ,
2495
+ "Unable to determine iOS SDK path." ,
2496
+ ) ) ;
2497
+ }
2498
+ } ;
2499
+ Ok ( sdk_path. trim ( ) . into ( ) )
2500
+ }
2470
2501
}
2471
2502
2472
2503
impl Default for Build {
0 commit comments