Skip to content

Commit a69edc6

Browse files
committed
Add BootstrapCommand and run_cmd
1 parent bb74d1f commit a69edc6

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

src/bootstrap/src/lib.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use crate::core::config::flags;
3939
use crate::core::config::{DryRun, Target};
4040
use crate::core::config::{LlvmLibunwind, TargetSelection};
4141
use crate::utils::cache::{Interned, INTERNER};
42+
use crate::utils::exec::BootstrapCommand;
4243
use crate::utils::helpers::{
4344
self, dir_is_empty, exe, libdir, mtime, output, run, run_suppressed, symlink_dir,
4445
try_run_suppressed,
@@ -959,17 +960,32 @@ impl Build {
959960

960961
/// Runs a command, printing out contextual info if it fails, and delaying errors until the build finishes.
961962
pub(crate) fn run_delaying_failure(&self, cmd: &mut Command) -> bool {
962-
if !self.fail_fast {
963-
#[allow(deprecated)] // can't use Build::try_run, that's us
964-
if self.config.try_run(cmd).is_err() {
965-
let mut failures = self.delayed_failures.borrow_mut();
966-
failures.push(format!("{cmd:?}"));
967-
return false;
963+
let cmd: BootstrapCommand<'_> = cmd.into();
964+
self.run_cmd(cmd.delay_failure())
965+
}
966+
967+
/// A centralized function for running commands that do not return output.
968+
pub(crate) fn run_cmd<'a, C: Into<BootstrapCommand<'a>>>(&self, cmd: C) -> bool {
969+
let command = cmd.into();
970+
self.verbose(&format!("running: {command:?}"));
971+
972+
#[allow(deprecated)] // can't use Build::try_run, that's us
973+
let result = self.config.try_run(command.command);
974+
975+
match result {
976+
Ok(_) => true,
977+
Err(_) => {
978+
if command.delay_failure {
979+
let mut failures = self.delayed_failures.borrow_mut();
980+
failures.push(format!("{command:?}"));
981+
return false;
982+
}
983+
if self.fail_fast {
984+
exit!(1);
985+
}
986+
false
968987
}
969-
} else {
970-
self.run(cmd);
971988
}
972-
true
973989
}
974990

975991
pub fn is_verbose_than(&self, level: usize) -> bool {

src/bootstrap/src/utils/exec.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::process::Command;
2+
3+
/// Wrapper around `std::process::Command`.
4+
#[derive(Debug)]
5+
pub struct BootstrapCommand<'a> {
6+
pub command: &'a mut Command,
7+
/// Report failure later instead of immediately.
8+
pub delay_failure: bool,
9+
}
10+
11+
impl<'a> BootstrapCommand<'a> {
12+
pub fn delay_failure(self) -> Self {
13+
Self { delay_failure: true, ..self }
14+
}
15+
}
16+
17+
impl<'a> From<&'a mut Command> for BootstrapCommand<'a> {
18+
fn from(command: &'a mut Command) -> Self {
19+
Self { command, delay_failure: false }
20+
}
21+
}

src/bootstrap/src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub(crate) mod cache;
66
pub(crate) mod cc_detect;
77
pub(crate) mod channel;
88
pub(crate) mod dylib;
9+
pub(crate) mod exec;
910
pub(crate) mod helpers;
1011
pub(crate) mod job;
1112
#[cfg(feature = "build-metrics")]

0 commit comments

Comments
 (0)