Skip to main content

foundry_cli/opts/build/
revive.rs

1use clap::Parser;
2use foundry_config::{SolcReq, revive::PolkadotConfig};
3use serde::Serialize;
4
5#[derive(Clone, Debug, Default, Serialize, Parser)]
6#[clap(next_help_heading = "Resolc configuration")]
7/// Compiler options for resolc
8pub struct ResolcOpts {
9    #[arg(
10        value_name = "RESOLC_COMPILE",
11        help = "Enable compiling with resolc",
12        long = "resolc-compile",
13        visible_alias = "resolc",
14        action = clap::ArgAction::SetTrue
15    )]
16    pub resolc_compile: Option<bool>,
17
18    /// Specify the resolc version, or a path to a local resolc, to build with.
19    ///
20    /// Valid values follow the SemVer format `x.y.z-dev.n`, `resolc:x.y.z-dev.n` or
21    /// `path/to/resolc`.
22    #[arg(
23        long = "use-resolc",
24        help = "Use resolc version",
25        alias = "resolc-compiler-version",
26        value_name = "RESOLC_VERSION"
27    )]
28    #[serde(skip)]
29    pub use_resolc: Option<String>,
30
31    /// Set the LLVM optimization parameter `-O[0 | 1 | 2 | 3 | s | z]`.
32    /// Use `3` for best performance and `z` for minimal size.
33    #[arg(
34        short = 'O',
35        long = "resolc-optimizer-mode",
36        help = "Set the resolc optimization mode `-O[0 | 1 | 2 | 3 | s | z]`",
37        visible_alias = "resolc-optimization",
38        value_name = "LEVEL"
39    )]
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub optimizer_mode: Option<String>,
42
43    /// The emulated EVM linear heap memory static buffer size in bytes.
44    #[arg(long = "heap-size", help = "Set the contracts heap size in bytes", value_name = "SIZE")]
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub heap_size: Option<u32>,
47
48    /// The contracts total stack size in bytes.
49    #[arg(
50        long = "stack-size",
51        help = "Set the contracts total stack size in bytes",
52        value_name = "SIZE"
53    )]
54    #[serde(skip_serializing_if = "Option::is_none")]
55    pub stack_size: Option<u32>,
56
57    /// Generate source based debug information in the output code file.
58    #[arg(
59        long = "debug-info",
60        help = "Generate source based debug information in the output code file",
61        action = clap::ArgAction::SetTrue
62    )]
63    #[serde(skip_serializing_if = "Option::is_none")]
64    pub debug_information: Option<bool>,
65}
66
67impl ResolcOpts {
68    pub(crate) fn apply_overrides(&self, mut polkadot: PolkadotConfig) -> PolkadotConfig {
69        macro_rules! set_if_some {
70            ($src:expr, $dst:expr) => {
71                if let Some(src) = $src {
72                    $dst = src.into();
73                }
74            };
75        }
76
77        set_if_some!(
78            self.resolc_compile.and_then(|v| if v { Some(true) } else { None }),
79            polkadot.resolc_compile
80        );
81
82        set_if_some!(
83            self.use_resolc.as_ref().map(|v| SolcReq::from(v.trim_start_matches("resolc:"))),
84            polkadot.resolc
85        );
86        set_if_some!(
87            self.optimizer_mode.as_ref().and_then(|mode| mode.parse::<char>().ok()),
88            polkadot.optimizer_mode
89        );
90        set_if_some!(self.heap_size, polkadot.heap_size);
91        set_if_some!(self.stack_size, polkadot.stack_size);
92        set_if_some!(
93            self.debug_information.and_then(|v| if v { Some(true) } else { None }),
94            polkadot.debug_information
95        );
96
97        polkadot
98    }
99}