Skip to content

Commit e6479fc

Browse files
committed
Always attempt to get SDK root, and pass it to Clang via env var
The exact reasoning why we do not always pass the SDK root with `-isysroot` to `cc` when linking on macOS eludes me (the git history dead ends in rust-lang#100286), but I suspect it's because we want to support `cc`s which do not support this option. So instead, we pass the SDK root via the environment variable SDKROOT. This way, compiler drivers that support setting the SDK root (such as Clang and GCC) can use it, while compiler drivers that don't (presumably because they figure out the SDK root in some other way) can just ignore it. This fixes rust-lang#80817 (by always passing the SDK root, even when linking with cc on macOS).
1 parent 8f42605 commit e6479fc

File tree

2 files changed

+43
-49
lines changed
  • compiler
    • rustc_codegen_ssa/src/back
    • rustc_target/src/spec/base/apple

2 files changed

+43
-49
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+40-27
Original file line numberDiff line numberDiff line change
@@ -3101,47 +3101,60 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo
31013101
}
31023102

31033103
fn add_apple_sdk(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) -> Option<PathBuf> {
3104-
let os = &sess.target.os;
3105-
if sess.target.vendor != "apple"
3106-
|| !matches!(os.as_ref(), "ios" | "tvos" | "watchos" | "visionos" | "macos")
3107-
|| !matches!(flavor, LinkerFlavor::Darwin(..))
3108-
{
3104+
if !sess.target.is_like_osx {
31093105
return None;
31103106
}
3111-
3112-
if os == "macos" && !matches!(flavor, LinkerFlavor::Darwin(Cc::No, _)) {
3107+
let LinkerFlavor::Darwin(cc, _) = flavor else {
31133108
return None;
3114-
}
3109+
};
31153110

31163111
let sdk_name = rustc_target::spec::apple_sdk_name(&sess.target);
31173112

3118-
let sdk_root = match get_apple_sdk_root(sdk_name) {
3113+
let sdkroot = match get_apple_sdk_root(sdk_name) {
31193114
Ok(s) => s,
31203115
Err(e) => {
3121-
sess.dcx().emit_err(e);
3116+
// If cross compiling from non-macOS, the user might be using something like `zig cc`.
3117+
//
3118+
// In that case, we shouldn't error when the SDK is missing, though we still warn.
3119+
if cfg!(target_os = "macos") {
3120+
sess.dcx().emit_err(e);
3121+
} else {
3122+
sess.dcx().emit_warn(e);
3123+
}
31223124
return None;
31233125
}
31243126
};
31253127

3126-
match flavor {
3127-
LinkerFlavor::Darwin(Cc::Yes, _) => {
3128-
// Use `-isysroot` instead of `--sysroot`, as only the former
3129-
// makes Clang treat it as a platform SDK.
3130-
//
3131-
// This is admittedly a bit strange, as on most targets
3132-
// `-isysroot` only applies to include header files, but on Apple
3133-
// targets this also applies to libraries and frameworks.
3134-
cmd.cc_arg("-isysroot");
3135-
cmd.cc_arg(&sdk_root);
3136-
}
3137-
LinkerFlavor::Darwin(Cc::No, _) => {
3138-
cmd.link_arg("-syslibroot");
3139-
cmd.link_arg(&sdk_root);
3140-
}
3141-
_ => unreachable!(),
3128+
if cc == Cc::Yes {
3129+
// To pass the SDK root to `cc`, we have a few options:
3130+
// 1. `--sysroot` flag.
3131+
// 2. `-isysroot` flag.
3132+
// 3. `SDKROOT` environment variable.
3133+
//
3134+
// `--sysroot` isn't actually enough to get Clang to treat it as a platform SDK, you need to
3135+
// specify `-isysroot` - this is admittedly a bit strange, as on most targets `-isysroot`
3136+
// only applies to include header files, but on Apple targets it also applies to libraries
3137+
// and frameworks.
3138+
//
3139+
// Now, while the `-isysroot` flag is pretty well supported (both Clang and GCC implements
3140+
// the desired behaviour), it may not be understood by any `cc`'s that the user might want
3141+
// to use.
3142+
//
3143+
// So to better support such use-cases, we pass the SDK root in the standard environment
3144+
// variable instead. This is also supported by GCC since 2019:
3145+
// <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87243>
3146+
//
3147+
// This also works better with the trampoline `/usr/bin/cc` which calls `xcrun cc`
3148+
// internally, since the presence of `SDKROOT` means it won't have to look up the SDK root
3149+
// itself.
3150+
cmd.cmd().env("SDKROOT", &sdkroot);
3151+
} else {
3152+
// For `ld64`, we use the `-syslibroot` parameter (and there are no other options).
3153+
cmd.link_arg("-syslibroot");
3154+
cmd.link_arg(&sdkroot);
31423155
}
31433156

3144-
Some(sdk_root)
3157+
Some(sdkroot)
31453158
}
31463159

31473160
fn get_apple_sdk_root(sdk_name: &'static str) -> Result<PathBuf, errors::AppleSdkError> {

compiler/rustc_target/src/spec/base/apple/mod.rs

+3-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::borrow::Cow;
2+
use std::fmt;
23
use std::num::ParseIntError;
34
use std::str::FromStr;
4-
use std::{env, fmt};
55

66
use serde::de;
77

@@ -279,29 +279,10 @@ fn link_env_remove(os: &'static str) -> StaticCow<[StaticCow<str>]> {
279279
// that's only applicable to cross-OS compilation. Always leave anything for the
280280
// host OS alone though.
281281
if os == "macos" {
282-
let mut env_remove = Vec::with_capacity(2);
283-
// Remove the `SDKROOT` environment variable if it's clearly set for the wrong platform, which
284-
// may occur when we're linking a custom build script while targeting iOS for example.
285-
if let Ok(sdkroot) = env::var("SDKROOT") {
286-
if sdkroot.contains("iPhoneOS.platform")
287-
|| sdkroot.contains("iPhoneSimulator.platform")
288-
|| sdkroot.contains("AppleTVOS.platform")
289-
|| sdkroot.contains("AppleTVSimulator.platform")
290-
|| sdkroot.contains("WatchOS.platform")
291-
|| sdkroot.contains("WatchSimulator.platform")
292-
|| sdkroot.contains("XROS.platform")
293-
|| sdkroot.contains("XRSimulator.platform")
294-
{
295-
env_remove.push("SDKROOT".into())
296-
}
297-
}
298-
// Additionally, `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at
282+
// `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at
299283
// "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld",
300284
// although this is apparently ignored when using the linker at "/usr/bin/ld".
301-
env_remove.push("IPHONEOS_DEPLOYMENT_TARGET".into());
302-
env_remove.push("TVOS_DEPLOYMENT_TARGET".into());
303-
env_remove.push("XROS_DEPLOYMENT_TARGET".into());
304-
env_remove.into()
285+
cvs!["IPHONEOS_DEPLOYMENT_TARGET", "TVOS_DEPLOYMENT_TARGET", "XROS_DEPLOYMENT_TARGET"]
305286
} else {
306287
// Otherwise if cross-compiling for a different OS/SDK (including Mac Catalyst), remove any part
307288
// of the linking environment that's wrong and reversed.

0 commit comments

Comments
 (0)