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