]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/dist/buildtool.go
cmd/compile: Enables PGO in Go and performs profile-guided inlining
[gostls13.git] / src / cmd / dist / buildtool.go
1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Build toolchain using Go 1.4.
6 //
7 // The general strategy is to copy the source files we need into
8 // a new GOPATH workspace, adjust import paths appropriately,
9 // invoke the Go 1.4 go command to build those sources,
10 // and then copy the binaries back.
11
12 package main
13
14 import (
15         "fmt"
16         "os"
17         "path/filepath"
18         "regexp"
19         "strings"
20 )
21
22 // bootstrapDirs is a list of directories holding code that must be
23 // compiled with a Go 1.4 toolchain to produce the bootstrapTargets.
24 // All directories in this list are relative to and must be below $GOROOT/src.
25 //
26 // The list has two kinds of entries: names beginning with cmd/ with
27 // no other slashes, which are commands, and other paths, which are packages
28 // supporting the commands. Packages in the standard library can be listed
29 // if a newer copy needs to be substituted for the Go 1.4 copy when used
30 // by the command packages. Paths ending with /... automatically
31 // include all packages within subdirectories as well.
32 // These will be imported during bootstrap as bootstrap/name, like bootstrap/math/big.
33 var bootstrapDirs = []string{
34         "cmd/asm",
35         "cmd/asm/internal/...",
36         "cmd/cgo",
37         "cmd/compile",
38         "cmd/compile/internal/...",
39         "cmd/internal/archive",
40         "cmd/internal/bio",
41         "cmd/internal/codesign",
42         "cmd/internal/dwarf",
43         "cmd/internal/edit",
44         "cmd/internal/gcprog",
45         "cmd/internal/goobj",
46         "cmd/internal/notsha256",
47         "cmd/internal/obj/...",
48         "cmd/internal/objabi",
49         "cmd/internal/pkgpath",
50         "cmd/internal/quoted",
51         "cmd/internal/src",
52         "cmd/internal/sys",
53         "cmd/link",
54         "cmd/link/internal/...",
55         "compress/flate",
56         "compress/zlib",
57         "container/heap",
58         "debug/dwarf",
59         "debug/elf",
60         "debug/macho",
61         "debug/pe",
62         "go/constant",
63         "internal/coverage",
64         "internal/buildcfg",
65         "internal/goexperiment",
66         "internal/goroot",
67         "internal/goversion",
68         "internal/pkgbits",
69         "internal/profile",
70         "internal/race",
71         "internal/saferio",
72         "internal/platform",
73         "internal/types/errors",
74         "internal/unsafeheader",
75         "internal/xcoff",
76         "math/big",
77         "math/bits",
78         "sort",
79         "strconv",
80 }
81
82 // File prefixes that are ignored by go/build anyway, and cause
83 // problems with editor generated temporary files (#18931).
84 var ignorePrefixes = []string{
85         ".",
86         "_",
87         "#",
88 }
89
90 // File suffixes that use build tags introduced since Go 1.4.
91 // These must not be copied into the bootstrap build directory.
92 // Also ignore test files.
93 var ignoreSuffixes = []string{
94         "_arm64.s",
95         "_arm64.go",
96         "_loong64.s",
97         "_loong64.go",
98         "_riscv64.s",
99         "_riscv64.go",
100         "_wasm.s",
101         "_wasm.go",
102         "_test.s",
103         "_test.go",
104 }
105
106 var tryDirs = []string{
107         "sdk/go1.17",
108         "go1.17",
109 }
110
111 func bootstrapBuildTools() {
112         goroot_bootstrap := os.Getenv("GOROOT_BOOTSTRAP")
113         if goroot_bootstrap == "" {
114                 home := os.Getenv("HOME")
115                 goroot_bootstrap = pathf("%s/go1.4", home)
116                 for _, d := range tryDirs {
117                         if p := pathf("%s/%s", home, d); isdir(p) {
118                                 goroot_bootstrap = p
119                         }
120                 }
121         }
122         xprintf("Building Go toolchain1 using %s.\n", goroot_bootstrap)
123
124         mkbuildcfg(pathf("%s/src/internal/buildcfg/zbootstrap.go", goroot))
125         mkobjabi(pathf("%s/src/cmd/internal/objabi/zbootstrap.go", goroot))
126
127         // Use $GOROOT/pkg/bootstrap as the bootstrap workspace root.
128         // We use a subdirectory of $GOROOT/pkg because that's the
129         // space within $GOROOT where we store all generated objects.
130         // We could use a temporary directory outside $GOROOT instead,
131         // but it is easier to debug on failure if the files are in a known location.
132         workspace := pathf("%s/pkg/bootstrap", goroot)
133         xremoveall(workspace)
134         xatexit(func() { xremoveall(workspace) })
135         base := pathf("%s/src/bootstrap", workspace)
136         xmkdirall(base)
137
138         // Copy source code into $GOROOT/pkg/bootstrap and rewrite import paths.
139         writefile("module bootstrap\n", pathf("%s/%s", base, "go.mod"), 0)
140         for _, dir := range bootstrapDirs {
141                 recurse := strings.HasSuffix(dir, "/...")
142                 dir = strings.TrimSuffix(dir, "/...")
143                 filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
144                         if err != nil {
145                                 fatalf("walking bootstrap dirs failed: %v: %v", path, err)
146                         }
147
148                         name := filepath.Base(path)
149                         src := pathf("%s/src/%s", goroot, path)
150                         dst := pathf("%s/%s", base, path)
151
152                         if info.IsDir() {
153                                 if !recurse && path != dir || name == "testdata" {
154                                         return filepath.SkipDir
155                                 }
156
157                                 xmkdirall(dst)
158                                 if path == "cmd/cgo" {
159                                         // Write to src because we need the file both for bootstrap
160                                         // and for later in the main build.
161                                         mkzdefaultcc("", pathf("%s/zdefaultcc.go", src))
162                                         mkzdefaultcc("", pathf("%s/zdefaultcc.go", dst))
163                                 }
164                                 return nil
165                         }
166
167                         for _, pre := range ignorePrefixes {
168                                 if strings.HasPrefix(name, pre) {
169                                         return nil
170                                 }
171                         }
172                         for _, suf := range ignoreSuffixes {
173                                 if strings.HasSuffix(name, suf) {
174                                         return nil
175                                 }
176                         }
177
178                         text := bootstrapRewriteFile(src)
179                         writefile(text, dst, 0)
180                         return nil
181                 })
182         }
183
184         // Set up environment for invoking Go 1.4 go command.
185         // GOROOT points at Go 1.4 GOROOT,
186         // GOPATH points at our bootstrap workspace,
187         // GOBIN is empty, so that binaries are installed to GOPATH/bin,
188         // and GOOS, GOHOSTOS, GOARCH, and GOHOSTOS are empty,
189         // so that Go 1.4 builds whatever kind of binary it knows how to build.
190         // Restore GOROOT, GOPATH, and GOBIN when done.
191         // Don't bother with GOOS, GOHOSTOS, GOARCH, and GOHOSTARCH,
192         // because setup will take care of those when bootstrapBuildTools returns.
193
194         defer os.Setenv("GOROOT", os.Getenv("GOROOT"))
195         os.Setenv("GOROOT", goroot_bootstrap)
196
197         defer os.Setenv("GOPATH", os.Getenv("GOPATH"))
198         os.Setenv("GOPATH", workspace)
199
200         defer os.Setenv("GOBIN", os.Getenv("GOBIN"))
201         os.Setenv("GOBIN", "")
202
203         os.Setenv("GOOS", "")
204         os.Setenv("GOHOSTOS", "")
205         os.Setenv("GOARCH", "")
206         os.Setenv("GOHOSTARCH", "")
207
208         // Run Go 1.4 to build binaries. Use -gcflags=-l to disable inlining to
209         // workaround bugs in Go 1.4's compiler. See discussion thread:
210         // https://groups.google.com/d/msg/golang-dev/Ss7mCKsvk8w/Gsq7VYI0AwAJ
211         // Use the math_big_pure_go build tag to disable the assembly in math/big
212         // which may contain unsupported instructions.
213         // Use the purego build tag to disable other assembly code,
214         // such as in cmd/internal/notsha256.
215         // Note that if we are using Go 1.10 or later as bootstrap, the -gcflags=-l
216         // only applies to the final cmd/go binary, but that's OK: if this is Go 1.10
217         // or later we don't need to disable inlining to work around bugs in the Go 1.4 compiler.
218         cmd := []string{
219                 pathf("%s/bin/go", goroot_bootstrap),
220                 "install",
221                 "-gcflags=-l",
222                 "-tags=math_big_pure_go compiler_bootstrap purego",
223         }
224         if vflag > 0 {
225                 cmd = append(cmd, "-v")
226         }
227         if tool := os.Getenv("GOBOOTSTRAP_TOOLEXEC"); tool != "" {
228                 cmd = append(cmd, "-toolexec="+tool)
229         }
230         cmd = append(cmd, "bootstrap/cmd/...")
231         run(base, ShowOutput|CheckExit, cmd...)
232
233         // Copy binaries into tool binary directory.
234         for _, name := range bootstrapDirs {
235                 if !strings.HasPrefix(name, "cmd/") {
236                         continue
237                 }
238                 name = name[len("cmd/"):]
239                 if !strings.Contains(name, "/") {
240                         copyfile(pathf("%s/%s%s", tooldir, name, exe), pathf("%s/bin/%s%s", workspace, name, exe), writeExec)
241                 }
242         }
243
244         if vflag > 0 {
245                 xprintf("\n")
246         }
247 }
248
249 var ssaRewriteFileSubstring = filepath.FromSlash("src/cmd/compile/internal/ssa/rewrite")
250
251 // isUnneededSSARewriteFile reports whether srcFile is a
252 // src/cmd/compile/internal/ssa/rewriteARCHNAME.go file for an
253 // architecture that isn't for the given GOARCH.
254 //
255 // When unneeded is true archCaps is the rewrite base filename without
256 // the "rewrite" prefix or ".go" suffix: AMD64, 386, ARM, ARM64, etc.
257 func isUnneededSSARewriteFile(srcFile, goArch string) (archCaps string, unneeded bool) {
258         if !strings.Contains(srcFile, ssaRewriteFileSubstring) {
259                 return "", false
260         }
261         fileArch := strings.TrimSuffix(strings.TrimPrefix(filepath.Base(srcFile), "rewrite"), ".go")
262         if fileArch == "" {
263                 return "", false
264         }
265         b := fileArch[0]
266         if b == '_' || ('a' <= b && b <= 'z') {
267                 return "", false
268         }
269         archCaps = fileArch
270         fileArch = strings.ToLower(fileArch)
271         fileArch = strings.TrimSuffix(fileArch, "splitload")
272         fileArch = strings.TrimSuffix(fileArch, "latelower")
273         if fileArch == goArch {
274                 return "", false
275         }
276         if fileArch == strings.TrimSuffix(goArch, "le") {
277                 return "", false
278         }
279         return archCaps, true
280 }
281
282 func bootstrapRewriteFile(srcFile string) string {
283         // During bootstrap, generate dummy rewrite files for
284         // irrelevant architectures. We only need to build a bootstrap
285         // binary that works for the current gohostarch.
286         // This saves 6+ seconds of bootstrap.
287         if archCaps, ok := isUnneededSSARewriteFile(srcFile, gohostarch); ok {
288                 return fmt.Sprintf(`// Code generated by go tool dist; DO NOT EDIT.
289
290 package ssa
291
292 func rewriteValue%s(v *Value) bool { panic("unused during bootstrap") }
293 func rewriteBlock%s(b *Block) bool { panic("unused during bootstrap") }
294 `, archCaps, archCaps)
295         }
296
297         return bootstrapFixImports(srcFile)
298 }
299
300 func bootstrapFixImports(srcFile string) string {
301         text := readfile(srcFile)
302         if !strings.Contains(srcFile, "/cmd/") && !strings.Contains(srcFile, `\cmd\`) {
303                 text = regexp.MustCompile(`\bany\b`).ReplaceAllString(text, "interface{}")
304         }
305         lines := strings.SplitAfter(text, "\n")
306         inBlock := false
307         for i, line := range lines {
308                 if strings.HasPrefix(line, "import (") {
309                         inBlock = true
310                         continue
311                 }
312                 if inBlock && strings.HasPrefix(line, ")") {
313                         inBlock = false
314                         continue
315                 }
316                 if strings.HasPrefix(line, `import "`) || strings.HasPrefix(line, `import . "`) ||
317                         inBlock && (strings.HasPrefix(line, "\t\"") || strings.HasPrefix(line, "\t. \"") || strings.HasPrefix(line, "\texec \"")) {
318                         line = strings.Replace(line, `"cmd/`, `"bootstrap/cmd/`, -1)
319                         for _, dir := range bootstrapDirs {
320                                 if strings.HasPrefix(dir, "cmd/") {
321                                         continue
322                                 }
323                                 line = strings.Replace(line, `"`+dir+`"`, `"bootstrap/`+dir+`"`, -1)
324                         }
325                         lines[i] = line
326                 }
327         }
328
329         lines[0] = "// Code generated by go tool dist; DO NOT EDIT.\n// This is a bootstrap copy of " + srcFile + "\n\n//line " + srcFile + ":1\n" + lines[0]
330
331         return strings.Join(lines, "")
332 }