Skip to content

Commit 7d61394

Browse files
committed
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`
2 parents 0395fa3 + a3cf493 commit 7d61394

File tree

3 files changed

+53
-72
lines changed

3 files changed

+53
-72
lines changed

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

+47-60
Original file line numberDiff line numberDiff line change
@@ -97,28 +97,39 @@ fn pre_link_args(os: &'static str, arch: Arch, abi: &'static str) -> LinkArgs {
9797
_ => os.into(),
9898
};
9999

100-
let platform_version: StaticCow<str> = match os {
101-
"ios" => ios_lld_platform_version(arch),
102-
"tvos" => tvos_lld_platform_version(),
103-
"watchos" => watchos_lld_platform_version(),
104-
"macos" => macos_lld_platform_version(arch),
105-
_ => unreachable!(),
106-
}
107-
.into();
108-
109-
let arch = arch.target_name();
100+
let min_version: StaticCow<str> = {
101+
let (major, minor) = match os {
102+
"ios" => ios_deployment_target(arch, abi),
103+
"tvos" => tvos_deployment_target(),
104+
"watchos" => watchos_deployment_target(),
105+
"macos" => macos_deployment_target(arch),
106+
_ => unreachable!(),
107+
};
108+
format!("{major}.{minor}").into()
109+
};
110+
let sdk_version = min_version.clone();
110111

111112
let mut args = TargetOptions::link_args(
112113
LinkerFlavor::Darwin(Cc::No, Lld::No),
113-
&["-arch", arch, "-platform_version"],
114+
&["-arch", arch.target_name(), "-platform_version"],
114115
);
115116
add_link_args_iter(
116117
&mut args,
117118
LinkerFlavor::Darwin(Cc::No, Lld::No),
118-
[platform_name, platform_version.clone(), platform_version].into_iter(),
119+
[platform_name, min_version, sdk_version].into_iter(),
119120
);
120121
if abi != "macabi" {
121-
add_link_args(&mut args, LinkerFlavor::Darwin(Cc::Yes, Lld::No), &["-arch", arch]);
122+
add_link_args(
123+
&mut args,
124+
LinkerFlavor::Darwin(Cc::Yes, Lld::No),
125+
&["-arch", arch.target_name()],
126+
);
127+
} else {
128+
add_link_args_iter(
129+
&mut args,
130+
LinkerFlavor::Darwin(Cc::Yes, Lld::No),
131+
["-target".into(), mac_catalyst_llvm_target(arch).into()].into_iter(),
132+
);
122133
}
123134

124135
args
@@ -131,7 +142,7 @@ pub fn opts(os: &'static str, arch: Arch) -> TargetOptions {
131142
abi: abi.into(),
132143
os: os.into(),
133144
cpu: arch.target_cpu().into(),
134-
link_env_remove: link_env_remove(arch, os),
145+
link_env_remove: link_env_remove(os),
135146
vendor: "apple".into(),
136147
linker_flavor: LinkerFlavor::Darwin(Cc::Yes, Lld::No),
137148
// macOS has -dead_strip, which doesn't rely on function_sections
@@ -220,16 +231,13 @@ pub fn deployment_target(target: &Target) -> Option<(u32, u32)> {
220231
};
221232
macos_deployment_target(arch)
222233
}
223-
"ios" => match &*target.abi {
224-
"macabi" => mac_catalyst_deployment_target(),
225-
_ => {
226-
let arch = match target.arch.as_ref() {
227-
"arm64e" => Arm64e,
228-
_ => Arm64,
229-
};
230-
ios_deployment_target(arch)
231-
}
232-
},
234+
"ios" => {
235+
let arch = match target.arch.as_ref() {
236+
"arm64e" => Arm64e,
237+
_ => Arm64,
238+
};
239+
ios_deployment_target(arch, &target.abi)
240+
}
233241
"watchos" => watchos_deployment_target(),
234242
"tvos" => tvos_deployment_target(),
235243
_ => return None,
@@ -260,17 +268,12 @@ fn macos_deployment_target(arch: Arch) -> (u32, u32) {
260268
.unwrap_or_else(|| macos_default_deployment_target(arch))
261269
}
262270

263-
fn macos_lld_platform_version(arch: Arch) -> String {
264-
let (major, minor) = macos_deployment_target(arch);
265-
format!("{major}.{minor}")
266-
}
267-
268271
pub fn macos_llvm_target(arch: Arch) -> String {
269272
let (major, minor) = macos_deployment_target(arch);
270273
format!("{}-apple-macosx{}.{}.0", arch.target_name(), major, minor)
271274
}
272275

273-
fn link_env_remove(arch: Arch, os: &'static str) -> StaticCow<[StaticCow<str>]> {
276+
fn link_env_remove(os: &'static str) -> StaticCow<[StaticCow<str>]> {
274277
// Apple platforms only officially support macOS as a host for any compilation.
275278
//
276279
// If building for macOS, we go ahead and remove any erroneous environment state
@@ -298,47 +301,41 @@ fn link_env_remove(arch: Arch, os: &'static str) -> StaticCow<[StaticCow<str>]>
298301
env_remove.push("TVOS_DEPLOYMENT_TARGET".into());
299302
env_remove.into()
300303
} else {
301-
// Otherwise if cross-compiling for a different OS/SDK, remove any part
304+
// Otherwise if cross-compiling for a different OS/SDK (including Mac Catalyst), remove any part
302305
// of the linking environment that's wrong and reversed.
303-
match arch {
304-
Armv7k | Armv7s | Arm64 | Arm64e | Arm64_32 | I386 | I386_sim | I686 | X86_64
305-
| X86_64_sim | X86_64h | Arm64_sim => {
306-
cvs!["MACOSX_DEPLOYMENT_TARGET"]
307-
}
308-
X86_64_macabi | Arm64_macabi => cvs!["IPHONEOS_DEPLOYMENT_TARGET"],
309-
}
306+
cvs!["MACOSX_DEPLOYMENT_TARGET"]
310307
}
311308
}
312309

313-
fn ios_deployment_target(arch: Arch) -> (u32, u32) {
310+
fn ios_deployment_target(arch: Arch, abi: &str) -> (u32, u32) {
314311
// If you are looking for the default deployment target, prefer `rustc --print deployment-target`.
315-
let (major, minor) = if arch == Arm64e { (14, 0) } else { (10, 0) };
312+
let (major, minor) = match (arch, abi) {
313+
(Arm64e, _) => (14, 0),
314+
// Mac Catalyst defaults to 13.1 in Clang.
315+
(_, "macabi") => (13, 1),
316+
_ => (10, 0),
317+
};
316318
from_set_deployment_target("IPHONEOS_DEPLOYMENT_TARGET").unwrap_or((major, minor))
317319
}
318320

319-
fn mac_catalyst_deployment_target() -> (u32, u32) {
320-
// If you are looking for the default deployment target, prefer `rustc --print deployment-target`.
321-
from_set_deployment_target("IPHONEOS_DEPLOYMENT_TARGET").unwrap_or((14, 0))
322-
}
323-
324321
pub fn ios_llvm_target(arch: Arch) -> String {
325322
// Modern iOS tooling extracts information about deployment target
326323
// from LC_BUILD_VERSION. This load command will only be emitted when
327324
// we build with a version specific `llvm_target`, with the version
328325
// set high enough. Luckily one LC_BUILD_VERSION is enough, for Xcode
329326
// to pick it up (since std and core are still built with the fallback
330327
// of version 7.0 and hence emit the old LC_IPHONE_MIN_VERSION).
331-
let (major, minor) = ios_deployment_target(arch);
328+
let (major, minor) = ios_deployment_target(arch, "");
332329
format!("{}-apple-ios{}.{}.0", arch.target_name(), major, minor)
333330
}
334331

335-
fn ios_lld_platform_version(arch: Arch) -> String {
336-
let (major, minor) = ios_deployment_target(arch);
337-
format!("{major}.{minor}")
332+
pub fn mac_catalyst_llvm_target(arch: Arch) -> String {
333+
let (major, minor) = ios_deployment_target(arch, "macabi");
334+
format!("{}-apple-ios{}.{}.0-macabi", arch.target_name(), major, minor)
338335
}
339336

340337
pub fn ios_sim_llvm_target(arch: Arch) -> String {
341-
let (major, minor) = ios_deployment_target(arch);
338+
let (major, minor) = ios_deployment_target(arch, "sim");
342339
format!("{}-apple-ios{}.{}.0-simulator", arch.target_name(), major, minor)
343340
}
344341

@@ -347,11 +344,6 @@ fn tvos_deployment_target() -> (u32, u32) {
347344
from_set_deployment_target("TVOS_DEPLOYMENT_TARGET").unwrap_or((10, 0))
348345
}
349346

350-
fn tvos_lld_platform_version() -> String {
351-
let (major, minor) = tvos_deployment_target();
352-
format!("{major}.{minor}")
353-
}
354-
355347
pub fn tvos_llvm_target(arch: Arch) -> String {
356348
let (major, minor) = tvos_deployment_target();
357349
format!("{}-apple-tvos{}.{}.0", arch.target_name(), major, minor)
@@ -367,11 +359,6 @@ fn watchos_deployment_target() -> (u32, u32) {
367359
from_set_deployment_target("WATCHOS_DEPLOYMENT_TARGET").unwrap_or((5, 0))
368360
}
369361

370-
fn watchos_lld_platform_version() -> String {
371-
let (major, minor) = watchos_deployment_target();
372-
format!("{major}.{minor}")
373-
}
374-
375362
pub fn watchos_sim_llvm_target(arch: Arch) -> String {
376363
let (major, minor) = watchos_deployment_target();
377364
format!("{}-apple-watchos{}.{}.0-simulator", arch.target_name(), major, minor)

compiler/rustc_target/src/spec/targets/aarch64_apple_ios_macabi.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
use crate::spec::base::apple::{opts, Arch};
2-
use crate::spec::{Cc, FramePointer, LinkerFlavor, Lld, SanitizerSet, Target, TargetOptions};
1+
use crate::spec::base::apple::{mac_catalyst_llvm_target, opts, Arch};
2+
use crate::spec::{FramePointer, SanitizerSet, Target, TargetOptions};
33

44
pub fn target() -> Target {
5-
let llvm_target = "arm64-apple-ios14.0-macabi";
6-
75
let arch = Arch::Arm64_macabi;
86
let mut base = opts("ios", arch);
9-
base.add_pre_link_args(LinkerFlavor::Darwin(Cc::Yes, Lld::No), &["-target", llvm_target]);
107
base.supported_sanitizers = SanitizerSet::ADDRESS | SanitizerSet::LEAK | SanitizerSet::THREAD;
118

129
Target {
13-
llvm_target: llvm_target.into(),
10+
llvm_target: mac_catalyst_llvm_target(arch).into(),
1411
pointer_width: 64,
1512
data_layout: "e-m:o-i64:64-i128:128-n32:64-S128".into(),
1613
arch: arch.target_arch(),

compiler/rustc_target/src/spec/targets/x86_64_apple_ios_macabi.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
use crate::spec::base::apple::{opts, Arch};
2-
use crate::spec::{Cc, LinkerFlavor, Lld, SanitizerSet, Target, TargetOptions};
1+
use crate::spec::base::apple::{mac_catalyst_llvm_target, opts, Arch};
2+
use crate::spec::{SanitizerSet, Target, TargetOptions};
33

44
pub fn target() -> Target {
5-
let llvm_target = "x86_64-apple-ios14.0-macabi";
6-
75
let arch = Arch::X86_64_macabi;
86
let mut base = opts("ios", arch);
9-
base.add_pre_link_args(LinkerFlavor::Darwin(Cc::Yes, Lld::No), &["-target", llvm_target]);
107
base.supported_sanitizers = SanitizerSet::ADDRESS | SanitizerSet::LEAK | SanitizerSet::THREAD;
118

129
Target {
13-
llvm_target: llvm_target.into(),
10+
llvm_target: mac_catalyst_llvm_target(arch).into(),
1411
pointer_width: 64,
1512
data_layout:
1613
"e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128".into(),

0 commit comments

Comments
 (0)