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 {
30 } = &builder.config.cmd
32 // disable the most spammy clippy lints
33 let ignored_lints = vec![
34 "many_single_char_names", // there are a lot in stdarch
37 "missing_safety_doc", // almost 3K warnings
39 "needless_lifetimes", // people want to keep the lifetimes
40 "wrong_self_convention",
42 let mut args = vec![];
45 args.extend(strings(&[
46 "--fix", "-Zunstable-options",
47 // FIXME: currently, `--fix` gives an error while checking tests for libtest,
48 // possibly because libtest is not yet built in the sysroot.
49 // As a workaround, avoid checking tests and benches when passed --fix.
50 "--lib", "--bins", "--examples",
53 args.extend(strings(&["--", "--cap-lints", "warn"]));
54 args.extend(ignored_lints.iter().map(|lint| format!("-Aclippy::{}", lint)));
55 let mut clippy_lint_levels: Vec<String> = Vec::new();
56 clippy_lint_allow.iter().for_each(|v| clippy_lint_levels.push(format!("-A{}", v)));
57 clippy_lint_deny.iter().for_each(|v| clippy_lint_levels.push(format!("-D{}", v)));
58 clippy_lint_warn.iter().for_each(|v| clippy_lint_levels.push(format!("-W{}", v)));
59 clippy_lint_forbid.iter().for_each(|v| clippy_lint_levels.push(format!("-F{}", v)));
60 args.extend(clippy_lint_levels);
67 fn cargo_subcommand(kind: Kind) -> &'static str {
69 Kind::Check => "check",
70 Kind::Clippy => "clippy",
78 const DEFAULT: bool = true;
80 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
81 run.all_krates("test").path("library")
84 fn make_run(run: RunConfig<'_>) {
85 run.builder.ensure(Std { target: run.target });
88 fn run(self, builder: &Builder<'_>) {
89 builder.update_submodule(&Path::new("library").join("stdarch"));
91 let target = self.target;
92 let compiler = builder.compiler(builder.top_stage, builder.config.build);
94 let mut cargo = builder.cargo(
99 cargo_subcommand(builder.kind),
101 std_cargo(builder, target, compiler.stage, &mut cargo);
103 builder.info(&format!(
104 "Checking stage{} std artifacts ({} -> {})",
105 builder.top_stage, &compiler.host, target
111 &libstd_stamp(builder, compiler, target),
116 // We skip populating the sysroot in non-zero stage because that'll lead
117 // to rlib/rmeta conflicts if std gets built during this session.
118 if compiler.stage == 0 {
119 let libdir = builder.sysroot_libdir(compiler, target);
120 let hostdir = builder.sysroot_libdir(compiler, compiler.host);
121 add_to_sysroot(&builder, &libdir, &hostdir, &libstd_stamp(builder, compiler, target));
124 // don't run on std twice with x.py clippy
125 if builder.kind == Kind::Clippy {
129 // Then run cargo again, once we've put the rmeta files for the library
130 // crates into the sysroot. This is needed because e.g., core's tests
131 // depend on `libtest` -- Cargo presumes it will exist, but it doesn't
132 // since we initialize with an empty sysroot.
134 // Currently only the "libtest" tree of crates does this.
135 let mut cargo = builder.cargo(
140 cargo_subcommand(builder.kind),
143 // If we're not in stage 0, tests and examples will fail to compile
144 // from `core` definitions being loaded from two different `libcore`
145 // .rmeta and .rlib files.
146 if compiler.stage == 0 {
147 cargo.arg("--all-targets");
150 std_cargo(builder, target, compiler.stage, &mut cargo);
152 // Explicitly pass -p for all dependencies krates -- this will force cargo
153 // to also check the tests/benches/examples for these crates, rather
154 // than just the leaf crate.
155 for krate in builder.in_tree_crates("test", Some(target)) {
156 cargo.arg("-p").arg(krate.name);
159 builder.info(&format!(
160 "Checking stage{} std test/bench/example targets ({} -> {})",
161 builder.top_stage, &compiler.host, target
167 &libstd_test_stamp(builder, compiler, target),
174 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
176 pub target: TargetSelection,
179 impl Step for Rustc {
181 const ONLY_HOSTS: bool = true;
182 const DEFAULT: bool = true;
184 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
185 run.all_krates("rustc-main").path("compiler")
188 fn make_run(run: RunConfig<'_>) {
189 run.builder.ensure(Rustc { target: run.target });
192 /// Builds the compiler.
194 /// This will build the compiler for a particular stage of the build using
195 /// the `compiler` targeting the `target` architecture. The artifacts
196 /// created will also be linked into the sysroot directory.
197 fn run(self, builder: &Builder<'_>) {
198 let compiler = builder.compiler(builder.top_stage, builder.config.build);
199 let target = self.target;
201 if compiler.stage != 0 {
202 // If we're not in stage 0, then we won't have a std from the beta
203 // compiler around. That means we need to make sure there's one in
204 // the sysroot for the compiler to find. Otherwise, we're going to
205 // fail when building crates that need to generate code (e.g., build
206 // scripts and their dependencies).
207 builder.ensure(crate::compile::Std::new(compiler, compiler.host));
208 builder.ensure(crate::compile::Std::new(compiler, target));
210 builder.ensure(Std { target });
213 let mut cargo = builder.cargo(
218 cargo_subcommand(builder.kind),
220 rustc_cargo(builder, &mut cargo, target);
222 // For ./x.py clippy, don't run with --all-targets because
223 // linting tests and benchmarks can produce very noisy results
224 if builder.kind != Kind::Clippy {
225 cargo.arg("--all-targets");
228 // Explicitly pass -p for all compiler krates -- this will force cargo
229 // to also check the tests/benches/examples for these crates, rather
230 // than just the leaf crate.
231 for krate in builder.in_tree_crates("rustc-main", Some(target)) {
232 cargo.arg("-p").arg(krate.name);
235 builder.info(&format!(
236 "Checking stage{} compiler artifacts ({} -> {})",
237 builder.top_stage, &compiler.host, target
243 &librustc_stamp(builder, compiler, target),
248 let libdir = builder.sysroot_libdir(compiler, target);
249 let hostdir = builder.sysroot_libdir(compiler, compiler.host);
250 add_to_sysroot(&builder, &libdir, &hostdir, &librustc_stamp(builder, compiler, target));
254 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
255 pub struct CodegenBackend {
256 pub target: TargetSelection,
257 pub backend: Interned<String>,
260 impl Step for CodegenBackend {
262 const ONLY_HOSTS: bool = true;
263 const DEFAULT: bool = true;
265 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
266 run.paths(&["compiler/rustc_codegen_cranelift", "compiler/rustc_codegen_gcc"])
269 fn make_run(run: RunConfig<'_>) {
270 for &backend in &[INTERNER.intern_str("cranelift"), INTERNER.intern_str("gcc")] {
271 run.builder.ensure(CodegenBackend { target: run.target, backend });
275 fn run(self, builder: &Builder<'_>) {
276 let compiler = builder.compiler(builder.top_stage, builder.config.build);
277 let target = self.target;
278 let backend = self.backend;
280 builder.ensure(Rustc { target });
282 let mut cargo = builder.cargo(
287 cargo_subcommand(builder.kind),
290 .arg("--manifest-path")
291 .arg(builder.src.join(format!("compiler/rustc_codegen_{}/Cargo.toml", backend)));
292 rustc_cargo_env(builder, &mut cargo, target);
294 builder.info(&format!(
295 "Checking stage{} {} artifacts ({} -> {})",
296 builder.top_stage, backend, &compiler.host.triple, target.triple
303 &codegen_backend_stamp(builder, compiler, target, backend),
310 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
311 pub struct RustAnalyzer {
312 pub target: TargetSelection,
315 impl Step for RustAnalyzer {
317 const ONLY_HOSTS: bool = true;
318 const DEFAULT: bool = true;
320 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
321 run.path("src/tools/rust-analyzer")
324 fn make_run(run: RunConfig<'_>) {
325 run.builder.ensure(RustAnalyzer { target: run.target });
328 fn run(self, builder: &Builder<'_>) {
329 let compiler = builder.compiler(builder.top_stage, builder.config.build);
330 let target = self.target;
332 builder.ensure(Std { target });
334 let mut cargo = prepare_tool_cargo(
339 cargo_subcommand(builder.kind),
340 "src/tools/rust-analyzer",
342 &["rust-analyzer/in-rust-tree".to_owned()],
346 "-Zallow-features=proc_macro_internals,proc_macro_diagnostic,proc_macro_span",
349 // For ./x.py clippy, don't check those targets because
350 // linting tests and benchmarks can produce very noisy results
351 if builder.kind != Kind::Clippy {
352 // can't use `--all-targets` because `--examples` doesn't work well
354 cargo.arg("--tests");
355 cargo.arg("--benches");
358 builder.info(&format!(
359 "Checking stage{} {} artifacts ({} -> {})",
360 compiler.stage, "rust-analyzer", &compiler.host.triple, target.triple
362 run_cargo(builder, cargo, args(builder), &stamp(builder, compiler, target), vec![], true);
364 /// Cargo's output path in a given stage, compiled by a particular
365 /// compiler for the specified target.
366 fn stamp(builder: &Builder<'_>, compiler: Compiler, target: TargetSelection) -> PathBuf {
367 builder.cargo_out(compiler, Mode::ToolStd, target).join(".rust-analyzer-check.stamp")
372 macro_rules! tool_check_step {
373 ($name:ident, $path:literal, $($alias:literal, )* $source_type:path $(, $default:literal )?) => {
374 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
376 pub target: TargetSelection,
379 impl Step for $name {
381 const ONLY_HOSTS: bool = true;
382 // don't ever check out-of-tree tools by default, they'll fail when toolstate is broken
383 const DEFAULT: bool = matches!($source_type, SourceType::InTree) $( && $default )?;
385 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
386 run.paths(&[ $path, $($alias),* ])
389 fn make_run(run: RunConfig<'_>) {
390 run.builder.ensure($name { target: run.target });
393 fn run(self, builder: &Builder<'_>) {
394 let compiler = builder.compiler(builder.top_stage, builder.config.build);
395 let target = self.target;
397 builder.ensure(Rustc { target });
399 let mut cargo = prepare_tool_cargo(
404 cargo_subcommand(builder.kind),
410 // For ./x.py clippy, don't run with --all-targets because
411 // linting tests and benchmarks can produce very noisy results
412 if builder.kind != Kind::Clippy {
413 cargo.arg("--all-targets");
416 // Enable internal lints for clippy and rustdoc
417 // NOTE: this doesn't enable lints for any other tools unless they explicitly add `#![warn(rustc::internal)]`
418 // See https://github.com/rust-lang/rust/pull/80573#issuecomment-754010776
419 cargo.rustflag("-Zunstable-options");
421 builder.info(&format!(
422 "Checking stage{} {} artifacts ({} -> {})",
424 stringify!($name).to_lowercase(),
425 &compiler.host.triple,
432 &stamp(builder, compiler, target),
437 /// Cargo's output path in a given stage, compiled by a particular
438 /// compiler for the specified target.
440 builder: &Builder<'_>,
442 target: TargetSelection,
445 .cargo_out(compiler, Mode::ToolRustc, target)
446 .join(format!(".{}-check.stamp", stringify!($name).to_lowercase()))
453 tool_check_step!(Rustdoc, "src/tools/rustdoc", "src/librustdoc", SourceType::InTree);
454 // Clippy and Rustfmt are hybrids. They are external tools, but use a git subtree instead
455 // of a submodule. Since the SourceType only drives the deny-warnings
456 // behavior, treat it as in-tree so that any new warnings in clippy will be
458 tool_check_step!(Clippy, "src/tools/clippy", SourceType::InTree);
459 tool_check_step!(Miri, "src/tools/miri", SourceType::Submodule);
460 tool_check_step!(Rls, "src/tools/rls", SourceType::Submodule);
461 tool_check_step!(Rustfmt, "src/tools/rustfmt", SourceType::InTree);
463 tool_check_step!(Bootstrap, "src/bootstrap", SourceType::InTree, false);
465 /// Cargo's output path for the standard library in a given stage, compiled
466 /// by a particular compiler for the specified target.
467 fn libstd_stamp(builder: &Builder<'_>, compiler: Compiler, target: TargetSelection) -> PathBuf {
468 builder.cargo_out(compiler, Mode::Std, target).join(".libstd-check.stamp")
471 /// Cargo's output path for the standard library in a given stage, compiled
472 /// by a particular compiler for the specified target.
473 fn libstd_test_stamp(
474 builder: &Builder<'_>,
476 target: TargetSelection,
478 builder.cargo_out(compiler, Mode::Std, target).join(".libstd-check-test.stamp")
481 /// Cargo's output path for librustc in a given stage, compiled by a particular
482 /// compiler for the specified target.
483 fn librustc_stamp(builder: &Builder<'_>, compiler: Compiler, target: TargetSelection) -> PathBuf {
484 builder.cargo_out(compiler, Mode::Rustc, target).join(".librustc-check.stamp")
487 /// Cargo's output path for librustc_codegen_llvm in a given stage, compiled by a particular
488 /// compiler for the specified target and backend.
489 fn codegen_backend_stamp(
490 builder: &Builder<'_>,
492 target: TargetSelection,
493 backend: Interned<String>,
496 .cargo_out(compiler, Mode::Codegen, target)
497 .join(format!(".librustc_codegen_{}-check.stamp", backend))