1use clap::Parser;
2use foundry_compilers::artifacts::{output_selection::ContractOutputSelection, EvmVersion};
3use serde::Serialize;
45mod core;
6pub use self::core::BuildOpts;
78mod paths;
9pub use self::paths::ProjectPathOpts;
1011// 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)]
21pub ast: bool,
2223/// The target EVM version.
24#[arg(long, value_name = "VERSION")]
25 #[serde(skip_serializing_if = "Option::is_none")]
26pub evm_version: Option<EvmVersion>,
2728/// Activate the Solidity optimizer.
29#[arg(long, default_missing_value="true", num_args = 0..=1)]
30 #[serde(skip)]
31pub optimize: Option<bool>,
3233/// 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")]
40pub optimizer_runs: Option<usize>,
4142/// 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")]
49pub extra_output: Vec<ContractOutputSelection>,
5051/// 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")]
56pub extra_output_files: Vec<ContractOutputSelection>,
57}
5859#[cfg(test)]
60mod tests {
61use super::*;
6263#[test]
64fn can_parse_evm_version() {
65let args: CompilerOpts =
66 CompilerOpts::parse_from(["foundry-cli", "--evm-version", "london"]);
67assert_eq!(args.evm_version, Some(EvmVersion::London));
68 }
6970#[test]
71fn can_parse_extra_output() {
72let args: CompilerOpts =
73 CompilerOpts::parse_from(["foundry-cli", "--extra-output", "metadata", "ir-optimized"]);
74assert_eq!(
75 args.extra_output,
76vec![ContractOutputSelection::Metadata, ContractOutputSelection::IrOptimized]
77 );
78 }
7980#[test]
81fn can_parse_extra_output_files() {
82let args: CompilerOpts = CompilerOpts::parse_from([
83"foundry-cli",
84"--extra-output-files",
85"metadata",
86"ir-optimized",
87 ]);
88assert_eq!(
89 args.extra_output_files,
90vec![ContractOutputSelection::Metadata, ContractOutputSelection::IrOptimized]
91 );
92 }
93}