Skip to content

Commit 0fc8091

Browse files
authored
Fix xctoolchain AppleClang to corrrectly use -isysroot (#703)
* Add -isysroot for xctoolchain cc * -isysroot priors to $SDKROOT
1 parent a211d91 commit 0fc8091

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

src/lib.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -2515,7 +2515,8 @@ impl Build {
25152515
ArchSpec::Catalyst(_) => "macosx".to_owned(),
25162516
};
25172517

2518-
if !is_mac {
2518+
// AppleClang sometimes requires sysroot even for darwin
2519+
if cmd.is_xctoolchain_clang() || !target.ends_with("-darwin") {
25192520
self.print(&format_args!("Detecting {:?} SDK path for {}", os, sdk));
25202521
let sdk_path = if let Some(sdkroot) = env::var_os("SDKROOT") {
25212522
sdkroot
@@ -2525,7 +2526,10 @@ impl Build {
25252526

25262527
cmd.args.push("-isysroot".into());
25272528
cmd.args.push(sdk_path);
2528-
// TODO: Remove this once Apple stops accepting apps built with Xcode 13
2529+
}
2530+
2531+
// TODO: Remove this once Apple stops accepting apps built with Xcode 13
2532+
if !is_mac {
25292533
cmd.args.push("-fembed-bitcode".into());
25302534
}
25312535

@@ -3382,19 +3386,6 @@ impl Build {
33823386
let target = self.get_target()?;
33833387
let host = self.get_host()?;
33843388
if host.contains("apple-darwin") && target.contains("apple-darwin") {
3385-
// If, for example, `cargo` runs during the build of an XCode project, then `SDKROOT` environment variable
3386-
// would represent the current target, and this is the problem for us, if we want to compile something
3387-
// for the host, when host != target.
3388-
// We can not just remove `SDKROOT`, because, again, for example, XCode add to PATH
3389-
// /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
3390-
// and `cc` from this path can not find system include files, like `pthread.h`, if `SDKROOT`
3391-
// is not set
3392-
if let Ok(sdkroot) = env::var("SDKROOT") {
3393-
if !sdkroot.contains("MacOSX") {
3394-
let macos_sdk = self.apple_sdk_root("macosx")?;
3395-
cmd.env("SDKROOT", macos_sdk);
3396-
}
3397-
}
33983389
// Additionally, `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at
33993390
// "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld",
34003391
// although this is apparently ignored when using the linker at "/usr/bin/ld".
@@ -3718,6 +3709,17 @@ impl Tool {
37183709
self.family == ToolFamily::Clang
37193710
}
37203711

3712+
/// Whether the tool is AppleClang under .xctoolchain
3713+
#[cfg(target_vendor = "apple")]
3714+
fn is_xctoolchain_clang(&self) -> bool {
3715+
let path = self.path.to_string_lossy();
3716+
path.contains(".xctoolchain/")
3717+
}
3718+
#[cfg(not(target_vendor = "apple"))]
3719+
fn is_xctoolchain_clang(&self) -> bool {
3720+
false
3721+
}
3722+
37213723
/// Whether the tool is MSVC-like.
37223724
pub fn is_like_msvc(&self) -> bool {
37233725
match self.family {

tests/test.rs

+2
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,10 @@ fn gnu_apple_darwin() {
489489
.file("foo.c")
490490
.compile("foo");
491491

492+
let cmd = test.cmd(0);
492493
test.cmd(0)
493494
.must_have(format!("-mmacosx-version-min={}", version));
495+
cmd.must_not_have("-isysroot");
494496
}
495497
}
496498

0 commit comments

Comments
 (0)