bootstrap/core/build_steps/
synthetic_targets.rs
1use crate::Compiler;
11use crate::core::builder::{Builder, ShouldRun, Step};
12use crate::core::config::TargetSelection;
13use crate::utils::exec::command;
14
15#[derive(Debug, Clone, PartialEq, Eq, Hash)]
16pub(crate) struct MirOptPanicAbortSyntheticTarget {
17 pub(crate) compiler: Compiler,
18 pub(crate) base: TargetSelection,
19}
20
21impl Step for MirOptPanicAbortSyntheticTarget {
22 type Output = TargetSelection;
23 const DEFAULT: bool = true;
24 const ONLY_HOSTS: bool = false;
25
26 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
27 run.never()
28 }
29
30 fn 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}
36
37fn 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 {
44 if base.contains("synthetic") {
45 panic!("cannot create synthetic targets with other synthetic targets as their base");
48 }
49
50 let name = format!("{base}-synthetic-{suffix}");
51 let path = builder.out.join("synthetic-target-specs").join(format!("{name}.json"));
52 std::fs::create_dir_all(path.parent().unwrap()).unwrap();
53
54 if builder.config.dry_run() {
55 std::fs::write(&path, b"dry run\n").unwrap();
56 return TargetSelection::create_synthetic(&name, path.to_str().unwrap());
57 }
58
59 let mut cmd = command(builder.rustc(compiler));
60 cmd.arg("--target").arg(base.rustc_target_arg());
61 cmd.args(["-Zunstable-options", "--print", "target-spec-json"]);
62
63 cmd.env("RUSTC_BOOTSTRAP", "1");
66
67 let output = cmd.run_capture(builder).stdout();
68 let mut spec: serde_json::Value = serde_json::from_slice(output.as_bytes()).unwrap();
69 let spec_map = spec.as_object_mut().unwrap();
70
71 spec_map.remove("is-builtin");
73
74 customize(spec_map);
75
76 std::fs::write(&path, serde_json::to_vec_pretty(&spec).unwrap()).unwrap();
77 TargetSelection::create_synthetic(&name, path.to_str().unwrap())
78}