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