solidity.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package compiler wraps the Solidity and Vyper compiler executables (solc; vyper).
  17. package compiler
  18. import (
  19. "bytes"
  20. "encoding/json"
  21. "errors"
  22. "fmt"
  23. "os/exec"
  24. "strconv"
  25. "strings"
  26. )
  27. // Solidity contains information about the solidity compiler.
  28. type Solidity struct {
  29. Path, Version, FullVersion string
  30. Major, Minor, Patch int
  31. }
  32. // --combined-output format
  33. type solcOutput struct {
  34. Contracts map[string]struct {
  35. BinRuntime string `json:"bin-runtime"`
  36. SrcMapRuntime string `json:"srcmap-runtime"`
  37. Bin, SrcMap, Abi, Devdoc, Userdoc, Metadata string
  38. Hashes map[string]string
  39. }
  40. Version string
  41. }
  42. func (s *Solidity) makeArgs() []string {
  43. p := []string{
  44. "--combined-json", "bin,bin-runtime,srcmap,srcmap-runtime,abi,userdoc,devdoc",
  45. "--optimize", // code optimizer switched on
  46. }
  47. if s.Major > 0 || s.Minor > 4 || s.Patch > 6 {
  48. p[1] += ",metadata,hashes"
  49. }
  50. return p
  51. }
  52. // SolidityVersion runs solc and parses its version output.
  53. func SolidityVersion(solc string) (*Solidity, error) {
  54. if solc == "" {
  55. solc = "solc"
  56. }
  57. var out bytes.Buffer
  58. cmd := exec.Command(solc, "--version")
  59. cmd.Stdout = &out
  60. err := cmd.Run()
  61. if err != nil {
  62. return nil, err
  63. }
  64. matches := versionRegexp.FindStringSubmatch(out.String())
  65. if len(matches) != 4 {
  66. return nil, fmt.Errorf("can't parse solc version %q", out.String())
  67. }
  68. s := &Solidity{Path: cmd.Path, FullVersion: out.String(), Version: matches[0]}
  69. if s.Major, err = strconv.Atoi(matches[1]); err != nil {
  70. return nil, err
  71. }
  72. if s.Minor, err = strconv.Atoi(matches[2]); err != nil {
  73. return nil, err
  74. }
  75. if s.Patch, err = strconv.Atoi(matches[3]); err != nil {
  76. return nil, err
  77. }
  78. return s, nil
  79. }
  80. // CompileSolidityString builds and returns all the contracts contained within a source string.
  81. func CompileSolidityString(solc, source string) (map[string]*Contract, error) {
  82. if len(source) == 0 {
  83. return nil, errors.New("solc: empty source string")
  84. }
  85. s, err := SolidityVersion(solc)
  86. if err != nil {
  87. return nil, err
  88. }
  89. args := append(s.makeArgs(), "--")
  90. cmd := exec.Command(s.Path, append(args, "-")...)
  91. cmd.Stdin = strings.NewReader(source)
  92. return s.run(cmd, source)
  93. }
  94. // CompileSolidity compiles all given Solidity source files.
  95. func CompileSolidity(solc string, sourcefiles ...string) (map[string]*Contract, error) {
  96. if len(sourcefiles) == 0 {
  97. return nil, errors.New("solc: no source files")
  98. }
  99. source, err := slurpFiles(sourcefiles)
  100. if err != nil {
  101. return nil, err
  102. }
  103. s, err := SolidityVersion(solc)
  104. if err != nil {
  105. return nil, err
  106. }
  107. args := append(s.makeArgs(), "--")
  108. cmd := exec.Command(s.Path, append(args, sourcefiles...)...)
  109. return s.run(cmd, source)
  110. }
  111. func (s *Solidity) run(cmd *exec.Cmd, source string) (map[string]*Contract, error) {
  112. var stderr, stdout bytes.Buffer
  113. cmd.Stderr = &stderr
  114. cmd.Stdout = &stdout
  115. if err := cmd.Run(); err != nil {
  116. return nil, fmt.Errorf("solc: %v\n%s", err, stderr.Bytes())
  117. }
  118. return ParseCombinedJSON(stdout.Bytes(), source, s.Version, s.Version, strings.Join(s.makeArgs(), " "))
  119. }
  120. // ParseCombinedJSON takes the direct output of a solc --combined-output run and
  121. // parses it into a map of string contract name to Contract structs. The
  122. // provided source, language and compiler version, and compiler options are all
  123. // passed through into the Contract structs.
  124. //
  125. // The solc output is expected to contain ABI, source mapping, user docs, and dev docs.
  126. //
  127. // Returns an error if the JSON is malformed or missing data, or if the JSON
  128. // embedded within the JSON is malformed.
  129. func ParseCombinedJSON(combinedJSON []byte, source string, languageVersion string, compilerVersion string, compilerOptions string) (map[string]*Contract, error) {
  130. var output solcOutput
  131. if err := json.Unmarshal(combinedJSON, &output); err != nil {
  132. return nil, err
  133. }
  134. // Compilation succeeded, assemble and return the contracts.
  135. contracts := make(map[string]*Contract)
  136. for name, info := range output.Contracts {
  137. // Parse the individual compilation results.
  138. var abi interface{}
  139. if err := json.Unmarshal([]byte(info.Abi), &abi); err != nil {
  140. return nil, fmt.Errorf("solc: error reading abi definition (%v)", err)
  141. }
  142. var userdoc, devdoc interface{}
  143. json.Unmarshal([]byte(info.Userdoc), &userdoc)
  144. json.Unmarshal([]byte(info.Devdoc), &devdoc)
  145. contracts[name] = &Contract{
  146. Code: "0x" + info.Bin,
  147. RuntimeCode: "0x" + info.BinRuntime,
  148. Hashes: info.Hashes,
  149. Info: ContractInfo{
  150. Source: source,
  151. Language: "Solidity",
  152. LanguageVersion: languageVersion,
  153. CompilerVersion: compilerVersion,
  154. CompilerOptions: compilerOptions,
  155. SrcMap: info.SrcMap,
  156. SrcMapRuntime: info.SrcMapRuntime,
  157. AbiDefinition: abi,
  158. UserDoc: userdoc,
  159. DeveloperDoc: devdoc,
  160. Metadata: info.Metadata,
  161. },
  162. }
  163. }
  164. return contracts, nil
  165. }