1 //! Implementation of compiling the compiler and standard library, in "check"-based modes.
3 use crate::builder::{Builder, Kind, RunConfig, ShouldRun, Step};
4 use crate::cache::Interned;
5 use crate::compile::{add_to_sysroot, run_cargo, rustc_cargo, rustc_cargo_env, std_cargo};
6 use crate::config::TargetSelection;
7 use crate::tool::{prepare_tool_cargo, SourceType};
9 use crate::{Compiler, Mode, Subcommand};
10 use std::path::{Path, PathBuf};
12 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
14 pub target: TargetSelection,
17 /// Returns args for the subcommand itself (not for cargo)
18 fn args(builder: &Builder<'_>) -> Vec<String> {
19 fn strings<'a>(arr: &'a [&str]) -> impl Iterator<Item = String> + 'a {
20 arr.iter().copied().map(String::from)
23 if let Subcommand::Clippy { fix, .. } = builder.config.cmd {
24 // disable the most spammy clippy lints
25 let ignored_lints = vec![
26 "many_single_char_names", // there are a lot in stdarch
29 "missing_safety_doc", // almost 3K warnings
31 "needless_lifetimes", // people want to keep the lifetimes
32 "wrong_self_convention",
34 let mut args = vec![];
37 args.extend(strings(&[
38 "--fix", "-Zunstable-options",
39 // FIXME: currently, `--fix` gives an error while checking tests for libtest,
40 // possibly because libtest is not yet built in the sysroot.
41 // As a workaround, avoid checking tests and benches when passed --fix.
42 "--lib", "--bins", "--examples",
45 args.extend(strings(&["--", "--cap-lints", "warn"]));
46 args.extend(ignored_lints.iter().map(|lint| format!("-Aclippy::{}", lint)));
53 fn cargo_subcommand(kind: Kind) -> &'static str {
55 Kind::Check => "check",
56 Kind::Clippy => "clippy",
64 const DEFAULT: bool = true;
66 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
67 run.all_krates("test")
70 fn make_run(run: RunConfig<'_>) {
71 run.builder.ensure(Std { target: run.target });
74 fn run(self, builder: &Builder<'_>) {
75 builder.update_submodule(&Path::new("library").join("stdarch"));
77 let target = self.target;
78 let compiler = builder.compiler(builder.top_stage, builder.config.build);
80 let mut cargo = builder.cargo(
85 cargo_subcommand(builder.kind),
87 std_cargo(builder, target, compiler.stage, &mut cargo);
89 builder.info(&format!(
90 "Checking stage{} std artifacts ({} -> {})",
91 builder.top_stage, &compiler.host, target
97 &libstd_stamp(builder, compiler, target),
102 // We skip populating the sysroot in non-zero stage because that'll lead
103 // to rlib/rmeta conflicts if std gets built during this session.
104 if compiler.stage == 0 {
105 let libdir = builder.sysroot_libdir(compiler, target);
106 let hostdir = builder.sysroot_libdir(compiler, compiler.host);
107 add_to_sysroot(&builder, &libdir, &hostdir, &libstd_stamp(builder, compiler, target));
110 // Then run cargo again, once we've put the rmeta files for the library
111 // crates into the sysroot. This is needed because e.g., core's tests
112 // depend on `libtest` -- Cargo presumes it will exist, but it doesn't
113 // since we initialize with an empty sysroot.
115 // Currently only the "libtest" tree of crates does this.
116 let mut cargo = builder.cargo(
121 cargo_subcommand(builder.kind),
123 cargo.arg("--all-targets");
124 std_cargo(builder, target, compiler.stage, &mut cargo);
126 // Explicitly pass -p for all dependencies krates -- this will force cargo
127 // to also check the tests/benches/examples for these crates, rather
128 // than just the leaf crate.
129 for krate in builder.in_tree_crates("test", Some(target)) {
130 cargo.arg("-p").arg(krate.name);
133 builder.info(&format!(
134 "Checking stage{} std test/bench/example targets ({} -> {})",
135 builder.top_stage, &compiler.host, target
141 &libstd_test_stamp(builder, compiler, target),
148 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
150 pub target: TargetSelection,
153 impl Step for Rustc {
155 const ONLY_HOSTS: bool = true;
156 const DEFAULT: bool = true;
158 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
159 run.all_krates("rustc-main")
162 fn make_run(run: RunConfig<'_>) {
163 run.builder.ensure(Rustc { target: run.target });
166 /// Builds the compiler.
168 /// This will build the compiler for a particular stage of the build using
169 /// the `compiler` targeting the `target` architecture. The artifacts
170 /// created will also be linked into the sysroot directory.
171 fn run(self, builder: &Builder<'_>) {
172 let compiler = builder.compiler(builder.top_stage, builder.config.build);
173 let target = self.target;
175 if compiler.stage != 0 {
176 // If we're not in stage 0, then we won't have a std from the beta
177 // compiler around. That means we need to make sure there's one in
178 // the sysroot for the compiler to find. Otherwise, we're going to
179 // fail when building crates that need to generate code (e.g., build
180 // scripts and their dependencies).
181 builder.ensure(crate::compile::Std { target: compiler.host, compiler });
182 builder.ensure(crate::compile::Std { target, compiler });
184 builder.ensure(Std { target });
187 let mut cargo = builder.cargo(
192 cargo_subcommand(builder.kind),
194 rustc_cargo(builder, &mut cargo, target);
195 cargo.arg("--all-targets");
197 // Explicitly pass -p for all compiler krates -- this will force cargo
198 // to also check the tests/benches/examples for these crates, rather
199 // than just the leaf crate.
200 for krate in builder.in_tree_crates("rustc-main", Some(target)) {
201 cargo.arg("-p").arg(krate.name);
204 builder.info(&format!(
205 "Checking stage{} compiler artifacts ({} -> {})",
206 builder.top_stage, &compiler.host, target
212 &librustc_stamp(builder, compiler, target),
217 let libdir = builder.sysroot_libdir(compiler, target);
218 let hostdir = builder.sysroot_libdir(compiler, compiler.host);
219 add_to_sysroot(&builder, &libdir, &hostdir, &librustc_stamp(builder, compiler, target));
223 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
224 pub struct CodegenBackend {
225 pub target: TargetSelection,
226 pub backend: Interned<String>,
229 impl Step for CodegenBackend {
231 const ONLY_HOSTS: bool = true;
232 const DEFAULT: bool = true;
234 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
235 run.paths(&["compiler/rustc_codegen_cranelift", "rustc_codegen_cranelift"])
238 fn make_run(run: RunConfig<'_>) {
239 for &backend in &[INTERNER.intern_str("cranelift")] {
240 run.builder.ensure(CodegenBackend { target: run.target, backend });
244 fn run(self, builder: &Builder<'_>) {
245 let compiler = builder.compiler(builder.top_stage, builder.config.build);
246 let target = self.target;
247 let backend = self.backend;
249 builder.ensure(Rustc { target });
251 let mut cargo = builder.cargo(
254 SourceType::Submodule,
256 cargo_subcommand(builder.kind),
259 .arg("--manifest-path")
260 .arg(builder.src.join(format!("compiler/rustc_codegen_{}/Cargo.toml", backend)));
261 rustc_cargo_env(builder, &mut cargo, target);
263 builder.info(&format!(
264 "Checking stage{} {} artifacts ({} -> {})",
265 builder.top_stage, backend, &compiler.host.triple, target.triple
272 &codegen_backend_stamp(builder, compiler, target, backend),
279 macro_rules! tool_check_step {
280 ($name:ident, $path:literal, $($alias:literal, )* $source_type:path $(, $default:literal )?) => {
281 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
283 pub target: TargetSelection,
286 impl Step for $name {
288 const ONLY_HOSTS: bool = true;
289 // don't ever check out-of-tree tools by default, they'll fail when toolstate is broken
290 const DEFAULT: bool = matches!($source_type, SourceType::InTree) $( && $default )?;
292 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
293 run.paths(&[ $path, $($alias),* ])
296 fn make_run(run: RunConfig<'_>) {
297 run.builder.ensure($name { target: run.target });
300 fn run(self, builder: &Builder<'_>) {
301 let compiler = builder.compiler(builder.top_stage, builder.config.build);
302 let target = self.target;
304 builder.ensure(Rustc { target });
306 let mut cargo = prepare_tool_cargo(
311 cargo_subcommand(builder.kind),
316 cargo.arg("--all-targets");
318 // Enable internal lints for clippy and rustdoc
319 // NOTE: this doesn't enable lints for any other tools unless they explicitly add `#![warn(rustc::internal)]`
320 // See https://github.com/rust-lang/rust/pull/80573#issuecomment-754010776
321 cargo.rustflag("-Zunstable-options");
323 builder.info(&format!(
324 "Checking stage{} {} artifacts ({} -> {})",
326 stringify!($name).to_lowercase(),
327 &compiler.host.triple,
334 &stamp(builder, compiler, target),
339 /// Cargo's output path in a given stage, compiled by a particular
340 /// compiler for the specified target.
342 builder: &Builder<'_>,
344 target: TargetSelection,
347 .cargo_out(compiler, Mode::ToolRustc, target)
348 .join(format!(".{}-check.stamp", stringify!($name).to_lowercase()))
355 tool_check_step!(Rustdoc, "src/tools/rustdoc", "src/librustdoc", SourceType::InTree);
356 // Clippy and Rustfmt are hybrids. They are external tools, but use a git subtree instead
357 // of a submodule. Since the SourceType only drives the deny-warnings
358 // behavior, treat it as in-tree so that any new warnings in clippy will be
360 tool_check_step!(Clippy, "src/tools/clippy", SourceType::InTree);
361 tool_check_step!(Miri, "src/tools/miri", SourceType::Submodule);
362 tool_check_step!(Rls, "src/tools/rls", SourceType::Submodule);
363 tool_check_step!(Rustfmt, "src/tools/rustfmt", SourceType::InTree);
365 tool_check_step!(Bootstrap, "src/bootstrap", SourceType::InTree, false);
367 /// Cargo's output path for the standard library in a given stage, compiled
368 /// by a particular compiler for the specified target.
369 fn libstd_stamp(builder: &Builder<'_>, compiler: Compiler, target: TargetSelection) -> PathBuf {
370 builder.cargo_out(compiler, Mode::Std, target).join(".libstd-check.stamp")
373 /// Cargo's output path for the standard library in a given stage, compiled
374 /// by a particular compiler for the specified target.
375 fn libstd_test_stamp(
376 builder: &Builder<'_>,
378 target: TargetSelection,
380 builder.cargo_out(compiler, Mode::Std, target).join(".libstd-check-test.stamp")
383 /// Cargo's output path for librustc in a given stage, compiled by a particular
384 /// compiler for the specified target.
385 fn librustc_stamp(builder: &Builder<'_>, compiler: Compiler, target: TargetSelection) -> PathBuf {
386 builder.cargo_out(compiler, Mode::Rustc, target).join(".librustc-check.stamp")
389 /// Cargo's output path for librustc_codegen_llvm in a given stage, compiled by a particular
390 /// compiler for the specified target and backend.
391 fn codegen_backend_stamp(
392 builder: &Builder<'_>,
394 target: TargetSelection,
395 backend: Interned<String>,
398 .cargo_out(compiler, Mode::Codegen, target)
399 .join(format!(".librustc_codegen_{}-check.stamp", backend))