Skip to content

Commit 8dc4485

Browse files
committed
Simplify and generalize implementation of output mode
1 parent 8e5cfc6 commit 8dc4485

File tree

2 files changed

+42
-38
lines changed

2 files changed

+42
-38
lines changed

src/bootstrap/src/lib.rs

+13-22
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::fmt::Display;
2323
use std::fs::{self, File};
2424
use std::io;
2525
use std::path::{Path, PathBuf};
26-
use std::process::{Command, Output, Stdio};
26+
use std::process::{Command, Stdio};
2727
use std::str;
2828
use std::sync::OnceLock;
2929

@@ -40,7 +40,7 @@ use crate::core::builder::Kind;
4040
use crate::core::config::{flags, LldMode};
4141
use crate::core::config::{DryRun, Target};
4242
use crate::core::config::{LlvmLibunwind, TargetSelection};
43-
use crate::utils::exec::{BehaviorOnFailure, BootstrapCommand, CommandOutput, OutputMode};
43+
use crate::utils::exec::{BehaviorOnFailure, BootstrapCommand, CommandOutput};
4444
use crate::utils::helpers::{self, dir_is_empty, exe, libdir, mtime, output, symlink_dir};
4545

4646
mod core;
@@ -971,18 +971,10 @@ impl Build {
971971

972972
self.verbose(|| println!("running: {command:?}"));
973973

974-
let output: io::Result<Output> = match command.output_mode {
975-
OutputMode::Print => command.command.status().map(|status| Output {
976-
status,
977-
stdout: vec![],
978-
stderr: vec![],
979-
}),
980-
OutputMode::CaptureAll => command.command.output(),
981-
OutputMode::CaptureStdout => {
982-
command.command.stderr(Stdio::inherit());
983-
command.command.output()
984-
}
985-
};
974+
command.command.stdout(command.stdout.stdio());
975+
command.command.stderr(command.stderr.stdio());
976+
977+
let output = command.command.output();
986978

987979
use std::fmt::Write;
988980

@@ -1001,16 +993,15 @@ impl Build {
1001993
.unwrap();
1002994

1003995
let output: CommandOutput = output.into();
1004-
// If the output mode is OutputMode::Print, the output has already been printed to
996+
997+
// If the output mode is OutputMode::Capture, we can now print the output.
998+
// If it is OutputMode::Print, then the output has already been printed to
1005999
// stdout/stderr, and we thus don't have anything captured to print anyway.
1006-
if matches!(command.output_mode, OutputMode::CaptureAll | OutputMode::CaptureStdout)
1007-
{
1000+
if command.stdout.captures() {
10081001
writeln!(message, "\nSTDOUT ----\n{}", output.stdout().trim()).unwrap();
1009-
1010-
// Stderr is added to the message only if it was captured
1011-
if matches!(command.output_mode, OutputMode::CaptureAll) {
1012-
writeln!(message, "\nSTDERR ----\n{}", output.stderr().trim()).unwrap();
1013-
}
1002+
}
1003+
if command.stderr.captures() {
1004+
writeln!(message, "\nSTDERR ----\n{}", output.stderr().trim()).unwrap();
10141005
}
10151006
output
10161007
}

src/bootstrap/src/utils/exec.rs

+29-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::ffi::OsStr;
22
use std::path::Path;
3-
use std::process::{Command, CommandArgs, CommandEnvs, ExitStatus, Output};
3+
use std::process::{Command, CommandArgs, CommandEnvs, ExitStatus, Output, Stdio};
44

55
/// What should be done when the command fails.
66
#[derive(Debug, Copy, Clone)]
@@ -13,19 +13,30 @@ pub enum BehaviorOnFailure {
1313
Ignore,
1414
}
1515

16-
/// How should the output of the command be handled (whether it should be captured or printed).
16+
/// How should the output of a specific stream of the command (stdout/stderr) be handled
17+
/// (whether it should be captured or printed).
1718
#[derive(Debug, Copy, Clone)]
1819
pub enum OutputMode {
19-
/// Prints the stdout/stderr of the command to stdout/stderr of bootstrap (by inheriting these
20-
/// streams).
21-
/// Corresponds to calling `cmd.status()`.
20+
/// Prints the stream by inheriting it from the bootstrap process.
2221
Print,
23-
/// Captures the stdout and stderr of the command into memory.
24-
/// Corresponds to calling `cmd.output()`.
25-
CaptureAll,
26-
/// Captures the stdout of the command into memory, inherits stderr.
27-
/// Corresponds to calling `cmd.output()`.
28-
CaptureStdout,
22+
/// Captures the stream into memory.
23+
Capture,
24+
}
25+
26+
impl OutputMode {
27+
pub fn captures(&self) -> bool {
28+
match self {
29+
OutputMode::Print => false,
30+
OutputMode::Capture => true,
31+
}
32+
}
33+
34+
pub fn stdio(&self) -> Stdio {
35+
match self {
36+
OutputMode::Print => Stdio::inherit(),
37+
OutputMode::Capture => Stdio::piped(),
38+
}
39+
}
2940
}
3041

3142
/// Wrapper around `std::process::Command`.
@@ -45,7 +56,8 @@ pub enum OutputMode {
4556
pub struct BootstrapCommand {
4657
pub command: Command,
4758
pub failure_behavior: BehaviorOnFailure,
48-
pub output_mode: OutputMode,
59+
pub stdout: OutputMode,
60+
pub stderr: OutputMode,
4961
// Run the command even during dry run
5062
pub run_always: bool,
5163
}
@@ -113,14 +125,14 @@ impl BootstrapCommand {
113125
self
114126
}
115127

116-
/// Capture the output of the command, do not print it.
128+
/// Capture all output of the command, do not print it.
117129
pub fn capture(self) -> Self {
118-
Self { output_mode: OutputMode::CaptureAll, ..self }
130+
Self { stdout: OutputMode::Capture, stderr: OutputMode::Capture, ..self }
119131
}
120132

121133
/// Capture stdout of the command, do not print it.
122134
pub fn capture_stdout(self) -> Self {
123-
Self { output_mode: OutputMode::CaptureStdout, ..self }
135+
Self { stdout: OutputMode::Capture, ..self }
124136
}
125137
}
126138

@@ -137,7 +149,8 @@ impl From<Command> for BootstrapCommand {
137149
Self {
138150
command,
139151
failure_behavior: BehaviorOnFailure::Exit,
140-
output_mode: OutputMode::Print,
152+
stdout: OutputMode::Print,
153+
stderr: OutputMode::Print,
141154
run_always: false,
142155
}
143156
}

0 commit comments

Comments
 (0)