foundry_cli/opts/build/
mod.rs

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