cargo_zigbuild/
build.rs

1use std::ops::{Deref, DerefMut};
2use std::path::PathBuf;
3#[cfg(feature = "universal2")]
4use std::process::Stdio;
5use std::process::{self, Child, Command};
6
7use anyhow::{Context, Result};
8use clap::Parser;
9
10use crate::zig::Zig;
11
12/// Compile a local package and all of its dependencies
13/// using zig as the linker
14#[derive(Clone, Debug, Default, Parser)]
15#[command(
16    after_help = "Run `cargo help build` for more detailed information.",
17    display_order = 1
18)]
19pub struct Build {
20    #[command(flatten)]
21    pub cargo: cargo_options::Build,
22
23    /// Disable zig linker
24    #[arg(skip)]
25    pub disable_zig_linker: bool,
26
27    /// Enable zig ar
28    #[arg(skip)]
29    pub enable_zig_ar: bool,
30}
31
32impl Build {
33    /// Create a new build from manifest path
34    #[allow(clippy::field_reassign_with_default)]
35    pub fn new(manifest_path: Option<PathBuf>) -> Self {
36        let mut build = Self::default();
37        build.manifest_path = manifest_path;
38        build
39    }
40
41    /// Execute `cargo build` command with zig as the linker
42    pub fn execute(&self) -> Result<()> {
43        let has_universal2 = self
44            .cargo
45            .target
46            .contains(&"universal2-apple-darwin".to_string());
47        let mut build = self.build_command()?;
48        let mut child = build.spawn().context("Failed to run cargo build")?;
49        if has_universal2 {
50            self.handle_universal2_build(child)?;
51        } else {
52            let status = child.wait().expect("Failed to wait on cargo build process");
53            if !status.success() {
54                process::exit(status.code().unwrap_or(1));
55            }
56        }
57        Ok(())
58    }
59
60    #[cfg(not(feature = "universal2"))]
61    fn handle_universal2_build(&self, mut _child: Child) -> Result<()> {
62        anyhow::bail!("Unsupported Rust target: universal2-apple-darwin")
63    }
64
65    #[cfg(feature = "universal2")]
66    fn handle_universal2_build(&self, mut child: Child) -> Result<()> {
67        use cargo_metadata::Message;
68        use std::io::BufReader;
69        use std::path::Path;
70
71        // Find root crate package id
72        let manifest_path = self
73            .manifest_path
74            .as_deref()
75            .unwrap_or_else(|| Path::new("Cargo.toml"));
76        let mut metadata_cmd = cargo_metadata::MetadataCommand::new();
77        metadata_cmd.manifest_path(manifest_path);
78        let metadata = metadata_cmd.exec()?;
79        let root_pkg = metadata.root_package().expect("Should have a root package");
80
81        let mut x86_64_artifacts = Vec::new();
82        let mut aarch64_artifacts = Vec::new();
83
84        let stream = child
85            .stdout
86            .take()
87            .expect("Cargo build should have a stdout");
88        for message in Message::parse_stream(BufReader::new(stream)) {
89            let message = message.context("Failed to parse cargo metadata message")?;
90            match message {
91                Message::CompilerArtifact(artifact) => {
92                    if artifact.package_id == root_pkg.id {
93                        for filename in artifact.filenames {
94                            if filename.as_str().contains("x86_64-apple-darwin") {
95                                x86_64_artifacts.push(filename);
96                            } else if filename.as_str().contains("aarch64-apple-darwin") {
97                                aarch64_artifacts.push(filename);
98                            }
99                        }
100                    }
101                }
102                Message::CompilerMessage(msg) => {
103                    println!("{}", msg.message);
104                }
105                _ => {}
106            }
107        }
108        let status = child.wait().expect("Failed to wait on cargo build process");
109        if !status.success() {
110            process::exit(status.code().unwrap_or(1));
111        }
112        // create fat binaries for artifacts
113        for (x86_64_path, aarch64_path) in x86_64_artifacts
114            .into_iter()
115            .zip(aarch64_artifacts.into_iter())
116        {
117            let mut fat = fat_macho::FatWriter::new();
118            match fat.add(fs_err::read(&x86_64_path)?) {
119                Err(fat_macho::Error::InvalidMachO(_)) => continue,
120                Err(e) => return Err(e)?,
121                Ok(()) => {}
122            }
123            match fat.add(fs_err::read(&aarch64_path)?) {
124                Err(fat_macho::Error::InvalidMachO(_)) => continue,
125                Err(e) => return Err(e)?,
126                Ok(()) => {}
127            }
128            let universal2_path = PathBuf::from(
129                x86_64_path
130                    .to_string()
131                    .replace("x86_64-apple-darwin", "universal2-apple-darwin"),
132            );
133            let universal2_dir = universal2_path.parent().unwrap();
134            fs_err::create_dir_all(universal2_dir)?;
135            fat.write_to_file(universal2_path)?;
136        }
137        Ok(())
138    }
139
140    /// Generate cargo subcommand
141    #[cfg(not(feature = "universal2"))]
142    pub fn build_command(&self) -> Result<Command> {
143        let mut build = self.cargo.command();
144        if !self.disable_zig_linker {
145            Zig::apply_command_env(
146                self.manifest_path.as_deref(),
147                self.release,
148                &self.cargo.common,
149                &mut build,
150                self.enable_zig_ar,
151            )?;
152        }
153        Ok(build)
154    }
155
156    /// Generate cargo subcommand
157    #[cfg(feature = "universal2")]
158    pub fn build_command(&self) -> Result<Command> {
159        let build = if let Some(index) = self
160            .cargo
161            .target
162            .iter()
163            .position(|t| t == "universal2-apple-darwin")
164        {
165            let mut cargo = self.cargo.clone();
166            cargo.target.remove(index);
167            if !cargo.target.contains(&"x86_64-apple-darwin".to_string()) {
168                cargo.target.push("x86_64-apple-darwin".to_string());
169            }
170            if !cargo.target.contains(&"aarch64-apple-darwin".to_string()) {
171                cargo.target.push("aarch64-apple-darwin".to_string());
172            }
173            if !cargo.message_format.iter().any(|f| f.starts_with("json")) {
174                cargo.message_format.push("json".to_string());
175            }
176            let mut build = cargo.command();
177            build.stdout(Stdio::piped()).stderr(Stdio::inherit());
178            if !self.disable_zig_linker {
179                Zig::apply_command_env(
180                    self.manifest_path.as_deref(),
181                    self.release,
182                    &cargo.common,
183                    &mut build,
184                    self.enable_zig_ar,
185                )?;
186            }
187            build
188        } else {
189            let mut build = self.cargo.command();
190            if !self.disable_zig_linker {
191                Zig::apply_command_env(
192                    self.manifest_path.as_deref(),
193                    self.release,
194                    &self.cargo.common,
195                    &mut build,
196                    self.enable_zig_ar,
197                )?;
198            }
199            build
200        };
201        Ok(build)
202    }
203}
204
205impl Deref for Build {
206    type Target = cargo_options::Build;
207
208    fn deref(&self) -> &Self::Target {
209        &self.cargo
210    }
211}
212
213impl DerefMut for Build {
214    fn deref_mut(&mut self) -> &mut Self::Target {
215        &mut self.cargo
216    }
217}
218
219impl From<cargo_options::Build> for Build {
220    fn from(cargo: cargo_options::Build) -> Self {
221        Self {
222            cargo,
223            ..Default::default()
224        }
225    }
226}