Skip to content

Commit 63d7992

Browse files
committed
Deduplicate Builder::try_run and mark Config::try_run as deprecated
This does three things: 1. Remove `forward!(Build, fn try_run())`. Having `try_run` behave differently as a free function than an associated function is confusing, and `Builder::try_run` is a very desirable name. 2. Move `test::try_run` and `run::try_run` to `Builder::try_run`. These functions are different than `Config::try_run` - they delay the failure and print it out at the end of the build. 3. Mark `Config::try_run` as deprecated to encourage people to use `Builder::try_run` instead.
1 parent 4d6e426 commit 63d7992

File tree

6 files changed

+63
-63
lines changed

6 files changed

+63
-63
lines changed

src/bootstrap/config.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,18 @@ impl Config {
17421742
}
17431743
}
17441744

1745+
/// Runs a command, printing out nice contextual information if it fails.
1746+
/// Exits if the command failed to execute at all, otherwise returns its
1747+
/// `status.success()`.
1748+
#[deprecated = "use `Builder::try_run` instead where possible"]
1749+
pub(crate) fn try_run(&self, cmd: &mut Command) -> Result<(), ()> {
1750+
if self.dry_run() {
1751+
return Ok(());
1752+
}
1753+
self.verbose(&format!("running: {:?}", cmd));
1754+
build_helper::util::try_run(cmd, self.is_verbose())
1755+
}
1756+
17451757
/// A git invocation which runs inside the source directory.
17461758
///
17471759
/// Use this rather than `Command::new("git")` in order to support out-of-tree builds.

src/bootstrap/download.rs

+15-18
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77
process::{Command, Stdio},
88
};
99

10-
use build_helper::{ci::CiEnv, util::try_run};
10+
use build_helper::ci::CiEnv;
1111
use once_cell::sync::OnceCell;
1212
use xz2::bufread::XzDecoder;
1313

@@ -21,6 +21,12 @@ use crate::{
2121

2222
static SHOULD_FIX_BINS_AND_DYLIBS: OnceCell<bool> = OnceCell::new();
2323

24+
/// `Config::try_run` wrapper for this module to avoid warnings on `try_run`, since we don't have access to a `builder` yet.
25+
fn try_run(config: &Config, cmd: &mut Command) -> Result<(), ()> {
26+
#[allow(deprecated)]
27+
config.try_run(cmd)
28+
}
29+
2430
/// Generic helpers that are useful anywhere in bootstrap.
2531
impl Config {
2632
pub fn is_verbose(&self) -> bool {
@@ -51,17 +57,6 @@ impl Config {
5157
tmp
5258
}
5359

54-
/// Runs a command, printing out nice contextual information if it fails.
55-
/// Exits if the command failed to execute at all, otherwise returns its
56-
/// `status.success()`.
57-
pub(crate) fn try_run(&self, cmd: &mut Command) -> Result<(), ()> {
58-
if self.dry_run() {
59-
return Ok(());
60-
}
61-
self.verbose(&format!("running: {:?}", cmd));
62-
try_run(cmd, self.is_verbose())
63-
}
64-
6560
/// Runs a command, printing out nice contextual information if it fails.
6661
/// Returns false if do not execute at all, otherwise returns its
6762
/// `status.success()`.
@@ -156,14 +151,16 @@ impl Config {
156151
];
157152
}
158153
";
159-
nix_build_succeeded = self
160-
.try_run(Command::new("nix-build").args(&[
154+
nix_build_succeeded = try_run(
155+
self,
156+
Command::new("nix-build").args(&[
161157
Path::new("-E"),
162158
Path::new(NIX_EXPR),
163159
Path::new("-o"),
164160
&nix_deps_dir,
165-
]))
166-
.is_ok();
161+
]),
162+
)
163+
.is_ok();
167164
nix_deps_dir
168165
});
169166
if !nix_build_succeeded {
@@ -188,7 +185,7 @@ impl Config {
188185
patchelf.args(&["--set-interpreter", dynamic_linker.trim_end()]);
189186
}
190187

191-
let _ = self.try_run(patchelf.arg(fname));
188+
let _ = try_run(self, patchelf.arg(fname));
192189
}
193190

194191
fn download_file(&self, url: &str, dest_path: &Path, help_on_error: &str) {
@@ -236,7 +233,7 @@ impl Config {
236233
if self.build.contains("windows-msvc") {
237234
eprintln!("Fallback to PowerShell");
238235
for _ in 0..3 {
239-
if self.try_run(Command::new("PowerShell.exe").args(&[
236+
if try_run(self, Command::new("PowerShell.exe").args(&[
240237
"/nologo",
241238
"-Command",
242239
"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;",

src/bootstrap/lib.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ forward! {
333333
create(path: &Path, s: &str),
334334
remove(f: &Path),
335335
tempdir() -> PathBuf,
336-
try_run(cmd: &mut Command) -> Result<(), ()>,
337336
llvm_link_shared() -> bool,
338337
download_rustc() -> bool,
339338
initial_rustfmt() -> Option<PathBuf>,
@@ -617,7 +616,9 @@ impl Build {
617616
}
618617

619618
// Save any local changes, but avoid running `git stash pop` if there are none (since it will exit with an error).
619+
#[allow(deprecated)] // diff-index reports the modifications through the exit status
620620
let has_local_modifications = self
621+
.config
621622
.try_run(
622623
Command::new("git")
623624
.args(&["diff-index", "--quiet", "HEAD"])
@@ -979,6 +980,21 @@ impl Build {
979980
try_run_suppressed(cmd)
980981
}
981982

983+
/// Runs a command, printing out contextual info if it fails, and delaying errors until the build finishes.
984+
pub(crate) fn try_run(&self, cmd: &mut Command) -> bool {
985+
if !self.fail_fast {
986+
#[allow(deprecated)] // can't use Build::try_run, that's us
987+
if self.config.try_run(cmd).is_err() {
988+
let mut failures = self.delayed_failures.borrow_mut();
989+
failures.push(format!("{:?}", cmd));
990+
return false;
991+
}
992+
} else {
993+
self.run(cmd);
994+
}
995+
true
996+
}
997+
982998
pub fn is_verbose_than(&self, level: usize) -> bool {
983999
self.verbosity > level
9841000
}

src/bootstrap/run.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ impl Step for ExpandYamlAnchors {
2424
/// anchors in them, since GitHub Actions doesn't support them.
2525
fn run(self, builder: &Builder<'_>) {
2626
builder.info("Expanding YAML anchors in the GitHub Actions configuration");
27-
try_run(
28-
builder,
27+
builder.try_run(
2928
&mut builder.tool_cmd(Tool::ExpandYamlAnchors).arg("generate").arg(&builder.src),
3029
);
3130
}
@@ -39,19 +38,6 @@ impl Step for ExpandYamlAnchors {
3938
}
4039
}
4140

42-
fn try_run(builder: &Builder<'_>, cmd: &mut Command) -> bool {
43-
if !builder.fail_fast {
44-
if builder.try_run(cmd).is_err() {
45-
let mut failures = builder.delayed_failures.borrow_mut();
46-
failures.push(format!("{:?}", cmd));
47-
return false;
48-
}
49-
} else {
50-
builder.run(cmd);
51-
}
52-
true
53-
}
54-
5541
#[derive(Debug, PartialOrd, Ord, Copy, Clone, Hash, PartialEq, Eq)]
5642
pub struct BuildManifest;
5743

src/bootstrap/test.rs

+16-28
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,6 @@ const MIR_OPT_BLESS_TARGET_MAPPING: &[(&str, &str)] = &[
4848
// build for, so there is no entry for "aarch64-apple-darwin" here.
4949
];
5050

51-
fn try_run(builder: &Builder<'_>, cmd: &mut Command) -> bool {
52-
if !builder.fail_fast {
53-
if builder.try_run(cmd).is_err() {
54-
let mut failures = builder.delayed_failures.borrow_mut();
55-
failures.push(format!("{:?}", cmd));
56-
return false;
57-
}
58-
} else {
59-
builder.run(cmd);
60-
}
61-
true
62-
}
63-
6451
fn try_run_quiet(builder: &Builder<'_>, cmd: &mut Command) -> bool {
6552
if !builder.fail_fast {
6653
if !builder.try_run_quiet(cmd) {
@@ -193,7 +180,9 @@ You can skip linkcheck with --exclude src/tools/linkchecker"
193180
let _guard =
194181
builder.msg(Kind::Test, compiler.stage, "Linkcheck", bootstrap_host, bootstrap_host);
195182
let _time = util::timeit(&builder);
196-
try_run(builder, linkchecker.arg(builder.out.join(host.triple).join("doc")));
183+
builder.try_run(
184+
linkchecker.arg(builder.out.join(host.triple).join("doc")),
185+
);
197186
}
198187

199188
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
@@ -246,7 +235,7 @@ impl Step for HtmlCheck {
246235
builder.default_doc(&[]);
247236
builder.ensure(crate::doc::Rustc::new(builder.top_stage, self.target, builder));
248237

249-
try_run(builder, builder.tool_cmd(Tool::HtmlChecker).arg(builder.doc_out(self.target)));
238+
builder.try_run(builder.tool_cmd(Tool::HtmlChecker).arg(builder.doc_out(self.target)));
250239
}
251240
}
252241

@@ -285,8 +274,7 @@ impl Step for Cargotest {
285274

286275
let _time = util::timeit(&builder);
287276
let mut cmd = builder.tool_cmd(Tool::CargoTest);
288-
try_run(
289-
builder,
277+
builder.try_run(
290278
cmd.arg(&cargo)
291279
.arg(&out_dir)
292280
.args(builder.config.test_args())
@@ -827,7 +815,8 @@ impl Step for Clippy {
827815

828816
let _guard = builder.msg_sysroot_tool(Kind::Test, compiler.stage, "clippy", host, host);
829817

830-
if builder.try_run(&mut cargo).is_ok() {
818+
#[allow(deprecated)] // Clippy reports errors if it blessed the outputs
819+
if builder.config.try_run(&mut cargo).is_ok() {
831820
// The tests succeeded; nothing to do.
832821
return;
833822
}
@@ -887,7 +876,7 @@ impl Step for RustdocTheme {
887876
util::lld_flag_no_threads(self.compiler.host.contains("windows")),
888877
);
889878
}
890-
try_run(builder, &mut cmd);
879+
builder.try_run(&mut cmd);
891880
}
892881
}
893882

@@ -1147,7 +1136,7 @@ help: to skip test's attempt to check tidiness, pass `--exclude src/tools/tidy`
11471136
}
11481137

11491138
builder.info("tidy check");
1150-
try_run(builder, &mut cmd);
1139+
builder.try_run(&mut cmd);
11511140

11521141
builder.ensure(ExpandYamlAnchors);
11531142

@@ -1192,8 +1181,7 @@ impl Step for ExpandYamlAnchors {
11921181
/// by the user before committing CI changes.
11931182
fn run(self, builder: &Builder<'_>) {
11941183
builder.info("Ensuring the YAML anchors in the GitHub Actions config were expanded");
1195-
try_run(
1196-
builder,
1184+
builder.try_run(
11971185
&mut builder.tool_cmd(Tool::ExpandYamlAnchors).arg("check").arg(&builder.src),
11981186
);
11991187
}
@@ -1982,7 +1970,7 @@ impl BookTest {
19821970
compiler.host,
19831971
);
19841972
let _time = util::timeit(&builder);
1985-
let toolstate = if try_run(builder, &mut rustbook_cmd) {
1973+
let toolstate = if builder.try_run(&mut rustbook_cmd) {
19861974
ToolState::TestPass
19871975
} else {
19881976
ToolState::TestFail
@@ -2144,7 +2132,7 @@ fn markdown_test(builder: &Builder<'_>, compiler: Compiler, markdown: &Path) ->
21442132
cmd.arg("--test-args").arg(test_args);
21452133

21462134
if builder.config.verbose_tests {
2147-
try_run(builder, &mut cmd)
2135+
builder.try_run(&mut cmd)
21482136
} else {
21492137
try_run_quiet(builder, &mut cmd)
21502138
}
@@ -2172,7 +2160,7 @@ impl Step for RustcGuide {
21722160

21732161
let src = builder.src.join(relative_path);
21742162
let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
2175-
let toolstate = if try_run(builder, rustbook_cmd.arg("linkcheck").arg(&src)) {
2163+
let toolstate = if builder.try_run(rustbook_cmd.arg("linkcheck").arg(&src)) {
21762164
ToolState::TestPass
21772165
} else {
21782166
ToolState::TestFail
@@ -2725,7 +2713,7 @@ impl Step for Bootstrap {
27252713
.current_dir(builder.src.join("src/bootstrap/"));
27262714
// NOTE: we intentionally don't pass test_args here because the args for unittest and cargo test are mutually incompatible.
27272715
// Use `python -m unittest` manually if you want to pass arguments.
2728-
try_run(builder, &mut check_bootstrap);
2716+
builder.try_run(&mut check_bootstrap);
27292717

27302718
let mut cmd = Command::new(&builder.initial_cargo);
27312719
cmd.arg("test")
@@ -2801,7 +2789,7 @@ impl Step for TierCheck {
28012789
self.compiler.host,
28022790
self.compiler.host,
28032791
);
2804-
try_run(builder, &mut cargo.into());
2792+
builder.try_run(&mut cargo.into());
28052793
}
28062794
}
28072795

@@ -2887,7 +2875,7 @@ impl Step for RustInstaller {
28872875
cmd.env("CARGO", &builder.initial_cargo);
28882876
cmd.env("RUSTC", &builder.initial_rustc);
28892877
cmd.env("TMP_DIR", &tmpdir);
2890-
try_run(builder, &mut cmd);
2878+
builder.try_run(&mut cmd);
28912879
}
28922880

28932881
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {

src/bootstrap/tool.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ impl Step for ToolBuild {
108108
);
109109

110110
let mut cargo = Command::from(cargo);
111-
let is_expected = builder.try_run(&mut cargo).is_ok();
111+
#[allow(deprecated)] // we check this in `is_optional_tool` in a second
112+
let is_expected = builder.config.try_run(&mut cargo).is_ok();
112113

113114
builder.save_toolstate(
114115
tool,

0 commit comments

Comments
 (0)