Skip to main content

foundry_cli/opts/build/
mod.rs

1use clap::Parser;
2use foundry_compilers::artifacts::{EvmVersion, output_selection::ContractOutputSelection};
3use serde::Serialize;
4
5mod core;
6pub use self::core::BuildOpts;
7
8mod paths;
9pub use self::paths::ProjectPathOpts;
10
11mod revive;
12pub use self::revive::ResolcOpts;
13mod utils;
14pub use self::utils::{solar_pcx_from_build_opts, solar_pcx_from_solc_project};
15
16// A set of solc compiler settings that can be set via command line arguments, which are intended
17// to be merged into an existing `foundry_config::Config`.
18//
19// See also `BuildArgs`.
20#[derive(Clone, Debug, Default, Serialize, Parser)]
21#[command(next_help_heading = "Compiler options")]
22pub struct CompilerOpts {
23    /// Includes the AST as JSON in the compiler output.
24    #[arg(long, help_heading = "Compiler options")]
25    #[serde(skip)]
26    pub ast: bool,
27
28    /// The target EVM version.
29    #[arg(long, value_name = "VERSION")]
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub evm_version: Option<EvmVersion>,
32
33    /// Activate the Solidity optimizer.
34    #[arg(long, default_missing_value="true", num_args = 0..=1)]
35    #[serde(skip)]
36    pub optimize: Option<bool>,
37
38    /// The number of runs specifies roughly how often each opcode of the deployed code will be
39    /// executed across the life-time of the contract. This means it is a trade-off parameter
40    /// between code size (deploy cost) and code execution cost (cost after deployment).
41    /// An `optimizer_runs` parameter of `1` will produce short but expensive code. In contrast, a
42    /// larger `optimizer_runs` parameter will produce longer but more gas efficient code.
43    #[arg(long, value_name = "RUNS")]
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub optimizer_runs: Option<usize>,
46
47    /// Extra output to include in the contract's artifact.
48    ///
49    /// Example keys: evm.assembly, ewasm, ir, irOptimized, metadata
50    ///
51    /// For a full description, see <https://docs.soliditylang.org/en/v0.8.13/using-the-compiler.html#input-description>
52    #[arg(long, num_args(1..), value_name = "SELECTOR")]
53    #[serde(skip_serializing_if = "Vec::is_empty")]
54    pub extra_output: Vec<ContractOutputSelection>,
55
56    /// Extra output to write to separate files.
57    ///
58    /// Valid values: metadata, ir, irOptimized, ewasm, evm.assembly
59    #[arg(long, num_args(1..), value_name = "SELECTOR")]
60    #[serde(skip_serializing_if = "Vec::is_empty")]
61    pub extra_output_files: Vec<ContractOutputSelection>,
62
63    /// Compiler settings for resolc.
64    #[command(flatten)]
65    #[serde(skip)]
66    pub resolc_opts: ResolcOpts,
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn can_parse_evm_version() {
75        let args: CompilerOpts =
76            CompilerOpts::parse_from(["foundry-cli", "--evm-version", "london"]);
77        assert_eq!(args.evm_version, Some(EvmVersion::London));
78    }
79
80    #[test]
81    fn can_parse_extra_output() {
82        let args: CompilerOpts =
83            CompilerOpts::parse_from(["foundry-cli", "--extra-output", "metadata", "ir-optimized"]);
84        assert_eq!(
85            args.extra_output,
86            vec![ContractOutputSelection::Metadata, ContractOutputSelection::IrOptimized]
87        );
88    }
89
90    #[test]
91    fn can_parse_extra_output_files() {
92        let args: CompilerOpts = CompilerOpts::parse_from([
93            "foundry-cli",
94            "--extra-output-files",
95            "metadata",
96            "ir-optimized",
97        ]);
98        assert_eq!(
99            args.extra_output_files,
100            vec![ContractOutputSelection::Metadata, ContractOutputSelection::IrOptimized]
101        );
102    }
103}