use std::borrow::Cow;
use std::env;
use crate::spec::{
add_link_args, add_link_args_iter, cvs, Cc, DebuginfoKind, FramePointer, LinkArgs,
LinkerFlavor, Lld, SplitDebuginfo, StackProbeType, StaticCow, Target, TargetOptions,
};
#[cfg(test)]
mod tests;
use Arch::*;
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, PartialEq)]
pub enum Arch {
Armv7k,
Armv7s,
Arm64,
Arm64e,
Arm64_32,
I386,
I686,
X86_64,
X86_64h,
}
impl Arch {
pub fn target_name(self) -> &'static str {
match self {
Armv7k => "armv7k",
Armv7s => "armv7s",
Arm64 => "arm64",
Arm64e => "arm64e",
Arm64_32 => "arm64_32",
I386 => "i386",
I686 => "i686",
X86_64 => "x86_64",
X86_64h => "x86_64h",
}
}
pub fn target_arch(self) -> Cow<'static, str> {
Cow::Borrowed(match self {
Armv7k | Armv7s => "arm",
Arm64 | Arm64e | Arm64_32 => "aarch64",
I386 | I686 => "x86",
X86_64 | X86_64h => "x86_64",
})
}
fn target_cpu(self, abi: TargetAbi) -> &'static str {
match self {
Armv7k => "cortex-a8",
Armv7s => "swift", Arm64 => match abi {
TargetAbi::Normal => "apple-a7",
TargetAbi::Simulator => "apple-a12",
TargetAbi::MacCatalyst => "apple-a12",
},
Arm64e => "apple-a12",
Arm64_32 => "apple-s4",
I386 | I686 => "penryn",
X86_64 => "penryn",
X86_64h => "core-avx2",
}
}
fn stack_probes(self) -> StackProbeType {
match self {
Armv7k | Armv7s => StackProbeType::None,
Arm64 | Arm64e | Arm64_32 | I386 | I686 | X86_64 | X86_64h => StackProbeType::Inline,
}
}
}
#[derive(Copy, Clone, PartialEq)]
pub enum TargetAbi {
Normal,
Simulator,
MacCatalyst,
}
impl TargetAbi {
fn target_abi(self) -> &'static str {
match self {
Self::Normal => "",
Self::MacCatalyst => "macabi",
Self::Simulator => "sim",
}
}
}
fn pre_link_args(os: &'static str, arch: Arch, abi: TargetAbi) -> LinkArgs {
let platform_name: StaticCow<str> = match abi {
TargetAbi::Normal => os.into(),
TargetAbi::Simulator => format!("{os}-simulator").into(),
TargetAbi::MacCatalyst => "mac-catalyst".into(),
};
let min_version: StaticCow<str> = {
let (major, minor) = match os {
"ios" => ios_deployment_target(arch, abi.target_abi()),
"tvos" => tvos_deployment_target(),
"watchos" => watchos_deployment_target(),
"visionos" => visionos_deployment_target(),
"macos" => macos_deployment_target(arch),
_ => unreachable!(),
};
format!("{major}.{minor}").into()
};
let sdk_version = min_version.clone();
let mut args = TargetOptions::link_args(
LinkerFlavor::Darwin(Cc::No, Lld::No),
&["-arch", arch.target_name(), "-platform_version"],
);
add_link_args_iter(
&mut args,
LinkerFlavor::Darwin(Cc::No, Lld::No),
[platform_name, min_version, sdk_version].into_iter(),
);
if abi != TargetAbi::MacCatalyst {
add_link_args(
&mut args,
LinkerFlavor::Darwin(Cc::Yes, Lld::No),
&["-arch", arch.target_name()],
);
} else {
add_link_args_iter(
&mut args,
LinkerFlavor::Darwin(Cc::Yes, Lld::No),
["-target".into(), mac_catalyst_llvm_target(arch).into()].into_iter(),
);
}
args
}
pub fn opts(os: &'static str, arch: Arch, abi: TargetAbi) -> TargetOptions {
TargetOptions {
abi: abi.target_abi().into(),
os: os.into(),
cpu: arch.target_cpu(abi).into(),
link_env_remove: link_env_remove(os),
vendor: "apple".into(),
linker_flavor: LinkerFlavor::Darwin(Cc::Yes, Lld::No),
function_sections: false,
dynamic_linking: true,
pre_link_args: pre_link_args(os, arch, abi),
families: cvs!["unix"],
is_like_osx: true,
default_dwarf_version: 4,
frame_pointer: FramePointer::Always,
has_rpath: true,
dll_suffix: ".dylib".into(),
archive_format: "darwin".into(),
has_thread_local: true,
abi_return_struct_as_int: true,
emit_debug_gdb_scripts: false,
eh_frame_header: false,
stack_probes: arch.stack_probes(),
debuginfo_kind: DebuginfoKind::DwarfDsym,
split_debuginfo: SplitDebuginfo::Packed,
supported_split_debuginfo: Cow::Borrowed(&[
SplitDebuginfo::Packed,
SplitDebuginfo::Unpacked,
SplitDebuginfo::Off,
]),
link_env: Cow::Borrowed(&[(Cow::Borrowed("ZERO_AR_DATE"), Cow::Borrowed("1"))]),
..Default::default()
}
}
pub fn sdk_version(platform: u32) -> Option<(u32, u32)> {
match platform {
object::macho::PLATFORM_MACOS => Some((13, 1)),
object::macho::PLATFORM_IOS
| object::macho::PLATFORM_IOSSIMULATOR
| object::macho::PLATFORM_TVOS
| object::macho::PLATFORM_TVOSSIMULATOR
| object::macho::PLATFORM_MACCATALYST => Some((16, 2)),
object::macho::PLATFORM_WATCHOS | object::macho::PLATFORM_WATCHOSSIMULATOR => Some((9, 1)),
11 | 12 => Some((1, 0)),
_ => None,
}
}
pub fn platform(target: &Target) -> Option<u32> {
Some(match (&*target.os, &*target.abi) {
("macos", _) => object::macho::PLATFORM_MACOS,
("ios", "macabi") => object::macho::PLATFORM_MACCATALYST,
("ios", "sim") => object::macho::PLATFORM_IOSSIMULATOR,
("ios", _) => object::macho::PLATFORM_IOS,
("watchos", "sim") => object::macho::PLATFORM_WATCHOSSIMULATOR,
("watchos", _) => object::macho::PLATFORM_WATCHOS,
("tvos", "sim") => object::macho::PLATFORM_TVOSSIMULATOR,
("tvos", _) => object::macho::PLATFORM_TVOS,
("visionos", "sim") => 12,
("visionos", _) => 11,
_ => return None,
})
}
pub fn deployment_target(target: &Target) -> Option<(u32, u32)> {
let (major, minor) = match &*target.os {
"macos" => {
let arch = match target.arch.as_ref() {
"x86" | "x86_64" => X86_64,
"arm64e" => Arm64e,
_ => Arm64,
};
macos_deployment_target(arch)
}
"ios" => {
let arch = match target.arch.as_ref() {
"arm64e" => Arm64e,
_ => Arm64,
};
ios_deployment_target(arch, &target.abi)
}
"watchos" => watchos_deployment_target(),
"tvos" => tvos_deployment_target(),
"visionos" => visionos_deployment_target(),
_ => return None,
};
Some((major, minor))
}
fn from_set_deployment_target(var_name: &str) -> Option<(u32, u32)> {
let deployment_target = env::var(var_name).ok()?;
let (unparsed_major, unparsed_minor) = deployment_target.split_once('.')?;
let (major, minor) = (unparsed_major.parse().ok()?, unparsed_minor.parse().ok()?);
Some((major, minor))
}
fn macos_default_deployment_target(arch: Arch) -> (u32, u32) {
match arch {
Arm64 | Arm64e => (11, 0),
_ => (10, 12),
}
}
fn macos_deployment_target(arch: Arch) -> (u32, u32) {
from_set_deployment_target("MACOSX_DEPLOYMENT_TARGET")
.unwrap_or_else(|| macos_default_deployment_target(arch))
}
pub fn macos_llvm_target(arch: Arch) -> String {
let (major, minor) = macos_deployment_target(arch);
format!("{}-apple-macosx{}.{}.0", arch.target_name(), major, minor)
}
fn link_env_remove(os: &'static str) -> StaticCow<[StaticCow<str>]> {
if os == "macos" {
let mut env_remove = Vec::with_capacity(2);
if let Ok(sdkroot) = env::var("SDKROOT") {
if sdkroot.contains("iPhoneOS.platform")
|| sdkroot.contains("iPhoneSimulator.platform")
|| sdkroot.contains("AppleTVOS.platform")
|| sdkroot.contains("AppleTVSimulator.platform")
|| sdkroot.contains("WatchOS.platform")
|| sdkroot.contains("WatchSimulator.platform")
|| sdkroot.contains("XROS.platform")
|| sdkroot.contains("XRSimulator.platform")
{
env_remove.push("SDKROOT".into())
}
}
env_remove.push("IPHONEOS_DEPLOYMENT_TARGET".into());
env_remove.push("TVOS_DEPLOYMENT_TARGET".into());
env_remove.push("XROS_DEPLOYMENT_TARGET".into());
env_remove.into()
} else {
cvs!["MACOSX_DEPLOYMENT_TARGET"]
}
}
fn ios_deployment_target(arch: Arch, abi: &str) -> (u32, u32) {
let (major, minor) = match (arch, abi) {
(Arm64e, _) => (14, 0),
(_, "macabi") => (13, 1),
_ => (10, 0),
};
from_set_deployment_target("IPHONEOS_DEPLOYMENT_TARGET").unwrap_or((major, minor))
}
pub fn ios_llvm_target(arch: Arch) -> String {
let (major, minor) = ios_deployment_target(arch, "");
format!("{}-apple-ios{}.{}.0", arch.target_name(), major, minor)
}
pub fn mac_catalyst_llvm_target(arch: Arch) -> String {
let (major, minor) = ios_deployment_target(arch, "macabi");
format!("{}-apple-ios{}.{}.0-macabi", arch.target_name(), major, minor)
}
pub fn ios_sim_llvm_target(arch: Arch) -> String {
let (major, minor) = ios_deployment_target(arch, "sim");
format!("{}-apple-ios{}.{}.0-simulator", arch.target_name(), major, minor)
}
fn tvos_deployment_target() -> (u32, u32) {
from_set_deployment_target("TVOS_DEPLOYMENT_TARGET").unwrap_or((10, 0))
}
pub fn tvos_llvm_target(arch: Arch) -> String {
let (major, minor) = tvos_deployment_target();
format!("{}-apple-tvos{}.{}.0", arch.target_name(), major, minor)
}
pub fn tvos_sim_llvm_target(arch: Arch) -> String {
let (major, minor) = tvos_deployment_target();
format!("{}-apple-tvos{}.{}.0-simulator", arch.target_name(), major, minor)
}
fn watchos_deployment_target() -> (u32, u32) {
from_set_deployment_target("WATCHOS_DEPLOYMENT_TARGET").unwrap_or((5, 0))
}
pub fn watchos_llvm_target(arch: Arch) -> String {
let (major, minor) = watchos_deployment_target();
format!("{}-apple-watchos{}.{}.0", arch.target_name(), major, minor)
}
pub fn watchos_sim_llvm_target(arch: Arch) -> String {
let (major, minor) = watchos_deployment_target();
format!("{}-apple-watchos{}.{}.0-simulator", arch.target_name(), major, minor)
}
fn visionos_deployment_target() -> (u32, u32) {
from_set_deployment_target("XROS_DEPLOYMENT_TARGET").unwrap_or((1, 0))
}
pub fn visionos_llvm_target(arch: Arch) -> String {
let (major, minor) = visionos_deployment_target();
format!("{}-apple-visionos{}.{}.0", arch.target_name(), major, minor)
}
pub fn visionos_sim_llvm_target(arch: Arch) -> String {
let (major, minor) = visionos_deployment_target();
format!("{}-apple-visionos{}.{}.0-simulator", arch.target_name(), major, minor)
}