bootstrap/core/build_steps/
synthetic_targets.rs1use 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 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 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 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}