Skip to content

Commit b9f1cbe

Browse files
committed
Use Builder::std where possible
1 parent 4a2ca11 commit b9f1cbe

File tree

10 files changed

+83
-40
lines changed

10 files changed

+83
-40
lines changed

src/bootstrap/src/core/build_steps/check.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Implementation of compiling the compiler and standard library, in "check"-based modes.
22
3-
use crate::core::build_steps::compile;
43
use crate::core::build_steps::compile::{
54
add_to_sysroot, run_cargo, rustc_cargo, rustc_cargo_env, std_cargo, std_crates_for_run_make,
65
};
@@ -106,7 +105,7 @@ impl Step for Std {
106105
}
107106

108107
// Reuse the stage0 libstd
109-
builder.ensure(compile::Std::new(compiler, target));
108+
builder.std(compiler, target);
110109
return;
111110
}
112111

@@ -253,8 +252,8 @@ impl Step for Rustc {
253252
// the sysroot for the compiler to find. Otherwise, we're going to
254253
// fail when building crates that need to generate code (e.g., build
255254
// scripts and their dependencies).
256-
builder.ensure(crate::core::build_steps::compile::Std::new(compiler, compiler.host));
257-
builder.ensure(crate::core::build_steps::compile::Std::new(compiler, target));
255+
builder.std(compiler, compiler.host);
256+
builder.std(compiler, target);
258257
} else {
259258
builder.ensure(Std::new(target).build_kind(self.override_build_kind));
260259
}

src/bootstrap/src/core/build_steps/clippy.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Implementation of running clippy on the compiler, standard library and various tools.
22
3+
use super::check;
34
use super::compile::{run_cargo, rustc_cargo, std_cargo};
45
use super::tool::{SourceType, prepare_tool_cargo};
5-
use super::{check, compile};
66
use crate::builder::{Builder, ShouldRun};
77
use crate::core::build_steps::compile::std_crates_for_run_make;
88
use crate::core::builder;
@@ -214,8 +214,8 @@ impl Step for Rustc {
214214
// the sysroot for the compiler to find. Otherwise, we're going to
215215
// fail when building crates that need to generate code (e.g., build
216216
// scripts and their dependencies).
217-
builder.ensure(compile::Std::new(compiler, compiler.host));
218-
builder.ensure(compile::Std::new(compiler, target));
217+
builder.std(compiler, compiler.host);
218+
builder.std(compiler, target);
219219
} else {
220220
builder.ensure(check::Std::new(target).build_kind(Some(Kind::Check)));
221221
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl Step for Std {
212212
{
213213
trace!(?compiler_to_use, ?compiler, "compiler != compiler_to_use, uplifting library");
214214

215-
builder.ensure(Std::new(compiler_to_use, target));
215+
builder.std(compiler_to_use, target);
216216
let msg = if compiler_to_use.host == target {
217217
format!(
218218
"Uplifting library (stage{} -> stage{})",
@@ -681,7 +681,7 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
681681
}
682682

683683
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
684-
struct StdLink {
684+
pub struct StdLink {
685685
pub compiler: Compiler,
686686
pub target_compiler: Compiler,
687687
pub target: TargetSelection,
@@ -692,7 +692,7 @@ struct StdLink {
692692
}
693693

694694
impl StdLink {
695-
fn from_std(std: Std, host_compiler: Compiler) -> Self {
695+
pub fn from_std(std: Std, host_compiler: Compiler) -> Self {
696696
Self {
697697
compiler: host_compiler,
698698
target_compiler: std.compiler,
@@ -1058,7 +1058,7 @@ impl Step for Rustc {
10581058

10591059
// Build a standard library for `target` using the `build_compiler`.
10601060
// This will be the standard library that the rustc which we build *links to*.
1061-
builder.ensure(Std::new(build_compiler, target));
1061+
builder.std(build_compiler, target);
10621062

10631063
if builder.config.keep_stage.contains(&build_compiler.stage) {
10641064
trace!(stage = build_compiler.stage, "`keep-stage` requested");
@@ -1099,10 +1099,10 @@ impl Step for Rustc {
10991099
// build scripts and proc macros.
11001100
// If we are not cross-compiling, the Std build above will be the same one as the one we
11011101
// prepare here.
1102-
builder.ensure(Std::new(
1102+
builder.std(
11031103
builder.compiler(self.build_compiler.stage, builder.config.host_target),
11041104
builder.config.host_target,
1105-
));
1105+
);
11061106

11071107
let mut cargo = builder::Cargo::new(
11081108
builder,
@@ -2062,15 +2062,15 @@ impl Step for Assemble {
20622062
if builder.download_rustc() {
20632063
trace!("`download-rustc` requested, reusing CI compiler for stage > 0");
20642064

2065-
builder.ensure(Std::new(target_compiler, target_compiler.host));
2065+
builder.std(target_compiler, target_compiler.host);
20662066
let sysroot =
20672067
builder.ensure(Sysroot { compiler: target_compiler, force_recompile: false });
20682068
// Ensure that `libLLVM.so` ends up in the newly created target directory,
20692069
// so that tools using `rustc_private` can use it.
20702070
dist::maybe_install_llvm_target(builder, target_compiler.host, &sysroot);
20712071
// Lower stages use `ci-rustc-sysroot`, not stageN
20722072
if target_compiler.stage == builder.top_stage {
2073-
builder.info(&format!("Creating a sysroot for stage{stage} compiler (use `rustup toolchain link 'name' build/host/stage{stage}`)", stage=target_compiler.stage));
2073+
builder.info(&format!("Creating a sysroot for stage{stage} compiler (use `rustup toolchain link 'name' build/host/stage{stage}`)", stage = target_compiler.stage));
20742074
}
20752075

20762076
let mut precompiled_compiler = target_compiler;

src/bootstrap/src/core/build_steps/dist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ impl Step for Std {
711711
return None;
712712
}
713713

714-
builder.ensure(compile::Std::new(compiler, target));
714+
builder.std(compiler, target);
715715

716716
let mut tarball = Tarball::new(builder, "rust-std", &target.triple);
717717
tarball.include_target_in_component_name(true);

src/bootstrap/src/core/build_steps/doc.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ impl Step for Rustc {
804804
// Build the standard library, so that proc-macros can use it.
805805
// (Normally, only the metadata would be necessary, but proc-macros are special since they run at compile-time.)
806806
let compiler = builder.compiler(stage, builder.config.host_target);
807-
builder.ensure(compile::Std::new(compiler, builder.config.host_target));
807+
builder.std(compiler, builder.config.host_target);
808808

809809
let _guard = builder.msg_sysroot_tool(
810810
Kind::Doc,
@@ -947,7 +947,7 @@ macro_rules! tool_doc {
947947
t!(fs::create_dir_all(&out));
948948

949949
let compiler = builder.compiler(stage, builder.config.host_target);
950-
builder.ensure(compile::Std::new(compiler, target));
950+
builder.std(compiler, target);
951951

952952
if true $(&& $rustc_tool)? {
953953
// Build rustc docs so that we generate relative links.
@@ -1195,7 +1195,7 @@ impl Step for RustcBook {
11951195
let rustc = builder.rustc(self.compiler);
11961196
// The tool runs `rustc` for extracting output examples, so it needs a
11971197
// functional sysroot.
1198-
builder.ensure(compile::Std::new(self.compiler, self.target));
1198+
builder.std(self.compiler, self.target);
11991199
let mut cmd = builder.tool_cmd(Tool::LintDocs);
12001200
cmd.arg("--src");
12011201
cmd.arg(builder.src.join("compiler"));
@@ -1272,7 +1272,7 @@ impl Step for Reference {
12721272

12731273
// This is needed for generating links to the standard library using
12741274
// the mdbook-spec plugin.
1275-
builder.ensure(compile::Std::new(self.compiler, builder.config.host_target));
1275+
builder.std(self.compiler, builder.config.host_target);
12761276

12771277
// Run rustbook/mdbook to generate the HTML pages.
12781278
builder.ensure(RustbookSrc {

src/bootstrap/src/core/build_steps/perf.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::env::consts::EXE_EXTENSION;
22
use std::fmt::{Display, Formatter};
33

4-
use crate::core::build_steps::compile::{Std, Sysroot};
4+
use crate::core::build_steps::compile::Sysroot;
55
use crate::core::build_steps::tool::{RustcPerf, Rustdoc};
66
use crate::core::builder::Builder;
77
use crate::core::config::DebuginfoLevel;
@@ -152,7 +152,7 @@ Consider setting `rust.debuginfo-level = 1` in `bootstrap.toml`."#);
152152
}
153153

154154
let compiler = builder.compiler(builder.top_stage, builder.config.host_target);
155-
builder.ensure(Std::new(compiler, builder.config.host_target));
155+
builder.std(compiler, builder.config.host_target);
156156

157157
if let Some(opts) = args.cmd.shared_opts()
158158
&& opts.profiles.contains(&Profile::Doc)

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{env, fs, iter};
1010

1111
use clap_complete::shells;
1212

13-
use crate::core::build_steps::compile::run_cargo;
13+
use crate::core::build_steps::compile::{Std, run_cargo};
1414
use crate::core::build_steps::doc::DocumentationFormat;
1515
use crate::core::build_steps::gcc::{Gcc, add_cg_gcc_cargo_flags};
1616
use crate::core::build_steps::llvm::get_llvm_version;
@@ -544,7 +544,7 @@ impl Step for Miri {
544544
// We also need sysroots, for Miri and for the host (the latter for build scripts).
545545
// This is for the tests so everything is done with the target compiler.
546546
let miri_sysroot = Miri::build_miri_sysroot(builder, target_compiler, target);
547-
builder.ensure(compile::Std::new(target_compiler, host));
547+
builder.std(target_compiler, host);
548548
let host_sysroot = builder.sysroot(target_compiler);
549549

550550
// Miri has its own "target dir" for ui test dependencies. Make sure it gets cleared when
@@ -709,7 +709,7 @@ impl Step for CompiletestTest {
709709

710710
// We need `ToolStd` for the locally-built sysroot because
711711
// compiletest uses unstable features of the `test` crate.
712-
builder.ensure(compile::Std::new(compiler, host));
712+
builder.std(compiler, host);
713713
let mut cargo = tool::prepare_tool_cargo(
714714
builder,
715715
compiler,
@@ -1009,7 +1009,7 @@ impl Step for RustdocGUI {
10091009
}
10101010

10111011
fn run(self, builder: &Builder<'_>) {
1012-
builder.ensure(compile::Std::new(self.compiler, self.target));
1012+
builder.std(self.compiler, self.target);
10131013

10141014
let mut cmd = builder.tool_cmd(Tool::RustdocGUITest);
10151015

@@ -1634,15 +1634,15 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the
16341634
if suite == "mir-opt" {
16351635
builder.ensure(compile::Std::new(compiler, compiler.host).is_for_mir_opt_tests(true));
16361636
} else {
1637-
builder.ensure(compile::Std::new(compiler, compiler.host));
1637+
builder.std(compiler, compiler.host);
16381638
}
16391639

16401640
let mut cmd = builder.tool_cmd(Tool::Compiletest);
16411641

16421642
if suite == "mir-opt" {
16431643
builder.ensure(compile::Std::new(compiler, target).is_for_mir_opt_tests(true));
16441644
} else {
1645-
builder.ensure(compile::Std::new(compiler, target));
1645+
builder.std(compiler, target);
16461646
}
16471647

16481648
builder.ensure(RemoteCopyLibs { compiler, target });
@@ -2177,7 +2177,7 @@ impl BookTest {
21772177
fn run_ext_doc(self, builder: &Builder<'_>) {
21782178
let compiler = self.compiler;
21792179

2180-
builder.ensure(compile::Std::new(compiler, compiler.host));
2180+
builder.std(compiler, compiler.host);
21812181

21822182
// mdbook just executes a binary named "rustdoc", so we need to update
21832183
// PATH so that it points to our rustdoc.
@@ -2263,7 +2263,7 @@ impl BookTest {
22632263
let compiler = self.compiler;
22642264
let host = self.compiler.host;
22652265

2266-
builder.ensure(compile::Std::new(compiler, host));
2266+
builder.std(compiler, host);
22672267

22682268
let _guard =
22692269
builder.msg(Kind::Test, compiler.stage, format!("book {}", self.name), host, host);
@@ -2410,7 +2410,7 @@ impl Step for ErrorIndex {
24102410
drop(guard);
24112411
// The tests themselves need to link to std, so make sure it is
24122412
// available.
2413-
builder.ensure(compile::Std::new(compiler, compiler.host));
2413+
builder.std(compiler, compiler.host);
24142414
markdown_test(builder, compiler, &output);
24152415
}
24162416
}
@@ -2473,7 +2473,7 @@ impl Step for CrateLibrustc {
24732473
}
24742474

24752475
fn run(self, builder: &Builder<'_>) {
2476-
builder.ensure(compile::Std::new(self.compiler, self.target));
2476+
builder.std(self.compiler, self.target);
24772477

24782478
// To actually run the tests, delegate to a copy of the `Crate` step.
24792479
builder.ensure(Crate {
@@ -2641,7 +2641,7 @@ impl Step for Crate {
26412641

26422642
// Prepare sysroot
26432643
// See [field@compile::Std::force_recompile].
2644-
builder.ensure(compile::Std::new(compiler, compiler.host).force_recompile(true));
2644+
builder.ensure(Std::new(compiler, compiler.host).force_recompile(true));
26452645

26462646
// If we're not doing a full bootstrap but we're testing a stage2
26472647
// version of libstd, then what we're actually testing is the libstd
@@ -2767,7 +2767,7 @@ impl Step for CrateRustdoc {
27672767
// using `download-rustc`, the rustc_private artifacts may be in a *different sysroot* from
27682768
// the target rustdoc (`ci-rustc-sysroot` vs `stage2`). In that case, we need to ensure this
27692769
// explicitly to make sure it ends up in the stage2 sysroot.
2770-
builder.ensure(compile::Std::new(compiler, target));
2770+
builder.std(compiler, target);
27712771
builder.ensure(compile::Rustc::new(compiler, target));
27722772

27732773
let mut cargo = tool::prepare_tool_cargo(
@@ -2911,7 +2911,7 @@ impl Step for RemoteCopyLibs {
29112911
return;
29122912
}
29132913

2914-
builder.ensure(compile::Std::new(compiler, target));
2914+
builder.std(compiler, target);
29152915

29162916
builder.info(&format!("REMOTE copy libs to emulator ({target})"));
29172917

@@ -3101,7 +3101,7 @@ impl Step for TierCheck {
31013101

31023102
/// Tests the Platform Support page in the rustc book.
31033103
fn run(self, builder: &Builder<'_>) {
3104-
builder.ensure(compile::Std::new(self.compiler, self.compiler.host));
3104+
builder.std(self.compiler, self.compiler.host);
31053105
let mut cargo = tool::prepare_tool_cargo(
31063106
builder,
31073107
self.compiler,
@@ -3334,7 +3334,7 @@ impl Step for CodegenCranelift {
33343334
let compiler = self.compiler;
33353335
let target = self.target;
33363336

3337-
builder.ensure(compile::Std::new(compiler, target));
3337+
builder.std(compiler, target);
33383338

33393339
// If we're not doing a full bootstrap but we're testing a stage2
33403340
// version of libstd, then what we're actually testing is the libstd

src/bootstrap/src/core/build_steps/tool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl Step for ToolBuild {
129129
Mode::ToolStd => {
130130
// If compiler was forced, its artifacts should be prepared earlier.
131131
if !self.compiler.is_forced_compiler() {
132-
builder.ensure(compile::Std::new(self.compiler, target))
132+
builder.std(self.compiler, target)
133133
}
134134
}
135135
Mode::ToolBootstrap => {} // uses downloaded stage0 compiler libs

src/bootstrap/src/core/builder/cargo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::ffi::{OsStr, OsString};
33
use std::path::{Path, PathBuf};
44

55
use super::{Builder, Kind};
6+
use crate::core::build_steps::test;
67
use crate::core::build_steps::tool::SourceType;
7-
use crate::core::build_steps::{compile, test};
88
use crate::core::config::SplitDebuginfo;
99
use crate::core::config::flags::Color;
1010
use crate::utils::build_stamp;
@@ -842,7 +842,7 @@ impl Builder<'_> {
842842

843843
// If this is for `miri-test`, prepare the sysroots.
844844
if cmd_kind == Kind::MiriTest {
845-
self.ensure(compile::Std::new(compiler, compiler.host));
845+
self.std(compiler, compiler.host);
846846
let host_sysroot = self.sysroot(compiler);
847847
let miri_sysroot = test::Miri::build_miri_sysroot(self, compiler, target);
848848
cargo.env("MIRI_SYSROOT", &miri_sysroot);

src/bootstrap/src/core/builder/mod.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use tracing::instrument;
1515

1616
pub use self::cargo::{Cargo, cargo_profile_var};
1717
pub use crate::Compiler;
18+
use crate::core::build_steps::compile::{Std, StdLink};
1819
use crate::core::build_steps::{
1920
check, clean, clippy, compile, dist, doc, gcc, install, llvm, run, setup, test, tool, vendor,
2021
};
@@ -1318,6 +1319,49 @@ impl<'a> Builder<'a> {
13181319
resolved_compiler
13191320
}
13201321

1322+
/// Obtain a standard library for the given target that will be built by the passed compiler.
1323+
/// The standard library will be linked to the sysroot of the passed compiler.
1324+
///
1325+
/// Prefer using this method rather than manually invoking `Std::new`.
1326+
#[cfg_attr(
1327+
feature = "tracing",
1328+
instrument(
1329+
level = "trace",
1330+
name = "Builder::std",
1331+
target = "STD",
1332+
skip_all,
1333+
fields(
1334+
stage = stage,
1335+
host = ?host,
1336+
),
1337+
),
1338+
)]
1339+
pub fn std(&self, compiler: Compiler, target: TargetSelection) {
1340+
// FIXME: make the `Std` step return some type-level "proof" that std was indeed built,
1341+
// and then require passing that to all Cargo invocations that we do.
1342+
1343+
// The "stage 0" std is always precompiled and comes with the stage0 compiler, so we have
1344+
// special logic for it, to avoid creating needless and confusing Std steps that don't
1345+
// actually build anything.
1346+
if compiler.stage == 0 {
1347+
if target != compiler.host {
1348+
panic!(
1349+
r"It is not possible to build the standard library for `{target}` using the stage0 compiler.
1350+
You have to build a stage1 compiler for `{}` first, and then use it to build a standard library for `{target}`.
1351+
",
1352+
compiler.host
1353+
)
1354+
}
1355+
1356+
// We still need to link the prebuilt standard library into the ephemeral stage0 sysroot
1357+
self.ensure(StdLink::from_std(Std::new(compiler, target), compiler));
1358+
} else {
1359+
// This step both compiles the std and links it into the compiler's sysroot.
1360+
// Yes, it's quite magical and side-effecty.. would be nice to refactor later.
1361+
self.ensure(Std::new(compiler, target));
1362+
}
1363+
}
1364+
13211365
pub fn sysroot(&self, compiler: Compiler) -> PathBuf {
13221366
self.ensure(compile::Sysroot::new(compiler))
13231367
}

0 commit comments

Comments
 (0)