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.
5 // Build toolchain using Go bootstrap version.
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.
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.
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{
35 "cmd/asm/internal/...",
38 "cmd/compile/internal/...",
39 "cmd/internal/archive",
41 "cmd/internal/codesign",
44 "cmd/internal/gcprog",
46 "cmd/internal/notsha256",
47 "cmd/internal/obj/...",
48 "cmd/internal/objabi",
49 "cmd/internal/pkgpath",
50 "cmd/internal/quoted",
54 "cmd/link/internal/...",
62 "go/build/constraint",
70 "internal/goexperiment",
73 // internal/lazyregexp is provided by Go 1.17, which permits it to
74 // be imported by other packages in this list, but is not provided
75 // by the Go 1.17 version of gccgo. It's on this list only to
76 // support gccgo, and can be removed if we require gccgo 14 or later.
77 "internal/lazyregexp",
83 "internal/syscall/unix",
84 "internal/types/errors",
85 "internal/unsafeheader",
94 // File prefixes that are ignored by go/build anyway, and cause
95 // problems with editor generated temporary files (#18931).
96 var ignorePrefixes = []string{
102 // File suffixes that use build tags introduced since Go 1.17.
103 // These must not be copied into the bootstrap build directory.
104 // Also ignore test files.
105 var ignoreSuffixes = []string{
108 // Skip PGO profile. No need to build toolchain1 compiler
109 // with PGO. And as it is not a text file the import path
110 // rewrite will break it.
114 var tryDirs = []string{
119 func bootstrapBuildTools() {
120 goroot_bootstrap := os.Getenv("GOROOT_BOOTSTRAP")
121 if goroot_bootstrap == "" {
122 home := os.Getenv("HOME")
123 goroot_bootstrap = pathf("%s/go1.4", home)
124 for _, d := range tryDirs {
125 if p := pathf("%s/%s", home, d); isdir(p) {
130 xprintf("Building Go toolchain1 using %s.\n", goroot_bootstrap)
132 mkbuildcfg(pathf("%s/src/internal/buildcfg/zbootstrap.go", goroot))
133 mkobjabi(pathf("%s/src/cmd/internal/objabi/zbootstrap.go", goroot))
135 // Use $GOROOT/pkg/bootstrap as the bootstrap workspace root.
136 // We use a subdirectory of $GOROOT/pkg because that's the
137 // space within $GOROOT where we store all generated objects.
138 // We could use a temporary directory outside $GOROOT instead,
139 // but it is easier to debug on failure if the files are in a known location.
140 workspace := pathf("%s/pkg/bootstrap", goroot)
141 xremoveall(workspace)
142 xatexit(func() { xremoveall(workspace) })
143 base := pathf("%s/src/bootstrap", workspace)
146 // Copy source code into $GOROOT/pkg/bootstrap and rewrite import paths.
147 writefile("module bootstrap\ngo 1.20\n", pathf("%s/%s", base, "go.mod"), 0)
148 for _, dir := range bootstrapDirs {
149 recurse := strings.HasSuffix(dir, "/...")
150 dir = strings.TrimSuffix(dir, "/...")
151 filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
153 fatalf("walking bootstrap dirs failed: %v: %v", path, err)
156 name := filepath.Base(path)
157 src := pathf("%s/src/%s", goroot, path)
158 dst := pathf("%s/%s", base, path)
161 if !recurse && path != dir || name == "testdata" {
162 return filepath.SkipDir
166 if path == "cmd/cgo" {
167 // Write to src because we need the file both for bootstrap
168 // and for later in the main build.
169 mkzdefaultcc("", pathf("%s/zdefaultcc.go", src))
170 mkzdefaultcc("", pathf("%s/zdefaultcc.go", dst))
175 for _, pre := range ignorePrefixes {
176 if strings.HasPrefix(name, pre) {
180 for _, suf := range ignoreSuffixes {
181 if strings.HasSuffix(name, suf) {
186 text := bootstrapRewriteFile(src)
187 writefile(text, dst, 0)
192 // Set up environment for invoking Go bootstrap toolchains go command.
193 // GOROOT points at Go bootstrap GOROOT,
194 // GOPATH points at our bootstrap workspace,
195 // GOBIN is empty, so that binaries are installed to GOPATH/bin,
196 // and GOOS, GOHOSTOS, GOARCH, and GOHOSTOS are empty,
197 // so that Go bootstrap toolchain builds whatever kind of binary it knows how to build.
198 // Restore GOROOT, GOPATH, and GOBIN when done.
199 // Don't bother with GOOS, GOHOSTOS, GOARCH, and GOHOSTARCH,
200 // because setup will take care of those when bootstrapBuildTools returns.
202 defer os.Setenv("GOROOT", os.Getenv("GOROOT"))
203 os.Setenv("GOROOT", goroot_bootstrap)
205 defer os.Setenv("GOPATH", os.Getenv("GOPATH"))
206 os.Setenv("GOPATH", workspace)
208 defer os.Setenv("GOBIN", os.Getenv("GOBIN"))
209 os.Setenv("GOBIN", "")
211 os.Setenv("GOOS", "")
212 os.Setenv("GOHOSTOS", "")
213 os.Setenv("GOARCH", "")
214 os.Setenv("GOHOSTARCH", "")
216 // Run Go bootstrap to build binaries.
217 // Use the math_big_pure_go build tag to disable the assembly in math/big
218 // which may contain unsupported instructions.
219 // Use the purego build tag to disable other assembly code,
220 // such as in cmd/internal/notsha256.
222 pathf("%s/bin/go", goroot_bootstrap),
224 "-tags=math_big_pure_go compiler_bootstrap purego",
227 cmd = append(cmd, "-v")
229 if tool := os.Getenv("GOBOOTSTRAP_TOOLEXEC"); tool != "" {
230 cmd = append(cmd, "-toolexec="+tool)
232 cmd = append(cmd, "bootstrap/cmd/...")
233 run(base, ShowOutput|CheckExit, cmd...)
235 // Copy binaries into tool binary directory.
236 for _, name := range bootstrapDirs {
237 if !strings.HasPrefix(name, "cmd/") {
240 name = name[len("cmd/"):]
241 if !strings.Contains(name, "/") {
242 copyfile(pathf("%s/%s%s", tooldir, name, exe), pathf("%s/bin/%s%s", workspace, name, exe), writeExec)
251 var ssaRewriteFileSubstring = filepath.FromSlash("src/cmd/compile/internal/ssa/rewrite")
253 // isUnneededSSARewriteFile reports whether srcFile is a
254 // src/cmd/compile/internal/ssa/rewriteARCHNAME.go file for an
255 // architecture that isn't for the given GOARCH.
257 // When unneeded is true archCaps is the rewrite base filename without
258 // the "rewrite" prefix or ".go" suffix: AMD64, 386, ARM, ARM64, etc.
259 func isUnneededSSARewriteFile(srcFile, goArch string) (archCaps string, unneeded bool) {
260 if !strings.Contains(srcFile, ssaRewriteFileSubstring) {
263 fileArch := strings.TrimSuffix(strings.TrimPrefix(filepath.Base(srcFile), "rewrite"), ".go")
268 if b == '_' || ('a' <= b && b <= 'z') {
272 fileArch = strings.ToLower(fileArch)
273 fileArch = strings.TrimSuffix(fileArch, "splitload")
274 fileArch = strings.TrimSuffix(fileArch, "latelower")
275 if fileArch == goArch {
278 if fileArch == strings.TrimSuffix(goArch, "le") {
281 return archCaps, true
284 func bootstrapRewriteFile(srcFile string) string {
285 // During bootstrap, generate dummy rewrite files for
286 // irrelevant architectures. We only need to build a bootstrap
287 // binary that works for the current gohostarch.
288 // This saves 6+ seconds of bootstrap.
289 if archCaps, ok := isUnneededSSARewriteFile(srcFile, gohostarch); ok {
290 return fmt.Sprintf(`%spackage ssa
292 func rewriteValue%s(v *Value) bool { panic("unused during bootstrap") }
293 func rewriteBlock%s(b *Block) bool { panic("unused during bootstrap") }
294 `, generatedHeader, archCaps, archCaps)
297 return bootstrapFixImports(srcFile)
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{}")
305 lines := strings.SplitAfter(text, "\n")
307 for i, line := range lines {
308 if strings.HasPrefix(line, "import (") {
312 if inBlock && strings.HasPrefix(line, ")") {
316 if strings.HasPrefix(line, `import "`) || strings.HasPrefix(line, `import . "`) ||
317 inBlock && (strings.HasPrefix(line, "\t\"") || strings.HasPrefix(line, "\t. \"") || strings.HasPrefix(line, "\texec \"") || strings.HasPrefix(line, "\trtabi \"")) {
318 line = strings.Replace(line, `"cmd/`, `"bootstrap/cmd/`, -1)
319 for _, dir := range bootstrapDirs {
320 if strings.HasPrefix(dir, "cmd/") {
323 line = strings.Replace(line, `"`+dir+`"`, `"bootstrap/`+dir+`"`, -1)
329 lines[0] = generatedHeader + "// This is a bootstrap copy of " + srcFile + "\n\n//line " + srcFile + ":1\n" + lines[0]
331 return strings.Join(lines, "")