1 use std::{borrow::Cow, env};
3 use crate::spec::{cvs, FramePointer, LldFlavor, SplitDebuginfo, TargetOptions};
5 pub fn opts(os: &'static str) -> TargetOptions {
6 // ELF TLS is only available in macOS 10.7+. If you try to compile for 10.6
7 // either the linker will complain if it is used or the binary will end up
8 // segfaulting at runtime when run on 10.6. Rust by default supports macOS
9 // 10.7+, but there is a standard environment variable,
10 // MACOSX_DEPLOYMENT_TARGET, which is used to signal targeting older
11 // versions of macOS. For example compiling on 10.10 with
12 // MACOSX_DEPLOYMENT_TARGET set to 10.6 will cause the linker to generate
13 // warnings about the usage of ELF TLS.
15 // Here we detect what version is being requested, defaulting to 10.7. ELF
16 // TLS is flagged as enabled if it looks to be supported. The architecture
17 // only matters for default deployment target which is 11.0 for ARM64 and
18 // 10.7 for everything else.
19 let has_thread_local = macos_deployment_target("x86_64") >= (10, 7);
23 vendor: "apple".into(),
24 // macOS has -dead_strip, which doesn't rely on function_sections
25 function_sections: false,
26 dynamic_linking: true,
29 families: cvs!["unix"],
31 dwarf_version: Some(2),
32 frame_pointer: FramePointer::Always,
34 dll_suffix: ".dylib".into(),
35 archive_format: "darwin".into(),
37 abi_return_struct_as_int: true,
38 emit_debug_gdb_scripts: false,
39 eh_frame_header: false,
40 lld_flavor: LldFlavor::Ld64,
42 // The historical default for macOS targets is to run `dsymutil` which
43 // generates a packed version of debuginfo split from the main file.
44 split_debuginfo: SplitDebuginfo::Packed,
46 // This environment variable is pretty magical but is intended for
47 // producing deterministic builds. This was first discovered to be used
48 // by the `ar` tool as a way to control whether or not mtime entries in
49 // the archive headers were set to zero or not. It appears that
50 // eventually the linker got updated to do the same thing and now reads
51 // this environment variable too in recent versions.
53 // For some more info see the commentary on #47086
54 link_env: Cow::Borrowed(&[(Cow::Borrowed("ZERO_AR_DATE"), Cow::Borrowed("1"))]),
60 fn deployment_target(var_name: &str) -> Option<(u32, u32)> {
61 let deployment_target = env::var(var_name).ok();
64 .and_then(|s| s.split_once('.'))
65 .and_then(|(a, b)| a.parse::<u32>().and_then(|a| b.parse::<u32>().map(|b| (a, b))).ok())
68 fn macos_default_deployment_target(arch: &str) -> (u32, u32) {
69 if arch == "arm64" { (11, 0) } else { (10, 7) }
72 fn macos_deployment_target(arch: &str) -> (u32, u32) {
73 deployment_target("MACOSX_DEPLOYMENT_TARGET")
74 .unwrap_or_else(|| macos_default_deployment_target(arch))
77 pub fn macos_llvm_target(arch: &str) -> String {
78 let (major, minor) = macos_deployment_target(arch);
79 format!("{}-apple-macosx{}.{}.0", arch, major, minor)
82 pub fn macos_link_env_remove() -> Vec<Cow<'static, str>> {
83 let mut env_remove = Vec::with_capacity(2);
84 // Remove the `SDKROOT` environment variable if it's clearly set for the wrong platform, which
85 // may occur when we're linking a custom build script while targeting iOS for example.
86 if let Ok(sdkroot) = env::var("SDKROOT") {
87 if sdkroot.contains("iPhoneOS.platform") || sdkroot.contains("iPhoneSimulator.platform") {
88 env_remove.push("SDKROOT".into())
91 // Additionally, `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at
92 // "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld",
93 // although this is apparently ignored when using the linker at "/usr/bin/ld".
94 env_remove.push("IPHONEOS_DEPLOYMENT_TARGET".into());
98 fn ios_deployment_target() -> (u32, u32) {
99 deployment_target("IPHONEOS_DEPLOYMENT_TARGET").unwrap_or((7, 0))
102 pub fn ios_llvm_target(arch: &str) -> String {
103 // Modern iOS tooling extracts information about deployment target
104 // from LC_BUILD_VERSION. This load command will only be emitted when
105 // we build with a version specific `llvm_target`, with the version
106 // set high enough. Luckily one LC_BUILD_VERSION is enough, for Xcode
107 // to pick it up (since std and core are still built with the fallback
108 // of version 7.0 and hence emit the old LC_IPHONE_MIN_VERSION).
109 let (major, minor) = ios_deployment_target();
110 format!("{}-apple-ios{}.{}.0", arch, major, minor)
113 pub fn ios_sim_llvm_target(arch: &str) -> String {
114 let (major, minor) = ios_deployment_target();
115 format!("{}-apple-ios{}.{}.0-simulator", arch, major, minor)