diff --git a/src/lib.rs b/src/lib.rs index 3d3cff487..f10d39b71 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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(()) @@ -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 @@ -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 { + 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 {