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