Skip to main content

flowey_lib_hvlite/
build_test_igvm_agent_rpc_server.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Build `test_igvm_agent_rpc_server` 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)]
14pub struct TestIgvmAgentRpcServerOutput {
15    #[serde(rename = "test_igvm_agent_rpc_server.exe")]
16    pub exe: PathBuf,
17    #[serde(rename = "test_igvm_agent_rpc_server.pdb")]
18    #[serde(default, skip_serializing_if = "Option::is_none")]
19    pub pdb: Option<PathBuf>,
20}
21
22impl Artifact for TestIgvmAgentRpcServerOutput {}
23
24flowey_request! {
25    pub struct Request {
26        pub target: CommonTriple,
27        pub profile: CommonProfile,
28        pub test_igvm_agent_rpc_server: WriteVar<TestIgvmAgentRpcServerOutput>,
29    }
30}
31
32new_flow_node!(struct Node);
33
34impl FlowNode for Node {
35    type Request = Request;
36
37    fn imports(ctx: &mut ImportCtx<'_>) {
38        ctx.import::<crate::run_cargo_build::Node>();
39    }
40
41    fn emit(requests: Vec<Self::Request>, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
42        let requests = group_by(
43            requests
44                .into_iter()
45                .map(|r| ((r.target, r.profile), r.test_igvm_agent_rpc_server)),
46        );
47        for ((target, profile), test_igvm_agent_rpc_server) in requests {
48            let target_triple = target.as_triple();
49
50            // Only Windows MSVC is supported for test_igvm_agent_rpc_server
51            if target_triple.operating_system != target_lexicon::OperatingSystem::Windows
52                || target_triple.environment != target_lexicon::Environment::Msvc
53            {
54                anyhow::bail!(
55                    "test_igvm_agent_rpc_server only supports Windows MSVC targets, got: {}",
56                    target_triple
57                );
58            }
59
60            let env_key = format!(
61                "CARGO_TARGET_{}_RUSTFLAGS",
62                target_triple.to_string().replace('-', "_").to_uppercase()
63            );
64
65            let mut env: BTreeMap<String, String> = BTreeMap::new();
66
67            // Enable CRT static linking
68            env.insert(env_key, "-Ctarget-feature=+crt-static".to_string());
69
70            let extra_env = Some(ReadVar::from_static(env));
71
72            let output = ctx.reqv(|v| crate::run_cargo_build::Request {
73                crate_name: "test_igvm_agent_rpc_server".into(),
74                out_name: "test_igvm_agent_rpc_server".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 test_igvm_agent_rpc_server", |ctx| {
86                let test_igvm_agent_rpc_server = test_igvm_agent_rpc_server.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                            TestIgvmAgentRpcServerOutput { exe, pdb }
92                        }
93                        _ => unreachable!(
94                            "unsupported build output variant for test_igvm_agent_rpc_server"
95                        ),
96                    };
97
98                    rt.write_all(test_igvm_agent_rpc_server, &output);
99                }
100            });
101        }
102
103        Ok(())
104    }
105}