github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/src/cmd/dist/main.go (about)

     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  	case "aix":
    85  		// uname -m doesn't work under AIX
    86  		gohostarch = "ppc64"
    87  	}
    88  
    89  	sysinit()
    90  
    91  	if gohostarch == "" {
    92  		// Default Unix system.
    93  		out := run("", CheckExit, "uname", "-m")
    94  		switch {
    95  		case strings.Contains(out, "x86_64"), strings.Contains(out, "amd64"):
    96  			gohostarch = "amd64"
    97  		case strings.Contains(out, "86"):
    98  			gohostarch = "386"
    99  		case strings.Contains(out, "arm"):
   100  			gohostarch = "arm"
   101  		case strings.Contains(out, "aarch64"):
   102  			gohostarch = "arm64"
   103  		case strings.Contains(out, "ppc64le"):
   104  			gohostarch = "ppc64le"
   105  		case strings.Contains(out, "ppc64"):
   106  			gohostarch = "ppc64"
   107  		case strings.Contains(out, "mips64"):
   108  			gohostarch = "mips64"
   109  			if elfIsLittleEndian(os.Args[0]) {
   110  				gohostarch = "mips64le"
   111  			}
   112  		case strings.Contains(out, "mips"):
   113  			gohostarch = "mips"
   114  			if elfIsLittleEndian(os.Args[0]) {
   115  				gohostarch = "mipsle"
   116  			}
   117  		case strings.Contains(out, "s390x"):
   118  			gohostarch = "s390x"
   119  		case gohostos == "darwin":
   120  			if strings.Contains(run("", CheckExit, "uname", "-v"), "RELEASE_ARM_") {
   121  				gohostarch = "arm"
   122  			}
   123  		default:
   124  			fatalf("unknown architecture: %s", out)
   125  		}
   126  	}
   127  
   128  	if gohostarch == "arm" || gohostarch == "mips64" || gohostarch == "mips64le" {
   129  		maxbg = min(maxbg, runtime.NumCPU())
   130  	}
   131  	bginit()
   132  
   133  	if len(os.Args) > 1 && os.Args[1] == "-check-goarm" {
   134  		useVFPv1() // might fail with SIGILL
   135  		println("VFPv1 OK.")
   136  		useVFPv3() // might fail with SIGILL
   137  		println("VFPv3 OK.")
   138  		os.Exit(0)
   139  	}
   140  
   141  	xinit()
   142  	xmain()
   143  	xexit(0)
   144  }
   145  
   146  // The OS-specific main calls into the portable code here.
   147  func xmain() {
   148  	if len(os.Args) < 2 {
   149  		usage()
   150  	}
   151  	cmd := os.Args[1]
   152  	os.Args = os.Args[1:] // for flag parsing during cmd
   153  	flag.Usage = func() {
   154  		fmt.Fprintf(os.Stderr, "usage: go tool dist %s [options]\n", cmd)
   155  		flag.PrintDefaults()
   156  		os.Exit(2)
   157  	}
   158  	if f, ok := commands[cmd]; ok {
   159  		f()
   160  	} else {
   161  		xprintf("unknown command %s\n", cmd)
   162  		usage()
   163  	}
   164  }