2017-02-02 20:52:59 +00:00
***** ***** ***** ***
Using the compiler
***** ***** ***** ***
2016-06-10 15:25:51 +00:00
.. index :: ! commandline compiler, compiler;commandline, ! solc, ! linker
.. _commandline-compiler:
Using the Commandline Compiler
***** ***** ***** ***** ***** *****
2018-02-26 23:36:01 +00:00
.. note ::
2018-09-25 16:43:27 +00:00
This section does not apply to :ref: `solcjs <solcjs>` , not even if it is used in commandline mode.
2018-02-26 23:36:01 +00:00
2016-06-10 15:25:51 +00:00
One of the build targets of the Solidity repository is `` solc `` , the solidity commandline compiler.
Using `` solc --help `` provides you with an explanation of all options. The compiler can produce various outputs, ranging from simple binaries and assembly over an abstract syntax tree (parse tree) to estimations of gas usage.
2018-05-02 15:07:12 +00:00
If you only want to compile a single file, you run it as `` solc --bin sourceFile.sol `` and it will print the binary. If you want to get some of the more advanced output variants of `` solc `` , it is probably better to tell it to output everything to separate files using `` solc -o outputDirectory --bin --ast --asm sourceFile.sol `` .
2018-09-25 16:43:27 +00:00
Before you deploy your contract, activate the optimizer when compiling using `` solc --optimize --bin sourceFile.sol `` .
By default, the optimizer will optimize the contract assuming it is called 200 times across its lifetime.
If you want the initial contract deployment to be cheaper and the later function executions to be more expensive,
set it to `` --runs=1 `` . If you expect many transactions and do not care for higher deployment cost and
output size, set `` --runs `` to a high number.
2016-06-10 15:25:51 +00:00
The commandline compiler will automatically read imported files from the filesystem, but
it is also possible to provide path redirects using `` prefix=path `` in the following way:
::
2018-08-09 18:46:22 +00:00
solc github.com/ethereum/dapp-bin/=/usr/local/lib/dapp-bin/ file.sol
2016-06-10 15:25:51 +00:00
This essentially instructs the compiler to search for anything starting with
2018-08-09 18:46:22 +00:00
`` github.com/ethereum/dapp-bin/ `` under `` /usr/local/lib/dapp-bin `` .
`` solc `` will not read files from the filesystem that lie outside of
2016-06-10 15:25:51 +00:00
the remapping targets and outside of the directories where explicitly specified source
2018-08-09 18:46:22 +00:00
files reside, so things like `` import "/etc/passwd"; `` only work if you add `` /=/ `` as a remapping.
An empty remapping prefix is not allowed.
2016-06-10 15:25:51 +00:00
If there are multiple matches due to remappings, the one with the longest common prefix is selected.
2017-04-19 15:59:03 +00:00
For security reasons the compiler has restrictions what directories it can access. Paths (and their subdirectories) of source files specified on the commandline and paths defined by remappings are allowed for import statements, but everything else is rejected. Additional paths (and their subdirectories) can be allowed via the `` --allow-paths /sample/path,/another/sample/path `` switch.
2018-10-08 13:08:12 +00:00
If your contracts use :ref: `libraries <libraries>` , you will notice that the bytecode contains substrings of the form `` __$53aea86b7d70b31448b230b20ae141a537$__ `` . These are placeholders for the actual library addresses.
The placeholder is a 34 character prefix of the hex encoding of the keccak256 hash of the fully qualified library name.
The bytecode file will also contain lines of the form `` // <placeholder> -> <fq library name> `` at the end to help
identify which libraries the placeholders represent. Note that the fully qualified library name
is the path of its source file and the library name separated by `` : `` .
2018-10-08 10:18:56 +00:00
You can use `` solc `` as a linker meaning that it will insert the library addresses for you at those points:
2016-06-10 15:25:51 +00:00
2018-10-08 10:18:56 +00:00
Either add `` --libraries "file.sol:Math:0x1234567890123456789012345678901234567890 file.sol:Heap:0xabCD567890123456789012345678901234567890" `` to your command to provide an address for each library or store the string in a file (one library per line) and run `` solc `` using `` --libraries fileName `` .
2016-06-10 15:25:51 +00:00
2018-10-08 13:08:12 +00:00
If `` solc `` is called with the option `` --link `` , all input files are interpreted to be unlinked binaries (hex-encoded) in the `` __$53aea86b7d70b31448b230b20ae141a537$__ `` -format given above and are linked in-place (if the input is read from stdin, it is written to stdout). All options except `` --libraries `` are ignored (including `` -o `` ) in this case.
2016-06-10 15:25:51 +00:00
2018-09-25 16:43:27 +00:00
If `` solc `` is called with the option `` --standard-json `` , it will expect a JSON input (as explained below) on the standard input, and return a JSON output on the standard output. This is the recommended interface for more complex and especially automated uses.
2017-04-24 10:25:24 +00:00
2018-10-08 10:18:56 +00:00
.. note ::
The library placeholder used to be the fully qualified name of the library itself
instead of the hash of it. This format is still supported by `` solc --link `` but
the compiler will no longer output it. This change was made to reduce
the likelihood of a collision between libraries, since only the first 36 characters
of the fully qualified library name could be used.
2018-08-13 14:46:46 +00:00
.. _evm-version:
.. index :: ! EVM version, compile target
Setting the EVM version to target
***** ***** ***** ***** ***** ***** ***
When you compile your contract code you can specify the Ethereum virtual machine
version to compile for to avoid particular features or behaviours.
.. warning ::
Compiling for the wrong EVM version can result in wrong, strange and failing
behaviour. Please ensure, especially if running a private chain, that you
use matching EVM versions.
2018-09-25 16:43:27 +00:00
On the command line, you can select the EVM version as follows:
2018-08-13 14:46:46 +00:00
.. code-block :: shell
solc --evm-version <VERSION> contract.sol
2018-09-25 16:43:27 +00:00
In the :ref: `standard JSON interface <compiler-api>` , use the `` "evmVersion" ``
key in the `` "settings" `` field:
2018-08-13 14:46:46 +00:00
2018-09-25 16:43:27 +00:00
.. code-block :: none
2018-08-13 14:46:46 +00:00
{
2018-09-25 16:43:27 +00:00
"sources": { ... },
"settings": {
"optimizer": { ... },
"evmVersion": "<VERSION>"
}
2018-08-13 14:46:46 +00:00
}
Target options
--------------
Below is a list of target EVM versions and the compiler-relevant changes introduced
at each version. Backward compatibility is not guaranteed between each version.
2018-09-25 16:43:27 +00:00
- `` homestead `` (oldest version)
2018-08-13 14:46:46 +00:00
- `` tangerineWhistle ``
- gas cost for access to other accounts increased, relevant for gas estimation and the optimizer.
- all gas sent by default for external calls, previously a certain amount had to be retained.
- `` spuriousDragon ``
- gas cost for the `` exp `` opcode increased, relevant for gas estimation and the optimizer.
- `` byzantium `` (**default** )
- opcodes `` returndatacopy `` , `` returndatasize `` and `` staticcall `` are available in assembly.
2018-09-25 16:43:27 +00:00
- the `` staticcall `` opcode is used when calling non-library view or pure functions, which prevents the functions from modifying state at the EVM level, i.e., even applies when you use invalid type conversions.
2018-08-13 14:46:46 +00:00
- it is possible to access dynamic data returned from function calls.
- `` revert `` opcode introduced, which means that `` revert() `` will not waste gas.
- `` constantinople `` (still in progress)
- opcodes `` shl `` , `` shr `` and `` sar `` are available in assembly.
- shifting operators use shifting opcodes and thus need less gas.
2017-02-02 20:52:59 +00:00
.. _compiler-api:
2016-06-10 15:25:51 +00:00
2017-02-02 20:52:59 +00:00
Compiler Input and Output JSON Description
***** ***** ***** ***** ***** ***** ***** ***** **
2018-09-25 16:43:27 +00:00
The recommended way to interface with the Solidity compiler especially for
more complex and automated setups is the so-called JSON-input-output interface.
The same interface is provided by all distributions of the compiler.
The fields are generally subject to change,
some are optional (as noted), but we try to only make backwards compatible changes.
2017-02-02 20:52:59 +00:00
2017-02-08 19:22:40 +00:00
The compiler API expects a JSON formatted input and outputs the compilation result in a JSON formatted output.
2018-09-25 16:43:27 +00:00
The following subsections describe the format through an example.
2016-06-10 15:25:51 +00:00
Comments are of course not permitted and used here only for explanatory purposes.
Input Description
-----------------
2016-12-02 13:46:56 +00:00
.. code-block :: none
2016-06-10 15:25:51 +00:00
{
2018-09-25 16:43:27 +00:00
// Required: Source code language, such as "Solidity", "Vyper", "lll", "assembly", etc.
2017-02-08 19:00:07 +00:00
language: "Solidity",
2017-02-02 13:54:21 +00:00
// Required
2016-06-10 15:25:51 +00:00
sources:
{
2017-02-02 13:54:21 +00:00
// The keys here are the "global" names of the source files,
// imports can use other files via remappings (see below).
"myFile.sol":
{
// Optional: keccak256 hash of the source file
2017-02-02 21:10:19 +00:00
// It is used to verify the retrieved content if imported via URLs.
2017-02-02 13:54:21 +00:00
"keccak256": "0x123...",
// Required (unless "content" is used, see below): URL(s) to the source file.
// URL(s) should be imported in this order and the result checked against the
// keccak256 hash (if available). If the hash doesn't match or none of the
// URL(s) result in success, an error should be raised.
"urls":
[
"bzzr://56ab...",
"ipfs://Qma...",
2018-09-03 13:11:35 +00:00
// If files are used, their directories should be added to the command line via
// `--allow-paths <path>` .
2017-02-02 13:54:21 +00:00
"file:///tmp/path/to/file.sol"
]
},
"mortal":
{
// Optional: keccak256 hash of the source file
"keccak256": "0x234...",
// Required (unless "urls" is used): literal contents of the source file
"content": "contract mortal is owned { function kill() { if (msg.sender == owner) selfdestruct(owner); } }"
}
2016-06-10 15:25:51 +00:00
},
2017-02-02 13:58:30 +00:00
// Optional
2016-06-10 15:25:51 +00:00
settings:
{
2017-02-02 13:58:30 +00:00
// Optional: Sorted list of remappings
remappings: [ ":g/dir" ],
2018-05-02 15:07:12 +00:00
// Optional: Optimizer settings
2017-02-02 13:58:30 +00:00
optimizer: {
2018-05-02 15:07:12 +00:00
// disabled by default
2017-02-02 13:58:30 +00:00
enabled: true,
2018-05-02 15:07:12 +00:00
// Optimize for how many times you intend to run the code.
// Lower values will optimize more for initial deployment cost, higher values will optimize more for high-frequency usage.
runs: 200
2017-02-02 13:58:30 +00:00
},
2018-02-27 17:53:51 +00:00
evmVersion: "byzantium", // Version of the EVM to compile for. Affects type checking and code generation. Can be homestead, tangerineWhistle, spuriousDragon, byzantium or constantinople
2017-02-02 15:00:01 +00:00
// Metadata settings (optional)
metadata: {
// Use only literal content and not URLs (false by default)
useLiteralContent: true
},
2017-02-02 13:58:30 +00:00
// Addresses of the libraries. If not all libraries are given here, it can result in unlinked objects whose output data is different.
libraries: {
// The top level key is the the name of the source file where the library is used.
// If remappings are used, this source file should match the global path after remappings were applied.
// If this key is an empty string, that refers to a global level.
"myFile.sol": {
"MyLib": "0x123123..."
}
}
2017-02-02 14:57:07 +00:00
// The following can be used to select desired outputs.
// If this field is omitted, then the compiler loads and does type checking, but will not generate any outputs apart from errors.
// The first level key is the file name and the second is the contract name, where empty contract name refers to the file itself,
// while the star refers to all of the contracts.
//
// The available output types are as follows:
// abi - ABI
2017-05-22 13:47:09 +00:00
// ast - AST of all source files
2017-04-19 17:53:39 +00:00
// legacyAST - legacy AST of all source files
2017-02-02 14:57:07 +00:00
// devdoc - Developer documentation (natspec)
// userdoc - User documentation (natspec)
// metadata - Metadata
2017-04-07 13:48:35 +00:00
// ir - New assembly format before desugaring
2017-02-08 19:16:23 +00:00
// evm.assembly - New assembly format after desugaring
2017-04-07 13:59:13 +00:00
// evm.legacyAssembly - Old-style assembly format in JSON
2017-03-29 21:30:00 +00:00
// evm.bytecode.object - Bytecode object
// evm.bytecode.opcodes - Opcodes list
// evm.bytecode.sourceMap - Source mapping (useful for debugging)
// evm.bytecode.linkReferences - Link references (if unlinked object)
// evm.deployedBytecode* - Deployed bytecode (has the same options as evm.bytecode)
2017-02-02 14:57:07 +00:00
// evm.methodIdentifiers - The list of function hashes
// evm.gasEstimates - Function gas estimates
// ewasm.wast - eWASM S-expressions format (not supported atm)
// ewasm.wasm - eWASM binary format (not supported atm)
2017-04-07 14:34:38 +00:00
//
// Note that using a using `evm` , `evm.bytecode` , `ewasm` , etc. will select every
2017-10-19 13:24:33 +00:00
// target part of that output. Additionally, `*` can be used as a wildcard to request everything.
2017-04-07 14:34:38 +00:00
//
2016-06-10 15:25:51 +00:00
outputSelection: {
2017-02-02 14:57:07 +00:00
// Enable the metadata and bytecode outputs of every single contract.
"*": {
"*": [ "metadata", "evm.bytecode" ]
},
// Enable the abi and opcodes output of MyContract defined in file def.
"def": {
2018-01-21 08:35:59 +00:00
"MyContract": [ "abi", "evm.bytecode.opcodes" ]
2017-02-02 14:57:07 +00:00
},
// Enable the source map output of every single contract.
"*": {
2018-01-21 08:35:59 +00:00
"*": [ "evm.bytecode.sourceMap" ]
2017-02-02 14:57:07 +00:00
},
2017-05-02 13:49:13 +00:00
// Enable the legacy AST output of every single file.
2017-02-02 14:57:07 +00:00
"*": {
2017-05-02 13:49:13 +00:00
"": [ "legacyAST" ]
2017-02-02 14:57:07 +00:00
}
2016-06-10 15:25:51 +00:00
}
}
}
2016-11-16 13:05:24 +00:00
2017-02-02 20:52:59 +00:00
Output Description
------------------
2016-11-16 13:05:24 +00:00
2016-12-02 13:46:56 +00:00
.. code-block :: none
2016-11-16 13:05:24 +00:00
{
2017-02-02 21:18:09 +00:00
// Optional: not present if no errors/warnings were encountered
2016-11-16 13:05:24 +00:00
errors: [
2017-02-02 21:18:09 +00:00
{
2017-02-02 21:35:07 +00:00
// Optional: Location within the source file.
2017-02-06 16:55:56 +00:00
sourceLocation: {
file: "sourceFile.sol",
start: 0,
end: 100
2017-02-02 21:35:07 +00:00
],
2017-10-20 04:51:13 +00:00
// Mandatory: Error type, such as "TypeError", "InternalCompilerError", "Exception", etc.
// See below for complete list of types.
2017-02-02 21:18:09 +00:00
type: "TypeError",
2017-05-02 13:49:13 +00:00
// Mandatory: Component where the error originated, such as "general", "ewasm", etc.
2017-02-02 21:18:09 +00:00
component: "general",
// Mandatory ("error" or "warning")
severity: "error",
// Mandatory
message: "Invalid keyword"
2017-04-07 14:37:11 +00:00
// Optional: the message formatted with source location
formattedMessage: "sourceFile.sol:100: Invalid keyword"
2017-02-02 21:18:09 +00:00
}
],
2017-02-02 22:06:10 +00:00
// This contains the file-level outputs. In can be limited/filtered by the outputSelection settings.
sources: {
"sourceFile.sol": {
// Identifier (used in source maps)
id: 1,
// The AST object
2017-04-19 17:53:39 +00:00
ast: {},
// The legacy AST object
legacyAST: {}
2017-02-02 22:06:10 +00:00
}
},
2017-02-02 21:27:12 +00:00
// This contains the contract-level outputs. It can be limited/filtered by the outputSelection settings.
2016-11-16 13:05:24 +00:00
contracts: {
2017-02-02 21:27:12 +00:00
"sourceFile.sol": {
2017-02-08 19:00:07 +00:00
// If the language used has no contract names, this field should equal to an empty string.
2017-02-02 21:27:12 +00:00
"ContractName": {
// The Ethereum Contract ABI. If empty, it is represented as an empty array.
// See https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
abi: [],
2017-03-29 21:33:14 +00:00
// See the Metadata Output documentation (serialised JSON string)
metadata: "{...}",
2017-03-29 21:23:35 +00:00
// User documentation (natspec)
userdoc: {},
// Developer documentation (natspec)
devdoc: {},
2017-04-07 13:48:35 +00:00
// Intermediate representation (string)
ir: "",
2017-03-29 21:23:35 +00:00
// EVM-related outputs
2017-02-02 21:27:12 +00:00
evm: {
2017-02-02 21:52:35 +00:00
// Assembly (string)
assembly: "",
2017-04-07 13:59:13 +00:00
// Old-style assembly (object)
2017-05-02 14:20:27 +00:00
legacyAssembly: {},
2017-02-02 21:33:46 +00:00
// Bytecode and related details.
bytecode: {
// The bytecode as a hex string.
object: "00fe",
2017-03-29 21:21:33 +00:00
// Opcodes list (string)
opcodes: "",
2017-02-02 21:33:46 +00:00
// The source mapping as a string. See the source mapping definition.
sourceMap: "",
// If given, this is an unlinked object.
linkReferences: {
"libraryFile.sol": {
// Byte offsets into the bytecode. Linking replaces the 20 bytes located there.
2017-02-02 21:35:57 +00:00
"Library1": [
{ start: 0, length: 20 },
{ start: 200, length: 20 }
]
2017-02-02 21:33:46 +00:00
}
}
2017-03-29 21:21:33 +00:00
},
2017-02-02 21:33:46 +00:00
// The same layout as above.
deployedBytecode: { },
2017-02-02 21:52:35 +00:00
// The list of function hashes
methodIdentifiers: {
2017-03-29 21:25:05 +00:00
"delegate(address)": "5c19a95c"
2017-02-02 21:52:35 +00:00
},
// Function gas estimates
gasEstimates: {
creation: {
2017-04-27 12:13:49 +00:00
codeDepositCost: "420000",
executionCost: "infinite",
totalCost: "infinite"
2017-02-02 21:52:35 +00:00
},
external: {
2017-04-27 12:13:49 +00:00
"delegate(address)": "25000"
2017-02-02 21:52:35 +00:00
},
internal: {
2017-04-27 12:13:49 +00:00
"heavyLifting()": "infinite"
2017-02-02 21:52:35 +00:00
}
}
2017-02-02 21:27:12 +00:00
},
2017-03-29 21:23:35 +00:00
// eWASM related outputs
2017-02-02 21:27:12 +00:00
ewasm: {
2017-02-02 21:52:35 +00:00
// S-expressions format
wast: "",
// Binary format (hex string)
wasm: ""
2017-03-29 21:23:35 +00:00
}
2017-02-02 21:27:12 +00:00
}
2016-11-16 13:05:24 +00:00
}
2017-05-02 13:49:13 +00:00
}
2016-11-16 13:05:24 +00:00
}
2017-10-20 04:51:13 +00:00
Error types
~~~~~~~~~~~
1. `` JSONError `` : JSON input doesn't conform to the required format, e.g. input is not a JSON object, the language is not supported, etc.
2. `` IOError `` : IO and import processing errors, such as unresolvable URL or hash mismatch in supplied sources.
3. `` ParserError `` : Source code doesn't conform to the language rules.
4. `` DocstringParsingError `` : The NatSpec tags in the comment block cannot be parsed.
5. `` SyntaxError `` : Syntactical error, such as `` continue `` is used outside of a `` for `` loop.
6. `` DeclarationError `` : Invalid, unresolvable or clashing identifier names. e.g. `` Identifier not found ``
7. `` TypeError `` : Error within the type system, such as invalid type conversions, invalid assignments, etc.
8. `` UnimplementedFeatureError `` : Feature is not supported by the compiler, but is expected to be supported in future versions.
9. `` InternalCompilerError `` : Internal bug triggered in the compiler - this should be reported as an issue.
10. `` Exception `` : Unknown failure during compilation - this should be reported as an issue.
11. `` CompilerError `` : Invalid use of the compiler stack - this should be reported as an issue.
12. `` FatalError `` : Fatal error not processed correctly - this should be reported as an issue.
13. `` Warning `` : A warning, which didn't stop the compilation, but should be addressed if possible.