| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
|
|
| package main |
|
|
| import ( |
| "fmt" |
| "go/version" |
| "os" |
| "path/filepath" |
| "regexp" |
| "strings" |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 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", |
| "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", |
| } |
|
|
| |
| |
| var ignorePrefixes = []string{ |
| ".", |
| "_", |
| "#", |
| } |
|
|
| |
| |
| |
| var ignoreSuffixes = []string{ |
| "_test.s", |
| "_test.go", |
| |
| |
| |
| ".pgo", |
| |
| "~", |
| } |
|
|
| 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 |
| } |
| } |
| } |
|
|
| |
| ver := run(pathf("%s/bin", goroot_bootstrap), CheckExit, pathf("%s/bin/go", goroot_bootstrap), "env", "GOVERSION") |
| |
| 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)) |
|
|
| |
| |
| |
| |
| |
| workspace := pathf("%s/pkg/bootstrap", goroot) |
| xremoveall(workspace) |
| xatexit(func() { xremoveall(workspace) }) |
| base := pathf("%s/src/bootstrap", workspace) |
| xmkdirall(base) |
|
|
| |
| minBootstrapVers := requiredBootstrapVersion(goModVersion()) |
| 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" { |
| |
| |
| 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 |
| }) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| 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", "") |
|
|
| |
| |
| |
| |
| 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...) |
|
|
| |
| 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") |
|
|
| |
| |
| |
| |
| |
| |
| 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 { |
| |
| |
| |
| |
| 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 |
| } |
| } |
| } |
|
|
| |
| if path == "internal/reflectlite" { |
| lines[i] = strings.ReplaceAll(line, `"reflect"`, `reflectlite "reflect"`) |
| continue |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| 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, "") |
| } |
|
|