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 assert!(
188 config_path.exists(),
189 "error: config-path `{}` does not exist",
190 config_path.display()
191 );
192 assert!(
193 config_path.ends_with(Config::FILE_NAME),
194 "error: the config-path must be a path to a foundry.toml file"
195 );
196 let config_path = canonicalized(config_path);
197 config_path.parent().unwrap().to_path_buf()
198 } else {
199 args.project_paths.project_root()
200 };
201 let mut figment = Config::figment_with_root(root);
202
203 let mut remappings = Remappings::new_with_remappings(args.project_paths.get_remappings())
205 .with_figment(&figment);
206 remappings
207 .extend(figment.extract_inner::<Vec<Remapping>>("remappings").unwrap_or_default());
208 figment = figment.merge(("remappings", remappings.into_inner())).merge(args);
209
210 if let Some(skip) = &args.skip {
211 let mut skip = skip.iter().map(|s| s.file_pattern().to_string()).collect::<Vec<_>>();
212 skip.extend(figment.extract_inner::<Vec<String>>("skip").unwrap_or_default());
213 figment = figment.merge(("skip", skip));
214 };
215
216 figment
217 }
218}
219
220impl Provider for BuildOpts {
221 fn metadata(&self) -> Metadata {
222 Metadata::named("Core Build Args Provider")
223 }
224
225 fn data(&self) -> Result<Map<Profile, Dict>, figment::Error> {
226 let value = Value::serialize(self)?;
227 let error = InvalidType(value.to_actual(), "map".into());
228 let mut dict = value.into_dict().ok_or(error)?;
229
230 if self.no_auto_detect {
231 dict.insert("auto_detect_solc".to_string(), false.into());
232 }
233
234 if let Some(ref solc) = self.use_solc {
235 dict.insert("solc".to_string(), solc.trim_start_matches("solc:").into());
236 }
237
238 if self.offline {
239 dict.insert("offline".to_string(), true.into());
240 }
241
242 if self.deny_warnings {
243 dict.insert("deny".to_string(), figment::value::Value::serialize(DenyLevel::Warnings)?);
244 _ = sh_warn!("`--deny-warnings` is being deprecated in favor of `--deny warnings`.");
245 } else if let Some(deny) = self.deny {
246 dict.insert("deny".to_string(), figment::value::Value::serialize(deny)?);
247 }
248
249 if self.via_ir {
250 dict.insert("via_ir".to_string(), true.into());
251 }
252
253 if self.use_literal_content {
254 dict.insert("use_literal_content".to_string(), true.into());
255 }
256
257 if self.no_metadata {
258 dict.insert("bytecode_hash".to_string(), "none".into());
259 dict.insert("cbor_metadata".to_string(), false.into());
260 }
261
262 if self.force {
263 dict.insert("force".to_string(), self.force.into());
264 }
265
266 if self.no_cache {
268 dict.insert("cache".to_string(), false.into());
269 }
270
271 if self.dynamic_test_linking {
272 dict.insert("dynamic_test_linking".to_string(), true.into());
273 }
274
275 if self.build_info {
276 dict.insert("build_info".to_string(), self.build_info.into());
277 }
278
279 if self.compiler.ast {
280 dict.insert("ast".to_string(), true.into());
281 }
282
283 if let Some(optimize) = self.compiler.optimize {
284 dict.insert("optimizer".to_string(), optimize.into());
285 }
286
287 if !self.compiler.extra_output.is_empty() {
288 let selection: Vec<_> =
289 self.compiler.extra_output.iter().map(|s| s.to_string()).collect();
290 dict.insert("extra_output".to_string(), selection.into());
291 }
292
293 if !self.compiler.extra_output_files.is_empty() {
294 let selection: Vec<_> =
295 self.compiler.extra_output_files.iter().map(|s| s.to_string()).collect();
296 dict.insert("extra_output_files".to_string(), selection.into());
297 }
298
299 if let Some(ref revert) = self.revert_strings {
300 dict.insert("revert_strings".to_string(), revert.to_string().into());
301 }
302
303 Ok(Map::from([(Config::selected_profile(), dict)]))
304 }
305}