Source file
src/cmd/dist/buildtool.go
1
2
3
4
5
6
7
8
9
10
11
12 package main
13
14 import (
15 "fmt"
16 "go/version"
17 "os"
18 "path/filepath"
19 "regexp"
20 "strings"
21 )
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 var bootstrapDirs = []string{
37 "cmp",
38 "cmd/asm",
39 "cmd/asm/internal/...",
40 "cmd/cgo",
41 "cmd/compile",
42 "cmd/compile/internal/...",
43 "cmd/go",
44 "cmd/go/internal/base",
45 "cmd/go/internal/bug",
46 "cmd/go/internal/build",
47 "cmd/go/internal/cache",
48 "cmd/go/internal/cacheprog",
49 "cmd/go/internal/cfg",
50 "cmd/go/internal/clean",
51 "cmd/go/internal/cmdflag",
52 "cmd/go/internal/doc",
53 "cmd/go/internal/envcmd",
54 "cmd/go/internal/fips140",
55 "cmd/go/internal/fmtcmd",
56 "cmd/go/internal/fsys",
57 "cmd/go/internal/generate",
58 "cmd/go/internal/gover",
59 "cmd/go/internal/help",
60 "cmd/go/internal/imports",
61 "cmd/go/internal/list",
62 "cmd/go/internal/load",
63 "cmd/go/internal/lockedfile",
64 "cmd/go/internal/lockedfile/internal/filelock",
65 "cmd/go/internal/mmap",
66 "cmd/go/internal/modcmd",
67 "cmd/go/internal/modfetch",
68 "cmd/go/internal/modfetch/codehost",
69 "cmd/go/internal/modget",
70 "cmd/go/internal/modindex",
71 "cmd/go/internal/modinfo",
72 "cmd/go/internal/modload",
73 "cmd/go/internal/mvs",
74 "cmd/go/internal/run",
75 "cmd/go/internal/search",
76 "cmd/go/internal/str",
77 "cmd/go/internal/telemetrycmd",
78 "cmd/go/internal/telemetrystats",
79 "cmd/go/internal/test",
80 "cmd/go/internal/tool",
81 "cmd/go/internal/toolchain",
82 "cmd/go/internal/trace",
83 "cmd/go/internal/vcs",
84 "cmd/go/internal/version",
85 "cmd/go/internal/vet",
86 "cmd/go/internal/web",
87 "cmd/go/internal/work",
88 "cmd/go/internal/workcmd",
89 "cmd/internal/archive",
90 "cmd/internal/bio",
91 "cmd/internal/buildid",
92 "cmd/internal/codesign",
93 "cmd/internal/dwarf",
94 "cmd/internal/edit",
95 "cmd/internal/gcprog",
96 "cmd/internal/goobj",
97 "cmd/internal/hash",
98 "cmd/internal/macho",
99 "cmd/internal/obj/...",
100 "cmd/internal/objabi",
101 "cmd/internal/par",
102 "cmd/internal/pathcache",
103 "cmd/internal/pgo",
104 "cmd/internal/pkgpath",
105 "cmd/internal/pkgpattern",
106 "cmd/internal/quoted",
107 "cmd/internal/robustio",
108 "cmd/internal/src",
109 "cmd/internal/sys",
110 "cmd/internal/telemetry",
111 "cmd/internal/telemetry/counter",
112 "cmd/internal/test2json",
113 "cmd/link",
114 "cmd/link/internal/...",
115 "cmd/preprofile",
116 "cmd/vendor/golang.org/x/mod/internal/lazyregexp",
117 "cmd/vendor/golang.org/x/mod/modfile",
118 "cmd/vendor/golang.org/x/mod/module",
119 "cmd/vendor/golang.org/x/mod/semver",
120 "cmd/vendor/golang.org/x/mod/sumdb/dirhash",
121 "cmd/vendor/golang.org/x/mod/zip",
122 "cmd/vendor/golang.org/x/sync/semaphore",
123 "compress/flate",
124 "compress/zlib",
125 "container/heap",
126 "debug/dwarf",
127 "debug/elf",
128 "debug/macho",
129 "debug/pe",
130 "go/build",
131 "go/build/constraint",
132 "go/constant",
133 "go/version",
134 "internal/abi",
135 "internal/coverage",
136 "cmd/internal/cov/covcmd",
137 "internal/bisect",
138 "internal/buildcfg",
139 "internal/cfg",
140 "internal/diff",
141 "internal/exportdata",
142 "internal/goarch",
143 "internal/godebugs",
144 "internal/goexperiment",
145 "internal/goroot",
146 "internal/gover",
147 "internal/goversion",
148
149
150
151
152 "internal/lazyregexp",
153 "internal/lazytemplate",
154 "internal/oserror",
155 "internal/pkgbits",
156 "internal/platform",
157 "internal/profile",
158 "internal/race",
159 "internal/runtime/gc",
160 "internal/saferio",
161 "internal/singleflight",
162 "internal/strconv",
163 "internal/syslist",
164 "internal/trace/traceviewer/format",
165 "internal/types/errors",
166 "internal/unsafeheader",
167 "internal/xcoff",
168 "internal/zstd",
169 "math/bits",
170 "sort",
171 }
172
173
174
175 var ignorePrefixes = []string{
176 ".",
177 "_",
178 "#",
179 }
180
181
182
183
184 var ignoreSuffixes = []string{
185 "_test.s",
186 "_test.go",
187
188
189
190 ".pgo",
191
192 "~",
193 }
194
195 const minBootstrap = "go1.26.0"
196
197 var tryDirs = []string{
198 "sdk/" + minBootstrap,
199 minBootstrap,
200 }
201
202 func bootstrapBuildTools() {
203 goroot_bootstrap := os.Getenv("GOROOT_BOOTSTRAP")
204 if goroot_bootstrap == "" {
205 home := os.Getenv("HOME")
206 goroot_bootstrap = pathf("%s/go1.4", home)
207 for _, d := range tryDirs {
208 if p := pathf("%s/%s", home, d); isdir(p) {
209 goroot_bootstrap = p
210 }
211 }
212 }
213
214
215
216
217
218
219
220
221 defer os.Setenv("GOWASM", os.Getenv("GOWASM"))
222 os.Setenv("GOWASM", "")
223
224
225 ver := run(pathf("%s/bin", goroot_bootstrap), CheckExit, pathf("%s/bin/go", goroot_bootstrap), "env", "GOVERSION")
226
227 ver = ver[:len(ver)-1]
228 if version.Compare(ver, version.Lang(minBootstrap)) > 0 && version.Compare(ver, minBootstrap) < 0 {
229 fatalf("%s does not meet the minimum bootstrap requirement of %s or later", ver, minBootstrap)
230 }
231
232 xprintf("Building Go toolchain1 and bootstrap cmd/go (go_bootstrap) using %s.\n", goroot_bootstrap)
233
234 mkbuildcfg(pathf("%s/src/internal/buildcfg/zbootstrap.go", goroot))
235 mkobjabi(pathf("%s/src/cmd/internal/objabi/zbootstrap.go", goroot))
236
237
238
239
240
241
242 workspace := pathf("%s/pkg/bootstrap", goroot)
243 xremoveall(workspace)
244 xatexit(func() { xremoveall(workspace) })
245 base := pathf("%s/src/bootstrap", workspace)
246 xmkdirall(base)
247
248
249 copySpan := startSpan("copy bootstrap sources")
250 minBootstrapVers := requiredBootstrapVersion(goModVersion())
251 writefile("module bootstrap\ngo "+minBootstrapVers+"\n", pathf("%s/%s", base, "go.mod"), 0)
252 for _, dir := range bootstrapDirs {
253 recurse := strings.HasSuffix(dir, "/...")
254 dir = strings.TrimSuffix(dir, "/...")
255 filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
256 if err != nil {
257 fatalf("walking bootstrap dirs failed: %v: %v", path, err)
258 }
259
260 name := filepath.Base(path)
261 src := pathf("%s/src/%s", goroot, path)
262 dst := pathf("%s/%s", base, strings.TrimPrefix(filepath.ToSlash(path), "cmd/vendor/"))
263
264 if info.IsDir() {
265 if !recurse && path != dir || name == "testdata" {
266 return filepath.SkipDir
267 }
268
269 xmkdirall(dst)
270 if path == "cmd/cgo" {
271
272
273 mkzdefaultcc("", pathf("%s/zdefaultcc.go", src))
274 mkzdefaultcc("", pathf("%s/zdefaultcc.go", dst))
275 }
276 return nil
277 }
278
279 for _, pre := range ignorePrefixes {
280 if strings.HasPrefix(name, pre) {
281 return nil
282 }
283 }
284 for _, suf := range ignoreSuffixes {
285 if strings.HasSuffix(name, suf) {
286 return nil
287 }
288 }
289
290 text := bootstrapRewriteFile(src)
291 writefile(text, dst, 0)
292 return nil
293 })
294 }
295 copySpan.done()
296
297
298
299
300
301
302
303
304
305
306
307
308 defer os.Setenv("GOROOT", os.Getenv("GOROOT"))
309 os.Setenv("GOROOT", goroot_bootstrap)
310
311 defer os.Setenv("GOPATH", os.Getenv("GOPATH"))
312 os.Setenv("GOPATH", workspace)
313
314 defer os.Setenv("GOBIN", os.Getenv("GOBIN"))
315 os.Setenv("GOBIN", "")
316
317 defer os.Setenv("GOFIPS140", os.Getenv("GOFIPS140"))
318 os.Setenv("GOFIPS140", "off")
319
320 os.Setenv("GOOS", gohostos)
321 os.Setenv("GOHOSTOS", "")
322 os.Setenv("GOARCH", gohostarch)
323 os.Setenv("GOHOSTARCH", "")
324
325
326 bindir := pathf("%s/bin", workspace)
327 xmkdirall(bindir)
328 cmd := []string{
329 pathf("%s/bin/go", goroot_bootstrap),
330 "build",
331 "-o", bindir,
332 "-tags=compiler_bootstrap,cmd_go_bootstrap",
333 }
334 if vflag > 0 {
335 cmd = append(cmd, "-v")
336 }
337 if tool := os.Getenv("GOBOOTSTRAP_TOOLEXEC"); tool != "" {
338 cmd = append(cmd, "-toolexec="+tool)
339 }
340 cmd = append(cmd, maybeTraceFlag("toolchain1")...)
341 cmd = append(cmd, "bootstrap/cmd/...")
342 toolchain1Span := startSpan("toolchain1")
343 run(base, ShowOutput|CheckExit, cmd...)
344 toolchain1Span.done()
345
346
347 copyBinSpan := startSpan("copy toolchain1 binaries")
348 for _, name := range bootstrapDirs {
349 if !strings.HasPrefix(name, "cmd/") {
350 continue
351 }
352 name = name[len("cmd/"):]
353 if !strings.Contains(name, "/") {
354 tool := name
355 if name == "go" {
356 tool = "go_bootstrap"
357 }
358 copyfile(pathf("%s/%s%s", tooldir, tool, exe), pathf("%s/bin/%s%s", workspace, name, exe), writeExec)
359 }
360 }
361 copyBinSpan.done()
362
363 if vflag > 0 {
364 xprintf("\n")
365 }
366 }
367
368 var ssaRewriteFileSubstring = filepath.FromSlash("src/cmd/compile/internal/ssacompile/rewrite")
369
370 func init() {
371 if ssaRewriteSplitPackages {
372 ssaRewriteFileSubstring = filepath.FromSlash("src/cmd/compile/internal/ssa/rewrite")
373 }
374 }
375
376
377
378
379
380
381
382
383 func isUnneededSSARewriteFile(srcFile, goArch string) (archCaps string, unneeded bool) {
384 if !strings.Contains(srcFile, ssaRewriteFileSubstring) {
385 return "", false
386 }
387 if ssaRewriteSplitPackages && !strings.HasPrefix(filepath.Base(srcFile), "rewrite") {
388
389
390
391 return "", false
392 }
393 fileArch := strings.TrimSuffix(strings.TrimPrefix(filepath.Base(srcFile), "rewrite"), ".go")
394 if fileArch == "" {
395 return "", false
396 }
397 b := fileArch[0]
398 if b == '_' || ('a' <= b && b <= 'z') {
399 return "", false
400 }
401 archCaps = fileArch
402 fileArch = strings.ToLower(fileArch)
403 fileArch = strings.TrimSuffix(fileArch, "splitload")
404 fileArch = strings.TrimSuffix(fileArch, "latelower")
405 if fileArch == goArch {
406 return "", false
407 }
408 if fileArch == strings.TrimSuffix(goArch, "le") {
409 return "", false
410 }
411 return archCaps, true
412 }
413
414 func bootstrapRewriteFile(srcFile string) string {
415
416
417
418
419 rewrite := "rewrite"
420 if archCaps, ok := isUnneededSSARewriteFile(srcFile, gohostarch); ok {
421 if ssaRewriteSplitPackages {
422 rewrite = "Rewrite"
423 archCaps = ""
424 }
425 pkg := filepath.Base(filepath.Dir(srcFile))
426 return fmt.Sprintf(`%spackage %s
427
428 import "bootstrap/cmd/compile/internal/ssa"
429
430 func %sValue%s(v *ssa.Value) bool { panic("unused during bootstrap") }
431 func %sBlock%s(b *ssa.Block) bool { panic("unused during bootstrap") }
432 `, generatedHeader, pkg, rewrite, archCaps, rewrite, archCaps)
433 }
434
435 return bootstrapFixImports(srcFile)
436 }
437
438 var ssaRewriteSplitPackages = true
439
440 var (
441 importRE = regexp.MustCompile(`\Aimport\s+(\.|[A-Za-z0-9_]+)?\s*"([^"]+)"\s*(//.*)?\n\z`)
442 importBlockRE = regexp.MustCompile(`\A\s*(?:(\.|[A-Za-z0-9_]+)?\s*"([^"]+)")?\s*(//.*)?\n\z`)
443 )
444
445 func bootstrapFixImports(srcFile string) string {
446 text := readfile(srcFile)
447 lines := strings.SplitAfter(text, "\n")
448 inBlock := false
449 inComment := false
450 sawImport := false
451 for i, line := range lines {
452 if strings.HasSuffix(line, "*/\n") {
453 inComment = false
454 }
455 if strings.HasSuffix(line, "/*\n") {
456 inComment = true
457 }
458 if inComment {
459 continue
460 }
461 if strings.HasPrefix(line, "import (") {
462 inBlock = true
463 sawImport = true
464 continue
465 }
466 if inBlock && strings.HasPrefix(line, ")") {
467 inBlock = false
468 continue
469 }
470
471 var m []string
472 if !inBlock {
473 if !strings.HasPrefix(line, "import ") {
474 if sawImport && strings.TrimSpace(line) != "" && !strings.HasPrefix(line, "//") {
475 break
476 }
477 continue
478 }
479 sawImport = true
480 m = importRE.FindStringSubmatch(line)
481 if m == nil {
482 fatalf("%s:%d: invalid import declaration: %q", srcFile, i+1, line)
483 }
484 } else {
485 m = importBlockRE.FindStringSubmatch(line)
486 if m == nil {
487 fatalf("%s:%d: invalid import block line", srcFile, i+1)
488 }
489 if m[2] == "" {
490 continue
491 }
492 }
493
494 path := m[2]
495 if strings.HasPrefix(path, "cmd/") {
496 path = "bootstrap/" + path
497 } else {
498 for _, dir := range bootstrapDirs {
499 if path == dir || "cmd/vendor/"+path == dir {
500 path = "bootstrap/" + path
501 break
502 }
503 }
504 }
505
506
507
508
509 if path == "internal/godebug" || path == "internal/syscall/unix" || path == "internal/syscall/windows" {
510 continue
511 }
512
513
514 if path == "internal/reflectlite" {
515 lines[i] = strings.ReplaceAll(line, `"reflect"`, `reflectlite "reflect"`)
516 continue
517 }
518
519
520
521
522
523
524
525
526 if strings.HasPrefix(path, "internal/") {
527 fatalf("%s:%d: bootstrap-copied source file cannot import %s", srcFile, i+1, path)
528 }
529 if path != m[2] {
530 lines[i] = strings.ReplaceAll(line, `"`+m[2]+`"`, `"`+path+`"`)
531 }
532 }
533
534 lines[0] = generatedHeader + "// This is a bootstrap copy of " + srcFile + "\n\n//line " + srcFile + ":1\n" + lines[0]
535
536 return strings.Join(lines, "")
537 }
538
View as plain text