Skip to content

Commit cfcb860

Browse files
committed
Add behavior on failure to BootstrapCommand
1 parent a331ba6 commit cfcb860

File tree

2 files changed

+45
-44
lines changed

2 files changed

+45
-44
lines changed

src/bootstrap/exec.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
use std::process::Command;
22

3+
/// What should be done when the command fails.
4+
#[derive(Debug, Copy, Clone)]
5+
pub enum BehaviorOnFailure {
6+
/// Immediately stop bootstrap.
7+
Exit,
8+
/// Delay failure until the end of bootstrap invocation.
9+
DelayFail,
10+
}
11+
312
/// Wrapper around `std::process::Command`.
413
#[derive(Debug)]
514
pub struct BootstrapCommand<'a> {
615
pub command: &'a mut Command,
7-
/// Report failure later instead of immediately.
8-
pub delay_failure: bool,
16+
pub failure_behavior: Option<BehaviorOnFailure>,
917
}
1018

1119
impl<'a> BootstrapCommand<'a> {
1220
pub fn delay_failure(self) -> Self {
13-
Self { delay_failure: true, ..self }
21+
Self { failure_behavior: Some(BehaviorOnFailure::DelayFail), ..self }
22+
}
23+
pub fn fail_fast(self) -> Self {
24+
Self { failure_behavior: Some(BehaviorOnFailure::Exit), ..self }
1425
}
1526
}
1627

1728
impl<'a> From<&'a mut Command> for BootstrapCommand<'a> {
1829
fn from(command: &'a mut Command) -> Self {
19-
Self { command, delay_failure: false }
30+
Self { command, failure_behavior: None }
2031
}
2132
}

src/bootstrap/lib.rs

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,21 @@ use std::str;
2828

2929
use build_helper::ci::{gha, CiEnv};
3030
use build_helper::exit;
31-
use channel::GitInfo;
32-
use config::{DryRun, Target};
3331
use filetime::FileTime;
3432
use once_cell::sync::OnceCell;
33+
use termcolor::{ColorChoice, StandardStream, WriteColor};
34+
35+
use channel::GitInfo;
36+
use config::{DryRun, Target};
3537

3638
use crate::builder::Kind;
39+
pub use crate::builder::PathSet;
40+
use crate::cache::{Interned, INTERNER};
41+
pub use crate::config::Config;
3742
use crate::config::{LlvmLibunwind, TargetSelection};
38-
use crate::util::{
39-
dir_is_empty, exe, libdir, mtime, output, run, run_suppressed, symlink_dir, try_run_suppressed,
40-
};
43+
use crate::exec::{BehaviorOnFailure, BootstrapCommand};
44+
pub use crate::flags::Subcommand;
45+
use crate::util::{dir_is_empty, exe, libdir, mtime, output, run, symlink_dir};
4146

4247
mod builder;
4348
mod cache;
@@ -88,13 +93,6 @@ mod job {
8893
pub unsafe fn setup(_build: &mut crate::Build) {}
8994
}
9095

91-
pub use crate::builder::PathSet;
92-
use crate::cache::{Interned, INTERNER};
93-
pub use crate::config::Config;
94-
use crate::exec::BootstrapCommand;
95-
pub use crate::flags::Subcommand;
96-
use termcolor::{ColorChoice, StandardStream, WriteColor};
97-
9896
const LLVM_TOOLS: &[&str] = &[
9997
"llvm-cov", // used to generate coverage report
10098
"llvm-nm", // used to inspect binaries; it shows symbol names, their sizes and visibility
@@ -967,44 +965,30 @@ impl Build {
967965

968966
/// Runs a command, printing out nice contextual information if it fails.
969967
fn run(&self, cmd: &mut Command) {
970-
if self.config.dry_run() {
971-
return;
972-
}
973-
self.verbose(&format!("running: {cmd:?}"));
974-
run(cmd, self.is_verbose())
968+
// FIXME: output mode -> status + err if self.is_verbose()
969+
let cmd: BootstrapCommand<'_> = cmd.into();
970+
self.run_cmd(cmd.fail_fast());
975971
}
976972

977973
/// Runs a command, printing out nice contextual information if it fails.
978974
fn run_quiet(&self, cmd: &mut Command) {
979-
if self.config.dry_run() {
980-
return;
981-
}
982-
self.verbose(&format!("running: {cmd:?}"));
983-
run_suppressed(cmd)
975+
// FIXME: output mode -> output + err
976+
let cmd: BootstrapCommand<'_> = cmd.into();
977+
self.run_cmd(cmd.fail_fast());
984978
}
985979

986980
/// Runs a command, printing out nice contextual information if it fails.
987981
/// Exits if the command failed to execute at all, otherwise returns its
988982
/// `status.success()`.
989983
fn run_quiet_delaying_failure(&self, cmd: &mut Command) -> bool {
990-
if self.config.dry_run() {
991-
return true;
992-
}
993-
if !self.fail_fast {
994-
self.verbose(&format!("running: {cmd:?}"));
995-
if !try_run_suppressed(cmd) {
996-
let mut failures = self.delayed_failures.borrow_mut();
997-
failures.push(format!("{cmd:?}"));
998-
return false;
999-
}
1000-
} else {
1001-
self.run_quiet(cmd);
1002-
}
1003-
true
984+
// FIXME: output mode -> output + err
985+
let cmd: BootstrapCommand<'_> = cmd.into();
986+
self.run_cmd(cmd.delay_failure())
1004987
}
1005988

1006989
/// Runs a command, printing out contextual info if it fails, and delaying errors until the build finishes.
1007990
pub(crate) fn run_delaying_failure(&self, cmd: &mut Command) -> bool {
991+
// FIXME: output mode -> status + err if self.is_verbose()
1008992
let cmd: BootstrapCommand<'_> = cmd.into();
1009993
self.run_cmd(cmd.delay_failure())
1010994
}
@@ -1020,10 +1004,16 @@ impl Build {
10201004
match result {
10211005
Ok(_) => true,
10221006
Err(_) => {
1023-
if command.delay_failure {
1024-
let mut failures = self.delayed_failures.borrow_mut();
1025-
failures.push(format!("{command:?}"));
1026-
return false;
1007+
if let Some(failure_behavior) = command.failure_behavior {
1008+
match failure_behavior {
1009+
BehaviorOnFailure::DelayFail => {
1010+
let mut failures = self.delayed_failures.borrow_mut();
1011+
failures.push(format!("{command:?}"));
1012+
}
1013+
BehaviorOnFailure::Exit => {
1014+
exit!(1);
1015+
}
1016+
}
10271017
}
10281018
if self.fail_fast {
10291019
exit!(1);

0 commit comments

Comments
 (0)