openvm_build/
config.rs

1// Initial version copied from risc0 under Apache License 2.0
2
3use std::path::{Path, PathBuf};
4
5use cargo_metadata::Package;
6use serde::{Deserialize, Serialize};
7
8/// Options defining how to embed a guest package.
9#[derive(Default, Clone)]
10pub struct GuestOptions {
11    /// Features for cargo to build the guest with.
12    pub features: Vec<String>,
13    /// Configuration flags to build the guest with.
14    pub rustc_flags: Vec<String>,
15    /// Cargo profile
16    pub profile: Option<String>,
17    /// Target directory
18    pub target_dir: Option<PathBuf>,
19    /// Custom options to pass as args to `cargo build`.
20    pub(crate) options: Vec<String>,
21}
22
23impl GuestOptions {
24    /// Add custom options to pass to `cargo build`.
25    pub fn with_options<S: AsRef<str>>(mut self, options: impl IntoIterator<Item = S>) -> Self {
26        self.options
27            .extend(options.into_iter().map(|s| s.as_ref().to_string()));
28        self
29    }
30
31    /// Add package features to pass to `cargo build`.
32    pub fn with_features<S: AsRef<str>>(mut self, features: impl IntoIterator<Item = S>) -> Self {
33        self.features
34            .extend(features.into_iter().map(|s| s.as_ref().to_string()));
35        self
36    }
37
38    /// Add rustc flags for building the guest.
39    pub fn with_rustc_flags<S: AsRef<str>>(mut self, flags: impl IntoIterator<Item = S>) -> Self {
40        self.rustc_flags
41            .extend(flags.into_iter().map(|s| s.as_ref().to_string()));
42        self
43    }
44
45    /// Set the cargo profile.
46    pub fn with_profile(mut self, profile: String) -> Self {
47        self.profile = Some(profile);
48        self
49    }
50
51    /// Set the target directory.
52    pub fn with_target_dir<P: AsRef<Path>>(mut self, target_dir: P) -> Self {
53        self.target_dir = Some(target_dir.as_ref().to_path_buf());
54        self
55    }
56
57    #[allow(dead_code)]
58    pub(crate) fn with_metadata(mut self, metadata: GuestMetadata) -> Self {
59        self.rustc_flags = metadata.rustc_flags.unwrap_or_default();
60        self
61    }
62}
63
64/// Metadata defining options to build a guest
65#[derive(Serialize, Deserialize, Clone, Default)]
66pub(crate) struct GuestMetadata {
67    /// Configuration flags to build the guest with.
68    #[serde(rename = "rustc-flags")]
69    pub(crate) rustc_flags: Option<Vec<String>>,
70}
71
72impl From<&Package> for GuestMetadata {
73    fn from(value: &Package) -> Self {
74        let Some(obj) = value.metadata.get("openvm") else {
75            return Default::default();
76        };
77        serde_json::from_value(obj.clone()).unwrap()
78    }
79}