]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/dist/buildtool.go
cmd/compile: add coverage fixup mode
[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/goversion",
67         "internal/pkgbits",
68         "internal/race",
69         "internal/saferio",
70         "internal/unsafeheader",
71         "internal/xcoff",
72         "math/big",
73         "math/bits",
74         "sort",
75         "strconv",
76 }
77
78 // File prefixes that are ignored by go/build anyway, and cause
79 // problems with editor generated temporary files (#18931).
80 var ignorePrefixes = []string{
81         ".",
82         "_",
83         "#",
84 }
85
86 // File suffixes that use build tags introduced since Go 1.4.
87 // These must not be copied into the bootstrap build directory.
88 // Also ignore test files.
89 var ignoreSuffixes = []string{
90         "_arm64.s",
91         "_arm64.go",
92         "_loong64.s",
93         "_loong64.go",
94         "_riscv64.s",
95         "_riscv64.go",
96         "_wasm.s",
97         "_wasm.go",
98         "_test.s",
99         "_test.go",
100 }
101
102 var tryDirs = []string{
103         "sdk/go1.17",
104         "go1.17",
105 }
106
107 func bootstrapBuildTools() {
108         goroot_bootstrap := os.Getenv("GOROOT_BOOTSTRAP")
109         if goroot_bootstrap == "" {
110                 home := os.Getenv("HOME")
111                 goroot_bootstrap = pathf("%s/go1.4", home)
112                 for _, d := range tryDirs {
113                         if p := pathf("%s/%s", home, d); isdir(p) {
114                                 goroot_bootstrap = p
115                         }
116                 }
117         }
118         xprintf("Building Go toolchain1 using %s.\n", goroot_bootstrap)
119
120         mkbuildcfg(pathf("%s/src/internal/buildcfg/zbootstrap.go", goroot))
121         mkobjabi(pathf("%s/src/cmd/internal/objabi/zbootstrap.go", goroot))
122
123         // Use $GOROOT/pkg/bootstrap as the bootstrap workspace root.
124         // We use a subdirectory of $GOROOT/pkg because that's the
125         // space within $GOROOT where we store all generated objects.
126         // We could use a temporary directory outside $GOROOT instead,
127         // but it is easier to debug on failure if the files are in a known location.
128         workspace := pathf("%s/pkg/bootstrap", goroot)
129         xremoveall(workspace)
130         xatexit(func() { xremoveall(workspace) })
131         base := pathf("%s/src/bootstrap", workspace)
132         xmkdirall(base)
133
134         // Copy source code into $GOROOT/pkg/bootstrap and rewrite import paths.
135         writefile("module bootstrap\n", pathf("%s/%s", base, "go.mod"), 0)
136         for _, dir := range bootstrapDirs {
137                 recurse := strings.HasSuffix(dir, "/...")
138                 dir = strings.TrimSuffix(dir, "/...")
139                 filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
140                         if err != nil {
141                                 fatalf("walking bootstrap dirs failed: %v: %v", path, err)
142                         }
143
144                         name := filepath.Base(path)
145                         src := pathf("%s/src/%s", goroot, path)
146                         dst := pathf("%s/%s", base, path)
147
148                         if info.IsDir() {
149                                 if !recurse && path != dir || name == "testdata" {
150                                         return filepath.SkipDir
151                                 }
152
153                                 xmkdirall(dst)
154                                 if path == "cmd/cgo" {
155                                         // Write to src because we need the file both for bootstrap
156                                         // and for later in the main build.
157                                         mkzdefaultcc("", pathf("%s/zdefaultcc.go", src))
158                                         mkzdefaultcc("", pathf("%s/zdefaultcc.go", dst))
159                                 }
160                                 return nil
161                         }
162
163                         for _, pre := range ignorePrefixes {
164                                 if strings.HasPrefix(name, pre) {
165                                         return nil
166                                 }
167                         }
168                         for _, suf := range ignoreSuffixes {
169                                 if strings.HasSuffix(name, suf) {
170                                         return nil
171                                 }
172                         }
173
174                         text := bootstrapRewriteFile(src)
175                         writefile(text, dst, 0)
176                         return nil
177                 })
178         }
179
180         // Set up environment for invoking Go 1.4 go command.
181         // GOROOT points at Go 1.4 GOROOT,
182         // GOPATH points at our bootstrap workspace,
183         // GOBIN is empty, so that binaries are installed to GOPATH/bin,
184         // and GOOS, GOHOSTOS, GOARCH, and GOHOSTOS are empty,
185         // so that Go 1.4 builds whatever kind of binary it knows how to build.
186         // Restore GOROOT, GOPATH, and GOBIN when done.
187         // Don't bother with GOOS, GOHOSTOS, GOARCH, and GOHOSTARCH,
188         // because setup will take care of those when bootstrapBuildTools returns.
189
190         defer os.Setenv("GOROOT", os.Getenv("GOROOT"))
191         os.Setenv("GOROOT", goroot_bootstrap)
192
193         defer os.Setenv("GOPATH", os.Getenv("GOPATH"))
194         os.Setenv("GOPATH", workspace)
195
196         defer os.Setenv("GOBIN", os.Getenv("GOBIN"))
197         os.Setenv("GOBIN", "")
198
199         os.Setenv("GOOS", "")
200         os.Setenv("GOHOSTOS", "")
201         os.Setenv("GOARCH", "")
202         os.Setenv("GOHOSTARCH", "")
203
204         // Run Go 1.4 to build binaries. Use -gcflags=-l to disable inlining to
205         // workaround bugs in Go 1.4's compiler. See discussion thread:
206         // https://groups.google.com/d/msg/golang-dev/Ss7mCKsvk8w/Gsq7VYI0AwAJ
207         // Use the math_big_pure_go build tag to disable the assembly in math/big
208         // which may contain unsupported instructions.
209         // Use the purego build tag to disable other assembly code,
210         // such as in cmd/internal/notsha256.
211         // Note that if we are using Go 1.10 or later as bootstrap, the -gcflags=-l
212         // only applies to the final cmd/go binary, but that's OK: if this is Go 1.10
213         // or later we don't need to disable inlining to work around bugs in the Go 1.4 compiler.
214         cmd := []string{
215                 pathf("%s/bin/go", goroot_bootstrap),
216                 "install",
217                 "-gcflags=-l",
218                 "-tags=math_big_pure_go compiler_bootstrap purego",
219         }
220         if vflag > 0 {
221                 cmd = append(cmd, "-v")
222         }
223         if tool := os.Getenv("GOBOOTSTRAP_TOOLEXEC"); tool != "" {
224                 cmd = append(cmd, "-toolexec="+tool)
225         }
226         cmd = append(cmd, "bootstrap/cmd/...")
227         run(base, ShowOutput|CheckExit, cmd...)
228
229         // Copy binaries into tool binary directory.
230         for _, name := range bootstrapDirs {
231                 if !strings.HasPrefix(name, "cmd/") {
232                         continue
233                 }
234                 name = name[len("cmd/"):]
235                 if !strings.Contains(name, "/") {
236                         copyfile(pathf("%s/%s%s", tooldir, name, exe), pathf("%s/bin/%s%s", workspace, name, exe), writeExec)
237                 }
238         }
239
240         if vflag > 0 {
241                 xprintf("\n")
242         }
243 }
244
245 var ssaRewriteFileSubstring = filepath.FromSlash("src/cmd/compile/internal/ssa/rewrite")
246
247 // isUnneededSSARewriteFile reports whether srcFile is a
248 // src/cmd/compile/internal/ssa/rewriteARCHNAME.go file for an
249 // architecture that isn't for the given GOARCH.
250 //
251 // When unneeded is true archCaps is the rewrite base filename without
252 // the "rewrite" prefix or ".go" suffix: AMD64, 386, ARM, ARM64, etc.
253 func isUnneededSSARewriteFile(srcFile, goArch string) (archCaps string, unneeded bool) {
254         if !strings.Contains(srcFile, ssaRewriteFileSubstring) {
255                 return "", false
256         }
257         fileArch := strings.TrimSuffix(strings.TrimPrefix(filepath.Base(srcFile), "rewrite"), ".go")
258         if fileArch == "" {
259                 return "", false
260         }
261         b := fileArch[0]
262         if b == '_' || ('a' <= b && b <= 'z') {
263                 return "", false
264         }
265         archCaps = fileArch
266         fileArch = strings.ToLower(fileArch)
267         fileArch = strings.TrimSuffix(fileArch, "splitload")
268         if fileArch == goArch {
269                 return "", false
270         }
271         if fileArch == strings.TrimSuffix(goArch, "le") {
272                 return "", false
273         }
274         return archCaps, true
275 }
276
277 func bootstrapRewriteFile(srcFile string) string {
278         // During bootstrap, generate dummy rewrite files for
279         // irrelevant architectures. We only need to build a bootstrap
280         // binary that works for the current gohostarch.
281         // This saves 6+ seconds of bootstrap.
282         if archCaps, ok := isUnneededSSARewriteFile(srcFile, gohostarch); ok {
283                 return fmt.Sprintf(`// Code generated by go tool dist; DO NOT EDIT.
284
285 package ssa
286
287 func rewriteValue%s(v *Value) bool { panic("unused during bootstrap") }
288 func rewriteBlock%s(b *Block) bool { panic("unused during bootstrap") }
289 `, archCaps, archCaps)
290         }
291
292         return bootstrapFixImports(srcFile)
293 }
294
295 func bootstrapFixImports(srcFile string) string {
296         text := readfile(srcFile)
297         if !strings.Contains(srcFile, "/cmd/") && !strings.Contains(srcFile, `\cmd\`) {
298                 text = regexp.MustCompile(`\bany\b`).ReplaceAllString(text, "interface{}")
299         }
300         lines := strings.SplitAfter(text, "\n")
301         inBlock := false
302         for i, line := range lines {
303                 if strings.HasPrefix(line, "import (") {
304                         inBlock = true
305                         continue
306                 }
307                 if inBlock && strings.HasPrefix(line, ")") {
308                         inBlock = false
309                         continue
310                 }
311                 if strings.HasPrefix(line, `import "`) || strings.HasPrefix(line, `import . "`) ||
312                         inBlock && (strings.HasPrefix(line, "\t\"") || strings.HasPrefix(line, "\t. \"") || strings.HasPrefix(line, "\texec \"")) {
313                         line = strings.Replace(line, `"cmd/`, `"bootstrap/cmd/`, -1)
314                         for _, dir := range bootstrapDirs {
315                                 if strings.HasPrefix(dir, "cmd/") {
316                                         continue
317                                 }
318                                 line = strings.Replace(line, `"`+dir+`"`, `"bootstrap/`+dir+`"`, -1)
319                         }
320                         lines[i] = line
321                 }
322         }
323
324         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]
325
326         return strings.Join(lines, "")
327 }