1use crate::pipelines_shared::cfg_common_params::CommonArchCli;
7use flowey::node::prelude::ReadVar;
8use flowey::pipeline::prelude::*;
9use flowey_lib_hvlite::build_openhcl_igvm_from_recipe::OpenhclIgvmRecipe;
10use flowey_lib_hvlite::build_openhcl_igvm_from_recipe::OpenhclKernelPackage;
11use flowey_lib_hvlite::build_openvmm_hcl::MaxTraceLevel;
12use flowey_lib_hvlite::common::CommonArch;
13use std::path::PathBuf;
14
15#[derive(clap::ValueEnum, Copy, Clone)]
16pub enum OpenhclRecipeCli {
17 Aarch64,
19 Aarch64Devkern,
21 X64Cvm,
23 X64CvmDevkern,
25 X64TestLinuxDirect,
28 X64TestLinuxDirectDevkern,
31 X64,
33 X64Devkern,
35}
36
37#[derive(clap::Args)]
39pub struct BuildIgvmCli<Recipe = OpenhclRecipeCli>
40where
41 Recipe: clap::ValueEnum + Clone + Send + Sync + 'static,
44{
45 pub recipe: Recipe,
54
55 #[clap(long)]
60 pub release: bool,
61
62 #[clap(long)]
65 pub release_cfg: bool,
66
67 #[clap(long)]
69 pub verbose: bool,
70
71 #[clap(long)]
73 pub locked: bool,
74
75 #[clap(long)]
77 pub install_missing_deps: bool,
78
79 #[clap(flatten)]
80 pub customizations: BuildIgvmCliCustomizations,
81}
82
83#[derive(clap::Args)]
84#[clap(next_help_heading = "Customizations")]
85pub struct BuildIgvmCliCustomizations {
86 #[clap(long, short = 'o')]
90 pub build_label: Option<String>,
91
92 #[clap(long)]
94 pub override_kernel_pkg: Option<KernelPackageKindCli>,
95
96 #[clap(long)]
98 pub override_openvmm_hcl_feature: Vec<String>,
99
100 #[clap(long)]
103 pub override_arch: Option<CommonArchCli>,
104
105 #[clap(long)]
108 pub override_manifest: Option<PathBuf>,
109
110 #[clap(long, requires = "release")]
116 pub with_perf_tools: bool,
117
118 #[clap(long)]
123 pub with_debuginfo: bool,
124
125 #[clap(long)]
129 pub with_mi_secure: bool,
130
131 #[clap(long)]
135 pub disable_secure_avic: bool,
136
137 #[clap(long)]
146 pub confidential_debug: bool,
147
148 #[clap(long)]
150 pub custom_openvmm_hcl: Option<PathBuf>,
151
152 #[clap(long)]
154 pub custom_openhcl_boot: Option<PathBuf>,
155
156 #[clap(long)]
158 pub custom_uefi: Option<PathBuf>,
159
160 #[clap(long)]
163 pub custom_kernel: Option<PathBuf>,
164
165 #[clap(long, requires = "custom_kernel")]
168 pub custom_kernel_modules: Option<PathBuf>,
169
170 #[clap(long)]
175 pub custom_vtl0_kernel: Option<PathBuf>,
176
177 #[clap(long)]
179 pub custom_layer: Vec<PathBuf>,
180
181 #[clap(long)]
183 pub custom_directory: Vec<PathBuf>,
184
185 #[clap(long)]
187 pub custom_extra_rootfs: Vec<PathBuf>,
188
189 #[clap(long)]
191 pub with_sidecar: bool,
192
193 #[clap(long, requires = "with_sidecar")]
196 pub custom_sidecar: Option<PathBuf>,
197
198 #[clap(long)]
201 pub max_trace_level: Option<MaxTraceLevelCli>,
202
203 #[clap(long, requires_all = ["custom_openvmm_deps", "custom_protoc", "custom_kernel", "custom_kernel_modules", "custom_uefi"])]
206 pub use_local_deps: bool,
207
208 #[clap(long)]
210 pub custom_openvmm_deps: Option<PathBuf>,
211
212 #[clap(long)]
214 pub custom_protoc: Option<PathBuf>,
215}
216
217#[derive(clap::ValueEnum, Copy, Clone, PartialEq, Eq, Debug)]
218pub enum KernelPackageKindCli {
219 Main,
221 Cvm,
223 Dev,
225 CvmDev,
227}
228
229#[derive(clap::ValueEnum, Copy, Clone, PartialEq, Eq, Debug)]
230pub enum MaxTraceLevelCli {
231 Trace,
233 Debug,
235 Info,
237 Warn,
239 Error,
241 Off,
243}
244
245impl From<MaxTraceLevelCli> for MaxTraceLevel {
246 fn from(cli: MaxTraceLevelCli) -> Self {
247 match cli {
248 MaxTraceLevelCli::Trace => MaxTraceLevel::Trace,
249 MaxTraceLevelCli::Debug => MaxTraceLevel::Debug,
250 MaxTraceLevelCli::Info => MaxTraceLevel::Info,
251 MaxTraceLevelCli::Warn => MaxTraceLevel::Warn,
252 MaxTraceLevelCli::Error => MaxTraceLevel::Error,
253 MaxTraceLevelCli::Off => MaxTraceLevel::Off,
254 }
255 }
256}
257
258pub fn bail_if_running_in_ci() -> anyhow::Result<()> {
259 const OVERRIDE_ENV: &str = "I_HAVE_A_GOOD_REASON_TO_RUN_BUILD_IGVM_IN_CI";
260
261 if std::env::var(OVERRIDE_ENV).is_ok() {
262 return Ok(());
263 }
264
265 for ci_env in ["TF_BUILD", "GITHUB_ACTIONS"] {
266 if std::env::var(ci_env).is_ok() {
267 log::warn!("Detected that {ci_env} is set");
268 log::warn!("");
269 log::warn!("Do not use `build-igvm` in CI scripts!");
270 log::warn!(
271 "This is a local-only, inner-dev-loop tool to build IGVM files, with an UNSTABLE CLI."
272 );
273 log::warn!("");
274 log::warn!(
275 "Automated pipelines should use the underlying `flowey` nodes that power build-igvm directly, _without_ relying on its CLI!"
276 );
277 log::warn!("");
278 log::warn!(
279 "If you _really_ know what you're doing, you can set {OVERRIDE_ENV} to disable this error."
280 );
281 anyhow::bail!("attempted to run `build-igvm` in CI")
282 }
283 }
284
285 Ok(())
286}
287
288impl IntoPipeline for BuildIgvmCli {
289 fn into_pipeline(self, backend_hint: PipelineBackendHint) -> anyhow::Result<Pipeline> {
290 if !matches!(backend_hint, PipelineBackendHint::Local) {
291 anyhow::bail!("build-igvm is for local use only")
292 }
293
294 bail_if_running_in_ci()?;
295
296 let openvmm_repo = flowey_lib_common::git_checkout::RepoSource::ExistingClone(
297 ReadVar::from_static(crate::repo_root()),
298 );
299
300 let Self {
301 recipe,
302 release,
303 release_cfg,
304 verbose,
305 locked,
306 install_missing_deps,
307 customizations:
308 BuildIgvmCliCustomizations {
309 build_label,
310 override_kernel_pkg,
311 override_openvmm_hcl_feature,
312 override_arch,
313 override_manifest,
314 with_perf_tools,
315 with_debuginfo,
316 with_mi_secure,
317 disable_secure_avic,
318 confidential_debug,
319 custom_openvmm_hcl,
320 custom_openhcl_boot,
321 custom_uefi,
322 custom_kernel,
323 custom_kernel_modules,
324 custom_vtl0_kernel,
325 custom_layer,
326 custom_directory,
327 with_sidecar,
328 custom_sidecar,
329 mut custom_extra_rootfs,
330 max_trace_level,
331 custom_openvmm_deps,
332 custom_protoc,
333 use_local_deps: _, },
335 } = self;
336
337 if with_perf_tools {
338 custom_extra_rootfs.push(crate::repo_root().join("openhcl/perftoolsfs.config"));
339 }
340
341 let mut pipeline = Pipeline::new();
342
343 let (pub_out_dir, _) = pipeline.new_artifact("build-igvm");
344
345 let recipe_arch = match recipe {
347 OpenhclRecipeCli::X64
348 | OpenhclRecipeCli::X64Devkern
349 | OpenhclRecipeCli::X64Cvm
350 | OpenhclRecipeCli::X64CvmDevkern
351 | OpenhclRecipeCli::X64TestLinuxDirect
352 | OpenhclRecipeCli::X64TestLinuxDirectDevkern => CommonArch::X86_64,
353 OpenhclRecipeCli::Aarch64 | OpenhclRecipeCli::Aarch64Devkern => CommonArch::Aarch64,
354 };
355
356 let effective_arch = override_arch.map(CommonArch::from).unwrap_or(recipe_arch);
358
359 let mut job = pipeline.new_job(
360 FlowPlatform::host(backend_hint),
361 FlowArch::host(backend_hint),
362 "build-igvm",
363 );
364
365 job = job.dep_on(|_| flowey_lib_hvlite::_jobs::cfg_versions::Request::Init);
368
369 if let Some(openvmm_deps_path) = custom_openvmm_deps {
371 job = job.dep_on(move |_| {
372 flowey_lib_hvlite::_jobs::cfg_versions::Request::LocalOpenvmmDeps(
373 effective_arch,
374 ReadVar::from_static(openvmm_deps_path),
375 )
376 });
377 }
378
379 if let Some(protoc_path) = custom_protoc {
381 job = job.dep_on(move |_| {
382 flowey_lib_hvlite::_jobs::cfg_versions::Request::LocalProtoc(ReadVar::from_static(
383 protoc_path,
384 ))
385 });
386 }
387
388 if let (Some(kernel_path), Some(modules_path)) =
390 (custom_kernel.clone(), custom_kernel_modules.clone())
391 {
392 job =
393 job.dep_on(
394 move |_| flowey_lib_hvlite::_jobs::cfg_versions::Request::LocalKernel {
395 arch: effective_arch,
396 kernel: ReadVar::from_static(kernel_path),
397 modules: ReadVar::from_static(modules_path),
398 },
399 );
400 }
401
402 if let Some(uefi_path) = custom_uefi {
404 job = job.dep_on(move |_| {
405 flowey_lib_hvlite::_jobs::cfg_versions::Request::LocalUefi(
406 effective_arch,
407 ReadVar::from_static(uefi_path),
408 )
409 });
410 }
411
412 job.dep_on(
413 |_| flowey_lib_hvlite::_jobs::cfg_hvlite_reposource::Params {
414 hvlite_repo_source: openvmm_repo,
415 },
416 )
417 .dep_on(|_| flowey_lib_hvlite::_jobs::cfg_common::Params {
418 local_only: Some(flowey_lib_hvlite::_jobs::cfg_common::LocalOnlyParams {
419 interactive: true,
420 auto_install: install_missing_deps,
421 ignore_rust_version: true,
422 }),
423 verbose: ReadVar::from_static(verbose),
424 locked,
425 deny_warnings: false,
426 no_incremental: false,
427 })
428 .dep_on(|ctx| flowey_lib_hvlite::_jobs::local_build_igvm::Params {
429 artifact_dir: ctx.publish_artifact(pub_out_dir),
430 done: ctx.new_done_handle(),
431
432 base_recipe: match recipe {
433 OpenhclRecipeCli::X64 => OpenhclIgvmRecipe::X64,
434 OpenhclRecipeCli::X64Devkern => OpenhclIgvmRecipe::X64Devkern,
435 OpenhclRecipeCli::X64TestLinuxDirect => OpenhclIgvmRecipe::X64TestLinuxDirect,
436 OpenhclRecipeCli::X64TestLinuxDirectDevkern => {
437 OpenhclIgvmRecipe::X64TestLinuxDirectDevkern
438 }
439 OpenhclRecipeCli::X64Cvm => OpenhclIgvmRecipe::X64Cvm,
440 OpenhclRecipeCli::X64CvmDevkern => OpenhclIgvmRecipe::X64CvmDevkern,
441 OpenhclRecipeCli::Aarch64 => OpenhclIgvmRecipe::Aarch64,
442 OpenhclRecipeCli::Aarch64Devkern => OpenhclIgvmRecipe::Aarch64Devkern,
443 },
444 release,
445 release_cfg,
446
447 customizations: flowey_lib_hvlite::_jobs::local_build_igvm::Customizations {
448 build_label,
449 override_arch: override_arch.map(CommonArch::from),
450 with_perf_tools,
451 with_debuginfo,
452 with_mi_secure,
453 disable_secure_avic,
454 confidential_debug,
455 override_kernel_pkg: override_kernel_pkg.map(|p| match p {
456 KernelPackageKindCli::Main => OpenhclKernelPackage::Main,
457 KernelPackageKindCli::Cvm => OpenhclKernelPackage::Cvm,
458 KernelPackageKindCli::Dev => OpenhclKernelPackage::Dev,
459 KernelPackageKindCli::CvmDev => OpenhclKernelPackage::CvmDev,
460 }),
461 with_sidecar,
462 custom_extra_rootfs,
463 override_openvmm_hcl_feature,
464 custom_sidecar,
465 override_manifest,
466 override_max_trace_level: max_trace_level.map(Into::into),
467 custom_openvmm_hcl,
468 custom_openhcl_boot,
469 custom_kernel,
470 custom_vtl0_kernel,
471 custom_layer,
472 custom_directory,
473 },
474 })
475 .finish();
476
477 Ok(pipeline)
478 }
479}