]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/check.rs
Auto merge of #85971 - FabianWolff:issue-85586, r=davidtwco
[rust.git] / src / bootstrap / check.rs
1 //! Implementation of compiling the compiler and standard library, in "check"-based modes.
2
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};
8 use crate::INTERNER;
9 use crate::{Compiler, Mode, Subcommand};
10 use std::path::{Path, PathBuf};
11
12 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
13 pub struct Std {
14     pub target: TargetSelection,
15 }
16
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)
21     }
22
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
27             "collapsible_if",
28             "type_complexity",
29             "missing_safety_doc", // almost 3K warnings
30             "too_many_arguments",
31             "needless_lifetimes", // people want to keep the lifetimes
32             "wrong_self_convention",
33         ];
34         let mut args = vec![];
35         if fix {
36             #[rustfmt::skip]
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",
43             ]));
44         }
45         args.extend(strings(&["--", "--cap-lints", "warn"]));
46         args.extend(ignored_lints.iter().map(|lint| format!("-Aclippy::{}", lint)));
47         args
48     } else {
49         vec![]
50     }
51 }
52
53 fn cargo_subcommand(kind: Kind) -> &'static str {
54     match kind {
55         Kind::Check => "check",
56         Kind::Clippy => "clippy",
57         Kind::Fix => "fix",
58         _ => unreachable!(),
59     }
60 }
61
62 impl Step for Std {
63     type Output = ();
64     const DEFAULT: bool = true;
65
66     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
67         run.all_krates("test")
68     }
69
70     fn make_run(run: RunConfig<'_>) {
71         run.builder.ensure(Std { target: run.target });
72     }
73
74     fn run(self, builder: &Builder<'_>) {
75         builder.update_submodule(&Path::new("library").join("stdarch"));
76
77         let target = self.target;
78         let compiler = builder.compiler(builder.top_stage, builder.config.build);
79
80         let mut cargo = builder.cargo(
81             compiler,
82             Mode::Std,
83             SourceType::InTree,
84             target,
85             cargo_subcommand(builder.kind),
86         );
87         std_cargo(builder, target, compiler.stage, &mut cargo);
88
89         builder.info(&format!(
90             "Checking stage{} std artifacts ({} -> {})",
91             builder.top_stage, &compiler.host, target
92         ));
93         run_cargo(
94             builder,
95             cargo,
96             args(builder),
97             &libstd_stamp(builder, compiler, target),
98             vec![],
99             true,
100         );
101
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));
108         }
109
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.
114         //
115         // Currently only the "libtest" tree of crates does this.
116
117         if let Subcommand::Check { all_targets: true, .. } = builder.config.cmd {
118             let mut cargo = builder.cargo(
119                 compiler,
120                 Mode::Std,
121                 SourceType::InTree,
122                 target,
123                 cargo_subcommand(builder.kind),
124             );
125             std_cargo(builder, target, compiler.stage, &mut cargo);
126             cargo.arg("--all-targets");
127
128             // Explicitly pass -p for all dependencies krates -- this will force cargo
129             // to also check the tests/benches/examples for these crates, rather
130             // than just the leaf crate.
131             for krate in builder.in_tree_crates("test", Some(target)) {
132                 cargo.arg("-p").arg(krate.name);
133             }
134
135             builder.info(&format!(
136                 "Checking stage{} std test/bench/example targets ({} -> {})",
137                 builder.top_stage, &compiler.host, target
138             ));
139             run_cargo(
140                 builder,
141                 cargo,
142                 args(builder),
143                 &libstd_test_stamp(builder, compiler, target),
144                 vec![],
145                 true,
146             );
147         }
148     }
149 }
150
151 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
152 pub struct Rustc {
153     pub target: TargetSelection,
154 }
155
156 impl Step for Rustc {
157     type Output = ();
158     const ONLY_HOSTS: bool = true;
159     const DEFAULT: bool = true;
160
161     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
162         run.all_krates("rustc-main")
163     }
164
165     fn make_run(run: RunConfig<'_>) {
166         run.builder.ensure(Rustc { target: run.target });
167     }
168
169     /// Builds the compiler.
170     ///
171     /// This will build the compiler for a particular stage of the build using
172     /// the `compiler` targeting the `target` architecture. The artifacts
173     /// created will also be linked into the sysroot directory.
174     fn run(self, builder: &Builder<'_>) {
175         let compiler = builder.compiler(builder.top_stage, builder.config.build);
176         let target = self.target;
177
178         if compiler.stage != 0 {
179             // If we're not in stage 0, then we won't have a std from the beta
180             // compiler around. That means we need to make sure there's one in
181             // the sysroot for the compiler to find. Otherwise, we're going to
182             // fail when building crates that need to generate code (e.g., build
183             // scripts and their dependencies).
184             builder.ensure(crate::compile::Std { target: compiler.host, compiler });
185             builder.ensure(crate::compile::Std { target, compiler });
186         } else {
187             builder.ensure(Std { target });
188         }
189
190         let mut cargo = builder.cargo(
191             compiler,
192             Mode::Rustc,
193             SourceType::InTree,
194             target,
195             cargo_subcommand(builder.kind),
196         );
197         rustc_cargo(builder, &mut cargo, target);
198         if let Subcommand::Check { all_targets: true, .. } = builder.config.cmd {
199             cargo.arg("--all-targets");
200         }
201
202         // Explicitly pass -p for all compiler krates -- this will force cargo
203         // to also check the tests/benches/examples for these crates, rather
204         // than just the leaf crate.
205         for krate in builder.in_tree_crates("rustc-main", Some(target)) {
206             cargo.arg("-p").arg(krate.name);
207         }
208
209         builder.info(&format!(
210             "Checking stage{} compiler artifacts ({} -> {})",
211             builder.top_stage, &compiler.host, target
212         ));
213         run_cargo(
214             builder,
215             cargo,
216             args(builder),
217             &librustc_stamp(builder, compiler, target),
218             vec![],
219             true,
220         );
221
222         let libdir = builder.sysroot_libdir(compiler, target);
223         let hostdir = builder.sysroot_libdir(compiler, compiler.host);
224         add_to_sysroot(&builder, &libdir, &hostdir, &librustc_stamp(builder, compiler, target));
225     }
226 }
227
228 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
229 pub struct CodegenBackend {
230     pub target: TargetSelection,
231     pub backend: Interned<String>,
232 }
233
234 impl Step for CodegenBackend {
235     type Output = ();
236     const ONLY_HOSTS: bool = true;
237     const DEFAULT: bool = true;
238
239     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
240         run.paths(&["compiler/rustc_codegen_cranelift", "rustc_codegen_cranelift"])
241     }
242
243     fn make_run(run: RunConfig<'_>) {
244         for &backend in &[INTERNER.intern_str("cranelift")] {
245             run.builder.ensure(CodegenBackend { target: run.target, backend });
246         }
247     }
248
249     fn run(self, builder: &Builder<'_>) {
250         let compiler = builder.compiler(builder.top_stage, builder.config.build);
251         let target = self.target;
252         let backend = self.backend;
253
254         builder.ensure(Rustc { target });
255
256         let mut cargo = builder.cargo(
257             compiler,
258             Mode::Codegen,
259             SourceType::Submodule,
260             target,
261             cargo_subcommand(builder.kind),
262         );
263         cargo
264             .arg("--manifest-path")
265             .arg(builder.src.join(format!("compiler/rustc_codegen_{}/Cargo.toml", backend)));
266         rustc_cargo_env(builder, &mut cargo, target);
267
268         builder.info(&format!(
269             "Checking stage{} {} artifacts ({} -> {})",
270             builder.top_stage, backend, &compiler.host.triple, target.triple
271         ));
272
273         run_cargo(
274             builder,
275             cargo,
276             args(builder),
277             &codegen_backend_stamp(builder, compiler, target, backend),
278             vec![],
279             true,
280         );
281     }
282 }
283
284 macro_rules! tool_check_step {
285     ($name:ident, $path:literal, $($alias:literal, )* $source_type:path $(, $default:literal )?) => {
286         #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
287         pub struct $name {
288             pub target: TargetSelection,
289         }
290
291         impl Step for $name {
292             type Output = ();
293             const ONLY_HOSTS: bool = true;
294             // don't ever check out-of-tree tools by default, they'll fail when toolstate is broken
295             const DEFAULT: bool = matches!($source_type, SourceType::InTree) $( && $default )?;
296
297             fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
298                 run.paths(&[ $path, $($alias),* ])
299             }
300
301             fn make_run(run: RunConfig<'_>) {
302                 run.builder.ensure($name { target: run.target });
303             }
304
305             fn run(self, builder: &Builder<'_>) {
306                 let compiler = builder.compiler(builder.top_stage, builder.config.build);
307                 let target = self.target;
308
309                 builder.ensure(Rustc { target });
310
311                 let mut cargo = prepare_tool_cargo(
312                     builder,
313                     compiler,
314                     Mode::ToolRustc,
315                     target,
316                     cargo_subcommand(builder.kind),
317                     $path,
318                     $source_type,
319                     &[],
320                 );
321
322                 if let Subcommand::Check { all_targets: true, .. } = builder.config.cmd {
323                     cargo.arg("--all-targets");
324                 }
325
326                 // Enable internal lints for clippy and rustdoc
327                 // NOTE: this doesn't enable lints for any other tools unless they explicitly add `#![warn(rustc::internal)]`
328                 // See https://github.com/rust-lang/rust/pull/80573#issuecomment-754010776
329                 cargo.rustflag("-Zunstable-options");
330
331                 builder.info(&format!(
332                     "Checking stage{} {} artifacts ({} -> {})",
333                     builder.top_stage,
334                     stringify!($name).to_lowercase(),
335                     &compiler.host.triple,
336                     target.triple
337                 ));
338                 run_cargo(
339                     builder,
340                     cargo,
341                     args(builder),
342                     &stamp(builder, compiler, target),
343                     vec![],
344                     true,
345                 );
346
347                 /// Cargo's output path in a given stage, compiled by a particular
348                 /// compiler for the specified target.
349                 fn stamp(
350                     builder: &Builder<'_>,
351                     compiler: Compiler,
352                     target: TargetSelection,
353                 ) -> PathBuf {
354                     builder
355                         .cargo_out(compiler, Mode::ToolRustc, target)
356                         .join(format!(".{}-check.stamp", stringify!($name).to_lowercase()))
357                 }
358             }
359         }
360     };
361 }
362
363 tool_check_step!(Rustdoc, "src/tools/rustdoc", "src/librustdoc", SourceType::InTree);
364 // Clippy and Rustfmt are hybrids. They are external tools, but use a git subtree instead
365 // of a submodule. Since the SourceType only drives the deny-warnings
366 // behavior, treat it as in-tree so that any new warnings in clippy will be
367 // rejected.
368 tool_check_step!(Clippy, "src/tools/clippy", SourceType::InTree);
369 tool_check_step!(Miri, "src/tools/miri", SourceType::Submodule);
370 tool_check_step!(Rls, "src/tools/rls", SourceType::Submodule);
371 tool_check_step!(Rustfmt, "src/tools/rustfmt", SourceType::InTree);
372
373 tool_check_step!(Bootstrap, "src/bootstrap", SourceType::InTree, false);
374
375 /// Cargo's output path for the standard library in a given stage, compiled
376 /// by a particular compiler for the specified target.
377 fn libstd_stamp(builder: &Builder<'_>, compiler: Compiler, target: TargetSelection) -> PathBuf {
378     builder.cargo_out(compiler, Mode::Std, target).join(".libstd-check.stamp")
379 }
380
381 /// Cargo's output path for the standard library in a given stage, compiled
382 /// by a particular compiler for the specified target.
383 fn libstd_test_stamp(
384     builder: &Builder<'_>,
385     compiler: Compiler,
386     target: TargetSelection,
387 ) -> PathBuf {
388     builder.cargo_out(compiler, Mode::Std, target).join(".libstd-check-test.stamp")
389 }
390
391 /// Cargo's output path for librustc in a given stage, compiled by a particular
392 /// compiler for the specified target.
393 fn librustc_stamp(builder: &Builder<'_>, compiler: Compiler, target: TargetSelection) -> PathBuf {
394     builder.cargo_out(compiler, Mode::Rustc, target).join(".librustc-check.stamp")
395 }
396
397 /// Cargo's output path for librustc_codegen_llvm in a given stage, compiled by a particular
398 /// compiler for the specified target and backend.
399 fn codegen_backend_stamp(
400     builder: &Builder<'_>,
401     compiler: Compiler,
402     target: TargetSelection,
403     backend: Interned<String>,
404 ) -> PathBuf {
405     builder
406         .cargo_out(compiler, Mode::Codegen, target)
407         .join(format!(".librustc_codegen_{}-check.stamp", backend))
408 }