apple_platforms/
triple.rs

1use anyhow::{anyhow, Result};
2use std::{borrow::Cow, fmt, str::FromStr};
3
4#[derive(Debug)]
5pub enum Architecture {
6    X86,
7    X86_64,
8    Arm,
9    Arm64,
10}
11
12impl FromStr for Architecture {
13    type Err = anyhow::Error;
14
15    fn from_str(target: &str) -> Result<Self> {
16        match target.to_lowercase().as_str() {
17            "x86" => Ok(Architecture::X86),
18            "x86_64" => Ok(Architecture::X86_64),
19            "arm" => Ok(Architecture::Arm),
20            "aarch64" => Ok(Architecture::Arm64),
21            _ => Err(anyhow!("Unsupported architecture: {target}")),
22        }
23    }
24}
25
26impl fmt::Display for Architecture {
27    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28        let str = match self {
29            Architecture::X86 => Cow::from("x86"),
30            Architecture::X86_64 => Cow::from("x64"),
31            Architecture::Arm => Cow::from("arm"),
32            Architecture::Arm64 => Cow::from("arm64"),
33        };
34        write!(f, "{}", str)
35    }
36}
37
38pub struct Triple;
39
40impl Triple {
41    /**
42     * Convert rust target to clang compatible target.
43     * https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/BinaryFormat/MachO.def#L123-L138
44     * Note: `visionos` is still called `xros` in clang, though it's `visionos` in Xcode and rust.
45     */
46    pub fn target_to_clang_target(target: &str) -> &str {
47        // TODO: implement using Architecture and Platform.tapi_target
48        match target {
49            "aarch64-apple-ios-macabi" => "arm64-apple-maccatalyst",
50            "aarch64-apple-ios" => "arm64-apple-ios",
51            "aarch64-apple-ios-sim" => "aarch64-apple-ios-simulator",
52            "aarch64-apple-tvos" => "arm64-apple-tvos",
53            "aarch64-apple-tvos-sim" => "aarch-apple-tvos-simulator",
54            "aarch64-apple-watchos-sim" => "aarch64-apple-watchos-simulator",
55            "aarch64-apple-driverkit" => "arm64-apple-driverkit",
56            "aarch64-apple-visionos" => "arm64-apple-xros",
57            "aarch64-apple-visionos-sim" => "aarch64-apple-xros-simulator",
58            "aarch64-apple-darwin" => "arm64-apple-darwin",
59            // "aarch64-apple-watchos" => "",
60            // "arm64_32-apple-watchos" => "",
61            // "armv7k-apple-watchos" => "",
62            // "aarch64-apple-watchos-sim" => "",
63            // "x86_64-apple-watchos-sim" => "",
64            // "aarch64-apple-tvos" => "",
65            // "aarch64-apple-tvos-sim" => "",
66            // "x86_64-apple-tvos" => "",
67            _ => target,
68        }
69    }
70}
71
72pub struct SDK;
73
74impl SDK {
75    pub fn target_to_sdk(target: &str) -> Result<&str> {
76        match target {
77            "aarch64-apple-darwin" | "x86_64-apple-darwin" => Ok("macosx"),
78            "x86_64-apple-ios" | "i386-apple-ios" | "aarch64-apple-ios-sim" => {
79                Ok("iphonesimulator")
80            }
81            "aarch64-apple-ios" | "armv7-apple-ios" | "armv7s-apple-ios" => Ok("iphoneos"),
82            "aarch64-apple-visionos-sim" => Ok("xrsimulator"),
83            "aarch64-apple-visionos" => Ok("xros"),
84            "aarch64-apple-tvos-sim" | "x86_64-apple-tvos" => Ok("appletvsimulator"),
85            "aarch64-apple-tvos" => Ok("appletvos"),
86            "aarch64-apple-watchos" | "armv7k-apple-watchos" | "arm64_32-apple-watchos" => {
87                Ok("watchos")
88            }
89
90            "aarch64-apple-watchos-sim" | "x86_64-apple-watchos-sim" => Ok("watchsimulator"),
91            _ => Err(anyhow!("Unsupported target for sdk: {target}")),
92        }
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99
100    #[test]
101    fn converts_to_clang() {
102        let clang_target = Triple::target_to_clang_target("aarch64-apple-visionos");
103        assert_eq!(clang_target, "arm64-apple-xros")
104    }
105}