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").path("library")
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 // don't run on std twice with x.py clippy
111 if builder.kind == Kind::Clippy {
115 // Then run cargo again, once we've put the rmeta files for the library
116 // crates into the sysroot. This is needed because e.g., core's tests
117 // depend on `libtest` -- Cargo presumes it will exist, but it doesn't
118 // since we initialize with an empty sysroot.
120 // Currently only the "libtest" tree of crates does this.
121 let mut cargo = builder.cargo(
126 cargo_subcommand(builder.kind),
129 cargo.arg("--all-targets");
130 std_cargo(builder, target, compiler.stage, &mut cargo);
132 // Explicitly pass -p for all dependencies krates -- this will force cargo
133 // to also check the tests/benches/examples for these crates, rather
134 // than just the leaf crate.
135 for krate in builder.in_tree_crates("test", Some(target)) {
136 cargo.arg("-p").arg(krate.name);
139 builder.info(&format!(
140 "Checking stage{} std test/bench/example targets ({} -> {})",
141 builder.top_stage, &compiler.host, target
147 &libstd_test_stamp(builder, compiler, target),
154 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
156 pub target: TargetSelection,
159 impl Step for Rustc {
161 const ONLY_HOSTS: bool = true;
162 const DEFAULT: bool = true;
164 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
165 run.all_krates("rustc-main").path("compiler")
168 fn make_run(run: RunConfig<'_>) {
169 run.builder.ensure(Rustc { target: run.target });
172 /// Builds the compiler.
174 /// This will build the compiler for a particular stage of the build using
175 /// the `compiler` targeting the `target` architecture. The artifacts
176 /// created will also be linked into the sysroot directory.
177 fn run(self, builder: &Builder<'_>) {
178 let compiler = builder.compiler(builder.top_stage, builder.config.build);
179 let target = self.target;
181 if compiler.stage != 0 {
182 // If we're not in stage 0, then we won't have a std from the beta
183 // compiler around. That means we need to make sure there's one in
184 // the sysroot for the compiler to find. Otherwise, we're going to
185 // fail when building crates that need to generate code (e.g., build
186 // scripts and their dependencies).
187 builder.ensure(crate::compile::Std { target: compiler.host, compiler });
188 builder.ensure(crate::compile::Std { target, compiler });
190 builder.ensure(Std { target });
193 let mut cargo = builder.cargo(
198 cargo_subcommand(builder.kind),
200 rustc_cargo(builder, &mut cargo, target);
202 // For ./x.py clippy, don't run with --all-targets because
203 // linting tests and benchmarks can produce very noisy results
204 if builder.kind != Kind::Clippy {
205 cargo.arg("--all-targets");
208 // Explicitly pass -p for all compiler krates -- this will force cargo
209 // to also check the tests/benches/examples for these crates, rather
210 // than just the leaf crate.
211 for krate in builder.in_tree_crates("rustc-main", Some(target)) {
212 cargo.arg("-p").arg(krate.name);
215 builder.info(&format!(
216 "Checking stage{} compiler artifacts ({} -> {})",
217 builder.top_stage, &compiler.host, target
223 &librustc_stamp(builder, compiler, target),
228 let libdir = builder.sysroot_libdir(compiler, target);
229 let hostdir = builder.sysroot_libdir(compiler, compiler.host);
230 add_to_sysroot(&builder, &libdir, &hostdir, &librustc_stamp(builder, compiler, target));
234 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
235 pub struct CodegenBackend {
236 pub target: TargetSelection,
237 pub backend: Interned<String>,
240 impl Step for CodegenBackend {
242 const ONLY_HOSTS: bool = true;
243 const DEFAULT: bool = true;
245 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
246 run.paths(&["compiler/rustc_codegen_cranelift", "compiler/rustc_codegen_gcc"])
249 fn make_run(run: RunConfig<'_>) {
250 for &backend in &[INTERNER.intern_str("cranelift"), INTERNER.intern_str("gcc")] {
251 run.builder.ensure(CodegenBackend { target: run.target, backend });
255 fn run(self, builder: &Builder<'_>) {
256 let compiler = builder.compiler(builder.top_stage, builder.config.build);
257 let target = self.target;
258 let backend = self.backend;
260 builder.ensure(Rustc { target });
262 let mut cargo = builder.cargo(
267 cargo_subcommand(builder.kind),
270 .arg("--manifest-path")
271 .arg(builder.src.join(format!("compiler/rustc_codegen_{}/Cargo.toml", backend)));
272 rustc_cargo_env(builder, &mut cargo, target);
274 builder.info(&format!(
275 "Checking stage{} {} artifacts ({} -> {})",
276 builder.top_stage, backend, &compiler.host.triple, target.triple
283 &codegen_backend_stamp(builder, compiler, target, backend),
290 macro_rules! tool_check_step {
291 ($name:ident, $path:literal, $($alias:literal, )* $source_type:path $(, $default:literal )?) => {
292 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
294 pub target: TargetSelection,
297 impl Step for $name {
299 const ONLY_HOSTS: bool = true;
300 // don't ever check out-of-tree tools by default, they'll fail when toolstate is broken
301 const DEFAULT: bool = matches!($source_type, SourceType::InTree) $( && $default )?;
303 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
304 run.paths(&[ $path, $($alias),* ])
307 fn make_run(run: RunConfig<'_>) {
308 run.builder.ensure($name { target: run.target });
311 fn run(self, builder: &Builder<'_>) {
312 let compiler = builder.compiler(builder.top_stage, builder.config.build);
313 let target = self.target;
315 builder.ensure(Rustc { target });
317 let mut cargo = prepare_tool_cargo(
322 cargo_subcommand(builder.kind),
328 // For ./x.py clippy, don't run with --all-targets because
329 // linting tests and benchmarks can produce very noisy results
330 if builder.kind != Kind::Clippy {
331 cargo.arg("--all-targets");
334 // Enable internal lints for clippy and rustdoc
335 // NOTE: this doesn't enable lints for any other tools unless they explicitly add `#![warn(rustc::internal)]`
336 // See https://github.com/rust-lang/rust/pull/80573#issuecomment-754010776
337 cargo.rustflag("-Zunstable-options");
339 builder.info(&format!(
340 "Checking stage{} {} artifacts ({} -> {})",
342 stringify!($name).to_lowercase(),
343 &compiler.host.triple,
350 &stamp(builder, compiler, target),
355 /// Cargo's output path in a given stage, compiled by a particular
356 /// compiler for the specified target.
358 builder: &Builder<'_>,
360 target: TargetSelection,
363 .cargo_out(compiler, Mode::ToolRustc, target)
364 .join(format!(".{}-check.stamp", stringify!($name).to_lowercase()))
371 tool_check_step!(Rustdoc, "src/tools/rustdoc", "src/librustdoc", SourceType::InTree);
372 // Clippy and Rustfmt are hybrids. They are external tools, but use a git subtree instead
373 // of a submodule. Since the SourceType only drives the deny-warnings
374 // behavior, treat it as in-tree so that any new warnings in clippy will be
376 tool_check_step!(Clippy, "src/tools/clippy", SourceType::InTree);
377 tool_check_step!(Miri, "src/tools/miri", SourceType::Submodule);
378 tool_check_step!(Rls, "src/tools/rls", SourceType::Submodule);
379 tool_check_step!(Rustfmt, "src/tools/rustfmt", SourceType::InTree);
381 tool_check_step!(Bootstrap, "src/bootstrap", SourceType::InTree, false);
383 /// Cargo's output path for the standard library in a given stage, compiled
384 /// by a particular compiler for the specified target.
385 fn libstd_stamp(builder: &Builder<'_>, compiler: Compiler, target: TargetSelection) -> PathBuf {
386 builder.cargo_out(compiler, Mode::Std, target).join(".libstd-check.stamp")
389 /// Cargo's output path for the standard library in a given stage, compiled
390 /// by a particular compiler for the specified target.
391 fn libstd_test_stamp(
392 builder: &Builder<'_>,
394 target: TargetSelection,
396 builder.cargo_out(compiler, Mode::Std, target).join(".libstd-check-test.stamp")
399 /// Cargo's output path for librustc in a given stage, compiled by a particular
400 /// compiler for the specified target.
401 fn librustc_stamp(builder: &Builder<'_>, compiler: Compiler, target: TargetSelection) -> PathBuf {
402 builder.cargo_out(compiler, Mode::Rustc, target).join(".librustc-check.stamp")
405 /// Cargo's output path for librustc_codegen_llvm in a given stage, compiled by a particular
406 /// compiler for the specified target and backend.
407 fn codegen_backend_stamp(
408 builder: &Builder<'_>,
410 target: TargetSelection,
411 backend: Interned<String>,
414 .cargo_out(compiler, Mode::Codegen, target)
415 .join(format!(".librustc_codegen_{}-check.stamp", backend))