diff --git a/src/bootstrap/src/core/build_steps/compile.rs b/src/bootstrap/src/core/build_steps/compile.rs index 3e2bdc2d6b5ae..caf2d83d213ff 100644 --- a/src/bootstrap/src/core/build_steps/compile.rs +++ b/src/bootstrap/src/core/build_steps/compile.rs @@ -449,26 +449,24 @@ fn copy_self_contained_objects( target_deps } -/// Resolves standard library crates for `Std::run_make` for any build kind (like check, build, clippy, etc.). +/// Resolves standard library crates for `Std::run_make` for any build kind (like check, doc, +/// build, clippy, etc.). pub fn std_crates_for_run_make(run: &RunConfig<'_>) -> Vec { - // FIXME: Extend builder tests to cover the `crates` field of `Std` instances. - if cfg!(test) { - return vec![]; - } - - let has_alias = run.paths.iter().any(|set| set.assert_single_path().path.ends_with("library")); + let mut crates = run.make_run_crates(builder::Alias::Library); + + // For no_std targets, we only want to check core and alloc + // Regardless of core/alloc being selected explicitly or via the "library" default alias, + // we only want to keep these two crates. + // The set of no_std crates should be kept in sync with what `Builder::std_cargo` does. + // Note: an alternative design would be to return an enum from this function (Default vs Subset) + // of crates. However, several steps currently pass `-p ` even if all crates are + // selected, because Cargo behaves differently in that case. To keep that behavior without + // making further changes, we pre-filter the no-std crates here. let target_is_no_std = run.builder.no_std(run.target).unwrap_or(false); - - // For no_std targets, do not add any additional crates to the compilation other than what `compile::std_cargo` already adds for no_std targets. if target_is_no_std { - vec![] - } - // If the paths include "library", build the entire standard library. - else if has_alias { - run.make_run_crates(builder::Alias::Library) - } else { - run.cargo_crates_in_set() + crates.retain(|c| c == "core" || c == "alloc"); } + crates } /// Tries to find LLVM's `compiler-rt` source directory, for building `library/profiler_builtins`. diff --git a/src/bootstrap/src/core/build_steps/doc.rs b/src/bootstrap/src/core/build_steps/doc.rs index f7c4c5ad0bbd3..48dde4aae68a1 100644 --- a/src/bootstrap/src/core/build_steps/doc.rs +++ b/src/bootstrap/src/core/build_steps/doc.rs @@ -665,7 +665,11 @@ impl Step for Std { } fn metadata(&self) -> Option { - Some(StepMetadata::doc("std", self.target).stage(self.stage)) + Some( + StepMetadata::doc("std", self.target) + .stage(self.stage) + .with_metadata(format!("crates=[{}]", self.crates.join(","))), + ) } } diff --git a/src/bootstrap/src/core/builder/mod.rs b/src/bootstrap/src/core/builder/mod.rs index b96a988cde3ff..72e5728f2c2b1 100644 --- a/src/bootstrap/src/core/builder/mod.rs +++ b/src/bootstrap/src/core/builder/mod.rs @@ -146,6 +146,8 @@ pub struct StepMetadata { target: TargetSelection, built_by: Option, stage: Option, + /// Additional opaque string printed in the metadata + metadata: Option, } impl StepMetadata { @@ -170,7 +172,7 @@ impl StepMetadata { } fn new(name: &'static str, target: TargetSelection, kind: Kind) -> Self { - Self { name, kind, target, built_by: None, stage: None } + Self { name, kind, target, built_by: None, stage: None, metadata: None } } pub fn built_by(mut self, compiler: Compiler) -> Self { @@ -183,6 +185,11 @@ impl StepMetadata { self } + pub fn with_metadata(mut self, metadata: String) -> Self { + self.metadata = Some(metadata); + self + } + pub fn get_stage(&self) -> Option { self.stage.or(self .built_by diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index 1d5690a819716..9ca79131b8da8 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -637,8 +637,8 @@ mod snapshot { use crate::core::build_steps::{compile, dist, doc, test, tool}; use crate::core::builder::tests::{ - TEST_TRIPLE_1, TEST_TRIPLE_2, TEST_TRIPLE_3, configure, configure_with_args, first, - host_target, render_steps, run_build, + RenderConfig, TEST_TRIPLE_1, TEST_TRIPLE_2, TEST_TRIPLE_3, configure, configure_with_args, + first, host_target, render_steps, run_build, }; use crate::core::builder::{Builder, Kind, StepDescription, StepMetadata}; use crate::core::config::TargetSelection; @@ -942,7 +942,7 @@ mod snapshot { [build] llvm [build] rustc 0 -> rustc 1 [build] rustdoc 0 - [doc] std 1 + [doc] std 1 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,std,sysroot,test,unwind] "); } @@ -991,12 +991,12 @@ mod snapshot { [build] rustc 1 -> std 1 [build] rustc 1 -> rustc 2 [build] rustdoc 1 - [doc] std 2 + [doc] std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,std,sysroot,test,unwind] [build] rustc 2 -> std 2 [build] rustc 0 -> LintDocs 1 [build] rustc 0 -> RustInstaller 1 [dist] docs - [doc] std 2 + [doc] std 2 crates=[] [dist] mingw [build] rustc 0 -> GenerateCopyright 1 [dist] rustc @@ -1023,12 +1023,12 @@ mod snapshot { [build] rustc 1 -> rustc 2 [build] rustc 1 -> WasmComponentLd 2 [build] rustdoc 1 - [doc] std 2 + [doc] std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,std,sysroot,test,unwind] [build] rustc 2 -> std 2 [build] rustc 0 -> LintDocs 1 [build] rustc 0 -> RustInstaller 1 [dist] docs - [doc] std 2 + [doc] std 2 crates=[] [dist] mingw [build] rustc 0 -> GenerateCopyright 1 [dist] rustc @@ -1059,15 +1059,15 @@ mod snapshot { [build] rustc 1 -> std 1 [build] rustc 1 -> rustc 2 [build] rustdoc 1 - [doc] std 2 - [doc] std 2 + [doc] std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,std,sysroot,test,unwind] + [doc] std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,std,sysroot,test,unwind] [build] rustc 2 -> std 2 [build] rustc 0 -> LintDocs 1 [build] rustc 0 -> RustInstaller 1 [dist] docs [dist] docs - [doc] std 2 - [doc] std 2 + [doc] std 2 crates=[] + [doc] std 2 crates=[] [dist] mingw [dist] mingw [build] rustc 0 -> GenerateCopyright 1 @@ -1096,14 +1096,14 @@ mod snapshot { [build] rustc 1 -> std 1 [build] rustc 1 -> rustc 2 [build] rustdoc 1 - [doc] std 2 + [doc] std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,std,sysroot,test,unwind] [build] rustc 2 -> std 2 [build] rustc 0 -> LintDocs 1 [build] rustc 1 -> std 1 [build] rustc 2 -> std 2 [build] rustc 0 -> RustInstaller 1 [dist] docs - [doc] std 2 + [doc] std 2 crates=[] [dist] mingw [build] rustc 0 -> GenerateCopyright 1 [dist] rustc @@ -1133,8 +1133,8 @@ mod snapshot { [build] rustc 1 -> std 1 [build] rustc 1 -> rustc 2 [build] rustdoc 1 - [doc] std 2 - [doc] std 2 + [doc] std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,std,sysroot,test,unwind] + [doc] std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,std,sysroot,test,unwind] [build] rustc 2 -> std 2 [build] rustc 0 -> LintDocs 1 [build] rustc 1 -> std 1 @@ -1142,8 +1142,8 @@ mod snapshot { [build] rustc 0 -> RustInstaller 1 [dist] docs [dist] docs - [doc] std 2 - [doc] std 2 + [doc] std 2 crates=[] + [doc] std 2 crates=[] [dist] mingw [dist] mingw [build] rustc 0 -> GenerateCopyright 1 @@ -1175,11 +1175,11 @@ mod snapshot { [build] rustc 1 -> std 1 [build] rustc 1 -> rustc 2 [build] rustdoc 1 - [doc] std 2 + [doc] std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,std,sysroot,test,unwind] [build] rustc 2 -> std 2 [build] rustc 0 -> RustInstaller 1 [dist] docs - [doc] std 2 + [doc] std 2 crates=[] [dist] mingw [build] rustc 2 -> std 2 [dist] rustc 2 -> std 2 @@ -1207,14 +1207,14 @@ mod snapshot { [build] rustc 1 -> rustc 2 [build] rustc 1 -> WasmComponentLd 2 [build] rustdoc 1 - [doc] std 2 + [doc] std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,std,sysroot,test,unwind] [build] rustc 2 -> std 2 [build] rustc 1 -> std 1 [build] rustc 2 -> std 2 [build] rustc 0 -> LintDocs 1 [build] rustc 0 -> RustInstaller 1 [dist] docs - [doc] std 2 + [doc] std 2 crates=[] [dist] mingw [build] llvm [build] rustc 1 -> rustc 2 @@ -1521,6 +1521,80 @@ mod snapshot { steps.assert_contains(StepMetadata::test("CrateLibrustc", host)); steps.assert_contains_fuzzy(StepMetadata::build("rustc", host)); } + + #[test] + fn doc_library() { + let ctx = TestCtx::new(); + insta::assert_snapshot!( + ctx.config("doc") + .path("library") + .render_steps(), @r" + [build] llvm + [build] rustc 0 -> rustc 1 + [build] rustdoc 0 + [doc] std 1 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,std,sysroot,test,unwind] + "); + } + + #[test] + fn doc_core() { + let ctx = TestCtx::new(); + insta::assert_snapshot!( + ctx.config("doc") + .path("core") + .render_steps(), @r" + [build] llvm + [build] rustc 0 -> rustc 1 + [build] rustdoc 0 + [doc] std 1 crates=[core] + "); + } + + #[test] + fn doc_core_no_std_target() { + let ctx = TestCtx::new(); + insta::assert_snapshot!( + ctx.config("doc") + .path("core") + .override_target_no_std(&host_target()) + .render_steps(), @r" + [build] llvm + [build] rustc 0 -> rustc 1 + [build] rustdoc 0 + [doc] std 1 crates=[core] + "); + } + + #[test] + fn doc_library_no_std_target() { + let ctx = TestCtx::new(); + insta::assert_snapshot!( + ctx.config("doc") + .path("library") + .override_target_no_std(&host_target()) + .render_steps(), @r" + [build] llvm + [build] rustc 0 -> rustc 1 + [build] rustdoc 0 + [doc] std 1 crates=[alloc,core] + "); + } + + #[test] + fn doc_library_no_std_target_cross_compile() { + let ctx = TestCtx::new(); + insta::assert_snapshot!( + ctx.config("doc") + .path("library") + .targets(&[TEST_TRIPLE_1]) + .override_target_no_std(TEST_TRIPLE_1) + .render_steps(), @r" + [build] llvm + [build] rustc 0 -> rustc 1 + [build] rustdoc 0 + [doc] std 1 crates=[alloc,core] + "); + } } struct ExecutedSteps { @@ -1529,7 +1603,11 @@ struct ExecutedSteps { impl ExecutedSteps { fn render(&self) -> String { - render_steps(&self.steps) + self.render_with(RenderConfig::default()) + } + + fn render_with(&self, config: RenderConfig) -> String { + render_steps(&self.steps, config) } #[track_caller] @@ -1538,7 +1616,7 @@ impl ExecutedSteps { if !self.contains(&metadata) { panic!( "Metadata `{}` ({metadata:?}) not found in executed steps:\n{}", - render_metadata(&metadata), + render_metadata(&metadata, &RenderConfig::default()), self.render() ); } @@ -1553,7 +1631,7 @@ impl ExecutedSteps { if !self.contains_fuzzy(&metadata) { panic!( "Metadata `{}` ({metadata:?}) not found in executed steps:\n{}", - render_metadata(&metadata), + render_metadata(&metadata, &RenderConfig::default()), self.render() ); } @@ -1565,7 +1643,7 @@ impl ExecutedSteps { if self.contains(&metadata) { panic!( "Metadata `{}` ({metadata:?}) found in executed steps (it should not be there):\n{}", - render_metadata(&metadata), + render_metadata(&metadata, &RenderConfig::default()), self.render() ); } @@ -1587,7 +1665,7 @@ impl ExecutedSteps { } fn fuzzy_metadata_eq(executed: &StepMetadata, to_match: &StepMetadata) -> bool { - let StepMetadata { name, kind, target, built_by: _, stage: _ } = executed; + let StepMetadata { name, kind, target, built_by: _, stage: _, metadata } = executed; *name == to_match.name && *kind == to_match.kind && *target == to_match.target } @@ -1618,6 +1696,16 @@ impl ConfigBuilder { } } +struct RenderConfig { + normalize_host: bool, +} + +impl Default for RenderConfig { + fn default() -> Self { + Self { normalize_host: true } + } +} + /// Renders the executed bootstrap steps for usage in snapshot tests with insta. /// Only renders certain important steps. /// Each value in `steps` should be a tuple of (Step, step output). @@ -1625,7 +1713,7 @@ impl ConfigBuilder { /// The arrow in the rendered output (`X -> Y`) means `X builds Y`. /// This is similar to the output printed by bootstrap to stdout, but here it is /// generated purely for the purpose of tests. -fn render_steps(steps: &[ExecutedStep]) -> String { +fn render_steps(steps: &[ExecutedStep], config: RenderConfig) -> String { steps .iter() .filter_map(|step| { @@ -1635,32 +1723,35 @@ fn render_steps(steps: &[ExecutedStep]) -> String { return None; }; - Some(render_metadata(&metadata)) + Some(render_metadata(&metadata, &config)) }) .collect::>() .join("\n") } -fn render_metadata(metadata: &StepMetadata) -> String { +fn render_metadata(metadata: &StepMetadata, config: &RenderConfig) -> String { let mut record = format!("[{}] ", metadata.kind.as_str()); if let Some(compiler) = metadata.built_by { - write!(record, "{} -> ", render_compiler(compiler)); + write!(record, "{} -> ", render_compiler(compiler, config)); } let stage = metadata.get_stage().map(|stage| format!("{stage} ")).unwrap_or_default(); - write!(record, "{} {stage}<{}>", metadata.name, normalize_target(metadata.target)); + write!(record, "{} {stage}<{}>", metadata.name, normalize_target(metadata.target, config)); + if let Some(metadata) = &metadata.metadata { + write!(record, " {metadata}"); + } record } -fn normalize_target(target: TargetSelection) -> String { - target - .to_string() - .replace(&host_target(), "host") - .replace(TEST_TRIPLE_1, "target1") - .replace(TEST_TRIPLE_2, "target2") +fn normalize_target(target: TargetSelection, config: &RenderConfig) -> String { + let mut target = target.to_string(); + if config.normalize_host { + target = target.replace(&host_target(), "host"); + } + target.replace(TEST_TRIPLE_1, "target1").replace(TEST_TRIPLE_2, "target2") } -fn render_compiler(compiler: Compiler) -> String { - format!("rustc {} <{}>", compiler.stage, normalize_target(compiler.host)) +fn render_compiler(compiler: Compiler, config: &RenderConfig) -> String { + format!("rustc {} <{}>", compiler.stage, normalize_target(compiler.host, config)) } fn host_target() -> String { diff --git a/src/bootstrap/src/utils/tests/mod.rs b/src/bootstrap/src/utils/tests/mod.rs index 59c169b0f2b7a..0e516b4526858 100644 --- a/src/bootstrap/src/utils/tests/mod.rs +++ b/src/bootstrap/src/utils/tests/mod.rs @@ -48,17 +48,30 @@ impl ConfigBuilder { } pub fn path(mut self, path: &str) -> Self { - self.args.push(path.to_string()); - self + self.arg(path) } pub fn paths(mut self, paths: &[&str]) -> Self { - for path in paths { - self = self.path(path); + self.args(paths) + } + + pub fn arg(mut self, arg: &str) -> Self { + self.args.push(arg.to_string()); + self + } + + pub fn args(mut self, args: &[&str]) -> Self { + for arg in args { + self = self.arg(arg); } self } + /// Set the specified target to be treated as a no_std target. + pub fn override_target_no_std(mut self, target: &str) -> Self { + self.args(&["--set", &format!("target.{target}.no-std=true")]) + } + pub fn hosts(mut self, targets: &[&str]) -> Self { self.args.push("--host".to_string()); self.args.push(targets.join(",")); @@ -77,13 +90,6 @@ impl ConfigBuilder { self } - pub fn args(mut self, args: &[&str]) -> Self { - for arg in args { - self.args.push(arg.to_string()); - } - self - } - pub fn create_config(mut self) -> Config { // Run in dry-check, otherwise the test would be too slow self.args.push("--dry-run".to_string());