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::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 let mut args = vec![];
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",
35 args.extend(strings(&["--", "--cap-lints", "warn"]));
42 fn cargo_subcommand(kind: Kind) -> &'static str {
44 Kind::Check => "check",
45 Kind::Clippy => "clippy",
53 const DEFAULT: bool = true;
55 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
56 run.all_krates("test")
59 fn make_run(run: RunConfig<'_>) {
60 run.builder.ensure(Std { target: run.target });
63 fn run(self, builder: &Builder<'_>) {
64 let target = self.target;
65 let compiler = builder.compiler(0, builder.config.build);
67 let mut cargo = builder.cargo(
72 cargo_subcommand(builder.kind),
74 std_cargo(builder, target, compiler.stage, &mut cargo);
76 builder.info(&format!("Checking std artifacts ({} -> {})", &compiler.host, target));
81 &libstd_stamp(builder, compiler, target),
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));
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.
95 // Currently only the "libtest" tree of crates does this.
97 if let Subcommand::Check { all_targets: true, .. } = builder.config.cmd {
98 let mut cargo = builder.cargo(
103 cargo_subcommand(builder.kind),
105 std_cargo(builder, target, compiler.stage, &mut cargo);
106 cargo.arg("--all-targets");
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);
115 builder.info(&format!(
116 "Checking std test/bench/example targets ({} -> {})",
117 &compiler.host, target
123 &libstd_test_stamp(builder, compiler, target),
131 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
133 pub target: TargetSelection,
136 impl Step for Rustc {
138 const ONLY_HOSTS: bool = true;
139 const DEFAULT: bool = true;
141 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
142 run.all_krates("rustc-main")
145 fn make_run(run: RunConfig<'_>) {
146 run.builder.ensure(Rustc { target: run.target });
149 /// Builds the compiler.
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;
158 builder.ensure(Std { target });
160 let mut cargo = builder.cargo(
165 cargo_subcommand(builder.kind),
167 rustc_cargo(builder, &mut cargo, target);
168 if let Subcommand::Check { all_targets: true, .. } = builder.config.cmd {
169 cargo.arg("--all-targets");
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);
179 builder.info(&format!("Checking compiler artifacts ({} -> {})", &compiler.host, target));
184 &librustc_stamp(builder, compiler, target),
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));
195 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
196 pub struct CodegenBackend {
197 pub target: TargetSelection,
198 pub backend: Interned<String>,
201 impl Step for CodegenBackend {
203 const ONLY_HOSTS: bool = true;
204 const DEFAULT: bool = true;
206 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
207 run.paths(&["compiler/rustc_codegen_cranelift", "rustc_codegen_cranelift"])
210 fn make_run(run: RunConfig<'_>) {
211 for &backend in &[INTERNER.intern_str("cranelift")] {
212 run.builder.ensure(CodegenBackend { target: run.target, backend });
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;
221 builder.ensure(Rustc { target });
223 let mut cargo = builder.cargo(
226 SourceType::Submodule,
228 cargo_subcommand(builder.kind),
231 .arg("--manifest-path")
232 .arg(builder.src.join(format!("compiler/rustc_codegen_{}/Cargo.toml", backend)));
233 rustc_cargo_env(builder, &mut cargo, target);
239 &codegen_backend_stamp(builder, compiler, target, backend),
246 macro_rules! tool_check_step {
247 ($name:ident, $path:expr, $source_type:expr) => {
248 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
250 pub target: TargetSelection,
253 impl Step for $name {
255 const ONLY_HOSTS: bool = true;
256 const DEFAULT: bool = true;
258 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
262 fn make_run(run: RunConfig<'_>) {
263 run.builder.ensure($name { target: run.target });
266 fn run(self, builder: &Builder<'_>) {
267 let compiler = builder.compiler(0, builder.config.build);
268 let target = self.target;
270 builder.ensure(Rustc { target });
272 let mut cargo = prepare_tool_cargo(
277 cargo_subcommand(builder.kind),
283 if let Subcommand::Check { all_targets: true, .. } = builder.config.cmd {
284 cargo.arg("--all-targets");
287 builder.info(&format!(
288 "Checking {} artifacts ({} -> {})",
289 stringify!($name).to_lowercase(),
290 &compiler.host.triple,
297 &stamp(builder, compiler, target),
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));
306 /// Cargo's output path in a given stage, compiled by a particular
307 /// compiler for the specified target.
309 builder: &Builder<'_>,
311 target: TargetSelection,
314 .cargo_out(compiler, Mode::ToolRustc, target)
315 .join(format!(".{}-check.stamp", stringify!($name).to_lowercase()))
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
327 tool_check_step!(Clippy, "src/tools/clippy", SourceType::InTree);
329 tool_check_step!(Bootstrap, "src/bootstrap", SourceType::InTree);
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")
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<'_>,
342 target: TargetSelection,
344 builder.cargo_out(compiler, Mode::Std, target).join(".libstd-check-test.stamp")
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")
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<'_>,
358 target: TargetSelection,
359 backend: Interned<String>,
362 .cargo_out(compiler, Mode::Codegen, target)
363 .join(format!(".librustc_codegen_{}-check.stamp", backend))