github.com/ethereum/go-ethereum@v1.14.3/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) string {
    88  	version, err := Version(csdb, "golang")
    89  	if err != nil {
    90  		log.Fatal(err)
    91  	}
    92  	// Shortcut: if the Go version that runs this script matches the
    93  	// requested version exactly, there is no need to download anything.
    94  	activeGo := strings.TrimPrefix(runtime.Version(), "go")
    95  	if activeGo == version {
    96  		log.Printf("-dlgo version matches active Go version %s, skipping download.", activeGo)
    97  		return runtime.GOROOT()
    98  	}
    99  
   100  	ucache, err := os.UserCacheDir()
   101  	if err != nil {
   102  		log.Fatal(err)
   103  	}
   104  
   105  	// For Arm architecture, GOARCH includes ISA version.
   106  	os := runtime.GOOS
   107  	arch := runtime.GOARCH
   108  	if arch == "arm" {
   109  		arch = "armv6l"
   110  	}
   111  	file := fmt.Sprintf("go%s.%s-%s", version, os, arch)
   112  	if os == "windows" {
   113  		file += ".zip"
   114  	} else {
   115  		file += ".tar.gz"
   116  	}
   117  	url := "https://golang.org/dl/" + file
   118  	dst := filepath.Join(ucache, file)
   119  	if err := csdb.DownloadFile(url, dst); err != nil {
   120  		log.Fatal(err)
   121  	}
   122  
   123  	godir := filepath.Join(ucache, fmt.Sprintf("geth-go-%s-%s-%s", version, os, arch))
   124  	if err := ExtractArchive(dst, godir); err != nil {
   125  		log.Fatal(err)
   126  	}
   127  	goroot, err := filepath.Abs(filepath.Join(godir, "go"))
   128  	if err != nil {
   129  		log.Fatal(err)
   130  	}
   131  	return goroot
   132  }
   133  
   134  // Version returns the versions defined in the checksumdb.
   135  func Version(csdb *ChecksumDB, version string) (string, error) {
   136  	for _, l := range csdb.allChecksums {
   137  		if !strings.HasPrefix(l, "# version:") {
   138  			continue
   139  		}
   140  		v := strings.Split(l, ":")[1]
   141  		parts := strings.Split(v, " ")
   142  		if len(parts) != 2 {
   143  			log.Print("Erroneous version-string", "v", l)
   144  			continue
   145  		}
   146  		if parts[0] == version {
   147  			return parts[1], nil
   148  		}
   149  	}
   150  	return "", fmt.Errorf("no version found for '%v'", version)
   151  }
   152  
   153  // DownloadAndVerifyChecksums downloads all files and checks that they match
   154  // the checksum given in checksums.txt.
   155  // This task can be used to sanity-check new checksums.
   156  func DownloadAndVerifyChecksums(csdb *ChecksumDB) {
   157  	var (
   158  		base   = ""
   159  		ucache = os.TempDir()
   160  	)
   161  	for _, l := range csdb.allChecksums {
   162  		if strings.HasPrefix(l, "# https://") {
   163  			base = l[2:]
   164  			continue
   165  		}
   166  		if strings.HasPrefix(l, "#") {
   167  			continue
   168  		}
   169  		hashFile := strings.Split(l, "  ")
   170  		if len(hashFile) != 2 {
   171  			continue
   172  		}
   173  		file := hashFile[1]
   174  		url := base + file
   175  		dst := filepath.Join(ucache, file)
   176  		if err := csdb.DownloadFile(url, dst); err != nil {
   177  			log.Print(err)
   178  		}
   179  	}
   180  }