Skip to content

Commit 2823cfb

Browse files
committed
Auto merge of #127680 - Kobzol:bootstrap-cmd-refactor-6, r=onur-ozkan
Bootstrap command refactoring: port remaining commands with access to `Build` (step 6) Continuation of #127450. This PR ports commands in bootstrap that can easily get access to `Build(er)` to `BootstrapCommand`. After this PR, everything that can access `Build(er)` should be using the new API. Statistics of `bootstrap` code (ignoring `src/bin/<shims>`) after this PR: ``` 7 usages of `Command::new` 69 usages of `command()` (new API) - out of that: 16 usages of `as_command_mut()` (new API, but accesses the inner command) ``` Tracking issue: #126819 r? `@onur-ozkan`
2 parents 5c84886 + 7a54117 commit 2823cfb

File tree

11 files changed

+147
-187
lines changed

11 files changed

+147
-187
lines changed

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

+9-17
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;
@@ -695,10 +695,10 @@ fn copy_sanitizers(
695695
|| target == "x86_64-apple-ios"
696696
{
697697
// Update the library’s install name to reflect that it has been renamed.
698-
apple_darwin_update_library_name(&dst, &format!("@rpath/{}", &runtime.name));
698+
apple_darwin_update_library_name(builder, &dst, &format!("@rpath/{}", &runtime.name));
699699
// Upon renaming the install name, the code signature of the file will invalidate,
700700
// so we will sign it again.
701-
apple_darwin_sign_file(&dst);
701+
apple_darwin_sign_file(builder, &dst);
702702
}
703703

704704
target_deps.push(dst);
@@ -707,25 +707,17 @@ fn copy_sanitizers(
707707
target_deps
708708
}
709709

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

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

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

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ pub fn prebuilt_llvm_config(builder: &Builder<'_>, target: TargetSelection) -> L
125125
static STAMP_HASH_MEMO: OnceLock<String> = OnceLock::new();
126126
let smart_stamp_hash = STAMP_HASH_MEMO.get_or_init(|| {
127127
generate_smart_stamp_hash(
128+
builder,
128129
&builder.config.src.join("src/llvm-project"),
129130
builder.in_tree_llvm_info.sha().unwrap_or_default(),
130131
)
@@ -912,7 +913,7 @@ impl Step for Lld {
912913
if let Some(clang_cl_path) = builder.config.llvm_clang_cl.as_ref() {
913914
// Find clang's runtime library directory and push that as a search path to the
914915
// cmake linker flags.
915-
let clang_rt_dir = get_clang_cl_resource_dir(clang_cl_path);
916+
let clang_rt_dir = get_clang_cl_resource_dir(builder, clang_cl_path);
916917
ldflags.push_all(format!("/libpath:{}", clang_rt_dir.display()));
917918
}
918919
}

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

+28-32
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 {
@@ -476,20 +474,18 @@ impl Step for Hook {
476474
if config.dry_run() {
477475
return;
478476
}
479-
t!(install_git_hook_maybe(config));
477+
t!(install_git_hook_maybe(builder, config));
480478
}
481479
}
482480

483481
// install a git hook to automatically run tidy, if they want
484-
fn install_git_hook_maybe(config: &Config) -> io::Result<()> {
482+
fn install_git_hook_maybe(builder: &Builder<'_>, config: &Config) -> io::Result<()> {
485483
let git = helpers::git(Some(&config.src))
484+
.capture()
486485
.args(["rev-parse", "--git-common-dir"])
487-
.as_command_mut()
488-
.output()
489-
.map(|output| {
490-
assert!(output.status.success(), "failed to run `git`");
491-
PathBuf::from(t!(String::from_utf8(output.stdout)).trim())
492-
})?;
486+
.run(builder)
487+
.stdout();
488+
let git = PathBuf::from(git.trim());
493489
let hooks_dir = git.join("hooks");
494490
let dst = hooks_dir.join("pre-push");
495491
if dst.exists() {

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

+6-12
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."
@@ -2099,9 +2095,7 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the
20992095
let git_config = builder.config.git_config();
21002096
cmd.arg("--git-repository").arg(git_config.git_repository);
21012097
cmd.arg("--nightly-branch").arg(git_config.nightly_branch);
2102-
2103-
// FIXME: Move CiEnv back to bootstrap, it is only used here anyway
2104-
builder.ci_env.force_coloring_in_ci(cmd.as_command_mut());
2098+
cmd.force_coloring_in_ci(builder.ci_env);
21052099

21062100
#[cfg(feature = "build-metrics")]
21072101
builder.metrics.begin_test_suite(

0 commit comments

Comments
 (0)