Skip to content

Commit 4f578aa

Browse files
committed
handle wrong SDKROOT for macos->macos build
fixes #530
1 parent 369eeac commit 4f578aa

File tree

1 file changed

+51
-20
lines changed

1 file changed

+51
-20
lines changed

src/lib.rs

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,7 @@ impl Build {
11721172
cmd.arg("-c");
11731173
}
11741174
cmd.arg(&obj.src);
1175+
self.fix_env_for_apple_os(&mut cmd)?;
11751176

11761177
run(&mut cmd, &name)?;
11771178
Ok(())
@@ -1887,27 +1888,9 @@ impl Build {
18871888
};
18881889

18891890
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)?;
19091892
cmd.args.push("-isysroot".into());
1910-
cmd.args.push(sdk_path.trim().into());
1893+
cmd.args.push(sdk_path);
19111894
cmd.args.push("-fembed-bitcode".into());
19121895
/*
19131896
* TODO we probably ultimately want the -fembed-bitcode-marker flag
@@ -2467,6 +2450,54 @@ impl Build {
24672450
println!("{}", s);
24682451
}
24692452
}
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+
}
24702501
}
24712502

24722503
impl Default for Build {

0 commit comments

Comments
 (0)