1
2
3
4
5
6
7 package work
8
9 import (
10 "bytes"
11 "cmd/internal/cov/covcmd"
12 "context"
13 "crypto/sha256"
14 "encoding/json"
15 "errors"
16 "fmt"
17 "go/token"
18 "internal/lazyregexp"
19 "io"
20 "io/fs"
21 "log"
22 "math/rand"
23 "os"
24 "os/exec"
25 "path/filepath"
26 "regexp"
27 "runtime"
28 "slices"
29 "sort"
30 "strconv"
31 "strings"
32 "sync"
33 "time"
34
35 "cmd/go/internal/base"
36 "cmd/go/internal/cache"
37 "cmd/go/internal/cfg"
38 "cmd/go/internal/fsys"
39 "cmd/go/internal/gover"
40 "cmd/go/internal/load"
41 "cmd/go/internal/modload"
42 "cmd/go/internal/str"
43 "cmd/go/internal/trace"
44 "cmd/internal/buildid"
45 "cmd/internal/quoted"
46 "cmd/internal/sys"
47 )
48
49 const defaultCFlags = "-O2 -g"
50
51
52
53 func actionList(root *Action) []*Action {
54 seen := map[*Action]bool{}
55 all := []*Action{}
56 var walk func(*Action)
57 walk = func(a *Action) {
58 if seen[a] {
59 return
60 }
61 seen[a] = true
62 for _, a1 := range a.Deps {
63 walk(a1)
64 }
65 all = append(all, a)
66 }
67 walk(root)
68 return all
69 }
70
71
72 func (b *Builder) Do(ctx context.Context, root *Action) {
73 ctx, span := trace.StartSpan(ctx, "exec.Builder.Do ("+root.Mode+" "+root.Target+")")
74 defer span.Done()
75
76 if !b.IsCmdList {
77
78 c := cache.Default()
79 defer func() {
80 if err := c.Close(); err != nil {
81 base.Fatalf("go: failed to trim cache: %v", err)
82 }
83 }()
84 }
85
86
87
88
89
90
91
92
93
94
95
96
97 all := actionList(root)
98 for i, a := range all {
99 a.priority = i
100 }
101
102
103 writeActionGraph := func() {
104 if file := cfg.DebugActiongraph; file != "" {
105 if strings.HasSuffix(file, ".go") {
106
107
108 base.Fatalf("go: refusing to write action graph to %v\n", file)
109 }
110 js := actionGraphJSON(root)
111 if err := os.WriteFile(file, []byte(js), 0666); err != nil {
112 fmt.Fprintf(os.Stderr, "go: writing action graph: %v\n", err)
113 base.SetExitStatus(1)
114 }
115 }
116 }
117 writeActionGraph()
118
119 b.readySema = make(chan bool, len(all))
120
121
122 for _, a := range all {
123 for _, a1 := range a.Deps {
124 a1.triggers = append(a1.triggers, a)
125 }
126 a.pending = len(a.Deps)
127 if a.pending == 0 {
128 b.ready.push(a)
129 b.readySema <- true
130 }
131 }
132
133
134
135 handle := func(ctx context.Context, a *Action) {
136 if a.json != nil {
137 a.json.TimeStart = time.Now()
138 }
139 var err error
140 if a.Actor != nil && (!a.Failed || a.IgnoreFail) {
141
142 desc := "Executing action (" + a.Mode
143 if a.Package != nil {
144 desc += " " + a.Package.Desc()
145 }
146 desc += ")"
147 ctx, span := trace.StartSpan(ctx, desc)
148 a.traceSpan = span
149 for _, d := range a.Deps {
150 trace.Flow(ctx, d.traceSpan, a.traceSpan)
151 }
152 err = a.Actor.Act(b, ctx, a)
153 span.Done()
154 }
155 if a.json != nil {
156 a.json.TimeDone = time.Now()
157 }
158
159
160
161 b.exec.Lock()
162 defer b.exec.Unlock()
163
164 if err != nil {
165 if b.AllowErrors && a.Package != nil {
166 if a.Package.Error == nil {
167 a.Package.Error = &load.PackageError{Err: err}
168 a.Package.Incomplete = true
169 }
170 } else {
171 var ipe load.ImportPathError
172 if a.Package != nil && (!errors.As(err, &ipe) || ipe.ImportPath() != a.Package.ImportPath) {
173 err = fmt.Errorf("%s: %v", a.Package.ImportPath, err)
174 }
175 base.Errorf("%s", err)
176 }
177 a.Failed = true
178 }
179
180 for _, a0 := range a.triggers {
181 if a.Failed {
182 a0.Failed = true
183 }
184 if a0.pending--; a0.pending == 0 {
185 b.ready.push(a0)
186 b.readySema <- true
187 }
188 }
189
190 if a == root {
191 close(b.readySema)
192 }
193 }
194
195 var wg sync.WaitGroup
196
197
198
199
200
201 par := cfg.BuildP
202 if cfg.BuildN {
203 par = 1
204 }
205 for i := 0; i < par; i++ {
206 wg.Add(1)
207 go func() {
208 ctx := trace.StartGoroutine(ctx)
209 defer wg.Done()
210 for {
211 select {
212 case _, ok := <-b.readySema:
213 if !ok {
214 return
215 }
216
217
218 b.exec.Lock()
219 a := b.ready.pop()
220 b.exec.Unlock()
221 handle(ctx, a)
222 case <-base.Interrupted:
223 base.SetExitStatus(1)
224 return
225 }
226 }
227 }()
228 }
229
230 wg.Wait()
231
232
233 writeActionGraph()
234 }
235
236
237 func (b *Builder) buildActionID(a *Action) cache.ActionID {
238 p := a.Package
239 h := cache.NewHash("build " + p.ImportPath)
240
241
242
243
244
245
246 fmt.Fprintf(h, "compile\n")
247
248
249
250 if cfg.BuildTrimpath {
251
252
253
254 if p.Module != nil {
255 fmt.Fprintf(h, "module %s@%s\n", p.Module.Path, p.Module.Version)
256 }
257 } else if p.Goroot {
258
259
260
261
262
263
264
265
266
267
268
269
270
271 } else if !strings.HasPrefix(p.Dir, b.WorkDir) {
272
273
274
275 fmt.Fprintf(h, "dir %s\n", p.Dir)
276 }
277
278 if p.Module != nil {
279 fmt.Fprintf(h, "go %s\n", p.Module.GoVersion)
280 }
281 fmt.Fprintf(h, "goos %s goarch %s\n", cfg.Goos, cfg.Goarch)
282 fmt.Fprintf(h, "import %q\n", p.ImportPath)
283 fmt.Fprintf(h, "omitdebug %v standard %v local %v prefix %q\n", p.Internal.OmitDebug, p.Standard, p.Internal.Local, p.Internal.LocalPrefix)
284 if cfg.BuildTrimpath {
285 fmt.Fprintln(h, "trimpath")
286 }
287 if p.Internal.ForceLibrary {
288 fmt.Fprintf(h, "forcelibrary\n")
289 }
290 if len(p.CgoFiles)+len(p.SwigFiles)+len(p.SwigCXXFiles) > 0 {
291 fmt.Fprintf(h, "cgo %q\n", b.toolID("cgo"))
292 cppflags, cflags, cxxflags, fflags, ldflags, _ := b.CFlags(p)
293
294 ccExe := b.ccExe()
295 fmt.Fprintf(h, "CC=%q %q %q %q\n", ccExe, cppflags, cflags, ldflags)
296
297
298 if ccID, _, err := b.gccToolID(ccExe[0], "c"); err == nil {
299 fmt.Fprintf(h, "CC ID=%q\n", ccID)
300 }
301 if len(p.CXXFiles)+len(p.SwigCXXFiles) > 0 {
302 cxxExe := b.cxxExe()
303 fmt.Fprintf(h, "CXX=%q %q\n", cxxExe, cxxflags)
304 if cxxID, _, err := b.gccToolID(cxxExe[0], "c++"); err == nil {
305 fmt.Fprintf(h, "CXX ID=%q\n", cxxID)
306 }
307 }
308 if len(p.FFiles) > 0 {
309 fcExe := b.fcExe()
310 fmt.Fprintf(h, "FC=%q %q\n", fcExe, fflags)
311 if fcID, _, err := b.gccToolID(fcExe[0], "f95"); err == nil {
312 fmt.Fprintf(h, "FC ID=%q\n", fcID)
313 }
314 }
315
316 }
317 if p.Internal.Cover.Mode != "" {
318 fmt.Fprintf(h, "cover %q %q\n", p.Internal.Cover.Mode, b.toolID("cover"))
319 }
320 if p.Internal.FuzzInstrument {
321 if fuzzFlags := fuzzInstrumentFlags(); fuzzFlags != nil {
322 fmt.Fprintf(h, "fuzz %q\n", fuzzFlags)
323 }
324 }
325 if p.Internal.BuildInfo != nil {
326 fmt.Fprintf(h, "modinfo %q\n", p.Internal.BuildInfo.String())
327 }
328
329
330 switch cfg.BuildToolchainName {
331 default:
332 base.Fatalf("buildActionID: unknown build toolchain %q", cfg.BuildToolchainName)
333 case "gc":
334 fmt.Fprintf(h, "compile %s %q %q\n", b.toolID("compile"), forcedGcflags, p.Internal.Gcflags)
335 if len(p.SFiles) > 0 {
336 fmt.Fprintf(h, "asm %q %q %q\n", b.toolID("asm"), forcedAsmflags, p.Internal.Asmflags)
337 }
338
339
340 key, val := cfg.GetArchEnv()
341 fmt.Fprintf(h, "%s=%s\n", key, val)
342
343 if cfg.CleanGOEXPERIMENT != "" {
344 fmt.Fprintf(h, "GOEXPERIMENT=%q\n", cfg.CleanGOEXPERIMENT)
345 }
346
347
348
349
350
351
352 magic := []string{
353 "GOCLOBBERDEADHASH",
354 "GOSSAFUNC",
355 "GOSSADIR",
356 "GOCOMPILEDEBUG",
357 }
358 for _, env := range magic {
359 if x := os.Getenv(env); x != "" {
360 fmt.Fprintf(h, "magic %s=%s\n", env, x)
361 }
362 }
363
364 case "gccgo":
365 id, _, err := b.gccToolID(BuildToolchain.compiler(), "go")
366 if err != nil {
367 base.Fatalf("%v", err)
368 }
369 fmt.Fprintf(h, "compile %s %q %q\n", id, forcedGccgoflags, p.Internal.Gccgoflags)
370 fmt.Fprintf(h, "pkgpath %s\n", gccgoPkgpath(p))
371 fmt.Fprintf(h, "ar %q\n", BuildToolchain.(gccgoToolchain).ar())
372 if len(p.SFiles) > 0 {
373 id, _, _ = b.gccToolID(BuildToolchain.compiler(), "assembler-with-cpp")
374
375
376 fmt.Fprintf(h, "asm %q\n", id)
377 }
378 }
379
380
381 inputFiles := str.StringList(
382 p.GoFiles,
383 p.CgoFiles,
384 p.CFiles,
385 p.CXXFiles,
386 p.FFiles,
387 p.MFiles,
388 p.HFiles,
389 p.SFiles,
390 p.SysoFiles,
391 p.SwigFiles,
392 p.SwigCXXFiles,
393 p.EmbedFiles,
394 )
395 for _, file := range inputFiles {
396 fmt.Fprintf(h, "file %s %s\n", file, b.fileHash(filepath.Join(p.Dir, file)))
397 }
398 for _, a1 := range a.Deps {
399 p1 := a1.Package
400 if p1 != nil {
401 fmt.Fprintf(h, "import %s %s\n", p1.ImportPath, contentID(a1.buildID))
402 }
403 if a1.Mode == "preprocess PGO profile" {
404 fmt.Fprintf(h, "pgofile %s\n", b.fileHash(a1.built))
405 }
406 }
407
408 return h.Sum()
409 }
410
411
412
413 func (b *Builder) needCgoHdr(a *Action) bool {
414
415 if !b.IsCmdList && (a.Package.UsesCgo() || a.Package.UsesSwig()) && (cfg.BuildBuildmode == "c-archive" || cfg.BuildBuildmode == "c-shared") {
416 for _, t1 := range a.triggers {
417 if t1.Mode == "install header" {
418 return true
419 }
420 }
421 for _, t1 := range a.triggers {
422 for _, t2 := range t1.triggers {
423 if t2.Mode == "install header" {
424 return true
425 }
426 }
427 }
428 }
429 return false
430 }
431
432
433
434
435 func allowedVersion(v string) bool {
436
437 if v == "" {
438 return true
439 }
440 return gover.Compare(gover.Local(), v) >= 0
441 }
442
443 const (
444 needBuild uint32 = 1 << iota
445 needCgoHdr
446 needVet
447 needCompiledGoFiles
448 needCovMetaFile
449 needStale
450 )
451
452
453
454 func (b *Builder) build(ctx context.Context, a *Action) (err error) {
455 p := a.Package
456 sh := b.Shell(a)
457
458 bit := func(x uint32, b bool) uint32 {
459 if b {
460 return x
461 }
462 return 0
463 }
464
465 cachedBuild := false
466 needCovMeta := p.Internal.Cover.GenMeta
467 need := bit(needBuild, !b.IsCmdList && a.needBuild || b.NeedExport) |
468 bit(needCgoHdr, b.needCgoHdr(a)) |
469 bit(needVet, a.needVet) |
470 bit(needCovMetaFile, needCovMeta) |
471 bit(needCompiledGoFiles, b.NeedCompiledGoFiles)
472
473 if !p.BinaryOnly {
474 if b.useCache(a, b.buildActionID(a), p.Target, need&needBuild != 0) {
475
476
477
478
479
480 cachedBuild = true
481 a.output = []byte{}
482 need &^= needBuild
483 if b.NeedExport {
484 p.Export = a.built
485 p.BuildID = a.buildID
486 }
487 if need&needCompiledGoFiles != 0 {
488 if err := b.loadCachedCompiledGoFiles(a); err == nil {
489 need &^= needCompiledGoFiles
490 }
491 }
492 }
493
494
495
496 if !cachedBuild && need&needCompiledGoFiles != 0 {
497 if err := b.loadCachedCompiledGoFiles(a); err == nil {
498 need &^= needCompiledGoFiles
499 }
500 }
501
502 if need == 0 {
503 return nil
504 }
505 defer b.flushOutput(a)
506 }
507
508 defer func() {
509 if err != nil && b.IsCmdList && b.NeedError && p.Error == nil {
510 p.Error = &load.PackageError{Err: err}
511 }
512 }()
513 if cfg.BuildN {
514
515
516
517
518
519 sh.Print("\n#\n# " + p.ImportPath + "\n#\n\n")
520 }
521
522 if cfg.BuildV {
523 sh.Print(p.ImportPath + "\n")
524 }
525
526 if p.Error != nil {
527
528
529 return p.Error
530 }
531
532 if p.BinaryOnly {
533 p.Stale = true
534 p.StaleReason = "binary-only packages are no longer supported"
535 if b.IsCmdList {
536 return nil
537 }
538 return errors.New("binary-only packages are no longer supported")
539 }
540
541 if p.Module != nil && !allowedVersion(p.Module.GoVersion) {
542 return errors.New("module requires Go " + p.Module.GoVersion + " or later")
543 }
544
545 if err := b.checkDirectives(a); err != nil {
546 return err
547 }
548
549 if err := sh.Mkdir(a.Objdir); err != nil {
550 return err
551 }
552 objdir := a.Objdir
553
554
555 if cachedBuild && need&needCgoHdr != 0 {
556 if err := b.loadCachedCgoHdr(a); err == nil {
557 need &^= needCgoHdr
558 }
559 }
560
561
562
563 if cachedBuild && need&needCovMetaFile != 0 {
564 bact := a.Actor.(*buildActor)
565 if err := b.loadCachedObjdirFile(a, cache.Default(), bact.covMetaFileName); err == nil {
566 need &^= needCovMetaFile
567 }
568 }
569
570
571
572
573
574 if need == needVet {
575 if err := b.loadCachedVet(a); err == nil {
576 need &^= needVet
577 }
578 }
579 if need == 0 {
580 return nil
581 }
582
583 if err := AllowInstall(a); err != nil {
584 return err
585 }
586
587
588 dir, _ := filepath.Split(a.Target)
589 if dir != "" {
590 if err := sh.Mkdir(dir); err != nil {
591 return err
592 }
593 }
594
595 gofiles := str.StringList(p.GoFiles)
596 cgofiles := str.StringList(p.CgoFiles)
597 cfiles := str.StringList(p.CFiles)
598 sfiles := str.StringList(p.SFiles)
599 cxxfiles := str.StringList(p.CXXFiles)
600 var objects, cgoObjects, pcCFLAGS, pcLDFLAGS []string
601
602 if p.UsesCgo() || p.UsesSwig() {
603 if pcCFLAGS, pcLDFLAGS, err = b.getPkgConfigFlags(a); err != nil {
604 return
605 }
606 }
607
608
609
610
611
612 nonGoFileLists := [][]string{p.CFiles, p.SFiles, p.CXXFiles, p.HFiles, p.FFiles}
613 OverlayLoop:
614 for _, fs := range nonGoFileLists {
615 for _, f := range fs {
616 if _, ok := fsys.OverlayPath(mkAbs(p.Dir, f)); ok {
617 a.nonGoOverlay = make(map[string]string)
618 break OverlayLoop
619 }
620 }
621 }
622 if a.nonGoOverlay != nil {
623 for _, fs := range nonGoFileLists {
624 for i := range fs {
625 from := mkAbs(p.Dir, fs[i])
626 opath, _ := fsys.OverlayPath(from)
627 dst := objdir + filepath.Base(fs[i])
628 if err := sh.CopyFile(dst, opath, 0666, false); err != nil {
629 return err
630 }
631 a.nonGoOverlay[from] = dst
632 }
633 }
634 }
635
636
637 if p.Internal.Cover.Mode != "" {
638 outfiles := []string{}
639 infiles := []string{}
640 for i, file := range str.StringList(gofiles, cgofiles) {
641 if base.IsTestFile(file) {
642 continue
643 }
644
645 var sourceFile string
646 var coverFile string
647 var key string
648 if base, found := strings.CutSuffix(file, ".cgo1.go"); found {
649
650 base = filepath.Base(base)
651 sourceFile = file
652 coverFile = objdir + base + ".cgo1.go"
653 key = base + ".go"
654 } else {
655 sourceFile = filepath.Join(p.Dir, file)
656 coverFile = objdir + file
657 key = file
658 }
659 coverFile = strings.TrimSuffix(coverFile, ".go") + ".cover.go"
660 if cfg.Experiment.CoverageRedesign {
661 infiles = append(infiles, sourceFile)
662 outfiles = append(outfiles, coverFile)
663 } else {
664 cover := p.Internal.CoverVars[key]
665 if cover == nil {
666 continue
667 }
668 if err := b.cover(a, coverFile, sourceFile, cover.Var); err != nil {
669 return err
670 }
671 }
672 if i < len(gofiles) {
673 gofiles[i] = coverFile
674 } else {
675 cgofiles[i-len(gofiles)] = coverFile
676 }
677 }
678
679 if cfg.Experiment.CoverageRedesign {
680 if len(infiles) != 0 {
681
682
683
684
685
686
687
688
689
690 sum := sha256.Sum256([]byte(a.Package.ImportPath))
691 coverVar := fmt.Sprintf("goCover_%x_", sum[:6])
692 mode := a.Package.Internal.Cover.Mode
693 if mode == "" {
694 panic("covermode should be set at this point")
695 }
696 if newoutfiles, err := b.cover2(a, infiles, outfiles, coverVar, mode); err != nil {
697 return err
698 } else {
699 outfiles = newoutfiles
700 gofiles = append([]string{newoutfiles[0]}, gofiles...)
701 }
702 } else {
703
704
705
706
707
708 p.Internal.Cover.Mode = ""
709 }
710 if ba, ok := a.Actor.(*buildActor); ok && ba.covMetaFileName != "" {
711 b.cacheObjdirFile(a, cache.Default(), ba.covMetaFileName)
712 }
713 }
714 }
715
716
717
718
719
720
721
722 if p.UsesSwig() {
723 outGo, outC, outCXX, err := b.swig(a, objdir, pcCFLAGS)
724 if err != nil {
725 return err
726 }
727 cgofiles = append(cgofiles, outGo...)
728 cfiles = append(cfiles, outC...)
729 cxxfiles = append(cxxfiles, outCXX...)
730 }
731
732
733 if p.UsesCgo() || p.UsesSwig() {
734
735
736
737
738 var gccfiles []string
739 gccfiles = append(gccfiles, cfiles...)
740 cfiles = nil
741 if p.Standard && p.ImportPath == "runtime/cgo" {
742 filter := func(files, nongcc, gcc []string) ([]string, []string) {
743 for _, f := range files {
744 if strings.HasPrefix(f, "gcc_") {
745 gcc = append(gcc, f)
746 } else {
747 nongcc = append(nongcc, f)
748 }
749 }
750 return nongcc, gcc
751 }
752 sfiles, gccfiles = filter(sfiles, sfiles[:0], gccfiles)
753 } else {
754 for _, sfile := range sfiles {
755 data, err := os.ReadFile(filepath.Join(p.Dir, sfile))
756 if err == nil {
757 if bytes.HasPrefix(data, []byte("TEXT")) || bytes.Contains(data, []byte("\nTEXT")) ||
758 bytes.HasPrefix(data, []byte("DATA")) || bytes.Contains(data, []byte("\nDATA")) ||
759 bytes.HasPrefix(data, []byte("GLOBL")) || bytes.Contains(data, []byte("\nGLOBL")) {
760 return fmt.Errorf("package using cgo has Go assembly file %s", sfile)
761 }
762 }
763 }
764 gccfiles = append(gccfiles, sfiles...)
765 sfiles = nil
766 }
767
768 outGo, outObj, err := b.cgo(a, base.Tool("cgo"), objdir, pcCFLAGS, pcLDFLAGS, mkAbsFiles(p.Dir, cgofiles), gccfiles, cxxfiles, p.MFiles, p.FFiles)
769
770
771 cxxfiles = nil
772
773 if err != nil {
774 return err
775 }
776 if cfg.BuildToolchainName == "gccgo" {
777 cgoObjects = append(cgoObjects, a.Objdir+"_cgo_flags")
778 }
779 cgoObjects = append(cgoObjects, outObj...)
780 gofiles = append(gofiles, outGo...)
781
782 switch cfg.BuildBuildmode {
783 case "c-archive", "c-shared":
784 b.cacheCgoHdr(a)
785 }
786 }
787
788 var srcfiles []string
789 srcfiles = append(srcfiles, gofiles...)
790 srcfiles = append(srcfiles, sfiles...)
791 srcfiles = append(srcfiles, cfiles...)
792 srcfiles = append(srcfiles, cxxfiles...)
793 b.cacheSrcFiles(a, srcfiles)
794
795
796 need &^= needCgoHdr
797
798
799 if len(gofiles) == 0 {
800 return &load.NoGoError{Package: p}
801 }
802
803
804 if need&needVet != 0 {
805 buildVetConfig(a, srcfiles)
806 need &^= needVet
807 }
808 if need&needCompiledGoFiles != 0 {
809 if err := b.loadCachedCompiledGoFiles(a); err != nil {
810 return fmt.Errorf("loading compiled Go files from cache: %w", err)
811 }
812 need &^= needCompiledGoFiles
813 }
814 if need == 0 {
815
816 return nil
817 }
818
819
820 symabis, err := BuildToolchain.symabis(b, a, sfiles)
821 if err != nil {
822 return err
823 }
824
825
826
827
828
829
830
831 var icfg bytes.Buffer
832 fmt.Fprintf(&icfg, "# import config\n")
833 for i, raw := range p.Internal.RawImports {
834 final := p.Imports[i]
835 if final != raw {
836 fmt.Fprintf(&icfg, "importmap %s=%s\n", raw, final)
837 }
838 }
839 for _, a1 := range a.Deps {
840 p1 := a1.Package
841 if p1 == nil || p1.ImportPath == "" || a1.built == "" {
842 continue
843 }
844 fmt.Fprintf(&icfg, "packagefile %s=%s\n", p1.ImportPath, a1.built)
845 }
846
847
848
849 var embedcfg []byte
850 if len(p.Internal.Embed) > 0 {
851 var embed struct {
852 Patterns map[string][]string
853 Files map[string]string
854 }
855 embed.Patterns = p.Internal.Embed
856 embed.Files = make(map[string]string)
857 for _, file := range p.EmbedFiles {
858 embed.Files[file] = filepath.Join(p.Dir, file)
859 }
860 js, err := json.MarshalIndent(&embed, "", "\t")
861 if err != nil {
862 return fmt.Errorf("marshal embedcfg: %v", err)
863 }
864 embedcfg = js
865 }
866
867
868 var pgoProfile string
869 for _, a1 := range a.Deps {
870 if a1.Mode != "preprocess PGO profile" {
871 continue
872 }
873 if pgoProfile != "" {
874 return fmt.Errorf("action contains multiple PGO profile dependencies")
875 }
876 pgoProfile = a1.built
877 }
878
879 if p.Internal.BuildInfo != nil && cfg.ModulesEnabled {
880 prog := modload.ModInfoProg(p.Internal.BuildInfo.String(), cfg.BuildToolchainName == "gccgo")
881 if len(prog) > 0 {
882 if err := sh.writeFile(objdir+"_gomod_.go", prog); err != nil {
883 return err
884 }
885 gofiles = append(gofiles, objdir+"_gomod_.go")
886 }
887 }
888
889
890 objpkg := objdir + "_pkg_.a"
891 ofile, out, err := BuildToolchain.gc(b, a, objpkg, icfg.Bytes(), embedcfg, symabis, len(sfiles) > 0, pgoProfile, gofiles)
892 if err := sh.reportCmd("", "", out, err); err != nil {
893 return err
894 }
895 if ofile != objpkg {
896 objects = append(objects, ofile)
897 }
898
899
900
901
902 _goos_goarch := "_" + cfg.Goos + "_" + cfg.Goarch
903 _goos := "_" + cfg.Goos
904 _goarch := "_" + cfg.Goarch
905 for _, file := range p.HFiles {
906 name, ext := fileExtSplit(file)
907 switch {
908 case strings.HasSuffix(name, _goos_goarch):
909 targ := file[:len(name)-len(_goos_goarch)] + "_GOOS_GOARCH." + ext
910 if err := sh.CopyFile(objdir+targ, filepath.Join(p.Dir, file), 0666, true); err != nil {
911 return err
912 }
913 case strings.HasSuffix(name, _goarch):
914 targ := file[:len(name)-len(_goarch)] + "_GOARCH." + ext
915 if err := sh.CopyFile(objdir+targ, filepath.Join(p.Dir, file), 0666, true); err != nil {
916 return err
917 }
918 case strings.HasSuffix(name, _goos):
919 targ := file[:len(name)-len(_goos)] + "_GOOS." + ext
920 if err := sh.CopyFile(objdir+targ, filepath.Join(p.Dir, file), 0666, true); err != nil {
921 return err
922 }
923 }
924 }
925
926 for _, file := range cfiles {
927 out := file[:len(file)-len(".c")] + ".o"
928 if err := BuildToolchain.cc(b, a, objdir+out, file); err != nil {
929 return err
930 }
931 objects = append(objects, out)
932 }
933
934
935 if len(sfiles) > 0 {
936 ofiles, err := BuildToolchain.asm(b, a, sfiles)
937 if err != nil {
938 return err
939 }
940 objects = append(objects, ofiles...)
941 }
942
943
944
945
946 if a.buildID != "" && cfg.BuildToolchainName == "gccgo" {
947 switch cfg.Goos {
948 case "aix", "android", "dragonfly", "freebsd", "illumos", "linux", "netbsd", "openbsd", "solaris":
949 asmfile, err := b.gccgoBuildIDFile(a)
950 if err != nil {
951 return err
952 }
953 ofiles, err := BuildToolchain.asm(b, a, []string{asmfile})
954 if err != nil {
955 return err
956 }
957 objects = append(objects, ofiles...)
958 }
959 }
960
961
962
963
964
965 objects = append(objects, cgoObjects...)
966
967
968 for _, syso := range p.SysoFiles {
969 objects = append(objects, filepath.Join(p.Dir, syso))
970 }
971
972
973
974
975
976
977 if len(objects) > 0 {
978 if err := BuildToolchain.pack(b, a, objpkg, objects); err != nil {
979 return err
980 }
981 }
982
983 if err := b.updateBuildID(a, objpkg, true); err != nil {
984 return err
985 }
986
987 a.built = objpkg
988 return nil
989 }
990
991 func (b *Builder) checkDirectives(a *Action) error {
992 var msg *bytes.Buffer
993 p := a.Package
994 var seen map[string]token.Position
995 for _, d := range p.Internal.Build.Directives {
996 if strings.HasPrefix(d.Text, "//go:debug") {
997 key, _, err := load.ParseGoDebug(d.Text)
998 if err != nil && err != load.ErrNotGoDebug {
999 if msg == nil {
1000 msg = new(bytes.Buffer)
1001 }
1002 fmt.Fprintf(msg, "%s: invalid //go:debug: %v\n", d.Pos, err)
1003 continue
1004 }
1005 if pos, ok := seen[key]; ok {
1006 fmt.Fprintf(msg, "%s: repeated //go:debug for %v\n\t%s: previous //go:debug\n", d.Pos, key, pos)
1007 continue
1008 }
1009 if seen == nil {
1010 seen = make(map[string]token.Position)
1011 }
1012 seen[key] = d.Pos
1013 }
1014 }
1015 if msg != nil {
1016
1017
1018
1019 err := errors.New("invalid directive")
1020 return b.Shell(a).reportCmd("", "", msg.Bytes(), err)
1021 }
1022 return nil
1023 }
1024
1025 func (b *Builder) cacheObjdirFile(a *Action, c cache.Cache, name string) error {
1026 f, err := os.Open(a.Objdir + name)
1027 if err != nil {
1028 return err
1029 }
1030 defer f.Close()
1031 _, _, err = c.Put(cache.Subkey(a.actionID, name), f)
1032 return err
1033 }
1034
1035 func (b *Builder) findCachedObjdirFile(a *Action, c cache.Cache, name string) (string, error) {
1036 file, _, err := cache.GetFile(c, cache.Subkey(a.actionID, name))
1037 if err != nil {
1038 return "", fmt.Errorf("loading cached file %s: %w", name, err)
1039 }
1040 return file, nil
1041 }
1042
1043 func (b *Builder) loadCachedObjdirFile(a *Action, c cache.Cache, name string) error {
1044 cached, err := b.findCachedObjdirFile(a, c, name)
1045 if err != nil {
1046 return err
1047 }
1048 return b.Shell(a).CopyFile(a.Objdir+name, cached, 0666, true)
1049 }
1050
1051 func (b *Builder) cacheCgoHdr(a *Action) {
1052 c := cache.Default()
1053 b.cacheObjdirFile(a, c, "_cgo_install.h")
1054 }
1055
1056 func (b *Builder) loadCachedCgoHdr(a *Action) error {
1057 c := cache.Default()
1058 return b.loadCachedObjdirFile(a, c, "_cgo_install.h")
1059 }
1060
1061 func (b *Builder) cacheSrcFiles(a *Action, srcfiles []string) {
1062 c := cache.Default()
1063 var buf bytes.Buffer
1064 for _, file := range srcfiles {
1065 if !strings.HasPrefix(file, a.Objdir) {
1066
1067 buf.WriteString("./")
1068 buf.WriteString(file)
1069 buf.WriteString("\n")
1070 continue
1071 }
1072 name := file[len(a.Objdir):]
1073 buf.WriteString(name)
1074 buf.WriteString("\n")
1075 if err := b.cacheObjdirFile(a, c, name); err != nil {
1076 return
1077 }
1078 }
1079 cache.PutBytes(c, cache.Subkey(a.actionID, "srcfiles"), buf.Bytes())
1080 }
1081
1082 func (b *Builder) loadCachedVet(a *Action) error {
1083 c := cache.Default()
1084 list, _, err := cache.GetBytes(c, cache.Subkey(a.actionID, "srcfiles"))
1085 if err != nil {
1086 return fmt.Errorf("reading srcfiles list: %w", err)
1087 }
1088 var srcfiles []string
1089 for _, name := range strings.Split(string(list), "\n") {
1090 if name == "" {
1091 continue
1092 }
1093 if strings.HasPrefix(name, "./") {
1094 srcfiles = append(srcfiles, name[2:])
1095 continue
1096 }
1097 if err := b.loadCachedObjdirFile(a, c, name); err != nil {
1098 return err
1099 }
1100 srcfiles = append(srcfiles, a.Objdir+name)
1101 }
1102 buildVetConfig(a, srcfiles)
1103 return nil
1104 }
1105
1106 func (b *Builder) loadCachedCompiledGoFiles(a *Action) error {
1107 c := cache.Default()
1108 list, _, err := cache.GetBytes(c, cache.Subkey(a.actionID, "srcfiles"))
1109 if err != nil {
1110 return fmt.Errorf("reading srcfiles list: %w", err)
1111 }
1112 var gofiles []string
1113 for _, name := range strings.Split(string(list), "\n") {
1114 if name == "" {
1115 continue
1116 } else if !strings.HasSuffix(name, ".go") {
1117 continue
1118 }
1119 if strings.HasPrefix(name, "./") {
1120 gofiles = append(gofiles, name[len("./"):])
1121 continue
1122 }
1123 file, err := b.findCachedObjdirFile(a, c, name)
1124 if err != nil {
1125 return fmt.Errorf("finding %s: %w", name, err)
1126 }
1127 gofiles = append(gofiles, file)
1128 }
1129 a.Package.CompiledGoFiles = gofiles
1130 return nil
1131 }
1132
1133
1134 type vetConfig struct {
1135 ID string
1136 Compiler string
1137 Dir string
1138 ImportPath string
1139 GoFiles []string
1140 NonGoFiles []string
1141 IgnoredFiles []string
1142
1143 ImportMap map[string]string
1144 PackageFile map[string]string
1145 Standard map[string]bool
1146 PackageVetx map[string]string
1147 VetxOnly bool
1148 VetxOutput string
1149 GoVersion string
1150
1151 SucceedOnTypecheckFailure bool
1152 }
1153
1154 func buildVetConfig(a *Action, srcfiles []string) {
1155
1156
1157 var gofiles, nongofiles []string
1158 for _, name := range srcfiles {
1159 if strings.HasSuffix(name, ".go") {
1160 gofiles = append(gofiles, name)
1161 } else {
1162 nongofiles = append(nongofiles, name)
1163 }
1164 }
1165
1166 ignored := str.StringList(a.Package.IgnoredGoFiles, a.Package.IgnoredOtherFiles)
1167
1168
1169
1170
1171
1172 vcfg := &vetConfig{
1173 ID: a.Package.ImportPath,
1174 Compiler: cfg.BuildToolchainName,
1175 Dir: a.Package.Dir,
1176 GoFiles: mkAbsFiles(a.Package.Dir, gofiles),
1177 NonGoFiles: mkAbsFiles(a.Package.Dir, nongofiles),
1178 IgnoredFiles: mkAbsFiles(a.Package.Dir, ignored),
1179 ImportPath: a.Package.ImportPath,
1180 ImportMap: make(map[string]string),
1181 PackageFile: make(map[string]string),
1182 Standard: make(map[string]bool),
1183 }
1184 if a.Package.Module != nil {
1185 v := a.Package.Module.GoVersion
1186 if v == "" {
1187 v = gover.DefaultGoModVersion
1188 }
1189 vcfg.GoVersion = "go" + v
1190 }
1191 a.vetCfg = vcfg
1192 for i, raw := range a.Package.Internal.RawImports {
1193 final := a.Package.Imports[i]
1194 vcfg.ImportMap[raw] = final
1195 }
1196
1197
1198
1199 vcfgMapped := make(map[string]bool)
1200 for _, p := range vcfg.ImportMap {
1201 vcfgMapped[p] = true
1202 }
1203
1204 for _, a1 := range a.Deps {
1205 p1 := a1.Package
1206 if p1 == nil || p1.ImportPath == "" {
1207 continue
1208 }
1209
1210
1211 if !vcfgMapped[p1.ImportPath] {
1212 vcfg.ImportMap[p1.ImportPath] = p1.ImportPath
1213 }
1214 if a1.built != "" {
1215 vcfg.PackageFile[p1.ImportPath] = a1.built
1216 }
1217 if p1.Standard {
1218 vcfg.Standard[p1.ImportPath] = true
1219 }
1220 }
1221 }
1222
1223
1224
1225 var VetTool string
1226
1227
1228
1229 var VetFlags []string
1230
1231
1232 var VetExplicit bool
1233
1234 func (b *Builder) vet(ctx context.Context, a *Action) error {
1235
1236
1237
1238 a.Failed = false
1239
1240 if a.Deps[0].Failed {
1241
1242
1243
1244 return nil
1245 }
1246
1247 vcfg := a.Deps[0].vetCfg
1248 if vcfg == nil {
1249
1250 return fmt.Errorf("vet config not found")
1251 }
1252
1253 sh := b.Shell(a)
1254
1255 vcfg.VetxOnly = a.VetxOnly
1256 vcfg.VetxOutput = a.Objdir + "vet.out"
1257 vcfg.PackageVetx = make(map[string]string)
1258
1259 h := cache.NewHash("vet " + a.Package.ImportPath)
1260 fmt.Fprintf(h, "vet %q\n", b.toolID("vet"))
1261
1262 vetFlags := VetFlags
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280 if a.Package.Goroot && !VetExplicit && VetTool == "" {
1281
1282
1283
1284
1285
1286
1287
1288
1289 vetFlags = []string{"-unsafeptr=false"}
1290
1291
1292
1293
1294
1295
1296
1297
1298 if cfg.CmdName == "test" {
1299 vetFlags = append(vetFlags, "-unreachable=false")
1300 }
1301 }
1302
1303
1304
1305
1306
1307
1308 fmt.Fprintf(h, "vetflags %q\n", vetFlags)
1309
1310 fmt.Fprintf(h, "pkg %q\n", a.Deps[0].actionID)
1311 for _, a1 := range a.Deps {
1312 if a1.Mode == "vet" && a1.built != "" {
1313 fmt.Fprintf(h, "vetout %q %s\n", a1.Package.ImportPath, b.fileHash(a1.built))
1314 vcfg.PackageVetx[a1.Package.ImportPath] = a1.built
1315 }
1316 }
1317 key := cache.ActionID(h.Sum())
1318
1319 if vcfg.VetxOnly && !cfg.BuildA {
1320 c := cache.Default()
1321 if file, _, err := cache.GetFile(c, key); err == nil {
1322 a.built = file
1323 return nil
1324 }
1325 }
1326
1327 js, err := json.MarshalIndent(vcfg, "", "\t")
1328 if err != nil {
1329 return fmt.Errorf("internal error marshaling vet config: %v", err)
1330 }
1331 js = append(js, '\n')
1332 if err := sh.writeFile(a.Objdir+"vet.cfg", js); err != nil {
1333 return err
1334 }
1335
1336
1337 env := b.cCompilerEnv()
1338 if cfg.BuildToolchainName == "gccgo" {
1339 env = append(env, "GCCGO="+BuildToolchain.compiler())
1340 }
1341
1342 p := a.Package
1343 tool := VetTool
1344 if tool == "" {
1345 tool = base.Tool("vet")
1346 }
1347 runErr := sh.run(p.Dir, p.ImportPath, env, cfg.BuildToolexec, tool, vetFlags, a.Objdir+"vet.cfg")
1348
1349
1350 if f, err := os.Open(vcfg.VetxOutput); err == nil {
1351 a.built = vcfg.VetxOutput
1352 cache.Default().Put(key, f)
1353 f.Close()
1354 }
1355
1356 return runErr
1357 }
1358
1359
1360 func (b *Builder) linkActionID(a *Action) cache.ActionID {
1361 p := a.Package
1362 h := cache.NewHash("link " + p.ImportPath)
1363
1364
1365 fmt.Fprintf(h, "link\n")
1366 fmt.Fprintf(h, "buildmode %s goos %s goarch %s\n", cfg.BuildBuildmode, cfg.Goos, cfg.Goarch)
1367 fmt.Fprintf(h, "import %q\n", p.ImportPath)
1368 fmt.Fprintf(h, "omitdebug %v standard %v local %v prefix %q\n", p.Internal.OmitDebug, p.Standard, p.Internal.Local, p.Internal.LocalPrefix)
1369 if cfg.BuildTrimpath {
1370 fmt.Fprintln(h, "trimpath")
1371 }
1372
1373
1374 b.printLinkerConfig(h, p)
1375
1376
1377 for _, a1 := range a.Deps {
1378 p1 := a1.Package
1379 if p1 != nil {
1380 if a1.built != "" || a1.buildID != "" {
1381 buildID := a1.buildID
1382 if buildID == "" {
1383 buildID = b.buildID(a1.built)
1384 }
1385 fmt.Fprintf(h, "packagefile %s=%s\n", p1.ImportPath, contentID(buildID))
1386 }
1387
1388
1389 if p1.Name == "main" {
1390 fmt.Fprintf(h, "packagemain %s\n", a1.buildID)
1391 }
1392 if p1.Shlib != "" {
1393 fmt.Fprintf(h, "packageshlib %s=%s\n", p1.ImportPath, contentID(b.buildID(p1.Shlib)))
1394 }
1395 }
1396 }
1397
1398 return h.Sum()
1399 }
1400
1401
1402
1403 func (b *Builder) printLinkerConfig(h io.Writer, p *load.Package) {
1404 switch cfg.BuildToolchainName {
1405 default:
1406 base.Fatalf("linkActionID: unknown toolchain %q", cfg.BuildToolchainName)
1407
1408 case "gc":
1409 fmt.Fprintf(h, "link %s %q %s\n", b.toolID("link"), forcedLdflags, ldBuildmode)
1410 if p != nil {
1411 fmt.Fprintf(h, "linkflags %q\n", p.Internal.Ldflags)
1412 }
1413
1414
1415 key, val := cfg.GetArchEnv()
1416 fmt.Fprintf(h, "%s=%s\n", key, val)
1417
1418 if cfg.CleanGOEXPERIMENT != "" {
1419 fmt.Fprintf(h, "GOEXPERIMENT=%q\n", cfg.CleanGOEXPERIMENT)
1420 }
1421
1422
1423
1424 gorootFinal := cfg.GOROOT
1425 if cfg.BuildTrimpath {
1426 gorootFinal = ""
1427 }
1428 fmt.Fprintf(h, "GOROOT=%s\n", gorootFinal)
1429
1430
1431 fmt.Fprintf(h, "GO_EXTLINK_ENABLED=%s\n", cfg.Getenv("GO_EXTLINK_ENABLED"))
1432
1433
1434
1435
1436 case "gccgo":
1437 id, _, err := b.gccToolID(BuildToolchain.linker(), "go")
1438 if err != nil {
1439 base.Fatalf("%v", err)
1440 }
1441 fmt.Fprintf(h, "link %s %s\n", id, ldBuildmode)
1442
1443 }
1444 }
1445
1446
1447
1448 func (b *Builder) link(ctx context.Context, a *Action) (err error) {
1449 if b.useCache(a, b.linkActionID(a), a.Package.Target, !b.IsCmdList) || b.IsCmdList {
1450 return nil
1451 }
1452 defer b.flushOutput(a)
1453
1454 sh := b.Shell(a)
1455 if err := sh.Mkdir(a.Objdir); err != nil {
1456 return err
1457 }
1458
1459 importcfg := a.Objdir + "importcfg.link"
1460 if err := b.writeLinkImportcfg(a, importcfg); err != nil {
1461 return err
1462 }
1463
1464 if err := AllowInstall(a); err != nil {
1465 return err
1466 }
1467
1468
1469 dir, _ := filepath.Split(a.Target)
1470 if dir != "" {
1471 if err := sh.Mkdir(dir); err != nil {
1472 return err
1473 }
1474 }
1475
1476 if err := BuildToolchain.ld(b, a, a.Target, importcfg, a.Deps[0].built); err != nil {
1477 return err
1478 }
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496 if err := b.updateBuildID(a, a.Target, !a.Package.Internal.OmitDebug); err != nil {
1497 return err
1498 }
1499
1500 a.built = a.Target
1501 return nil
1502 }
1503
1504 func (b *Builder) writeLinkImportcfg(a *Action, file string) error {
1505
1506 var icfg bytes.Buffer
1507 for _, a1 := range a.Deps {
1508 p1 := a1.Package
1509 if p1 == nil {
1510 continue
1511 }
1512 fmt.Fprintf(&icfg, "packagefile %s=%s\n", p1.ImportPath, a1.built)
1513 if p1.Shlib != "" {
1514 fmt.Fprintf(&icfg, "packageshlib %s=%s\n", p1.ImportPath, p1.Shlib)
1515 }
1516 }
1517 info := ""
1518 if a.Package.Internal.BuildInfo != nil {
1519 info = a.Package.Internal.BuildInfo.String()
1520 }
1521 fmt.Fprintf(&icfg, "modinfo %q\n", modload.ModInfoData(info))
1522 return b.Shell(a).writeFile(file, icfg.Bytes())
1523 }
1524
1525
1526
1527 func (b *Builder) PkgconfigCmd() string {
1528 return envList("PKG_CONFIG", cfg.DefaultPkgConfig)[0]
1529 }
1530
1531
1532
1533
1534
1535
1536
1537 func splitPkgConfigOutput(out []byte) ([]string, error) {
1538 if len(out) == 0 {
1539 return nil, nil
1540 }
1541 var flags []string
1542 flag := make([]byte, 0, len(out))
1543 didQuote := false
1544 escaped := false
1545 quote := byte(0)
1546
1547 for _, c := range out {
1548 if escaped {
1549 if quote == '"' {
1550
1551
1552
1553 switch c {
1554 case '$', '`', '"', '\\', '\n':
1555
1556 default:
1557
1558 flag = append(flag, '\\', c)
1559 escaped = false
1560 continue
1561 }
1562 }
1563
1564 if c == '\n' {
1565
1566
1567 } else {
1568 flag = append(flag, c)
1569 }
1570 escaped = false
1571 continue
1572 }
1573
1574 if quote != 0 && c == quote {
1575 quote = 0
1576 continue
1577 }
1578 switch quote {
1579 case '\'':
1580
1581 flag = append(flag, c)
1582 continue
1583 case '"':
1584
1585
1586 switch c {
1587 case '`', '$', '\\':
1588 default:
1589 flag = append(flag, c)
1590 continue
1591 }
1592 }
1593
1594
1595
1596 switch c {
1597 case '|', '&', ';', '<', '>', '(', ')', '$', '`':
1598 return nil, fmt.Errorf("unexpected shell character %q in pkgconf output", c)
1599
1600 case '\\':
1601
1602
1603 escaped = true
1604 continue
1605
1606 case '"', '\'':
1607 quote = c
1608 didQuote = true
1609 continue
1610
1611 case ' ', '\t', '\n':
1612 if len(flag) > 0 || didQuote {
1613 flags = append(flags, string(flag))
1614 }
1615 flag, didQuote = flag[:0], false
1616 continue
1617 }
1618
1619 flag = append(flag, c)
1620 }
1621
1622
1623
1624
1625 if quote != 0 {
1626 return nil, errors.New("unterminated quoted string in pkgconf output")
1627 }
1628 if escaped {
1629 return nil, errors.New("broken character escaping in pkgconf output")
1630 }
1631
1632 if len(flag) > 0 || didQuote {
1633 flags = append(flags, string(flag))
1634 }
1635 return flags, nil
1636 }
1637
1638
1639 func (b *Builder) getPkgConfigFlags(a *Action) (cflags, ldflags []string, err error) {
1640 p := a.Package
1641 sh := b.Shell(a)
1642 if pcargs := p.CgoPkgConfig; len(pcargs) > 0 {
1643
1644
1645 var pcflags []string
1646 var pkgs []string
1647 for _, pcarg := range pcargs {
1648 if pcarg == "--" {
1649
1650 } else if strings.HasPrefix(pcarg, "--") {
1651 pcflags = append(pcflags, pcarg)
1652 } else {
1653 pkgs = append(pkgs, pcarg)
1654 }
1655 }
1656 for _, pkg := range pkgs {
1657 if !load.SafeArg(pkg) {
1658 return nil, nil, fmt.Errorf("invalid pkg-config package name: %s", pkg)
1659 }
1660 }
1661 var out []byte
1662 out, err = sh.runOut(p.Dir, nil, b.PkgconfigCmd(), "--cflags", pcflags, "--", pkgs)
1663 if err != nil {
1664 desc := b.PkgconfigCmd() + " --cflags " + strings.Join(pcflags, " ") + " -- " + strings.Join(pkgs, " ")
1665 return nil, nil, sh.reportCmd(desc, "", out, err)
1666 }
1667 if len(out) > 0 {
1668 cflags, err = splitPkgConfigOutput(bytes.TrimSpace(out))
1669 if err != nil {
1670 return nil, nil, err
1671 }
1672 if err := checkCompilerFlags("CFLAGS", "pkg-config --cflags", cflags); err != nil {
1673 return nil, nil, err
1674 }
1675 }
1676 out, err = sh.runOut(p.Dir, nil, b.PkgconfigCmd(), "--libs", pcflags, "--", pkgs)
1677 if err != nil {
1678 desc := b.PkgconfigCmd() + " --libs " + strings.Join(pcflags, " ") + " -- " + strings.Join(pkgs, " ")
1679 return nil, nil, sh.reportCmd(desc, "", out, err)
1680 }
1681 if len(out) > 0 {
1682
1683
1684 ldflags, err = splitPkgConfigOutput(bytes.TrimSpace(out))
1685 if err != nil {
1686 return nil, nil, err
1687 }
1688 if err := checkLinkerFlags("LDFLAGS", "pkg-config --libs", ldflags); err != nil {
1689 return nil, nil, err
1690 }
1691 }
1692 }
1693
1694 return
1695 }
1696
1697 func (b *Builder) installShlibname(ctx context.Context, a *Action) error {
1698 if err := AllowInstall(a); err != nil {
1699 return err
1700 }
1701
1702 sh := b.Shell(a)
1703 a1 := a.Deps[0]
1704 if !cfg.BuildN {
1705 if err := sh.Mkdir(filepath.Dir(a.Target)); err != nil {
1706 return err
1707 }
1708 }
1709 return sh.writeFile(a.Target, []byte(filepath.Base(a1.Target)+"\n"))
1710 }
1711
1712 func (b *Builder) linkSharedActionID(a *Action) cache.ActionID {
1713 h := cache.NewHash("linkShared")
1714
1715
1716 fmt.Fprintf(h, "linkShared\n")
1717 fmt.Fprintf(h, "goos %s goarch %s\n", cfg.Goos, cfg.Goarch)
1718
1719
1720 b.printLinkerConfig(h, nil)
1721
1722
1723 for _, a1 := range a.Deps {
1724 p1 := a1.Package
1725 if a1.built == "" {
1726 continue
1727 }
1728 if p1 != nil {
1729 fmt.Fprintf(h, "packagefile %s=%s\n", p1.ImportPath, contentID(b.buildID(a1.built)))
1730 if p1.Shlib != "" {
1731 fmt.Fprintf(h, "packageshlib %s=%s\n", p1.ImportPath, contentID(b.buildID(p1.Shlib)))
1732 }
1733 }
1734 }
1735
1736 for _, a1 := range a.Deps[0].Deps {
1737 p1 := a1.Package
1738 fmt.Fprintf(h, "top %s=%s\n", p1.ImportPath, contentID(b.buildID(a1.built)))
1739 }
1740
1741 return h.Sum()
1742 }
1743
1744 func (b *Builder) linkShared(ctx context.Context, a *Action) (err error) {
1745 if b.useCache(a, b.linkSharedActionID(a), a.Target, !b.IsCmdList) || b.IsCmdList {
1746 return nil
1747 }
1748 defer b.flushOutput(a)
1749
1750 if err := AllowInstall(a); err != nil {
1751 return err
1752 }
1753
1754 if err := b.Shell(a).Mkdir(a.Objdir); err != nil {
1755 return err
1756 }
1757
1758 importcfg := a.Objdir + "importcfg.link"
1759 if err := b.writeLinkImportcfg(a, importcfg); err != nil {
1760 return err
1761 }
1762
1763
1764
1765 a.built = a.Target
1766 return BuildToolchain.ldShared(b, a, a.Deps[0].Deps, a.Target, importcfg, a.Deps)
1767 }
1768
1769
1770 func BuildInstallFunc(b *Builder, ctx context.Context, a *Action) (err error) {
1771 defer func() {
1772 if err != nil {
1773
1774
1775
1776 sep, path := "", ""
1777 if a.Package != nil {
1778 sep, path = " ", a.Package.ImportPath
1779 }
1780 err = fmt.Errorf("go %s%s%s: %v", cfg.CmdName, sep, path, err)
1781 }
1782 }()
1783 sh := b.Shell(a)
1784
1785 a1 := a.Deps[0]
1786 a.buildID = a1.buildID
1787 if a.json != nil {
1788 a.json.BuildID = a.buildID
1789 }
1790
1791
1792
1793
1794
1795
1796 if a1.built == a.Target {
1797 a.built = a.Target
1798 if !a.buggyInstall {
1799 b.cleanup(a1)
1800 }
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819 if !a.buggyInstall && !b.IsCmdList {
1820 if cfg.BuildN {
1821 sh.ShowCmd("", "touch %s", a.Target)
1822 } else if err := AllowInstall(a); err == nil {
1823 now := time.Now()
1824 os.Chtimes(a.Target, now, now)
1825 }
1826 }
1827 return nil
1828 }
1829
1830
1831
1832 if b.IsCmdList {
1833 a.built = a1.built
1834 return nil
1835 }
1836 if err := AllowInstall(a); err != nil {
1837 return err
1838 }
1839
1840 if err := sh.Mkdir(a.Objdir); err != nil {
1841 return err
1842 }
1843
1844 perm := fs.FileMode(0666)
1845 if a1.Mode == "link" {
1846 switch cfg.BuildBuildmode {
1847 case "c-archive", "c-shared", "plugin":
1848 default:
1849 perm = 0777
1850 }
1851 }
1852
1853
1854 dir, _ := filepath.Split(a.Target)
1855 if dir != "" {
1856 if err := sh.Mkdir(dir); err != nil {
1857 return err
1858 }
1859 }
1860
1861 if !a.buggyInstall {
1862 defer b.cleanup(a1)
1863 }
1864
1865 return sh.moveOrCopyFile(a.Target, a1.built, perm, false)
1866 }
1867
1868
1869
1870
1871
1872
1873 var AllowInstall = func(*Action) error { return nil }
1874
1875
1876
1877
1878
1879 func (b *Builder) cleanup(a *Action) {
1880 if !cfg.BuildWork {
1881 b.Shell(a).RemoveAll(a.Objdir)
1882 }
1883 }
1884
1885
1886 func (b *Builder) installHeader(ctx context.Context, a *Action) error {
1887 sh := b.Shell(a)
1888
1889 src := a.Objdir + "_cgo_install.h"
1890 if _, err := os.Stat(src); os.IsNotExist(err) {
1891
1892
1893
1894
1895
1896 if cfg.BuildX {
1897 sh.ShowCmd("", "# %s not created", src)
1898 }
1899 return nil
1900 }
1901
1902 if err := AllowInstall(a); err != nil {
1903 return err
1904 }
1905
1906 dir, _ := filepath.Split(a.Target)
1907 if dir != "" {
1908 if err := sh.Mkdir(dir); err != nil {
1909 return err
1910 }
1911 }
1912
1913 return sh.moveOrCopyFile(a.Target, src, 0666, true)
1914 }
1915
1916
1917
1918
1919 func (b *Builder) cover(a *Action, dst, src string, varName string) error {
1920 return b.Shell(a).run(a.Objdir, "", nil,
1921 cfg.BuildToolexec,
1922 base.Tool("cover"),
1923 "-mode", a.Package.Internal.Cover.Mode,
1924 "-var", varName,
1925 "-o", dst,
1926 src)
1927 }
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937 func (b *Builder) cover2(a *Action, infiles, outfiles []string, varName string, mode string) ([]string, error) {
1938 pkgcfg := a.Objdir + "pkgcfg.txt"
1939 covoutputs := a.Objdir + "coveroutfiles.txt"
1940 odir := filepath.Dir(outfiles[0])
1941 cv := filepath.Join(odir, "covervars.go")
1942 outfiles = append([]string{cv}, outfiles...)
1943 if err := b.writeCoverPkgInputs(a, pkgcfg, covoutputs, outfiles); err != nil {
1944 return nil, err
1945 }
1946 args := []string{base.Tool("cover"),
1947 "-pkgcfg", pkgcfg,
1948 "-mode", mode,
1949 "-var", varName,
1950 "-outfilelist", covoutputs,
1951 }
1952 args = append(args, infiles...)
1953 if err := b.Shell(a).run(a.Objdir, "", nil,
1954 cfg.BuildToolexec, args); err != nil {
1955 return nil, err
1956 }
1957 return outfiles, nil
1958 }
1959
1960 func (b *Builder) writeCoverPkgInputs(a *Action, pconfigfile string, covoutputsfile string, outfiles []string) error {
1961 sh := b.Shell(a)
1962 p := a.Package
1963 p.Internal.Cover.Cfg = a.Objdir + "coveragecfg"
1964 pcfg := covcmd.CoverPkgConfig{
1965 PkgPath: p.ImportPath,
1966 PkgName: p.Name,
1967
1968
1969
1970
1971 Granularity: "perblock",
1972 OutConfig: p.Internal.Cover.Cfg,
1973 Local: p.Internal.Local,
1974 }
1975 if ba, ok := a.Actor.(*buildActor); ok && ba.covMetaFileName != "" {
1976 pcfg.EmitMetaFile = a.Objdir + ba.covMetaFileName
1977 }
1978 if a.Package.Module != nil {
1979 pcfg.ModulePath = a.Package.Module.Path
1980 }
1981 data, err := json.Marshal(pcfg)
1982 if err != nil {
1983 return err
1984 }
1985 data = append(data, '\n')
1986 if err := sh.writeFile(pconfigfile, data); err != nil {
1987 return err
1988 }
1989 var sb strings.Builder
1990 for i := range outfiles {
1991 fmt.Fprintf(&sb, "%s\n", outfiles[i])
1992 }
1993 return sh.writeFile(covoutputsfile, []byte(sb.String()))
1994 }
1995
1996 var objectMagic = [][]byte{
1997 {'!', '<', 'a', 'r', 'c', 'h', '>', '\n'},
1998 {'<', 'b', 'i', 'g', 'a', 'f', '>', '\n'},
1999 {'\x7F', 'E', 'L', 'F'},
2000 {0xFE, 0xED, 0xFA, 0xCE},
2001 {0xFE, 0xED, 0xFA, 0xCF},
2002 {0xCE, 0xFA, 0xED, 0xFE},
2003 {0xCF, 0xFA, 0xED, 0xFE},
2004 {0x4d, 0x5a, 0x90, 0x00, 0x03, 0x00},
2005 {0x4d, 0x5a, 0x78, 0x00, 0x01, 0x00},
2006 {0x00, 0x00, 0x01, 0xEB},
2007 {0x00, 0x00, 0x8a, 0x97},
2008 {0x00, 0x00, 0x06, 0x47},
2009 {0x00, 0x61, 0x73, 0x6D},
2010 {0x01, 0xDF},
2011 {0x01, 0xF7},
2012 }
2013
2014 func isObject(s string) bool {
2015 f, err := os.Open(s)
2016 if err != nil {
2017 return false
2018 }
2019 defer f.Close()
2020 buf := make([]byte, 64)
2021 io.ReadFull(f, buf)
2022 for _, magic := range objectMagic {
2023 if bytes.HasPrefix(buf, magic) {
2024 return true
2025 }
2026 }
2027 return false
2028 }
2029
2030
2031
2032
2033 func (b *Builder) cCompilerEnv() []string {
2034 return []string{"TERM=dumb"}
2035 }
2036
2037
2038
2039
2040
2041
2042 func mkAbs(dir, f string) string {
2043
2044
2045
2046
2047 if filepath.IsAbs(f) || strings.HasPrefix(f, "$WORK") {
2048 return f
2049 }
2050 return filepath.Join(dir, f)
2051 }
2052
2053 type toolchain interface {
2054
2055
2056 gc(b *Builder, a *Action, archive string, importcfg, embedcfg []byte, symabis string, asmhdr bool, pgoProfile string, gofiles []string) (ofile string, out []byte, err error)
2057
2058
2059 cc(b *Builder, a *Action, ofile, cfile string) error
2060
2061
2062 asm(b *Builder, a *Action, sfiles []string) ([]string, error)
2063
2064
2065 symabis(b *Builder, a *Action, sfiles []string) (string, error)
2066
2067
2068
2069 pack(b *Builder, a *Action, afile string, ofiles []string) error
2070
2071 ld(b *Builder, root *Action, targetPath, importcfg, mainpkg string) error
2072
2073 ldShared(b *Builder, root *Action, toplevelactions []*Action, targetPath, importcfg string, allactions []*Action) error
2074
2075 compiler() string
2076 linker() string
2077 }
2078
2079 type noToolchain struct{}
2080
2081 func noCompiler() error {
2082 log.Fatalf("unknown compiler %q", cfg.BuildContext.Compiler)
2083 return nil
2084 }
2085
2086 func (noToolchain) compiler() string {
2087 noCompiler()
2088 return ""
2089 }
2090
2091 func (noToolchain) linker() string {
2092 noCompiler()
2093 return ""
2094 }
2095
2096 func (noToolchain) gc(b *Builder, a *Action, archive string, importcfg, embedcfg []byte, symabis string, asmhdr bool, pgoProfile string, gofiles []string) (ofile string, out []byte, err error) {
2097 return "", nil, noCompiler()
2098 }
2099
2100 func (noToolchain) asm(b *Builder, a *Action, sfiles []string) ([]string, error) {
2101 return nil, noCompiler()
2102 }
2103
2104 func (noToolchain) symabis(b *Builder, a *Action, sfiles []string) (string, error) {
2105 return "", noCompiler()
2106 }
2107
2108 func (noToolchain) pack(b *Builder, a *Action, afile string, ofiles []string) error {
2109 return noCompiler()
2110 }
2111
2112 func (noToolchain) ld(b *Builder, root *Action, targetPath, importcfg, mainpkg string) error {
2113 return noCompiler()
2114 }
2115
2116 func (noToolchain) ldShared(b *Builder, root *Action, toplevelactions []*Action, targetPath, importcfg string, allactions []*Action) error {
2117 return noCompiler()
2118 }
2119
2120 func (noToolchain) cc(b *Builder, a *Action, ofile, cfile string) error {
2121 return noCompiler()
2122 }
2123
2124
2125 func (b *Builder) gcc(a *Action, workdir, out string, flags []string, cfile string) error {
2126 p := a.Package
2127 return b.ccompile(a, out, flags, cfile, b.GccCmd(p.Dir, workdir))
2128 }
2129
2130
2131 func (b *Builder) gxx(a *Action, workdir, out string, flags []string, cxxfile string) error {
2132 p := a.Package
2133 return b.ccompile(a, out, flags, cxxfile, b.GxxCmd(p.Dir, workdir))
2134 }
2135
2136
2137 func (b *Builder) gfortran(a *Action, workdir, out string, flags []string, ffile string) error {
2138 p := a.Package
2139 return b.ccompile(a, out, flags, ffile, b.gfortranCmd(p.Dir, workdir))
2140 }
2141
2142
2143 func (b *Builder) ccompile(a *Action, outfile string, flags []string, file string, compiler []string) error {
2144 p := a.Package
2145 sh := b.Shell(a)
2146 file = mkAbs(p.Dir, file)
2147 outfile = mkAbs(p.Dir, outfile)
2148
2149
2150
2151
2152
2153
2154 if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") {
2155 if cfg.BuildTrimpath || p.Goroot {
2156 prefixMapFlag := "-fdebug-prefix-map"
2157 if b.gccSupportsFlag(compiler, "-ffile-prefix-map=a=b") {
2158 prefixMapFlag = "-ffile-prefix-map"
2159 }
2160
2161
2162
2163 var from, toPath string
2164 if m := p.Module; m == nil {
2165 if p.Root == "" {
2166 from = p.Dir
2167 toPath = p.ImportPath
2168 } else if p.Goroot {
2169 from = p.Root
2170 toPath = "GOROOT"
2171 } else {
2172 from = p.Root
2173 toPath = "GOPATH"
2174 }
2175 } else if m.Dir == "" {
2176
2177
2178 from = modload.VendorDir()
2179 toPath = "vendor"
2180 } else {
2181 from = m.Dir
2182 toPath = m.Path
2183 if m.Version != "" {
2184 toPath += "@" + m.Version
2185 }
2186 }
2187
2188
2189
2190 var to string
2191 if cfg.BuildContext.GOOS == "windows" {
2192 to = filepath.Join(`\\_\_`, toPath)
2193 } else {
2194 to = filepath.Join("/_", toPath)
2195 }
2196 flags = append(slices.Clip(flags), prefixMapFlag+"="+from+"="+to)
2197 }
2198 }
2199
2200
2201
2202 if b.gccSupportsFlag(compiler, "-frandom-seed=1") {
2203 flags = append(flags, "-frandom-seed="+buildid.HashToString(a.actionID))
2204 }
2205
2206 overlayPath := file
2207 if p, ok := a.nonGoOverlay[overlayPath]; ok {
2208 overlayPath = p
2209 }
2210 output, err := sh.runOut(filepath.Dir(overlayPath), b.cCompilerEnv(), compiler, flags, "-o", outfile, "-c", filepath.Base(overlayPath))
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220 if bytes.Contains(output, []byte("DWARF2 only supports one section per compilation unit")) {
2221 newFlags := make([]string, 0, len(flags))
2222 for _, f := range flags {
2223 if !strings.HasPrefix(f, "-g") {
2224 newFlags = append(newFlags, f)
2225 }
2226 }
2227 if len(newFlags) < len(flags) {
2228 return b.ccompile(a, outfile, newFlags, file, compiler)
2229 }
2230 }
2231
2232 if len(output) > 0 && err == nil && os.Getenv("GO_BUILDER_NAME") != "" {
2233 output = append(output, "C compiler warning promoted to error on Go builders\n"...)
2234 err = errors.New("warning promoted to error")
2235 }
2236
2237 return sh.reportCmd("", "", output, err)
2238 }
2239
2240
2241
2242 func (b *Builder) gccld(a *Action, objdir, outfile string, flags []string, objs []string) error {
2243 p := a.Package
2244 sh := b.Shell(a)
2245 var cmd []string
2246 if len(p.CXXFiles) > 0 || len(p.SwigCXXFiles) > 0 {
2247 cmd = b.GxxCmd(p.Dir, objdir)
2248 } else {
2249 cmd = b.GccCmd(p.Dir, objdir)
2250 }
2251
2252 cmdargs := []any{cmd, "-o", outfile, objs, flags}
2253 out, err := sh.runOut(base.Cwd(), b.cCompilerEnv(), cmdargs...)
2254
2255 if len(out) > 0 {
2256
2257
2258 var save [][]byte
2259 var skipLines int
2260 for _, line := range bytes.SplitAfter(out, []byte("\n")) {
2261
2262 if bytes.Contains(line, []byte("ld: warning: text-based stub file")) {
2263 continue
2264 }
2265
2266 if skipLines > 0 {
2267 skipLines--
2268 continue
2269 }
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280 if p.ImportPath == "runtime/cgo" && bytes.Contains(line, []byte("ld: 0711-224 WARNING: Duplicate symbol: .main")) {
2281 skipLines = 1
2282 continue
2283 }
2284
2285 save = append(save, line)
2286 }
2287 out = bytes.Join(save, nil)
2288 }
2289
2290
2291 if cfg.BuildN || cfg.BuildX {
2292 sh.reportCmd("", "", out, nil)
2293 }
2294 return err
2295 }
2296
2297
2298
2299 func (b *Builder) GccCmd(incdir, workdir string) []string {
2300 return b.compilerCmd(b.ccExe(), incdir, workdir)
2301 }
2302
2303
2304
2305 func (b *Builder) GxxCmd(incdir, workdir string) []string {
2306 return b.compilerCmd(b.cxxExe(), incdir, workdir)
2307 }
2308
2309
2310 func (b *Builder) gfortranCmd(incdir, workdir string) []string {
2311 return b.compilerCmd(b.fcExe(), incdir, workdir)
2312 }
2313
2314
2315 func (b *Builder) ccExe() []string {
2316 return envList("CC", cfg.DefaultCC(cfg.Goos, cfg.Goarch))
2317 }
2318
2319
2320 func (b *Builder) cxxExe() []string {
2321 return envList("CXX", cfg.DefaultCXX(cfg.Goos, cfg.Goarch))
2322 }
2323
2324
2325 func (b *Builder) fcExe() []string {
2326 return envList("FC", "gfortran")
2327 }
2328
2329
2330
2331 func (b *Builder) compilerCmd(compiler []string, incdir, workdir string) []string {
2332 a := append(compiler, "-I", incdir)
2333
2334
2335
2336 if cfg.Goos != "windows" {
2337 a = append(a, "-fPIC")
2338 }
2339 a = append(a, b.gccArchArgs()...)
2340
2341
2342 if cfg.BuildContext.CgoEnabled {
2343 switch cfg.Goos {
2344 case "windows":
2345 a = append(a, "-mthreads")
2346 default:
2347 a = append(a, "-pthread")
2348 }
2349 }
2350
2351 if cfg.Goos == "aix" {
2352
2353 a = append(a, "-mcmodel=large")
2354 }
2355
2356
2357 if b.gccSupportsFlag(compiler, "-fno-caret-diagnostics") {
2358 a = append(a, "-fno-caret-diagnostics")
2359 }
2360
2361 if b.gccSupportsFlag(compiler, "-Qunused-arguments") {
2362 a = append(a, "-Qunused-arguments")
2363 }
2364
2365
2366
2367
2368 if b.gccSupportsFlag(compiler, "-Wl,--no-gc-sections") {
2369 a = append(a, "-Wl,--no-gc-sections")
2370 }
2371
2372
2373 a = append(a, "-fmessage-length=0")
2374
2375
2376 if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") {
2377 if workdir == "" {
2378 workdir = b.WorkDir
2379 }
2380 workdir = strings.TrimSuffix(workdir, string(filepath.Separator))
2381 if b.gccSupportsFlag(compiler, "-ffile-prefix-map=a=b") {
2382 a = append(a, "-ffile-prefix-map="+workdir+"=/tmp/go-build")
2383 } else {
2384 a = append(a, "-fdebug-prefix-map="+workdir+"=/tmp/go-build")
2385 }
2386 }
2387
2388
2389
2390 if b.gccSupportsFlag(compiler, "-gno-record-gcc-switches") {
2391 a = append(a, "-gno-record-gcc-switches")
2392 }
2393
2394
2395
2396
2397 if cfg.Goos == "darwin" || cfg.Goos == "ios" {
2398 a = append(a, "-fno-common")
2399 }
2400
2401 return a
2402 }
2403
2404
2405
2406
2407
2408 func (b *Builder) gccNoPie(linker []string) string {
2409 if b.gccSupportsFlag(linker, "-no-pie") {
2410 return "-no-pie"
2411 }
2412 if b.gccSupportsFlag(linker, "-nopie") {
2413 return "-nopie"
2414 }
2415 return ""
2416 }
2417
2418
2419 func (b *Builder) gccSupportsFlag(compiler []string, flag string) bool {
2420
2421
2422
2423 sh := b.BackgroundShell()
2424
2425 key := [2]string{compiler[0], flag}
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443 tmp := os.DevNull
2444 if runtime.GOOS == "windows" || runtime.GOOS == "ios" {
2445 f, err := os.CreateTemp(b.WorkDir, "")
2446 if err != nil {
2447 return false
2448 }
2449 f.Close()
2450 tmp = f.Name()
2451 defer os.Remove(tmp)
2452 }
2453
2454 cmdArgs := str.StringList(compiler, flag)
2455 if strings.HasPrefix(flag, "-Wl,") {
2456 ldflags, err := buildFlags("LDFLAGS", defaultCFlags, nil, checkLinkerFlags)
2457 if err != nil {
2458 return false
2459 }
2460 cmdArgs = append(cmdArgs, ldflags...)
2461 } else {
2462 cflags, err := buildFlags("CFLAGS", defaultCFlags, nil, checkCompilerFlags)
2463 if err != nil {
2464 return false
2465 }
2466 cmdArgs = append(cmdArgs, cflags...)
2467 cmdArgs = append(cmdArgs, "-c")
2468 }
2469
2470 cmdArgs = append(cmdArgs, "-x", "c", "-", "-o", tmp)
2471
2472 if cfg.BuildN {
2473 sh.ShowCmd(b.WorkDir, "%s || true", joinUnambiguously(cmdArgs))
2474 return false
2475 }
2476
2477
2478 compilerID, cacheOK := b.gccCompilerID(compiler[0])
2479
2480 b.exec.Lock()
2481 defer b.exec.Unlock()
2482 if b, ok := b.flagCache[key]; ok {
2483 return b
2484 }
2485 if b.flagCache == nil {
2486 b.flagCache = make(map[[2]string]bool)
2487 }
2488
2489
2490 var flagID cache.ActionID
2491 if cacheOK {
2492 flagID = cache.Subkey(compilerID, "gccSupportsFlag "+flag)
2493 if data, _, err := cache.GetBytes(cache.Default(), flagID); err == nil {
2494 supported := string(data) == "true"
2495 b.flagCache[key] = supported
2496 return supported
2497 }
2498 }
2499
2500 if cfg.BuildX {
2501 sh.ShowCmd(b.WorkDir, "%s || true", joinUnambiguously(cmdArgs))
2502 }
2503 cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
2504 cmd.Dir = b.WorkDir
2505 cmd.Env = append(cmd.Environ(), "LC_ALL=C")
2506 out, _ := cmd.CombinedOutput()
2507
2508
2509
2510
2511
2512
2513 supported := !bytes.Contains(out, []byte("unrecognized")) &&
2514 !bytes.Contains(out, []byte("unknown")) &&
2515 !bytes.Contains(out, []byte("unrecognised")) &&
2516 !bytes.Contains(out, []byte("is not supported")) &&
2517 !bytes.Contains(out, []byte("not recognized")) &&
2518 !bytes.Contains(out, []byte("unsupported"))
2519
2520 if cacheOK {
2521 s := "false"
2522 if supported {
2523 s = "true"
2524 }
2525 cache.PutBytes(cache.Default(), flagID, []byte(s))
2526 }
2527
2528 b.flagCache[key] = supported
2529 return supported
2530 }
2531
2532
2533 func statString(info os.FileInfo) string {
2534 return fmt.Sprintf("stat %d %x %v %v\n", info.Size(), uint64(info.Mode()), info.ModTime(), info.IsDir())
2535 }
2536
2537
2538
2539
2540
2541
2542 func (b *Builder) gccCompilerID(compiler string) (id cache.ActionID, ok bool) {
2543
2544
2545
2546 sh := b.BackgroundShell()
2547
2548 if cfg.BuildN {
2549 sh.ShowCmd(b.WorkDir, "%s || true", joinUnambiguously([]string{compiler, "--version"}))
2550 return cache.ActionID{}, false
2551 }
2552
2553 b.exec.Lock()
2554 defer b.exec.Unlock()
2555
2556 if id, ok := b.gccCompilerIDCache[compiler]; ok {
2557 return id, ok
2558 }
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574 exe, err := cfg.LookPath(compiler)
2575 if err != nil {
2576 return cache.ActionID{}, false
2577 }
2578
2579 h := cache.NewHash("gccCompilerID")
2580 fmt.Fprintf(h, "gccCompilerID %q", exe)
2581 key := h.Sum()
2582 data, _, err := cache.GetBytes(cache.Default(), key)
2583 if err == nil && len(data) > len(id) {
2584 stats := strings.Split(string(data[:len(data)-len(id)]), "\x00")
2585 if len(stats)%2 != 0 {
2586 goto Miss
2587 }
2588 for i := 0; i+2 <= len(stats); i++ {
2589 info, err := os.Stat(stats[i])
2590 if err != nil || statString(info) != stats[i+1] {
2591 goto Miss
2592 }
2593 }
2594 copy(id[:], data[len(data)-len(id):])
2595 return id, true
2596 Miss:
2597 }
2598
2599
2600
2601
2602
2603
2604 toolID, exe2, err := b.gccToolID(compiler, "c")
2605 if err != nil {
2606 return cache.ActionID{}, false
2607 }
2608
2609 exes := []string{exe, exe2}
2610 str.Uniq(&exes)
2611 fmt.Fprintf(h, "gccCompilerID %q %q\n", exes, toolID)
2612 id = h.Sum()
2613
2614 var buf bytes.Buffer
2615 for _, exe := range exes {
2616 if exe == "" {
2617 continue
2618 }
2619 info, err := os.Stat(exe)
2620 if err != nil {
2621 return cache.ActionID{}, false
2622 }
2623 buf.WriteString(exe)
2624 buf.WriteString("\x00")
2625 buf.WriteString(statString(info))
2626 buf.WriteString("\x00")
2627 }
2628 buf.Write(id[:])
2629
2630 cache.PutBytes(cache.Default(), key, buf.Bytes())
2631 if b.gccCompilerIDCache == nil {
2632 b.gccCompilerIDCache = make(map[string]cache.ActionID)
2633 }
2634 b.gccCompilerIDCache[compiler] = id
2635 return id, true
2636 }
2637
2638
2639 func (b *Builder) gccArchArgs() []string {
2640 switch cfg.Goarch {
2641 case "386":
2642 return []string{"-m32"}
2643 case "amd64":
2644 if cfg.Goos == "darwin" {
2645 return []string{"-arch", "x86_64", "-m64"}
2646 }
2647 return []string{"-m64"}
2648 case "arm64":
2649 if cfg.Goos == "darwin" {
2650 return []string{"-arch", "arm64"}
2651 }
2652 case "arm":
2653 return []string{"-marm"}
2654 case "s390x":
2655 return []string{"-m64", "-march=z196"}
2656 case "mips64", "mips64le":
2657 args := []string{"-mabi=64"}
2658 if cfg.GOMIPS64 == "hardfloat" {
2659 return append(args, "-mhard-float")
2660 } else if cfg.GOMIPS64 == "softfloat" {
2661 return append(args, "-msoft-float")
2662 }
2663 case "mips", "mipsle":
2664 args := []string{"-mabi=32", "-march=mips32"}
2665 if cfg.GOMIPS == "hardfloat" {
2666 return append(args, "-mhard-float", "-mfp32", "-mno-odd-spreg")
2667 } else if cfg.GOMIPS == "softfloat" {
2668 return append(args, "-msoft-float")
2669 }
2670 case "loong64":
2671 return []string{"-mabi=lp64d"}
2672 case "ppc64":
2673 if cfg.Goos == "aix" {
2674 return []string{"-maix64"}
2675 }
2676 }
2677 return nil
2678 }
2679
2680
2681
2682
2683
2684
2685
2686 func envList(key, def string) []string {
2687 v := cfg.Getenv(key)
2688 if v == "" {
2689 v = def
2690 }
2691 args, err := quoted.Split(v)
2692 if err != nil {
2693 panic(fmt.Sprintf("could not parse environment variable %s with value %q: %v", key, v, err))
2694 }
2695 return args
2696 }
2697
2698
2699 func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, ldflags []string, err error) {
2700 if cppflags, err = buildFlags("CPPFLAGS", "", p.CgoCPPFLAGS, checkCompilerFlags); err != nil {
2701 return
2702 }
2703 if cflags, err = buildFlags("CFLAGS", defaultCFlags, p.CgoCFLAGS, checkCompilerFlags); err != nil {
2704 return
2705 }
2706 if cxxflags, err = buildFlags("CXXFLAGS", defaultCFlags, p.CgoCXXFLAGS, checkCompilerFlags); err != nil {
2707 return
2708 }
2709 if fflags, err = buildFlags("FFLAGS", defaultCFlags, p.CgoFFLAGS, checkCompilerFlags); err != nil {
2710 return
2711 }
2712 if ldflags, err = buildFlags("LDFLAGS", defaultCFlags, p.CgoLDFLAGS, checkLinkerFlags); err != nil {
2713 return
2714 }
2715
2716 return
2717 }
2718
2719 func buildFlags(name, defaults string, fromPackage []string, check func(string, string, []string) error) ([]string, error) {
2720 if err := check(name, "#cgo "+name, fromPackage); err != nil {
2721 return nil, err
2722 }
2723 return str.StringList(envList("CGO_"+name, defaults), fromPackage), nil
2724 }
2725
2726 var cgoRe = lazyregexp.New(`[/\\:]`)
2727
2728 func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) {
2729 p := a.Package
2730 sh := b.Shell(a)
2731
2732 cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS, err := b.CFlags(p)
2733 if err != nil {
2734 return nil, nil, err
2735 }
2736
2737 cgoCPPFLAGS = append(cgoCPPFLAGS, pcCFLAGS...)
2738 cgoLDFLAGS = append(cgoLDFLAGS, pcLDFLAGS...)
2739
2740 if len(mfiles) > 0 {
2741 cgoLDFLAGS = append(cgoLDFLAGS, "-lobjc")
2742 }
2743
2744
2745
2746
2747 if len(ffiles) > 0 {
2748 fc := cfg.Getenv("FC")
2749 if fc == "" {
2750 fc = "gfortran"
2751 }
2752 if strings.Contains(fc, "gfortran") {
2753 cgoLDFLAGS = append(cgoLDFLAGS, "-lgfortran")
2754 }
2755 }
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772 flagSources := []string{"CGO_CFLAGS", "CGO_CXXFLAGS", "CGO_FFLAGS"}
2773 flagLists := [][]string{cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS}
2774 if flagsNotCompatibleWithInternalLinking(flagSources, flagLists) {
2775 tokenFile := objdir + "preferlinkext"
2776 if err := sh.writeFile(tokenFile, nil); err != nil {
2777 return nil, nil, err
2778 }
2779 outObj = append(outObj, tokenFile)
2780 }
2781
2782 if cfg.BuildMSan {
2783 cgoCFLAGS = append([]string{"-fsanitize=memory"}, cgoCFLAGS...)
2784 cgoLDFLAGS = append([]string{"-fsanitize=memory"}, cgoLDFLAGS...)
2785 }
2786 if cfg.BuildASan {
2787 cgoCFLAGS = append([]string{"-fsanitize=address"}, cgoCFLAGS...)
2788 cgoLDFLAGS = append([]string{"-fsanitize=address"}, cgoLDFLAGS...)
2789 }
2790
2791
2792
2793 cgoCPPFLAGS = append(cgoCPPFLAGS, "-I", objdir)
2794
2795
2796
2797 gofiles := []string{objdir + "_cgo_gotypes.go"}
2798 cfiles := []string{"_cgo_export.c"}
2799 for _, fn := range cgofiles {
2800 f := strings.TrimSuffix(filepath.Base(fn), ".go")
2801 gofiles = append(gofiles, objdir+f+".cgo1.go")
2802 cfiles = append(cfiles, f+".cgo2.c")
2803 }
2804
2805
2806
2807 cgoflags := []string{}
2808 if p.Standard && p.ImportPath == "runtime/cgo" {
2809 cgoflags = append(cgoflags, "-import_runtime_cgo=false")
2810 }
2811 if p.Standard && (p.ImportPath == "runtime/race" || p.ImportPath == "runtime/msan" || p.ImportPath == "runtime/cgo" || p.ImportPath == "runtime/asan") {
2812 cgoflags = append(cgoflags, "-import_syscall=false")
2813 }
2814
2815
2816
2817
2818
2819
2820
2821
2822 cgoenv := b.cCompilerEnv()
2823 if len(cgoLDFLAGS) > 0 {
2824 flags := make([]string, len(cgoLDFLAGS))
2825 for i, f := range cgoLDFLAGS {
2826 flags[i] = strconv.Quote(f)
2827 }
2828 cgoenv = append(cgoenv, "CGO_LDFLAGS="+strings.Join(flags, " "))
2829 }
2830
2831 if cfg.BuildToolchainName == "gccgo" {
2832 if b.gccSupportsFlag([]string{BuildToolchain.compiler()}, "-fsplit-stack") {
2833 cgoCFLAGS = append(cgoCFLAGS, "-fsplit-stack")
2834 }
2835 cgoflags = append(cgoflags, "-gccgo")
2836 if pkgpath := gccgoPkgpath(p); pkgpath != "" {
2837 cgoflags = append(cgoflags, "-gccgopkgpath="+pkgpath)
2838 }
2839 if !BuildToolchain.(gccgoToolchain).supportsCgoIncomplete(b, a) {
2840 cgoflags = append(cgoflags, "-gccgo_define_cgoincomplete")
2841 }
2842 }
2843
2844 switch cfg.BuildBuildmode {
2845 case "c-archive", "c-shared":
2846
2847
2848
2849 cgoflags = append(cgoflags, "-exportheader="+objdir+"_cgo_install.h")
2850 }
2851
2852
2853
2854 var trimpath []string
2855 for i := range cgofiles {
2856 path := mkAbs(p.Dir, cgofiles[i])
2857 if opath, ok := fsys.OverlayPath(path); ok {
2858 cgofiles[i] = opath
2859 trimpath = append(trimpath, opath+"=>"+path)
2860 }
2861 }
2862 if len(trimpath) > 0 {
2863 cgoflags = append(cgoflags, "-trimpath", strings.Join(trimpath, ";"))
2864 }
2865
2866 if err := sh.run(p.Dir, p.ImportPath, cgoenv, cfg.BuildToolexec, cgoExe, "-objdir", objdir, "-importpath", p.ImportPath, cgoflags, "--", cgoCPPFLAGS, cgoCFLAGS, cgofiles); err != nil {
2867 return nil, nil, err
2868 }
2869 outGo = append(outGo, gofiles...)
2870
2871
2872
2873
2874
2875
2876
2877 oseq := 0
2878 nextOfile := func() string {
2879 oseq++
2880 return objdir + fmt.Sprintf("_x%03d.o", oseq)
2881 }
2882
2883
2884 cflags := str.StringList(cgoCPPFLAGS, cgoCFLAGS)
2885 for _, cfile := range cfiles {
2886 ofile := nextOfile()
2887 if err := b.gcc(a, a.Objdir, ofile, cflags, objdir+cfile); err != nil {
2888 return nil, nil, err
2889 }
2890 outObj = append(outObj, ofile)
2891 }
2892
2893 for _, file := range gccfiles {
2894 ofile := nextOfile()
2895 if err := b.gcc(a, a.Objdir, ofile, cflags, file); err != nil {
2896 return nil, nil, err
2897 }
2898 outObj = append(outObj, ofile)
2899 }
2900
2901 cxxflags := str.StringList(cgoCPPFLAGS, cgoCXXFLAGS)
2902 for _, file := range gxxfiles {
2903 ofile := nextOfile()
2904 if err := b.gxx(a, a.Objdir, ofile, cxxflags, file); err != nil {
2905 return nil, nil, err
2906 }
2907 outObj = append(outObj, ofile)
2908 }
2909
2910 for _, file := range mfiles {
2911 ofile := nextOfile()
2912 if err := b.gcc(a, a.Objdir, ofile, cflags, file); err != nil {
2913 return nil, nil, err
2914 }
2915 outObj = append(outObj, ofile)
2916 }
2917
2918 fflags := str.StringList(cgoCPPFLAGS, cgoFFLAGS)
2919 for _, file := range ffiles {
2920 ofile := nextOfile()
2921 if err := b.gfortran(a, a.Objdir, ofile, fflags, file); err != nil {
2922 return nil, nil, err
2923 }
2924 outObj = append(outObj, ofile)
2925 }
2926
2927 switch cfg.BuildToolchainName {
2928 case "gc":
2929 importGo := objdir + "_cgo_import.go"
2930 dynOutGo, dynOutObj, err := b.dynimport(a, objdir, importGo, cgoExe, cflags, cgoLDFLAGS, outObj)
2931 if err != nil {
2932 return nil, nil, err
2933 }
2934 if dynOutGo != "" {
2935 outGo = append(outGo, dynOutGo)
2936 }
2937 if dynOutObj != "" {
2938 outObj = append(outObj, dynOutObj)
2939 }
2940
2941 case "gccgo":
2942 defunC := objdir + "_cgo_defun.c"
2943 defunObj := objdir + "_cgo_defun.o"
2944 if err := BuildToolchain.cc(b, a, defunObj, defunC); err != nil {
2945 return nil, nil, err
2946 }
2947 outObj = append(outObj, defunObj)
2948
2949 default:
2950 noCompiler()
2951 }
2952
2953
2954
2955
2956
2957
2958 if cfg.BuildToolchainName == "gc" && !cfg.BuildN {
2959 var flags []string
2960 for _, f := range outGo {
2961 if !strings.HasPrefix(filepath.Base(f), "_cgo_") {
2962 continue
2963 }
2964
2965 src, err := os.ReadFile(f)
2966 if err != nil {
2967 return nil, nil, err
2968 }
2969
2970 const cgoLdflag = "//go:cgo_ldflag"
2971 idx := bytes.Index(src, []byte(cgoLdflag))
2972 for idx >= 0 {
2973
2974
2975 start := bytes.LastIndex(src[:idx], []byte("\n"))
2976 if start == -1 {
2977 start = 0
2978 }
2979
2980
2981 end := bytes.Index(src[idx:], []byte("\n"))
2982 if end == -1 {
2983 end = len(src)
2984 } else {
2985 end += idx
2986 }
2987
2988
2989
2990
2991
2992 commentStart := bytes.Index(src[start:], []byte("//"))
2993 commentStart += start
2994
2995
2996 if bytes.HasPrefix(src[commentStart:], []byte(cgoLdflag)) {
2997
2998
2999 flag := string(src[idx+len(cgoLdflag) : end])
3000 flag = strings.TrimSpace(flag)
3001 flag = strings.Trim(flag, `"`)
3002 flags = append(flags, flag)
3003 }
3004 src = src[end:]
3005 idx = bytes.Index(src, []byte(cgoLdflag))
3006 }
3007 }
3008
3009
3010 if len(cgoLDFLAGS) > 0 {
3011 outer:
3012 for i := range flags {
3013 for j, f := range cgoLDFLAGS {
3014 if f != flags[i+j] {
3015 continue outer
3016 }
3017 }
3018 flags = append(flags[:i], flags[i+len(cgoLDFLAGS):]...)
3019 break
3020 }
3021 }
3022
3023 if err := checkLinkerFlags("LDFLAGS", "go:cgo_ldflag", flags); err != nil {
3024 return nil, nil, err
3025 }
3026 }
3027
3028 return outGo, outObj, nil
3029 }
3030
3031
3032
3033
3034
3035
3036
3037
3038 func flagsNotCompatibleWithInternalLinking(sourceList []string, flagListList [][]string) bool {
3039 for i := range sourceList {
3040 sn := sourceList[i]
3041 fll := flagListList[i]
3042 if err := checkCompilerFlagsForInternalLink(sn, sn, fll); err != nil {
3043 return true
3044 }
3045 }
3046 return false
3047 }
3048
3049
3050
3051
3052
3053
3054 func (b *Builder) dynimport(a *Action, objdir, importGo, cgoExe string, cflags, cgoLDFLAGS, outObj []string) (dynOutGo, dynOutObj string, err error) {
3055 p := a.Package
3056 sh := b.Shell(a)
3057
3058 cfile := objdir + "_cgo_main.c"
3059 ofile := objdir + "_cgo_main.o"
3060 if err := b.gcc(a, objdir, ofile, cflags, cfile); err != nil {
3061 return "", "", err
3062 }
3063
3064
3065 var syso []string
3066 seen := make(map[*Action]bool)
3067 var gatherSyso func(*Action)
3068 gatherSyso = func(a1 *Action) {
3069 if seen[a1] {
3070 return
3071 }
3072 seen[a1] = true
3073 if p1 := a1.Package; p1 != nil {
3074 syso = append(syso, mkAbsFiles(p1.Dir, p1.SysoFiles)...)
3075 }
3076 for _, a2 := range a1.Deps {
3077 gatherSyso(a2)
3078 }
3079 }
3080 gatherSyso(a)
3081 sort.Strings(syso)
3082 str.Uniq(&syso)
3083 linkobj := str.StringList(ofile, outObj, syso)
3084 dynobj := objdir + "_cgo_.o"
3085
3086 ldflags := cgoLDFLAGS
3087 if (cfg.Goarch == "arm" && cfg.Goos == "linux") || cfg.Goos == "android" {
3088 if !slices.Contains(ldflags, "-no-pie") {
3089
3090
3091 ldflags = append(ldflags, "-pie")
3092 }
3093 if slices.Contains(ldflags, "-pie") && slices.Contains(ldflags, "-static") {
3094
3095
3096 n := make([]string, 0, len(ldflags)-1)
3097 for _, flag := range ldflags {
3098 if flag != "-static" {
3099 n = append(n, flag)
3100 }
3101 }
3102 ldflags = n
3103 }
3104 }
3105 if err := b.gccld(a, objdir, dynobj, ldflags, linkobj); err != nil {
3106
3107
3108
3109
3110
3111
3112 fail := objdir + "dynimportfail"
3113 if err := sh.writeFile(fail, nil); err != nil {
3114 return "", "", err
3115 }
3116 return "", fail, nil
3117 }
3118
3119
3120 var cgoflags []string
3121 if p.Standard && p.ImportPath == "runtime/cgo" {
3122 cgoflags = []string{"-dynlinker"}
3123 }
3124 err = sh.run(base.Cwd(), p.ImportPath, b.cCompilerEnv(), cfg.BuildToolexec, cgoExe, "-dynpackage", p.Name, "-dynimport", dynobj, "-dynout", importGo, cgoflags)
3125 if err != nil {
3126 return "", "", err
3127 }
3128 return importGo, "", nil
3129 }
3130
3131
3132
3133
3134 func (b *Builder) swig(a *Action, objdir string, pcCFLAGS []string) (outGo, outC, outCXX []string, err error) {
3135 p := a.Package
3136
3137 if err := b.swigVersionCheck(); err != nil {
3138 return nil, nil, nil, err
3139 }
3140
3141 intgosize, err := b.swigIntSize(objdir)
3142 if err != nil {
3143 return nil, nil, nil, err
3144 }
3145
3146 for _, f := range p.SwigFiles {
3147 goFile, cFile, err := b.swigOne(a, f, objdir, pcCFLAGS, false, intgosize)
3148 if err != nil {
3149 return nil, nil, nil, err
3150 }
3151 if goFile != "" {
3152 outGo = append(outGo, goFile)
3153 }
3154 if cFile != "" {
3155 outC = append(outC, cFile)
3156 }
3157 }
3158 for _, f := range p.SwigCXXFiles {
3159 goFile, cxxFile, err := b.swigOne(a, f, objdir, pcCFLAGS, true, intgosize)
3160 if err != nil {
3161 return nil, nil, nil, err
3162 }
3163 if goFile != "" {
3164 outGo = append(outGo, goFile)
3165 }
3166 if cxxFile != "" {
3167 outCXX = append(outCXX, cxxFile)
3168 }
3169 }
3170 return outGo, outC, outCXX, nil
3171 }
3172
3173
3174 var (
3175 swigCheckOnce sync.Once
3176 swigCheck error
3177 )
3178
3179 func (b *Builder) swigDoVersionCheck() error {
3180 sh := b.BackgroundShell()
3181 out, err := sh.runOut(".", nil, "swig", "-version")
3182 if err != nil {
3183 return err
3184 }
3185 re := regexp.MustCompile(`[vV]ersion +(\d+)([.]\d+)?([.]\d+)?`)
3186 matches := re.FindSubmatch(out)
3187 if matches == nil {
3188
3189 return nil
3190 }
3191
3192 major, err := strconv.Atoi(string(matches[1]))
3193 if err != nil {
3194
3195 return nil
3196 }
3197 const errmsg = "must have SWIG version >= 3.0.6"
3198 if major < 3 {
3199 return errors.New(errmsg)
3200 }
3201 if major > 3 {
3202
3203 return nil
3204 }
3205
3206
3207 if len(matches[2]) > 0 {
3208 minor, err := strconv.Atoi(string(matches[2][1:]))
3209 if err != nil {
3210 return nil
3211 }
3212 if minor > 0 {
3213
3214 return nil
3215 }
3216 }
3217
3218
3219 if len(matches[3]) > 0 {
3220 patch, err := strconv.Atoi(string(matches[3][1:]))
3221 if err != nil {
3222 return nil
3223 }
3224 if patch < 6 {
3225
3226 return errors.New(errmsg)
3227 }
3228 }
3229
3230 return nil
3231 }
3232
3233 func (b *Builder) swigVersionCheck() error {
3234 swigCheckOnce.Do(func() {
3235 swigCheck = b.swigDoVersionCheck()
3236 })
3237 return swigCheck
3238 }
3239
3240
3241 var (
3242 swigIntSizeOnce sync.Once
3243 swigIntSize string
3244 swigIntSizeError error
3245 )
3246
3247
3248 const swigIntSizeCode = `
3249 package main
3250 const i int = 1 << 32
3251 `
3252
3253
3254
3255 func (b *Builder) swigDoIntSize(objdir string) (intsize string, err error) {
3256 if cfg.BuildN {
3257 return "$INTBITS", nil
3258 }
3259 src := filepath.Join(b.WorkDir, "swig_intsize.go")
3260 if err = os.WriteFile(src, []byte(swigIntSizeCode), 0666); err != nil {
3261 return
3262 }
3263 srcs := []string{src}
3264
3265 p := load.GoFilesPackage(context.TODO(), load.PackageOpts{}, srcs)
3266
3267 if _, _, e := BuildToolchain.gc(b, &Action{Mode: "swigDoIntSize", Package: p, Objdir: objdir}, "", nil, nil, "", false, "", srcs); e != nil {
3268 return "32", nil
3269 }
3270 return "64", nil
3271 }
3272
3273
3274
3275 func (b *Builder) swigIntSize(objdir string) (intsize string, err error) {
3276 swigIntSizeOnce.Do(func() {
3277 swigIntSize, swigIntSizeError = b.swigDoIntSize(objdir)
3278 })
3279 return swigIntSize, swigIntSizeError
3280 }
3281
3282
3283 func (b *Builder) swigOne(a *Action, file, objdir string, pcCFLAGS []string, cxx bool, intgosize string) (outGo, outC string, err error) {
3284 p := a.Package
3285 sh := b.Shell(a)
3286
3287 cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p)
3288 if err != nil {
3289 return "", "", err
3290 }
3291
3292 var cflags []string
3293 if cxx {
3294 cflags = str.StringList(cgoCPPFLAGS, pcCFLAGS, cgoCXXFLAGS)
3295 } else {
3296 cflags = str.StringList(cgoCPPFLAGS, pcCFLAGS, cgoCFLAGS)
3297 }
3298
3299 n := 5
3300 if cxx {
3301 n = 8
3302 }
3303 base := file[:len(file)-n]
3304 goFile := base + ".go"
3305 gccBase := base + "_wrap."
3306 gccExt := "c"
3307 if cxx {
3308 gccExt = "cxx"
3309 }
3310
3311 gccgo := cfg.BuildToolchainName == "gccgo"
3312
3313
3314 args := []string{
3315 "-go",
3316 "-cgo",
3317 "-intgosize", intgosize,
3318 "-module", base,
3319 "-o", objdir + gccBase + gccExt,
3320 "-outdir", objdir,
3321 }
3322
3323 for _, f := range cflags {
3324 if len(f) > 3 && f[:2] == "-I" {
3325 args = append(args, f)
3326 }
3327 }
3328
3329 if gccgo {
3330 args = append(args, "-gccgo")
3331 if pkgpath := gccgoPkgpath(p); pkgpath != "" {
3332 args = append(args, "-go-pkgpath", pkgpath)
3333 }
3334 }
3335 if cxx {
3336 args = append(args, "-c++")
3337 }
3338
3339 out, err := sh.runOut(p.Dir, nil, "swig", args, file)
3340 if err != nil && (bytes.Contains(out, []byte("-intgosize")) || bytes.Contains(out, []byte("-cgo"))) {
3341 return "", "", errors.New("must have SWIG version >= 3.0.6")
3342 }
3343 if err := sh.reportCmd("", "", out, err); err != nil {
3344 return "", "", err
3345 }
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355 goFile = objdir + goFile
3356 newGoFile := objdir + "_" + base + "_swig.go"
3357 if cfg.BuildX || cfg.BuildN {
3358 sh.ShowCmd("", "mv %s %s", goFile, newGoFile)
3359 }
3360 if !cfg.BuildN {
3361 if err := os.Rename(goFile, newGoFile); err != nil {
3362 return "", "", err
3363 }
3364 }
3365 return newGoFile, objdir + gccBase + gccExt, nil
3366 }
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378 func (b *Builder) disableBuildID(ldflags []string) []string {
3379 switch cfg.Goos {
3380 case "android", "dragonfly", "linux", "netbsd":
3381 ldflags = append(ldflags, "-Wl,--build-id=none")
3382 }
3383 return ldflags
3384 }
3385
3386
3387
3388
3389 func mkAbsFiles(dir string, files []string) []string {
3390 abs := make([]string, len(files))
3391 for i, f := range files {
3392 if !filepath.IsAbs(f) {
3393 f = filepath.Join(dir, f)
3394 }
3395 abs[i] = f
3396 }
3397 return abs
3398 }
3399
3400
3401
3402
3403
3404
3405
3406
3407 func passLongArgsInResponseFiles(cmd *exec.Cmd) (cleanup func()) {
3408 cleanup = func() {}
3409
3410 var argLen int
3411 for _, arg := range cmd.Args {
3412 argLen += len(arg)
3413 }
3414
3415
3416
3417 if !useResponseFile(cmd.Path, argLen) {
3418 return
3419 }
3420
3421 tf, err := os.CreateTemp("", "args")
3422 if err != nil {
3423 log.Fatalf("error writing long arguments to response file: %v", err)
3424 }
3425 cleanup = func() { os.Remove(tf.Name()) }
3426 var buf bytes.Buffer
3427 for _, arg := range cmd.Args[1:] {
3428 fmt.Fprintf(&buf, "%s\n", encodeArg(arg))
3429 }
3430 if _, err := tf.Write(buf.Bytes()); err != nil {
3431 tf.Close()
3432 cleanup()
3433 log.Fatalf("error writing long arguments to response file: %v", err)
3434 }
3435 if err := tf.Close(); err != nil {
3436 cleanup()
3437 log.Fatalf("error writing long arguments to response file: %v", err)
3438 }
3439 cmd.Args = []string{cmd.Args[0], "@" + tf.Name()}
3440 return cleanup
3441 }
3442
3443 func useResponseFile(path string, argLen int) bool {
3444
3445
3446
3447 prog := strings.TrimSuffix(filepath.Base(path), ".exe")
3448 switch prog {
3449 case "compile", "link", "cgo", "asm", "cover":
3450 default:
3451 return false
3452 }
3453
3454 if argLen > sys.ExecArgLengthLimit {
3455 return true
3456 }
3457
3458
3459
3460 isBuilder := os.Getenv("GO_BUILDER_NAME") != ""
3461 if isBuilder && rand.Intn(10) == 0 {
3462 return true
3463 }
3464
3465 return false
3466 }
3467
3468
3469 func encodeArg(arg string) string {
3470
3471 if !strings.ContainsAny(arg, "\\\n") {
3472 return arg
3473 }
3474 var b strings.Builder
3475 for _, r := range arg {
3476 switch r {
3477 case '\\':
3478 b.WriteByte('\\')
3479 b.WriteByte('\\')
3480 case '\n':
3481 b.WriteByte('\\')
3482 b.WriteByte('n')
3483 default:
3484 b.WriteRune(r)
3485 }
3486 }
3487 return b.String()
3488 }
3489
View as plain text