From 401609c3ff0da3d7bbf78f5034c87ef5c4640792 Mon Sep 17 00:00:00 2001 From: Marvin Stenger Date: Thu, 31 Aug 2017 13:00:19 +0200 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/cmd/dist/build.go | 18 ----- src/cmd/dist/main.go | 177 +++++++++++++++++++++++++++++++++--------- src/cmd/dist/util.go | 158 +++++++++---------------------------- 3 files changed, 177 insertions(+), 176 deletions(-) diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go index 6159cb56e8e..7b0c3a05e7c 100644 --- a/src/cmd/dist/build.go +++ b/src/cmd/dist/build.go @@ -942,24 +942,6 @@ func clean() { * 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. func cmdenv() { path := flag.Bool("p", false, "emit updated PATH") diff --git a/src/cmd/dist/main.go b/src/cmd/dist/main.go index b0471bdc87b..6f5e6417395 100644 --- a/src/cmd/dist/main.go +++ b/src/cmd/dist/main.go @@ -8,9 +8,29 @@ import ( "flag" "fmt" "os" + "runtime" "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. var cmdtab = []struct { name string @@ -26,6 +46,125 @@ var cmdtab = []struct { {"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. func xmain() { if len(os.Args) < 2 { @@ -44,44 +183,6 @@ func xmain() { return } } - xprintf("unknown command %s\n", cmd) 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 -} diff --git a/src/cmd/dist/util.go b/src/cmd/dist/util.go index 0dcfd2b6fd6..03405993f31 100644 --- a/src/cmd/dist/util.go +++ b/src/cmd/dist/util.go @@ -6,13 +6,13 @@ package main import ( "bytes" + "flag" "fmt" "io" "io/ioutil" "os" "os/exec" "path/filepath" - "runtime" "sort" "strconv" "strings" @@ -372,125 +372,6 @@ func errprintf(format string, args ...interface{}) { 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) func xsamefile(f1, f2 string) bool { fi1, err1 := os.Stat(f1) @@ -569,3 +450,40 @@ func elfIsLittleEndian(fn string) bool { } 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() + } +}