bootstrap/core/build_steps/
synthetic_targets.rs

1//! In some cases, parts of bootstrap need to change part of a target spec just for one or a few
2//! steps. Adding these targets to rustc proper would "leak" this implementation detail of
3//! bootstrap, and would make it more complex to apply additional changes if the need arises.
4//!
5//! To address that problem, this module implements support for "synthetic targets". Synthetic
6//! targets are custom target specs generated using builtin target specs as their base. You can use
7//! one of the target specs already defined in this module, or create new ones by adding a new step
8//! that calls create_synthetic_target.
9
10use crate::Compiler;
11use crate::core::builder::{Builder, ShouldRun, Step};
12use crate::core::config::TargetSelection;
13
14#[derive(Debug, Clone, PartialEq, Eq, Hash)]
15pub(crate) struct MirOptPanicAbortSyntheticTarget {
16    pub(crate) compiler: Compiler,
17    pub(crate) base: TargetSelection,
18}
19
20impl Step for MirOptPanicAbortSyntheticTarget {
21    type Output = TargetSelection;
22    const DEFAULT: bool = true;
23
24    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
25        run.never()
26    }
27
28    fn run(self, builder: &Builder<'_>) -> Self::Output {
29        create_synthetic_target(builder, self.compiler, "miropt-abort", self.base, |spec| {
30            spec.insert("panic-strategy".into(), "abort".into());
31        })
32    }
33}
34
35fn create_synthetic_target(
36    builder: &Builder<'_>,
37    compiler: Compiler,
38    suffix: &str,
39    base: TargetSelection,
40    customize: impl FnOnce(&mut serde_json::Map<String, serde_json::Value>),
41) -> TargetSelection {
42    if base.contains("synthetic") {
43        // This check is not strictly needed, but nothing currently needs recursive synthetic
44        // targets. If the need arises, removing this in the future *SHOULD* be safe.
45        panic!("cannot create synthetic targets with other synthetic targets as their base");
46    }
47
48    let name = format!("{base}-synthetic-{suffix}");
49    let path = builder.out.join("synthetic-target-specs").join(format!("{name}.json"));
50    std::fs::create_dir_all(path.parent().unwrap()).unwrap();
51
52    if builder.config.dry_run() {
53        std::fs::write(&path, b"dry run\n").unwrap();
54        return TargetSelection::create_synthetic(&name, path.to_str().unwrap());
55    }
56
57    let mut cmd = builder.rustc_cmd(compiler);
58    cmd.arg("--target").arg(base.rustc_target_arg());
59    cmd.args(["-Zunstable-options", "--print", "target-spec-json"]);
60
61    // If `rust.channel` is set to either beta or stable, rustc will complain that
62    // we cannot use nightly features. So `RUSTC_BOOTSTRAP` is needed here.
63    cmd.env("RUSTC_BOOTSTRAP", "1");
64
65    let output = cmd.run_capture(builder).stdout();
66    let mut spec: serde_json::Value = serde_json::from_slice(output.as_bytes()).unwrap();
67    let spec_map = spec.as_object_mut().unwrap();
68
69    // The `is-builtin` attribute of a spec needs to be removed, otherwise rustc will complain.
70    spec_map.remove("is-builtin");
71
72    customize(spec_map);
73
74    std::fs::write(&path, serde_json::to_vec_pretty(&spec).unwrap()).unwrap();
75    TargetSelection::create_synthetic(&name, path.to_str().unwrap())
76}