Skip to content

Commit e317093

Browse files
committed
Port all remaining simple command usages in bootstrap to BootstrapCommand
1 parent 8a63c84 commit e317093

File tree

5 files changed

+40
-54
lines changed

5 files changed

+40
-54
lines changed

src/bootstrap/src/core/build_steps/compile.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::fs;
1414
use std::io::prelude::*;
1515
use std::io::BufReader;
1616
use std::path::{Path, PathBuf};
17-
use std::process::{Command, Stdio};
17+
use std::process::Stdio;
1818
use std::str;
1919

2020
use serde_derive::Deserialize;
@@ -696,10 +696,10 @@ fn copy_sanitizers(
696696
|| target == "x86_64-apple-ios"
697697
{
698698
// Update the library’s install name to reflect that it has been renamed.
699-
apple_darwin_update_library_name(&dst, &format!("@rpath/{}", &runtime.name));
699+
apple_darwin_update_library_name(builder, &dst, &format!("@rpath/{}", &runtime.name));
700700
// Upon renaming the install name, the code signature of the file will invalidate,
701701
// so we will sign it again.
702-
apple_darwin_sign_file(&dst);
702+
apple_darwin_sign_file(builder, &dst);
703703
}
704704

705705
target_deps.push(dst);
@@ -708,25 +708,17 @@ fn copy_sanitizers(
708708
target_deps
709709
}
710710

711-
fn apple_darwin_update_library_name(library_path: &Path, new_name: &str) {
712-
let status = Command::new("install_name_tool")
713-
.arg("-id")
714-
.arg(new_name)
715-
.arg(library_path)
716-
.status()
717-
.expect("failed to execute `install_name_tool`");
718-
assert!(status.success());
711+
fn apple_darwin_update_library_name(builder: &Builder<'_>, library_path: &Path, new_name: &str) {
712+
command("install_name_tool").arg("-id").arg(new_name).arg(library_path).run(builder);
719713
}
720714

721-
fn apple_darwin_sign_file(file_path: &Path) {
722-
let status = Command::new("codesign")
715+
fn apple_darwin_sign_file(builder: &Builder<'_>, file_path: &Path) {
716+
command("codesign")
723717
.arg("-f") // Force to rewrite the existing signature
724718
.arg("-s")
725719
.arg("-")
726720
.arg(file_path)
727-
.status()
728-
.expect("failed to execute `codesign`");
729-
assert!(status.success());
721+
.run(builder);
730722
}
731723

732724
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -1172,7 +1164,7 @@ fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelect
11721164
if builder.config.llvm_profile_generate && target.is_msvc() {
11731165
if let Some(ref clang_cl_path) = builder.config.llvm_clang_cl {
11741166
// Add clang's runtime library directory to the search path
1175-
let clang_rt_dir = get_clang_cl_resource_dir(clang_cl_path);
1167+
let clang_rt_dir = get_clang_cl_resource_dir(builder, clang_cl_path);
11761168
llvm_linker_flags.push_str(&format!("-L{}", clang_rt_dir.display()));
11771169
}
11781170
}

src/bootstrap/src/core/build_steps/llvm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ impl Step for Lld {
912912
if let Some(clang_cl_path) = builder.config.llvm_clang_cl.as_ref() {
913913
// Find clang's runtime library directory and push that as a search path to the
914914
// cmake linker flags.
915-
let clang_rt_dir = get_clang_cl_resource_dir(clang_cl_path);
915+
let clang_rt_dir = get_clang_cl_resource_dir(builder, clang_cl_path);
916916
ldflags.push_all(format!("/libpath:{}", clang_rt_dir.display()));
917917
}
918918
}

src/bootstrap/src/core/build_steps/setup.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use crate::core::builder::{Builder, RunConfig, ShouldRun, Step};
99
use crate::t;
1010
use crate::utils::change_tracker::CONFIG_CHANGE_HISTORY;
11+
use crate::utils::exec::command;
1112
use crate::utils::helpers::{self, hex_encode};
1213
use crate::Config;
1314
use sha2::Digest;
@@ -16,7 +17,6 @@ use std::fmt::Write as _;
1617
use std::fs::File;
1718
use std::io::Write;
1819
use std::path::{Path, PathBuf, MAIN_SEPARATOR_STR};
19-
use std::process::Command;
2020
use std::str::FromStr;
2121
use std::{fmt, fs, io};
2222

@@ -266,20 +266,16 @@ impl Step for Link {
266266
}
267267
let stage_path =
268268
["build", config.build.rustc_target_arg(), "stage1"].join(MAIN_SEPARATOR_STR);
269-
if !rustup_installed() {
269+
if !rustup_installed(builder) {
270270
eprintln!("`rustup` is not installed; cannot link `stage1` toolchain");
271271
} else if stage_dir_exists(&stage_path[..]) && !config.dry_run() {
272-
attempt_toolchain_link(&stage_path[..]);
272+
attempt_toolchain_link(builder, &stage_path[..]);
273273
}
274274
}
275275
}
276276

277-
fn rustup_installed() -> bool {
278-
Command::new("rustup")
279-
.arg("--version")
280-
.stdout(std::process::Stdio::null())
281-
.output()
282-
.map_or(false, |output| output.status.success())
277+
fn rustup_installed(builder: &Builder<'_>) -> bool {
278+
command("rustup").capture_stdout().arg("--version").run(builder).is_success()
283279
}
284280

285281
fn stage_dir_exists(stage_path: &str) -> bool {
@@ -289,8 +285,8 @@ fn stage_dir_exists(stage_path: &str) -> bool {
289285
}
290286
}
291287

292-
fn attempt_toolchain_link(stage_path: &str) {
293-
if toolchain_is_linked() {
288+
fn attempt_toolchain_link(builder: &Builder<'_>, stage_path: &str) {
289+
if toolchain_is_linked(builder) {
294290
return;
295291
}
296292

@@ -301,7 +297,7 @@ fn attempt_toolchain_link(stage_path: &str) {
301297
return;
302298
}
303299

304-
if try_link_toolchain(stage_path) {
300+
if try_link_toolchain(builder, stage_path) {
305301
println!(
306302
"Added `stage1` rustup toolchain; try `cargo +stage1 build` on a separate rust project to run a newly-built toolchain"
307303
);
@@ -315,22 +311,24 @@ fn attempt_toolchain_link(stage_path: &str) {
315311
}
316312
}
317313

318-
fn toolchain_is_linked() -> bool {
319-
match Command::new("rustup")
314+
fn toolchain_is_linked(builder: &Builder<'_>) -> bool {
315+
match command("rustup")
316+
.capture_stdout()
317+
.allow_failure()
320318
.args(["toolchain", "list"])
321-
.stdout(std::process::Stdio::piped())
322-
.output()
319+
.run(builder)
320+
.stdout_if_ok()
323321
{
324-
Ok(toolchain_list) => {
325-
if !String::from_utf8_lossy(&toolchain_list.stdout).contains("stage1") {
322+
Some(toolchain_list) => {
323+
if !toolchain_list.contains("stage1") {
326324
return false;
327325
}
328326
// The toolchain has already been linked.
329327
println!(
330328
"`stage1` toolchain already linked; not attempting to link `stage1` toolchain"
331329
);
332330
}
333-
Err(_) => {
331+
None => {
334332
// In this case, we don't know if the `stage1` toolchain has been linked;
335333
// but `rustup` failed, so let's not go any further.
336334
println!(
@@ -341,12 +339,12 @@ fn toolchain_is_linked() -> bool {
341339
true
342340
}
343341

344-
fn try_link_toolchain(stage_path: &str) -> bool {
345-
Command::new("rustup")
346-
.stdout(std::process::Stdio::null())
342+
fn try_link_toolchain(builder: &Builder<'_>, stage_path: &str) -> bool {
343+
command("rustup")
344+
.capture_stdout()
347345
.args(["toolchain", "link", "stage1", stage_path])
348-
.output()
349-
.map_or(false, |output| output.status.success())
346+
.run(builder)
347+
.is_success()
350348
}
351349

352350
fn ensure_stage1_toolchain_placeholder_exists(stage_path: &str) -> bool {

src/bootstrap/src/core/build_steps/test.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::ffi::OsString;
99
use std::fs;
1010
use std::iter;
1111
use std::path::{Path, PathBuf};
12-
use std::process::{Command, Stdio};
1312

1413
use clap_complete::shells;
1514

@@ -169,12 +168,8 @@ You can skip linkcheck with --skip src/tools/linkchecker"
169168
}
170169
}
171170

172-
fn check_if_tidy_is_installed() -> bool {
173-
Command::new("tidy")
174-
.arg("--version")
175-
.stdout(Stdio::null())
176-
.status()
177-
.map_or(false, |status| status.success())
171+
fn check_if_tidy_is_installed(builder: &Builder<'_>) -> bool {
172+
command("tidy").capture_stdout().allow_failure().arg("--version").run(builder).is_success()
178173
}
179174

180175
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -188,16 +183,17 @@ impl Step for HtmlCheck {
188183
const ONLY_HOSTS: bool = true;
189184

190185
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
186+
let builder = run.builder;
191187
let run = run.path("src/tools/html-checker");
192-
run.lazy_default_condition(Box::new(check_if_tidy_is_installed))
188+
run.lazy_default_condition(Box::new(|| check_if_tidy_is_installed(builder)))
193189
}
194190

195191
fn make_run(run: RunConfig<'_>) {
196192
run.builder.ensure(HtmlCheck { target: run.target });
197193
}
198194

199195
fn run(self, builder: &Builder<'_>) {
200-
if !check_if_tidy_is_installed() {
196+
if !check_if_tidy_is_installed(builder) {
201197
eprintln!("not running HTML-check tool because `tidy` is missing");
202198
eprintln!(
203199
"You need the HTML tidy tool https://www.html-tidy.org/, this tool is *not* part of the rust project and needs to be installed separately, for example via your package manager."

src/bootstrap/src/utils/helpers.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,13 @@ fn dir_up_to_date(src: &Path, threshold: SystemTime) -> bool {
338338
/// When `clang-cl` is used with instrumentation, we need to add clang's runtime library resource
339339
/// directory to the linker flags, otherwise there will be linker errors about the profiler runtime
340340
/// missing. This function returns the path to that directory.
341-
pub fn get_clang_cl_resource_dir(clang_cl_path: &str) -> PathBuf {
341+
pub fn get_clang_cl_resource_dir(builder: &Builder<'_>, clang_cl_path: &str) -> PathBuf {
342342
// Similar to how LLVM does it, to find clang's library runtime directory:
343343
// - we ask `clang-cl` to locate the `clang_rt.builtins` lib.
344-
let mut builtins_locator = Command::new(clang_cl_path);
344+
let mut builtins_locator = command(clang_cl_path);
345345
builtins_locator.args(["/clang:-print-libgcc-file-name", "/clang:--rtlib=compiler-rt"]);
346346

347-
let clang_rt_builtins = output(&mut builtins_locator);
347+
let clang_rt_builtins = builtins_locator.capture_stdout().run(builder).stdout();
348348
let clang_rt_builtins = Path::new(clang_rt_builtins.trim());
349349
assert!(
350350
clang_rt_builtins.exists(),

0 commit comments

Comments
 (0)