forge-build - Build the project's smart contracts.
forge build
[options]
Build the project's smart contracts.
The command will try to detect the latest version that can compile your project by looking at the version requirements of all your contracts and dependencies.
You can override this behaviour by passing --no-auto-detect
. Alternatively, you can pass --use <SOLC_VERSION>
.
If the command detects that the Solidity compiler version it is using to build is not installed,
it will download it and install it in ~/.svm
. You can disable this behavior by passing --offline
.
The build is incremental, and the build cache is saved in cache/
in the project root by default. If you
want to clear the cache, pass --force
, and if you want to change the cache directory, pass --cache-path <PATH>
.
There are three build modes:
- Just compilation (default): Builds the project and saves the contract artifacts in
out/
(or the path specified by--out <PATH>
). - Size mode (
--sizes
): Builds the project, displays the size of non-test contracts and exits with code 1 if any of them are above the size limit. - Name mode (
--names
): Builds the project, displays the names of the contracts and exits.
You can enable the optimizer by passing --optimize
, and you can adjust the number of optimizer runs by passing --optimizer-runs <RUNS>
.
You can also opt-in to the Solidity IR compilation pipeline by passing --via-ir
. Read more about the IR pipeline in the Solidity docs.
By default, the optimizer is enabled and runs for 200 cycles.
You can add extra output from the Solidity compiler to your artifacts by passing --extra-output <SELECTOR>
.
The selector is a path in the Solidity compiler output, which you can read more about in the Solidity docs.
You can also write some of the compiler output to separate files by passing --extra-output-files <SELECTOR>
.
Valid selectors for --extra-output-files
are:
metadata
: Written as ametadata.json
file in the artifacts directoryir
: Written as a.ir
file in the artifacts directoryirOptimized
: Written as a.iropt
file in the artifacts directoryewasm
: Written as a.ewasm
file in the artifacts directoryevm.assembly
: Written as a.asm
file in the artifacts directory
The command can be run in watch mode by passing --watch [PATH...]
, which will rebuild every time a
watched file or directory is changed. The source directory is watched by default.
Sparse mode only compiles files that match certain criteria.
By default, this filter applies to files that have not been changed since the last build, but for commands that take file filters (e.g. forge test), sparse mode will only recompile files that match the filter.
Sparse mode is opt-in until the feature is stabilized. To opt-in to sparse mode and try it out, set sparse_mode
in your configuration file.
--names
Print compiled contract names.
--sizes
Print compiled non-test contract sizes, exiting with code 1 if any of them are above the size limit.
{{#include core-build-options.md}}
{{#include watch-options.md}}
{{#include common-options.md}}
-
Build the project:
forge build
-
Build the project with solc 0.6.0:
forge build --use solc:0.6.0
-
Build the project with additional artifact output:
forge build --extra-output evm.assembly
-
Build the project in watch mode:
forge build --watch