]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/dist: use bytes.Buffer for code generation
authorMarvin Stenger <marvin.stenger94@gmail.com>
Fri, 1 Sep 2017 16:17:10 +0000 (18:17 +0200)
committerIan Lance Taylor <iant@golang.org>
Wed, 20 Sep 2017 03:48:42 +0000 (03:48 +0000)
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>
src/cmd/dist/buildgo.go
src/cmd/dist/buildruntime.go

index f3e739b778d7629f947fc1d3ff16c6a6393e3e62..60cb24b6b576cf0617e26af8cb85721a7259a9c0 100644 (file)
@@ -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)
        }
index 836b2ef31c4e4c438da0e70bc88d49cffff78e00..8dd095b82d7d64181e34243c7b11d40e1785e843 100644 (file)
@@ -5,6 +5,7 @@
 package main
 
 import (
+       "bytes"
        "fmt"
        "os"
        "strings"
@@ -24,17 +25,18 @@ import (
 //     const StackGuardMultiplier = <multiplier value>
 //
 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