openvm_build/
config.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Initial version copied from risc0 under Apache License 2.0

use std::path::{Path, PathBuf};

use cargo_metadata::Package;
use serde::{Deserialize, Serialize};

/// Options defining how to embed a guest package.
#[derive(Default, Clone)]
pub struct GuestOptions {
    /// Features for cargo to build the guest with.
    pub features: Vec<String>,
    /// Custom options to pass as args to `cargo build`.
    pub options: Vec<String>,
    /// Configuration flags to build the guest with.
    pub rustc_flags: Vec<String>,
    /// Cargo profile
    pub profile: Option<String>,
    /// Target directory
    pub target_dir: Option<PathBuf>,
}

impl GuestOptions {
    /// Add custom options to pass to `cargo build`.
    pub fn with_options<S: AsRef<str>>(mut self, options: impl IntoIterator<Item = S>) -> Self {
        self.options
            .extend(options.into_iter().map(|s| s.as_ref().to_string()));
        self
    }

    /// Add package features to pass to `cargo build`.
    pub fn with_features<S: AsRef<str>>(mut self, features: impl IntoIterator<Item = S>) -> Self {
        self.features
            .extend(features.into_iter().map(|s| s.as_ref().to_string()));
        self
    }

    /// Add rustc flags for building the guest.
    pub fn with_rustc_flags<S: AsRef<str>>(mut self, flags: impl IntoIterator<Item = S>) -> Self {
        self.rustc_flags
            .extend(flags.into_iter().map(|s| s.as_ref().to_string()));
        self
    }

    /// Set the cargo profile.
    pub fn with_profile(mut self, profile: String) -> Self {
        self.profile = Some(profile);
        self
    }

    /// Set the target directory.
    pub fn with_target_dir<P: AsRef<Path>>(mut self, target_dir: P) -> Self {
        self.target_dir = Some(target_dir.as_ref().to_path_buf());
        self
    }

    #[allow(dead_code)]
    pub(crate) fn with_metadata(mut self, metadata: GuestMetadata) -> Self {
        self.rustc_flags = metadata.rustc_flags.unwrap_or_default();
        self
    }
}

/// Metadata defining options to build a guest
#[derive(Serialize, Deserialize, Clone, Default)]
pub(crate) struct GuestMetadata {
    /// Configuration flags to build the guest with.
    #[serde(rename = "rustc-flags")]
    pub(crate) rustc_flags: Option<Vec<String>>,
}

impl From<&Package> for GuestMetadata {
    fn from(value: &Package) -> Self {
        let Some(obj) = value.metadata.get("risc0") else {
            return Default::default();
        };
        serde_json::from_value(obj.clone()).unwrap()
    }
}