diff --git a/src/bootstrap/src/core/build_steps/compile.rs b/src/bootstrap/src/core/build_steps/compile.rs index d30005c8d51d9..0b75e85772f86 100644 --- a/src/bootstrap/src/core/build_steps/compile.rs +++ b/src/bootstrap/src/core/build_steps/compile.rs @@ -99,28 +99,12 @@ impl Std { deps } - /// Returns true if the standard library will be uplifted from stage 1 for the given - /// `build_compiler` (which determines the stdlib stage) and `target`. + /// Returns true if the standard library should be uplifted from stage 1. /// - /// Uplifting is enabled if we're building a stage2+ libstd, full bootstrap is - /// disabled and we have a stage1 libstd already compiled for the given target. - pub fn should_be_uplifted_from_stage_1( - builder: &Builder<'_>, - stage: u32, - target: TargetSelection, - ) -> bool { - stage > 1 - && !builder.config.full_bootstrap - // This estimates if a stage1 libstd exists for the given target. If we're not - // cross-compiling, it should definitely exist by the time we're building a stage2 - // libstd. - // Or if we are cross-compiling, and we are building a cross-compiled rustc, then that - // rustc needs to link to a cross-compiled libstd, so again we should have a stage1 - // libstd for the given target prepared. - // Even if we guess wrong in the cross-compiled case, the worst that should happen is - // that we build a fresh stage1 libstd below, and then we immediately uplift it, so we - // don't pay the libstd build cost twice. - && (target == builder.host_target || builder.config.hosts.contains(&target)) + /// Uplifting is enabled if we're building a stage2+ libstd and full bootstrap is + /// disabled. + pub fn should_be_uplifted_from_stage_1(builder: &Builder<'_>, stage: u32) -> bool { + stage > 1 && !builder.config.full_bootstrap } } @@ -150,7 +134,9 @@ impl Step for Std { trace!(force_recompile); run.builder.ensure(Std { - build_compiler: run.builder.compiler(run.builder.top_stage, run.build_triple()), + // Note: we don't use compiler_for_std here, so that `x build library --stage 2` + // builds a stage2 rustc. + build_compiler: run.builder.compiler(run.builder.top_stage, builder.host_target), target: run.target, crates, force_recompile, @@ -226,7 +212,7 @@ impl Step for Std { // Stage of the stdlib that we're building let stage = build_compiler.stage; - if Self::should_be_uplifted_from_stage_1(builder, build_compiler.stage, target) { + if Self::should_be_uplifted_from_stage_1(builder, build_compiler.stage) { let build_compiler_for_std_to_uplift = builder.compiler(1, builder.host_target); let stage_1_stamp = builder.std(build_compiler_for_std_to_uplift, target); diff --git a/src/bootstrap/src/core/build_steps/dist.rs b/src/bootstrap/src/core/build_steps/dist.rs index 778c3beb50f35..f113dd7683d87 100644 --- a/src/bootstrap/src/core/build_steps/dist.rs +++ b/src/bootstrap/src/core/build_steps/dist.rs @@ -96,6 +96,8 @@ impl Step for Docs { } } +/// Builds the `rust-docs-json` installer component. +/// It contains the documentation of the standard library in JSON format. #[derive(Debug, Clone, Hash, PartialEq, Eq)] pub struct JsonDocs { build_compiler: Compiler, @@ -113,12 +115,11 @@ impl Step for JsonDocs { fn make_run(run: RunConfig<'_>) { run.builder.ensure(JsonDocs { - build_compiler: run.builder.compiler(run.builder.top_stage, run.builder.host_target), + build_compiler: run.builder.compiler_for_std(run.builder.top_stage), target: run.target, }); } - /// Builds the `rust-docs-json` installer component. fn run(self, builder: &Builder<'_>) -> Option { let target = self.target; let directory = builder.ensure(crate::core::build_steps::doc::Std::from_build_compiler( @@ -135,6 +136,10 @@ impl Step for JsonDocs { tarball.add_bulk_dir(directory, dest); Some(tarball.generate()) } + + fn metadata(&self) -> Option { + Some(StepMetadata::dist("json-docs", self.target).built_by(self.build_compiler)) + } } /// Builds the `rustc-docs` installer component. @@ -764,22 +769,7 @@ pub struct Std { impl Std { pub fn new(builder: &Builder<'_>, target: TargetSelection) -> Self { - // This is an important optimization mainly for CI. - // Normally, to build stage N libstd, we need stage N rustc. - // However, if we know that we will uplift libstd from stage 1 anyway, building the stage N - // rustc can be wasteful. - // In particular, if we do a cross-compiling dist stage 2 build from T1 to T2, we need: - // - stage 2 libstd for T2 (uplifted from stage 1, where it was built by T1 rustc) - // - stage 2 rustc for T2 - // However, without this optimization, we would also build stage 2 rustc for **T1**, which - // is completely wasteful. - let build_compiler = - if compile::Std::should_be_uplifted_from_stage_1(builder, builder.top_stage, target) { - builder.compiler(1, builder.host_target) - } else { - builder.compiler(builder.top_stage, builder.host_target) - }; - Std { build_compiler, target } + Std { build_compiler: builder.compiler_for_std(builder.top_stage), target } } } diff --git a/src/bootstrap/src/core/build_steps/doc.rs b/src/bootstrap/src/core/build_steps/doc.rs index 8c20c8c479af0..9ef1fee1fecc5 100644 --- a/src/bootstrap/src/core/build_steps/doc.rs +++ b/src/bootstrap/src/core/build_steps/doc.rs @@ -616,7 +616,7 @@ impl Step for Std { return; } run.builder.ensure(Std { - build_compiler: run.builder.compiler(run.builder.top_stage, run.builder.host_target), + build_compiler: run.builder.compiler_for_std(run.builder.top_stage), target: run.target, format: if run.builder.config.cmd.json() { DocumentationFormat::Json diff --git a/src/bootstrap/src/core/builder/mod.rs b/src/bootstrap/src/core/builder/mod.rs index f794f4e079aeb..b224a7e73221d 100644 --- a/src/bootstrap/src/core/builder/mod.rs +++ b/src/bootstrap/src/core/builder/mod.rs @@ -1360,6 +1360,30 @@ impl<'a> Builder<'a> { self.ensure(compile::Assemble { target_compiler: Compiler::new(stage, host) }) } + /// This function can be used to provide a build compiler for building + /// the standard library, in order to avoid unnecessary rustc builds in case where std uplifting + /// would happen anyway. + /// + /// This is an important optimization mainly for CI. + /// + /// Normally, to build stage N libstd, we need stage N rustc. + /// However, if we know that we will uplift libstd from stage 1 anyway, building the stage N + /// rustc can be wasteful. + /// In particular, if we do a cross-compiling dist stage 2 build from target1 to target2, + /// we need: + /// - stage 2 libstd for target2 (uplifted from stage 1, where it was built by target1 rustc) + /// - stage 2 rustc for target2 + /// + /// However, without this optimization, we would also build stage 2 rustc for **target1**, + /// which is completely wasteful. + pub fn compiler_for_std(&self, stage: u32) -> Compiler { + if compile::Std::should_be_uplifted_from_stage_1(self, stage) { + self.compiler(1, self.host_target) + } else { + self.compiler(stage, self.host_target) + } + } + /// Similar to `compiler`, except handles the full-bootstrap option to /// silently use the stage1 compiler instead of a stage2 compiler if one is /// requested. diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index 3c2cb7828501d..b079117c5a7d1 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -1066,6 +1066,7 @@ mod snapshot { [build] rustc 1 -> rustc 2 [build] rustc 2 -> std 2 [build] rustc 2 -> std 2 + [build] rustc 1 -> std 1 [build] rustc 2 -> std 2 "); } @@ -1122,9 +1123,8 @@ mod snapshot { [doc] book/2018-edition (book) [build] rustdoc 1 [doc] rustc 1 -> standalone 2 + [doc] rustc 1 -> std 1 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [build] rustc 1 -> rustc 2 - [build] rustdoc 2 - [doc] rustc 2 -> std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [build] rustc 1 -> error-index 2 [doc] rustc 1 -> error-index 2 [doc] nomicon (book) @@ -1141,8 +1141,10 @@ mod snapshot { [doc] rustc 1 -> releases 2 [build] rustc 0 -> RustInstaller 1 [dist] docs - [doc] rustc 2 -> std 2 crates=[] + [doc] rustc 1 -> std 1 crates=[] + [dist] rustc 1 -> json-docs 2 [dist] mingw + [build] rustdoc 2 [build] rustc 0 -> GenerateCopyright 1 [dist] rustc [dist] rustc 1 -> std 1 @@ -1182,12 +1184,11 @@ mod snapshot { [doc] book/2018-edition (book) [build] rustdoc 1 [doc] rustc 1 -> standalone 2 + [doc] rustc 1 -> std 1 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [build] rustc 1 -> rustc 2 [build] rustc 1 -> LldWrapper 2 [build] rustc 1 -> WasmComponentLd 2 [build] rustc 1 -> LlvmBitcodeLinker 2 - [build] rustdoc 2 - [doc] rustc 2 -> std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [build] rustc 1 -> error-index 2 [doc] rustc 1 -> error-index 2 [doc] nomicon (book) @@ -1204,8 +1205,10 @@ mod snapshot { [doc] rustc 1 -> releases 2 [build] rustc 0 -> RustInstaller 1 [dist] docs - [doc] rustc 2 -> std 2 crates=[] + [doc] rustc 1 -> std 1 crates=[] + [dist] rustc 1 -> json-docs 2 [dist] mingw + [build] rustdoc 2 [build] rustc 1 -> rust-analyzer-proc-macro-srv 2 [build] rustc 0 -> GenerateCopyright 1 [dist] rustc @@ -1226,6 +1229,8 @@ mod snapshot { [build] rustc 1 -> miri 2 [build] rustc 1 -> cargo-miri 2 [dist] rustc 1 -> miri 2 + [doc] rustc 2 -> std 2 crates=[] + [dist] rustc 2 -> json-docs 3 [dist] rustc 1 -> extended 2 [dist] reproducible-artifacts "); @@ -1259,10 +1264,9 @@ mod snapshot { [doc] book/2018-edition (book) [doc] rustc 1 -> standalone 2 [doc] rustc 1 -> standalone 2 + [doc] rustc 1 -> std 1 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [doc] rustc 1 -> std 1 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [build] rustc 1 -> rustc 2 - [build] rustdoc 2 - [doc] rustc 2 -> std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] - [doc] rustc 2 -> std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [build] rustc 1 -> error-index 2 [doc] rustc 1 -> error-index 2 [doc] nomicon (book) @@ -1290,15 +1294,17 @@ mod snapshot { [build] rustc 0 -> RustInstaller 1 [dist] docs [dist] docs - [doc] rustc 2 -> std 2 crates=[] - [doc] rustc 2 -> std 2 crates=[] + [doc] rustc 1 -> std 1 crates=[] + [dist] rustc 1 -> json-docs 2 + [doc] rustc 1 -> std 1 crates=[] + [dist] rustc 1 -> json-docs 2 [dist] mingw [dist] mingw + [build] rustdoc 2 [build] rustc 0 -> GenerateCopyright 1 [dist] rustc [dist] rustc 1 -> std 1 - [build] rustc 2 -> std 2 - [dist] rustc 2 -> std 2 + [dist] rustc 1 -> std 1 [dist] rustc 1 -> rustc-dev 2 [dist] src <> [dist] reproducible-artifacts @@ -1327,9 +1333,8 @@ mod snapshot { [doc] book/2018-edition (book) [build] rustdoc 1 [doc] rustc 1 -> standalone 2 + [doc] rustc 1 -> std 1 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [build] rustc 1 -> rustc 2 - [build] rustdoc 2 - [doc] rustc 2 -> std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [build] rustc 1 -> error-index 2 [doc] rustc 1 -> error-index 2 [build] llvm @@ -1352,8 +1357,10 @@ mod snapshot { [doc] rustc 1 -> releases 2 [build] rustc 0 -> RustInstaller 1 [dist] docs - [doc] rustc 2 -> std 2 crates=[] + [doc] rustc 1 -> std 1 crates=[] + [dist] rustc 1 -> json-docs 2 [dist] mingw + [build] rustdoc 2 [build] rustc 0 -> GenerateCopyright 1 [dist] rustc [build] rustdoc 2 @@ -1396,10 +1403,9 @@ mod snapshot { [doc] book/2018-edition (book) [doc] rustc 1 -> standalone 2 [doc] rustc 1 -> standalone 2 + [doc] rustc 1 -> std 1 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [doc] rustc 1 -> std 1 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [build] rustc 1 -> rustc 2 - [build] rustdoc 2 - [doc] rustc 2 -> std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] - [doc] rustc 2 -> std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [build] rustc 1 -> error-index 2 [doc] rustc 1 -> error-index 2 [build] llvm @@ -1432,10 +1438,13 @@ mod snapshot { [build] rustc 0 -> RustInstaller 1 [dist] docs [dist] docs - [doc] rustc 2 -> std 2 crates=[] - [doc] rustc 2 -> std 2 crates=[] + [doc] rustc 1 -> std 1 crates=[] + [dist] rustc 1 -> json-docs 2 + [doc] rustc 1 -> std 1 crates=[] + [dist] rustc 1 -> json-docs 2 [dist] mingw [dist] mingw + [build] rustdoc 2 [build] rustc 0 -> GenerateCopyright 1 [dist] rustc [build] rustdoc 2 @@ -1473,9 +1482,7 @@ mod snapshot { [build] rustdoc 1 [build] rustc 1 -> std 1 [doc] rustc 1 -> standalone 2 - [build] rustc 1 -> rustc 2 - [build] rustdoc 2 - [doc] rustc 2 -> std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [doc] rustc 1 -> std 1 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [doc] nomicon (book) [doc] rustc 1 -> reference (book) 2 [doc] rustdoc (book) @@ -1488,10 +1495,10 @@ mod snapshot { [doc] rustc 1 -> releases 2 [build] rustc 0 -> RustInstaller 1 [dist] docs - [doc] rustc 2 -> std 2 crates=[] + [doc] rustc 1 -> std 1 crates=[] + [dist] rustc 1 -> json-docs 2 [dist] mingw - [build] rustc 2 -> std 2 - [dist] rustc 2 -> std 2 + [dist] rustc 1 -> std 1 "); } @@ -1519,10 +1526,7 @@ mod snapshot { [build] rustdoc 1 [build] rustc 1 -> std 1 [doc] rustc 1 -> standalone 2 - [build] rustc 1 -> rustc 2 - [build] rustc 1 -> WasmComponentLd 2 - [build] rustdoc 2 - [doc] rustc 2 -> std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [doc] rustc 1 -> std 1 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [build] llvm [build] rustc 1 -> rustc 2 [build] rustc 1 -> WasmComponentLd 2 @@ -1542,7 +1546,8 @@ mod snapshot { [doc] rustc 1 -> releases 2 [build] rustc 0 -> RustInstaller 1 [dist] docs - [doc] rustc 2 -> std 2 crates=[] + [doc] rustc 1 -> std 1 crates=[] + [dist] rustc 1 -> json-docs 2 [dist] mingw [build] rustdoc 2 [build] rustc 1 -> rust-analyzer-proc-macro-srv 2 @@ -1567,13 +1572,14 @@ mod snapshot { [dist] rustc 1 -> miri 2 [build] rustc 1 -> LlvmBitcodeLinker 2 [doc] rustc 2 -> std 2 crates=[] + [dist] rustc 2 -> json-docs 3 [dist] rustc 1 -> extended 2 [dist] reproducible-artifacts "); } /// Simulates e.g. the powerpc64 builder, which is fully cross-compiled from x64, but it does - /// not build docs. Crutically, it shouldn't build host stage 2 rustc. + /// not build docs. Crucially, it shouldn't build host stage 2 rustc. /// /// This is a regression test for /// and . @@ -1663,10 +1669,9 @@ mod snapshot { [doc] book/2018-edition (book) [build] rustdoc 1 [doc] rustc 1 -> standalone 2 + [doc] rustc 1 -> std 1 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [build] rustc 1 -> rustc 2 [build] rustc 1 -> rustc_codegen_cranelift 2 - [build] rustdoc 2 - [doc] rustc 2 -> std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [build] rustc 1 -> error-index 2 [doc] rustc 1 -> error-index 2 [doc] nomicon (book) @@ -1683,8 +1688,10 @@ mod snapshot { [doc] rustc 1 -> releases 2 [build] rustc 0 -> RustInstaller 1 [dist] docs - [doc] rustc 2 -> std 2 crates=[] + [doc] rustc 1 -> std 1 crates=[] + [dist] rustc 1 -> json-docs 2 [dist] mingw + [build] rustdoc 2 [build] rustc 0 -> GenerateCopyright 1 [dist] rustc [dist] rustc 1 -> rustc_codegen_cranelift 2 @@ -1724,6 +1731,46 @@ mod snapshot { "); } + #[test] + fn dist_rustc_docs() { + let ctx = TestCtx::new(); + insta::assert_snapshot!( + ctx + .config("dist") + .path("rustc-docs") + .render_steps(), @r" + [build] rustc 0 -> UnstableBookGen 1 + [build] rustc 0 -> Rustbook 1 + [doc] unstable-book (book) + [build] llvm + [build] rustc 0 -> rustc 1 + [build] rustc 1 -> std 1 + [doc] book (book) + [doc] book/first-edition (book) + [doc] book/second-edition (book) + [doc] book/2018-edition (book) + [build] rustdoc 1 + [doc] rustc 1 -> standalone 2 + [doc] rustc 1 -> std 1 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [build] rustc 1 -> rustc 2 + [build] rustc 1 -> error-index 2 + [doc] rustc 1 -> error-index 2 + [doc] nomicon (book) + [doc] rustc 1 -> reference (book) 2 + [doc] rustdoc (book) + [doc] rust-by-example (book) + [build] rustc 0 -> LintDocs 1 + [doc] rustc (book) + [doc] cargo (book) + [doc] clippy (book) + [doc] embedded-book (book) + [doc] edition-guide (book) + [doc] style-guide (book) + [doc] rustc 1 -> releases 2 + [build] rustc 0 -> RustInstaller 1 + "); + } + #[test] fn check_compiler_no_explicit_stage() { let ctx = TestCtx::new(); @@ -2415,10 +2462,9 @@ mod snapshot { [doc] book/2018-edition (book) [build] rustdoc 1 [doc] rustc 1 -> standalone 2 + [doc] rustc 1 -> std 1 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [build] rustc 1 -> rustc 2 [build] rustc 1 -> WasmComponentLd 2 - [build] rustdoc 2 - [doc] rustc 2 -> std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [build] rustc 1 -> error-index 2 [doc] rustc 1 -> error-index 2 [doc] nomicon (book) @@ -2436,6 +2482,7 @@ mod snapshot { [build] rustc 0 -> RustInstaller 1 [dist] docs [dist] rustc 1 -> std 1 + [build] rustdoc 2 [build] rustc 1 -> rust-analyzer-proc-macro-srv 2 [build] rustc 0 -> GenerateCopyright 1 [dist] rustc