multiversx_sc_meta_lib/contract/sc_config/
contract_variant_settings.rs

1mod contract_allocator;
2mod stack_size;
3
4pub use contract_allocator::{parse_allocator, ContractAllocator};
5pub use stack_size::*;
6
7use crate::{ei::EIVersion, tools};
8
9use super::ContractVariantProfileSerde;
10
11/// Collection of flags, specified in the multicontract config.
12#[derive(Clone, PartialEq, Eq, Debug)]
13pub struct ContractVariantSettings {
14    /// External view contracts are just readers of data from another contract.
15    pub external_view: bool,
16
17    /// Panic messages add a lot of bloat to the final bytecode,
18    /// so they should only be used for debugging purposes.
19    pub panic_message: bool,
20
21    /// Post-processing check of the VM hooks is based on this.
22    pub check_ei: Option<EIVersion>,
23
24    /// Allocator config, i.e which allocator to choose for the contract.
25    pub allocator: ContractAllocator,
26
27    pub stack_size: usize,
28
29    /// Features that are activated on the contract crate, from wasm.
30    pub features: Vec<String>,
31
32    /// Allows disabling default features in the contract crate, from wasm.
33    pub default_features: Option<bool>,
34
35    /// Forcibly remove the original contract legacy callback.
36    pub kill_legacy_callback: bool,
37
38    pub profile: ContractVariantProfile,
39
40    /// Allows the contract to be built with std.
41    pub std: bool,
42
43    /// Rustc target when building WebAssembly.
44    pub rustc_target: String,
45}
46
47impl Default for ContractVariantSettings {
48    fn default() -> Self {
49        ContractVariantSettings {
50            external_view: Default::default(),
51            panic_message: Default::default(),
52            check_ei: Some(EIVersion::default()),
53            allocator: Default::default(),
54            stack_size: DEFAULT_STACK_SIZE,
55            features: Default::default(),
56            default_features: None,
57            kill_legacy_callback: false,
58            profile: Default::default(),
59            std: false,
60            rustc_target: tools::build_target::default_target().to_owned(),
61        }
62    }
63}
64
65#[derive(Clone, PartialEq, Eq, Debug)]
66pub struct ContractVariantProfile {
67    pub codegen_units: u8,
68    pub opt_level: String,
69    pub lto: bool,
70    pub debug: bool,
71    pub panic: String,
72    pub overflow_checks: bool,
73}
74
75impl Default for ContractVariantProfile {
76    fn default() -> ContractVariantProfile {
77        ContractVariantProfile {
78            codegen_units: 1u8,
79            opt_level: "z".to_owned(),
80            lto: true,
81            debug: false,
82            panic: "abort".to_owned(),
83            overflow_checks: false,
84        }
85    }
86}
87
88impl ContractVariantProfile {
89    pub fn from_serde(opt_serde_profile: &Option<ContractVariantProfileSerde>) -> Self {
90        let mut result = Self::default();
91        if let Some(serde_profile) = opt_serde_profile {
92            if let Some(codegen_units) = serde_profile.codegen_units {
93                result.codegen_units = codegen_units;
94            }
95            if let Some(opt_level) = &serde_profile.opt_level {
96                result.opt_level.clone_from(opt_level);
97            }
98            if let Some(lto) = serde_profile.lto {
99                result.lto = lto;
100            }
101            if let Some(debug) = serde_profile.debug {
102                result.debug = debug;
103            }
104            if let Some(panic) = &serde_profile.panic {
105                result.panic.clone_from(panic);
106            }
107            if let Some(overflow_checks) = serde_profile.overflow_checks {
108                result.overflow_checks = overflow_checks;
109            }
110        }
111        result
112    }
113}