Skip to main content

flowey_lib_hvlite/
build_igvmfilegen.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Build `igvmfilegen` binaries
5
6use crate::common::CommonTriple;
7use crate::run_cargo_build::BuildProfile;
8use flowey::node::prelude::*;
9use flowey_lib_common::_util::group_by;
10
11#[derive(Serialize, Deserialize)]
12#[serde(untagged)]
13pub enum IgvmfilegenOutput {
14    LinuxBin {
15        #[serde(rename = "igvmfilegen")]
16        bin: PathBuf,
17        #[serde(rename = "igvmfilegen.dbg")]
18        dbg: PathBuf,
19    },
20    WindowsBin {
21        #[serde(rename = "igvmfilegen.exe")]
22        exe: PathBuf,
23        #[serde(rename = "igvmfilegen.pdb")]
24        #[serde(default, skip_serializing_if = "Option::is_none")]
25        pdb: Option<PathBuf>,
26    },
27}
28
29impl Artifact for IgvmfilegenOutput {}
30
31#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
32pub struct IgvmfilegenBuildParams {
33    pub target: CommonTriple,
34    pub profile: BuildProfile,
35}
36
37flowey_request! {
38    pub struct Request {
39        pub build_params: IgvmfilegenBuildParams,
40        pub igvmfilegen: WriteVar<IgvmfilegenOutput>,
41    }
42}
43
44new_flow_node!(struct Node);
45
46impl FlowNode for Node {
47    type Request = Request;
48
49    fn imports(ctx: &mut ImportCtx<'_>) {
50        ctx.import::<crate::run_cargo_build::Node>();
51        ctx.import::<flowey_lib_common::install_dist_pkg::Node>();
52    }
53
54    fn emit(requests: Vec<Self::Request>, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
55        let requests = group_by(
56            requests
57                .into_iter()
58                .map(|r| (r.build_params, r.igvmfilegen)),
59        );
60        if requests.is_empty() {
61            return Ok(());
62        }
63
64        // `crypto`'s vendored OpenSSL build needs the headers and perl.
65        let ssl_pkgs = crate::common::openssl_dev_packages(ctx.platform())?;
66        let ssl_dep = (!ssl_pkgs.is_empty()).then(|| {
67            ctx.reqv(|v| flowey_lib_common::install_dist_pkg::Request::Install {
68                package_names: ssl_pkgs,
69                done: v,
70            })
71        });
72
73        for (IgvmfilegenBuildParams { target, profile }, outvars) in requests {
74            let output = ctx.reqv(|v| crate::run_cargo_build::Request {
75                crate_name: "igvmfilegen".into(),
76                out_name: "igvmfilegen".into(),
77                crate_type: flowey_lib_common::run_cargo_build::CargoCrateType::Bin,
78                profile,
79                features: Default::default(),
80                target: target.as_triple(),
81                no_split_dbg_info: false,
82                extra_env: None,
83                pre_build_deps: ssl_dep.clone().into_iter().collect(),
84                output: v,
85            });
86
87            ctx.emit_minor_rust_step("report built igvmfilegen", |ctx| {
88                let outvars = outvars.claim(ctx);
89                let output = output.claim(ctx);
90                move |rt| {
91                    let output = match rt.read(output) {
92                        crate::run_cargo_build::CargoBuildOutput::WindowsBin { exe, pdb } => {
93                            IgvmfilegenOutput::WindowsBin { exe, pdb }
94                        }
95                        crate::run_cargo_build::CargoBuildOutput::ElfBin { bin, dbg } => {
96                            IgvmfilegenOutput::LinuxBin {
97                                bin,
98                                dbg: dbg.unwrap(),
99                            }
100                        }
101                        _ => unreachable!(),
102                    };
103
104                    rt.write_all(outvars, &output);
105                }
106            });
107        }
108
109        Ok(())
110    }
111}