Skip to main content

cargo_options/
build.rs

1use std::ops::{Deref, DerefMut};
2use std::path::PathBuf;
3use std::process::Command;
4
5use clap::{ArgAction, Parser};
6
7#[cfg(feature = "serde")]
8use serde::{Deserialize, Serialize};
9
10use crate::common::CommonOptions;
11use crate::heading;
12
13/// Compile a local package and all of its dependencies
14#[derive(Clone, Debug, Default, Parser)]
15#[command(
16    display_order = 1,
17    after_help = "Run `cargo help build` for more detailed information."
18)]
19#[group(skip)]
20#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
21pub struct Build {
22    #[command(flatten)]
23    #[cfg_attr(feature = "serde", serde(flatten))]
24    pub common: CommonOptions,
25
26    /// Path to Cargo.toml
27    #[arg(long, value_name = "PATH", help_heading = heading::MANIFEST_OPTIONS)]
28    #[cfg_attr(feature = "serde", serde(default))]
29    pub manifest_path: Option<PathBuf>,
30
31    /// Build artifacts in release mode, with optimizations
32    #[arg(
33        short = 'r',
34        long,
35        conflicts_with = "profile",
36        help_heading = heading::COMPILATION_OPTIONS,
37    )]
38    #[cfg_attr(feature = "serde", serde(default))]
39    pub release: bool,
40
41    /// Ignore `rust-version` specification in packages
42    #[arg(long, help_heading = heading::MANIFEST_OPTIONS)]
43    #[cfg_attr(feature = "serde", serde(default))]
44    pub ignore_rust_version: bool,
45
46    /// Output build graph in JSON (unstable)
47    #[arg(long, help_heading = heading::COMPILATION_OPTIONS)]
48    #[cfg_attr(feature = "serde", serde(default))]
49    pub unit_graph: bool,
50
51    /// Package to build (see `cargo help pkgid`)
52    #[arg(
53        short = 'p',
54        long = "package",
55        value_name = "SPEC",
56        action = ArgAction::Append,
57        num_args=0..=1,
58        help_heading = heading::PACKAGE_SELECTION,
59    )]
60    #[cfg_attr(feature = "serde", serde(default))]
61    pub packages: Vec<String>,
62
63    /// Build all packages in the workspace
64    #[arg(long, help_heading = heading::PACKAGE_SELECTION)]
65    #[cfg_attr(feature = "serde", serde(default))]
66    pub workspace: bool,
67
68    /// Exclude packages from the build
69    #[arg(
70        long,
71        value_name = "SPEC",
72        action = ArgAction::Append,
73        help_heading = heading::PACKAGE_SELECTION,
74    )]
75    #[cfg_attr(feature = "serde", serde(default))]
76    pub exclude: Vec<String>,
77
78    /// Alias for workspace (deprecated)
79    #[arg(long, help_heading = heading::PACKAGE_SELECTION)]
80    #[cfg_attr(feature = "serde", serde(default))]
81    pub all: bool,
82
83    /// Build only this package's library
84    #[arg(long, help_heading = heading::TARGET_SELECTION)]
85    #[cfg_attr(feature = "serde", serde(default))]
86    pub lib: bool,
87
88    /// Build only the specified binary
89    #[arg(
90        long,
91        value_name = "NAME",
92        action = ArgAction::Append,
93        num_args=0..=1,
94        help_heading = heading::TARGET_SELECTION,
95    )]
96    #[cfg_attr(feature = "serde", serde(default))]
97    pub bin: Vec<String>,
98
99    /// Build all binaries
100    #[arg(long, help_heading = heading::TARGET_SELECTION)]
101    #[cfg_attr(feature = "serde", serde(default))]
102    pub bins: bool,
103
104    /// Build only the specified example
105    #[arg(
106        long,
107        value_name = "NAME",
108        action = ArgAction::Append,
109        num_args=0..=1,
110        help_heading = heading::TARGET_SELECTION,
111    )]
112    #[cfg_attr(feature = "serde", serde(default))]
113    pub example: Vec<String>,
114
115    /// Build all examples
116    #[arg(long, help_heading = heading::TARGET_SELECTION)]
117    #[cfg_attr(feature = "serde", serde(default))]
118    pub examples: bool,
119
120    /// Build only the specified test target
121    #[arg(
122        long,
123        value_name = "NAME",
124        action = ArgAction::Append,
125        help_heading = heading::TARGET_SELECTION,
126    )]
127    #[cfg_attr(feature = "serde", serde(default))]
128    pub test: Vec<String>,
129
130    /// Build all targets that have `test = true` set
131    #[arg(long, help_heading = heading::TARGET_SELECTION)]
132    #[cfg_attr(feature = "serde", serde(default))]
133    pub tests: bool,
134
135    /// Build only the specified bench target
136    #[arg(
137        long,
138        value_name = "NAME",
139        action = ArgAction::Append,
140        help_heading = heading::TARGET_SELECTION,
141    )]
142    #[cfg_attr(feature = "serde", serde(default))]
143    pub bench: Vec<String>,
144
145    /// Build all targets that have `bench = true` set
146    #[arg(long, help_heading = heading::TARGET_SELECTION)]
147    #[cfg_attr(feature = "serde", serde(default))]
148    pub benches: bool,
149
150    /// Build all targets
151    #[arg(long, help_heading = heading::TARGET_SELECTION)]
152    #[cfg_attr(feature = "serde", serde(default))]
153    pub all_targets: bool,
154
155    /// Copy final artifacts to this directory (unstable)
156    #[arg(long, value_name = "PATH", help_heading = heading::COMPILATION_OPTIONS)]
157    #[cfg_attr(feature = "serde", serde(default))]
158    pub artifact_dir: Option<PathBuf>,
159
160    /// Build only compile-time dependencies (unstable)
161    #[arg(long, hide = true)]
162    #[cfg_attr(feature = "serde", serde(default))]
163    pub compile_time_deps: bool,
164
165    /// Outputs a future incompatibility report at the end of the build
166    #[arg(long)]
167    #[cfg_attr(feature = "serde", serde(default))]
168    pub future_incompat_report: bool,
169}
170
171impl Build {
172    /// Build a `cargo build` command
173    pub fn command(&self) -> Command {
174        let mut cmd = CommonOptions::cargo_command();
175        cmd.arg("build");
176
177        self.common.apply(&mut cmd);
178
179        if let Some(path) = self.manifest_path.as_ref() {
180            cmd.arg("--manifest-path").arg(path);
181        }
182        if self.release {
183            cmd.arg("--release");
184        }
185        if self.ignore_rust_version {
186            cmd.arg("--ignore-rust-version");
187        }
188        if self.unit_graph {
189            cmd.arg("--unit-graph");
190        }
191        for pkg in &self.packages {
192            cmd.arg("--package").arg(pkg);
193        }
194        if self.workspace {
195            cmd.arg("--workspace");
196        }
197        for item in &self.exclude {
198            cmd.arg("--exclude").arg(item);
199        }
200        if self.all {
201            cmd.arg("--all");
202        }
203        if self.lib {
204            cmd.arg("--lib");
205        }
206        for bin in &self.bin {
207            cmd.arg("--bin").arg(bin);
208        }
209        if self.bins {
210            cmd.arg("--bins");
211        }
212        for example in &self.example {
213            cmd.arg("--example").arg(example);
214        }
215        if self.examples {
216            cmd.arg("--examples");
217        }
218        for test in &self.test {
219            cmd.arg("--test").arg(test);
220        }
221        if self.tests {
222            cmd.arg("--tests");
223        }
224        for bench in &self.bench {
225            cmd.arg("--bench").arg(bench);
226        }
227        if self.benches {
228            cmd.arg("--benches");
229        }
230        if self.all_targets {
231            cmd.arg("--all-targets");
232        }
233        if let Some(dir) = self.artifact_dir.as_ref() {
234            cmd.arg("--artifact-dir").arg(dir);
235        }
236        if self.compile_time_deps {
237            cmd.arg("--compile-time-deps");
238        }
239        if self.future_incompat_report {
240            cmd.arg("--future-incompat-report");
241        }
242
243        cmd
244    }
245}
246
247impl Deref for Build {
248    type Target = CommonOptions;
249
250    fn deref(&self) -> &Self::Target {
251        &self.common
252    }
253}
254
255impl DerefMut for Build {
256    fn deref_mut(&mut self) -> &mut Self::Target {
257        &mut self.common
258    }
259}
260
261#[cfg(test)]
262mod test {
263    use super::Build;
264    use clap::CommandFactory;
265
266    #[test]
267    fn verify_cli() {
268        <Build as CommandFactory>::command().debug_assert()
269    }
270}