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