util.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2016 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 build
  17. import (
  18. "bytes"
  19. "flag"
  20. "fmt"
  21. "io"
  22. "io/ioutil"
  23. "log"
  24. "os"
  25. "os/exec"
  26. "path"
  27. "path/filepath"
  28. "runtime"
  29. "strings"
  30. "text/template"
  31. )
  32. var DryRunFlag = flag.Bool("n", false, "dry run, don't execute commands")
  33. // MustRun executes the given command and exits the host process for
  34. // any error.
  35. func MustRun(cmd *exec.Cmd) {
  36. fmt.Println(">>>", strings.Join(cmd.Args, " "))
  37. if !*DryRunFlag {
  38. cmd.Stderr = os.Stderr
  39. cmd.Stdout = os.Stdout
  40. if err := cmd.Run(); err != nil {
  41. log.Fatal(err)
  42. }
  43. }
  44. }
  45. func MustRunCommand(cmd string, args ...string) {
  46. MustRun(exec.Command(cmd, args...))
  47. }
  48. // GOPATH returns the value that the GOPATH environment
  49. // variable should be set to.
  50. func GOPATH() string {
  51. if os.Getenv("GOPATH") == "" {
  52. log.Fatal("GOPATH is not set")
  53. }
  54. return os.Getenv("GOPATH")
  55. }
  56. // VERSION returns the content of the VERSION file.
  57. func VERSION() string {
  58. version, err := ioutil.ReadFile("VERSION")
  59. if err != nil {
  60. log.Fatal(err)
  61. }
  62. return string(bytes.TrimSpace(version))
  63. }
  64. var warnedAboutGit bool
  65. // RunGit runs a git subcommand and returns its output.
  66. // The command must complete successfully.
  67. func RunGit(args ...string) string {
  68. cmd := exec.Command("git", args...)
  69. var stdout, stderr bytes.Buffer
  70. cmd.Stdout, cmd.Stderr = &stdout, &stderr
  71. if err := cmd.Run(); err == exec.ErrNotFound {
  72. if !warnedAboutGit {
  73. log.Println("Warning: can't find 'git' in PATH")
  74. warnedAboutGit = true
  75. }
  76. return ""
  77. } else if err != nil {
  78. log.Fatal(strings.Join(cmd.Args, " "), ": ", err, "\n", stderr.String())
  79. }
  80. return strings.TrimSpace(stdout.String())
  81. }
  82. // readGitFile returns content of file in .git directory.
  83. func readGitFile(file string) string {
  84. content, err := ioutil.ReadFile(path.Join(".git", file))
  85. if err != nil {
  86. return ""
  87. }
  88. return strings.TrimSpace(string(content))
  89. }
  90. // Render renders the given template file into outputFile.
  91. func Render(templateFile, outputFile string, outputPerm os.FileMode, x interface{}) {
  92. tpl := template.Must(template.ParseFiles(templateFile))
  93. render(tpl, outputFile, outputPerm, x)
  94. }
  95. // RenderString renders the given template string into outputFile.
  96. func RenderString(templateContent, outputFile string, outputPerm os.FileMode, x interface{}) {
  97. tpl := template.Must(template.New("").Parse(templateContent))
  98. render(tpl, outputFile, outputPerm, x)
  99. }
  100. func render(tpl *template.Template, outputFile string, outputPerm os.FileMode, x interface{}) {
  101. if err := os.MkdirAll(filepath.Dir(outputFile), 0755); err != nil {
  102. log.Fatal(err)
  103. }
  104. out, err := os.OpenFile(outputFile, os.O_CREATE|os.O_WRONLY|os.O_EXCL, outputPerm)
  105. if err != nil {
  106. log.Fatal(err)
  107. }
  108. if err := tpl.Execute(out, x); err != nil {
  109. log.Fatal(err)
  110. }
  111. if err := out.Close(); err != nil {
  112. log.Fatal(err)
  113. }
  114. }
  115. // CopyFile copies a file.
  116. func CopyFile(dst, src string, mode os.FileMode) {
  117. if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
  118. log.Fatal(err)
  119. }
  120. destFile, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode)
  121. if err != nil {
  122. log.Fatal(err)
  123. }
  124. defer destFile.Close()
  125. srcFile, err := os.Open(src)
  126. if err != nil {
  127. log.Fatal(err)
  128. }
  129. defer srcFile.Close()
  130. if _, err := io.Copy(destFile, srcFile); err != nil {
  131. log.Fatal(err)
  132. }
  133. }
  134. // GoTool returns the command that runs a go tool. This uses go from GOROOT instead of PATH
  135. // so that go commands executed by build use the same version of Go as the 'host' that runs
  136. // build code. e.g.
  137. //
  138. // /usr/lib/go-1.8/bin/go run build/ci.go ...
  139. //
  140. // runs using go 1.8 and invokes go 1.8 tools from the same GOROOT. This is also important
  141. // because runtime.Version checks on the host should match the tools that are run.
  142. func GoTool(tool string, args ...string) *exec.Cmd {
  143. args = append([]string{tool}, args...)
  144. return exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"), args...)
  145. }
  146. // ExpandPackagesNoVendor expands a cmd/go import path pattern, skipping
  147. // vendored packages.
  148. func ExpandPackagesNoVendor(patterns []string) []string {
  149. expand := false
  150. for _, pkg := range patterns {
  151. if strings.Contains(pkg, "...") {
  152. expand = true
  153. }
  154. }
  155. if expand {
  156. cmd := GoTool("list", patterns...)
  157. out, err := cmd.CombinedOutput()
  158. if err != nil {
  159. log.Fatalf("package listing failed: %v\n%s", err, string(out))
  160. }
  161. var packages []string
  162. for _, line := range strings.Split(string(out), "\n") {
  163. if !strings.Contains(line, "/vendor/") {
  164. packages = append(packages, strings.TrimSpace(line))
  165. }
  166. }
  167. return packages
  168. }
  169. return patterns
  170. }