From: Marvin Stenger Date: Fri, 1 Sep 2017 16:17:10 +0000 (+0200) Subject: cmd/dist: use bytes.Buffer for code generation X-Git-Tag: go1.10beta1~1059 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=88ced021907fb96d5609a3c63db0d9738bf0ac2b;p=gostls13.git 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 TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- diff --git a/src/cmd/dist/buildgo.go b/src/cmd/dist/buildgo.go index f3e739b778..60cb24b6b5 100644 --- a/src/cmd/dist/buildgo.go +++ b/src/cmd/dist/buildgo.go @@ -27,31 +27,29 @@ import ( // It is invoked to write cmd/go/internal/cfg/zdefaultcc.go // but we also write cmd/cgo/zdefaultcc.go func mkzdefaultcc(dir, file string) { - outGo := fmt.Sprintf( - "// Code generated by go tool dist; DO NOT EDIT.\n"+ - "\n"+ - "package cfg\n"+ - "\n"+ - "const DefaultCC = `%s`\n"+ - "const DefaultCXX = `%s`\n"+ - "const DefaultPkgConfig = `%s`\n", - defaultcctarget, defaultcxxtarget, defaultpkgconfigtarget) + var buf bytes.Buffer + fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n") + fmt.Fprintln(&buf) + fmt.Fprintf(&buf, "package cfg\n") + fmt.Fprintln(&buf) + fmt.Fprintf(&buf, "const DefaultCC = `%s`\n", defaultcctarget) + fmt.Fprintf(&buf, "const DefaultCXX = `%s`\n", defaultcxxtarget) + fmt.Fprintf(&buf, "const DefaultPkgConfig = `%s`\n", defaultpkgconfigtarget) - writefile(outGo, file, writeSkipSame) + writefile(buf.String(), file, writeSkipSame) + buf.Reset() - // Convert file name to replace: turn go/internal/cfg into cgo. - outCgo := fmt.Sprintf( - "// Code generated by go tool dist; DO NOT EDIT.\n"+ - "\n"+ - "package main\n"+ - "\n"+ - "const defaultCC = `%s`\n"+ - "const defaultCXX = `%s`\n"+ - "const defaultPkgConfig = `%s`\n", - defaultcctarget, defaultcxxtarget, defaultpkgconfigtarget) + fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n") + fmt.Fprintln(&buf) + fmt.Fprintf(&buf, "package main\n") + fmt.Fprintln(&buf) + fmt.Fprintf(&buf, "const defaultCC = `%s`\n", defaultcctarget) + fmt.Fprintf(&buf, "const defaultCXX = `%s`\n", defaultcxxtarget) + fmt.Fprintf(&buf, "const defaultPkgConfig = `%s`\n", defaultpkgconfigtarget) + // Convert file name. 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. @@ -92,14 +90,13 @@ func mkzcgo(dir, file string) { sort.Strings(list) var buf bytes.Buffer - - fmt.Fprintf(&buf, - "// Code generated by go tool dist; DO NOT EDIT.\n"+ - "\n"+ - "package build\n"+ - "\n"+ - "const defaultCGO_ENABLED = %q\n\n"+ - "var cgoEnabled = map[string]bool{\n", os.Getenv("CGO_ENABLED")) + fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n") + fmt.Fprintln(&buf) + fmt.Fprintf(&buf, "package build\n") + fmt.Fprintln(&buf) + fmt.Fprintf(&buf, "const defaultCGO_ENABLED = %q\n", os.Getenv("CGO_ENABLED")) + fmt.Fprintln(&buf) + fmt.Fprintf(&buf, "var cgoEnabled = map[string]bool{\n") for _, plat := range list { fmt.Fprintf(&buf, "\t%q: true,\n", plat) } diff --git a/src/cmd/dist/buildruntime.go b/src/cmd/dist/buildruntime.go index 836b2ef31c..8dd095b82d 100644 --- a/src/cmd/dist/buildruntime.go +++ b/src/cmd/dist/buildruntime.go @@ -5,6 +5,7 @@ package main import ( + "bytes" "fmt" "os" "strings" @@ -24,17 +25,18 @@ import ( // const StackGuardMultiplier = // func mkzversion(dir, file string) { - out := fmt.Sprintf( - "// Code generated by go tool dist; DO NOT EDIT.\n"+ - "\n"+ - "package sys\n"+ - "\n"+ - "var DefaultGoroot = `%s`\n\n"+ - "const TheVersion = `%s`\n"+ - "const Goexperiment = `%s`\n"+ - "const StackGuardMultiplier = %d\n\n", goroot_final, findgoversion(), os.Getenv("GOEXPERIMENT"), stackGuardMultiplier()) + var buf bytes.Buffer + fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n") + fmt.Fprintln(&buf) + fmt.Fprintf(&buf, "package sys\n") + fmt.Fprintln(&buf) + fmt.Fprintf(&buf, "var DefaultGoroot = `%s`\n", goroot_final) + fmt.Fprintln(&buf) + fmt.Fprintf(&buf, "const TheVersion = `%s`\n", findgoversion()) + 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: @@ -61,25 +63,24 @@ func mkzversion(dir, file string) { // This is more useful than having it default to generating objects for the // original target (in this example, a Mac). func mkzbootstrap(file string) { - out := fmt.Sprintf( - "// Code generated by go tool dist; DO NOT EDIT.\n"+ - "\n"+ - "package objabi\n"+ - "\n"+ - "import \"runtime\"\n"+ - "\n"+ - "const defaultGOROOT = `%s`\n"+ - "const defaultGO386 = `%s`\n"+ - "const defaultGOARM = `%s`\n"+ - "const defaultGOOS = runtime.GOOS\n"+ - "const defaultGOARCH = runtime.GOARCH\n"+ - "const defaultGO_EXTLINK_ENABLED = `%s`\n"+ - "const version = `%s`\n"+ - "const stackGuardMultiplier = %d\n"+ - "const goexperiment = `%s`\n", - goroot_final, go386, goarm, goextlinkenabled, findgoversion(), stackGuardMultiplier(), os.Getenv("GOEXPERIMENT")) + var buf bytes.Buffer + fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n") + fmt.Fprintln(&buf) + fmt.Fprintf(&buf, "package objabi\n") + fmt.Fprintln(&buf) + fmt.Fprintf(&buf, "import \"runtime\"\n") + fmt.Fprintln(&buf) + fmt.Fprintf(&buf, "const defaultGOROOT = `%s`\n", goroot_final) + fmt.Fprintf(&buf, "const defaultGO386 = `%s`\n", go386) + fmt.Fprintf(&buf, "const defaultGOARM = `%s`\n", goarm) + fmt.Fprintf(&buf, "const defaultGOOS = runtime.GOOS\n") + fmt.Fprintf(&buf, "const defaultGOARCH = runtime.GOARCH\n") + fmt.Fprintf(&buf, "const defaultGO_EXTLINK_ENABLED = `%s`\n", goextlinkenabled) + fmt.Fprintf(&buf, "const version = `%s`\n", findgoversion()) + fmt.Fprintf(&buf, "const stackGuardMultiplier = %d\n", stackGuardMultiplier()) + fmt.Fprintf(&buf, "const goexperiment = `%s`\n", os.Getenv("GOEXPERIMENT")) - writefile(out, file, writeSkipSame) + writefile(buf.String(), file, writeSkipSame) } // stackGuardMultiplier returns a multiplier to apply to the default