Skip to content

Commit 0bebedd

Browse files
committed
Document a bit more how the SDK version actually works
1 parent 612796c commit 0bebedd

File tree

1 file changed

+41
-7
lines changed
  • compiler/rustc_codegen_ssa/src/back

1 file changed

+41
-7
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+41-7
Original file line numberDiff line numberDiff line change
@@ -2959,11 +2959,12 @@ pub(crate) fn are_upstream_rust_objects_already_included(sess: &Session) -> bool
29592959
}
29602960
}
29612961

2962-
/// We need to communicate four things to the linker on Apple/Darwin targets:
2962+
/// We need to communicate five things to the linker on Apple/Darwin targets:
29632963
/// - The architecture.
29642964
/// - The operating system (and that it's an Apple platform).
2965-
/// - The deployment target.
29662965
/// - The environment / ABI.
2966+
/// - The deployment target.
2967+
/// - The SDK version.
29672968
fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
29682969
if !sess.target.is_like_osx {
29692970
return;
@@ -3039,7 +3040,38 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo
30393040
let (major, minor, patch) = current_apple_deployment_target(&sess.target);
30403041
let min_version = format!("{major}.{minor}.{patch}");
30413042

3042-
// Lie about the SDK version, we don't know it here
3043+
// The SDK version is used at runtime when compiling with a newer SDK / version of Xcode:
3044+
// - By dyld to give extra warnings and errors, see e.g.:
3045+
// <https://github.com/apple-oss-distributions/dyld/blob/dyld-1165.3/common/MachOFile.cpp#L3029>
3046+
// <https://github.com/apple-oss-distributions/dyld/blob/dyld-1165.3/common/MachOFile.cpp#L3738-L3857>
3047+
// - By system frameworks to change certain behaviour. For example, the default value of
3048+
// `-[NSView wantsBestResolutionOpenGLSurface]` is `YES` when the SDK version is >= 10.15.
3049+
// <https://developer.apple.com/documentation/appkit/nsview/1414938-wantsbestresolutionopenglsurface?language=objc>
3050+
//
3051+
// We do not currently know the actual SDK version though, so we have a few options:
3052+
// 1. Use the minimum version supported by rustc.
3053+
// 2. Use the same as the deployment target.
3054+
// 3. Use an arbitary recent version.
3055+
// 4. Omit the version.
3056+
//
3057+
// The first option is too low / too conservative, and means that users will not get the
3058+
// same behaviour from a binary compiled with rustc as with one compiled by clang.
3059+
//
3060+
// The second option is similarly conservative, and also wrong since if the user specified a
3061+
// higher deployment target than the SDK they're compiling/linking with, the runtime might
3062+
// make invalid assumptions about the capabilities of the binary.
3063+
//
3064+
// The third option requires that `rustc` is periodically kept up to date with Apple's SDK
3065+
// version, and is also wrong for similar reasons as above.
3066+
//
3067+
// The fourth option is bad because while `ld`, `otool`, `vtool` and such understand it to
3068+
// mean "absent" or `n/a`, dyld doesn't actually understand it, and will end up interpreting
3069+
// it as 0.0, which is again too low/conservative.
3070+
//
3071+
// Currently, we lie about the SDK version, and choose the second option.
3072+
//
3073+
// FIXME(madsmtm): Parse the SDK version from the SDK root instead.
3074+
// <https://github.com/rust-lang/rust/issues/129432>
30433075
let sdk_version = &*min_version;
30443076

30453077
// From the man page for ld64 (`man ld`):
@@ -3053,11 +3085,13 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo
30533085
cmd.link_args(&["-platform_version", platform_name, &*min_version, sdk_version]);
30543086
} else {
30553087
// cc == Cc::Yes
3088+
//
30563089
// We'd _like_ to use `-target` everywhere, since that can uniquely
3057-
// communicate all the required details, but that doesn't work on GCC,
3058-
// and since we don't know whether the `cc` compiler is Clang, GCC, or
3059-
// something else, we fall back to other options that also work on GCC
3060-
// when compiling for macOS.
3090+
// communicate all the required details except for the SDK version
3091+
// (which is read by Clang itself from the SDKROOT), but that doesn't
3092+
// work on GCC, and since we don't know whether the `cc` compiler is
3093+
// Clang, GCC, or something else, we fall back to other options that
3094+
// also work on GCC when compiling for macOS.
30613095
//
30623096
// Targets other than macOS are ill-supported by GCC (it doesn't even
30633097
// support e.g. `-miphoneos-version-min`), so in those cases we can

0 commit comments

Comments
 (0)