3 use crate::spec::{LinkArgs, TargetOptions};
5 pub fn opts(os: &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.
17 let version = macos_deployment_target();
21 vendor: "apple".to_string(),
22 // macOS has -dead_strip, which doesn't rely on function_sections
23 function_sections: false,
24 dynamic_linking: true,
26 os_family: Some("unix".to_string()),
28 dwarf_version: Some(2),
30 dll_prefix: "lib".to_string(),
31 dll_suffix: ".dylib".to_string(),
32 archive_format: "darwin".to_string(),
33 pre_link_args: LinkArgs::new(),
34 has_elf_tls: version >= (10, 7),
35 abi_return_struct_as_int: true,
36 emit_debug_gdb_scripts: false,
37 eh_frame_header: false,
39 // This environment variable is pretty magical but is intended for
40 // producing deterministic builds. This was first discovered to be used
41 // by the `ar` tool as a way to control whether or not mtime entries in
42 // the archive headers were set to zero or not. It appears that
43 // eventually the linker got updated to do the same thing and now reads
44 // this environment variable too in recent versions.
46 // For some more info see the commentary on #47086
47 link_env: vec![("ZERO_AR_DATE".to_string(), "1".to_string())],
53 fn macos_deployment_target() -> (u32, u32) {
54 let deployment_target = env::var("MACOSX_DEPLOYMENT_TARGET").ok();
55 let version = deployment_target
58 let mut i = s.splitn(2, '.');
59 i.next().and_then(|a| i.next().map(|b| (a, b)))
61 .and_then(|(a, b)| a.parse::<u32>().and_then(|a| b.parse::<u32>().map(|b| (a, b))).ok());
63 version.unwrap_or((10, 7))
66 pub fn macos_llvm_target(arch: &str) -> String {
67 let (major, minor) = macos_deployment_target();
68 format!("{}-apple-macosx{}.{}.0", arch, major, minor)
71 pub fn macos_link_env_remove() -> Vec<String> {
72 let mut env_remove = Vec::with_capacity(2);
73 // Remove the `SDKROOT` environment variable if it's clearly set for the wrong platform, which
74 // may occur when we're linking a custom build script while targeting iOS for example.
75 if let Ok(sdkroot) = env::var("SDKROOT") {
76 if sdkroot.contains("iPhoneOS.platform") || sdkroot.contains("iPhoneSimulator.platform") {
77 env_remove.push("SDKROOT".to_string())
80 // Additionally, `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at
81 // "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld",
82 // although this is apparently ignored when using the linker at "/usr/bin/ld".
83 env_remove.push("IPHONEOS_DEPLOYMENT_TARGET".to_string());