1
0
mirror of https://github.com/golang/go synced 2024-09-30 00:24:29 -06:00

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: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Marvin Stenger 2017-08-31 12:50:57 +02:00 committed by Ian Lance Taylor
parent c174e46ae9
commit 3844e707f6
4 changed files with 61 additions and 67 deletions

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

@ -43,8 +43,9 @@ var (
defaultcxxtarget string
defaultcctarget string
defaultpkgconfigtarget string
rebuildall bool
defaultclang bool
rebuildall bool
defaultclang bool
vflag int // verbosity
)
@ -98,10 +99,11 @@ func xinit() {
}
goroot = filepath.Clean(b)
goroot_final = os.Getenv("GOROOT_FINAL")
if goroot_final == "" {
goroot_final = goroot
b = os.Getenv("GOROOT_FINAL")
if b == "" {
b = goroot
}
goroot_final = b
b = os.Getenv("GOBIN")
if b == "" {
@ -134,8 +136,7 @@ func xinit() {
}
go386 = b
p := pathf("%s/src/all.bash", goroot)
if !isfile(p) {
if p := pathf("%s/src/all.bash", goroot); !isfile(p) {
fatal("$GOROOT is not set correctly or not exported\n"+
"\tGOROOT=%s\n"+
"\t%s does not exist", goroot, p)
@ -145,7 +146,6 @@ func xinit() {
if b != "" {
gohostarch = b
}
if find(gohostarch, okgoarch) < 0 {
fatal("unknown $GOHOSTARCH %s", gohostarch)
}
@ -253,20 +253,19 @@ func branchtag(branch string) (tag string, precise bool) {
// 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)
// 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 {
continue
}
i += len(" refs/tags/")
// The tag name ends at a comma or paren (prefer the first).
j := strings.Index(line[i:], ",")
if j < 0 {
j = strings.Index(line[i:], ")")
}
// Trim off known prefix.
line = line[i+len(s):]
// The tag name ends at a comma or paren.
j := strings.IndexAny(line, ",)")
if j < 0 {
continue // malformed line; ignore it
}
tag = line[i : i+j]
tag = line[:j]
if row == 0 {
precise = true // tag denotes HEAD
}
@ -339,8 +338,7 @@ func isGitRepo() bool {
if !filepath.IsAbs(gitDir) {
gitDir = filepath.Join(goroot, gitDir)
}
fi, err := os.Stat(gitDir)
return err == nil && fi.IsDir()
return isdir(gitDir)
}
/*

View File

@ -8,7 +8,9 @@ import (
"bytes"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
)
/*
@ -48,8 +50,7 @@ func mkzdefaultcc(dir, file string) {
"const defaultPkgConfig = `%s`\n",
defaultcctarget, defaultcxxtarget, defaultpkgconfigtarget)
i := len(file) - len("go/internal/cfg/zdefaultcc.go")
file = file[:i] + "cgo/zdefaultcc.go"
file = strings.Replace(file, filepath.FromSlash("go/internal/cfg"), "cgo", 1)
writefile(outCgo, file, writeSkipSame)
}
@ -63,13 +64,14 @@ func mkzosarch(dir, file string) {
sort.Strings(list)
var buf bytes.Buffer
buf.WriteString("// Code generated by go tool dist; DO NOT EDIT.\n\n")
buf.WriteString("package cfg\n\n")
fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n\n")
fmt.Fprintf(&buf, "package cfg\n\n")
fmt.Fprintf(&buf, "var OSArchSupportsCgo = map[string]bool{\n")
for _, plat := range list {
fmt.Fprintf(&buf, "\t%q: %v,\n", plat, cgoEnabled[plat])
}
fmt.Fprintf(&buf, "}\n")
writefile(buf.String(), file, writeSkipSame)
}

View File

@ -128,8 +128,7 @@ func bootstrapBuildTools() {
}
srcFile := pathf("%s/%s", src, name)
dstFile := pathf("%s/%s", dst, name)
text := readfile(srcFile)
text = bootstrapRewriteFile(text, srcFile)
text := bootstrapRewriteFile(srcFile)
writefile(text, dstFile, 0)
}
}
@ -221,7 +220,7 @@ func isUnneededSSARewriteFile(srcFile string) (archCaps string, unneeded bool) {
return archCaps, true
}
func bootstrapRewriteFile(text, srcFile string) string {
func bootstrapRewriteFile(srcFile string) string {
// During bootstrap, generate dummy rewrite files for
// irrelevant architectures. We only need to build a bootstrap
// binary that works for the current runtime.GOARCH.
@ -236,11 +235,11 @@ func rewriteBlock%s(b *Block) bool { panic("unused during bootstrap") }
`, archCaps, archCaps)
}
return bootstrapFixImports(text, srcFile)
return bootstrapFixImports(srcFile)
}
func bootstrapFixImports(text, srcFile string) string {
lines := strings.SplitAfter(text, "\n")
func bootstrapFixImports(srcFile string) string {
lines := strings.SplitAfter(readfile(srcFile), "\n")
inBlock := false
for i, line := range lines {
if strings.HasPrefix(line, "import (") {

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

@ -14,36 +14,33 @@ import (
)
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",
)
xprintf(`usage: go tool dist [command]
Commands are:
banner print installation banner
bootstrap rebuild everything
clean deletes all built files
env [-p] print environment (-p: include $PATH)
install [dir] install individual directory
list [-json] list all supported platforms
test [-h] run Go test(s)
version print Go version
All commands take -v flags to emit extra information.
`)
xexit(2)
}
// cmdtab records the available commands.
var cmdtab = []struct {
name string
f func()
}{
{"banner", cmdbanner},
{"bootstrap", cmdbootstrap},
{"clean", cmdclean},
{"env", cmdenv},
{"install", cmdinstall},
{"list", cmdlist},
{"test", cmdtest},
{"version", cmdversion},
// commands records the available commands.
var commands = map[string]func(){
"banner": cmdbanner,
"bootstrap": cmdbootstrap,
"clean": cmdclean,
"env": cmdenv,
"install": cmdinstall,
"list": cmdlist,
"test": cmdtest,
"version": cmdversion,
}
// main takes care of OS-specific startup and dispatches to xmain.
@ -172,17 +169,15 @@ func xmain() {
}
cmd := os.Args[1]
os.Args = os.Args[1:] // for flag parsing during cmd
for _, ct := range cmdtab {
if ct.name == cmd {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: go tool dist %s [options]\n", cmd)
flag.PrintDefaults()
os.Exit(2)
}
ct.f()
return
}
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: go tool dist %s [options]\n", cmd)
flag.PrintDefaults()
os.Exit(2)
}
if f, ok := commands[cmd]; ok {
f()
} else {
xprintf("unknown command %s\n", cmd)
usage()
}
xprintf("unknown command %s\n", cmd)
usage()
}