github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/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 "aix": 60 // uname -m doesn't work under AIX 61 gohostarch = "ppc64" 62 case "darwin": 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 // OpenBSD ships with GCC 4.2, which is now quite old. 70 defaultclang = true 71 case "plan9": 72 gohostarch = os.Getenv("objtype") 73 if gohostarch == "" { 74 fatalf("$objtype is unset") 75 } 76 case "solaris", "illumos": 77 // Solaris and illumos systems have multi-arch userlands, and 78 // "uname -m" reports the machine hardware name; e.g., 79 // "i86pc" on both 32- and 64-bit x86 systems. Check for the 80 // native (widest) instruction set on the running kernel: 81 out := run("", CheckExit, "isainfo", "-n") 82 if strings.Contains(out, "amd64") { 83 gohostarch = "amd64" 84 } 85 if strings.Contains(out, "i386") { 86 gohostarch = "386" 87 } 88 case "windows": 89 exe = ".exe" 90 } 91 92 sysinit() 93 94 if gohostarch == "" { 95 // Default Unix system. 96 out := run("", CheckExit, "uname", "-m") 97 switch { 98 case strings.Contains(out, "x86_64"), strings.Contains(out, "amd64"): 99 gohostarch = "amd64" 100 case strings.Contains(out, "86"): 101 gohostarch = "386" 102 if gohostos == "darwin" { 103 // Even on 64-bit platform, some versions of macOS uname -m prints i386. 104 // We don't support any of the OS X versions that run on 32-bit-only hardware anymore. 105 gohostarch = "amd64" 106 } 107 case strings.Contains(out, "aarch64"), strings.Contains(out, "arm64"): 108 gohostarch = "arm64" 109 case strings.Contains(out, "arm"): 110 gohostarch = "arm" 111 case strings.Contains(out, "ppc64le"): 112 gohostarch = "ppc64le" 113 case strings.Contains(out, "ppc64"): 114 gohostarch = "ppc64" 115 case strings.Contains(out, "mips64"): 116 gohostarch = "mips64" 117 if elfIsLittleEndian(os.Args[0]) { 118 gohostarch = "mips64le" 119 } 120 case strings.Contains(out, "mips"): 121 gohostarch = "mips" 122 if elfIsLittleEndian(os.Args[0]) { 123 gohostarch = "mipsle" 124 } 125 case strings.Contains(out, "riscv64"): 126 gohostarch = "riscv64" 127 case strings.Contains(out, "s390x"): 128 gohostarch = "s390x" 129 case gohostos == "darwin": 130 if strings.Contains(run("", CheckExit, "uname", "-v"), "RELEASE_ARM64_") { 131 gohostarch = "arm64" 132 } 133 default: 134 fatalf("unknown architecture: %s", out) 135 } 136 } 137 138 if gohostarch == "arm" || gohostarch == "mips64" || gohostarch == "mips64le" { 139 maxbg = min(maxbg, runtime.NumCPU()) 140 } 141 bginit() 142 143 if len(os.Args) > 1 && os.Args[1] == "-check-goarm" { 144 useVFPv1() // might fail with SIGILL 145 println("VFPv1 OK.") 146 useVFPv3() // might fail with SIGILL 147 println("VFPv3 OK.") 148 os.Exit(0) 149 } 150 151 xinit() 152 xmain() 153 xexit(0) 154 } 155 156 // The OS-specific main calls into the portable code here. 157 func xmain() { 158 if len(os.Args) < 2 { 159 usage() 160 } 161 cmd := os.Args[1] 162 os.Args = os.Args[1:] // for flag parsing during cmd 163 flag.Usage = func() { 164 fmt.Fprintf(os.Stderr, "usage: go tool dist %s [options]\n", cmd) 165 flag.PrintDefaults() 166 os.Exit(2) 167 } 168 if f, ok := commands[cmd]; ok { 169 f() 170 } else { 171 xprintf("unknown command %s\n", cmd) 172 usage() 173 } 174 }