From b681164d85ff8e16d4eb93e1402ad3b7128f139f Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sun, 31 Jul 2022 04:48:43 +0900 Subject: [PATCH 1/2] Add -isysroot for xctoolchain cc --- src/lib.rs | 19 +++++++++++++++++-- tests/test.rs | 2 ++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index aa5eef4b7..1fb28f084 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2515,7 +2515,8 @@ impl Build { ArchSpec::Catalyst(_) => "macosx".to_owned(), }; - if !is_mac { + // AppleClang sometimes requires sysroot even for darwin + if cmd.is_xctoolchain_clang() || !target.ends_with("-darwin") { self.print(&format_args!("Detecting {:?} SDK path for {}", os, sdk)); let sdk_path = if let Some(sdkroot) = env::var_os("SDKROOT") { sdkroot @@ -2525,7 +2526,10 @@ impl Build { cmd.args.push("-isysroot".into()); cmd.args.push(sdk_path); - // TODO: Remove this once Apple stops accepting apps built with Xcode 13 + } + + // TODO: Remove this once Apple stops accepting apps built with Xcode 13 + if !is_mac { cmd.args.push("-fembed-bitcode".into()); } @@ -3718,6 +3722,17 @@ impl Tool { self.family == ToolFamily::Clang } + /// Whether the tool is AppleClang under .xctoolchain + #[cfg(target_vendor = "apple")] + fn is_xctoolchain_clang(&self) -> bool { + let path = self.path.to_string_lossy(); + path.contains(".xctoolchain/") + } + #[cfg(not(target_vendor = "apple"))] + fn is_xctoolchain_clang(&self) -> bool { + false + } + /// Whether the tool is MSVC-like. pub fn is_like_msvc(&self) -> bool { match self.family { diff --git a/tests/test.rs b/tests/test.rs index c85f25e69..a0c258efe 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -489,8 +489,10 @@ fn gnu_apple_darwin() { .file("foo.c") .compile("foo"); + let cmd = test.cmd(0); test.cmd(0) .must_have(format!("-mmacosx-version-min={}", version)); + cmd.must_not_have("-isysroot"); } } From 8fb6eb987e64e367c884bc66d1f10bdb284b55af Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sun, 31 Jul 2022 05:20:04 +0900 Subject: [PATCH 2/2] -isysroot priors to $SDKROOT --- src/lib.rs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1fb28f084..1759c051d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3386,19 +3386,6 @@ impl Build { let target = self.get_target()?; let host = self.get_host()?; if host.contains("apple-darwin") && target.contains("apple-darwin") { - // If, for example, `cargo` runs during the build of an XCode project, then `SDKROOT` environment variable - // would represent the current target, and this is the problem for us, if we want to compile something - // for the host, when host != target. - // We can not just remove `SDKROOT`, because, again, for example, XCode add to PATH - // /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin - // and `cc` from this path can not find system include files, like `pthread.h`, if `SDKROOT` - // is not set - if let Ok(sdkroot) = env::var("SDKROOT") { - if !sdkroot.contains("MacOSX") { - 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".