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