-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcompile.rs
442 lines (385 loc) · 14.1 KB
/
compile.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! Support for compiling [foundry_compilers::Project]
use crate::{term::SpinnerReporter, TestFunctionExt};
use comfy_table::{presets::ASCII_MARKDOWN, Attribute, Cell, CellAlignment, Color, Table};
use eyre::Result;
use foundry_block_explorers::contract::Metadata;
use foundry_compilers::{
artifacts::{remappings::Remapping, BytecodeObject, Source},
compilers::{
solc::{Solc, SolcCompiler},
Compiler,
},
preprocessor::{TestOptimizerPreprocessor},
project::Preprocessor,
report::{BasicStdoutReporter, NoReporter, Report},
solc::SolcSettings,
Artifact, Project, ProjectBuilder, ProjectCompileOutput, ProjectPathsConfig, SolcConfig,
};
use num_format::{Locale, ToFormattedString};
use std::{
collections::BTreeMap,
fmt::Display,
io::IsTerminal,
path::{Path, PathBuf},
time::Instant,
};
/// Builder type to configure how to compile a project.
///
/// This is merely a wrapper for [`Project::compile()`] which also prints to stdout depending on its
/// settings.
#[must_use = "ProjectCompiler does nothing unless you call a `compile*` method"]
pub struct ProjectCompiler {
/// Whether we are going to verify the contracts after compilation.
verify: Option<bool>,
/// Whether to also print contract names.
print_names: Option<bool>,
/// Whether to also print contract sizes.
print_sizes: Option<bool>,
/// Whether to print anything at all. Overrides other `print` options.
quiet: Option<bool>,
/// Whether to bail on compiler errors.
bail: Option<bool>,
/// Extra files to include, that are not necessarily in the project's source dir.
files: Vec<PathBuf>,
}
impl Default for ProjectCompiler {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl ProjectCompiler {
/// Create a new builder with the default settings.
#[inline]
pub fn new() -> Self {
Self {
verify: None,
print_names: None,
print_sizes: None,
quiet: Some(crate::shell::verbosity().is_silent()),
bail: None,
files: Vec::new(),
}
}
/// Sets whether we are going to verify the contracts after compilation.
#[inline]
pub fn verify(mut self, yes: bool) -> Self {
self.verify = Some(yes);
self
}
/// Sets whether to print contract names.
#[inline]
pub fn print_names(mut self, yes: bool) -> Self {
self.print_names = Some(yes);
self
}
/// Sets whether to print contract sizes.
#[inline]
pub fn print_sizes(mut self, yes: bool) -> Self {
self.print_sizes = Some(yes);
self
}
/// Sets whether to print anything at all. Overrides other `print` options.
#[inline]
#[doc(alias = "silent")]
pub fn quiet(mut self, yes: bool) -> Self {
self.quiet = Some(yes);
self
}
/// Do not print anything at all if true. Overrides other `print` options.
#[inline]
pub fn quiet_if(mut self, maybe: bool) -> Self {
if maybe {
self.quiet = Some(true);
}
self
}
/// Sets whether to bail on compiler errors.
#[inline]
pub fn bail(mut self, yes: bool) -> Self {
self.bail = Some(yes);
self
}
/// Sets extra files to include, that are not necessarily in the project's source dir.
#[inline]
pub fn files(mut self, files: impl IntoIterator<Item = PathBuf>) -> Self {
self.files.extend(files);
self
}
/// Compiles the project.
pub fn compile<C: Compiler>(mut self, project: &Project<C>) -> Result<ProjectCompileOutput<C>>
where
TestOptimizerPreprocessor: Preprocessor<C>,
{
// TODO: Avoid process::exit
if !project.paths.has_input_files() && self.files.is_empty() {
println!("Nothing to compile");
// nothing to do here
std::process::exit(0);
}
// Taking is fine since we don't need these in `compile_with`.
let files = std::mem::take(&mut self.files);
self.compile_with(|| {
let sources = if !files.is_empty() {
Source::read_all(files)?
} else {
project.paths.read_input_files()?
};
foundry_compilers::project::ProjectCompiler::with_sources(project, sources)?
.with_preprocessor(TestOptimizerPreprocessor)
.compile()
.map_err(Into::into)
})
}
/// Compiles the project with the given closure
///
/// # Example
///
/// ```ignore
/// use foundry_common::compile::ProjectCompiler;
/// let config = foundry_config::Config::load();
/// let prj = config.project().unwrap();
/// ProjectCompiler::new().compile_with(|| Ok(prj.compile()?)).unwrap();
/// ```
#[instrument(target = "forge::compile", skip_all)]
fn compile_with<C: Compiler, F>(self, f: F) -> Result<ProjectCompileOutput<C>>
where
F: FnOnce() -> Result<ProjectCompileOutput<C>>,
{
let quiet = self.quiet.unwrap_or(false);
let bail = self.bail.unwrap_or(true);
let output = with_compilation_reporter(self.quiet.unwrap_or(false), || {
tracing::debug!("compiling project");
let timer = Instant::now();
let r = f();
let elapsed = timer.elapsed();
tracing::debug!("finished compiling in {:.3}s", elapsed.as_secs_f64());
r
})?;
if bail && output.has_compiler_errors() {
eyre::bail!("{output}")
}
if !quiet {
if output.is_unchanged() {
println!("No files changed, compilation skipped");
} else {
// print the compiler output / warnings
println!("{output}");
}
self.handle_output(&output);
}
Ok(output)
}
/// If configured, this will print sizes or names
fn handle_output<C: Compiler>(&self, output: &ProjectCompileOutput<C>) {
let print_names = self.print_names.unwrap_or(false);
let print_sizes = self.print_sizes.unwrap_or(false);
// print any sizes or names
if print_names {
let mut artifacts: BTreeMap<_, Vec<_>> = BTreeMap::new();
for (name, (_, version)) in output.versioned_artifacts() {
artifacts.entry(version).or_default().push(name);
}
for (version, names) in artifacts {
println!(
" compiler version: {}.{}.{}",
version.major, version.minor, version.patch
);
for name in names {
println!(" - {name}");
}
}
}
if print_sizes {
// add extra newline if names were already printed
if print_names {
println!();
}
let mut size_report = SizeReport { contracts: BTreeMap::new() };
let artifacts: BTreeMap<_, _> = output
.artifact_ids()
.filter(|(id, _)| {
// filter out forge-std specific contracts
!id.source.to_string_lossy().contains("/forge-std/src/")
})
.map(|(id, artifact)| (id.name, artifact))
.collect();
for (name, artifact) in artifacts {
let size = deployed_contract_size(artifact).unwrap_or_default();
let is_dev_contract = artifact
.abi
.as_ref()
.map(|abi| {
abi.functions().any(|f| {
f.test_function_kind().is_known() ||
matches!(f.name.as_str(), "IS_TEST" | "IS_SCRIPT")
})
})
.unwrap_or(false);
size_report.contracts.insert(name, ContractInfo { size, is_dev_contract });
}
println!("{size_report}");
// TODO: avoid process::exit
// exit with error if any contract exceeds the size limit, excluding test contracts.
if size_report.exceeds_size_limit() {
std::process::exit(1);
}
}
}
}
// https://eips.ethereum.org/EIPS/eip-170
const CONTRACT_SIZE_LIMIT: usize = 24576;
/// Contracts with info about their size
pub struct SizeReport {
/// `contract name -> info`
pub contracts: BTreeMap<String, ContractInfo>,
}
impl SizeReport {
/// Returns the size of the largest contract, excluding test contracts.
pub fn max_size(&self) -> usize {
let mut max_size = 0;
for contract in self.contracts.values() {
if !contract.is_dev_contract && contract.size > max_size {
max_size = contract.size;
}
}
max_size
}
/// Returns true if any contract exceeds the size limit, excluding test contracts.
pub fn exceeds_size_limit(&self) -> bool {
self.max_size() > CONTRACT_SIZE_LIMIT
}
}
impl Display for SizeReport {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
let mut table = Table::new();
table.load_preset(ASCII_MARKDOWN);
table.set_header([
Cell::new("Contract").add_attribute(Attribute::Bold).fg(Color::Blue),
Cell::new("Size (B)").add_attribute(Attribute::Bold).fg(Color::Blue),
Cell::new("Margin (B)").add_attribute(Attribute::Bold).fg(Color::Blue),
]);
// filters out non dev contracts (Test or Script)
let contracts = self.contracts.iter().filter(|(_, c)| !c.is_dev_contract && c.size > 0);
for (name, contract) in contracts {
let margin = CONTRACT_SIZE_LIMIT as isize - contract.size as isize;
let color = match contract.size {
0..=17999 => Color::Reset,
18000..=CONTRACT_SIZE_LIMIT => Color::Yellow,
_ => Color::Red,
};
let locale = &Locale::en;
table.add_row([
Cell::new(name).fg(color),
Cell::new(contract.size.to_formatted_string(locale))
.set_alignment(CellAlignment::Right)
.fg(color),
Cell::new(margin.to_formatted_string(locale))
.set_alignment(CellAlignment::Right)
.fg(color),
]);
}
writeln!(f, "{table}")?;
Ok(())
}
}
/// Returns the size of the deployed contract
pub fn deployed_contract_size<T: Artifact>(artifact: &T) -> Option<usize> {
let bytecode = artifact.get_deployed_bytecode_object()?;
let size = match bytecode.as_ref() {
BytecodeObject::Bytecode(bytes) => bytes.len(),
BytecodeObject::Unlinked(unlinked) => {
// we don't need to account for placeholders here, because library placeholders take up
// 40 characters: `__$<library hash>$__` which is the same as a 20byte address in hex.
let mut size = unlinked.as_bytes().len();
if unlinked.starts_with("0x") {
size -= 2;
}
// hex -> bytes
size / 2
}
};
Some(size)
}
/// How big the contract is and whether it is a dev contract where size limits can be neglected
#[derive(Clone, Copy, Debug)]
pub struct ContractInfo {
/// size of the contract in bytes
pub size: usize,
/// A development contract is either a Script or a Test contract.
pub is_dev_contract: bool,
}
/// Compiles target file path.
///
/// If `quiet` no solc related output will be emitted to stdout.
///
/// If `verify` and it's a standalone script, throw error. Only allowed for projects.
///
/// **Note:** this expects the `target_path` to be absolute
pub fn compile_target<C: Compiler>(
target_path: &Path,
project: &Project<C>,
quiet: bool,
) -> Result<ProjectCompileOutput<C>>
where
TestOptimizerPreprocessor: Preprocessor<C>,
{
ProjectCompiler::new().quiet(quiet).files([target_path.into()]).compile(project)
}
/// Creates a [Project] from an Etherscan source.
pub fn etherscan_project(
metadata: &Metadata,
target_path: impl AsRef<Path>,
) -> Result<Project<SolcCompiler>> {
let target_path = dunce::canonicalize(target_path.as_ref())?;
let sources_path = target_path.join(&metadata.contract_name);
metadata.source_tree().write_to(&target_path)?;
let mut settings = metadata.settings()?;
// make remappings absolute with our root
for remapping in settings.remappings.iter_mut() {
let new_path = sources_path.join(remapping.path.trim_start_matches('/'));
remapping.path = new_path.display().to_string();
}
// add missing remappings
if !settings.remappings.iter().any(|remapping| remapping.name.starts_with("@openzeppelin/")) {
let oz = Remapping {
context: None,
name: "@openzeppelin/".into(),
path: sources_path.join("@openzeppelin").display().to_string(),
};
settings.remappings.push(oz);
}
// root/
// ContractName/
// [source code]
let paths = ProjectPathsConfig::builder()
.sources(sources_path.clone())
.remappings(settings.remappings.clone())
.build_with_root(sources_path);
let v = metadata.compiler_version()?;
let solc = Solc::find_or_install(&v)?;
let compiler = SolcCompiler::Specific(solc);
Ok(ProjectBuilder::<SolcCompiler>::default()
.settings(SolcSettings {
settings: SolcConfig::builder().settings(settings).build().settings,
..Default::default()
})
.paths(paths)
.ephemeral()
.no_artifacts()
.build(compiler)?)
}
/// Configures the reporter and runs the given closure.
pub fn with_compilation_reporter<O>(quiet: bool, f: impl FnOnce() -> O) -> O {
#[allow(clippy::collapsible_else_if)]
let reporter = if quiet {
Report::new(NoReporter::default())
} else {
if std::io::stdout().is_terminal() {
Report::new(SpinnerReporter::spawn())
} else {
Report::new(BasicStdoutReporter::default())
}
};
foundry_compilers::report::with_scoped(&reporter, f)
}