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