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
use std::ops::{Deref, DerefMut};
use std::path::PathBuf;
use std::process::{self, Command};
use anyhow::{Context, Result};
use clap::Parser;
use crate::zig::Zig;
#[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,
#[arg(skip)]
pub disable_zig_linker: bool,
#[arg(skip)]
pub enable_zig_ar: bool,
}
impl Build {
#[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
}
pub fn execute(&self) -> Result<()> {
let mut build = self.build_command()?;
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(())
}
pub fn build_command(&self) -> Result<Command> {
let mut build = self.cargo.command();
if !self.disable_zig_linker {
Zig::apply_command_env(&self.cargo.common, &mut build, self.enable_zig_ar)?;
}
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()
}
}
}