]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/check.rs
4eb335979b983b004efb2a1358a094947f917a64
[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         let mut cargo = builder.cargo(
117             compiler,
118             Mode::Std,
119             SourceType::InTree,
120             target,
121             cargo_subcommand(builder.kind),
122         );
123         cargo.arg("--all-targets");
124         std_cargo(builder, target, compiler.stage, &mut cargo);
125
126         // Explicitly pass -p for all dependencies krates -- this will force cargo
127         // to also check the tests/benches/examples for these crates, rather
128         // than just the leaf crate.
129         for krate in builder.in_tree_crates("test", Some(target)) {
130             cargo.arg("-p").arg(krate.name);
131         }
132
133         builder.info(&format!(
134             "Checking stage{} std test/bench/example targets ({} -> {})",
135             builder.top_stage, &compiler.host, target
136         ));
137         run_cargo(
138             builder,
139             cargo,
140             args(builder),
141             &libstd_test_stamp(builder, compiler, target),
142             vec![],
143             true,
144         );
145     }
146 }
147
148 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
149 pub struct Rustc {
150     pub target: TargetSelection,
151 }
152
153 impl Step for Rustc {
154     type Output = ();
155     const ONLY_HOSTS: bool = true;
156     const DEFAULT: bool = true;
157
158     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
159         run.all_krates("rustc-main")
160     }
161
162     fn make_run(run: RunConfig<'_>) {
163         run.builder.ensure(Rustc { target: run.target });
164     }
165
166     /// Builds the compiler.
167     ///
168     /// This will build the compiler for a particular stage of the build using
169     /// the `compiler` targeting the `target` architecture. The artifacts
170     /// created will also be linked into the sysroot directory.
171     fn run(self, builder: &Builder<'_>) {
172         let compiler = builder.compiler(builder.top_stage, builder.config.build);
173         let target = self.target;
174
175         if compiler.stage != 0 {
176             // If we're not in stage 0, then we won't have a std from the beta
177             // compiler around. That means we need to make sure there's one in
178             // the sysroot for the compiler to find. Otherwise, we're going to
179             // fail when building crates that need to generate code (e.g., build
180             // scripts and their dependencies).
181             builder.ensure(crate::compile::Std { target: compiler.host, compiler });
182             builder.ensure(crate::compile::Std { target, compiler });
183         } else {
184             builder.ensure(Std { target });
185         }
186
187         let mut cargo = builder.cargo(
188             compiler,
189             Mode::Rustc,
190             SourceType::InTree,
191             target,
192             cargo_subcommand(builder.kind),
193         );
194         rustc_cargo(builder, &mut cargo, target);
195         cargo.arg("--all-targets");
196
197         // Explicitly pass -p for all compiler krates -- this will force cargo
198         // to also check the tests/benches/examples for these crates, rather
199         // than just the leaf crate.
200         for krate in builder.in_tree_crates("rustc-main", Some(target)) {
201             cargo.arg("-p").arg(krate.name);
202         }
203
204         builder.info(&format!(
205             "Checking stage{} compiler artifacts ({} -> {})",
206             builder.top_stage, &compiler.host, target
207         ));
208         run_cargo(
209             builder,
210             cargo,
211             args(builder),
212             &librustc_stamp(builder, compiler, target),
213             vec![],
214             true,
215         );
216
217         let libdir = builder.sysroot_libdir(compiler, target);
218         let hostdir = builder.sysroot_libdir(compiler, compiler.host);
219         add_to_sysroot(&builder, &libdir, &hostdir, &librustc_stamp(builder, compiler, target));
220     }
221 }
222
223 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
224 pub struct CodegenBackend {
225     pub target: TargetSelection,
226     pub backend: Interned<String>,
227 }
228
229 impl Step for CodegenBackend {
230     type Output = ();
231     const ONLY_HOSTS: bool = true;
232     const DEFAULT: bool = true;
233
234     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
235         run.paths(&["compiler/rustc_codegen_cranelift", "rustc_codegen_cranelift"])
236     }
237
238     fn make_run(run: RunConfig<'_>) {
239         for &backend in &[INTERNER.intern_str("cranelift")] {
240             run.builder.ensure(CodegenBackend { target: run.target, backend });
241         }
242     }
243
244     fn run(self, builder: &Builder<'_>) {
245         let compiler = builder.compiler(builder.top_stage, builder.config.build);
246         let target = self.target;
247         let backend = self.backend;
248
249         builder.ensure(Rustc { target });
250
251         let mut cargo = builder.cargo(
252             compiler,
253             Mode::Codegen,
254             SourceType::Submodule,
255             target,
256             cargo_subcommand(builder.kind),
257         );
258         cargo
259             .arg("--manifest-path")
260             .arg(builder.src.join(format!("compiler/rustc_codegen_{}/Cargo.toml", backend)));
261         rustc_cargo_env(builder, &mut cargo, target);
262
263         builder.info(&format!(
264             "Checking stage{} {} artifacts ({} -> {})",
265             builder.top_stage, backend, &compiler.host.triple, target.triple
266         ));
267
268         run_cargo(
269             builder,
270             cargo,
271             args(builder),
272             &codegen_backend_stamp(builder, compiler, target, backend),
273             vec![],
274             true,
275         );
276     }
277 }
278
279 macro_rules! tool_check_step {
280     ($name:ident, $path:literal, $($alias:literal, )* $source_type:path $(, $default:literal )?) => {
281         #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
282         pub struct $name {
283             pub target: TargetSelection,
284         }
285
286         impl Step for $name {
287             type Output = ();
288             const ONLY_HOSTS: bool = true;
289             // don't ever check out-of-tree tools by default, they'll fail when toolstate is broken
290             const DEFAULT: bool = matches!($source_type, SourceType::InTree) $( && $default )?;
291
292             fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
293                 run.paths(&[ $path, $($alias),* ])
294             }
295
296             fn make_run(run: RunConfig<'_>) {
297                 run.builder.ensure($name { target: run.target });
298             }
299
300             fn run(self, builder: &Builder<'_>) {
301                 let compiler = builder.compiler(builder.top_stage, builder.config.build);
302                 let target = self.target;
303
304                 builder.ensure(Rustc { target });
305
306                 let mut cargo = prepare_tool_cargo(
307                     builder,
308                     compiler,
309                     Mode::ToolRustc,
310                     target,
311                     cargo_subcommand(builder.kind),
312                     $path,
313                     $source_type,
314                     &[],
315                 );
316                 cargo.arg("--all-targets");
317
318                 // Enable internal lints for clippy and rustdoc
319                 // NOTE: this doesn't enable lints for any other tools unless they explicitly add `#![warn(rustc::internal)]`
320                 // See https://github.com/rust-lang/rust/pull/80573#issuecomment-754010776
321                 cargo.rustflag("-Zunstable-options");
322
323                 builder.info(&format!(
324                     "Checking stage{} {} artifacts ({} -> {})",
325                     builder.top_stage,
326                     stringify!($name).to_lowercase(),
327                     &compiler.host.triple,
328                     target.triple
329                 ));
330                 run_cargo(
331                     builder,
332                     cargo,
333                     args(builder),
334                     &stamp(builder, compiler, target),
335                     vec![],
336                     true,
337                 );
338
339                 /// Cargo's output path in a given stage, compiled by a particular
340                 /// compiler for the specified target.
341                 fn stamp(
342                     builder: &Builder<'_>,
343                     compiler: Compiler,
344                     target: TargetSelection,
345                 ) -> PathBuf {
346                     builder
347                         .cargo_out(compiler, Mode::ToolRustc, target)
348                         .join(format!(".{}-check.stamp", stringify!($name).to_lowercase()))
349                 }
350             }
351         }
352     };
353 }
354
355 tool_check_step!(Rustdoc, "src/tools/rustdoc", "src/librustdoc", SourceType::InTree);
356 // Clippy and Rustfmt are hybrids. They are external tools, but use a git subtree instead
357 // of a submodule. Since the SourceType only drives the deny-warnings
358 // behavior, treat it as in-tree so that any new warnings in clippy will be
359 // rejected.
360 tool_check_step!(Clippy, "src/tools/clippy", SourceType::InTree);
361 tool_check_step!(Miri, "src/tools/miri", SourceType::Submodule);
362 tool_check_step!(Rls, "src/tools/rls", SourceType::Submodule);
363 tool_check_step!(Rustfmt, "src/tools/rustfmt", SourceType::InTree);
364
365 tool_check_step!(Bootstrap, "src/bootstrap", SourceType::InTree, false);
366
367 /// Cargo's output path for the standard library in a given stage, compiled
368 /// by a particular compiler for the specified target.
369 fn libstd_stamp(builder: &Builder<'_>, compiler: Compiler, target: TargetSelection) -> PathBuf {
370     builder.cargo_out(compiler, Mode::Std, target).join(".libstd-check.stamp")
371 }
372
373 /// Cargo's output path for the standard library in a given stage, compiled
374 /// by a particular compiler for the specified target.
375 fn libstd_test_stamp(
376     builder: &Builder<'_>,
377     compiler: Compiler,
378     target: TargetSelection,
379 ) -> PathBuf {
380     builder.cargo_out(compiler, Mode::Std, target).join(".libstd-check-test.stamp")
381 }
382
383 /// Cargo's output path for librustc in a given stage, compiled by a particular
384 /// compiler for the specified target.
385 fn librustc_stamp(builder: &Builder<'_>, compiler: Compiler, target: TargetSelection) -> PathBuf {
386     builder.cargo_out(compiler, Mode::Rustc, target).join(".librustc-check.stamp")
387 }
388
389 /// Cargo's output path for librustc_codegen_llvm in a given stage, compiled by a particular
390 /// compiler for the specified target and backend.
391 fn codegen_backend_stamp(
392     builder: &Builder<'_>,
393     compiler: Compiler,
394     target: TargetSelection,
395     backend: Interned<String>,
396 ) -> PathBuf {
397     builder
398         .cargo_out(compiler, Mode::Codegen, target)
399         .join(format!(".librustc_codegen_{}-check.stamp", backend))
400 }