Add BootstrapCommand and run_cmd

This commit is contained in:
Jakub Beránek 2023-10-09 23:04:11 +02:00
parent bb74d1fa85
commit a69edc68aa
No known key found for this signature in database
GPG key ID: 909CD0D26483516B
3 changed files with 47 additions and 9 deletions

View file

@ -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<BootstrapCommand<'a>>>(&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 {

View file

@ -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 }
}
}

View file

@ -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")]