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#[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 #[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 #[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 #[arg(long, help_heading = heading::MANIFEST_OPTIONS)]
43 #[cfg_attr(feature = "serde", serde(default))]
44 pub ignore_rust_version: bool,
45
46 #[arg(long, help_heading = heading::COMPILATION_OPTIONS)]
48 #[cfg_attr(feature = "serde", serde(default))]
49 pub unit_graph: bool,
50
51 #[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 #[arg(long, help_heading = heading::PACKAGE_SELECTION)]
65 #[cfg_attr(feature = "serde", serde(default))]
66 pub workspace: bool,
67
68 #[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 #[arg(long, help_heading = heading::PACKAGE_SELECTION)]
80 #[cfg_attr(feature = "serde", serde(default))]
81 pub all: bool,
82
83 #[arg(long, help_heading = heading::TARGET_SELECTION)]
85 #[cfg_attr(feature = "serde", serde(default))]
86 pub lib: bool,
87
88 #[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 #[arg(long, help_heading = heading::TARGET_SELECTION)]
101 #[cfg_attr(feature = "serde", serde(default))]
102 pub bins: bool,
103
104 #[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 #[arg(long, help_heading = heading::TARGET_SELECTION)]
117 #[cfg_attr(feature = "serde", serde(default))]
118 pub examples: bool,
119
120 #[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 #[arg(long, help_heading = heading::TARGET_SELECTION)]
132 #[cfg_attr(feature = "serde", serde(default))]
133 pub tests: bool,
134
135 #[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 #[arg(long, help_heading = heading::TARGET_SELECTION)]
147 #[cfg_attr(feature = "serde", serde(default))]
148 pub benches: bool,
149
150 #[arg(long, help_heading = heading::TARGET_SELECTION)]
152 #[cfg_attr(feature = "serde", serde(default))]
153 pub all_targets: bool,
154
155 #[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 #[arg(long, hide = true)]
162 #[cfg_attr(feature = "serde", serde(default))]
163 pub compile_time_deps: bool,
164
165 #[arg(long)]
167 #[cfg_attr(feature = "serde", serde(default))]
168 pub future_incompat_report: bool,
169}
170
171impl Build {
172 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}