Commit 3844e707 authored by Marvin Stenger's avatar Marvin Stenger Committed by Ian Lance Taylor

cmd/dist: simplify code segments

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

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: Ia3c33ef060b4baaef354b729ba82ed0b28e52857
Reviewed-on: https://go-review.googlesource.com/61013
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent c174e46a
...@@ -43,8 +43,9 @@ var ( ...@@ -43,8 +43,9 @@ var (
defaultcxxtarget string defaultcxxtarget string
defaultcctarget string defaultcctarget string
defaultpkgconfigtarget string defaultpkgconfigtarget string
rebuildall bool
defaultclang bool rebuildall bool
defaultclang bool
vflag int // verbosity vflag int // verbosity
) )
...@@ -98,10 +99,11 @@ func xinit() { ...@@ -98,10 +99,11 @@ func xinit() {
} }
goroot = filepath.Clean(b) goroot = filepath.Clean(b)
goroot_final = os.Getenv("GOROOT_FINAL") b = os.Getenv("GOROOT_FINAL")
if goroot_final == "" { if b == "" {
goroot_final = goroot b = goroot
} }
goroot_final = b
b = os.Getenv("GOBIN") b = os.Getenv("GOBIN")
if b == "" { if b == "" {
...@@ -134,8 +136,7 @@ func xinit() { ...@@ -134,8 +136,7 @@ func xinit() {
} }
go386 = b go386 = b
p := pathf("%s/src/all.bash", goroot) if p := pathf("%s/src/all.bash", goroot); !isfile(p) {
if !isfile(p) {
fatal("$GOROOT is not set correctly or not exported\n"+ fatal("$GOROOT is not set correctly or not exported\n"+
"\tGOROOT=%s\n"+ "\tGOROOT=%s\n"+
"\t%s does not exist", goroot, p) "\t%s does not exist", goroot, p)
...@@ -145,7 +146,6 @@ func xinit() { ...@@ -145,7 +146,6 @@ func xinit() {
if b != "" { if b != "" {
gohostarch = b gohostarch = b
} }
if find(gohostarch, okgoarch) < 0 { if find(gohostarch, okgoarch) < 0 {
fatal("unknown $GOHOSTARCH %s", gohostarch) fatal("unknown $GOHOSTARCH %s", gohostarch)
} }
...@@ -253,20 +253,19 @@ func branchtag(branch string) (tag string, precise bool) { ...@@ -253,20 +253,19 @@ func branchtag(branch string) (tag string, precise bool) {
// Each line is either blank, or looks like // Each line is either blank, or looks like
// (tag: refs/tags/go1.4rc2, refs/remotes/origin/release-branch.go1.4, refs/heads/release-branch.go1.4) // (tag: refs/tags/go1.4rc2, refs/remotes/origin/release-branch.go1.4, refs/heads/release-branch.go1.4)
// We need to find an element starting with refs/tags/. // We need to find an element starting with refs/tags/.
i := strings.Index(line, " refs/tags/") const s = " refs/tags/"
i := strings.Index(line, s)
if i < 0 { if i < 0 {
continue continue
} }
i += len(" refs/tags/") // Trim off known prefix.
// The tag name ends at a comma or paren (prefer the first). line = line[i+len(s):]
j := strings.Index(line[i:], ",") // The tag name ends at a comma or paren.
if j < 0 { j := strings.IndexAny(line, ",)")
j = strings.Index(line[i:], ")")
}
if j < 0 { if j < 0 {
continue // malformed line; ignore it continue // malformed line; ignore it
} }
tag = line[i : i+j] tag = line[:j]
if row == 0 { if row == 0 {
precise = true // tag denotes HEAD precise = true // tag denotes HEAD
} }
...@@ -339,8 +338,7 @@ func isGitRepo() bool { ...@@ -339,8 +338,7 @@ func isGitRepo() bool {
if !filepath.IsAbs(gitDir) { if !filepath.IsAbs(gitDir) {
gitDir = filepath.Join(goroot, gitDir) gitDir = filepath.Join(goroot, gitDir)
} }
fi, err := os.Stat(gitDir) return isdir(gitDir)
return err == nil && fi.IsDir()
} }
/* /*
......
...@@ -8,7 +8,9 @@ import ( ...@@ -8,7 +8,9 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"os" "os"
"path/filepath"
"sort" "sort"
"strings"
) )
/* /*
...@@ -48,8 +50,7 @@ func mkzdefaultcc(dir, file string) { ...@@ -48,8 +50,7 @@ func mkzdefaultcc(dir, file string) {
"const defaultPkgConfig = `%s`\n", "const defaultPkgConfig = `%s`\n",
defaultcctarget, defaultcxxtarget, defaultpkgconfigtarget) defaultcctarget, defaultcxxtarget, defaultpkgconfigtarget)
i := len(file) - len("go/internal/cfg/zdefaultcc.go") file = strings.Replace(file, filepath.FromSlash("go/internal/cfg"), "cgo", 1)
file = file[:i] + "cgo/zdefaultcc.go"
writefile(outCgo, file, writeSkipSame) writefile(outCgo, file, writeSkipSame)
} }
...@@ -63,13 +64,14 @@ func mkzosarch(dir, file string) { ...@@ -63,13 +64,14 @@ func mkzosarch(dir, file string) {
sort.Strings(list) sort.Strings(list)
var buf bytes.Buffer var buf bytes.Buffer
buf.WriteString("// Code generated by go tool dist; DO NOT EDIT.\n\n") fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n\n")
buf.WriteString("package cfg\n\n") fmt.Fprintf(&buf, "package cfg\n\n")
fmt.Fprintf(&buf, "var OSArchSupportsCgo = map[string]bool{\n") fmt.Fprintf(&buf, "var OSArchSupportsCgo = map[string]bool{\n")
for _, plat := range list { for _, plat := range list {
fmt.Fprintf(&buf, "\t%q: %v,\n", plat, cgoEnabled[plat]) fmt.Fprintf(&buf, "\t%q: %v,\n", plat, cgoEnabled[plat])
} }
fmt.Fprintf(&buf, "}\n") fmt.Fprintf(&buf, "}\n")
writefile(buf.String(), file, writeSkipSame) writefile(buf.String(), file, writeSkipSame)
} }
......
...@@ -128,8 +128,7 @@ func bootstrapBuildTools() { ...@@ -128,8 +128,7 @@ func bootstrapBuildTools() {
} }
srcFile := pathf("%s/%s", src, name) srcFile := pathf("%s/%s", src, name)
dstFile := pathf("%s/%s", dst, name) dstFile := pathf("%s/%s", dst, name)
text := readfile(srcFile) text := bootstrapRewriteFile(srcFile)
text = bootstrapRewriteFile(text, srcFile)
writefile(text, dstFile, 0) writefile(text, dstFile, 0)
} }
} }
...@@ -221,7 +220,7 @@ func isUnneededSSARewriteFile(srcFile string) (archCaps string, unneeded bool) { ...@@ -221,7 +220,7 @@ func isUnneededSSARewriteFile(srcFile string) (archCaps string, unneeded bool) {
return archCaps, true return archCaps, true
} }
func bootstrapRewriteFile(text, srcFile string) string { func bootstrapRewriteFile(srcFile string) string {
// During bootstrap, generate dummy rewrite files for // During bootstrap, generate dummy rewrite files for
// irrelevant architectures. We only need to build a bootstrap // irrelevant architectures. We only need to build a bootstrap
// binary that works for the current runtime.GOARCH. // binary that works for the current runtime.GOARCH.
...@@ -236,11 +235,11 @@ func rewriteBlock%s(b *Block) bool { panic("unused during bootstrap") } ...@@ -236,11 +235,11 @@ func rewriteBlock%s(b *Block) bool { panic("unused during bootstrap") }
`, archCaps, archCaps) `, archCaps, archCaps)
} }
return bootstrapFixImports(text, srcFile) return bootstrapFixImports(srcFile)
} }
func bootstrapFixImports(text, srcFile string) string { func bootstrapFixImports(srcFile string) string {
lines := strings.SplitAfter(text, "\n") lines := strings.SplitAfter(readfile(srcFile), "\n")
inBlock := false inBlock := false
for i, line := range lines { for i, line := range lines {
if strings.HasPrefix(line, "import (") { if strings.HasPrefix(line, "import (") {
......
...@@ -14,36 +14,33 @@ import ( ...@@ -14,36 +14,33 @@ import (
) )
func usage() { func usage() {
xprintf("usage: go tool dist [command]\n" + xprintf(`usage: go tool dist [command]
"Commands are:\n" + Commands are:
"\n" +
"banner print installation banner\n" + banner print installation banner
"bootstrap rebuild everything\n" + bootstrap rebuild everything
"clean deletes all built files\n" + clean deletes all built files
"env [-p] print environment (-p: include $PATH)\n" + env [-p] print environment (-p: include $PATH)
"install [dir] install individual directory\n" + install [dir] install individual directory
"list [-json] list all supported platforms\n" + list [-json] list all supported platforms
"test [-h] run Go test(s)\n" + test [-h] run Go test(s)
"version print Go version\n" + version print Go version
"\n" +
"All commands take -v flags to emit extra information.\n", All commands take -v flags to emit extra information.
) `)
xexit(2) xexit(2)
} }
// cmdtab records the available commands. // commands records the available commands.
var cmdtab = []struct { var commands = map[string]func(){
name string "banner": cmdbanner,
f func() "bootstrap": cmdbootstrap,
}{ "clean": cmdclean,
{"banner", cmdbanner}, "env": cmdenv,
{"bootstrap", cmdbootstrap}, "install": cmdinstall,
{"clean", cmdclean}, "list": cmdlist,
{"env", cmdenv}, "test": cmdtest,
{"install", cmdinstall}, "version": cmdversion,
{"list", cmdlist},
{"test", cmdtest},
{"version", cmdversion},
} }
// main takes care of OS-specific startup and dispatches to xmain. // main takes care of OS-specific startup and dispatches to xmain.
...@@ -172,17 +169,15 @@ func xmain() { ...@@ -172,17 +169,15 @@ func xmain() {
} }
cmd := os.Args[1] cmd := os.Args[1]
os.Args = os.Args[1:] // for flag parsing during cmd os.Args = os.Args[1:] // for flag parsing during cmd
for _, ct := range cmdtab { flag.Usage = func() {
if ct.name == cmd { fmt.Fprintf(os.Stderr, "usage: go tool dist %s [options]\n", cmd)
flag.Usage = func() { flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "usage: go tool dist %s [options]\n", cmd) os.Exit(2)
flag.PrintDefaults() }
os.Exit(2) if f, ok := commands[cmd]; ok {
} f()
ct.f() } else {
return xprintf("unknown command %s\n", cmd)
} usage()
} }
xprintf("unknown command %s\n", cmd)
usage()
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment