bootstrap/core/build_steps/
run.rs

1//! Build-and-run steps for in-repo tools
2//!
3//! A bit of a hodge-podge as e.g. if a tool's a test fixture it should be in `build_steps::test`.
4//! If it can be reached from `./x.py run` it can go here.
5
6use std::path::PathBuf;
7
8use crate::Mode;
9use crate::core::build_steps::dist::distdir;
10use crate::core::build_steps::test;
11use crate::core::build_steps::tool::{self, SourceType, Tool};
12use crate::core::builder::{Builder, Kind, RunConfig, ShouldRun, Step};
13use crate::core::config::TargetSelection;
14use crate::core::config::flags::get_completion;
15use crate::utils::exec::command;
16
17#[derive(Debug, PartialOrd, Ord, Clone, Hash, PartialEq, Eq)]
18pub struct BuildManifest;
19
20impl Step for BuildManifest {
21    type Output = ();
22    const ONLY_HOSTS: bool = true;
23
24    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
25        run.path("src/tools/build-manifest")
26    }
27
28    fn make_run(run: RunConfig<'_>) {
29        run.builder.ensure(BuildManifest);
30    }
31
32    fn run(self, builder: &Builder<'_>) {
33        // This gets called by `promote-release`
34        // (https://github.com/rust-lang/promote-release).
35        let mut cmd = builder.tool_cmd(Tool::BuildManifest);
36        let sign = builder.config.dist_sign_folder.as_ref().unwrap_or_else(|| {
37            panic!("\n\nfailed to specify `dist.sign-folder` in `config.toml`\n\n")
38        });
39        let addr = builder.config.dist_upload_addr.as_ref().unwrap_or_else(|| {
40            panic!("\n\nfailed to specify `dist.upload-addr` in `config.toml`\n\n")
41        });
42
43        let today = command("date").arg("+%Y-%m-%d").run_capture_stdout(builder).stdout();
44
45        cmd.arg(sign);
46        cmd.arg(distdir(builder));
47        cmd.arg(today.trim());
48        cmd.arg(addr);
49        cmd.arg(&builder.config.channel);
50
51        builder.create_dir(&distdir(builder));
52        cmd.run(builder);
53    }
54}
55
56#[derive(Debug, PartialOrd, Ord, Clone, Hash, PartialEq, Eq)]
57pub struct BumpStage0;
58
59impl Step for BumpStage0 {
60    type Output = ();
61    const ONLY_HOSTS: bool = true;
62
63    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
64        run.path("src/tools/bump-stage0")
65    }
66
67    fn make_run(run: RunConfig<'_>) {
68        run.builder.ensure(BumpStage0);
69    }
70
71    fn run(self, builder: &Builder<'_>) -> Self::Output {
72        let mut cmd = builder.tool_cmd(Tool::BumpStage0);
73        cmd.args(builder.config.args());
74        cmd.run(builder);
75    }
76}
77
78#[derive(Debug, PartialOrd, Ord, Clone, Hash, PartialEq, Eq)]
79pub struct ReplaceVersionPlaceholder;
80
81impl Step for ReplaceVersionPlaceholder {
82    type Output = ();
83    const ONLY_HOSTS: bool = true;
84
85    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
86        run.path("src/tools/replace-version-placeholder")
87    }
88
89    fn make_run(run: RunConfig<'_>) {
90        run.builder.ensure(ReplaceVersionPlaceholder);
91    }
92
93    fn run(self, builder: &Builder<'_>) -> Self::Output {
94        let mut cmd = builder.tool_cmd(Tool::ReplaceVersionPlaceholder);
95        cmd.arg(&builder.src);
96        cmd.run(builder);
97    }
98}
99
100#[derive(Debug, Clone, PartialEq, Eq, Hash)]
101pub struct Miri {
102    target: TargetSelection,
103}
104
105impl Step for Miri {
106    type Output = ();
107    const ONLY_HOSTS: bool = false;
108
109    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
110        run.path("src/tools/miri")
111    }
112
113    fn make_run(run: RunConfig<'_>) {
114        run.builder.ensure(Miri { target: run.target });
115    }
116
117    fn run(self, builder: &Builder<'_>) {
118        let host = builder.build.build;
119        let target = self.target;
120        let stage = builder.top_stage;
121        if stage == 0 {
122            eprintln!("miri cannot be run at stage 0");
123            std::process::exit(1);
124        }
125
126        // This compiler runs on the host, we'll just use it for the target.
127        let target_compiler = builder.compiler(stage, host);
128        // Similar to `compile::Assemble`, build with the previous stage's compiler. Otherwise
129        // we'd have stageN/bin/rustc and stageN/bin/rustdoc be effectively different stage
130        // compilers, which isn't what we want. Rustdoc should be linked in the same way as the
131        // rustc compiler it's paired with, so it must be built with the previous stage compiler.
132        let host_compiler = builder.compiler(stage - 1, host);
133
134        // Get a target sysroot for Miri.
135        let miri_sysroot = test::Miri::build_miri_sysroot(builder, target_compiler, target);
136
137        // # Run miri.
138        // Running it via `cargo run` as that figures out the right dylib path.
139        // add_rustc_lib_path does not add the path that contains librustc_driver-<...>.so.
140        let mut miri = tool::prepare_tool_cargo(
141            builder,
142            host_compiler,
143            Mode::ToolRustc,
144            host,
145            Kind::Run,
146            "src/tools/miri",
147            SourceType::InTree,
148            &[],
149        );
150        miri.add_rustc_lib_path(builder);
151        miri.arg("--").arg("--target").arg(target.rustc_target_arg());
152
153        // miri tests need to know about the stage sysroot
154        miri.arg("--sysroot").arg(miri_sysroot);
155
156        // Forward arguments. This may contain further arguments to the program
157        // after another --, so this must be at the end.
158        miri.args(builder.config.args());
159
160        miri.into_cmd().run(builder);
161    }
162}
163
164#[derive(Debug, PartialOrd, Ord, Clone, Hash, PartialEq, Eq)]
165pub struct CollectLicenseMetadata;
166
167impl Step for CollectLicenseMetadata {
168    type Output = PathBuf;
169    const ONLY_HOSTS: bool = true;
170
171    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
172        run.path("src/tools/collect-license-metadata")
173    }
174
175    fn make_run(run: RunConfig<'_>) {
176        run.builder.ensure(CollectLicenseMetadata);
177    }
178
179    fn run(self, builder: &Builder<'_>) -> Self::Output {
180        let Some(reuse) = &builder.config.reuse else {
181            panic!("REUSE is required to collect the license metadata");
182        };
183
184        let dest = builder.src.join("license-metadata.json");
185
186        let mut cmd = builder.tool_cmd(Tool::CollectLicenseMetadata);
187        cmd.env("REUSE_EXE", reuse);
188        cmd.env("DEST", &dest);
189        cmd.run(builder);
190
191        dest
192    }
193}
194
195#[derive(Debug, PartialOrd, Ord, Clone, Hash, PartialEq, Eq)]
196pub struct GenerateCopyright;
197
198impl Step for GenerateCopyright {
199    type Output = Vec<PathBuf>;
200    const ONLY_HOSTS: bool = true;
201
202    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
203        run.path("src/tools/generate-copyright")
204    }
205
206    fn make_run(run: RunConfig<'_>) {
207        run.builder.ensure(GenerateCopyright);
208    }
209
210    fn run(self, builder: &Builder<'_>) -> Self::Output {
211        let license_metadata = builder.src.join("license-metadata.json");
212        let dest = builder.out.join("COPYRIGHT.html");
213        let dest_libstd = builder.out.join("COPYRIGHT-library.html");
214
215        let mut cmd = builder.tool_cmd(Tool::GenerateCopyright);
216        cmd.env("LICENSE_METADATA", &license_metadata);
217        cmd.env("DEST", &dest);
218        cmd.env("DEST_LIBSTD", &dest_libstd);
219        cmd.env("OUT_DIR", &builder.out);
220        cmd.env("CARGO", &builder.initial_cargo);
221        // it is important that generate-copyright runs from the root of the
222        // source tree, because it uses relative paths
223        cmd.current_dir(&builder.src);
224        cmd.run(builder);
225
226        vec![dest, dest_libstd]
227    }
228}
229
230#[derive(Debug, PartialOrd, Ord, Clone, Hash, PartialEq, Eq)]
231pub struct GenerateWindowsSys;
232
233impl Step for GenerateWindowsSys {
234    type Output = ();
235    const ONLY_HOSTS: bool = true;
236
237    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
238        run.path("src/tools/generate-windows-sys")
239    }
240
241    fn make_run(run: RunConfig<'_>) {
242        run.builder.ensure(GenerateWindowsSys);
243    }
244
245    fn run(self, builder: &Builder<'_>) {
246        let mut cmd = builder.tool_cmd(Tool::GenerateWindowsSys);
247        cmd.arg(&builder.src);
248        cmd.run(builder);
249    }
250}
251
252#[derive(Debug, Clone, PartialEq, Eq, Hash)]
253pub struct GenerateCompletions;
254
255macro_rules! generate_completions {
256    ( $( ( $shell:ident, $filename:expr ) ),* ) => {
257        $(
258            if let Some(comp) = get_completion($shell, &$filename) {
259                std::fs::write(&$filename, comp).expect(&format!("writing {} completion", stringify!($shell)));
260            }
261        )*
262    };
263}
264
265impl Step for GenerateCompletions {
266    type Output = ();
267
268    /// Uses `clap_complete` to generate shell completions.
269    fn run(self, builder: &Builder<'_>) {
270        use clap_complete::shells::{Bash, Fish, PowerShell, Zsh};
271
272        generate_completions!(
273            (Bash, builder.src.join("src/etc/completions/x.py.sh")),
274            (Zsh, builder.src.join("src/etc/completions/x.py.zsh")),
275            (Fish, builder.src.join("src/etc/completions/x.py.fish")),
276            (PowerShell, builder.src.join("src/etc/completions/x.py.ps1")),
277            (Bash, builder.src.join("src/etc/completions/x.sh")),
278            (Zsh, builder.src.join("src/etc/completions/x.zsh")),
279            (Fish, builder.src.join("src/etc/completions/x.fish")),
280            (PowerShell, builder.src.join("src/etc/completions/x.ps1"))
281        );
282    }
283
284    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
285        run.alias("generate-completions")
286    }
287
288    fn make_run(run: RunConfig<'_>) {
289        run.builder.ensure(GenerateCompletions);
290    }
291}
292
293#[derive(Debug, PartialOrd, Ord, Clone, Hash, PartialEq, Eq)]
294pub struct UnicodeTableGenerator;
295
296impl Step for UnicodeTableGenerator {
297    type Output = ();
298    const ONLY_HOSTS: bool = true;
299
300    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
301        run.path("src/tools/unicode-table-generator")
302    }
303
304    fn make_run(run: RunConfig<'_>) {
305        run.builder.ensure(UnicodeTableGenerator);
306    }
307
308    fn run(self, builder: &Builder<'_>) {
309        let mut cmd = builder.tool_cmd(Tool::UnicodeTableGenerator);
310        cmd.arg(builder.src.join("library/core/src/unicode/unicode_data.rs"));
311        cmd.run(builder);
312    }
313}
314
315#[derive(Debug, PartialOrd, Ord, Clone, Hash, PartialEq, Eq)]
316pub struct FeaturesStatusDump;
317
318impl Step for FeaturesStatusDump {
319    type Output = ();
320    const ONLY_HOSTS: bool = true;
321
322    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
323        run.path("src/tools/features-status-dump")
324    }
325
326    fn make_run(run: RunConfig<'_>) {
327        run.builder.ensure(FeaturesStatusDump);
328    }
329
330    fn run(self, builder: &Builder<'_>) {
331        let mut cmd = builder.tool_cmd(Tool::FeaturesStatusDump);
332
333        cmd.arg("--library-path");
334        cmd.arg(builder.src.join("library"));
335
336        cmd.arg("--compiler-path");
337        cmd.arg(builder.src.join("compiler"));
338
339        cmd.arg("--output-path");
340        cmd.arg(builder.out.join("features-status-dump.json"));
341
342        cmd.run(builder);
343    }
344}