Skip to content

Commit b35c8f5

Browse files
committed
Add output on failure to BootstrapCommand
1 parent a331ba6 commit b35c8f5

File tree

3 files changed

+59
-52
lines changed

3 files changed

+59
-52
lines changed

src/bootstrap/exec.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
use std::process::Command;
22

3+
/// What should be reported when the command fails.
4+
#[derive(Debug, Copy, Clone)]
5+
pub enum OutputOnFailure {
6+
/// Status code
7+
Succint,
8+
/// Status code together with stdout and stderr
9+
Verbose,
10+
}
11+
312
/// Wrapper around `std::process::Command`.
413
#[derive(Debug)]
514
pub struct BootstrapCommand<'a> {
615
pub command: &'a mut Command,
716
/// Report failure later instead of immediately.
817
pub delay_failure: bool,
18+
pub output_on_failure: Option<OutputOnFailure>,
919
}
1020

1121
impl<'a> BootstrapCommand<'a> {
1222
pub fn delay_failure(self) -> Self {
1323
Self { delay_failure: true, ..self }
1424
}
25+
pub fn output_on_failure(self, output_on_failure: OutputOnFailure) -> Self {
26+
Self { output_on_failure: Some(output_on_failure), ..self }
27+
}
1528
}
1629

1730
impl<'a> From<&'a mut Command> for BootstrapCommand<'a> {
1831
fn from(command: &'a mut Command) -> Self {
19-
Self { command, delay_failure: false }
32+
Self { command, delay_failure: false, output_on_failure: None }
2033
}
2134
}

src/bootstrap/lib.rs

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,15 @@ use std::str;
2828

2929
use build_helper::ci::{gha, CiEnv};
3030
use build_helper::exit;
31+
use build_helper::util::fail;
3132
use channel::GitInfo;
3233
use config::{DryRun, Target};
3334
use filetime::FileTime;
3435
use once_cell::sync::OnceCell;
3536

3637
use crate::builder::Kind;
3738
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-
};
39+
use crate::util::{dir_is_empty, exe, libdir, mtime, output, run, symlink_dir};
4140

4241
mod builder;
4342
mod cache;
@@ -91,7 +90,7 @@ mod job {
9190
pub use crate::builder::PathSet;
9291
use crate::cache::{Interned, INTERNER};
9392
pub use crate::config::Config;
94-
use crate::exec::BootstrapCommand;
93+
use crate::exec::{BootstrapCommand, OutputOnFailure};
9594
pub use crate::flags::Subcommand;
9695
use termcolor::{ColorChoice, StandardStream, WriteColor};
9796

@@ -976,31 +975,17 @@ impl Build {
976975

977976
/// Runs a command, printing out nice contextual information if it fails.
978977
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)
978+
let cmd: BootstrapCommand<'_> = cmd.into();
979+
// TODO: fail bootstrap immediately if this command fails
980+
self.run_cmd(cmd.output_on_failure(OutputOnFailure::Verbose));
984981
}
985982

986983
/// Runs a command, printing out nice contextual information if it fails.
987984
/// Exits if the command failed to execute at all, otherwise returns its
988985
/// `status.success()`.
989986
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
987+
let cmd: BootstrapCommand<'_> = cmd.into();
988+
self.run_cmd(cmd.output_on_failure(OutputOnFailure::Verbose).delay_failure())
1004989
}
1005990

1006991
/// Runs a command, printing out contextual info if it fails, and delaying errors until the build finishes.
@@ -1014,8 +999,43 @@ impl Build {
1014999
let command = cmd.into();
10151000
self.verbose(&format!("running: {command:?}"));
10161001

1017-
#[allow(deprecated)] // can't use Build::try_run, that's us
1018-
let result = self.config.try_run(command.command);
1002+
let output = match command.command.output() {
1003+
Ok(output) => output,
1004+
Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}", command, e)),
1005+
};
1006+
let result = if !output.status.success() {
1007+
let output_on_failure = match command.output_on_failure {
1008+
Some(output) => Some(output),
1009+
None if self.is_verbose() => Some(OutputOnFailure::Succint),
1010+
None => None,
1011+
};
1012+
1013+
if let Some(output_on_failure) = output_on_failure {
1014+
use std::fmt::Write;
1015+
1016+
let mut message = String::new();
1017+
writeln!(
1018+
message,
1019+
"\n\ncommand did not execute successfully: {:?}\n\
1020+
expected success, got: {}\n\n",
1021+
command.command, output.status
1022+
)
1023+
.unwrap();
1024+
if let OutputOnFailure::Verbose = output_on_failure {
1025+
writeln!(
1026+
message,
1027+
"stdout ----\n{}\n\
1028+
stderr ----\n{}\n\n",
1029+
String::from_utf8_lossy(&output.stdout),
1030+
String::from_utf8_lossy(&output.stderr)
1031+
)
1032+
.unwrap();
1033+
}
1034+
}
1035+
Err(())
1036+
} else {
1037+
Ok(())
1038+
};
10191039

10201040
match result {
10211041
Ok(_) => true,

src/bootstrap/util.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -239,32 +239,6 @@ pub fn check_run(cmd: &mut Command, print_cmd_on_fail: bool) -> bool {
239239
status.success()
240240
}
241241

242-
pub fn run_suppressed(cmd: &mut Command) {
243-
if !try_run_suppressed(cmd) {
244-
crate::exit!(1);
245-
}
246-
}
247-
248-
pub fn try_run_suppressed(cmd: &mut Command) -> bool {
249-
let output = match cmd.output() {
250-
Ok(status) => status,
251-
Err(e) => fail(&format!("failed to execute command: {cmd:?}\nerror: {e}")),
252-
};
253-
if !output.status.success() {
254-
println!(
255-
"\n\ncommand did not execute successfully: {:?}\n\
256-
expected success, got: {}\n\n\
257-
stdout ----\n{}\n\
258-
stderr ----\n{}\n\n",
259-
cmd,
260-
output.status,
261-
String::from_utf8_lossy(&output.stdout),
262-
String::from_utf8_lossy(&output.stderr)
263-
);
264-
}
265-
output.status.success()
266-
}
267-
268242
pub fn make(host: &str) -> PathBuf {
269243
if host.contains("dragonfly")
270244
|| host.contains("freebsd")

0 commit comments

Comments
 (0)