flowey_lib_hvlite/
init_openvmm_magicpath_lxutil.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Ensure the lxutil package is moved into the correct "magic directory"
5//! as expected by the project-level `[env]` table in `.cargo/config.toml`
6
7use crate::download_lxutil::LxutilArch;
8use flowey::node::prelude::*;
9use std::collections::BTreeMap;
10
11flowey_request! {
12    pub struct Request {
13        pub arch: LxutilArch,
14        pub done: WriteVar<SideEffect>,
15    }
16}
17
18new_flow_node!(struct Node);
19
20impl FlowNode for Node {
21    type Request = Request;
22
23    fn imports(ctx: &mut ImportCtx<'_>) {
24        ctx.import::<crate::cfg_openvmm_magicpath::Node>();
25        ctx.import::<crate::download_lxutil::Node>();
26    }
27
28    fn emit(requests: Vec<Self::Request>, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
29        let mut reqs: BTreeMap<LxutilArch, Vec<WriteVar<SideEffect>>> = BTreeMap::new();
30        for Request { arch, done } in requests {
31            reqs.entry(arch).or_default().push(done);
32        }
33
34        // -- end of req processing -- //
35
36        let packages = reqs
37            .into_iter()
38            .map(|(arch, dones)| {
39                (
40                    arch,
41                    (
42                        ctx.reqv(|v| crate::download_lxutil::Request::GetPackage { arch, pkg: v }),
43                        dones,
44                    ),
45                )
46            })
47            .collect::<BTreeMap<_, _>>();
48
49        let openvmm_magicpath = ctx.reqv(crate::cfg_openvmm_magicpath::Request);
50
51        ctx.emit_rust_step("move lxutil.dll into its magic folder", |ctx| {
52            let packages = packages.claim(ctx);
53            let openvmm_magicpath = openvmm_magicpath.claim(ctx);
54            |rt| {
55                let openvmm_magicpath = rt.read(openvmm_magicpath);
56                for (arch, (pkg, _dones)) in packages {
57                    let pkg = rt.read(pkg);
58                    let dst_folder = openvmm_magicpath
59                        .join(format!(
60                            "Microsoft.WSL.LxUtil.{}",
61                            match arch {
62                                LxutilArch::Aarch64 => "arm64fre",
63                                LxutilArch::X86_64 => "amd64fre",
64                            }
65                        ))
66                        .join("build/native/bin");
67                    let dst = dst_folder.join("lxutil.dll");
68
69                    if pkg.lxutil_dll.absolute()? != dst.absolute()? {
70                        fs_err::create_dir_all(&dst_folder)?;
71                        fs_err::copy(pkg.lxutil_dll, dst_folder.join("lxutil.dll"))?;
72                    }
73                }
74                Ok(())
75            }
76        });
77
78        Ok(())
79    }
80}