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