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