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 // 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")
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<'_> {
247 "compiler/rustc_codegen_cranelift",
248 "rustc_codegen_cranelift",
249 "compiler/rustc_codegen_gcc",
254 fn make_run(run: RunConfig<'_>) {
255 for &backend in &[INTERNER.intern_str("cranelift"), INTERNER.intern_str("gcc")] {
256 run.builder.ensure(CodegenBackend { target: run.target, backend });
260 fn run(self, builder: &Builder<'_>) {
261 let compiler = builder.compiler(builder.top_stage, builder.config.build);
262 let target = self.target;
263 let backend = self.backend;
265 builder.ensure(Rustc { target });
267 let mut cargo = builder.cargo(
272 cargo_subcommand(builder.kind),
275 .arg("--manifest-path")
276 .arg(builder.src.join(format!("compiler/rustc_codegen_{}/Cargo.toml", backend)));
277 rustc_cargo_env(builder, &mut cargo, target);
279 builder.info(&format!(
280 "Checking stage{} {} artifacts ({} -> {})",
281 builder.top_stage, backend, &compiler.host.triple, target.triple
288 &codegen_backend_stamp(builder, compiler, target, backend),
295 macro_rules! tool_check_step {
296 ($name:ident, $path:literal, $($alias:literal, )* $source_type:path $(, $default:literal )?) => {
297 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
299 pub target: TargetSelection,
302 impl Step for $name {
304 const ONLY_HOSTS: bool = true;
305 // don't ever check out-of-tree tools by default, they'll fail when toolstate is broken
306 const DEFAULT: bool = matches!($source_type, SourceType::InTree) $( && $default )?;
308 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
309 run.paths(&[ $path, $($alias),* ])
312 fn make_run(run: RunConfig<'_>) {
313 run.builder.ensure($name { target: run.target });
316 fn run(self, builder: &Builder<'_>) {
317 let compiler = builder.compiler(builder.top_stage, builder.config.build);
318 let target = self.target;
320 builder.ensure(Rustc { target });
322 let mut cargo = prepare_tool_cargo(
327 cargo_subcommand(builder.kind),
333 // For ./x.py clippy, don't run with --all-targets because
334 // linting tests and benchmarks can produce very noisy results
335 if builder.kind != Kind::Clippy {
336 cargo.arg("--all-targets");
339 // Enable internal lints for clippy and rustdoc
340 // NOTE: this doesn't enable lints for any other tools unless they explicitly add `#![warn(rustc::internal)]`
341 // See https://github.com/rust-lang/rust/pull/80573#issuecomment-754010776
342 cargo.rustflag("-Zunstable-options");
344 builder.info(&format!(
345 "Checking stage{} {} artifacts ({} -> {})",
347 stringify!($name).to_lowercase(),
348 &compiler.host.triple,
355 &stamp(builder, compiler, target),
360 /// Cargo's output path in a given stage, compiled by a particular
361 /// compiler for the specified target.
363 builder: &Builder<'_>,
365 target: TargetSelection,
368 .cargo_out(compiler, Mode::ToolRustc, target)
369 .join(format!(".{}-check.stamp", stringify!($name).to_lowercase()))
376 tool_check_step!(Rustdoc, "src/tools/rustdoc", "src/librustdoc", SourceType::InTree);
377 // Clippy and Rustfmt are hybrids. They are external tools, but use a git subtree instead
378 // of a submodule. Since the SourceType only drives the deny-warnings
379 // behavior, treat it as in-tree so that any new warnings in clippy will be
381 tool_check_step!(Clippy, "src/tools/clippy", SourceType::InTree);
382 tool_check_step!(Miri, "src/tools/miri", SourceType::Submodule);
383 tool_check_step!(Rls, "src/tools/rls", SourceType::Submodule);
384 tool_check_step!(Rustfmt, "src/tools/rustfmt", SourceType::InTree);
386 tool_check_step!(Bootstrap, "src/bootstrap", SourceType::InTree, false);
388 /// Cargo's output path for the standard library in a given stage, compiled
389 /// by a particular compiler for the specified target.
390 fn libstd_stamp(builder: &Builder<'_>, compiler: Compiler, target: TargetSelection) -> PathBuf {
391 builder.cargo_out(compiler, Mode::Std, target).join(".libstd-check.stamp")
394 /// Cargo's output path for the standard library in a given stage, compiled
395 /// by a particular compiler for the specified target.
396 fn libstd_test_stamp(
397 builder: &Builder<'_>,
399 target: TargetSelection,
401 builder.cargo_out(compiler, Mode::Std, target).join(".libstd-check-test.stamp")
404 /// Cargo's output path for librustc in a given stage, compiled by a particular
405 /// compiler for the specified target.
406 fn librustc_stamp(builder: &Builder<'_>, compiler: Compiler, target: TargetSelection) -> PathBuf {
407 builder.cargo_out(compiler, Mode::Rustc, target).join(".librustc-check.stamp")
410 /// Cargo's output path for librustc_codegen_llvm in a given stage, compiled by a particular
411 /// compiler for the specified target and backend.
412 fn codegen_backend_stamp(
413 builder: &Builder<'_>,
415 target: TargetSelection,
416 backend: Interned<String>,
419 .cargo_out(compiler, Mode::Codegen, target)
420 .join(format!(".librustc_codegen_{}-check.stamp", backend))