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 std::collections::BTreeMap;
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        // de-dupe incoming requests
56        let requests = requests
57            .into_iter()
58            .fold(BTreeMap::<_, Vec<_>>::new(), |mut m, r| {
59                let Request {
60                    build_params,
61                    igvmfilegen,
62                } = r;
63                m.entry(build_params).or_default().push(igvmfilegen);
64                m
65            });
66
67        // `crypto`'s vendored OpenSSL build needs the headers and perl.
68        let ssl_pkgs: Vec<String> = match ctx.platform() {
69            FlowPlatform::Linux(distro) => match distro {
70                FlowPlatformLinuxDistro::Ubuntu => vec!["libssl-dev".into()],
71                FlowPlatformLinuxDistro::Fedora | FlowPlatformLinuxDistro::AzureLinux => {
72                    vec!["openssl-devel".into(), "perl".into()]
73                }
74                FlowPlatformLinuxDistro::Arch => vec!["openssl".into(), "perl".into()],
75                FlowPlatformLinuxDistro::Nix => Vec::new(),
76                FlowPlatformLinuxDistro::Unknown => anyhow::bail!("Unknown Linux distribution"),
77            },
78            _ => Vec::new(),
79        };
80        let ssl_dep = (!ssl_pkgs.is_empty()).then(|| {
81            ctx.reqv(|v| flowey_lib_common::install_dist_pkg::Request::Install {
82                package_names: ssl_pkgs,
83                done: v,
84            })
85        });
86
87        for (IgvmfilegenBuildParams { target, profile }, outvars) in requests {
88            let output = ctx.reqv(|v| crate::run_cargo_build::Request {
89                crate_name: "igvmfilegen".into(),
90                out_name: "igvmfilegen".into(),
91                crate_type: flowey_lib_common::run_cargo_build::CargoCrateType::Bin,
92                profile,
93                features: Default::default(),
94                target: target.as_triple(),
95                no_split_dbg_info: false,
96                extra_env: None,
97                pre_build_deps: ssl_dep.clone().into_iter().collect(),
98                output: v,
99            });
100
101            ctx.emit_minor_rust_step("report built igvmfilegen", |ctx| {
102                let outvars = outvars.claim(ctx);
103                let output = output.claim(ctx);
104                move |rt| {
105                    let output = match rt.read(output) {
106                        crate::run_cargo_build::CargoBuildOutput::WindowsBin { exe, pdb } => {
107                            IgvmfilegenOutput::WindowsBin { exe, pdb }
108                        }
109                        crate::run_cargo_build::CargoBuildOutput::ElfBin { bin, dbg } => {
110                            IgvmfilegenOutput::LinuxBin {
111                                bin,
112                                dbg: dbg.unwrap(),
113                            }
114                        }
115                        _ => unreachable!(),
116                    };
117
118                    for var in outvars {
119                        rt.write(var, &output);
120                    }
121                }
122            });
123        }
124
125        Ok(())
126    }
127}