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