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 1.4.
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.
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.
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{
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/...",
64 "internal/goexperiment",
69 "internal/unsafeheader",
77 // File prefixes that are ignored by go/build anyway, and cause
78 // problems with editor generated temporary files (#18931).
79 var ignorePrefixes = []string{
85 // File suffixes that use build tags introduced since Go 1.4.
86 // These must not be copied into the bootstrap build directory.
87 // Also ignore test files.
88 var ignoreSuffixes = []string{
101 var tryDirs = []string{
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) {
117 xprintf("Building Go toolchain1 using %s.\n", goroot_bootstrap)
119 mkbuildcfg(pathf("%s/src/internal/buildcfg/zbootstrap.go", goroot))
120 mkobjabi(pathf("%s/src/cmd/internal/objabi/zbootstrap.go", goroot))
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)
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 {
140 fatalf("walking bootstrap dirs failed: %v: %v", path, err)
143 name := filepath.Base(path)
144 src := pathf("%s/src/%s", goroot, path)
145 dst := pathf("%s/%s", base, path)
148 if !recurse && path != dir || name == "testdata" {
149 return filepath.SkipDir
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))
162 for _, pre := range ignorePrefixes {
163 if strings.HasPrefix(name, pre) {
167 for _, suf := range ignoreSuffixes {
168 if strings.HasSuffix(name, suf) {
173 text := bootstrapRewriteFile(src)
174 writefile(text, dst, 0)
179 // Set up environment for invoking Go 1.4 go command.
180 // GOROOT points at Go 1.4 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 1.4 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.
189 defer os.Setenv("GOROOT", os.Getenv("GOROOT"))
190 os.Setenv("GOROOT", goroot_bootstrap)
192 defer os.Setenv("GOPATH", os.Getenv("GOPATH"))
193 os.Setenv("GOPATH", workspace)
195 defer os.Setenv("GOBIN", os.Getenv("GOBIN"))
196 os.Setenv("GOBIN", "")
198 os.Setenv("GOOS", "")
199 os.Setenv("GOHOSTOS", "")
200 os.Setenv("GOARCH", "")
201 os.Setenv("GOHOSTARCH", "")
203 // Run Go 1.4 to build binaries. Use -gcflags=-l to disable inlining to
204 // workaround bugs in Go 1.4's compiler. See discussion thread:
205 // https://groups.google.com/d/msg/golang-dev/Ss7mCKsvk8w/Gsq7VYI0AwAJ
206 // Use the math_big_pure_go build tag to disable the assembly in math/big
207 // which may contain unsupported instructions.
208 // Use the purego build tag to disable other assembly code,
209 // such as in cmd/internal/notsha256.
210 // Note that if we are using Go 1.10 or later as bootstrap, the -gcflags=-l
211 // only applies to the final cmd/go binary, but that's OK: if this is Go 1.10
212 // or later we don't need to disable inlining to work around bugs in the Go 1.4 compiler.
214 pathf("%s/bin/go", goroot_bootstrap),
217 "-tags=math_big_pure_go compiler_bootstrap purego",
220 cmd = append(cmd, "-v")
222 if tool := os.Getenv("GOBOOTSTRAP_TOOLEXEC"); tool != "" {
223 cmd = append(cmd, "-toolexec="+tool)
225 cmd = append(cmd, "bootstrap/cmd/...")
226 run(base, ShowOutput|CheckExit, cmd...)
228 // Copy binaries into tool binary directory.
229 for _, name := range bootstrapDirs {
230 if !strings.HasPrefix(name, "cmd/") {
233 name = name[len("cmd/"):]
234 if !strings.Contains(name, "/") {
235 copyfile(pathf("%s/%s%s", tooldir, name, exe), pathf("%s/bin/%s%s", workspace, name, exe), writeExec)
244 var ssaRewriteFileSubstring = filepath.FromSlash("src/cmd/compile/internal/ssa/rewrite")
246 // isUnneededSSARewriteFile reports whether srcFile is a
247 // src/cmd/compile/internal/ssa/rewriteARCHNAME.go file for an
248 // architecture that isn't for the given GOARCH.
250 // When unneeded is true archCaps is the rewrite base filename without
251 // the "rewrite" prefix or ".go" suffix: AMD64, 386, ARM, ARM64, etc.
252 func isUnneededSSARewriteFile(srcFile, goArch string) (archCaps string, unneeded bool) {
253 if !strings.Contains(srcFile, ssaRewriteFileSubstring) {
256 fileArch := strings.TrimSuffix(strings.TrimPrefix(filepath.Base(srcFile), "rewrite"), ".go")
261 if b == '_' || ('a' <= b && b <= 'z') {
265 fileArch = strings.ToLower(fileArch)
266 fileArch = strings.TrimSuffix(fileArch, "splitload")
267 if fileArch == goArch {
270 if fileArch == strings.TrimSuffix(goArch, "le") {
273 return archCaps, true
276 func bootstrapRewriteFile(srcFile string) string {
277 // During bootstrap, generate dummy rewrite files for
278 // irrelevant architectures. We only need to build a bootstrap
279 // binary that works for the current gohostarch.
280 // This saves 6+ seconds of bootstrap.
281 if archCaps, ok := isUnneededSSARewriteFile(srcFile, gohostarch); ok {
282 return fmt.Sprintf(`// Code generated by go tool dist; DO NOT EDIT.
286 func rewriteValue%s(v *Value) bool { panic("unused during bootstrap") }
287 func rewriteBlock%s(b *Block) bool { panic("unused during bootstrap") }
288 `, archCaps, archCaps)
291 return bootstrapFixImports(srcFile)
294 func bootstrapFixImports(srcFile string) string {
295 text := readfile(srcFile)
296 if !strings.Contains(srcFile, "/cmd/") && !strings.Contains(srcFile, `\cmd\`) {
297 text = regexp.MustCompile(`\bany\b`).ReplaceAllString(text, "interface{}")
299 lines := strings.SplitAfter(text, "\n")
301 for i, line := range lines {
302 if strings.HasPrefix(line, "import (") {
306 if inBlock && strings.HasPrefix(line, ")") {
310 if strings.HasPrefix(line, `import "`) || strings.HasPrefix(line, `import . "`) ||
311 inBlock && (strings.HasPrefix(line, "\t\"") || strings.HasPrefix(line, "\t. \"") || strings.HasPrefix(line, "\texec \"")) {
312 line = strings.Replace(line, `"cmd/`, `"bootstrap/cmd/`, -1)
313 for _, dir := range bootstrapDirs {
314 if strings.HasPrefix(dir, "cmd/") {
317 line = strings.Replace(line, `"`+dir+`"`, `"bootstrap/`+dir+`"`, -1)
323 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 return strings.Join(lines, "")