flowey_lib_hvlite/_jobs/
check_clippy.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Ensure the OpenVMM repo is `clippy` clean.
5
6use crate::init_openvmm_magicpath_openhcl_sysroot::OpenvmmSysrootArch;
7use crate::run_cargo_build::common::CommonArch;
8use crate::run_cargo_build::common::CommonPlatform;
9use crate::run_cargo_build::common::CommonProfile;
10use crate::run_cargo_build::common::CommonTriple;
11use flowey::node::prelude::*;
12use flowey_lib_common::run_cargo_build::CargoBuildProfile;
13use flowey_lib_common::run_cargo_build::CargoFeatureSet;
14use flowey_lib_common::run_cargo_clippy::CargoPackage;
15
16flowey_request! {
17    pub struct Request {
18        pub target: target_lexicon::Triple,
19        pub profile: CommonProfile,
20        pub done: WriteVar<SideEffect>,
21        pub also_check_misc_nostd_crates: bool,
22    }
23}
24
25new_simple_flow_node!(struct Node);
26
27impl SimpleFlowNode for Node {
28    type Request = Request;
29
30    fn imports(ctx: &mut ImportCtx<'_>) {
31        ctx.import::<crate::build_xtask::Node>();
32        ctx.import::<crate::git_checkout_openvmm_repo::Node>();
33        ctx.import::<crate::init_openvmm_magicpath_openhcl_sysroot::Node>();
34        ctx.import::<crate::install_openvmm_rust_build_essential::Node>();
35        ctx.import::<crate::init_cross_build::Node>();
36        ctx.import::<flowey_lib_common::install_rust::Node>();
37        ctx.import::<flowey_lib_common::install_dist_pkg::Node>();
38        ctx.import::<flowey_lib_common::run_cargo_clippy::Node>();
39    }
40
41    fn process_request(request: Self::Request, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
42        let Request {
43            target,
44            profile,
45            done,
46            also_check_misc_nostd_crates,
47        } = request;
48
49        let flowey_platform = ctx.platform();
50        let flowey_arch = ctx.arch();
51
52        let (boot_target, uefi_target, sysroot_arch) = match target.architecture {
53            target_lexicon::Architecture::X86_64 => (
54                "x86_64-unknown-none",
55                "x86_64-unknown-uefi",
56                OpenvmmSysrootArch::X64,
57            ),
58            target_lexicon::Architecture::Aarch64(_) => (
59                "aarch64-unknown-linux-musl",
60                "aarch64-unknown-uefi",
61                OpenvmmSysrootArch::Aarch64,
62            ),
63            arch => anyhow::bail!("unsupported arch {arch}"),
64        };
65
66        let mut pre_build_deps = Vec::new();
67
68        // FIXME: this will go away once we have a dedicated cargo .config.toml
69        // for the openhcl _bin_. until we have that, we are building _every_
70        // musl target using the openhcl toolchain...
71
72        if matches!(target.environment, target_lexicon::Environment::Musl) {
73            pre_build_deps.push(
74                ctx.reqv(|v| crate::init_openvmm_magicpath_openhcl_sysroot::Request {
75                    arch: sysroot_arch,
76                    path: v,
77                })
78                .into_side_effect(),
79            );
80        }
81
82        ctx.req(flowey_lib_common::install_rust::Request::InstallTargetTriple(target.clone()));
83        if also_check_misc_nostd_crates {
84            ctx.req(
85                flowey_lib_common::install_rust::Request::InstallTargetTriple(
86                    target_lexicon::triple!(uefi_target),
87                ),
88            );
89            ctx.req(
90                flowey_lib_common::install_rust::Request::InstallTargetTriple(
91                    target_lexicon::triple!(boot_target),
92                ),
93            );
94        }
95
96        pre_build_deps.push(
97            ctx.reqv(|v| flowey_lib_common::install_dist_pkg::Request::Install {
98                package_names: vec!["libssl-dev".into(), "build-essential".into()],
99                done: v,
100            }),
101        );
102
103        pre_build_deps.push(ctx.reqv(crate::install_openvmm_rust_build_essential::Request));
104
105        // Cross compiling for MacOS isn't supported, but clippy still works
106        // with no additional dependencies
107        if !matches!(
108            target.operating_system,
109            target_lexicon::OperatingSystem::Darwin(_)
110        ) {
111            pre_build_deps.push(
112                ctx.reqv(|v| crate::init_cross_build::Request {
113                    target: target.clone(),
114                    injected_env: v,
115                })
116                .into_side_effect(),
117            );
118        }
119
120        let xtask_target = CommonTriple::Common {
121            arch: match flowey_arch {
122                FlowArch::X86_64 => CommonArch::X86_64,
123                FlowArch::Aarch64 => CommonArch::Aarch64,
124                arch => anyhow::bail!("unsupported arch {arch}"),
125            },
126            platform: match flowey_platform {
127                FlowPlatform::Windows => CommonPlatform::WindowsMsvc,
128                FlowPlatform::Linux(_) => CommonPlatform::LinuxGnu,
129                FlowPlatform::MacOs => CommonPlatform::MacOs,
130                platform => anyhow::bail!("unsupported platform {platform}"),
131            },
132        };
133
134        let xtask = ctx.reqv(|v| crate::build_xtask::Request {
135            target: xtask_target,
136            xtask: v,
137        });
138
139        let profile = match profile {
140            CommonProfile::Release => CargoBuildProfile::Release,
141            CommonProfile::Debug => CargoBuildProfile::Debug,
142        };
143
144        let openvmm_repo_path = ctx.reqv(crate::git_checkout_openvmm_repo::req::GetRepoDir);
145
146        let exclude = ctx.emit_rust_stepv("determine clippy exclusions", |ctx| {
147            let xtask = xtask.claim(ctx);
148            let repo_path = openvmm_repo_path.clone().claim(ctx);
149            move |rt| {
150                let xtask = rt.read(xtask);
151                let repo_path = rt.read(repo_path);
152
153                let mut exclude = vec!["guest_test_uefi".into()];
154
155                // packages depending on libfuzzer-sys are currently x86 only
156                if !(matches!(target.architecture, target_lexicon::Architecture::X86_64)
157                    && matches!(flowey_arch, FlowArch::X86_64))
158                {
159                    let xtask_bin = match xtask {
160                        crate::build_xtask::XtaskOutput::LinuxBin { bin, dbg: _ } => bin,
161                        crate::build_xtask::XtaskOutput::WindowsBin { exe, pdb: _ } => exe,
162                    };
163
164                    let sh = xshell::Shell::new()?;
165                    sh.change_dir(repo_path);
166                    let output = xshell::cmd!(sh, "{xtask_bin} fuzz list --crates").output()?;
167                    let output = String::from_utf8(output.stdout)?;
168
169                    let fuzz_crates = output.trim().split('\n').map(|s| s.to_owned());
170                    exclude.extend(fuzz_crates);
171
172                    exclude.push("chipset_device_fuzz".into());
173                    exclude.push("xtask_fuzz".into());
174                }
175
176                // packages requiring openssl-sys won't cross compile for macos
177                if matches!(
178                    target.operating_system,
179                    target_lexicon::OperatingSystem::Darwin(_)
180                ) {
181                    exclude.extend(
182                        ["openssl_kdf", "vmgs_lib", "block_crypto", "disk_crypt"].map(|x| x.into()),
183                    );
184                }
185
186                Ok(Some(exclude))
187            }
188        });
189
190        // HACK: the following behavior has been cargo-culted from our old
191        // CI, and at some point, we should actually improve the testing
192        // story on windows, so that we can run with FeatureSet::All in CI.
193        //
194        // On windows & mac, we can't build with all features, as many crates
195        // require openSSL for crypto, which isn't supported in CI yet.
196        let features = if matches!(
197            target.operating_system,
198            target_lexicon::OperatingSystem::Windows | target_lexicon::OperatingSystem::Darwin(_)
199        ) {
200            CargoFeatureSet::None
201        } else {
202            CargoFeatureSet::All
203        };
204
205        let mut reqs = vec![ctx.reqv(|v| flowey_lib_common::run_cargo_clippy::Request {
206            in_folder: openvmm_repo_path.clone(),
207            package: CargoPackage::Workspace,
208            profile: profile.clone(),
209            features: features.clone(),
210            target,
211            extra_env: None,
212            exclude,
213            keep_going: true,
214            all_targets: true,
215            pre_build_deps: pre_build_deps.clone(),
216            done: v,
217        })];
218
219        if also_check_misc_nostd_crates {
220            reqs.push(ctx.reqv(|v| flowey_lib_common::run_cargo_clippy::Request {
221                in_folder: openvmm_repo_path.clone(),
222                package: CargoPackage::Crate("openhcl_boot".into()),
223                profile: profile.clone(),
224                features: features.clone(),
225                target: target_lexicon::triple!(boot_target),
226                extra_env: Some(vec![("MINIMAL_RT_BUILD".into(), "1".into())]),
227                exclude: ReadVar::from_static(None),
228                keep_going: true,
229                all_targets: false,
230                pre_build_deps: pre_build_deps.clone(),
231                done: v,
232            }));
233
234            // don't pass --all-targets, since that pulls in a std dependency
235            reqs.push(ctx.reqv(|v| flowey_lib_common::run_cargo_clippy::Request {
236                in_folder: openvmm_repo_path.clone(),
237                package: CargoPackage::Crate("guest_test_uefi".into()),
238                profile: profile.clone(),
239                features,
240                target: target_lexicon::triple!(uefi_target),
241                extra_env: None,
242                exclude: ReadVar::from_static(None),
243                keep_going: true,
244                all_targets: false,
245                pre_build_deps: pre_build_deps.clone(),
246                done: v,
247            }));
248        }
249
250        ctx.emit_side_effect_step(reqs, [done]);
251
252        Ok(())
253    }
254}