github.com/dominant-strategies/go-quai@v0.28.2/internal/build/gotool.go (about) 1 // Copyright 2021 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 17 package build 18 19 import ( 20 "fmt" 21 "log" 22 "os" 23 "os/exec" 24 "path/filepath" 25 "runtime" 26 "strings" 27 ) 28 29 type GoToolchain struct { 30 Root string // GOROOT 31 32 // Cross-compilation variables. These are set when running the go tool. 33 GOARCH string 34 GOOS string 35 CC string 36 } 37 38 // Go creates an invocation of the go command. 39 func (g *GoToolchain) Go(command string, args ...string) *exec.Cmd { 40 tool := g.goTool(command, args...) 41 42 // Configure environment for cross build. 43 if g.GOARCH != "" && g.GOARCH != runtime.GOARCH { 44 tool.Env = append(tool.Env, "CGO_ENABLED=1") 45 tool.Env = append(tool.Env, "GOARCH="+g.GOARCH) 46 } 47 if g.GOOS != "" && g.GOOS != runtime.GOOS { 48 tool.Env = append(tool.Env, "GOOS="+g.GOOS) 49 } 50 // Configure C compiler. 51 if g.CC != "" { 52 tool.Env = append(tool.Env, "CC="+g.CC) 53 } else if os.Getenv("CC") != "" { 54 tool.Env = append(tool.Env, "CC="+os.Getenv("CC")) 55 } 56 return tool 57 } 58 59 // Install creates an invocation of 'go install'. The command is configured to output 60 // executables to the given 'gobin' directory. 61 // 62 // This can be used to install auxiliary build tools without modifying the local go.mod and 63 // go.sum files. To install tools which are not required by go.mod, ensure that all module 64 // paths in 'args' contain a module version suffix (e.g. "...@latest"). 65 func (g *GoToolchain) Install(gobin string, args ...string) *exec.Cmd { 66 if !filepath.IsAbs(gobin) { 67 panic("GOBIN must be an absolute path") 68 } 69 tool := g.goTool("install") 70 tool.Env = append(tool.Env, "GOBIN="+gobin) 71 tool.Args = append(tool.Args, "-mod=readonly") 72 tool.Args = append(tool.Args, args...) 73 74 // Ensure GOPATH is set because go install seems to absolutely require it. This uses 75 // 'go env' because it resolves the default value when GOPATH is not set in the 76 // environment. Ignore errors running go env and leave any complaining about GOPATH to 77 // the install command. 78 pathTool := g.goTool("env", "GOPATH") 79 output, _ := pathTool.Output() 80 tool.Env = append(tool.Env, "GOPATH="+string(output)) 81 return tool 82 } 83 84 func (g *GoToolchain) goTool(command string, args ...string) *exec.Cmd { 85 if g.Root == "" { 86 g.Root = runtime.GOROOT() 87 } 88 tool := exec.Command(filepath.Join(g.Root, "bin", "go"), command) 89 tool.Args = append(tool.Args, args...) 90 tool.Env = append(tool.Env, "GOROOT="+g.Root) 91 92 // Forward environment variables to the tool, but skip compiler target settings. 93 // TODO: what about GOARM? 94 skip := map[string]struct{}{"GOROOT": {}, "GOARCH": {}, "GOOS": {}, "GOBIN": {}, "CC": {}} 95 for _, e := range os.Environ() { 96 if i := strings.IndexByte(e, '='); i >= 0 { 97 if _, ok := skip[e[:i]]; ok { 98 continue 99 } 100 } 101 tool.Env = append(tool.Env, e) 102 } 103 return tool 104 } 105 106 // DownloadGo downloads the Go binary distribution and unpacks it into a temporary 107 // directory. It returns the GOROOT of the unpacked toolchain. 108 func DownloadGo(csdb *ChecksumDB, version string) string { 109 // Shortcut: if the Go version that runs this script matches the 110 // requested version exactly, there is no need to download anything. 111 activeGo := strings.TrimPrefix(runtime.Version(), "go") 112 if activeGo == version { 113 log.Printf("-dlgo version matches active Go version %s, skipping download.", activeGo) 114 return runtime.GOROOT() 115 } 116 117 ucache, err := os.UserCacheDir() 118 if err != nil { 119 log.Fatal(err) 120 } 121 122 // For Arm architecture, GOARCH includes ISA version. 123 os := runtime.GOOS 124 arch := runtime.GOARCH 125 if arch == "arm" { 126 arch = "armv6l" 127 } 128 file := fmt.Sprintf("go%s.%s-%s", version, os, arch) 129 if os == "windows" { 130 file += ".zip" 131 } else { 132 file += ".tar.gz" 133 } 134 url := "https://golang.org/dl/" + file 135 dst := filepath.Join(ucache, file) 136 if err := csdb.DownloadFile(url, dst); err != nil { 137 log.Fatal(err) 138 } 139 140 godir := filepath.Join(ucache, fmt.Sprintf("quai-go-%s-%s-%s", version, os, arch)) 141 if err := ExtractArchive(dst, godir); err != nil { 142 log.Fatal(err) 143 } 144 goroot, err := filepath.Abs(filepath.Join(godir, "go")) 145 if err != nil { 146 log.Fatal(err) 147 } 148 return goroot 149 }