1use super::ProjectPathOpts;
2use crate::{opts::CompilerOpts, utils::LoadConfig};
3use clap::{Parser, ValueHint};
4use eyre::Result;
5use foundry_compilers::{
6 Project,
7 artifacts::{RevertStrings, remappings::Remapping},
8 compilers::multi::MultiCompiler,
9 utils::canonicalized,
10};
11use foundry_config::{
12 Config, DenyLevel, Remappings,
13 figment::{
14 self, Figment, Metadata, Profile, Provider,
15 error::Kind::InvalidType,
16 value::{Dict, Map, Value},
17 },
18 filter::SkipBuildFilter,
19};
20use serde::Serialize;
21use std::path::PathBuf;
22
23#[derive(Clone, Debug, Default, Serialize, Parser)]
24#[command(next_help_heading = "Build options")]
25pub struct BuildOpts {
26 #[arg(long, help_heading = "Cache options")]
28 #[serde(skip)]
29 pub force: bool,
30
31 #[arg(long)]
33 #[serde(skip)]
34 pub no_cache: bool,
35
36 #[arg(long, conflicts_with = "no_cache")]
38 #[serde(skip)]
39 pub dynamic_test_linking: bool,
40
41 #[arg(long, help_heading = "Linker options", env = "DAPP_LIBRARIES")]
43 #[serde(skip_serializing_if = "Vec::is_empty")]
44 pub libraries: Vec<String>,
45
46 #[arg(long, help_heading = "Compiler options", value_name = "ERROR_CODES")]
48 #[serde(skip_serializing_if = "Vec::is_empty")]
49 pub ignored_error_codes: Vec<u64>,
50
51 #[arg(
60 long,
61 short = 'D',
62 help_heading = "Compiler options",
63 value_name = "LEVEL",
64 conflicts_with = "deny_warnings"
65 )]
66 #[serde(skip)]
67 pub deny: Option<DenyLevel>,
68
69 #[arg(long = "deny-warnings", hide = true)]
71 pub deny_warnings: bool,
72
73 #[arg(long, help_heading = "Compiler options")]
75 #[serde(skip)]
76 pub no_auto_detect: bool,
77
78 #[arg(
82 long = "use",
83 alias = "compiler-version",
84 help_heading = "Compiler options",
85 value_name = "SOLC_VERSION"
86 )]
87 #[serde(skip)]
88 pub use_solc: Option<String>,
89
90 #[arg(help_heading = "Compiler options", long)]
94 #[serde(skip)]
95 pub offline: bool,
96
97 #[arg(long, help_heading = "Compiler options")]
99 #[serde(skip)]
100 pub via_ir: bool,
101
102 #[arg(long, help_heading = "Compiler options")]
104 #[serde(skip)]
105 pub use_literal_content: bool,
106
107 #[arg(long, help_heading = "Compiler options")]
111 #[serde(skip)]
112 pub no_metadata: bool,
113
114 #[arg(
116 long = "out",
117 short,
118 help_heading = "Project options",
119 value_hint = ValueHint::DirPath,
120 value_name = "PATH",
121 )]
122 #[serde(rename = "out", skip_serializing_if = "Option::is_none")]
123 pub out_path: Option<PathBuf>,
124
125 #[arg(long, help_heading = "Project options", value_name = "REVERT")]
130 #[serde(skip)]
131 pub revert_strings: Option<RevertStrings>,
132
133 #[arg(long, help_heading = "Project options")]
135 #[serde(skip)]
136 pub build_info: bool,
137
138 #[arg(
140 long,
141 help_heading = "Project options",
142 value_hint = ValueHint::DirPath,
143 value_name = "PATH",
144 requires = "build_info",
145 )]
146 #[serde(skip_serializing_if = "Option::is_none")]
147 pub build_info_path: Option<PathBuf>,
148
149 #[arg(long, num_args(1..))]
153 #[serde(skip)]
154 pub skip: Option<Vec<SkipBuildFilter>>,
155
156 #[command(flatten)]
157 #[serde(flatten)]
158 pub compiler: CompilerOpts,
159
160 #[command(flatten)]
161 #[serde(flatten)]
162 pub project_paths: ProjectPathOpts,
163}
164
165impl BuildOpts {
166 pub fn project(&self) -> Result<Project<MultiCompiler>> {
172 let config = self.load_config()?;
173 Ok(config.project()?)
174 }
175
176 #[deprecated(note = "Use ProjectPathsArgs::get_remappings() instead")]
178 pub fn get_remappings(&self) -> Vec<Remapping> {
179 self.project_paths.get_remappings()
180 }
181}
182
183impl<'a> From<&'a BuildOpts> for Figment {
185 fn from(args: &'a BuildOpts) -> Self {
186 let root = if let Some(config_path) = &args.project_paths.config_path {
187 if !config_path.exists() {
188 panic!("error: config-path `{}` does not exist", config_path.display())
189 }
190 if !config_path.ends_with(Config::FILE_NAME) {
191 panic!("error: the config-path must be a path to a foundry.toml file")
192 }
193 let config_path = canonicalized(config_path);
194 config_path.parent().unwrap().to_path_buf()
195 } else {
196 args.project_paths.project_root()
197 };
198 let mut figment = Config::figment_with_root(root);
199
200 let mut remappings = Remappings::new_with_remappings(args.project_paths.get_remappings())
202 .with_figment(&figment);
203 remappings
204 .extend(figment.extract_inner::<Vec<Remapping>>("remappings").unwrap_or_default());
205 figment = figment.merge(("remappings", remappings.into_inner())).merge(args);
206
207 if let Some(skip) = &args.skip {
208 let mut skip = skip.iter().map(|s| s.file_pattern().to_string()).collect::<Vec<_>>();
209 skip.extend(figment.extract_inner::<Vec<String>>("skip").unwrap_or_default());
210 figment = figment.merge(("skip", skip));
211 };
212
213 figment
214 }
215}
216
217impl Provider for BuildOpts {
218 fn metadata(&self) -> Metadata {
219 Metadata::named("Core Build Args Provider")
220 }
221
222 fn data(&self) -> Result<Map<Profile, Dict>, figment::Error> {
223 let value = Value::serialize(self)?;
224 let error = InvalidType(value.to_actual(), "map".into());
225 let mut dict = value.into_dict().ok_or(error)?;
226
227 if self.no_auto_detect {
228 dict.insert("auto_detect_solc".to_string(), false.into());
229 }
230
231 if let Some(ref solc) = self.use_solc {
232 dict.insert("solc".to_string(), solc.trim_start_matches("solc:").into());
233 }
234
235 if self.offline {
236 dict.insert("offline".to_string(), true.into());
237 }
238
239 if self.deny_warnings {
240 dict.insert("deny".to_string(), figment::value::Value::serialize(DenyLevel::Warnings)?);
241 _ = sh_warn!("`--deny-warnings` is being deprecated in favor of `--deny warnings`.");
242 } else if let Some(deny) = self.deny {
243 dict.insert("deny".to_string(), figment::value::Value::serialize(deny)?);
244 }
245
246 if self.via_ir {
247 dict.insert("via_ir".to_string(), true.into());
248 }
249
250 if self.use_literal_content {
251 dict.insert("use_literal_content".to_string(), true.into());
252 }
253
254 if self.no_metadata {
255 dict.insert("bytecode_hash".to_string(), "none".into());
256 dict.insert("cbor_metadata".to_string(), false.into());
257 }
258
259 if self.force {
260 dict.insert("force".to_string(), self.force.into());
261 }
262
263 if self.no_cache {
265 dict.insert("cache".to_string(), false.into());
266 }
267
268 if self.dynamic_test_linking {
269 dict.insert("dynamic_test_linking".to_string(), true.into());
270 }
271
272 if self.build_info {
273 dict.insert("build_info".to_string(), self.build_info.into());
274 }
275
276 if self.compiler.ast {
277 dict.insert("ast".to_string(), true.into());
278 }
279
280 if let Some(optimize) = self.compiler.optimize {
281 dict.insert("optimizer".to_string(), optimize.into());
282 }
283
284 if !self.compiler.extra_output.is_empty() {
285 let selection: Vec<_> =
286 self.compiler.extra_output.iter().map(|s| s.to_string()).collect();
287 dict.insert("extra_output".to_string(), selection.into());
288 }
289
290 if !self.compiler.extra_output_files.is_empty() {
291 let selection: Vec<_> =
292 self.compiler.extra_output_files.iter().map(|s| s.to_string()).collect();
293 dict.insert("extra_output_files".to_string(), selection.into());
294 }
295
296 if let Some(ref revert) = self.revert_strings {
297 dict.insert("revert_strings".to_string(), revert.to_string().into());
298 }
299
300 Ok(Map::from([(Config::selected_profile(), dict)]))
301 }
302}