about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-02-20 00:04:03 +0000
committerbors <bors@rust-lang.org>2024-02-20 00:04:03 +0000
commit7d6139494833d478aeb568966a028a68dfa01e73 (patch)
treefe76745066f04facb050877b6ce6a52e1c7263a2 /compiler
parent0395fa387ad4de63ea1b36703ca0d65017b8c2dc (diff)
parenta3cf493642e770783d2bd89deb8afe90af45525b (diff)
downloadrust-7d6139494833d478aeb568966a028a68dfa01e73.tar.gz
rust-7d6139494833d478aeb568966a028a68dfa01e73.zip
Auto merge of #121296 - madsmtm:fix-mac-catalyst-deployment-target, r=shepmaster
Fix `IPHONEOS_DEPLOYMENT_TARGET` on Mac Catalyst

Some of the target code invalidly assumed that the deployment target variable on Mac Catalyst is `MACOSX_DEPLOYMENT_TARGET`, which is wrong, Mac Catalyst uses the same environment variable as iOS.

Additionally, the deployment target was hardcoded to `14.0`, I've lowered this to `13.1` ([same default as Clang](https://github.com/llvm/llvm-project/blob/d022f32c73c57b59a9121eba909f5034e89c628e/clang/lib/Driver/ToolChains/Darwin.cpp#L2038)), and made it properly load from the environment.

This shouldn't require any changes to the `cc` crate, as that uses `rustc --print=deployment-target` to get this information automatically.

CC `@BlackHoleFox`
r? `@rust-lang/macos`
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_target/src/spec/base/apple/mod.rs107
-rw-r--r--compiler/rustc_target/src/spec/targets/aarch64_apple_ios_macabi.rs9
-rw-r--r--compiler/rustc_target/src/spec/targets/x86_64_apple_ios_macabi.rs9
3 files changed, 53 insertions, 72 deletions
diff --git a/compiler/rustc_target/src/spec/base/apple/mod.rs b/compiler/rustc_target/src/spec/base/apple/mod.rs
index aee5d60626e..dd75377ead2 100644
--- a/compiler/rustc_target/src/spec/base/apple/mod.rs
+++ b/compiler/rustc_target/src/spec/base/apple/mod.rs
@@ -97,28 +97,39 @@ fn pre_link_args(os: &'static str, arch: Arch, abi: &'static str) -> LinkArgs {
         _ => os.into(),
     };
 
-    let platform_version: StaticCow<str> = match os {
-        "ios" => ios_lld_platform_version(arch),
-        "tvos" => tvos_lld_platform_version(),
-        "watchos" => watchos_lld_platform_version(),
-        "macos" => macos_lld_platform_version(arch),
-        _ => unreachable!(),
-    }
-    .into();
-
-    let arch = arch.target_name();
+    let min_version: StaticCow<str> = {
+        let (major, minor) = match os {
+            "ios" => ios_deployment_target(arch, abi),
+            "tvos" => tvos_deployment_target(),
+            "watchos" => watchos_deployment_target(),
+            "macos" => macos_deployment_target(arch),
+            _ => unreachable!(),
+        };
+        format!("{major}.{minor}").into()
+    };
+    let sdk_version = min_version.clone();
 
     let mut args = TargetOptions::link_args(
         LinkerFlavor::Darwin(Cc::No, Lld::No),
-        &["-arch", arch, "-platform_version"],
+        &["-arch", arch.target_name(), "-platform_version"],
     );
     add_link_args_iter(
         &mut args,
         LinkerFlavor::Darwin(Cc::No, Lld::No),
-        [platform_name, platform_version.clone(), platform_version].into_iter(),
+        [platform_name, min_version, sdk_version].into_iter(),
     );
     if abi != "macabi" {
-        add_link_args(&mut args, LinkerFlavor::Darwin(Cc::Yes, Lld::No), &["-arch", arch]);
+        add_link_args(
+            &mut args,
+            LinkerFlavor::Darwin(Cc::Yes, Lld::No),
+            &["-arch", arch.target_name()],
+        );
+    } else {
+        add_link_args_iter(
+            &mut args,
+            LinkerFlavor::Darwin(Cc::Yes, Lld::No),
+            ["-target".into(), mac_catalyst_llvm_target(arch).into()].into_iter(),
+        );
     }
 
     args
@@ -131,7 +142,7 @@ pub fn opts(os: &'static str, arch: Arch) -> TargetOptions {
         abi: abi.into(),
         os: os.into(),
         cpu: arch.target_cpu().into(),
-        link_env_remove: link_env_remove(arch, os),
+        link_env_remove: link_env_remove(os),
         vendor: "apple".into(),
         linker_flavor: LinkerFlavor::Darwin(Cc::Yes, Lld::No),
         // macOS has -dead_strip, which doesn't rely on function_sections
@@ -220,16 +231,13 @@ pub fn deployment_target(target: &Target) -> Option<(u32, u32)> {
             };
             macos_deployment_target(arch)
         }
-        "ios" => match &*target.abi {
-            "macabi" => mac_catalyst_deployment_target(),
-            _ => {
-                let arch = match target.arch.as_ref() {
-                    "arm64e" => Arm64e,
-                    _ => Arm64,
-                };
-                ios_deployment_target(arch)
-            }
-        },
+        "ios" => {
+            let arch = match target.arch.as_ref() {
+                "arm64e" => Arm64e,
+                _ => Arm64,
+            };
+            ios_deployment_target(arch, &target.abi)
+        }
         "watchos" => watchos_deployment_target(),
         "tvos" => tvos_deployment_target(),
         _ => return None,
@@ -260,17 +268,12 @@ fn macos_deployment_target(arch: Arch) -> (u32, u32) {
         .unwrap_or_else(|| macos_default_deployment_target(arch))
 }
 
-fn macos_lld_platform_version(arch: Arch) -> String {
-    let (major, minor) = macos_deployment_target(arch);
-    format!("{major}.{minor}")
-}
-
 pub fn macos_llvm_target(arch: Arch) -> String {
     let (major, minor) = macos_deployment_target(arch);
     format!("{}-apple-macosx{}.{}.0", arch.target_name(), major, minor)
 }
 
-fn link_env_remove(arch: Arch, os: &'static str) -> StaticCow<[StaticCow<str>]> {
+fn link_env_remove(os: &'static str) -> StaticCow<[StaticCow<str>]> {
     // Apple platforms only officially support macOS as a host for any compilation.
     //
     // If building for macOS, we go ahead and remove any erroneous environment state
@@ -298,29 +301,23 @@ fn link_env_remove(arch: Arch, os: &'static str) -> StaticCow<[StaticCow<str>]>
         env_remove.push("TVOS_DEPLOYMENT_TARGET".into());
         env_remove.into()
     } else {
-        // Otherwise if cross-compiling for a different OS/SDK, remove any part
+        // Otherwise if cross-compiling for a different OS/SDK (including Mac Catalyst), remove any part
         // of the linking environment that's wrong and reversed.
-        match arch {
-            Armv7k | Armv7s | Arm64 | Arm64e | Arm64_32 | I386 | I386_sim | I686 | X86_64
-            | X86_64_sim | X86_64h | Arm64_sim => {
-                cvs!["MACOSX_DEPLOYMENT_TARGET"]
-            }
-            X86_64_macabi | Arm64_macabi => cvs!["IPHONEOS_DEPLOYMENT_TARGET"],
-        }
+        cvs!["MACOSX_DEPLOYMENT_TARGET"]
     }
 }
 
-fn ios_deployment_target(arch: Arch) -> (u32, u32) {
+fn ios_deployment_target(arch: Arch, abi: &str) -> (u32, u32) {
     // If you are looking for the default deployment target, prefer `rustc --print deployment-target`.
-    let (major, minor) = if arch == Arm64e { (14, 0) } else { (10, 0) };
+    let (major, minor) = match (arch, abi) {
+        (Arm64e, _) => (14, 0),
+        // Mac Catalyst defaults to 13.1 in Clang.
+        (_, "macabi") => (13, 1),
+        _ => (10, 0),
+    };
     from_set_deployment_target("IPHONEOS_DEPLOYMENT_TARGET").unwrap_or((major, minor))
 }
 
-fn mac_catalyst_deployment_target() -> (u32, u32) {
-    // If you are looking for the default deployment target, prefer `rustc --print deployment-target`.
-    from_set_deployment_target("IPHONEOS_DEPLOYMENT_TARGET").unwrap_or((14, 0))
-}
-
 pub fn ios_llvm_target(arch: Arch) -> String {
     // Modern iOS tooling extracts information about deployment target
     // from LC_BUILD_VERSION. This load command will only be emitted when
@@ -328,17 +325,17 @@ pub fn ios_llvm_target(arch: Arch) -> String {
     // set high enough. Luckily one LC_BUILD_VERSION is enough, for Xcode
     // to pick it up (since std and core are still built with the fallback
     // of version 7.0 and hence emit the old LC_IPHONE_MIN_VERSION).
-    let (major, minor) = ios_deployment_target(arch);
+    let (major, minor) = ios_deployment_target(arch, "");
     format!("{}-apple-ios{}.{}.0", arch.target_name(), major, minor)
 }
 
-fn ios_lld_platform_version(arch: Arch) -> String {
-    let (major, minor) = ios_deployment_target(arch);
-    format!("{major}.{minor}")
+pub fn mac_catalyst_llvm_target(arch: Arch) -> String {
+    let (major, minor) = ios_deployment_target(arch, "macabi");
+    format!("{}-apple-ios{}.{}.0-macabi", arch.target_name(), major, minor)
 }
 
 pub fn ios_sim_llvm_target(arch: Arch) -> String {
-    let (major, minor) = ios_deployment_target(arch);
+    let (major, minor) = ios_deployment_target(arch, "sim");
     format!("{}-apple-ios{}.{}.0-simulator", arch.target_name(), major, minor)
 }
 
@@ -347,11 +344,6 @@ fn tvos_deployment_target() -> (u32, u32) {
     from_set_deployment_target("TVOS_DEPLOYMENT_TARGET").unwrap_or((10, 0))
 }
 
-fn tvos_lld_platform_version() -> String {
-    let (major, minor) = tvos_deployment_target();
-    format!("{major}.{minor}")
-}
-
 pub fn tvos_llvm_target(arch: Arch) -> String {
     let (major, minor) = tvos_deployment_target();
     format!("{}-apple-tvos{}.{}.0", arch.target_name(), major, minor)
@@ -367,11 +359,6 @@ fn watchos_deployment_target() -> (u32, u32) {
     from_set_deployment_target("WATCHOS_DEPLOYMENT_TARGET").unwrap_or((5, 0))
 }
 
-fn watchos_lld_platform_version() -> String {
-    let (major, minor) = watchos_deployment_target();
-    format!("{major}.{minor}")
-}
-
 pub fn watchos_sim_llvm_target(arch: Arch) -> String {
     let (major, minor) = watchos_deployment_target();
     format!("{}-apple-watchos{}.{}.0-simulator", arch.target_name(), major, minor)
diff --git a/compiler/rustc_target/src/spec/targets/aarch64_apple_ios_macabi.rs b/compiler/rustc_target/src/spec/targets/aarch64_apple_ios_macabi.rs
index 78067a138a9..300e3014079 100644
--- a/compiler/rustc_target/src/spec/targets/aarch64_apple_ios_macabi.rs
+++ b/compiler/rustc_target/src/spec/targets/aarch64_apple_ios_macabi.rs
@@ -1,16 +1,13 @@
-use crate::spec::base::apple::{opts, Arch};
-use crate::spec::{Cc, FramePointer, LinkerFlavor, Lld, SanitizerSet, Target, TargetOptions};
+use crate::spec::base::apple::{mac_catalyst_llvm_target, opts, Arch};
+use crate::spec::{FramePointer, SanitizerSet, Target, TargetOptions};
 
 pub fn target() -> Target {
-    let llvm_target = "arm64-apple-ios14.0-macabi";
-
     let arch = Arch::Arm64_macabi;
     let mut base = opts("ios", arch);
-    base.add_pre_link_args(LinkerFlavor::Darwin(Cc::Yes, Lld::No), &["-target", llvm_target]);
     base.supported_sanitizers = SanitizerSet::ADDRESS | SanitizerSet::LEAK | SanitizerSet::THREAD;
 
     Target {
-        llvm_target: llvm_target.into(),
+        llvm_target: mac_catalyst_llvm_target(arch).into(),
         pointer_width: 64,
         data_layout: "e-m:o-i64:64-i128:128-n32:64-S128".into(),
         arch: arch.target_arch(),
diff --git a/compiler/rustc_target/src/spec/targets/x86_64_apple_ios_macabi.rs b/compiler/rustc_target/src/spec/targets/x86_64_apple_ios_macabi.rs
index ff21e489333..e59f41185de 100644
--- a/compiler/rustc_target/src/spec/targets/x86_64_apple_ios_macabi.rs
+++ b/compiler/rustc_target/src/spec/targets/x86_64_apple_ios_macabi.rs
@@ -1,16 +1,13 @@
-use crate::spec::base::apple::{opts, Arch};
-use crate::spec::{Cc, LinkerFlavor, Lld, SanitizerSet, Target, TargetOptions};
+use crate::spec::base::apple::{mac_catalyst_llvm_target, opts, Arch};
+use crate::spec::{SanitizerSet, Target, TargetOptions};
 
 pub fn target() -> Target {
-    let llvm_target = "x86_64-apple-ios14.0-macabi";
-
     let arch = Arch::X86_64_macabi;
     let mut base = opts("ios", arch);
-    base.add_pre_link_args(LinkerFlavor::Darwin(Cc::Yes, Lld::No), &["-target", llvm_target]);
     base.supported_sanitizers = SanitizerSet::ADDRESS | SanitizerSet::LEAK | SanitizerSet::THREAD;
 
     Target {
-        llvm_target: llvm_target.into(),
+        llvm_target: mac_catalyst_llvm_target(arch).into(),
         pointer_width: 64,
         data_layout:
             "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128".into(),