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.
910use crate::Compiler;
11use crate::core::builder::{Builder, ShouldRun, Step};
12use crate::core::config::TargetSelection;
13use crate::utils::exec::command;
1415#[derive(Debug, Clone, PartialEq, Eq, Hash)]
16pub(crate) struct MirOptPanicAbortSyntheticTarget {
17pub(crate) compiler: Compiler,
18pub(crate) base: TargetSelection,
19}
2021impl Step for MirOptPanicAbortSyntheticTarget {
22type Output = TargetSelection;
23const DEFAULT: bool = true;
24const ONLY_HOSTS: bool = false;
2526fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
27 run.never()
28 }
2930fn run(self, builder: &Builder<'_>) -> Self::Output {
31 create_synthetic_target(builder, self.compiler, "miropt-abort", self.base, |spec| {
32 spec.insert("panic-strategy".into(), "abort".into());
33 })
34 }
35}
3637fn create_synthetic_target(
38 builder: &Builder<'_>,
39 compiler: Compiler,
40 suffix: &str,
41 base: TargetSelection,
42 customize: impl FnOnce(&mut serde_json::Map<String, serde_json::Value>),
43) -> TargetSelection {
44if base.contains("synthetic") {
45// This check is not strictly needed, but nothing currently needs recursive synthetic
46 // targets. If the need arises, removing this in the future *SHOULD* be safe.
47panic!("cannot create synthetic targets with other synthetic targets as their base");
48 }
4950let name = format!("{base}-synthetic-{suffix}");
51let path = builder.out.join("synthetic-target-specs").join(format!("{name}.json"));
52 std::fs::create_dir_all(path.parent().unwrap()).unwrap();
5354if builder.config.dry_run() {
55 std::fs::write(&path, b"dry run\n").unwrap();
56return TargetSelection::create_synthetic(&name, path.to_str().unwrap());
57 }
5859let mut cmd = command(builder.rustc(compiler));
60 cmd.arg("--target").arg(base.rustc_target_arg());
61 cmd.args(["-Zunstable-options", "--print", "target-spec-json"]);
6263// If `rust.channel` is set to either beta or stable, rustc will complain that
64 // we cannot use nightly features. So `RUSTC_BOOTSTRAP` is needed here.
65cmd.env("RUSTC_BOOTSTRAP", "1");
6667let output = cmd.run_capture(builder).stdout();
68let mut spec: serde_json::Value = serde_json::from_slice(output.as_bytes()).unwrap();
69let spec_map = spec.as_object_mut().unwrap();
7071// The `is-builtin` attribute of a spec needs to be removed, otherwise rustc will complain.
72spec_map.remove("is-builtin");
7374 customize(spec_map);
7576 std::fs::write(&path, serde_json::to_vec_pretty(&spec).unwrap()).unwrap();
77 TargetSelection::create_synthetic(&name, path.to_str().unwrap())
78}