Skip to content

Commit

Permalink
handle wrong SDKROOT for macos->macos build
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushistov committed Aug 5, 2020
1 parent 369eeac commit 4f578aa
Showing 1 changed file with 51 additions and 20 deletions.
71 changes: 51 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,7 @@ impl Build {
cmd.arg("-c");
}
cmd.arg(&obj.src);
self.fix_env_for_apple_os(&mut cmd)?;

run(&mut cmd, &name)?;
Ok(())
Expand Down Expand Up @@ -1887,27 +1888,9 @@ impl Build {
};

self.print(&format!("Detecting iOS SDK path for {}", sdk));
let sdk_path = self
.cmd("xcrun")
.arg("--show-sdk-path")
.arg("--sdk")
.arg(sdk)
.stderr(Stdio::inherit())
.output()?
.stdout;

let sdk_path = match String::from_utf8(sdk_path) {
Ok(p) => p,
Err(_) => {
return Err(Error::new(
ErrorKind::IOError,
"Unable to determine iOS SDK path.",
));
}
};

let sdk_path = self.apple_sdk_root(sdk)?;
cmd.args.push("-isysroot".into());
cmd.args.push(sdk_path.trim().into());
cmd.args.push(sdk_path);
cmd.args.push("-fembed-bitcode".into());
/*
* TODO we probably ultimately want the -fembed-bitcode-marker flag
Expand Down Expand Up @@ -2467,6 +2450,54 @@ impl Build {
println!("{}", s);
}
}

#[cfg(target_os = "macos")]
fn fix_env_for_apple_os(&self, cmd: &mut Command) -> Result<(), Error> {
let target = self.get_target()?;
let host = self.get_host()?;
if host.contains("apple-darwin") && target.contains("apple-darwin") {
// Replace the `SDKROOT` environment variable if it's clearly set for the wrong platform, which
// may occur when we're linking a custom build script while targeting iOS for example.
// Removing is not enough, because of cc from XCode can not find include files in such case
if let Ok(sdkroot) = env::var("SDKROOT") {
if sdkroot.contains("iPhone") {
let macos_sdk = self.apple_sdk_root("macosx")?;
cmd.env("SDKROOT", macos_sdk);
}
}
// Additionally, `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at
// "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld",
// although this is apparently ignored when using the linker at "/usr/bin/ld".
cmd.env_remove("IPHONEOS_DEPLOYMENT_TARGET");
}
Ok(())
}
#[cfg(not(target_os = "macos"))]
fn fix_env_for_apple_os(&self, _cmd: &mut Command) -> Result<(), Error> {
Ok(())
}

fn apple_sdk_root(&self, sdk: &str) -> Result<OsString, Error> {
let sdk_path = self
.cmd("xcrun")
.arg("--show-sdk-path")
.arg("--sdk")
.arg(sdk)
.stderr(Stdio::inherit())
.output()?
.stdout;

let sdk_path = match String::from_utf8(sdk_path) {
Ok(p) => p,
Err(_) => {
return Err(Error::new(
ErrorKind::IOError,
"Unable to determine iOS SDK path.",
));
}
};
Ok(sdk_path.trim().into())
}
}

impl Default for Build {
Expand Down

0 comments on commit 4f578aa

Please sign in to comment.