Skip to main content

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::common::CommonArch;
7use crate::common::CommonTriple;
8use flowey::node::prelude::*;
9use flowey_lib_common::run_cargo_build::CargoFeatureSet;
10use std::collections::BTreeMap;
11use std::collections::BTreeSet;
12
13#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
14pub enum OpenvmmHclFeature {
15    Gdb,
16    MiSecure,
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        ctx.import::<flowey_lib_common::install_dist_pkg::Node>();
88    }
89
90    fn emit(requests: Vec<Self::Request>, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
91        // de-dupe incoming requests
92        let requests = requests
93            .into_iter()
94            .fold(BTreeMap::<_, Vec<_>>::new(), |mut m, r| {
95                let Request {
96                    build_params,
97                    openvmm_hcl_output,
98                } = r;
99                m.entry(build_params).or_default().push(openvmm_hcl_output);
100                m
101            });
102
103        // -- end of req processing -- //
104
105        for (
106            OpenvmmHclBuildParams {
107                target,
108                profile,
109                features,
110                no_split_dbg_info,
111                max_trace_level,
112            },
113            outvars,
114        ) in requests
115        {
116            let mut pre_build_deps = Vec::new();
117
118            let target = target.as_triple();
119
120            let arch = CommonArch::from_triple(&target)
121                .with_context(|| format!("cannot build openvmm_hcl on {}", target.architecture))?;
122
123            let openhcl_deps_path = ctx
124                .reqv(|v| crate::init_openvmm_magicpath_openhcl_sysroot::Request { arch, path: v });
125
126            // required due to ambient dependencies in openvmm_hcl's source code
127            pre_build_deps.push(openhcl_deps_path.clone().into_side_effect());
128
129            // TODO: install build tools for other platforms
130            if matches!(
131                ctx.platform(),
132                FlowPlatform::Linux(FlowPlatformLinuxDistro::Ubuntu)
133            ) {
134                pre_build_deps.push(ctx.reqv(|v| {
135                    flowey_lib_common::install_dist_pkg::Request::Install {
136                        package_names: vec!["build-essential".into()],
137                        done: v,
138                    }
139                }));
140            }
141
142            let mut features = features
143                .into_iter()
144                .map(|f| match f {
145                    OpenvmmHclFeature::Gdb => "gdb".into(),
146                    OpenvmmHclFeature::MiSecure => "mi-secure".into(),
147                    OpenvmmHclFeature::Tpm => "tpm".into(),
148                    OpenvmmHclFeature::LocalOnlyCustom(s) => s,
149                })
150                .collect::<Vec<String>>();
151
152            features.extend(max_trace_level.features());
153
154            let output = ctx.reqv(|v| crate::run_cargo_build::Request {
155                crate_name: "openvmm_hcl".into(),
156                out_name: "openvmm_hcl".into(),
157                crate_type: flowey_lib_common::run_cargo_build::CargoCrateType::Bin,
158                profile: match profile {
159                    OpenvmmHclBuildProfile::Debug => crate::run_cargo_build::BuildProfile::Debug,
160                    OpenvmmHclBuildProfile::Release => {
161                        crate::run_cargo_build::BuildProfile::Release
162                    }
163                    OpenvmmHclBuildProfile::OpenvmmHclShip => {
164                        crate::run_cargo_build::BuildProfile::UnderhillShip
165                    }
166                },
167                features: CargoFeatureSet::Specific(features),
168                target,
169                no_split_dbg_info,
170                extra_env: None,
171                pre_build_deps,
172                output: v,
173            });
174
175            ctx.emit_minor_rust_step("report built openvmm_hcl", |ctx| {
176                let outvars = outvars.claim(ctx);
177                let output = output.claim(ctx);
178                move |rt| {
179                    let output = match rt.read(output) {
180                        crate::run_cargo_build::CargoBuildOutput::ElfBin { bin, dbg } => {
181                            OpenvmmHclOutput { bin, dbg }
182                        }
183                        _ => unreachable!(),
184                    };
185
186                    for var in outvars {
187                        rt.write(var, &output);
188                    }
189                }
190            });
191        }
192
193        Ok(())
194    }
195}