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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use std::cell::RefCell;
use serde::ser;
use crate::core::compiler::{CompileKind, CompileTarget};
use crate::util::ProcessBuilder;
use crate::util::{CargoResult, Config, RustfixDiagnosticServer};
#[derive(Debug, Clone)]
pub enum ProfileKind {
Dev,
Release,
Custom(String),
}
impl ProfileKind {
pub fn name(&self) -> &str {
match self {
ProfileKind::Dev => "dev",
ProfileKind::Release => "release",
ProfileKind::Custom(name) => name,
}
}
}
#[derive(Debug)]
pub struct BuildConfig {
pub requested_kind: CompileKind,
pub jobs: u32,
pub profile_kind: ProfileKind,
pub mode: CompileMode,
pub message_format: MessageFormat,
pub force_rebuild: bool,
pub build_plan: bool,
pub primary_unit_rustc: Option<ProcessBuilder>,
pub rustfix_diagnostic_server: RefCell<Option<RustfixDiagnosticServer>>,
}
impl BuildConfig {
pub fn new(
config: &Config,
jobs: Option<u32>,
requested_target: &Option<String>,
mode: CompileMode,
) -> CargoResult<BuildConfig> {
let cfg = config.build_config()?;
let requested_kind = match requested_target {
Some(s) => CompileKind::Target(CompileTarget::new(s)?),
None => match &cfg.target {
Some(val) => {
let value = if val.raw_value().ends_with(".json") {
let path = val.clone().resolve_path(config);
path.to_str().expect("must be utf-8 in toml").to_string()
} else {
val.raw_value().to_string()
};
CompileKind::Target(CompileTarget::new(&value)?)
}
None => CompileKind::Host,
},
};
if jobs == Some(0) {
failure::bail!("jobs must be at least 1")
}
if jobs.is_some() && config.jobserver_from_env().is_some() {
config.shell().warn(
"a `-j` argument was passed to Cargo but Cargo is \
also configured with an external jobserver in \
its environment, ignoring the `-j` parameter",
)?;
}
let jobs = jobs.or(cfg.jobs).unwrap_or(::num_cpus::get() as u32);
Ok(BuildConfig {
requested_kind,
jobs,
profile_kind: ProfileKind::Dev,
mode,
message_format: MessageFormat::Human,
force_rebuild: false,
build_plan: false,
primary_unit_rustc: None,
rustfix_diagnostic_server: RefCell::new(None),
})
}
pub fn emit_json(&self) -> bool {
match self.message_format {
MessageFormat::Json { .. } => true,
_ => false,
}
}
pub fn profile_name(&self) -> &str {
self.profile_kind.name()
}
pub fn test(&self) -> bool {
self.mode == CompileMode::Test || self.mode == CompileMode::Bench
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MessageFormat {
Human,
Json {
render_diagnostics: bool,
short: bool,
ansi: bool,
},
Short,
}
#[derive(Clone, Copy, PartialEq, Debug, Eq, Hash, PartialOrd, Ord)]
pub enum CompileMode {
Test,
Build,
Check { test: bool },
Bench,
Doc { deps: bool },
Doctest,
RunCustomBuild,
}
impl ser::Serialize for CompileMode {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
use self::CompileMode::*;
match *self {
Test => "test".serialize(s),
Build => "build".serialize(s),
Check { .. } => "check".serialize(s),
Bench => "bench".serialize(s),
Doc { .. } => "doc".serialize(s),
Doctest => "doctest".serialize(s),
RunCustomBuild => "run-custom-build".serialize(s),
}
}
}
impl CompileMode {
pub fn is_check(self) -> bool {
match self {
CompileMode::Check { .. } => true,
_ => false,
}
}
pub fn is_doc(self) -> bool {
match self {
CompileMode::Doc { .. } => true,
_ => false,
}
}
pub fn is_doc_test(self) -> bool {
self == CompileMode::Doctest
}
pub fn is_any_test(self) -> bool {
match self {
CompileMode::Test
| CompileMode::Bench
| CompileMode::Check { test: true }
| CompileMode::Doctest => true,
_ => false,
}
}
pub fn is_rustc_test(self) -> bool {
match self {
CompileMode::Test | CompileMode::Bench | CompileMode::Check { test: true } => true,
_ => false,
}
}
pub fn is_run_custom_build(self) -> bool {
self == CompileMode::RunCustomBuild
}
pub fn all_modes() -> &'static [CompileMode] {
static ALL: [CompileMode; 9] = [
CompileMode::Test,
CompileMode::Build,
CompileMode::Check { test: true },
CompileMode::Check { test: false },
CompileMode::Bench,
CompileMode::Doc { deps: true },
CompileMode::Doc { deps: false },
CompileMode::Doctest,
CompileMode::RunCustomBuild,
];
&ALL
}
}