1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use std::process::Command;

use execute::command;
use swift_bridge_build::ApplePlatform;

pub trait TargetInfo {
    fn target(&self) -> Target;
}

#[derive(Debug, Clone)]
pub enum Target {
    Single {
        architecture: &'static str,
        display_name: &'static str,
        platform: ApplePlatform,
    },
    Universal {
        universal_name: &'static str,
        architectures: Vec<&'static str>,
        display_name: &'static str,
        platform: ApplePlatform,
    },
}

impl Target {
    fn cargo_build_commands(&self) -> Vec<Command> {
        self.architectures()
            .into_iter()
            .map(|arch| command(format!("cargo build --target {arch}")))
            .collect()
    }

    fn lipo_commands(&self, crate_name: &str) -> Vec<Command> {
        // TODO: Make this configurable
        let mode = "debug";
        match self {
            Target::Single { .. } => vec![],
            Target::Universal { architectures, .. } => {
                let path = self.framework_directory();

                let target_name = format!("lib{}.a", crate_name.replace('-', "_"));
                let component_paths: Vec<_> = architectures
                    .into_iter()
                    .map(|arch| format!("./target/{arch}/{mode}/{target_name}"))
                    .collect();
                let args = component_paths.join(" ");
                let target_path = self.framework_path(crate_name);

                let make_dir = command(format!("mkdir -p {path}"));
                let lipo = command(format!("lipo {args} -create -output {target_path}"));
                vec![make_dir, lipo]
            }
        }
    }

    /// Generates all commands necessary to build this target
    ///
    /// This function returns a list of commands that should be executed in their given
    /// order to build this target (and bundle architecture targets with lipo if it is a universal target).
    pub fn commands(&self, crate_name: &str) -> Vec<Command> {
        self.cargo_build_commands()
            .into_iter()
            .chain(self.lipo_commands(crate_name))
            .collect()
    }

    /// Returns the names of all target architectures for this target
    ///
    /// If this target is a single target, the returned vector will always contain exactly one element.
    /// The names returned here exactly match the identifiers of the respective official Rust targets.
    pub fn architectures(&self) -> Vec<&'static str> {
        match self {
            Target::Single { architecture, .. } => vec![architecture],
            Target::Universal { architectures, .. } => architectures.to_owned(),
        }
    }

    pub fn display_name(&self) -> &'static str {
        match self {
            Target::Single { display_name, .. } => display_name,
            Target::Universal { display_name, .. } => display_name,
        }
    }

    pub fn platform(&self) -> ApplePlatform {
        match self {
            Target::Single { platform, .. } => *platform,
            Target::Universal { platform, .. } => *platform,
        }
    }

    pub fn framework_directory(&self) -> String {
        // TODO: Make this configurable
        let mode = "debug";
        match self {
            Target::Single { architecture, .. } => format!("./target/{architecture}/{mode}"),
            Target::Universal { universal_name, .. } => format!("./target/{universal_name}/{mode}"),
        }
    }

    pub fn framework_path(&self, crate_name: &str) -> String {
        format!(
            "{}/lib{}.a",
            self.framework_directory(),
            crate_name.replace('-', "_")
        )
    }
}

impl TargetInfo for ApplePlatform {
    fn target(&self) -> Target {
        use ApplePlatform::*;
        match self {
            IOS => Target::Single {
                architecture: "aarch64-apple-ios",
                display_name: "iOS",
                platform: *self,
            },
            Simulator => Target::Universal {
                universal_name: "universal-ios",
                architectures: vec!["x86_64-apple-ios", "aarch64-apple-ios-sim"],
                display_name: "iOS Simulator",
                platform: *self,
            },
            MacOS => Target::Universal {
                universal_name: "universal-macos",
                architectures: vec!["x86_64-apple-darwin", "aarch64-apple-darwin"],
                display_name: "macOS",
                platform: *self,
            },
            MacCatalyst => {
                unimplemented!("No official Rust target for platform \"Mac Catalyst\"!")
            }
            TvOS => Target::Universal {
                universal_name: "universal-tvos",
                architectures: vec!["aarch64-apple-tvos", "x86_64-apple-tvos"],
                display_name: "tvOS",
                platform: *self,
            },
            WatchOS => {
                unimplemented!("No official Rust target for platform \"watchOS\"!")
            }
            WatchOSSimulator => {
                unimplemented!("No official Rust target for platform \"watchOS Simulator\"!")
            }
            CarPlayOS => unimplemented!("No official Rust target for platform \"CarPlay\"!"),
            CarPlayOSSimulator => {
                unimplemented!("No official Rust target for platform \"CarPlay Simulator\"!")
            }
        }
    }
}