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 cargo.arg("--all-targets");
144 std_cargo(builder, target, compiler.stage, &mut cargo);
146 // Explicitly pass -p for all dependencies krates -- this will force cargo
147 // to also check the tests/benches/examples for these crates, rather
148 // than just the leaf crate.
149 for krate in builder.in_tree_crates("test", Some(target)) {
150 cargo.arg("-p").arg(krate.name);
153 builder.info(&format!(
154 "Checking stage{} std test/bench/example targets ({} -> {})",
155 builder.top_stage, &compiler.host, target
161 &libstd_test_stamp(builder, compiler, target),
168 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
170 pub target: TargetSelection,
173 impl Step for Rustc {
175 const ONLY_HOSTS: bool = true;
176 const DEFAULT: bool = true;
178 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
179 run.all_krates("rustc-main").path("compiler")
182 fn make_run(run: RunConfig<'_>) {
183 run.builder.ensure(Rustc { target: run.target });
186 /// Builds the compiler.
188 /// This will build the compiler for a particular stage of the build using
189 /// the `compiler` targeting the `target` architecture. The artifacts
190 /// created will also be linked into the sysroot directory.
191 fn run(self, builder: &Builder<'_>) {
192 let compiler = builder.compiler(builder.top_stage, builder.config.build);
193 let target = self.target;
195 if compiler.stage != 0 {
196 // If we're not in stage 0, then we won't have a std from the beta
197 // compiler around. That means we need to make sure there's one in
198 // the sysroot for the compiler to find. Otherwise, we're going to
199 // fail when building crates that need to generate code (e.g., build
200 // scripts and their dependencies).
201 builder.ensure(crate::compile::Std::new(compiler, compiler.host));
202 builder.ensure(crate::compile::Std::new(compiler, target));
204 builder.ensure(Std { target });
207 let mut cargo = builder.cargo(
212 cargo_subcommand(builder.kind),
214 rustc_cargo(builder, &mut cargo, target);
216 // For ./x.py clippy, don't run with --all-targets because
217 // linting tests and benchmarks can produce very noisy results
218 if builder.kind != Kind::Clippy {
219 cargo.arg("--all-targets");
222 // Explicitly pass -p for all compiler krates -- this will force cargo
223 // to also check the tests/benches/examples for these crates, rather
224 // than just the leaf crate.
225 for krate in builder.in_tree_crates("rustc-main", Some(target)) {
226 cargo.arg("-p").arg(krate.name);
229 builder.info(&format!(
230 "Checking stage{} compiler artifacts ({} -> {})",
231 builder.top_stage, &compiler.host, target
237 &librustc_stamp(builder, compiler, target),
242 let libdir = builder.sysroot_libdir(compiler, target);
243 let hostdir = builder.sysroot_libdir(compiler, compiler.host);
244 add_to_sysroot(&builder, &libdir, &hostdir, &librustc_stamp(builder, compiler, target));
248 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
249 pub struct CodegenBackend {
250 pub target: TargetSelection,
251 pub backend: Interned<String>,
254 impl Step for CodegenBackend {
256 const ONLY_HOSTS: bool = true;
257 const DEFAULT: bool = true;
259 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
260 run.paths(&["compiler/rustc_codegen_cranelift", "compiler/rustc_codegen_gcc"])
263 fn make_run(run: RunConfig<'_>) {
264 for &backend in &[INTERNER.intern_str("cranelift"), INTERNER.intern_str("gcc")] {
265 run.builder.ensure(CodegenBackend { target: run.target, backend });
269 fn run(self, builder: &Builder<'_>) {
270 let compiler = builder.compiler(builder.top_stage, builder.config.build);
271 let target = self.target;
272 let backend = self.backend;
274 builder.ensure(Rustc { target });
276 let mut cargo = builder.cargo(
281 cargo_subcommand(builder.kind),
284 .arg("--manifest-path")
285 .arg(builder.src.join(format!("compiler/rustc_codegen_{}/Cargo.toml", backend)));
286 rustc_cargo_env(builder, &mut cargo, target);
288 builder.info(&format!(
289 "Checking stage{} {} artifacts ({} -> {})",
290 builder.top_stage, backend, &compiler.host.triple, target.triple
297 &codegen_backend_stamp(builder, compiler, target, backend),
304 macro_rules! tool_check_step {
305 ($name:ident, $path:literal, $($alias:literal, )* $source_type:path $(, $default:literal )?) => {
306 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
308 pub target: TargetSelection,
311 impl Step for $name {
313 const ONLY_HOSTS: bool = true;
314 // don't ever check out-of-tree tools by default, they'll fail when toolstate is broken
315 const DEFAULT: bool = matches!($source_type, SourceType::InTree) $( && $default )?;
317 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
318 run.paths(&[ $path, $($alias),* ])
321 fn make_run(run: RunConfig<'_>) {
322 run.builder.ensure($name { target: run.target });
325 fn run(self, builder: &Builder<'_>) {
326 let compiler = builder.compiler(builder.top_stage, builder.config.build);
327 let target = self.target;
329 builder.ensure(Rustc { target });
331 let mut cargo = prepare_tool_cargo(
336 cargo_subcommand(builder.kind),
342 // For ./x.py clippy, don't run with --all-targets because
343 // linting tests and benchmarks can produce very noisy results
344 if builder.kind != Kind::Clippy {
345 cargo.arg("--all-targets");
348 // Enable internal lints for clippy and rustdoc
349 // NOTE: this doesn't enable lints for any other tools unless they explicitly add `#![warn(rustc::internal)]`
350 // See https://github.com/rust-lang/rust/pull/80573#issuecomment-754010776
351 cargo.rustflag("-Zunstable-options");
353 builder.info(&format!(
354 "Checking stage{} {} artifacts ({} -> {})",
356 stringify!($name).to_lowercase(),
357 &compiler.host.triple,
364 &stamp(builder, compiler, target),
369 /// Cargo's output path in a given stage, compiled by a particular
370 /// compiler for the specified target.
372 builder: &Builder<'_>,
374 target: TargetSelection,
377 .cargo_out(compiler, Mode::ToolRustc, target)
378 .join(format!(".{}-check.stamp", stringify!($name).to_lowercase()))
385 tool_check_step!(Rustdoc, "src/tools/rustdoc", "src/librustdoc", SourceType::InTree);
386 // Clippy and Rustfmt are hybrids. They are external tools, but use a git subtree instead
387 // of a submodule. Since the SourceType only drives the deny-warnings
388 // behavior, treat it as in-tree so that any new warnings in clippy will be
390 tool_check_step!(Clippy, "src/tools/clippy", SourceType::InTree);
391 tool_check_step!(Miri, "src/tools/miri", SourceType::Submodule);
392 tool_check_step!(Rls, "src/tools/rls", SourceType::Submodule);
393 tool_check_step!(Rustfmt, "src/tools/rustfmt", SourceType::InTree);
395 tool_check_step!(Bootstrap, "src/bootstrap", SourceType::InTree, false);
397 /// Cargo's output path for the standard library in a given stage, compiled
398 /// by a particular compiler for the specified target.
399 fn libstd_stamp(builder: &Builder<'_>, compiler: Compiler, target: TargetSelection) -> PathBuf {
400 builder.cargo_out(compiler, Mode::Std, target).join(".libstd-check.stamp")
403 /// Cargo's output path for the standard library in a given stage, compiled
404 /// by a particular compiler for the specified target.
405 fn libstd_test_stamp(
406 builder: &Builder<'_>,
408 target: TargetSelection,
410 builder.cargo_out(compiler, Mode::Std, target).join(".libstd-check-test.stamp")
413 /// Cargo's output path for librustc in a given stage, compiled by a particular
414 /// compiler for the specified target.
415 fn librustc_stamp(builder: &Builder<'_>, compiler: Compiler, target: TargetSelection) -> PathBuf {
416 builder.cargo_out(compiler, Mode::Rustc, target).join(".librustc-check.stamp")
419 /// Cargo's output path for librustc_codegen_llvm in a given stage, compiled by a particular
420 /// compiler for the specified target and backend.
421 fn codegen_backend_stamp(
422 builder: &Builder<'_>,
424 target: TargetSelection,
425 backend: Interned<String>,
428 .cargo_out(compiler, Mode::Codegen, target)
429 .join(format!(".librustc_codegen_{}-check.stamp", backend))