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;
1011mod utils;
12pub use self::utils::{solar_pcx_from_build_opts, solar_pcx_from_solc_project};
1314// 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)]
24pub ast: bool,
2526/// The target EVM version.
27#[arg(long, value_name = "VERSION")]
28 #[serde(skip_serializing_if = "Option::is_none")]
29pub evm_version: Option<EvmVersion>,
3031/// Activate the Solidity optimizer.
32#[arg(long, default_missing_value="true", num_args = 0..=1)]
33 #[serde(skip)]
34pub optimize: Option<bool>,
3536/// 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")]
43pub optimizer_runs: Option<usize>,
4445/// 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")]
52pub extra_output: Vec<ContractOutputSelection>,
5354/// 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")]
59pub extra_output_files: Vec<ContractOutputSelection>,
60}
6162#[cfg(test)]
63mod tests {
64use super::*;
6566#[test]
67fn can_parse_evm_version() {
68let args: CompilerOpts =
69 CompilerOpts::parse_from(["foundry-cli", "--evm-version", "london"]);
70assert_eq!(args.evm_version, Some(EvmVersion::London));
71 }
7273#[test]
74fn can_parse_extra_output() {
75let args: CompilerOpts =
76 CompilerOpts::parse_from(["foundry-cli", "--extra-output", "metadata", "ir-optimized"]);
77assert_eq!(
78 args.extra_output,
79vec![ContractOutputSelection::Metadata, ContractOutputSelection::IrOptimized]
80 );
81 }
8283#[test]
84fn can_parse_extra_output_files() {
85let args: CompilerOpts = CompilerOpts::parse_from([
86"foundry-cli",
87"--extra-output-files",
88"metadata",
89"ir-optimized",
90 ]);
91assert_eq!(
92 args.extra_output_files,
93vec![ContractOutputSelection::Metadata, ContractOutputSelection::IrOptimized]
94 );
95 }
96}