...
Run Format

Source file src/cmd/dist/main.go

Documentation: cmd/dist

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"os"
    11  	"runtime"
    12  	"strings"
    13  )
    14  
    15  func usage() {
    16  	xprintf(`usage: go tool dist [command]
    17  Commands are:
    18  
    19  banner         print installation banner
    20  bootstrap      rebuild everything
    21  clean          deletes all built files
    22  env [-p]       print environment (-p: include $PATH)
    23  install [dir]  install individual directory
    24  list [-json]   list all supported platforms
    25  test [-h]      run Go test(s)
    26  version        print Go version
    27  
    28  All commands take -v flags to emit extra information.
    29  `)
    30  	xexit(2)
    31  }
    32  
    33  // commands records the available commands.
    34  var commands = map[string]func(){
    35  	"banner":    cmdbanner,
    36  	"bootstrap": cmdbootstrap,
    37  	"clean":     cmdclean,
    38  	"env":       cmdenv,
    39  	"install":   cmdinstall,
    40  	"list":      cmdlist,
    41  	"test":      cmdtest,
    42  	"version":   cmdversion,
    43  }
    44  
    45  // main takes care of OS-specific startup and dispatches to xmain.
    46  func main() {
    47  	os.Setenv("TERM", "dumb") // disable escape codes in clang errors
    48  
    49  	// provide -check-armv6k first, before checking for $GOROOT so that
    50  	// it is possible to run this check without having $GOROOT available.
    51  	if len(os.Args) > 1 && os.Args[1] == "-check-armv6k" {
    52  		useARMv6K() // might fail with SIGILL
    53  		println("ARMv6K supported.")
    54  		os.Exit(0)
    55  	}
    56  
    57  	gohostos = runtime.GOOS
    58  	switch gohostos {
    59  	case "darwin":
    60  		// Even on 64-bit platform, darwin uname -m prints i386.
    61  		// We don't support any of the OS X versions that run on 32-bit-only hardware anymore.
    62  		gohostarch = "amd64"
    63  		// macOS 10.9 and later require clang
    64  		defaultclang = true
    65  	case "freebsd":
    66  		// Since FreeBSD 10 gcc is no longer part of the base system.
    67  		defaultclang = true
    68  	case "solaris":
    69  		// Even on 64-bit platform, solaris uname -m prints i86pc.
    70  		out := run("", CheckExit, "isainfo", "-n")
    71  		if strings.Contains(out, "amd64") {
    72  			gohostarch = "amd64"
    73  		}
    74  		if strings.Contains(out, "i386") {
    75  			gohostarch = "386"
    76  		}
    77  	case "plan9":
    78  		gohostarch = os.Getenv("objtype")
    79  		if gohostarch == "" {
    80  			fatalf("$objtype is unset")
    81  		}
    82  	case "windows":
    83  		exe = ".exe"
    84  	}
    85  
    86  	sysinit()
    87  
    88  	if gohostarch == "" {
    89  		// Default Unix system.
    90  		out := run("", CheckExit, "uname", "-m")
    91  		switch {
    92  		case strings.Contains(out, "x86_64"), strings.Contains(out, "amd64"):
    93  			gohostarch = "amd64"
    94  		case strings.Contains(out, "86"):
    95  			gohostarch = "386"
    96  		case strings.Contains(out, "arm"):
    97  			gohostarch = "arm"
    98  		case strings.Contains(out, "aarch64"):
    99  			gohostarch = "arm64"
   100  		case strings.Contains(out, "ppc64le"):
   101  			gohostarch = "ppc64le"
   102  		case strings.Contains(out, "ppc64"):
   103  			gohostarch = "ppc64"
   104  		case strings.Contains(out, "mips64"):
   105  			gohostarch = "mips64"
   106  			if elfIsLittleEndian(os.Args[0]) {
   107  				gohostarch = "mips64le"
   108  			}
   109  		case strings.Contains(out, "mips"):
   110  			gohostarch = "mips"
   111  			if elfIsLittleEndian(os.Args[0]) {
   112  				gohostarch = "mipsle"
   113  			}
   114  		case strings.Contains(out, "s390x"):
   115  			gohostarch = "s390x"
   116  		case gohostos == "darwin":
   117  			if strings.Contains(run("", CheckExit, "uname", "-v"), "RELEASE_ARM_") {
   118  				gohostarch = "arm"
   119  			}
   120  		default:
   121  			fatalf("unknown architecture: %s", out)
   122  		}
   123  	}
   124  
   125  	if gohostarch == "arm" || gohostarch == "mips64" || gohostarch == "mips64le" {
   126  		maxbg = min(maxbg, runtime.NumCPU())
   127  	}
   128  	bginit()
   129  
   130  	if len(os.Args) > 1 && os.Args[1] == "-check-goarm" {
   131  		useVFPv1() // might fail with SIGILL
   132  		println("VFPv1 OK.")
   133  		useVFPv3() // might fail with SIGILL
   134  		println("VFPv3 OK.")
   135  		os.Exit(0)
   136  	}
   137  
   138  	xinit()
   139  	xmain()
   140  	xexit(0)
   141  }
   142  
   143  // The OS-specific main calls into the portable code here.
   144  func xmain() {
   145  	if len(os.Args) < 2 {
   146  		usage()
   147  	}
   148  	cmd := os.Args[1]
   149  	os.Args = os.Args[1:] // for flag parsing during cmd
   150  	flag.Usage = func() {
   151  		fmt.Fprintf(os.Stderr, "usage: go tool dist %s [options]\n", cmd)
   152  		flag.PrintDefaults()
   153  		os.Exit(2)
   154  	}
   155  	if f, ok := commands[cmd]; ok {
   156  		f()
   157  	} else {
   158  		xprintf("unknown command %s\n", cmd)
   159  		usage()
   160  	}
   161  }
   162  

View as plain text