rustc_target/spec/base/apple/
mod.rs

1use std::borrow::Cow;
2use std::fmt::{Display, from_fn};
3use std::num::ParseIntError;
4use std::str::FromStr;
5
6use crate::spec::{
7    BinaryFormat, Cc, DebuginfoKind, FloatAbi, FramePointer, LinkerFlavor, Lld, RustcAbi,
8    SplitDebuginfo, StackProbeType, StaticCow, Target, TargetOptions, cvs,
9};
10
11#[cfg(test)]
12mod tests;
13
14use Arch::*;
15#[allow(non_camel_case_types)]
16#[derive(Copy, Clone, PartialEq)]
17pub(crate) enum Arch {
18    Armv7k,
19    Armv7s,
20    Arm64,
21    Arm64e,
22    Arm64_32,
23    I386,
24    I686,
25    X86_64,
26    X86_64h,
27}
28
29impl Arch {
30    fn target_name(self) -> &'static str {
31        match self {
32            Armv7k => "armv7k",
33            Armv7s => "armv7s",
34            Arm64 => "arm64",
35            Arm64e => "arm64e",
36            Arm64_32 => "arm64_32",
37            I386 => "i386",
38            I686 => "i686",
39            X86_64 => "x86_64",
40            X86_64h => "x86_64h",
41        }
42    }
43
44    pub(crate) fn target_arch(self) -> Cow<'static, str> {
45        Cow::Borrowed(match self {
46            Armv7k | Armv7s => "arm",
47            Arm64 | Arm64e | Arm64_32 => "aarch64",
48            I386 | I686 => "x86",
49            X86_64 | X86_64h => "x86_64",
50        })
51    }
52
53    fn target_cpu(self, env: TargetEnv) -> &'static str {
54        match self {
55            Armv7k => "cortex-a8",
56            Armv7s => "swift", // iOS 10 is only supported on iPhone 5 or higher.
57            Arm64 => match env {
58                TargetEnv::Normal => "apple-a7",
59                TargetEnv::Simulator => "apple-a12",
60                TargetEnv::MacCatalyst => "apple-a12",
61            },
62            Arm64e => "apple-a12",
63            Arm64_32 => "apple-s4",
64            // Only macOS 10.12+ is supported, which means
65            // all x86_64/x86 CPUs must be running at least penryn
66            // https://github.com/llvm/llvm-project/blob/01f924d0e37a5deae51df0d77e10a15b63aa0c0f/clang/lib/Driver/ToolChains/Arch/X86.cpp#L79-L82
67            I386 | I686 => "penryn",
68            X86_64 => "penryn",
69            // Note: `core-avx2` is slightly more advanced than `x86_64h`, see
70            // comments (and disabled features) in `x86_64h_apple_darwin` for
71            // details. It is a higher baseline then `penryn` however.
72            X86_64h => "core-avx2",
73        }
74    }
75
76    fn stack_probes(self) -> StackProbeType {
77        match self {
78            Armv7k | Armv7s => StackProbeType::None,
79            Arm64 | Arm64e | Arm64_32 | I386 | I686 | X86_64 | X86_64h => StackProbeType::Inline,
80        }
81    }
82}
83
84#[derive(Copy, Clone, PartialEq)]
85pub(crate) enum TargetEnv {
86    Normal,
87    Simulator,
88    MacCatalyst,
89}
90
91impl TargetEnv {
92    fn target_env(self) -> &'static str {
93        match self {
94            Self::Normal => "",
95            Self::MacCatalyst => "macabi",
96            Self::Simulator => "sim",
97        }
98    }
99}
100
101/// Get the base target options, unversioned LLVM target and `target_arch` from the three
102/// things that uniquely identify Rust's Apple targets: The OS, the architecture, and the ABI.
103pub(crate) fn base(
104    os: &'static str,
105    arch: Arch,
106    env: TargetEnv,
107) -> (TargetOptions, StaticCow<str>, StaticCow<str>) {
108    let mut opts = TargetOptions {
109        llvm_floatabi: Some(FloatAbi::Hard),
110        os: os.into(),
111        env: env.target_env().into(),
112        // NOTE: We originally set `cfg(target_abi = "macabi")` / `cfg(target_abi = "sim")`,
113        // before it was discovered that those are actually environments:
114        // https://github.com/rust-lang/rust/issues/133331
115        //
116        // But let's continue setting them for backwards compatibility.
117        // FIXME(madsmtm): Warn about using these in the future.
118        abi: env.target_env().into(),
119        cpu: arch.target_cpu(env).into(),
120        link_env_remove: link_env_remove(os),
121        vendor: "apple".into(),
122        linker_flavor: LinkerFlavor::Darwin(Cc::Yes, Lld::No),
123        // macOS has -dead_strip, which doesn't rely on function_sections
124        function_sections: false,
125        dynamic_linking: true,
126        families: cvs!["unix"],
127        is_like_darwin: true,
128        binary_format: BinaryFormat::MachO,
129        // LLVM notes that macOS 10.11+ and iOS 9+ default
130        // to v4, so we do the same.
131        // https://github.com/llvm/llvm-project/blob/378778a0d10c2f8d5df8ceff81f95b6002984a4b/clang/lib/Driver/ToolChains/Darwin.cpp#L1203
132        default_dwarf_version: 4,
133        frame_pointer: match arch {
134            // clang ignores `-fomit-frame-pointer` for Armv7, it only accepts `-momit-leaf-frame-pointer`
135            Armv7k | Armv7s => FramePointer::Always,
136            // clang supports omitting frame pointers for the rest, but... don't?
137            Arm64 | Arm64e | Arm64_32 => FramePointer::NonLeaf,
138            I386 | I686 | X86_64 | X86_64h => FramePointer::Always,
139        },
140        has_rpath: true,
141        dll_suffix: ".dylib".into(),
142        archive_format: "darwin".into(),
143        // Thread locals became available with iOS 8 and macOS 10.7,
144        // and both are far below our minimum.
145        has_thread_local: true,
146        abi_return_struct_as_int: true,
147        emit_debug_gdb_scripts: false,
148        eh_frame_header: false,
149        stack_probes: arch.stack_probes(),
150
151        debuginfo_kind: DebuginfoKind::DwarfDsym,
152        // The historical default for macOS targets is to run `dsymutil` which
153        // generates a packed version of debuginfo split from the main file.
154        split_debuginfo: SplitDebuginfo::Packed,
155        supported_split_debuginfo: Cow::Borrowed(&[
156            SplitDebuginfo::Packed,
157            SplitDebuginfo::Unpacked,
158            SplitDebuginfo::Off,
159        ]),
160
161        // Tell the linker that we would like it to avoid irreproducible binaries.
162        //
163        // This environment variable is pretty magical but is intended for
164        // producing deterministic builds. This was first discovered to be used
165        // by the `ar` tool as a way to control whether or not mtime entries in
166        // the archive headers were set to zero or not.
167        //
168        // In `ld64-351.8`, shipped with Xcode 9.3, the linker was updated to
169        // read this flag too. Linker versions that don't support this flag
170        // may embed modification timestamps in binaries (especially in debug
171        // information).
172        //
173        // A cleaner alternative would be to pass the `-reproducible` flag,
174        // though that is only supported since `ld64-819.6` shipped with Xcode
175        // 14, which is too new for our minimum supported version:
176        // https://doc.rust-lang.org/rustc/platform-support/apple-darwin.html#host-tooling
177        //
178        // For some more info see the commentary on #47086
179        link_env: Cow::Borrowed(&[(Cow::Borrowed("ZERO_AR_DATE"), Cow::Borrowed("1"))]),
180
181        ..Default::default()
182    };
183    if matches!(arch, Arch::I386 | Arch::I686) {
184        // All Apple x86-32 targets have SSE2.
185        opts.rustc_abi = Some(RustcAbi::X86Sse2);
186    }
187    (opts, unversioned_llvm_target(os, arch, env), arch.target_arch())
188}
189
190/// Generate part of the LLVM target triple.
191///
192/// See `rustc_codegen_ssa::back::versioned_llvm_target` for the full triple passed to LLVM and
193/// Clang.
194fn unversioned_llvm_target(os: &str, arch: Arch, env: TargetEnv) -> StaticCow<str> {
195    let arch = arch.target_name();
196    // Convert to the "canonical" OS name used by LLVM:
197    // https://github.com/llvm/llvm-project/blob/llvmorg-18.1.8/llvm/lib/TargetParser/Triple.cpp#L236-L282
198    let os = match os {
199        "macos" => "macosx",
200        "ios" => "ios",
201        "watchos" => "watchos",
202        "tvos" => "tvos",
203        "visionos" => "xros",
204        _ => unreachable!("tried to get LLVM target OS for non-Apple platform"),
205    };
206    let environment = match env {
207        TargetEnv::Normal => "",
208        TargetEnv::MacCatalyst => "-macabi",
209        TargetEnv::Simulator => "-simulator",
210    };
211    format!("{arch}-apple-{os}{environment}").into()
212}
213
214fn link_env_remove(os: &'static str) -> StaticCow<[StaticCow<str>]> {
215    // Apple platforms only officially support macOS as a host for any compilation.
216    //
217    // If building for macOS, we go ahead and remove any erroneous environment state
218    // that's only applicable to cross-OS compilation. Always leave anything for the
219    // host OS alone though.
220    if os == "macos" {
221        // `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at
222        // "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld",
223        // although this is apparently ignored when using the linker at "/usr/bin/ld".
224        cvs!["IPHONEOS_DEPLOYMENT_TARGET", "TVOS_DEPLOYMENT_TARGET", "XROS_DEPLOYMENT_TARGET"]
225    } else {
226        // Otherwise if cross-compiling for a different OS/SDK (including Mac Catalyst), remove any part
227        // of the linking environment that's wrong and reversed.
228        cvs!["MACOSX_DEPLOYMENT_TARGET"]
229    }
230}
231
232/// Deployment target or SDK version.
233///
234/// The size of the numbers in here are limited by Mach-O's `LC_BUILD_VERSION`.
235#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
236pub struct OSVersion {
237    pub major: u16,
238    pub minor: u8,
239    pub patch: u8,
240}
241
242impl FromStr for OSVersion {
243    type Err = ParseIntError;
244
245    /// Parse an OS version triple (SDK version or deployment target).
246    fn from_str(version: &str) -> Result<Self, ParseIntError> {
247        if let Some((major, minor)) = version.split_once('.') {
248            let major = major.parse()?;
249            if let Some((minor, patch)) = minor.split_once('.') {
250                Ok(Self { major, minor: minor.parse()?, patch: patch.parse()? })
251            } else {
252                Ok(Self { major, minor: minor.parse()?, patch: 0 })
253            }
254        } else {
255            Ok(Self { major: version.parse()?, minor: 0, patch: 0 })
256        }
257    }
258}
259
260impl OSVersion {
261    pub fn new(major: u16, minor: u8, patch: u8) -> Self {
262        Self { major, minor, patch }
263    }
264
265    pub fn fmt_pretty(self) -> impl Display {
266        let Self { major, minor, patch } = self;
267        from_fn(move |f| {
268            write!(f, "{major}.{minor}")?;
269            if patch != 0 {
270                write!(f, ".{patch}")?;
271            }
272            Ok(())
273        })
274    }
275
276    pub fn fmt_full(self) -> impl Display {
277        let Self { major, minor, patch } = self;
278        from_fn(move |f| write!(f, "{major}.{minor}.{patch}"))
279    }
280
281    /// Minimum operating system versions currently supported by `rustc`.
282    pub fn os_minimum_deployment_target(os: &str) -> Self {
283        // When bumping a version in here, remember to update the platform-support docs too.
284        //
285        // NOTE: The defaults may change in future `rustc` versions, so if you are looking for the
286        // default deployment target, prefer:
287        // ```
288        // $ rustc --print deployment-target
289        // ```
290        let (major, minor, patch) = match os {
291            "macos" => (10, 12, 0),
292            "ios" => (10, 0, 0),
293            "tvos" => (10, 0, 0),
294            "watchos" => (5, 0, 0),
295            "visionos" => (1, 0, 0),
296            _ => unreachable!("tried to get deployment target for non-Apple platform"),
297        };
298        Self { major, minor, patch }
299    }
300
301    /// The deployment target for the given target.
302    ///
303    /// This is similar to `os_minimum_deployment_target`, except that on certain targets it makes sense
304    /// to raise the minimum OS version.
305    ///
306    /// This matches what LLVM does, see in part:
307    /// <https://github.com/llvm/llvm-project/blob/llvmorg-21.1.3/llvm/lib/TargetParser/Triple.cpp#L2140-L2175>
308    pub fn minimum_deployment_target(target: &Target) -> Self {
309        let (major, minor, patch) = match (&*target.os, &*target.arch, &*target.env) {
310            ("macos", "aarch64", _) => (11, 0, 0),
311            ("ios", "aarch64", "macabi") => (14, 0, 0),
312            ("ios", "aarch64", "sim") => (14, 0, 0),
313            ("ios", _, _) if target.llvm_target.starts_with("arm64e") => (14, 0, 0),
314            // Mac Catalyst defaults to 13.1 in Clang.
315            ("ios", _, "macabi") => (13, 1, 0),
316            ("tvos", "aarch64", "sim") => (14, 0, 0),
317            ("watchos", "aarch64", "sim") => (7, 0, 0),
318            // True Aarch64 on watchOS (instead of their Aarch64 Ilp32 called `arm64_32`) has been
319            // available since Xcode 14, but it's only actually used more recently in watchOS 26.
320            ("watchos", "aarch64", "") if !target.llvm_target.starts_with("arm64_32") => (26, 0, 0),
321            (os, _, _) => return Self::os_minimum_deployment_target(os),
322        };
323        Self { major, minor, patch }
324    }
325}
326
327/// Name of the environment variable used to fetch the deployment target on the given OS.
328pub fn deployment_target_env_var(os: &str) -> &'static str {
329    match os {
330        "macos" => "MACOSX_DEPLOYMENT_TARGET",
331        "ios" => "IPHONEOS_DEPLOYMENT_TARGET",
332        "watchos" => "WATCHOS_DEPLOYMENT_TARGET",
333        "tvos" => "TVOS_DEPLOYMENT_TARGET",
334        "visionos" => "XROS_DEPLOYMENT_TARGET",
335        _ => unreachable!("tried to get deployment target env var for non-Apple platform"),
336    }
337}