]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/check.rs
Rollup merge of #78969 - tmiasko:normalize, 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::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         let mut args = vec![];
25         if fix {
26             #[rustfmt::skip]
27             args.extend(strings(&[
28                 "--fix", "-Zunstable-options",
29                 // FIXME: currently, `--fix` gives an error while checking tests for libtest,
30                 // possibly because libtest is not yet built in the sysroot.
31                 // As a workaround, avoid checking tests and benches when passed --fix.
32                 "--lib", "--bins", "--examples",
33             ]));
34         }
35         args.extend(strings(&["--", "--cap-lints", "warn"]));
36         args
37     } else {
38         vec![]
39     }
40 }
41
42 fn cargo_subcommand(kind: Kind) -> &'static str {
43     match kind {
44         Kind::Check => "check",
45         Kind::Clippy => "clippy",
46         Kind::Fix => "fix",
47         _ => unreachable!(),
48     }
49 }
50
51 impl Step for Std {
52     type Output = ();
53     const DEFAULT: bool = true;
54
55     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
56         run.all_krates("test")
57     }
58
59     fn make_run(run: RunConfig<'_>) {
60         run.builder.ensure(Std { target: run.target });
61     }
62
63     fn run(self, builder: &Builder<'_>) {
64         let target = self.target;
65         let compiler = builder.compiler(0, builder.config.build);
66
67         let mut cargo = builder.cargo(
68             compiler,
69             Mode::Std,
70             SourceType::InTree,
71             target,
72             cargo_subcommand(builder.kind),
73         );
74         std_cargo(builder, target, compiler.stage, &mut cargo);
75
76         builder.info(&format!("Checking std artifacts ({} -> {})", &compiler.host, target));
77         run_cargo(
78             builder,
79             cargo,
80             args(builder),
81             &libstd_stamp(builder, compiler, target),
82             vec![],
83             true,
84         );
85
86         let libdir = builder.sysroot_libdir(compiler, target);
87         let hostdir = builder.sysroot_libdir(compiler, compiler.host);
88         add_to_sysroot(&builder, &libdir, &hostdir, &libstd_stamp(builder, compiler, target));
89
90         // Then run cargo again, once we've put the rmeta files for the library
91         // crates into the sysroot. This is needed because e.g., core's tests
92         // depend on `libtest` -- Cargo presumes it will exist, but it doesn't
93         // since we initialize with an empty sysroot.
94         //
95         // Currently only the "libtest" tree of crates does this.
96
97         if let Subcommand::Check { all_targets: true, .. } = builder.config.cmd {
98             let mut cargo = builder.cargo(
99                 compiler,
100                 Mode::Std,
101                 SourceType::InTree,
102                 target,
103                 cargo_subcommand(builder.kind),
104             );
105             std_cargo(builder, target, compiler.stage, &mut cargo);
106             cargo.arg("--all-targets");
107
108             // Explicitly pass -p for all dependencies krates -- this will force cargo
109             // to also check the tests/benches/examples for these crates, rather
110             // than just the leaf crate.
111             for krate in builder.in_tree_crates("test", Some(target)) {
112                 cargo.arg("-p").arg(krate.name);
113             }
114
115             builder.info(&format!(
116                 "Checking std test/bench/example targets ({} -> {})",
117                 &compiler.host, target
118             ));
119             run_cargo(
120                 builder,
121                 cargo,
122                 args(builder),
123                 &libstd_test_stamp(builder, compiler, target),
124                 vec![],
125                 true,
126             );
127         }
128     }
129 }
130
131 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
132 pub struct Rustc {
133     pub target: TargetSelection,
134 }
135
136 impl Step for Rustc {
137     type Output = ();
138     const ONLY_HOSTS: bool = true;
139     const DEFAULT: bool = true;
140
141     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
142         run.all_krates("rustc-main")
143     }
144
145     fn make_run(run: RunConfig<'_>) {
146         run.builder.ensure(Rustc { target: run.target });
147     }
148
149     /// Builds the compiler.
150     ///
151     /// This will build the compiler for a particular stage of the build using
152     /// the `compiler` targeting the `target` architecture. The artifacts
153     /// created will also be linked into the sysroot directory.
154     fn run(self, builder: &Builder<'_>) {
155         let compiler = builder.compiler(0, builder.config.build);
156         let target = self.target;
157
158         builder.ensure(Std { target });
159
160         let mut cargo = builder.cargo(
161             compiler,
162             Mode::Rustc,
163             SourceType::InTree,
164             target,
165             cargo_subcommand(builder.kind),
166         );
167         rustc_cargo(builder, &mut cargo, target);
168         if let Subcommand::Check { all_targets: true, .. } = builder.config.cmd {
169             cargo.arg("--all-targets");
170         }
171
172         // Explicitly pass -p for all compiler krates -- this will force cargo
173         // to also check the tests/benches/examples for these crates, rather
174         // than just the leaf crate.
175         for krate in builder.in_tree_crates("rustc-main", Some(target)) {
176             cargo.arg("-p").arg(krate.name);
177         }
178
179         builder.info(&format!("Checking compiler artifacts ({} -> {})", &compiler.host, target));
180         run_cargo(
181             builder,
182             cargo,
183             args(builder),
184             &librustc_stamp(builder, compiler, target),
185             vec![],
186             true,
187         );
188
189         let libdir = builder.sysroot_libdir(compiler, target);
190         let hostdir = builder.sysroot_libdir(compiler, compiler.host);
191         add_to_sysroot(&builder, &libdir, &hostdir, &librustc_stamp(builder, compiler, target));
192     }
193 }
194
195 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
196 pub struct CodegenBackend {
197     pub target: TargetSelection,
198     pub backend: Interned<String>,
199 }
200
201 impl Step for CodegenBackend {
202     type Output = ();
203     const ONLY_HOSTS: bool = true;
204     const DEFAULT: bool = true;
205
206     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
207         run.paths(&["compiler/rustc_codegen_cranelift", "rustc_codegen_cranelift"])
208     }
209
210     fn make_run(run: RunConfig<'_>) {
211         for &backend in &[INTERNER.intern_str("cranelift")] {
212             run.builder.ensure(CodegenBackend { target: run.target, backend });
213         }
214     }
215
216     fn run(self, builder: &Builder<'_>) {
217         let compiler = builder.compiler(0, builder.config.build);
218         let target = self.target;
219         let backend = self.backend;
220
221         builder.ensure(Rustc { target });
222
223         let mut cargo = builder.cargo(
224             compiler,
225             Mode::Codegen,
226             SourceType::Submodule,
227             target,
228             cargo_subcommand(builder.kind),
229         );
230         cargo
231             .arg("--manifest-path")
232             .arg(builder.src.join(format!("compiler/rustc_codegen_{}/Cargo.toml", backend)));
233         rustc_cargo_env(builder, &mut cargo, target);
234
235         run_cargo(
236             builder,
237             cargo,
238             args(builder),
239             &codegen_backend_stamp(builder, compiler, target, backend),
240             vec![],
241             true,
242         );
243     }
244 }
245
246 macro_rules! tool_check_step {
247     ($name:ident, $path:expr, $source_type:expr) => {
248         #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
249         pub struct $name {
250             pub target: TargetSelection,
251         }
252
253         impl Step for $name {
254             type Output = ();
255             const ONLY_HOSTS: bool = true;
256             const DEFAULT: bool = true;
257
258             fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
259                 run.path($path)
260             }
261
262             fn make_run(run: RunConfig<'_>) {
263                 run.builder.ensure($name { target: run.target });
264             }
265
266             fn run(self, builder: &Builder<'_>) {
267                 let compiler = builder.compiler(0, builder.config.build);
268                 let target = self.target;
269
270                 builder.ensure(Rustc { target });
271
272                 let mut cargo = prepare_tool_cargo(
273                     builder,
274                     compiler,
275                     Mode::ToolRustc,
276                     target,
277                     cargo_subcommand(builder.kind),
278                     $path,
279                     $source_type,
280                     &[],
281                 );
282
283                 if let Subcommand::Check { all_targets: true, .. } = builder.config.cmd {
284                     cargo.arg("--all-targets");
285                 }
286
287                 builder.info(&format!(
288                     "Checking {} artifacts ({} -> {})",
289                     stringify!($name).to_lowercase(),
290                     &compiler.host.triple,
291                     target.triple
292                 ));
293                 run_cargo(
294                     builder,
295                     cargo,
296                     args(builder),
297                     &stamp(builder, compiler, target),
298                     vec![],
299                     true,
300                 );
301
302                 let libdir = builder.sysroot_libdir(compiler, target);
303                 let hostdir = builder.sysroot_libdir(compiler, compiler.host);
304                 add_to_sysroot(&builder, &libdir, &hostdir, &stamp(builder, compiler, target));
305
306                 /// Cargo's output path in a given stage, compiled by a particular
307                 /// compiler for the specified target.
308                 fn stamp(
309                     builder: &Builder<'_>,
310                     compiler: Compiler,
311                     target: TargetSelection,
312                 ) -> PathBuf {
313                     builder
314                         .cargo_out(compiler, Mode::ToolRustc, target)
315                         .join(format!(".{}-check.stamp", stringify!($name).to_lowercase()))
316                 }
317             }
318         }
319     };
320 }
321
322 tool_check_step!(Rustdoc, "src/tools/rustdoc", SourceType::InTree);
323 // Clippy is a hybrid. It is an external tool, but uses a git subtree instead
324 // of a submodule. Since the SourceType only drives the deny-warnings
325 // behavior, treat it as in-tree so that any new warnings in clippy will be
326 // rejected.
327 tool_check_step!(Clippy, "src/tools/clippy", SourceType::InTree);
328
329 tool_check_step!(Bootstrap, "src/bootstrap", SourceType::InTree);
330
331 /// Cargo's output path for the standard library in a given stage, compiled
332 /// by a particular compiler for the specified target.
333 fn libstd_stamp(builder: &Builder<'_>, compiler: Compiler, target: TargetSelection) -> PathBuf {
334     builder.cargo_out(compiler, Mode::Std, target).join(".libstd-check.stamp")
335 }
336
337 /// Cargo's output path for the standard library in a given stage, compiled
338 /// by a particular compiler for the specified target.
339 fn libstd_test_stamp(
340     builder: &Builder<'_>,
341     compiler: Compiler,
342     target: TargetSelection,
343 ) -> PathBuf {
344     builder.cargo_out(compiler, Mode::Std, target).join(".libstd-check-test.stamp")
345 }
346
347 /// Cargo's output path for librustc in a given stage, compiled by a particular
348 /// compiler for the specified target.
349 fn librustc_stamp(builder: &Builder<'_>, compiler: Compiler, target: TargetSelection) -> PathBuf {
350     builder.cargo_out(compiler, Mode::Rustc, target).join(".librustc-check.stamp")
351 }
352
353 /// Cargo's output path for librustc_codegen_llvm in a given stage, compiled by a particular
354 /// compiler for the specified target and backend.
355 fn codegen_backend_stamp(
356     builder: &Builder<'_>,
357     compiler: Compiler,
358     target: TargetSelection,
359     backend: Interned<String>,
360 ) -> PathBuf {
361     builder
362         .cargo_out(compiler, Mode::Codegen, target)
363         .join(format!(".librustc_codegen_{}-check.stamp", backend))
364 }