Skip to main content

flowey_lib_hvlite/
build_tpm_guest_tests.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Build `tpm_guest_tests` binaries
5
6use crate::common::CommonProfile;
7use crate::common::CommonTriple;
8use flowey::node::prelude::*;
9use flowey_lib_common::_util::group_by;
10use flowey_lib_common::run_cargo_build::CargoCrateType;
11use std::collections::BTreeMap;
12
13#[derive(Serialize, Deserialize)]
14#[serde(untagged)]
15pub enum TpmGuestTestsOutput {
16    WindowsBin {
17        #[serde(rename = "tpm_guest_tests.exe")]
18        exe: PathBuf,
19        #[serde(rename = "tpm_guest_tests.pdb")]
20        #[serde(default, skip_serializing_if = "Option::is_none")]
21        pdb: Option<PathBuf>,
22    },
23    LinuxBin {
24        #[serde(rename = "tpm_guest_tests")]
25        bin: PathBuf,
26        #[serde(rename = "tpm_guest_tests.dbg")]
27        dbg: Option<PathBuf>,
28    },
29}
30
31impl Artifact for TpmGuestTestsOutput {}
32
33flowey_request! {
34    pub struct Request {
35        pub target: CommonTriple,
36        pub profile: CommonProfile,
37        pub tpm_guest_tests: WriteVar<TpmGuestTestsOutput>,
38    }
39}
40
41new_flow_node!(struct Node);
42
43impl FlowNode for Node {
44    type Request = Request;
45
46    fn imports(ctx: &mut ImportCtx<'_>) {
47        ctx.import::<crate::run_cargo_build::Node>();
48    }
49
50    fn emit(requests: Vec<Self::Request>, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
51        let requests = group_by(
52            requests
53                .into_iter()
54                .map(|r| ((r.target, r.profile), r.tpm_guest_tests)),
55        );
56        for ((target, profile), tpm_guest_tests) in requests {
57            let target_triple = target.as_triple();
58            let extra_env = if target_triple.environment == target_lexicon::Environment::Msvc {
59                let env_key = format!(
60                    "CARGO_TARGET_{}_RUSTFLAGS",
61                    target_triple.to_string().replace('-', "_").to_uppercase()
62                );
63
64                let mut env: BTreeMap<String, String> = BTreeMap::new();
65
66                // Enable CRT static linking
67                env.insert(env_key, "-Ctarget-feature=+crt-static".to_string());
68
69                Some(ReadVar::from_static(env))
70            } else {
71                None
72            };
73
74            let output = ctx.reqv(|v| crate::run_cargo_build::Request {
75                crate_name: "tpm_guest_tests".into(),
76                out_name: "tpm_guest_tests".into(),
77                crate_type: CargoCrateType::Bin,
78                profile: profile.into(),
79                features: Default::default(),
80                target: target_triple,
81                no_split_dbg_info: false,
82                extra_env,
83                pre_build_deps: Vec::new(),
84                output: v,
85            });
86
87            ctx.emit_minor_rust_step("report built tpm_guest_tests", |ctx| {
88                let tpm_guest_tests = tpm_guest_tests.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                            TpmGuestTestsOutput::WindowsBin { exe, pdb }
94                        }
95                        crate::run_cargo_build::CargoBuildOutput::ElfBin { bin, dbg } => {
96                            TpmGuestTestsOutput::LinuxBin { bin, dbg }
97                        }
98                        _ => unreachable!("unsupported build output variant for tpm_guest_tests"),
99                    };
100
101                    rt.write_all(tpm_guest_tests, &output);
102                }
103            });
104        }
105
106        Ok(())
107    }
108}