1
2
3
4
5 package work
6
7 import (
8 "bufio"
9 "bytes"
10 "fmt"
11 "internal/buildcfg"
12 "internal/platform"
13 "io"
14 "log"
15 "os"
16 "path/filepath"
17 "runtime"
18 "strings"
19
20 "cmd/go/internal/base"
21 "cmd/go/internal/cfg"
22 "cmd/go/internal/fsys"
23 "cmd/go/internal/gover"
24 "cmd/go/internal/load"
25 "cmd/go/internal/str"
26 "cmd/internal/quoted"
27 "crypto/sha1"
28 )
29
30
31 var ToolchainVersion = runtime.Version()
32
33
34
35 type gcToolchain struct{}
36
37 func (gcToolchain) compiler() string {
38 return base.Tool("compile")
39 }
40
41 func (gcToolchain) linker() string {
42 return base.Tool("link")
43 }
44
45 func pkgPath(a *Action) string {
46 p := a.Package
47 ppath := p.ImportPath
48 if cfg.BuildBuildmode == "plugin" {
49 ppath = pluginPath(a)
50 } else if p.Name == "main" && !p.Internal.ForceLibrary {
51 ppath = "main"
52 }
53 return ppath
54 }
55
56 func (gcToolchain) gc(b *Builder, a *Action, archive string, importcfg, embedcfg []byte, symabis string, asmhdr bool, pgoProfile string, gofiles []string) (ofile string, output []byte, err error) {
57 p := a.Package
58 sh := b.Shell(a)
59 objdir := a.Objdir
60 if archive != "" {
61 ofile = archive
62 } else {
63 out := "_go_.o"
64 ofile = objdir + out
65 }
66
67 pkgpath := pkgPath(a)
68 defaultGcFlags := []string{"-p", pkgpath}
69 vers := gover.Local()
70 if p.Module != nil {
71 v := p.Module.GoVersion
72 if v == "" {
73 v = gover.DefaultGoModVersion
74 }
75
76 if allowedVersion(v) {
77 vers = v
78 }
79 }
80 defaultGcFlags = append(defaultGcFlags, "-lang=go"+gover.Lang(vers))
81 if p.Standard {
82 defaultGcFlags = append(defaultGcFlags, "-std")
83 }
84
85
86
87
88
89 extFiles := len(p.CgoFiles) + len(p.CFiles) + len(p.CXXFiles) + len(p.MFiles) + len(p.FFiles) + len(p.SFiles) + len(p.SysoFiles) + len(p.SwigFiles) + len(p.SwigCXXFiles)
90 if p.Standard {
91 switch p.ImportPath {
92 case "bytes", "internal/poll", "net", "os":
93 fallthrough
94 case "runtime/metrics", "runtime/pprof", "runtime/trace":
95 fallthrough
96 case "sync", "syscall", "time":
97 extFiles++
98 }
99 }
100 if extFiles == 0 {
101 defaultGcFlags = append(defaultGcFlags, "-complete")
102 }
103 if cfg.BuildContext.InstallSuffix != "" {
104 defaultGcFlags = append(defaultGcFlags, "-installsuffix", cfg.BuildContext.InstallSuffix)
105 }
106 if a.buildID != "" {
107 defaultGcFlags = append(defaultGcFlags, "-buildid", a.buildID)
108 }
109 if p.Internal.OmitDebug || cfg.Goos == "plan9" || cfg.Goarch == "wasm" {
110 defaultGcFlags = append(defaultGcFlags, "-dwarf=false")
111 }
112 if strings.HasPrefix(ToolchainVersion, "go1") && !strings.Contains(os.Args[0], "go_bootstrap") {
113 defaultGcFlags = append(defaultGcFlags, "-goversion", ToolchainVersion)
114 }
115 if p.Internal.Cover.Cfg != "" {
116 defaultGcFlags = append(defaultGcFlags, "-coveragecfg="+p.Internal.Cover.Cfg)
117 }
118 if pgoProfile != "" {
119 defaultGcFlags = append(defaultGcFlags, "-pgoprofile="+pgoProfile)
120 }
121 if symabis != "" {
122 defaultGcFlags = append(defaultGcFlags, "-symabis", symabis)
123 }
124
125 gcflags := str.StringList(forcedGcflags, p.Internal.Gcflags)
126 if p.Internal.FuzzInstrument {
127 gcflags = append(gcflags, fuzzInstrumentFlags()...)
128 }
129
130 if c := gcBackendConcurrency(gcflags); c > 1 {
131 defaultGcFlags = append(defaultGcFlags, fmt.Sprintf("-c=%d", c))
132 }
133
134 args := []any{cfg.BuildToolexec, base.Tool("compile"), "-o", ofile, "-trimpath", a.trimpath(), defaultGcFlags, gcflags}
135 if p.Internal.LocalPrefix == "" {
136 args = append(args, "-nolocalimports")
137 } else {
138 args = append(args, "-D", p.Internal.LocalPrefix)
139 }
140 if importcfg != nil {
141 if err := sh.writeFile(objdir+"importcfg", importcfg); err != nil {
142 return "", nil, err
143 }
144 args = append(args, "-importcfg", objdir+"importcfg")
145 }
146 if embedcfg != nil {
147 if err := sh.writeFile(objdir+"embedcfg", embedcfg); err != nil {
148 return "", nil, err
149 }
150 args = append(args, "-embedcfg", objdir+"embedcfg")
151 }
152 if ofile == archive {
153 args = append(args, "-pack")
154 }
155 if asmhdr {
156 args = append(args, "-asmhdr", objdir+"go_asm.h")
157 }
158
159 for _, f := range gofiles {
160 f := mkAbs(p.Dir, f)
161
162
163
164
165
166
167
168
169
170
171
172
173
174 f, _ = fsys.OverlayPath(f)
175
176 args = append(args, f)
177 }
178
179 output, err = sh.runOut(base.Cwd(), nil, args...)
180 return ofile, output, err
181 }
182
183
184 func gcBackendConcurrency(gcflags []string) int {
185
186 canDashC := concurrentGCBackendCompilationEnabledByDefault
187
188 switch e := os.Getenv("GO19CONCURRENTCOMPILATION"); e {
189 case "0":
190 canDashC = false
191 case "1":
192 canDashC = true
193 case "":
194
195 default:
196 log.Fatalf("GO19CONCURRENTCOMPILATION must be 0, 1, or unset, got %q", e)
197 }
198
199
200 if cfg.ExperimentErr != nil || cfg.Experiment.FieldTrack || cfg.Experiment.PreemptibleLoops {
201 canDashC = false
202 }
203
204 if !canDashC {
205 return 1
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 c := runtime.GOMAXPROCS(0)
232 if cfg.BuildP == 1 {
233
234 return c
235 }
236
237 if c > 4 {
238 c = 4
239 }
240 return c
241 }
242
243
244
245 func (a *Action) trimpath() string {
246
247
248
249
250
251 objdir := a.Objdir
252 if len(objdir) > 1 && objdir[len(objdir)-1] == filepath.Separator {
253 objdir = objdir[:len(objdir)-1]
254 }
255 rewrite := ""
256
257 rewriteDir := a.Package.Dir
258 if cfg.BuildTrimpath {
259 importPath := a.Package.Internal.OrigImportPath
260 if m := a.Package.Module; m != nil && m.Version != "" {
261 rewriteDir = m.Path + "@" + m.Version + strings.TrimPrefix(importPath, m.Path)
262 } else {
263 rewriteDir = importPath
264 }
265 rewrite += a.Package.Dir + "=>" + rewriteDir + ";"
266 }
267
268
269
270
271
272 cgoFiles := make(map[string]bool)
273 for _, f := range a.Package.CgoFiles {
274 cgoFiles[f] = true
275 }
276
277
278
279
280
281 var overlayNonGoRewrites string
282 hasCgoOverlay := false
283 if fsys.OverlayFile != "" {
284 for _, filename := range a.Package.AllFiles() {
285 path := filename
286 if !filepath.IsAbs(path) {
287 path = filepath.Join(a.Package.Dir, path)
288 }
289 base := filepath.Base(path)
290 isGo := strings.HasSuffix(filename, ".go") || strings.HasSuffix(filename, ".s")
291 isCgo := cgoFiles[filename] || !isGo
292 overlayPath, isOverlay := fsys.OverlayPath(path)
293 if isCgo && isOverlay {
294 hasCgoOverlay = true
295 }
296 if !isCgo && isOverlay {
297 rewrite += overlayPath + "=>" + filepath.Join(rewriteDir, base) + ";"
298 } else if isCgo {
299
300 if filepath.Dir(path) == a.Package.Dir {
301
302 overlayNonGoRewrites += filepath.Join(objdir, base) + "=>" + filepath.Join(rewriteDir, base) + ";"
303 }
304 } else {
305
306 }
307 }
308 }
309 if hasCgoOverlay {
310 rewrite += overlayNonGoRewrites
311 }
312 rewrite += objdir + "=>"
313
314 return rewrite
315 }
316
317 func asmArgs(a *Action, p *load.Package) []any {
318
319 inc := filepath.Join(cfg.GOROOT, "pkg", "include")
320 pkgpath := pkgPath(a)
321 args := []any{cfg.BuildToolexec, base.Tool("asm"), "-p", pkgpath, "-trimpath", a.trimpath(), "-I", a.Objdir, "-I", inc, "-D", "GOOS_" + cfg.Goos, "-D", "GOARCH_" + cfg.Goarch, forcedAsmflags, p.Internal.Asmflags}
322 if p.ImportPath == "runtime" && cfg.Goarch == "386" {
323 for _, arg := range forcedAsmflags {
324 if arg == "-dynlink" {
325 args = append(args, "-D=GOBUILDMODE_shared=1")
326 }
327 }
328 }
329
330 if cfg.Goarch == "386" {
331
332 args = append(args, "-D", "GO386_"+cfg.GO386)
333 }
334
335 if cfg.Goarch == "amd64" {
336
337 args = append(args, "-D", "GOAMD64_"+cfg.GOAMD64)
338 }
339
340 if cfg.Goarch == "mips" || cfg.Goarch == "mipsle" {
341
342 args = append(args, "-D", "GOMIPS_"+cfg.GOMIPS)
343 }
344
345 if cfg.Goarch == "mips64" || cfg.Goarch == "mips64le" {
346
347 args = append(args, "-D", "GOMIPS64_"+cfg.GOMIPS64)
348 }
349
350 if cfg.Goarch == "ppc64" || cfg.Goarch == "ppc64le" {
351
352
353 switch cfg.GOPPC64 {
354 case "power10":
355 args = append(args, "-D", "GOPPC64_power10")
356 fallthrough
357 case "power9":
358 args = append(args, "-D", "GOPPC64_power9")
359 fallthrough
360 default:
361 args = append(args, "-D", "GOPPC64_power8")
362 }
363 }
364
365 if cfg.Goarch == "riscv64" {
366
367 args = append(args, "-D", "GORISCV64_"+cfg.GORISCV64)
368 }
369
370 if cfg.Goarch == "arm" {
371
372
373 switch {
374 case strings.Contains(cfg.GOARM, "7"):
375 args = append(args, "-D", "GOARM_7")
376 fallthrough
377 case strings.Contains(cfg.GOARM, "6"):
378 args = append(args, "-D", "GOARM_6")
379 fallthrough
380 default:
381 args = append(args, "-D", "GOARM_5")
382 }
383 }
384
385 if cfg.Goarch == "arm64" {
386 g, err := buildcfg.ParseGoarm64(cfg.GOARM64)
387 if err == nil && g.LSE {
388 args = append(args, "-D", "GOARM64_LSE")
389 }
390 }
391
392 return args
393 }
394
395 func (gcToolchain) asm(b *Builder, a *Action, sfiles []string) ([]string, error) {
396 p := a.Package
397 args := asmArgs(a, p)
398
399 var ofiles []string
400 for _, sfile := range sfiles {
401 overlayPath, _ := fsys.OverlayPath(mkAbs(p.Dir, sfile))
402 ofile := a.Objdir + sfile[:len(sfile)-len(".s")] + ".o"
403 ofiles = append(ofiles, ofile)
404 args1 := append(args, "-o", ofile, overlayPath)
405 if err := b.Shell(a).run(p.Dir, p.ImportPath, nil, args1...); err != nil {
406 return nil, err
407 }
408 }
409 return ofiles, nil
410 }
411
412 func (gcToolchain) symabis(b *Builder, a *Action, sfiles []string) (string, error) {
413 sh := b.Shell(a)
414
415 mkSymabis := func(p *load.Package, sfiles []string, path string) error {
416 args := asmArgs(a, p)
417 args = append(args, "-gensymabis", "-o", path)
418 for _, sfile := range sfiles {
419 if p.ImportPath == "runtime/cgo" && strings.HasPrefix(sfile, "gcc_") {
420 continue
421 }
422 op, _ := fsys.OverlayPath(mkAbs(p.Dir, sfile))
423 args = append(args, op)
424 }
425
426
427
428
429 if err := sh.writeFile(a.Objdir+"go_asm.h", nil); err != nil {
430 return err
431 }
432
433 return sh.run(p.Dir, p.ImportPath, nil, args...)
434 }
435
436 var symabis string
437 p := a.Package
438 if len(sfiles) != 0 {
439 symabis = a.Objdir + "symabis"
440 if err := mkSymabis(p, sfiles, symabis); err != nil {
441 return "", err
442 }
443 }
444
445 return symabis, nil
446 }
447
448
449
450
451 func toolVerify(a *Action, b *Builder, p *load.Package, newTool string, ofile string, args []any) error {
452 newArgs := make([]any, len(args))
453 copy(newArgs, args)
454 newArgs[1] = base.Tool(newTool)
455 newArgs[3] = ofile + ".new"
456 if err := b.Shell(a).run(p.Dir, p.ImportPath, nil, newArgs...); err != nil {
457 return err
458 }
459 data1, err := os.ReadFile(ofile)
460 if err != nil {
461 return err
462 }
463 data2, err := os.ReadFile(ofile + ".new")
464 if err != nil {
465 return err
466 }
467 if !bytes.Equal(data1, data2) {
468 return fmt.Errorf("%s and %s produced different output files:\n%s\n%s", filepath.Base(args[1].(string)), newTool, strings.Join(str.StringList(args...), " "), strings.Join(str.StringList(newArgs...), " "))
469 }
470 os.Remove(ofile + ".new")
471 return nil
472 }
473
474 func (gcToolchain) pack(b *Builder, a *Action, afile string, ofiles []string) error {
475 var absOfiles []string
476 for _, f := range ofiles {
477 absOfiles = append(absOfiles, mkAbs(a.Objdir, f))
478 }
479 absAfile := mkAbs(a.Objdir, afile)
480
481
482
483 if !cfg.BuildN {
484 if _, err := os.Stat(absAfile); err != nil {
485 base.Fatalf("os.Stat of archive file failed: %v", err)
486 }
487 }
488
489 p := a.Package
490 sh := b.Shell(a)
491 if cfg.BuildN || cfg.BuildX {
492 cmdline := str.StringList(base.Tool("pack"), "r", absAfile, absOfiles)
493 sh.ShowCmd(p.Dir, "%s # internal", joinUnambiguously(cmdline))
494 }
495 if cfg.BuildN {
496 return nil
497 }
498 if err := packInternal(absAfile, absOfiles); err != nil {
499 return sh.reportCmd("", "", nil, err)
500 }
501 return nil
502 }
503
504 func packInternal(afile string, ofiles []string) error {
505 dst, err := os.OpenFile(afile, os.O_WRONLY|os.O_APPEND, 0)
506 if err != nil {
507 return err
508 }
509 defer dst.Close()
510 w := bufio.NewWriter(dst)
511
512 for _, ofile := range ofiles {
513 src, err := os.Open(ofile)
514 if err != nil {
515 return err
516 }
517 fi, err := src.Stat()
518 if err != nil {
519 src.Close()
520 return err
521 }
522
523
524 name := fi.Name()
525 if len(name) > 16 {
526 name = name[:16]
527 } else {
528 name += strings.Repeat(" ", 16-len(name))
529 }
530 size := fi.Size()
531 fmt.Fprintf(w, "%s%-12d%-6d%-6d%-8o%-10d`\n",
532 name, 0, 0, 0, 0644, size)
533 n, err := io.Copy(w, src)
534 src.Close()
535 if err == nil && n < size {
536 err = io.ErrUnexpectedEOF
537 } else if err == nil && n > size {
538 err = fmt.Errorf("file larger than size reported by stat")
539 }
540 if err != nil {
541 return fmt.Errorf("copying %s to %s: %v", ofile, afile, err)
542 }
543 if size&1 != 0 {
544 w.WriteByte(0)
545 }
546 }
547
548 if err := w.Flush(); err != nil {
549 return err
550 }
551 return dst.Close()
552 }
553
554
555 func setextld(ldflags []string, compiler []string) ([]string, error) {
556 for _, f := range ldflags {
557 if f == "-extld" || strings.HasPrefix(f, "-extld=") {
558
559 return ldflags, nil
560 }
561 }
562 joined, err := quoted.Join(compiler)
563 if err != nil {
564 return nil, err
565 }
566 return append(ldflags, "-extld="+joined), nil
567 }
568
569
570
571
572
573
574
575
576 func pluginPath(a *Action) string {
577 p := a.Package
578 if p.ImportPath != "command-line-arguments" {
579 return p.ImportPath
580 }
581 h := sha1.New()
582 buildID := a.buildID
583 if a.Mode == "link" {
584
585
586
587
588
589
590
591
592
593 id := strings.Split(buildID, buildIDSeparator)
594 buildID = id[1] + buildIDSeparator + id[1]
595 }
596 fmt.Fprintf(h, "build ID: %s\n", buildID)
597 for _, file := range str.StringList(p.GoFiles, p.CgoFiles, p.SFiles) {
598 data, err := os.ReadFile(filepath.Join(p.Dir, file))
599 if err != nil {
600 base.Fatalf("go: %s", err)
601 }
602 h.Write(data)
603 }
604 return fmt.Sprintf("plugin/unnamed-%x", h.Sum(nil))
605 }
606
607 func (gcToolchain) ld(b *Builder, root *Action, targetPath, importcfg, mainpkg string) error {
608 cxx := len(root.Package.CXXFiles) > 0 || len(root.Package.SwigCXXFiles) > 0
609 for _, a := range root.Deps {
610 if a.Package != nil && (len(a.Package.CXXFiles) > 0 || len(a.Package.SwigCXXFiles) > 0) {
611 cxx = true
612 }
613 }
614 var ldflags []string
615 if cfg.BuildContext.InstallSuffix != "" {
616 ldflags = append(ldflags, "-installsuffix", cfg.BuildContext.InstallSuffix)
617 }
618 if root.Package.Internal.OmitDebug {
619 ldflags = append(ldflags, "-s", "-w")
620 }
621 if cfg.BuildBuildmode == "plugin" {
622 ldflags = append(ldflags, "-pluginpath", pluginPath(root))
623 }
624
625
626
627 if root.Package.Goroot && strings.HasPrefix(root.Package.ImportPath, "cmd/") {
628
629
630
631
632 if !platform.MustLinkExternal(cfg.Goos, cfg.Goarch, false) {
633 ldflags = append(ldflags, "-X=cmd/internal/objabi.buildID="+root.buildID)
634 }
635 }
636
637
638 if root.Package.DefaultGODEBUG != "" {
639 ldflags = append(ldflags, "-X=runtime.godebugDefault="+root.Package.DefaultGODEBUG)
640 }
641
642
643
644
645
646 var compiler []string
647 if cxx {
648 compiler = envList("CXX", cfg.DefaultCXX(cfg.Goos, cfg.Goarch))
649 } else {
650 compiler = envList("CC", cfg.DefaultCC(cfg.Goos, cfg.Goarch))
651 }
652 ldflags = append(ldflags, "-buildmode="+ldBuildmode)
653 if root.buildID != "" {
654 ldflags = append(ldflags, "-buildid="+root.buildID)
655 }
656 ldflags = append(ldflags, forcedLdflags...)
657 ldflags = append(ldflags, root.Package.Internal.Ldflags...)
658 ldflags, err := setextld(ldflags, compiler)
659 if err != nil {
660 return err
661 }
662
663
664
665
666
667
668
669
670
671
672
673
674 dir := "."
675 if cfg.BuildBuildmode == "c-shared" || cfg.BuildBuildmode == "plugin" {
676 dir, targetPath = filepath.Split(targetPath)
677 }
678
679 env := []string{}
680
681 if cfg.BuildTrimpath {
682 env = append(env, "GOROOT=")
683 } else {
684 env = append(env, "GOROOT="+cfg.GOROOT)
685 }
686 return b.Shell(root).run(dir, root.Package.ImportPath, env, cfg.BuildToolexec, base.Tool("link"), "-o", targetPath, "-importcfg", importcfg, ldflags, mainpkg)
687 }
688
689 func (gcToolchain) ldShared(b *Builder, root *Action, toplevelactions []*Action, targetPath, importcfg string, allactions []*Action) error {
690 ldflags := []string{"-installsuffix", cfg.BuildContext.InstallSuffix}
691 ldflags = append(ldflags, "-buildmode=shared")
692 ldflags = append(ldflags, forcedLdflags...)
693 ldflags = append(ldflags, root.Package.Internal.Ldflags...)
694 cxx := false
695 for _, a := range allactions {
696 if a.Package != nil && (len(a.Package.CXXFiles) > 0 || len(a.Package.SwigCXXFiles) > 0) {
697 cxx = true
698 }
699 }
700
701
702
703
704 var compiler []string
705 if cxx {
706 compiler = envList("CXX", cfg.DefaultCXX(cfg.Goos, cfg.Goarch))
707 } else {
708 compiler = envList("CC", cfg.DefaultCC(cfg.Goos, cfg.Goarch))
709 }
710 ldflags, err := setextld(ldflags, compiler)
711 if err != nil {
712 return err
713 }
714 for _, d := range toplevelactions {
715 if !strings.HasSuffix(d.Target, ".a") {
716 continue
717 }
718 ldflags = append(ldflags, d.Package.ImportPath+"="+d.Target)
719 }
720 return b.Shell(root).run(".", targetPath, nil, cfg.BuildToolexec, base.Tool("link"), "-o", targetPath, "-importcfg", importcfg, ldflags)
721 }
722
723 func (gcToolchain) cc(b *Builder, a *Action, ofile, cfile string) error {
724 return fmt.Errorf("%s: C source files not supported without cgo", mkAbs(a.Package.Dir, cfile))
725 }
726
View as plain text