Skip to content

Commit ffa88dd

Browse files
committed
filter SDKROOT for macos->macos build
fixes #530
1 parent 369eeac commit ffa88dd

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,7 @@ impl Build {
894894
///
895895
/// This will return a result instead of panicing; see compile() for the complete description.
896896
pub fn try_compile(&self, output: &str) -> Result<(), Error> {
897+
self.fix_env_variabes()?;
897898
let (lib_name, gnu_lib_name) = if output.starts_with("lib") && output.ends_with(".a") {
898899
(&output[3..output.len() - 2], output.to_owned())
899900
} else {
@@ -2467,6 +2468,28 @@ impl Build {
24672468
println!("{}", s);
24682469
}
24692470
}
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+
}
24702493
}
24712494

24722495
impl Default for Build {

0 commit comments

Comments
 (0)