-
-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathmain.rs
More file actions
461 lines (407 loc) · 13.3 KB
/
main.rs
File metadata and controls
461 lines (407 loc) · 13.3 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// SPDX-License-Identifier: MIT OR Apache-2.0
#![allow(clippy::collapsible_if)]
mod arch;
mod cargo;
mod check_raw;
mod device_path;
mod disk;
mod net;
mod opt;
mod pipe;
mod platform;
mod qemu;
mod tpm;
mod util;
use crate::opt::{FmtOpt, TestOpt};
use anyhow::{Result, bail};
use arch::UefiArch;
use cargo::{Cargo, CargoAction, Feature, Package, TargetTypes};
use clap::Parser;
use itertools::Itertools;
use log::{LevelFilter, Metadata, Record};
use opt::{Action, BuildOpt, ClippyOpt, CovOpt, DocOpt, Opt, QemuOpt, TpmVersion};
use std::process::Command;
use util::run_cmd;
/// Builds various feature permutations that are valid for the `uefi` crate.
fn build_feature_permutations(opt: &BuildOpt) -> Result<()> {
let all_package_features = Feature::package_features(Package::Uefi);
for features in all_package_features.iter().powerset() {
let features = features.iter().map(|f| **f).collect();
let cargo = Cargo {
action: CargoAction::Build,
features,
packages: vec![Package::Uefi],
release: opt.build_mode.release,
target: Some(*opt.target),
warnings_as_errors: true,
target_types: TargetTypes::BinsExamplesLib,
};
run_cmd(cargo.command()?)?;
}
Ok(())
}
fn build(opt: &BuildOpt) -> Result<()> {
if opt.feature_permutations {
return build_feature_permutations(opt);
}
let cargo = Cargo {
action: CargoAction::Build,
features: Feature::more_code(false, true),
packages: Package::all_except_xtask(),
release: opt.build_mode.release,
target: Some(*opt.target),
warnings_as_errors: false,
target_types: TargetTypes::BinsExamplesLib,
};
run_cmd(cargo.command()?)
}
fn clippy(opt: &ClippyOpt) -> Result<()> {
// Run clippy on all the UEFI packages.
let cargo = Cargo {
action: CargoAction::Clippy,
features: Feature::more_code(false, true),
packages: Package::all_except_xtask(),
release: false,
target: Some(*opt.target),
warnings_as_errors: opt.warning.warnings_as_errors,
target_types: TargetTypes::BinsExamplesLib,
};
run_cmd(cargo.command()?)?;
// Run clippy on xtask.
let cargo = Cargo {
action: CargoAction::Clippy,
features: Vec::new(),
packages: vec![Package::Xtask],
release: false,
target: None,
warnings_as_errors: opt.warning.warnings_as_errors,
target_types: TargetTypes::Default,
};
run_cmd(cargo.command()?)
}
/// Generate a code coverage report.
fn code_coverage(opt: &CovOpt) -> Result<()> {
if has_cmd("cargo-llvm-cov") {
let cargo = Cargo {
action: CargoAction::Coverage {
lcov: opt.lcov,
open: opt.open,
},
features: Feature::more_code(*opt.unstable, false),
// Leave out uefi-macros; the compilation tests will just make
// things slower without contributing anything to the coverage
// report.
packages: vec![Package::UefiRaw, Package::Uefi],
release: false,
target: None,
warnings_as_errors: false,
target_types: TargetTypes::Default,
};
run_cmd(cargo.command()?)
} else {
bail!("cargo-llvm-cov not found, see https://github.com/taiki-e/cargo-llvm-cov");
}
}
/// Build docs.
fn doc(opt: &DocOpt) -> Result<()> {
let cargo = Cargo {
action: CargoAction::Doc {
open: opt.open,
document_private_items: opt.document_private_items,
},
features: Feature::more_code(*opt.unstable, true),
packages: Package::published(),
release: false,
target: None,
warnings_as_errors: opt.warning.warnings_as_errors,
target_types: TargetTypes::Default,
};
run_cmd(cargo.command()?)
}
/// Run unit tests and doctests under Miri.
fn run_miri() -> Result<()> {
let cargo = Cargo {
action: CargoAction::Miri,
features: [Feature::Alloc].into(),
packages: [Package::Uefi].into(),
release: false,
target: None,
warnings_as_errors: false,
target_types: TargetTypes::Default,
};
run_cmd(cargo.command()?)
}
/// Build uefi-test-runner and run it in QEMU.
fn run_vm_tests(opt: &QemuOpt) -> Result<()> {
let mut features = vec![];
// Enable the DebugSupport test on supported platforms. Not available on
// AARCH64 since edk2 commit f4213fed34.
if *opt.target != UefiArch::AArch64 {
features.push(Feature::DebugSupport);
}
// Enable the PXE test unless networking is disabled
if !opt.disable_network {
features.push(Feature::Pxe);
}
// Enable TPM tests if a TPM device is present.
match opt.tpm {
Some(TpmVersion::V1) => features.push(Feature::TpmV1),
Some(TpmVersion::V2) => features.push(Feature::TpmV2),
None => {}
}
// Enable the multi-processor test if not targeting AARCH64, and if KVM is
// available. KVM is available on Linux generally, but not in our CI.
if *opt.target != UefiArch::AArch64 && platform::is_linux() && !opt.ci {
features.push(Feature::MultiProcessor);
}
// Enable `unstable` if requested.
if *opt.unstable {
features.push(Feature::TestUnstable);
}
// Build uefi-test-runner.
let cargo = Cargo {
action: CargoAction::Build,
features,
packages: vec![Package::UefiTestRunner],
release: opt.build_mode.release,
target: Some(*opt.target),
warnings_as_errors: false,
target_types: TargetTypes::BinsExamples,
};
run_cmd(cargo.command()?)?;
qemu::run_qemu(*opt.target, opt)
}
/// Run unit tests and doctests on the host. Most of uefi-rs is tested
/// with VM tests, but a few things like macros and data types can be
/// tested with regular tests.
fn run_host_tests(test_opt: &TestOpt) -> Result<()> {
// Run xtask tests.
let cargo = Cargo {
action: CargoAction::Test,
features: Vec::new(),
packages: vec![Package::Xtask],
release: false,
target: None,
warnings_as_errors: false,
target_types: TargetTypes::Default,
};
run_cmd(cargo.command()?)?;
let mut packages = vec![Package::UefiRaw, Package::Uefi];
if !test_opt.skip_macro_tests {
packages.push(Package::UefiMacros);
}
// Run uefi-rs and uefi-macros tests with `unstable` feature.
let cargo = Cargo {
action: CargoAction::Test,
// Some tests may behave differently depending on the unstable feature.
features: Feature::more_code(*test_opt.unstable, false),
packages,
release: false,
// Use the host target so that tests can run without a VM.
target: None,
warnings_as_errors: false,
target_types: TargetTypes::Default,
};
run_cmd(cargo.command()?)
}
/// Formats the project: nix, rust, and yml.
fn run_fmt_project(fmt_opt: &FmtOpt) -> Result<()> {
let mut any_errors = false;
{
eprintln!("Formatting: file headers");
match format_file_headers(fmt_opt) {
Ok(_) => {
eprintln!("✅ expected file headers present")
}
Err(e) => {
if fmt_opt.check {
eprintln!("❌ {e:#?}");
} else {
eprintln!("❌ rust formatter failed: {e:#?}");
}
any_errors = true;
}
}
}
// fmt rust
{
eprintln!("Formatting: rust");
let mut command = Command::new("cargo");
command.arg("fmt");
if fmt_opt.check {
command.arg("--check");
}
command
.arg("--all")
.arg("--")
.arg("--config")
.arg("imports_granularity=Module");
match run_cmd(command) {
Ok(_) => {
eprintln!("✅ rust files formatted")
}
Err(e) => {
if fmt_opt.check {
eprintln!("❌ rust files do not pass check");
} else {
eprintln!("❌ rust formatter failed: {e:#?}");
}
any_errors = true;
}
}
}
// fmt yml
if has_cmd("yamlfmt") {
eprintln!("Formatting: yml");
let mut command = Command::new("yamlfmt");
if fmt_opt.check {
command.arg("-lint");
}
// We only have yml files here.
command.arg(".github");
match run_cmd(command) {
Ok(_) => {
eprintln!("✅ yml files formatted")
}
Err(e) => {
if fmt_opt.check {
eprintln!("❌ yml files do not pass check");
} else {
eprintln!("❌ yml formatter failed: {e:#?}");
}
any_errors = true;
}
}
} else {
eprintln!("Formatting: yml - SKIPPED");
}
// fmt nix (by passing files as arguments)
// `find . -name "*.nix" -type f -exec nix fmt {} \+`
if has_cmd("find") && has_cmd("nixfmt") {
eprintln!("Formatting: nix");
let mut command = Command::new("find");
command.arg(".");
command.arg("-name");
command.arg("*.nix");
command.arg("-type");
command.arg("f");
command.arg("-exec");
command.arg("nixfmt");
if fmt_opt.check {
command.arg("--check");
}
command.arg("{}");
command.arg("+");
match run_cmd(command) {
Ok(_) => {
eprintln!("✅ nix files formatted")
}
Err(e) => {
if fmt_opt.check {
eprintln!("❌ nix files do not pass check");
} else {
eprintln!("❌ nix formatter failed: {e:#?}");
}
any_errors = true;
}
}
} else {
eprintln!("Formatting: nix - SKIPPED");
}
if any_errors {
bail!("one or more formatting errors");
}
Ok(())
}
/// Check that SPDX file headers are present.
///
/// If not in check mode, add any missing headers.
fn format_file_headers(fmt_opt: &FmtOpt) -> Result<()> {
const EXPECTED_HEADER: &str = "// SPDX-License-Identifier: MIT OR Apache-2.0\n\n";
// Path prefixes that should not be checked/formatted.
const EXCLUDE_PATH_PREFIXES: &[&str] = &[
// A user copying the template or uefi-std-example does not need to use
// our license.
"template/src/main.rs",
"uefi-std-example/src/main.rs",
// This directory contains short code snippets used in `trybuild` tests,
// no license needed.
"uefi-macros/tests/ui/",
];
// Recursively get Rust files
let mut cmd = Command::new("git");
cmd.args(["ls-files", "*.rs"]);
let output = cmd.output()?;
if !output.status.success() {
bail!("command failed: {}", output.status);
}
let mut paths: Vec<&str> = std::str::from_utf8(&output.stdout)?.lines().collect();
// Filter out excluded paths.
paths.retain(|path| {
!EXCLUDE_PATH_PREFIXES
.iter()
.any(|prefix| path.starts_with(prefix))
});
// Paths that are missing the file header (only used in check mode).
let mut missing = Vec::new();
// Format or check each path.
for path in paths {
let text = fs_err::read_to_string(path)?;
if text.starts_with(EXPECTED_HEADER) {
// File header is present, nothing to do.
continue;
}
if fmt_opt.check {
// Add to the list of paths missing file headers.
missing.push(path);
} else {
// Rewrite the file to prepend the header.
let text = EXPECTED_HEADER.to_owned() + &text;
fs_err::write(path, text)?;
}
}
if fmt_opt.check && !missing.is_empty() {
bail!("expected header missing from {}", missing.join(", "));
}
Ok(())
}
fn has_cmd(target_cmd: &str) -> bool {
#[cfg(target_os = "windows")]
let mut cmd = Command::new("where");
#[cfg(target_family = "unix")]
let mut cmd = Command::new("which");
cmd.arg(target_cmd);
run_cmd(cmd).is_ok()
}
fn install_logger() {
struct Logger;
impl log::Log for Logger {
fn enabled(&self, _: &Metadata) -> bool {
true
}
fn log(&self, record: &Record) {
println!("[{}] {}", record.level(), record.args());
}
fn flush(&self) {}
}
static LOGGER: Logger = Logger;
log::set_logger(&LOGGER)
.map(|()| log::set_max_level(LevelFilter::Info))
.unwrap();
}
fn main() -> Result<()> {
let opt = Opt::parse();
install_logger();
match &opt.action {
Action::Build(build_opt) => build(build_opt),
Action::CheckRaw(_) => check_raw::check_raw(),
Action::Clippy(clippy_opt) => clippy(clippy_opt),
Action::Cov(cov_opt) => code_coverage(cov_opt),
Action::Doc(doc_opt) => doc(doc_opt),
Action::GenCode(gen_opt) => device_path::gen_code(gen_opt),
Action::Miri(_) => run_miri(),
Action::Run(qemu_opt) => run_vm_tests(qemu_opt),
Action::Test(test_opt) => run_host_tests(test_opt),
Action::Fmt(fmt_opt) => run_fmt_project(fmt_opt),
}
}