github.com/theQRL/go-zond@v0.2.1/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  
    57  	return tool
    58  }
    59  
    60  func (g *GoToolchain) goTool(command string, args ...string) *exec.Cmd {
    61  	if g.Root == "" {
    62  		g.Root = runtime.GOROOT()
    63  	}
    64  	tool := exec.Command(filepath.Join(g.Root, "bin", "go"), command) // nolint: gosec
    65  	tool.Args = append(tool.Args, args...)
    66  	tool.Env = append(tool.Env, "GOROOT="+g.Root)
    67  
    68  	// Forward environment variables to the tool, but skip compiler target settings.
    69  	// TODO: what about GOARM?
    70  	skip := map[string]struct{}{"GOROOT": {}, "GOARCH": {}, "GOOS": {}, "GOBIN": {}, "CC": {}}
    71  	for _, e := range os.Environ() {
    72  		if i := strings.IndexByte(e, '='); i >= 0 {
    73  			if _, ok := skip[e[:i]]; ok {
    74  				continue
    75  			}
    76  		}
    77  		tool.Env = append(tool.Env, e)
    78  	}
    79  	return tool
    80  }
    81  
    82  // DownloadGo downloads the Go binary distribution and unpacks it into a temporary
    83  // directory. It returns the GOROOT of the unpacked toolchain.
    84  func DownloadGo(csdb *ChecksumDB, version string) string {
    85  	// Shortcut: if the Go version that runs this script matches the
    86  	// requested version exactly, there is no need to download anything.
    87  	activeGo := strings.TrimPrefix(runtime.Version(), "go")
    88  	if activeGo == version {
    89  		log.Printf("-dlgo version matches active Go version %s, skipping download.", activeGo)
    90  		return runtime.GOROOT()
    91  	}
    92  
    93  	ucache, err := os.UserCacheDir()
    94  	if err != nil {
    95  		log.Fatal(err)
    96  	}
    97  
    98  	// For Arm architecture, GOARCH includes ISA version.
    99  	os := runtime.GOOS
   100  	arch := runtime.GOARCH
   101  	if arch == "arm" {
   102  		arch = "armv6l"
   103  	}
   104  	file := fmt.Sprintf("go%s.%s-%s", version, os, arch)
   105  	if os == "windows" {
   106  		file += ".zip"
   107  	} else {
   108  		file += ".tar.gz"
   109  	}
   110  	url := "https://golang.org/dl/" + file
   111  	dst := filepath.Join(ucache, file)
   112  	if err := csdb.DownloadFile(url, dst); err != nil {
   113  		log.Fatal(err)
   114  	}
   115  
   116  	godir := filepath.Join(ucache, fmt.Sprintf("gzond-go-%s-%s-%s", version, os, arch))
   117  	if err := ExtractArchive(dst, godir); err != nil {
   118  		log.Fatal(err)
   119  	}
   120  	goroot, err := filepath.Abs(filepath.Join(godir, "go"))
   121  	if err != nil {
   122  		log.Fatal(err)
   123  	}
   124  	return goroot
   125  }