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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
use std::env;
use std::ops::{Deref, DerefMut};
use std::path::{Path, PathBuf};
use std::process::{self, Command};

use anyhow::{Context, Result};
use clap::Parser;
use fs_err as fs;
use path_slash::PathBufExt;

use crate::linux::ARM_FEATURES_H;
use crate::macos::LIBICONV_TBD;
use crate::zig::{is_mingw_shell, prepare_zig_linker, Zig};

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

    /// Disable zig linker
    #[clap(skip)]
    pub disable_zig_linker: 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 mut build = self.build_command("build")?;
        let mut child = build.spawn().context("Failed to run cargo build")?;
        let status = child.wait().expect("Failed to wait on cargo build process");
        if !status.success() {
            process::exit(status.code().unwrap_or(1));
        }
        Ok(())
    }

    /// Generate cargo subcommand
    pub fn build_command(&self, subcommand: &str) -> Result<Command> {
        let mut build = Command::new("cargo");
        build.arg(subcommand);

        let rust_targets = self
            .cargo
            .target
            .iter()
            .map(|target| target.split_once('.').map(|(t, _)| t).unwrap_or(target))
            .collect::<Vec<&str>>();

        // collect cargo build arguments
        if self.cargo.quiet {
            build.arg("--quiet");
        }
        for pkg in &self.cargo.packages {
            build.arg("--package").arg(pkg);
        }
        if self.cargo.workspace {
            build.arg("--workspace");
        }
        for item in &self.cargo.exclude {
            build.arg("--exclude").arg(item);
        }
        if self.cargo.all {
            build.arg("--all");
        }
        if let Some(jobs) = self.cargo.jobs {
            build.arg("--jobs").arg(jobs.to_string());
        }
        if self.cargo.lib {
            build.arg("--lib");
        }
        for bin in &self.cargo.bin {
            build.arg("--bin").arg(bin);
        }
        if self.cargo.bins {
            build.arg("--bins");
        }
        for example in &self.cargo.example {
            build.arg("--example").arg(example);
        }
        if self.cargo.examples {
            build.arg("--examples");
        }
        for test in &self.cargo.test {
            build.arg("--test").arg(test);
        }
        if self.cargo.tests {
            build.arg("--tests");
        }
        for bench in &self.cargo.bench {
            build.arg("--bench").arg(bench);
        }
        if self.cargo.benches {
            build.arg("--benches");
        }
        if self.cargo.all_targets {
            build.arg("--all-targets");
        }
        if self.cargo.release {
            build.arg("--release");
        }
        if let Some(profile) = self.cargo.profile.as_ref() {
            build.arg("--profile").arg(profile);
        }
        for feature in &self.cargo.features {
            build.arg("--features").arg(feature);
        }
        if self.cargo.all_features {
            build.arg("--all-features");
        }
        if self.cargo.no_default_features {
            build.arg("--no-default-features");
        }

        rust_targets.iter().for_each(|target| {
            build.arg("--target").arg(&target);
        });

        if let Some(dir) = self.cargo.target_dir.as_ref() {
            build.arg("--target-dir").arg(dir);
        }
        if let Some(dir) = self.cargo.out_dir.as_ref() {
            build.arg("--out-dir").arg(dir);
        }
        if let Some(path) = self.cargo.manifest_path.as_ref() {
            build.arg("--manifest-path").arg(path);
        }
        if self.cargo.ignore_rust_version {
            build.arg("--ignore-rust-version");
        }
        for fmt in &self.cargo.message_format {
            build.arg("--message-format").arg(fmt);
        }
        if self.cargo.build_plan {
            build.arg("--build-plan");
        }
        if self.cargo.unit_graph {
            build.arg("--unit-graph");
        }
        if self.cargo.future_incompat_report {
            build.arg("--future-incompat-report");
        }
        if self.cargo.verbose > 0 {
            build.arg(format!("-{}", "v".repeat(self.cargo.verbose)));
        }
        if let Some(color) = self.cargo.color.as_ref() {
            build.arg("--color").arg(color);
        }
        if self.cargo.frozen {
            build.arg("--frozen");
        }
        if self.cargo.locked {
            build.arg("--locked");
        }
        if self.cargo.offline {
            build.arg("--offline");
        }
        for config in &self.cargo.config {
            build.arg("--config").arg(config);
        }
        for flag in &self.cargo.unstable_flags {
            build.arg("-Z").arg(flag);
        }
        if let Some(timings) = &self.cargo.timings {
            if timings.is_empty() {
                build.arg("--timings");
            } else {
                let timings: Vec<_> = timings.iter().map(|x| x.as_str()).collect();
                build.arg(format!("--timings={}", timings.join(",")));
            }
        }

        if !self.disable_zig_linker {
            // setup zig as linker
            let rustc_meta = rustc_version::version_meta()?;
            let host_target = &rustc_meta.host;
            for (parsed_target, raw_target) in rust_targets.iter().zip(&self.cargo.target) {
                // we only setup zig as linker when target isn't exactly the same as host target
                if host_target != raw_target {
                    let env_target = parsed_target.replace('-', "_");
                    let (zig_cc, zig_cxx) = prepare_zig_linker(raw_target)?;
                    if is_mingw_shell() {
                        let zig_cc = zig_cc.to_slash_lossy();
                        build.env(format!("CC_{}", env_target), &zig_cc);
                        build.env(format!("CXX_{}", env_target), &zig_cxx.to_slash_lossy());
                        build.env(
                            format!("CARGO_TARGET_{}_LINKER", env_target.to_uppercase()),
                            &zig_cc,
                        );
                    } else {
                        build.env(format!("CC_{}", env_target), &zig_cc);
                        build.env(format!("CXX_{}", env_target), &zig_cxx);
                        build.env(
                            format!("CARGO_TARGET_{}_LINKER", env_target.to_uppercase()),
                            &zig_cc,
                        );
                    }

                    self.setup_os_deps()?;

                    if raw_target.contains("windows-gnu") {
                        build.env("WINAPI_NO_BUNDLED_LIBRARIES", "1");
                    }

                    if raw_target.contains("apple-darwin") {
                        if let Some(sdkroot) = env::var_os("SDKROOT") {
                            if !sdkroot.is_empty()
                                && env::var_os("PKG_CONFIG_SYSROOT_DIR").is_none()
                            {
                                // Set PKG_CONFIG_SYSROOT_DIR for pkg-config crate
                                build.env("PKG_CONFIG_SYSROOT_DIR", sdkroot);
                            }
                        }
                    }

                    // Enable unstable `target-applies-to-host` option automatically
                    // when target is the same as host but may have specified glibc version
                    if host_target == parsed_target {
                        if !matches!(rustc_meta.channel, rustc_version::Channel::Nightly) {
                            // Hack to use the unstable feature on stable Rust
                            // https://github.com/rust-lang/cargo/pull/9753#issuecomment-1022919343
                            build.env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "nightly");
                        }
                        build.env("CARGO_UNSTABLE_TARGET_APPLIES_TO_HOST", "true");
                        build.env("CARGO_TARGET_APPLIES_TO_HOST", "false");
                    }
                }
            }
        }

        Ok(build)
    }

    fn setup_os_deps(&self) -> Result<()> {
        for target in &self.cargo.target {
            if target.contains("apple") {
                let target_dir = if let Some(target_dir) = self.cargo.target_dir.clone() {
                    target_dir.join(target)
                } else {
                    let manifest_path = self
                        .cargo
                        .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()?;
                    metadata.target_directory.into_std_path_buf().join(target)
                };
                let profile = match self.cargo.profile.as_deref() {
                    Some("dev" | "test") => "debug",
                    Some("release" | "bench") => "release",
                    Some(profile) => profile,
                    None => {
                        if self.cargo.release {
                            "release"
                        } else {
                            "debug"
                        }
                    }
                };
                let deps_dir = target_dir.join(profile).join("deps");
                fs::create_dir_all(&deps_dir)?;
                fs::write(deps_dir.join("libiconv.tbd"), LIBICONV_TBD)?;
            } else if target.contains("arm") && target.contains("linux") {
                // See https://github.com/ziglang/zig/issues/3287
                if let Ok(lib_dir) = Zig::lib_dir() {
                    let arm_features_h = lib_dir
                        .join("libc")
                        .join("glibc")
                        .join("sysdeps")
                        .join("arm")
                        .join("arm-features.h");
                    if !arm_features_h.is_file() {
                        fs::write(arm_features_h, ARM_FEATURES_H)?;
                    }
                }
            }
        }
        Ok(())
    }
}

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,
            disable_zig_linker: false,
        }
    }
}