flowey_lib_hvlite/
build_openvmm_hcl.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Build `openvmm_hcl` binaries (NOT IGVM FILES!)

use crate::init_openvmm_magicpath_openhcl_sysroot::OpenvmmSysrootArch;
use crate::run_cargo_build::common::CommonArch;
use crate::run_cargo_build::common::CommonTriple;
use flowey::node::prelude::*;
use std::collections::BTreeMap;
use std::collections::BTreeSet;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum OpenvmmHclFeature {
    Gdb,
    Tpm,
    LocalOnlyCustom(String),
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum OpenvmmHclBuildProfile {
    Debug,
    Release,
    OpenvmmHclShip,
}

#[derive(Serialize, Deserialize)]
pub struct OpenvmmHclOutput {
    pub bin: PathBuf,
    pub dbg: Option<PathBuf>,
}

#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct OpenvmmHclBuildParams {
    pub target: CommonTriple,
    pub profile: OpenvmmHclBuildProfile,
    pub features: BTreeSet<OpenvmmHclFeature>,
    pub no_split_dbg_info: bool,
}

flowey_request! {
    pub struct Request {
        pub build_params: OpenvmmHclBuildParams,
        pub openvmm_hcl_output: WriteVar<OpenvmmHclOutput>,
    }
}

new_flow_node!(struct Node);

impl FlowNode for Node {
    type Request = Request;

    fn imports(ctx: &mut ImportCtx<'_>) {
        ctx.import::<crate::run_cargo_build::Node>();
        ctx.import::<crate::init_openvmm_magicpath_openhcl_sysroot::Node>();
    }

    fn emit(requests: Vec<Self::Request>, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
        // de-dupe incoming requests
        let requests = requests
            .into_iter()
            .fold(BTreeMap::<_, Vec<_>>::new(), |mut m, r| {
                let Request {
                    build_params,
                    openvmm_hcl_output,
                } = r;
                m.entry(build_params).or_default().push(openvmm_hcl_output);
                m
            });

        // -- end of req processing -- //

        for (
            OpenvmmHclBuildParams {
                target,
                profile,
                features,
                no_split_dbg_info,
            },
            outvars,
        ) in requests
        {
            let mut pre_build_deps = Vec::new();

            let target = target.as_triple();

            let arch = CommonArch::from_triple(&target).ok_or_else(|| {
                anyhow::anyhow!("cannot build openvmm_hcl on {}", target.architecture)
            })?;

            let openhcl_deps_path =
                ctx.reqv(|v| crate::init_openvmm_magicpath_openhcl_sysroot::Request {
                    arch: match arch {
                        CommonArch::X86_64 => OpenvmmSysrootArch::X64,
                        CommonArch::Aarch64 => OpenvmmSysrootArch::Aarch64,
                    },
                    path: v,
                });

            // required due to ambient dependencies in openvmm_hcl's source code
            pre_build_deps.push(openhcl_deps_path.clone().into_side_effect());

            let output = ctx.reqv(|v| crate::run_cargo_build::Request {
                crate_name: "openvmm_hcl".into(),
                out_name: "openvmm_hcl".into(),
                crate_type: flowey_lib_common::run_cargo_build::CargoCrateType::Bin,
                profile: match profile {
                    OpenvmmHclBuildProfile::Debug => crate::run_cargo_build::BuildProfile::Debug,
                    OpenvmmHclBuildProfile::Release => {
                        crate::run_cargo_build::BuildProfile::Release
                    }
                    OpenvmmHclBuildProfile::OpenvmmHclShip => {
                        crate::run_cargo_build::BuildProfile::UnderhillShip
                    }
                },
                features: features
                    .iter()
                    .map(|f| {
                        match f {
                            OpenvmmHclFeature::Gdb => "gdb",
                            OpenvmmHclFeature::Tpm => "tpm",
                            OpenvmmHclFeature::LocalOnlyCustom(s) => s,
                        }
                        .into()
                    })
                    .collect(),
                target,
                no_split_dbg_info,
                extra_env: None,
                pre_build_deps,
                output: v,
            });

            ctx.emit_minor_rust_step("report built openvmm_hcl", |ctx| {
                let outvars = outvars.claim(ctx);
                let output = output.claim(ctx);
                move |rt| {
                    let output = match rt.read(output) {
                        crate::run_cargo_build::CargoBuildOutput::ElfBin { bin, dbg } => {
                            OpenvmmHclOutput { bin, dbg }
                        }
                        _ => unreachable!(),
                    };

                    for var in outvars {
                        rt.write(var, &output);
                    }
                }
            });
        }

        Ok(())
    }
}