1
0
mirror of https://github.com/golang/go synced 2024-11-18 01:04:48 -07:00

cmd/dist: use bytes.Buffer for code generation

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

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: I2d5a071eb8e14690325612271432fdc5f43b108b
Reviewed-on: https://go-review.googlesource.com/61014
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-09-01 18:17:10 +02:00 committed by Ian Lance Taylor
parent 822f832d29
commit 88ced02190
2 changed files with 55 additions and 57 deletions

View File

@ -27,31 +27,29 @@ import (
// It is invoked to write cmd/go/internal/cfg/zdefaultcc.go // It is invoked to write cmd/go/internal/cfg/zdefaultcc.go
// but we also write cmd/cgo/zdefaultcc.go // but we also write cmd/cgo/zdefaultcc.go
func mkzdefaultcc(dir, file string) { func mkzdefaultcc(dir, file string) {
outGo := fmt.Sprintf( var buf bytes.Buffer
"// Code generated by go tool dist; DO NOT EDIT.\n"+ fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n")
"\n"+ fmt.Fprintln(&buf)
"package cfg\n"+ fmt.Fprintf(&buf, "package cfg\n")
"\n"+ fmt.Fprintln(&buf)
"const DefaultCC = `%s`\n"+ fmt.Fprintf(&buf, "const DefaultCC = `%s`\n", defaultcctarget)
"const DefaultCXX = `%s`\n"+ fmt.Fprintf(&buf, "const DefaultCXX = `%s`\n", defaultcxxtarget)
"const DefaultPkgConfig = `%s`\n", fmt.Fprintf(&buf, "const DefaultPkgConfig = `%s`\n", defaultpkgconfigtarget)
defaultcctarget, defaultcxxtarget, defaultpkgconfigtarget)
writefile(outGo, file, writeSkipSame) writefile(buf.String(), file, writeSkipSame)
buf.Reset()
// Convert file name to replace: turn go/internal/cfg into cgo. fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n")
outCgo := fmt.Sprintf( fmt.Fprintln(&buf)
"// Code generated by go tool dist; DO NOT EDIT.\n"+ fmt.Fprintf(&buf, "package main\n")
"\n"+ fmt.Fprintln(&buf)
"package main\n"+ fmt.Fprintf(&buf, "const defaultCC = `%s`\n", defaultcctarget)
"\n"+ fmt.Fprintf(&buf, "const defaultCXX = `%s`\n", defaultcxxtarget)
"const defaultCC = `%s`\n"+ fmt.Fprintf(&buf, "const defaultPkgConfig = `%s`\n", defaultpkgconfigtarget)
"const defaultCXX = `%s`\n"+
"const defaultPkgConfig = `%s`\n",
defaultcctarget, defaultcxxtarget, defaultpkgconfigtarget)
// Convert file name.
file = strings.Replace(file, filepath.FromSlash("go/internal/cfg"), "cgo", 1) file = strings.Replace(file, filepath.FromSlash("go/internal/cfg"), "cgo", 1)
writefile(outCgo, file, writeSkipSame) writefile(buf.String(), file, writeSkipSame)
} }
// mkzcgo writes zosarch.go for cmd/go. // mkzcgo writes zosarch.go for cmd/go.
@ -92,14 +90,13 @@ func mkzcgo(dir, file string) {
sort.Strings(list) sort.Strings(list)
var buf bytes.Buffer var buf bytes.Buffer
fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n")
fmt.Fprintf(&buf, fmt.Fprintln(&buf)
"// Code generated by go tool dist; DO NOT EDIT.\n"+ fmt.Fprintf(&buf, "package build\n")
"\n"+ fmt.Fprintln(&buf)
"package build\n"+ fmt.Fprintf(&buf, "const defaultCGO_ENABLED = %q\n", os.Getenv("CGO_ENABLED"))
"\n"+ fmt.Fprintln(&buf)
"const defaultCGO_ENABLED = %q\n\n"+ fmt.Fprintf(&buf, "var cgoEnabled = map[string]bool{\n")
"var cgoEnabled = map[string]bool{\n", os.Getenv("CGO_ENABLED"))
for _, plat := range list { for _, plat := range list {
fmt.Fprintf(&buf, "\t%q: true,\n", plat) fmt.Fprintf(&buf, "\t%q: true,\n", plat)
} }

View File

@ -5,6 +5,7 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"os" "os"
"strings" "strings"
@ -24,17 +25,18 @@ import (
// const StackGuardMultiplier = <multiplier value> // const StackGuardMultiplier = <multiplier value>
// //
func mkzversion(dir, file string) { func mkzversion(dir, file string) {
out := fmt.Sprintf( var buf bytes.Buffer
"// Code generated by go tool dist; DO NOT EDIT.\n"+ fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n")
"\n"+ fmt.Fprintln(&buf)
"package sys\n"+ fmt.Fprintf(&buf, "package sys\n")
"\n"+ fmt.Fprintln(&buf)
"var DefaultGoroot = `%s`\n\n"+ fmt.Fprintf(&buf, "var DefaultGoroot = `%s`\n", goroot_final)
"const TheVersion = `%s`\n"+ fmt.Fprintln(&buf)
"const Goexperiment = `%s`\n"+ fmt.Fprintf(&buf, "const TheVersion = `%s`\n", findgoversion())
"const StackGuardMultiplier = %d\n\n", goroot_final, findgoversion(), os.Getenv("GOEXPERIMENT"), stackGuardMultiplier()) fmt.Fprintf(&buf, "const Goexperiment = `%s`\n", os.Getenv("GOEXPERIMENT"))
fmt.Fprintf(&buf, "const StackGuardMultiplier = %d\n", stackGuardMultiplier())
writefile(out, file, writeSkipSame) writefile(buf.String(), file, writeSkipSame)
} }
// mkzbootstrap writes cmd/internal/objabi/zbootstrap.go: // mkzbootstrap writes cmd/internal/objabi/zbootstrap.go:
@ -61,25 +63,24 @@ func mkzversion(dir, file string) {
// This is more useful than having it default to generating objects for the // This is more useful than having it default to generating objects for the
// original target (in this example, a Mac). // original target (in this example, a Mac).
func mkzbootstrap(file string) { func mkzbootstrap(file string) {
out := fmt.Sprintf( var buf bytes.Buffer
"// Code generated by go tool dist; DO NOT EDIT.\n"+ fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n")
"\n"+ fmt.Fprintln(&buf)
"package objabi\n"+ fmt.Fprintf(&buf, "package objabi\n")
"\n"+ fmt.Fprintln(&buf)
"import \"runtime\"\n"+ fmt.Fprintf(&buf, "import \"runtime\"\n")
"\n"+ fmt.Fprintln(&buf)
"const defaultGOROOT = `%s`\n"+ fmt.Fprintf(&buf, "const defaultGOROOT = `%s`\n", goroot_final)
"const defaultGO386 = `%s`\n"+ fmt.Fprintf(&buf, "const defaultGO386 = `%s`\n", go386)
"const defaultGOARM = `%s`\n"+ fmt.Fprintf(&buf, "const defaultGOARM = `%s`\n", goarm)
"const defaultGOOS = runtime.GOOS\n"+ fmt.Fprintf(&buf, "const defaultGOOS = runtime.GOOS\n")
"const defaultGOARCH = runtime.GOARCH\n"+ fmt.Fprintf(&buf, "const defaultGOARCH = runtime.GOARCH\n")
"const defaultGO_EXTLINK_ENABLED = `%s`\n"+ fmt.Fprintf(&buf, "const defaultGO_EXTLINK_ENABLED = `%s`\n", goextlinkenabled)
"const version = `%s`\n"+ fmt.Fprintf(&buf, "const version = `%s`\n", findgoversion())
"const stackGuardMultiplier = %d\n"+ fmt.Fprintf(&buf, "const stackGuardMultiplier = %d\n", stackGuardMultiplier())
"const goexperiment = `%s`\n", fmt.Fprintf(&buf, "const goexperiment = `%s`\n", os.Getenv("GOEXPERIMENT"))
goroot_final, go386, goarm, goextlinkenabled, findgoversion(), stackGuardMultiplier(), os.Getenv("GOEXPERIMENT"))
writefile(out, file, writeSkipSame) writefile(buf.String(), file, writeSkipSame)
} }
// stackGuardMultiplier returns a multiplier to apply to the default // stackGuardMultiplier returns a multiplier to apply to the default