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>
defaultcxxtarget string
defaultcctarget string
defaultpkgconfigtarget string
- rebuildall bool
- defaultclang bool
+
+ rebuildall bool
+ defaultclang bool
vflag int // verbosity
)
}
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 == "" {
}
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)
if b != "" {
gohostarch = b
}
-
if find(gohostarch, okgoarch) < 0 {
fatal("unknown $GOHOSTARCH %s", gohostarch)
}
// 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
}
if !filepath.IsAbs(gitDir) {
gitDir = filepath.Join(goroot, gitDir)
}
- fi, err := os.Stat(gitDir)
- return err == nil && fi.IsDir()
+ return isdir(gitDir)
}
/*
"bytes"
"fmt"
"os"
+ "path/filepath"
"sort"
+ "strings"
)
/*
"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)
}
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)
}
}
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)
}
}
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.
`, 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 (") {
)
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.
}
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()
}