Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix xctoolchain AppleClang to corrrectly use -isysroot #703

Merged
merged 2 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why this is conditional now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole function apple_flags - which was previously ios_watchos_flags was not called from macos target before. I'd like to keep the previous behavior for macos target in this PR. If you'd like to add this to macos too, I will remove it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good catch.

cmd.args.push("-fembed-bitcode".into());
}

Expand Down Expand Up @@ -3382,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".
Expand Down Expand Up @@ -3718,6 +3709,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 {
Expand Down
2 changes: 2 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down