flowey_lib_hvlite/
build_openvmm_hcl.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Build `openvmm_hcl` binaries (NOT IGVM FILES!)
5
6use crate::init_openvmm_magicpath_openhcl_sysroot::OpenvmmSysrootArch;
7use crate::run_cargo_build::common::CommonArch;
8use crate::run_cargo_build::common::CommonTriple;
9use flowey::node::prelude::*;
10use flowey_lib_common::run_cargo_build::CargoFeatureSet;
11use std::collections::BTreeMap;
12use std::collections::BTreeSet;
13
14#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
15pub enum OpenvmmHclFeature {
16    Gdb,
17    Tpm,
18    LocalOnlyCustom(String),
19}
20
21#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
22pub enum OpenvmmHclBuildProfile {
23    Debug,
24    Release,
25    OpenvmmHclShip,
26}
27
28#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
29pub enum MaxTraceLevel {
30    Trace,
31    Debug,
32    Info,
33    Warn,
34    Error,
35    Off,
36}
37
38impl MaxTraceLevel {
39    pub fn features(&self) -> Vec<String> {
40        let name = match self {
41            MaxTraceLevel::Trace => return Vec::new(),
42            MaxTraceLevel::Debug => "debug",
43            MaxTraceLevel::Info => "info",
44            MaxTraceLevel::Warn => "warn",
45            MaxTraceLevel::Error => "error",
46            MaxTraceLevel::Off => "off",
47        };
48        // Add both release and non-release variants of the feature
49        // regardless of the profile to work around `tracing` bugs.
50        vec![
51            format!("tracing/max_level_{}", name),
52            format!("tracing/release_max_level_{}", name),
53        ]
54    }
55}
56
57#[derive(Serialize, Deserialize)]
58pub struct OpenvmmHclOutput {
59    pub bin: PathBuf,
60    pub dbg: Option<PathBuf>,
61}
62
63#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
64pub struct OpenvmmHclBuildParams {
65    pub target: CommonTriple,
66    pub profile: OpenvmmHclBuildProfile,
67    pub features: BTreeSet<OpenvmmHclFeature>,
68    pub no_split_dbg_info: bool,
69    pub max_trace_level: MaxTraceLevel,
70}
71
72flowey_request! {
73    pub struct Request {
74        pub build_params: OpenvmmHclBuildParams,
75        pub openvmm_hcl_output: WriteVar<OpenvmmHclOutput>,
76    }
77}
78
79new_flow_node!(struct Node);
80
81impl FlowNode for Node {
82    type Request = Request;
83
84    fn imports(ctx: &mut ImportCtx<'_>) {
85        ctx.import::<crate::run_cargo_build::Node>();
86        ctx.import::<crate::init_openvmm_magicpath_openhcl_sysroot::Node>();
87    }
88
89    fn emit(requests: Vec<Self::Request>, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
90        // de-dupe incoming requests
91        let requests = requests
92            .into_iter()
93            .fold(BTreeMap::<_, Vec<_>>::new(), |mut m, r| {
94                let Request {
95                    build_params,
96                    openvmm_hcl_output,
97                } = r;
98                m.entry(build_params).or_default().push(openvmm_hcl_output);
99                m
100            });
101
102        // -- end of req processing -- //
103
104        for (
105            OpenvmmHclBuildParams {
106                target,
107                profile,
108                features,
109                no_split_dbg_info,
110                max_trace_level,
111            },
112            outvars,
113        ) in requests
114        {
115            let mut pre_build_deps = Vec::new();
116
117            let target = target.as_triple();
118
119            let arch = CommonArch::from_triple(&target).ok_or_else(|| {
120                anyhow::anyhow!("cannot build openvmm_hcl on {}", target.architecture)
121            })?;
122
123            let openhcl_deps_path =
124                ctx.reqv(|v| crate::init_openvmm_magicpath_openhcl_sysroot::Request {
125                    arch: match arch {
126                        CommonArch::X86_64 => OpenvmmSysrootArch::X64,
127                        CommonArch::Aarch64 => OpenvmmSysrootArch::Aarch64,
128                    },
129                    path: v,
130                });
131
132            // required due to ambient dependencies in openvmm_hcl's source code
133            pre_build_deps.push(openhcl_deps_path.clone().into_side_effect());
134
135            let mut features = features
136                .into_iter()
137                .map(|f| match f {
138                    OpenvmmHclFeature::Gdb => "gdb".into(),
139                    OpenvmmHclFeature::Tpm => "tpm".into(),
140                    OpenvmmHclFeature::LocalOnlyCustom(s) => s,
141                })
142                .collect::<Vec<String>>();
143
144            features.extend(max_trace_level.features());
145
146            let output = ctx.reqv(|v| crate::run_cargo_build::Request {
147                crate_name: "openvmm_hcl".into(),
148                out_name: "openvmm_hcl".into(),
149                crate_type: flowey_lib_common::run_cargo_build::CargoCrateType::Bin,
150                profile: match profile {
151                    OpenvmmHclBuildProfile::Debug => crate::run_cargo_build::BuildProfile::Debug,
152                    OpenvmmHclBuildProfile::Release => {
153                        crate::run_cargo_build::BuildProfile::Release
154                    }
155                    OpenvmmHclBuildProfile::OpenvmmHclShip => {
156                        crate::run_cargo_build::BuildProfile::UnderhillShip
157                    }
158                },
159                features: CargoFeatureSet::Specific(features),
160                target,
161                no_split_dbg_info,
162                extra_env: None,
163                pre_build_deps,
164                output: v,
165            });
166
167            ctx.emit_minor_rust_step("report built openvmm_hcl", |ctx| {
168                let outvars = outvars.claim(ctx);
169                let output = output.claim(ctx);
170                move |rt| {
171                    let output = match rt.read(output) {
172                        crate::run_cargo_build::CargoBuildOutput::ElfBin { bin, dbg } => {
173                            OpenvmmHclOutput { bin, dbg }
174                        }
175                        _ => unreachable!(),
176                    };
177
178                    for var in outvars {
179                        rt.write(var, &output);
180                    }
181                }
182            });
183        }
184
185        Ok(())
186    }
187}