Solidity存儲庫的一個構建目標是solc
,solidity命令行編譯器。 使用solc --help
爲您提供全部選項的解釋。 編譯器能夠生成各類輸出,範圍從簡單的二進制文件和彙編到抽象語法樹(解析樹),以估計gas使用狀況。 若是您只想編譯單個文件,則將其做爲solc --bin sourceFile.sol
運行,並打印二進制文件。 在部署合同以前,在編譯時使用solc --optimize --bin sourceFile.sol
來激活優化器。 若是你想得到solc
的一些更高級的輸出變體,最好告訴它使用solc -o outputDirectory --bin --ast --asm sourceFile.sol
輸出全部東西來分離文件。git
命令行編譯器會自動從文件系統中讀取導入的文件,但也能夠按照如下方式使用prefix = path
來提供路徑重定向:github
solc github.com/ethereum/dapp-bin/=/usr/local/lib/dapp-bin/ =/usr/local/lib/fallback file.sol
這本質上指示編譯器搜索以/usr/local/lib/dapp-bin
下的github.com/ethereum/dapp-bin/
開頭的任何內容,若是它沒有在那裏找到該文件,它將查看/usr/local/lib/fallback
(空的前綴老是匹配)。 solc不會讀取文件系統中位於重映射目標以外和顯式指定的源文件所在目錄以外的文件,所以import「/etc/ passwd
」; 只有在添加= /
做爲從新映射時纔有效。express
若是因爲重映射而存在多個匹配,則選擇具備最長公共前綴的那個匹配。json
出於安全緣由,編譯器限制了它能夠訪問的目錄。 在命令行中指定的源文件的路徑(及其子目錄)和經過重映射定義的路徑可用於導入語句,但其餘全部內容都被拒絕。 能夠經過--allow-paths /sample/path,/another/sample/path
開關容許其餘路徑(及其子目錄)。安全
若是您的合約使用庫,您會注意到該字節碼包含__LibraryName______
形式的子字符串。 您可使用solc
做爲連接器,這意味着它將在這些位置爲您插入庫地址:app
或者添加--libraries "Math:0x12345678901234567890 Heap:0xabcdef0123456"
到您的命令中,爲每一個庫提供一個地址或將該字符串存儲在一個文件中(每行一個庫),並使用--libraries fileName
運行solc
。less
若是使用選項--link
調用solc
,則全部輸入文件被解釋爲以上給出的__LibraryName ____
格式的非連接二進制文件(十六進制編碼),並就地連接(若是從stdin讀取輸入,則將其寫入 到標準輸出)。 在這種狀況下,除了--libraries
以外的全部選項都被忽略(包括-o
)。優化
若是使用選項--standard-json
調用solc
,則它將在標準輸入上指望JSON輸入(以下所述),並在標準輸出上返回JSON輸出。ui
編譯器輸入輸出JSON描述
這些JSON格式由編譯器API使用,也能夠經過solc
使用。 這些可能會發生變化,有些字段是可選的(如上所述),但其目的僅在於進行向後兼容的更改。this
編譯器API須要JSON格式的輸入,並以JSON格式的輸出輸出編譯結果。
評論固然是不容許的,這裏僅用於解釋目的。
輸入描述
{ // Required: Source code language, such as "Solidity", "serpent", "lll", "assembly", etc. language: "Solidity", // Required sources: { // 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 // It is used to verify the retrieved content if imported via URLs. "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...", "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); } }" } }, // Optional settings: { // Optional: Sorted list of remappings remappings: [ ":g/dir" ], // Optional: Optimizer settings (enabled defaults to false) optimizer: { enabled: true, runs: 500 }, // Metadata settings (optional) metadata: { // Use only literal content and not URLs (false by default) useLiteralContent: true }, // 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..." } } // 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 // ast - AST of all source files // legacyAST - legacy AST of all source files // devdoc - Developer documentation (natspec) // userdoc - User documentation (natspec) // metadata - Metadata // ir - New assembly format before desugaring // evm.assembly - New assembly format after desugaring // evm.legacyAssembly - Old-style assembly format in JSON // 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) // 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) // // Note that using a using `evm`, `evm.bytecode`, `ewasm`, etc. will select every // target part of that output. Additionally, `*` can be used as a wildcard to request everything. // outputSelection: { // 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": { "MyContract": [ "abi", "evm.bytecode.opcodes" ] }, // Enable the source map output of every single contract. "*": { "*": [ "evm.bytecode.sourceMap" ] }, // Enable the legacy AST output of every single file. "*": { "": [ "legacyAST" ] } } } }
輸出描述
{ // Optional: not present if no errors/warnings were encountered errors: [ { // Optional: Location within the source file. sourceLocation: { file: "sourceFile.sol", start: 0, end: 100 ], // Mandatory: Error type, such as "TypeError", "InternalCompilerError", "Exception", etc. // See below for complete list of types. type: "TypeError", // Mandatory: Component where the error originated, such as "general", "ewasm", etc. component: "general", // Mandatory ("error" or "warning") severity: "error", // Mandatory message: "Invalid keyword" // Optional: the message formatted with source location formattedMessage: "sourceFile.sol:100: Invalid keyword" } ], // 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 ast: {}, // The legacy AST object legacyAST: {} } }, // This contains the contract-level outputs. It can be limited/filtered by the outputSelection settings. contracts: { "sourceFile.sol": { // If the language used has no contract names, this field should equal to an empty string. "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: [], // See the Metadata Output documentation (serialised JSON string) metadata: "{...}", // User documentation (natspec) userdoc: {}, // Developer documentation (natspec) devdoc: {}, // Intermediate representation (string) ir: "", // EVM-related outputs evm: { // Assembly (string) assembly: "", // Old-style assembly (object) legacyAssembly: {}, // Bytecode and related details. bytecode: { // The bytecode as a hex string. object: "00fe", // Opcodes list (string) opcodes: "", // 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. "Library1": [ { start: 0, length: 20 }, { start: 200, length: 20 } ] } } }, // The same layout as above. deployedBytecode: { }, // The list of function hashes methodIdentifiers: { "delegate(address)": "5c19a95c" }, // Function gas estimates gasEstimates: { creation: { codeDepositCost: "420000", executionCost: "infinite", totalCost: "infinite" }, external: { "delegate(address)": "25000" }, internal: { "heavyLifting()": "infinite" } } }, // eWASM related outputs ewasm: { // S-expressions format wast: "", // Binary format (hex string) wasm: "" } } } } }
錯誤類型
JSONError
:JSON輸入不符合所需的格式,例如輸入不是JSON對象,不支持該語言等。IOError
:IO和導入處理錯誤,例如在所提供的源中沒法解析的URL或散列不匹配。ParserError
:源代碼不符合語言規則。DocstringParsingError
:沒法分析註釋塊中的NatSpec標籤。SyntaxError
:句法錯誤,例如continue在for循環以外使用。DeclarationError
:無效的,沒法解析的或衝突的標識符名稱。例如標識符未找到TypeError
:類型系統中的錯誤,例如無效類型轉換,無效賦值等。UnimplementedFeatureError
:編譯器不支持該功能,但預計將在將來的版本中受支持。InternalCompilerError
:在編譯器中觸發的內部錯誤 - 這應報告爲問題。Exception
:編譯期間未知的失敗 -這應該被報告爲一個問題。CompilerError
:編譯器堆棧的使用無效 - 這應報告爲問題。FatalError
:致命錯誤未正確處理 -應將此報告爲問題。Warning
:警告不會中止編譯,但應儘量解決。