Skip to main content

flowey_hvlite/pipelines/
build_reproducible.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! See [`BuildReproducibleCli`]
5
6use flowey::node::prelude::FlowPlatformLinuxDistro;
7use flowey::node::prelude::ReadVar;
8use flowey::pipeline::prelude::*;
9use flowey_lib_common::git_checkout::RepoSource;
10use flowey_lib_hvlite::_jobs::build_and_publish_openhcl_igvm_from_recipe::OpenhclIgvmBuildParams;
11use flowey_lib_hvlite::_jobs::build_and_publish_openhcl_igvm_from_recipe::OpenhclIgvmOutputs;
12use flowey_lib_hvlite::build_openhcl_igvm_from_recipe::OpenhclIgvmRecipe;
13use flowey_lib_hvlite::build_openvmm_hcl::OpenvmmHclBuildProfile;
14use flowey_lib_hvlite::common::CommonArch;
15use flowey_lib_hvlite::common::CommonPlatform;
16use flowey_lib_hvlite::common::CommonTriple;
17use flowey_lib_hvlite::resolve_openhcl_kernel_package::OpenhclKernelPackageKind;
18use std::collections::BTreeSet;
19use target_lexicon::Triple;
20
21/// A list of pre-defined OpenHCL recipes that support being built reproducibly. Each recipe has a matching CI pipeline job that can be reproduced with this local CLI.
22#[derive(clap::ValueEnum, Copy, Clone)]
23pub enum ReproducibleOpenHclRecipe {
24    X64Cvm,
25}
26
27/// Build reproducible artifacts locally. DO NOT USE IN CI.
28#[derive(clap::Args)]
29pub struct BuildReproducibleCli {
30    /// Specify which OpenHCL recipe to build / customize off-of.
31    ///
32    /// A "recipe" corresponds to the various standard IGVM SKUs that are
33    /// actively supported and tested in our build infrastructure.
34    ///
35    /// It encodes all the details of what goes into an individual IGVM file,
36    /// such as what build flags `openvmm_hcl` should be built with, what goes
37    /// into a VTL2 initrd, what `igvmfilegen` manifest is being used, etc...
38    pub recipe: ReproducibleOpenHclRecipe,
39
40    /// Build using release variants of all constituent binary components.
41    ///
42    /// Uses --profile=boot-release for openhcl_boot, --profile=openhcl-ship
43    /// when building openvmm_hcl, etc...
44    #[clap(long)]
45    pub release: bool,
46}
47
48impl IntoPipeline for BuildReproducibleCli {
49    fn into_pipeline(self, backend_hint: PipelineBackendHint) -> anyhow::Result<Pipeline> {
50        if !matches!(backend_hint, PipelineBackendHint::Local) {
51            anyhow::bail!("build-reproducible is for local use only")
52        }
53
54        let Self { recipe, release } = self;
55
56        let mut pipeline = Pipeline::new();
57
58        let (pub_openhcl_igvm, _use_openhcl_igvm) =
59            pipeline.new_typed_artifact("x64-openhcl-igvm-cvm");
60        let (pub_openhcl_igvm_extras, _use_openhcl_igvm_extras) =
61            pipeline.new_typed_artifact("x64-openhcl-igvm-cvm-extras");
62
63        let local_run_args = {
64            let mut args = crate::pipelines_shared::cfg_common_params::LocalRunArgs::default();
65            args.locked = true;
66            args.no_incremental = true;
67            args
68        };
69        let cfg_common_params = crate::pipelines_shared::cfg_common_params::get_cfg_common_params(
70            &mut pipeline,
71            backend_hint,
72            Some(local_run_args),
73        )?;
74
75        let openvmm_repo_source =
76            RepoSource::ExistingClone(ReadVar::from_static(crate::repo_root()));
77
78        let mut job = pipeline.new_job(
79            FlowPlatform::Linux(FlowPlatformLinuxDistro::Nix),
80            FlowArch::host(backend_hint),
81            "build-reproducible",
82        );
83
84        // wrap all shell commands with `nix-shell --pure --run`
85        job = job.set_command_wrapper(flowey::shell::CommandWrapperKind::NixShell {
86            path: Some(crate::repo_root().join("shell.nix")),
87        });
88
89        let openvmm_hcl_profile = if release {
90            OpenvmmHclBuildProfile::OpenvmmHclShip
91        } else {
92            OpenvmmHclBuildProfile::Debug
93        };
94
95        let (recipe_arch, kernel_kind) = match recipe {
96            ReproducibleOpenHclRecipe::X64Cvm => {
97                (CommonArch::X86_64, OpenhclKernelPackageKind::Cvm)
98            }
99        };
100
101        let recipe = match recipe {
102            ReproducibleOpenHclRecipe::X64Cvm => OpenhclIgvmRecipe::X64Cvm,
103        };
104
105        let openhcl_musl_target = |arch: CommonArch| -> Triple {
106            CommonTriple::Common {
107                arch,
108                platform: CommonPlatform::LinuxMusl,
109            }
110            .as_triple()
111        };
112
113        job = job.dep_on(&cfg_common_params);
114        job = job.dep_on(
115            |_| flowey_lib_hvlite::_jobs::cfg_hvlite_reposource::Params {
116                hvlite_repo_source: openvmm_repo_source.clone(),
117            },
118        );
119        job = job.dep_on(|ctx| {
120            flowey_lib_hvlite::_jobs::build_openhcl_igvm_from_recipe_nix::Params {
121                arch: recipe_arch,
122                kernel_kind,
123                igvm_files: vec![(
124                    OpenhclIgvmBuildParams {
125                        profile: openvmm_hcl_profile,
126                        recipe,
127                        custom_target: Some(CommonTriple::Custom(openhcl_musl_target(recipe_arch))),
128                        extra_features: BTreeSet::new(),
129                        uefi_firmware_flavor: None,
130                        release_cfg: release,
131                        confidential_debug: false,
132                    },
133                    OpenhclIgvmOutputs {
134                        openhcl_igvm: ctx.publish_typed_artifact(pub_openhcl_igvm),
135                        openhcl_igvm_extras: ctx.publish_typed_artifact(pub_openhcl_igvm_extras),
136                        vmfirmwareigvm_dll: None,
137                        artifact_openhcl_verify_size_baseline: None,
138                    },
139                )],
140            }
141        });
142
143        job.finish();
144
145        Ok(pipeline)
146    }
147}