github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/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 "openbsd":
    69  		// The gcc available on OpenBSD armv7 is old/inadequate (for example, lacks
    70  		// __sync_fetch_and_*/__sync_*_and_fetch) and will likely be removed in the
    71  		// not-to-distant future - use clang instead.
    72  		if runtime.GOARCH == "arm" {
    73  			defaultclang = true
    74  		}
    75  	case "solaris":
    76  		// Even on 64-bit platform, solaris uname -m prints i86pc.
    77  		out := run("", CheckExit, "isainfo", "-n")
    78  		if strings.Contains(out, "amd64") {
    79  			gohostarch = "amd64"
    80  		}
    81  		if strings.Contains(out, "i386") {
    82  			gohostarch = "386"
    83  		}
    84  	case "plan9":
    85  		gohostarch = os.Getenv("objtype")
    86  		if gohostarch == "" {
    87  			fatalf("$objtype is unset")
    88  		}
    89  	case "windows":
    90  		exe = ".exe"
    91  	case "aix":
    92  		// uname -m doesn't work under AIX
    93  		gohostarch = "ppc64"
    94  	}
    95  
    96  	sysinit()
    97  
    98  	if gohostarch == "" {
    99  		// Default Unix system.
   100  		out := run("", CheckExit, "uname", "-m")
   101  		switch {
   102  		case strings.Contains(out, "x86_64"), strings.Contains(out, "amd64"):
   103  			gohostarch = "amd64"
   104  		case strings.Contains(out, "86"):
   105  			gohostarch = "386"
   106  		case strings.Contains(out, "arm"):
   107  			gohostarch = "arm"
   108  		case strings.Contains(out, "aarch64"):
   109  			gohostarch = "arm64"
   110  		case strings.Contains(out, "ppc64le"):
   111  			gohostarch = "ppc64le"
   112  		case strings.Contains(out, "ppc64"):
   113  			gohostarch = "ppc64"
   114  		case strings.Contains(out, "mips64"):
   115  			gohostarch = "mips64"
   116  			if elfIsLittleEndian(os.Args[0]) {
   117  				gohostarch = "mips64le"
   118  			}
   119  		case strings.Contains(out, "mips"):
   120  			gohostarch = "mips"
   121  			if elfIsLittleEndian(os.Args[0]) {
   122  				gohostarch = "mipsle"
   123  			}
   124  		case strings.Contains(out, "s390x"):
   125  			gohostarch = "s390x"
   126  		case gohostos == "darwin":
   127  			if strings.Contains(run("", CheckExit, "uname", "-v"), "RELEASE_ARM_") {
   128  				gohostarch = "arm"
   129  			}
   130  		default:
   131  			fatalf("unknown architecture: %s", out)
   132  		}
   133  	}
   134  
   135  	if gohostarch == "arm" || gohostarch == "mips64" || gohostarch == "mips64le" {
   136  		maxbg = min(maxbg, runtime.NumCPU())
   137  	}
   138  	bginit()
   139  
   140  	if len(os.Args) > 1 && os.Args[1] == "-check-goarm" {
   141  		useVFPv1() // might fail with SIGILL
   142  		println("VFPv1 OK.")
   143  		useVFPv3() // might fail with SIGILL
   144  		println("VFPv3 OK.")
   145  		os.Exit(0)
   146  	}
   147  
   148  	xinit()
   149  	xmain()
   150  	xexit(0)
   151  }
   152  
   153  // The OS-specific main calls into the portable code here.
   154  func xmain() {
   155  	if len(os.Args) < 2 {
   156  		usage()
   157  	}
   158  	cmd := os.Args[1]
   159  	os.Args = os.Args[1:] // for flag parsing during cmd
   160  	flag.Usage = func() {
   161  		fmt.Fprintf(os.Stderr, "usage: go tool dist %s [options]\n", cmd)
   162  		flag.PrintDefaults()
   163  		os.Exit(2)
   164  	}
   165  	if f, ok := commands[cmd]; ok {
   166  		f()
   167  	} else {
   168  		xprintf("unknown command %s\n", cmd)
   169  		usage()
   170  	}
   171  }