vyper.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2019 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. // Vyper contains information about the vyper compiler.
  28. type Vyper struct {
  29. Path, Version, FullVersion string
  30. Major, Minor, Patch int
  31. }
  32. func (s *Vyper) makeArgs() []string {
  33. p := []string{
  34. "-f", "combined_json",
  35. }
  36. return p
  37. }
  38. // VyperVersion runs vyper and parses its version output.
  39. func VyperVersion(vyper string) (*Vyper, error) {
  40. if vyper == "" {
  41. vyper = "vyper"
  42. }
  43. var out bytes.Buffer
  44. cmd := exec.Command(vyper, "--version")
  45. cmd.Stdout = &out
  46. err := cmd.Run()
  47. if err != nil {
  48. return nil, err
  49. }
  50. matches := versionRegexp.FindStringSubmatch(out.String())
  51. if len(matches) != 4 {
  52. return nil, fmt.Errorf("can't parse vyper version %q", out.String())
  53. }
  54. s := &Vyper{Path: cmd.Path, FullVersion: out.String(), Version: matches[0]}
  55. if s.Major, err = strconv.Atoi(matches[1]); err != nil {
  56. return nil, err
  57. }
  58. if s.Minor, err = strconv.Atoi(matches[2]); err != nil {
  59. return nil, err
  60. }
  61. if s.Patch, err = strconv.Atoi(matches[3]); err != nil {
  62. return nil, err
  63. }
  64. return s, nil
  65. }
  66. // CompileVyper compiles all given Vyper source files.
  67. func CompileVyper(vyper string, sourcefiles ...string) (map[string]*Contract, error) {
  68. if len(sourcefiles) == 0 {
  69. return nil, errors.New("vyper: no source files")
  70. }
  71. source, err := slurpFiles(sourcefiles)
  72. if err != nil {
  73. return nil, err
  74. }
  75. s, err := VyperVersion(vyper)
  76. if err != nil {
  77. return nil, err
  78. }
  79. args := s.makeArgs()
  80. cmd := exec.Command(s.Path, append(args, sourcefiles...)...)
  81. return s.run(cmd, source)
  82. }
  83. func (s *Vyper) run(cmd *exec.Cmd, source string) (map[string]*Contract, error) {
  84. var stderr, stdout bytes.Buffer
  85. cmd.Stderr = &stderr
  86. cmd.Stdout = &stdout
  87. if err := cmd.Run(); err != nil {
  88. return nil, fmt.Errorf("vyper: %v\n%s", err, stderr.Bytes())
  89. }
  90. return ParseVyperJSON(stdout.Bytes(), source, s.Version, s.Version, strings.Join(s.makeArgs(), " "))
  91. }
  92. // ParseVyperJSON takes the direct output of a vyper --f combined_json run and
  93. // parses it into a map of string contract name to Contract structs. The
  94. // provided source, language and compiler version, and compiler options are all
  95. // passed through into the Contract structs.
  96. //
  97. // The vyper output is expected to contain ABI and source mapping.
  98. //
  99. // Returns an error if the JSON is malformed or missing data, or if the JSON
  100. // embedded within the JSON is malformed.
  101. func ParseVyperJSON(combinedJSON []byte, source string, languageVersion string, compilerVersion string, compilerOptions string) (map[string]*Contract, error) {
  102. var output map[string]interface{}
  103. if err := json.Unmarshal(combinedJSON, &output); err != nil {
  104. return nil, err
  105. }
  106. // Compilation succeeded, assemble and return the contracts.
  107. contracts := make(map[string]*Contract)
  108. for name, info := range output {
  109. // Parse the individual compilation results.
  110. if name == "version" {
  111. continue
  112. }
  113. c := info.(map[string]interface{})
  114. contracts[name] = &Contract{
  115. Code: c["bytecode"].(string),
  116. RuntimeCode: c["bytecode_runtime"].(string),
  117. Info: ContractInfo{
  118. Source: source,
  119. Language: "Vyper",
  120. LanguageVersion: languageVersion,
  121. CompilerVersion: compilerVersion,
  122. CompilerOptions: compilerOptions,
  123. SrcMap: c["source_map"],
  124. SrcMapRuntime: "",
  125. AbiDefinition: c["abi"],
  126. UserDoc: "",
  127. DeveloperDoc: "",
  128. Metadata: "",
  129. },
  130. }
  131. }
  132. return contracts, nil
  133. }