flowey_lib_hvlite/
init_openvmm_magicpath_protoc.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Ensure protoc is symlinked into the correct "magic directory" set by the
5//! project-level `[env]` table in `.cargo/config.toml`
6
7use flowey::node::prelude::*;
8
9flowey_request! {
10    pub struct Request(pub WriteVar<SideEffect>);
11}
12
13new_flow_node!(struct Node);
14
15impl FlowNode for Node {
16    type Request = Request;
17
18    fn imports(ctx: &mut ImportCtx<'_>) {
19        ctx.import::<crate::cfg_openvmm_magicpath::Node>();
20        ctx.import::<flowey_lib_common::download_protoc::Node>();
21    }
22
23    fn emit(requests: Vec<Self::Request>, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
24        let protoc_pkg = ctx.reqv(flowey_lib_common::download_protoc::Request::Get);
25        let openvmm_magicpath = ctx.reqv(crate::cfg_openvmm_magicpath::Request);
26
27        ctx.emit_rust_step("symlink protoc", move |ctx| {
28            requests.into_iter().for_each(|x| {
29                x.0.claim(ctx);
30            });
31            let protoc_pkg = protoc_pkg.claim(ctx);
32            let openvmm_magicpath = openvmm_magicpath.claim(ctx);
33            move |rt| {
34                let expected_protoc_bin = {
35                    if cfg!(target_os = "windows") {
36                        "protoc.exe"
37                    } else if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
38                        "protoc"
39                    } else {
40                        unreachable!("unknown host os")
41                    }
42                };
43
44                let openvmm_magicpath = rt.read(openvmm_magicpath);
45                let dst_folder = openvmm_magicpath.join("Google.Protobuf.Tools/tools");
46                fs_err::create_dir_all(&dst_folder)?;
47
48                let protoc_pkg = rt.read(protoc_pkg);
49
50                flowey_lib_common::_util::copy_dir_all(
51                    protoc_pkg.include_dir,
52                    openvmm_magicpath.join("Google.Protobuf.Tools/tools/include"),
53                )?;
54
55                let src = protoc_pkg.protoc_bin;
56                let dst = dst_folder.join(expected_protoc_bin);
57
58                let _ = fs_err::remove_file(&dst);
59
60                if !dst.exists() {
61                    fs_err::hard_link(src.clone(), &dst)?;
62
63                    // Make sure protoc is executable
64                    #[cfg(unix)]
65                    {
66                        use std::os::unix::fs::PermissionsExt;
67                        let old_mode = dst.metadata()?.permissions().mode();
68                        fs_err::set_permissions(
69                            src,
70                            std::fs::Permissions::from_mode(old_mode | 0o111),
71                        )?;
72                    }
73                }
74
75                Ok(())
76            }
77        });
78
79        Ok(())
80    }
81}