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