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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use std::ops::{Deref, DerefMut};
use std::path::PathBuf;
#[cfg(feature = "universal2")]
use std::process::Stdio;
use std::process::{self, Child, Command};

use anyhow::{Context, Result};
use clap::Parser;

use crate::zig::Zig;

/// Compile a local package and all of its dependencies
/// using zig as the linker
#[derive(Clone, Debug, Default, Parser)]
#[command(
    after_help = "Run `cargo help build` for more detailed information.",
    display_order = 1
)]
pub struct Build {
    #[command(flatten)]
    pub cargo: cargo_options::Build,

    /// Disable zig linker
    #[arg(skip)]
    pub disable_zig_linker: bool,

    /// Enable zig ar
    #[arg(skip)]
    pub enable_zig_ar: bool,
}

impl Build {
    /// Create a new build from manifest path
    #[allow(clippy::field_reassign_with_default)]
    pub fn new(manifest_path: Option<PathBuf>) -> Self {
        let mut build = Self::default();
        build.manifest_path = manifest_path;
        build
    }

    /// Execute `cargo build` command with zig as the linker
    pub fn execute(&self) -> Result<()> {
        let has_universal2 = self
            .cargo
            .target
            .contains(&"universal2-apple-darwin".to_string());
        let mut build = self.build_command()?;
        let mut child = build.spawn().context("Failed to run cargo build")?;
        if has_universal2 {
            self.handle_universal2_build(child)?;
        } else {
            let status = child.wait().expect("Failed to wait on cargo build process");
            if !status.success() {
                process::exit(status.code().unwrap_or(1));
            }
        }
        Ok(())
    }

    #[cfg(not(feature = "universal2"))]
    fn handle_universal2_build(&self, mut _child: Child) -> Result<()> {
        anyhow::bail!("Unsupported Rust target: universal2-apple-darwin")
    }

    #[cfg(feature = "universal2")]
    fn handle_universal2_build(&self, mut child: Child) -> Result<()> {
        use cargo_metadata::Message;
        use std::io::BufReader;
        use std::path::Path;

        // Find root crate package id
        let manifest_path = self
            .manifest_path
            .as_deref()
            .unwrap_or_else(|| Path::new("Cargo.toml"));
        let mut metadata_cmd = cargo_metadata::MetadataCommand::new();
        metadata_cmd.manifest_path(manifest_path);
        let metadata = metadata_cmd.exec()?;
        let root_pkg = metadata.root_package().expect("Should have a root package");

        let mut x86_64_artifacts = Vec::new();
        let mut aarch64_artifacts = Vec::new();

        let stream = child
            .stdout
            .take()
            .expect("Cargo build should have a stdout");
        for message in Message::parse_stream(BufReader::new(stream)) {
            let message = message.context("Failed to parse cargo metadata message")?;
            match message {
                Message::CompilerArtifact(artifact) => {
                    if artifact.package_id == root_pkg.id {
                        for filename in artifact.filenames {
                            if filename.as_str().contains("x86_64-apple-darwin") {
                                x86_64_artifacts.push(filename);
                            } else if filename.as_str().contains("aarch64-apple-darwin") {
                                aarch64_artifacts.push(filename);
                            }
                        }
                    }
                }
                Message::CompilerMessage(msg) => {
                    println!("{}", msg.message);
                }
                _ => {}
            }
        }
        let status = child.wait().expect("Failed to wait on cargo build process");
        if !status.success() {
            process::exit(status.code().unwrap_or(1));
        }
        // create fat binaries for artifacts
        for (x86_64_path, aarch64_path) in x86_64_artifacts
            .into_iter()
            .zip(aarch64_artifacts.into_iter())
        {
            let mut fat = fat_macho::FatWriter::new();
            match fat.add(fs_err::read(&x86_64_path)?) {
                Err(fat_macho::Error::InvalidMachO(_)) => continue,
                Err(e) => return Err(e)?,
                Ok(()) => {}
            }
            match fat.add(fs_err::read(&aarch64_path)?) {
                Err(fat_macho::Error::InvalidMachO(_)) => continue,
                Err(e) => return Err(e)?,
                Ok(()) => {}
            }
            let universal2_path = PathBuf::from(
                x86_64_path
                    .to_string()
                    .replace("x86_64-apple-darwin", "universal2-apple-darwin"),
            );
            let universal2_dir = universal2_path.parent().unwrap();
            fs_err::create_dir_all(universal2_dir)?;
            fat.write_to_file(universal2_path)?;
        }
        Ok(())
    }

    /// Generate cargo subcommand
    #[cfg(not(feature = "universal2"))]
    pub fn build_command(&self) -> Result<Command> {
        let mut build = self.cargo.command();
        if !self.disable_zig_linker {
            Zig::apply_command_env(
                self.manifest_path.as_deref(),
                self.release,
                &self.cargo.common,
                &mut build,
                self.enable_zig_ar,
            )?;
        }
        Ok(build)
    }

    /// Generate cargo subcommand
    #[cfg(feature = "universal2")]
    pub fn build_command(&self) -> Result<Command> {
        let build = if let Some(index) = self
            .cargo
            .target
            .iter()
            .position(|t| t == "universal2-apple-darwin")
        {
            let mut cargo = self.cargo.clone();
            cargo.target.remove(index);
            if !cargo.target.contains(&"x86_64-apple-darwin".to_string()) {
                cargo.target.push("x86_64-apple-darwin".to_string());
            }
            if !cargo.target.contains(&"aarch64-apple-darwin".to_string()) {
                cargo.target.push("aarch64-apple-darwin".to_string());
            }
            if !cargo.message_format.iter().any(|f| f.starts_with("json")) {
                cargo.message_format.push("json".to_string());
            }
            let mut build = cargo.command();
            build.stdout(Stdio::piped()).stderr(Stdio::inherit());
            if !self.disable_zig_linker {
                Zig::apply_command_env(
                    self.manifest_path.as_deref(),
                    self.release,
                    &cargo.common,
                    &mut build,
                    self.enable_zig_ar,
                )?;
            }
            build
        } else {
            let mut build = self.cargo.command();
            if !self.disable_zig_linker {
                Zig::apply_command_env(
                    self.manifest_path.as_deref(),
                    self.release,
                    &self.cargo.common,
                    &mut build,
                    self.enable_zig_ar,
                )?;
            }
            build
        };
        Ok(build)
    }
}

impl Deref for Build {
    type Target = cargo_options::Build;

    fn deref(&self) -> &Self::Target {
        &self.cargo
    }
}

impl DerefMut for Build {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.cargo
    }
}

impl From<cargo_options::Build> for Build {
    fn from(cargo: cargo_options::Build) -> Self {
        Self {
            cargo,
            ..Default::default()
        }
    }
}