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