1
0
mirror of https://github.com/golang/go synced 2024-11-20 08:54:40 -07:00

cmd/dist: move functions for the better

This belongs to a series of clean-up changes (see below) for cmd/dist.
This is change (6).

These changes include:
(1)  apply minor fixes
(2)  restore behavior of branchtag
(3)  unleash bootstrap optimization for windows
(4)  use standard generated code header
(5)  remove trivial variables + functions
(6)  move functions for the better
(7)  simplify code segments
(8)  use bytes.Buffer for code generation
(9)  rename variables + functions
(10) remove doc.go

Change-Id: I1c49e3427079194210a6416057100a7e94a37619
Reviewed-on: https://go-review.googlesource.com/61012
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Marvin Stenger 2017-08-31 13:00:19 +02:00 committed by Ian Lance Taylor
parent 6ea4cfb347
commit 401609c3ff
3 changed files with 177 additions and 176 deletions

18
src/cmd/dist/build.go vendored
View File

@ -942,24 +942,6 @@ func clean() {
* command implementations * command implementations
*/ */
func usage() {
xprintf("usage: go tool dist [command]\n" +
"Commands are:\n" +
"\n" +
"banner print installation banner\n" +
"bootstrap rebuild everything\n" +
"clean deletes all built files\n" +
"env [-p] print environment (-p: include $PATH)\n" +
"install [dir] install individual directory\n" +
"list [-json] list all supported platforms\n" +
"test [-h] run Go test(s)\n" +
"version print Go version\n" +
"\n" +
"All commands take -v flags to emit extra information.\n",
)
xexit(2)
}
// The env command prints the default environment. // The env command prints the default environment.
func cmdenv() { func cmdenv() {
path := flag.Bool("p", false, "emit updated PATH") path := flag.Bool("p", false, "emit updated PATH")

177
src/cmd/dist/main.go vendored
View File

@ -8,9 +8,29 @@ import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
"runtime"
"strconv" "strconv"
"strings"
) )
func usage() {
xprintf("usage: go tool dist [command]\n" +
"Commands are:\n" +
"\n" +
"banner print installation banner\n" +
"bootstrap rebuild everything\n" +
"clean deletes all built files\n" +
"env [-p] print environment (-p: include $PATH)\n" +
"install [dir] install individual directory\n" +
"list [-json] list all supported platforms\n" +
"test [-h] run Go test(s)\n" +
"version print Go version\n" +
"\n" +
"All commands take -v flags to emit extra information.\n",
)
xexit(2)
}
// cmdtab records the available commands. // cmdtab records the available commands.
var cmdtab = []struct { var cmdtab = []struct {
name string name string
@ -26,6 +46,125 @@ var cmdtab = []struct {
{"version", cmdversion}, {"version", cmdversion},
} }
// main takes care of OS-specific startup and dispatches to xmain.
func main() {
os.Setenv("TERM", "dumb") // disable escape codes in clang errors
// provide -check-armv6k first, before checking for $GOROOT so that
// it is possible to run this check without having $GOROOT available.
if len(os.Args) > 1 && os.Args[1] == "-check-armv6k" {
useARMv6K() // might fail with SIGILL
println("ARMv6K supported.")
os.Exit(0)
}
gohostos = runtime.GOOS
switch gohostos {
case "darwin":
// Even on 64-bit platform, darwin uname -m prints i386.
// We don't support any of the OS X versions that run on 32-bit-only hardware anymore.
gohostarch = "amd64"
case "freebsd":
// Since FreeBSD 10 gcc is no longer part of the base system.
defaultclang = true
case "solaris":
// Even on 64-bit platform, solaris uname -m prints i86pc.
out := run("", CheckExit, "isainfo", "-n")
if strings.Contains(out, "amd64") {
gohostarch = "amd64"
}
if strings.Contains(out, "i386") {
gohostarch = "386"
}
case "plan9":
gohostarch = os.Getenv("objtype")
if gohostarch == "" {
fatal("$objtype is unset")
}
case "windows":
exe = ".exe"
}
sysinit()
if gohostarch == "" {
// Default Unix system.
out := run("", CheckExit, "uname", "-m")
switch {
case strings.Contains(out, "x86_64"), strings.Contains(out, "amd64"):
gohostarch = "amd64"
case strings.Contains(out, "86"):
gohostarch = "386"
case strings.Contains(out, "arm"):
gohostarch = "arm"
case strings.Contains(out, "aarch64"):
gohostarch = "arm64"
case strings.Contains(out, "ppc64le"):
gohostarch = "ppc64le"
case strings.Contains(out, "ppc64"):
gohostarch = "ppc64"
case strings.Contains(out, "mips64"):
gohostarch = "mips64"
if elfIsLittleEndian(os.Args[0]) {
gohostarch = "mips64le"
}
case strings.Contains(out, "mips"):
gohostarch = "mips"
if elfIsLittleEndian(os.Args[0]) {
gohostarch = "mipsle"
}
case strings.Contains(out, "s390x"):
gohostarch = "s390x"
case gohostos == "darwin":
if strings.Contains(run("", CheckExit, "uname", "-v"), "RELEASE_ARM_") {
gohostarch = "arm"
}
default:
fatal("unknown architecture: %s", out)
}
}
if gohostarch == "arm" || gohostarch == "mips64" || gohostarch == "mips64le" {
maxbg = min(maxbg, runtime.NumCPU())
}
bginit()
// The OS X 10.6 linker does not support external linking mode.
// See golang.org/issue/5130.
//
// OS X 10.6 does not work with clang either, but OS X 10.9 requires it.
// It seems to work with OS X 10.8, so we default to clang for 10.8 and later.
// See golang.org/issue/5822.
//
// Roughly, OS X 10.N shows up as uname release (N+4),
// so OS X 10.6 is uname version 10 and OS X 10.8 is uname version 12.
if gohostos == "darwin" {
rel := run("", CheckExit, "uname", "-r")
if i := strings.Index(rel, "."); i >= 0 {
rel = rel[:i]
}
osx, _ := strconv.Atoi(rel)
if osx <= 6+4 {
goextlinkenabled = "0"
}
if osx >= 8+4 {
defaultclang = true
}
}
if len(os.Args) > 1 && os.Args[1] == "-check-goarm" {
useVFPv1() // might fail with SIGILL
println("VFPv1 OK.")
useVFPv3() // might fail with SIGILL
println("VFPv3 OK.")
os.Exit(0)
}
xinit()
xmain()
xexit(0)
}
// The OS-specific main calls into the portable code here. // The OS-specific main calls into the portable code here.
func xmain() { func xmain() {
if len(os.Args) < 2 { if len(os.Args) < 2 {
@ -44,44 +183,6 @@ func xmain() {
return return
} }
} }
xprintf("unknown command %s\n", cmd) xprintf("unknown command %s\n", cmd)
usage() usage()
} }
func xflagparse(maxargs int) {
flag.Var((*count)(&vflag), "v", "verbosity")
flag.Parse()
if maxargs >= 0 && flag.NArg() > maxargs {
flag.Usage()
}
}
// count is a flag.Value that is like a flag.Bool and a flag.Int.
// If used as -name, it increments the count, but -name=x sets the count.
// Used for verbose flag -v.
type count int
func (c *count) String() string {
return fmt.Sprint(int(*c))
}
func (c *count) Set(s string) error {
switch s {
case "true":
*c++
case "false":
*c = 0
default:
n, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("invalid count %q", s)
}
*c = count(n)
}
return nil
}
func (c *count) IsBoolFlag() bool {
return true
}

158
src/cmd/dist/util.go vendored
View File

@ -6,13 +6,13 @@ package main
import ( import (
"bytes" "bytes"
"flag"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@ -372,125 +372,6 @@ func errprintf(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format, args...) fmt.Fprintf(os.Stderr, format, args...)
} }
// main takes care of OS-specific startup and dispatches to xmain.
func main() {
os.Setenv("TERM", "dumb") // disable escape codes in clang errors
// provide -check-armv6k first, before checking for $GOROOT so that
// it is possible to run this check without having $GOROOT available.
if len(os.Args) > 1 && os.Args[1] == "-check-armv6k" {
useARMv6K() // might fail with SIGILL
println("ARMv6K supported.")
os.Exit(0)
}
gohostos = runtime.GOOS
switch gohostos {
case "darwin":
// Even on 64-bit platform, darwin uname -m prints i386.
// We don't support any of the OS X versions that run on 32-bit-only hardware anymore.
gohostarch = "amd64"
case "freebsd":
// Since FreeBSD 10 gcc is no longer part of the base system.
defaultclang = true
case "solaris":
// Even on 64-bit platform, solaris uname -m prints i86pc.
out := run("", CheckExit, "isainfo", "-n")
if strings.Contains(out, "amd64") {
gohostarch = "amd64"
}
if strings.Contains(out, "i386") {
gohostarch = "386"
}
case "plan9":
gohostarch = os.Getenv("objtype")
if gohostarch == "" {
fatal("$objtype is unset")
}
case "windows":
exe = ".exe"
}
sysinit()
if gohostarch == "" {
// Default Unix system.
out := run("", CheckExit, "uname", "-m")
switch {
case strings.Contains(out, "x86_64"), strings.Contains(out, "amd64"):
gohostarch = "amd64"
case strings.Contains(out, "86"):
gohostarch = "386"
case strings.Contains(out, "arm"):
gohostarch = "arm"
case strings.Contains(out, "aarch64"):
gohostarch = "arm64"
case strings.Contains(out, "ppc64le"):
gohostarch = "ppc64le"
case strings.Contains(out, "ppc64"):
gohostarch = "ppc64"
case strings.Contains(out, "mips64"):
gohostarch = "mips64"
if elfIsLittleEndian(os.Args[0]) {
gohostarch = "mips64le"
}
case strings.Contains(out, "mips"):
gohostarch = "mips"
if elfIsLittleEndian(os.Args[0]) {
gohostarch = "mipsle"
}
case strings.Contains(out, "s390x"):
gohostarch = "s390x"
case gohostos == "darwin":
if strings.Contains(run("", CheckExit, "uname", "-v"), "RELEASE_ARM_") {
gohostarch = "arm"
}
default:
fatal("unknown architecture: %s", out)
}
}
if gohostarch == "arm" || gohostarch == "mips64" || gohostarch == "mips64le" {
maxbg = min(maxbg, runtime.NumCPU())
}
bginit()
// The OS X 10.6 linker does not support external linking mode.
// See golang.org/issue/5130.
//
// OS X 10.6 does not work with clang either, but OS X 10.9 requires it.
// It seems to work with OS X 10.8, so we default to clang for 10.8 and later.
// See golang.org/issue/5822.
//
// Roughly, OS X 10.N shows up as uname release (N+4),
// so OS X 10.6 is uname version 10 and OS X 10.8 is uname version 12.
if gohostos == "darwin" {
rel := run("", CheckExit, "uname", "-r")
if i := strings.Index(rel, "."); i >= 0 {
rel = rel[:i]
}
osx, _ := strconv.Atoi(rel)
if osx <= 6+4 {
goextlinkenabled = "0"
}
if osx >= 8+4 {
defaultclang = true
}
}
if len(os.Args) > 1 && os.Args[1] == "-check-goarm" {
useVFPv1() // might fail with SIGILL
println("VFPv1 OK.")
useVFPv3() // might fail with SIGILL
println("VFPv3 OK.")
os.Exit(0)
}
xinit()
xmain()
xexit(0)
}
// xsamefile reports whether f1 and f2 are the same file (or dir) // xsamefile reports whether f1 and f2 are the same file (or dir)
func xsamefile(f1, f2 string) bool { func xsamefile(f1, f2 string) bool {
fi1, err1 := os.Stat(f1) fi1, err1 := os.Stat(f1)
@ -569,3 +450,40 @@ func elfIsLittleEndian(fn string) bool {
} }
panic("unreachable") panic("unreachable")
} }
// count is a flag.Value that is like a flag.Bool and a flag.Int.
// If used as -name, it increments the count, but -name=x sets the count.
// Used for verbose flag -v.
type count int
func (c *count) String() string {
return fmt.Sprint(int(*c))
}
func (c *count) Set(s string) error {
switch s {
case "true":
*c++
case "false":
*c = 0
default:
n, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("invalid count %q", s)
}
*c = count(n)
}
return nil
}
func (c *count) IsBoolFlag() bool {
return true
}
func xflagparse(maxargs int) {
flag.Var((*count)(&vflag), "v", "verbosity")
flag.Parse()
if maxargs >= 0 && flag.NArg() > maxargs {
flag.Usage()
}
}