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
//! Cargo flags for selecting crates in a workspace.

use std::ffi::OsString;

use crate::{impls::Merge, Args};

#[derive(Default, Clone, Debug, PartialEq, Eq, clap::Args)]
#[non_exhaustive]
pub struct CargoBuild {
    /// Add additional nightly features for optimizing
    #[clap(long)]
    pub optimize: bool,

    /// Build for the target triple
    #[clap(long, name = "TRIPLE")]
    pub target: Option<String>,

    /// Build all targets
    #[clap(long)]
    pub all_targets: bool,

    #[clap(long)]
    pub link_args: bool,

    /// Build artifacts in release mode, with optimizations
    #[clap(long, short = 'r')]
    pub release: bool,

    /// Build artifacts with the specified profile
    #[clap(long, name = "PROFILE_NAME")]
    pub profile: Option<String>,
}

impl CargoBuild {
    pub fn profile(&self) -> &str {
        self.profile
            .as_deref()
            .unwrap_or_else(|| self.release_or_debug())
    }

    fn release_or_debug(&self) -> &str {
        if self.release {
            "release"
        } else {
            "debug"
        }
    }
}

impl Merge for CargoBuild {
    fn merge(&mut self, other: CargoBuild) {
        let CargoBuild {
            optimize,
            target,
            all_targets,
            link_args,
            release,
            profile,
        } = other;
        self.optimize = self.optimize || optimize;
        if self.target.is_none() {
            self.target = target;
        }
        self.all_targets = self.all_targets || all_targets;
        self.link_args = self.link_args || link_args;
        self.release = self.release || release;
        if self.profile.is_none() {
            self.profile = profile;
        }
    }
}

impl Args for CargoBuild {
    fn to_args(&self) -> Vec<OsString> {
        let mut args: Vec<OsString> = Vec::new();
        if self.optimize {
            args.push("-Z=build-std=std,panic_abort".into());
            args.push("-Z=build-std-features=panic_immediate_abort".into());
        }
        if let Some(target) = &self.target {
            args.push("--target".into());
            args.push(target.into());
        }
        if self.all_targets {
            args.push("--all-targets".into());
        }
        if self.release {
            args.push("--release".into());
        }
        if let Some(profile) = &self.profile {
            args.push("--profile".into());
            args.push(profile.into());
        }
        args
    }
}