File size: 12,372 Bytes
61bba11 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | // Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Build toolchain using Go bootstrap version.
//
// The general strategy is to copy the source files we need into
// a new GOPATH workspace, adjust import paths appropriately,
// invoke the Go bootstrap toolchains go command to build those sources,
// and then copy the binaries back.
package main
import (
"fmt"
"go/version"
"os"
"path/filepath"
"regexp"
"strings"
)
// bootstrapDirs is a list of directories holding code that must be
// compiled with the Go bootstrap toolchain to produce the bootstrapTargets.
// All directories in this list are relative to and must be below $GOROOT/src.
//
// The list has two kinds of entries: names beginning with cmd/ with
// no other slashes, which are commands, and other paths, which are packages
// supporting the commands. Packages in the standard library can be listed
// if a newer copy needs to be substituted for the Go bootstrap copy when used
// by the command packages. Paths ending with /... automatically
// include all packages within subdirectories as well.
// These will be imported during bootstrap as bootstrap/name, like bootstrap/math/big.
var bootstrapDirs = []string{
"cmp",
"cmd/asm",
"cmd/asm/internal/...",
"cmd/cgo",
"cmd/compile",
"cmd/compile/internal/...",
"cmd/internal/archive",
"cmd/internal/bio",
"cmd/internal/codesign",
"cmd/internal/dwarf",
"cmd/internal/edit",
"cmd/internal/gcprog",
"cmd/internal/goobj",
"cmd/internal/hash",
"cmd/internal/macho",
"cmd/internal/obj/...",
"cmd/internal/objabi",
"cmd/internal/par",
"cmd/internal/pgo",
"cmd/internal/pkgpath",
"cmd/internal/quoted",
"cmd/internal/src",
"cmd/internal/sys",
"cmd/internal/telemetry",
"cmd/internal/telemetry/counter",
"cmd/link",
"cmd/link/internal/...",
"compress/flate",
"compress/zlib",
"container/heap",
"debug/dwarf",
"debug/elf",
"debug/macho",
"debug/pe",
"go/build/constraint",
"go/constant",
"go/version",
"internal/abi",
"internal/coverage",
"cmd/internal/cov/covcmd",
"internal/bisect",
"internal/buildcfg",
"internal/exportdata",
"internal/goarch",
"internal/godebugs",
"internal/goexperiment",
"internal/goroot",
"internal/gover",
"internal/goversion",
// internal/lazyregexp is provided by Go 1.17, which permits it to
// be imported by other packages in this list, but is not provided
// by the Go 1.17 version of gccgo. It's on this list only to
// support gccgo, and can be removed if we require gccgo 14 or later.
"internal/lazyregexp",
"internal/pkgbits",
"internal/platform",
"internal/profile",
"internal/race",
"internal/runtime/gc",
"internal/saferio",
"internal/strconv",
"internal/syscall/unix",
"internal/types/errors",
"internal/unsafeheader",
"internal/xcoff",
"internal/zstd",
"math/bits",
"sort",
}
// File prefixes that are ignored by go/build anyway, and cause
// problems with editor generated temporary files (#18931).
var ignorePrefixes = []string{
".",
"_",
"#",
}
// File suffixes that use build tags introduced since Go 1.17.
// These must not be copied into the bootstrap build directory.
// Also ignore test files.
var ignoreSuffixes = []string{
"_test.s",
"_test.go",
// Skip PGO profile. No need to build toolchain1 compiler
// with PGO. And as it is not a text file the import path
// rewrite will break it.
".pgo",
// Skip editor backup files.
"~",
}
const minBootstrap = "go1.24.6"
var tryDirs = []string{
"sdk/" + minBootstrap,
minBootstrap,
}
func bootstrapBuildTools() {
goroot_bootstrap := os.Getenv("GOROOT_BOOTSTRAP")
if goroot_bootstrap == "" {
home := os.Getenv("HOME")
goroot_bootstrap = pathf("%s/go1.4", home)
for _, d := range tryDirs {
if p := pathf("%s/%s", home, d); isdir(p) {
goroot_bootstrap = p
}
}
}
// check bootstrap version.
ver := run(pathf("%s/bin", goroot_bootstrap), CheckExit, pathf("%s/bin/go", goroot_bootstrap), "env", "GOVERSION")
// go env GOVERSION output like "go1.22.6\n" or "devel go1.24-ffb3e574 Thu Aug 29 20:16:26 2024 +0000\n".
ver = ver[:len(ver)-1]
if version.Compare(ver, version.Lang(minBootstrap)) > 0 && version.Compare(ver, minBootstrap) < 0 {
fatalf("%s does not meet the minimum bootstrap requirement of %s or later", ver, minBootstrap)
}
xprintf("Building Go toolchain1 using %s.\n", goroot_bootstrap)
mkbuildcfg(pathf("%s/src/internal/buildcfg/zbootstrap.go", goroot))
mkobjabi(pathf("%s/src/cmd/internal/objabi/zbootstrap.go", goroot))
// Use $GOROOT/pkg/bootstrap as the bootstrap workspace root.
// We use a subdirectory of $GOROOT/pkg because that's the
// space within $GOROOT where we store all generated objects.
// We could use a temporary directory outside $GOROOT instead,
// but it is easier to debug on failure if the files are in a known location.
workspace := pathf("%s/pkg/bootstrap", goroot)
xremoveall(workspace)
xatexit(func() { xremoveall(workspace) })
base := pathf("%s/src/bootstrap", workspace)
xmkdirall(base)
// Copy source code into $GOROOT/pkg/bootstrap and rewrite import paths.
minBootstrapVers := requiredBootstrapVersion(goModVersion()) // require the minimum required go version to build this go version in the go.mod file
writefile("module bootstrap\ngo "+minBootstrapVers+"\n", pathf("%s/%s", base, "go.mod"), 0)
for _, dir := range bootstrapDirs {
recurse := strings.HasSuffix(dir, "/...")
dir = strings.TrimSuffix(dir, "/...")
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
fatalf("walking bootstrap dirs failed: %v: %v", path, err)
}
name := filepath.Base(path)
src := pathf("%s/src/%s", goroot, path)
dst := pathf("%s/%s", base, path)
if info.IsDir() {
if !recurse && path != dir || name == "testdata" {
return filepath.SkipDir
}
xmkdirall(dst)
if path == "cmd/cgo" {
// Write to src because we need the file both for bootstrap
// and for later in the main build.
mkzdefaultcc("", pathf("%s/zdefaultcc.go", src))
mkzdefaultcc("", pathf("%s/zdefaultcc.go", dst))
}
return nil
}
for _, pre := range ignorePrefixes {
if strings.HasPrefix(name, pre) {
return nil
}
}
for _, suf := range ignoreSuffixes {
if strings.HasSuffix(name, suf) {
return nil
}
}
text := bootstrapRewriteFile(src)
writefile(text, dst, 0)
return nil
})
}
// Set up environment for invoking Go bootstrap toolchains go command.
// GOROOT points at Go bootstrap GOROOT,
// GOPATH points at our bootstrap workspace,
// GOBIN is empty, so that binaries are installed to GOPATH/bin,
// and GOOS, GOHOSTOS, GOARCH, and GOHOSTOS are empty,
// so that Go bootstrap toolchain builds whatever kind of binary it knows how to build.
// Restore GOROOT, GOPATH, and GOBIN when done.
// Don't bother with GOOS, GOHOSTOS, GOARCH, and GOHOSTARCH,
// because setup will take care of those when bootstrapBuildTools returns.
defer os.Setenv("GOROOT", os.Getenv("GOROOT"))
os.Setenv("GOROOT", goroot_bootstrap)
defer os.Setenv("GOPATH", os.Getenv("GOPATH"))
os.Setenv("GOPATH", workspace)
defer os.Setenv("GOBIN", os.Getenv("GOBIN"))
os.Setenv("GOBIN", "")
os.Setenv("GOOS", "")
os.Setenv("GOHOSTOS", "")
os.Setenv("GOARCH", "")
os.Setenv("GOHOSTARCH", "")
// Run Go bootstrap to build binaries.
// Use the math_big_pure_go build tag to disable the assembly in math/big
// which may contain unsupported instructions.
// Use the purego build tag to disable other assembly code.
cmd := []string{
pathf("%s/bin/go", goroot_bootstrap),
"install",
"-tags=math_big_pure_go compiler_bootstrap purego",
}
if vflag > 0 {
cmd = append(cmd, "-v")
}
if tool := os.Getenv("GOBOOTSTRAP_TOOLEXEC"); tool != "" {
cmd = append(cmd, "-toolexec="+tool)
}
cmd = append(cmd, "bootstrap/cmd/...")
run(base, ShowOutput|CheckExit, cmd...)
// Copy binaries into tool binary directory.
for _, name := range bootstrapDirs {
if !strings.HasPrefix(name, "cmd/") {
continue
}
name = name[len("cmd/"):]
if !strings.Contains(name, "/") {
copyfile(pathf("%s/%s%s", tooldir, name, exe), pathf("%s/bin/%s%s", workspace, name, exe), writeExec)
}
}
if vflag > 0 {
xprintf("\n")
}
}
var ssaRewriteFileSubstring = filepath.FromSlash("src/cmd/compile/internal/ssa/rewrite")
// isUnneededSSARewriteFile reports whether srcFile is a
// src/cmd/compile/internal/ssa/rewriteARCHNAME.go file for an
// architecture that isn't for the given GOARCH.
//
// When unneeded is true archCaps is the rewrite base filename without
// the "rewrite" prefix or ".go" suffix: AMD64, 386, ARM, ARM64, etc.
func isUnneededSSARewriteFile(srcFile, goArch string) (archCaps string, unneeded bool) {
if !strings.Contains(srcFile, ssaRewriteFileSubstring) {
return "", false
}
fileArch := strings.TrimSuffix(strings.TrimPrefix(filepath.Base(srcFile), "rewrite"), ".go")
if fileArch == "" {
return "", false
}
b := fileArch[0]
if b == '_' || ('a' <= b && b <= 'z') {
return "", false
}
archCaps = fileArch
fileArch = strings.ToLower(fileArch)
fileArch = strings.TrimSuffix(fileArch, "splitload")
fileArch = strings.TrimSuffix(fileArch, "latelower")
if fileArch == goArch {
return "", false
}
if fileArch == strings.TrimSuffix(goArch, "le") {
return "", false
}
return archCaps, true
}
func bootstrapRewriteFile(srcFile string) string {
// During bootstrap, generate dummy rewrite files for
// irrelevant architectures. We only need to build a bootstrap
// binary that works for the current gohostarch.
// This saves 6+ seconds of bootstrap.
if archCaps, ok := isUnneededSSARewriteFile(srcFile, gohostarch); ok {
return fmt.Sprintf(`%spackage ssa
func rewriteValue%s(v *Value) bool { panic("unused during bootstrap") }
func rewriteBlock%s(b *Block) bool { panic("unused during bootstrap") }
`, generatedHeader, archCaps, archCaps)
}
return bootstrapFixImports(srcFile)
}
var (
importRE = regexp.MustCompile(`\Aimport\s+(\.|[A-Za-z0-9_]+)?\s*"([^"]+)"\s*(//.*)?\n\z`)
importBlockRE = regexp.MustCompile(`\A\s*(?:(\.|[A-Za-z0-9_]+)?\s*"([^"]+)")?\s*(//.*)?\n\z`)
)
func bootstrapFixImports(srcFile string) string {
text := readfile(srcFile)
lines := strings.SplitAfter(text, "\n")
inBlock := false
inComment := false
for i, line := range lines {
if strings.HasSuffix(line, "*/\n") {
inComment = false
}
if strings.HasSuffix(line, "/*\n") {
inComment = true
}
if inComment {
continue
}
if strings.HasPrefix(line, "import (") {
inBlock = true
continue
}
if inBlock && strings.HasPrefix(line, ")") {
inBlock = false
continue
}
var m []string
if !inBlock {
if !strings.HasPrefix(line, "import ") {
continue
}
m = importRE.FindStringSubmatch(line)
if m == nil {
fatalf("%s:%d: invalid import declaration: %q", srcFile, i+1, line)
}
} else {
m = importBlockRE.FindStringSubmatch(line)
if m == nil {
fatalf("%s:%d: invalid import block line", srcFile, i+1)
}
if m[2] == "" {
continue
}
}
path := m[2]
if strings.HasPrefix(path, "cmd/") {
path = "bootstrap/" + path
} else {
for _, dir := range bootstrapDirs {
if path == dir {
path = "bootstrap/" + dir
break
}
}
}
// Rewrite use of internal/reflectlite to be plain reflect.
if path == "internal/reflectlite" {
lines[i] = strings.ReplaceAll(line, `"reflect"`, `reflectlite "reflect"`)
continue
}
// Otherwise, reject direct imports of internal packages,
// since that implies knowledge of internal details that might
// change from one bootstrap toolchain to the next.
// There are many internal packages that are listed in
// bootstrapDirs and made into bootstrap copies based on the
// current repo's source code. Those are fine; this is catching
// references to internal packages in the older bootstrap toolchain.
if strings.HasPrefix(path, "internal/") {
fatalf("%s:%d: bootstrap-copied source file cannot import %s", srcFile, i+1, path)
}
if path != m[2] {
lines[i] = strings.ReplaceAll(line, `"`+m[2]+`"`, `"`+path+`"`)
}
}
lines[0] = generatedHeader + "// This is a bootstrap copy of " + srcFile + "\n\n//line " + srcFile + ":1\n" + lines[0]
return strings.Join(lines, "")
}
|