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
use std::path::PathBuf;
use std::process;
use anyhow::{Context, Result};
use clap::Parser;
use crate::build::Build;
#[derive(Clone, Debug, Default, Parser)]
#[clap(
setting = clap::AppSettings::DeriveDisplayOrder,
trailing_var_arg = true,
after_help = "Run `cargo help rustc` for more detailed information."
)]
pub struct Rustc {
#[clap(short = 'q', long)]
pub quiet: bool,
#[clap(short = 'p', long = "package", value_name = "SPEC")]
pub packages: Vec<String>,
#[clap(short = 'j', long, value_name = "N")]
pub jobs: Option<usize>,
#[clap(long)]
pub lib: bool,
#[clap(long, value_name = "NAME", multiple_values = true)]
pub bin: Vec<String>,
#[clap(long)]
pub bins: bool,
#[clap(long, value_name = "NAME", multiple_values = true)]
pub example: Vec<String>,
#[clap(long)]
pub examples: bool,
#[clap(long, value_name = "NAME", multiple_values = true)]
pub test: Vec<String>,
#[clap(long)]
pub tests: bool,
#[clap(long, value_name = "NAME", multiple_values = true)]
pub bench: Vec<String>,
#[clap(long)]
pub benches: bool,
#[clap(long)]
pub all_targets: bool,
#[clap(short = 'r', long)]
pub release: bool,
#[clap(long, value_name = "PROFILE-NAME")]
pub profile: Option<String>,
#[clap(long, multiple_values = true)]
pub features: Vec<String>,
#[clap(long)]
pub all_features: bool,
#[clap(long)]
pub no_default_features: bool,
#[clap(
long,
value_name = "TRIPLE",
env = "CARGO_BUILD_TARGET",
multiple_occurrences = true
)]
pub target: Vec<String>,
#[clap(long, value_name = "INFO")]
pub print: Option<String>,
#[clap(
long,
value_name = "CRATE-TYPE",
use_value_delimiter = true,
multiple_values = true
)]
pub crate_type: Vec<String>,
#[clap(long, value_name = "DIRECTORY", parse(from_os_str))]
pub target_dir: Option<PathBuf>,
#[clap(long, value_name = "PATH", parse(from_os_str))]
pub manifest_path: Option<PathBuf>,
#[clap(long)]
pub ignore_rust_version: bool,
#[clap(long, value_name = "FMT", multiple_values = true)]
pub message_format: Vec<String>,
#[clap(long)]
pub unit_graph: bool,
#[clap(long)]
pub future_incompat_report: bool,
#[clap(short = 'v', long, parse(from_occurrences), max_occurrences = 2)]
pub verbose: usize,
#[clap(long, value_name = "WHEN")]
pub color: Option<String>,
#[clap(long)]
pub frozen: bool,
#[clap(long)]
pub locked: bool,
#[clap(long)]
pub offline: bool,
#[clap(long, value_name = "KEY=VALUE", multiple_values = true)]
pub config: Vec<String>,
#[clap(short = 'Z', value_name = "FLAG", multiple_values = true)]
pub unstable_flags: Vec<String>,
#[clap(takes_value = true, multiple_values = true)]
pub args: Vec<String>,
}
impl Rustc {
pub fn execute(&self) -> Result<()> {
let build = Build {
quiet: self.quiet,
packages: self.packages.clone(),
jobs: self.jobs,
lib: self.lib,
bin: self.bin.clone(),
bins: self.bins,
example: self.example.clone(),
examples: self.examples,
test: self.test.clone(),
tests: self.tests,
bench: self.bench.clone(),
benches: self.benches,
all_targets: self.all_targets,
release: self.release,
profile: self.profile.clone(),
features: self.features.clone(),
all_features: self.all_features,
no_default_features: self.no_default_features,
target: self.target.clone(),
target_dir: self.target_dir.clone(),
manifest_path: self.manifest_path.clone(),
ignore_rust_version: self.ignore_rust_version,
message_format: self.message_format.clone(),
unit_graph: self.unit_graph,
future_incompat_report: self.future_incompat_report,
verbose: self.verbose,
color: self.color.clone(),
frozen: self.frozen,
locked: self.locked,
offline: self.offline,
config: self.config.clone(),
unstable_flags: self.unstable_flags.clone(),
..Default::default()
};
let mut rustc = build.build_command("rustc")?;
if let Some(print) = self.print.as_ref() {
rustc.arg("--print").arg(print);
}
if !self.crate_type.is_empty() {
rustc.arg("--crate-type").arg(self.crate_type.join(","));
}
if !self.args.is_empty() {
rustc.arg("--").args(&self.args);
}
let mut child = rustc.spawn().context("Failed to run cargo rustc")?;
let status = child.wait().expect("Failed to wait on cargo build process");
if !status.success() {
process::exit(status.code().unwrap_or(1));
}
Ok(())
}
}