From a69edc68aa224891a7e54d8ced094ffc28c4be3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Mon, 9 Oct 2023 23:04:11 +0200 Subject: [PATCH] Add BootstrapCommand and `run_cmd` --- src/bootstrap/src/lib.rs | 34 ++++++++++++++++++++++++--------- src/bootstrap/src/utils/exec.rs | 21 ++++++++++++++++++++ src/bootstrap/src/utils/mod.rs | 1 + 3 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 src/bootstrap/src/utils/exec.rs diff --git a/src/bootstrap/src/lib.rs b/src/bootstrap/src/lib.rs index 97c743074af4..b20c20a360b9 100644 --- a/src/bootstrap/src/lib.rs +++ b/src/bootstrap/src/lib.rs @@ -39,6 +39,7 @@ use crate::core::config::flags; use crate::core::config::{DryRun, Target}; use crate::core::config::{LlvmLibunwind, TargetSelection}; use crate::utils::cache::{Interned, INTERNER}; +use crate::utils::exec::BootstrapCommand; use crate::utils::helpers::{ self, dir_is_empty, exe, libdir, mtime, output, run, run_suppressed, symlink_dir, try_run_suppressed, @@ -959,17 +960,32 @@ impl Build { /// Runs a command, printing out contextual info if it fails, and delaying errors until the build finishes. pub(crate) fn run_delaying_failure(&self, cmd: &mut Command) -> bool { - if !self.fail_fast { - #[allow(deprecated)] // can't use Build::try_run, that's us - if self.config.try_run(cmd).is_err() { - let mut failures = self.delayed_failures.borrow_mut(); - failures.push(format!("{cmd:?}")); - return false; + let cmd: BootstrapCommand<'_> = cmd.into(); + self.run_cmd(cmd.delay_failure()) + } + + /// A centralized function for running commands that do not return output. + pub(crate) fn run_cmd<'a, C: Into>>(&self, cmd: C) -> bool { + let command = cmd.into(); + self.verbose(&format!("running: {command:?}")); + + #[allow(deprecated)] // can't use Build::try_run, that's us + let result = self.config.try_run(command.command); + + match result { + Ok(_) => true, + Err(_) => { + if command.delay_failure { + let mut failures = self.delayed_failures.borrow_mut(); + failures.push(format!("{command:?}")); + return false; + } + if self.fail_fast { + exit!(1); + } + false } - } else { - self.run(cmd); } - true } pub fn is_verbose_than(&self, level: usize) -> bool { diff --git a/src/bootstrap/src/utils/exec.rs b/src/bootstrap/src/utils/exec.rs new file mode 100644 index 000000000000..a590171984e3 --- /dev/null +++ b/src/bootstrap/src/utils/exec.rs @@ -0,0 +1,21 @@ +use std::process::Command; + +/// Wrapper around `std::process::Command`. +#[derive(Debug)] +pub struct BootstrapCommand<'a> { + pub command: &'a mut Command, + /// Report failure later instead of immediately. + pub delay_failure: bool, +} + +impl<'a> BootstrapCommand<'a> { + pub fn delay_failure(self) -> Self { + Self { delay_failure: true, ..self } + } +} + +impl<'a> From<&'a mut Command> for BootstrapCommand<'a> { + fn from(command: &'a mut Command) -> Self { + Self { command, delay_failure: false } + } +} diff --git a/src/bootstrap/src/utils/mod.rs b/src/bootstrap/src/utils/mod.rs index 7dcb6a828620..8ca22d00865b 100644 --- a/src/bootstrap/src/utils/mod.rs +++ b/src/bootstrap/src/utils/mod.rs @@ -6,6 +6,7 @@ pub(crate) mod cache; pub(crate) mod cc_detect; pub(crate) mod channel; pub(crate) mod dylib; +pub(crate) mod exec; pub(crate) mod helpers; pub(crate) mod job; #[cfg(feature = "build-metrics")]