Skip to content

Commit 9dfc8ab

Browse files
committed
Use Builder::std where possible
1 parent 14e3dde commit 9dfc8ab

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 });
@@ -2178,7 +2178,7 @@ impl BookTest {
21782178
fn run_ext_doc(self, builder: &Builder<'_>) {
21792179
let compiler = self.compiler;
21802180

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

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

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

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

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

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

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

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

27742774
let mut cargo = tool::prepare_tool_cargo(
@@ -2912,7 +2912,7 @@ impl Step for RemoteCopyLibs {
29122912
return;
29132913
}
29142914

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

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

@@ -3102,7 +3102,7 @@ impl Step for TierCheck {
31023102

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

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

33403340
// If we're not doing a full bootstrap but we're testing a stage2
33413341
// 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;
@@ -844,7 +844,7 @@ impl Builder<'_> {
844844

845845
// If this is for `miri-test`, prepare the sysroots.
846846
if cmd_kind == Kind::MiriTest {
847-
self.ensure(compile::Std::new(compiler, compiler.host));
847+
self.std(compiler, compiler.host);
848848
let host_sysroot = self.sysroot(compiler);
849849
let miri_sysroot = test::Miri::build_miri_sysroot(self, compiler, target);
850850
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
};
@@ -1314,6 +1315,49 @@ impl<'a> Builder<'a> {
13141315
resolved_compiler
13151316
}
13161317

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

0 commit comments

Comments
 (0)