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 []byte
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 msg = fmt.Appendf(msg, "%s: invalid //go:debug: %v\n", d.Pos, err)
1000 continue
1001 }
1002 if pos, ok := seen[key]; ok {
1003 msg = fmt.Appendf(msg, "%s: repeated //go:debug for %v\n\t%s: previous //go:debug\n", d.Pos, key, pos)
1004 continue
1005 }
1006 if seen == nil {
1007 seen = make(map[string]token.Position)
1008 }
1009 seen[key] = d.Pos
1010 }
1011 }
1012 if len(msg) > 0 {
1013
1014
1015
1016 err := errors.New("invalid directive")
1017 return b.Shell(a).reportCmd("", "", msg, err)
1018 }
1019 return nil
1020 }
1021
1022 func (b *Builder) cacheObjdirFile(a *Action, c cache.Cache, name string) error {
1023 f, err := os.Open(a.Objdir + name)
1024 if err != nil {
1025 return err
1026 }
1027 defer f.Close()
1028 _, _, err = c.Put(cache.Subkey(a.actionID, name), f)
1029 return err
1030 }
1031
1032 func (b *Builder) findCachedObjdirFile(a *Action, c cache.Cache, name string) (string, error) {
1033 file, _, err := cache.GetFile(c, cache.Subkey(a.actionID, name))
1034 if err != nil {
1035 return "", fmt.Errorf("loading cached file %s: %w", name, err)
1036 }
1037 return file, nil
1038 }
1039
1040 func (b *Builder) loadCachedObjdirFile(a *Action, c cache.Cache, name string) error {
1041 cached, err := b.findCachedObjdirFile(a, c, name)
1042 if err != nil {
1043 return err
1044 }
1045 return b.Shell(a).CopyFile(a.Objdir+name, cached, 0666, true)
1046 }
1047
1048 func (b *Builder) cacheCgoHdr(a *Action) {
1049 c := cache.Default()
1050 b.cacheObjdirFile(a, c, "_cgo_install.h")
1051 }
1052
1053 func (b *Builder) loadCachedCgoHdr(a *Action) error {
1054 c := cache.Default()
1055 return b.loadCachedObjdirFile(a, c, "_cgo_install.h")
1056 }
1057
1058 func (b *Builder) cacheSrcFiles(a *Action, srcfiles []string) {
1059 c := cache.Default()
1060 var buf bytes.Buffer
1061 for _, file := range srcfiles {
1062 if !strings.HasPrefix(file, a.Objdir) {
1063
1064 buf.WriteString("./")
1065 buf.WriteString(file)
1066 buf.WriteString("\n")
1067 continue
1068 }
1069 name := file[len(a.Objdir):]
1070 buf.WriteString(name)
1071 buf.WriteString("\n")
1072 if err := b.cacheObjdirFile(a, c, name); err != nil {
1073 return
1074 }
1075 }
1076 cache.PutBytes(c, cache.Subkey(a.actionID, "srcfiles"), buf.Bytes())
1077 }
1078
1079 func (b *Builder) loadCachedVet(a *Action) error {
1080 c := cache.Default()
1081 list, _, err := cache.GetBytes(c, cache.Subkey(a.actionID, "srcfiles"))
1082 if err != nil {
1083 return fmt.Errorf("reading srcfiles list: %w", err)
1084 }
1085 var srcfiles []string
1086 for _, name := range strings.Split(string(list), "\n") {
1087 if name == "" {
1088 continue
1089 }
1090 if strings.HasPrefix(name, "./") {
1091 srcfiles = append(srcfiles, name[2:])
1092 continue
1093 }
1094 if err := b.loadCachedObjdirFile(a, c, name); err != nil {
1095 return err
1096 }
1097 srcfiles = append(srcfiles, a.Objdir+name)
1098 }
1099 buildVetConfig(a, srcfiles)
1100 return nil
1101 }
1102
1103 func (b *Builder) loadCachedCompiledGoFiles(a *Action) error {
1104 c := cache.Default()
1105 list, _, err := cache.GetBytes(c, cache.Subkey(a.actionID, "srcfiles"))
1106 if err != nil {
1107 return fmt.Errorf("reading srcfiles list: %w", err)
1108 }
1109 var gofiles []string
1110 for _, name := range strings.Split(string(list), "\n") {
1111 if name == "" {
1112 continue
1113 } else if !strings.HasSuffix(name, ".go") {
1114 continue
1115 }
1116 if strings.HasPrefix(name, "./") {
1117 gofiles = append(gofiles, name[len("./"):])
1118 continue
1119 }
1120 file, err := b.findCachedObjdirFile(a, c, name)
1121 if err != nil {
1122 return fmt.Errorf("finding %s: %w", name, err)
1123 }
1124 gofiles = append(gofiles, file)
1125 }
1126 a.Package.CompiledGoFiles = gofiles
1127 return nil
1128 }
1129
1130
1131 type vetConfig struct {
1132 ID string
1133 Compiler string
1134 Dir string
1135 ImportPath string
1136 GoFiles []string
1137 NonGoFiles []string
1138 IgnoredFiles []string
1139
1140 ModulePath string
1141 ModuleVersion string
1142 ImportMap map[string]string
1143 PackageFile map[string]string
1144 Standard map[string]bool
1145 PackageVetx map[string]string
1146 VetxOnly bool
1147 VetxOutput string
1148 GoVersion string
1149
1150 SucceedOnTypecheckFailure bool
1151 }
1152
1153 func buildVetConfig(a *Action, srcfiles []string) {
1154
1155
1156 var gofiles, nongofiles []string
1157 for _, name := range srcfiles {
1158 if strings.HasSuffix(name, ".go") {
1159 gofiles = append(gofiles, name)
1160 } else {
1161 nongofiles = append(nongofiles, name)
1162 }
1163 }
1164
1165 ignored := str.StringList(a.Package.IgnoredGoFiles, a.Package.IgnoredOtherFiles)
1166
1167
1168
1169
1170
1171 vcfg := &vetConfig{
1172 ID: a.Package.ImportPath,
1173 Compiler: cfg.BuildToolchainName,
1174 Dir: a.Package.Dir,
1175 GoFiles: mkAbsFiles(a.Package.Dir, gofiles),
1176 NonGoFiles: mkAbsFiles(a.Package.Dir, nongofiles),
1177 IgnoredFiles: mkAbsFiles(a.Package.Dir, ignored),
1178 ImportPath: a.Package.ImportPath,
1179 ImportMap: make(map[string]string),
1180 PackageFile: make(map[string]string),
1181 Standard: make(map[string]bool),
1182 }
1183 vcfg.GoVersion = "go" + gover.Local()
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 if a.Package.Module.Error == nil {
1192 vcfg.ModulePath = a.Package.Module.Path
1193 vcfg.ModuleVersion = a.Package.Module.Version
1194 }
1195 }
1196 a.vetCfg = vcfg
1197 for i, raw := range a.Package.Internal.RawImports {
1198 final := a.Package.Imports[i]
1199 vcfg.ImportMap[raw] = final
1200 }
1201
1202
1203
1204 vcfgMapped := make(map[string]bool)
1205 for _, p := range vcfg.ImportMap {
1206 vcfgMapped[p] = true
1207 }
1208
1209 for _, a1 := range a.Deps {
1210 p1 := a1.Package
1211 if p1 == nil || p1.ImportPath == "" {
1212 continue
1213 }
1214
1215
1216 if !vcfgMapped[p1.ImportPath] {
1217 vcfg.ImportMap[p1.ImportPath] = p1.ImportPath
1218 }
1219 if a1.built != "" {
1220 vcfg.PackageFile[p1.ImportPath] = a1.built
1221 }
1222 if p1.Standard {
1223 vcfg.Standard[p1.ImportPath] = true
1224 }
1225 }
1226 }
1227
1228
1229
1230 var VetTool string
1231
1232
1233
1234 var VetFlags []string
1235
1236
1237 var VetExplicit bool
1238
1239 func (b *Builder) vet(ctx context.Context, a *Action) error {
1240
1241
1242
1243 a.Failed = false
1244
1245 if a.Deps[0].Failed {
1246
1247
1248
1249 return nil
1250 }
1251
1252 vcfg := a.Deps[0].vetCfg
1253 if vcfg == nil {
1254
1255 return fmt.Errorf("vet config not found")
1256 }
1257
1258 sh := b.Shell(a)
1259
1260 vcfg.VetxOnly = a.VetxOnly
1261 vcfg.VetxOutput = a.Objdir + "vet.out"
1262 vcfg.PackageVetx = make(map[string]string)
1263
1264 h := cache.NewHash("vet " + a.Package.ImportPath)
1265 fmt.Fprintf(h, "vet %q\n", b.toolID("vet"))
1266
1267 vetFlags := VetFlags
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285 if a.Package.Goroot && !VetExplicit && VetTool == "" {
1286
1287
1288
1289
1290
1291
1292
1293
1294 vetFlags = []string{"-unsafeptr=false"}
1295
1296
1297
1298
1299
1300
1301
1302
1303 if cfg.CmdName == "test" {
1304 vetFlags = append(vetFlags, "-unreachable=false")
1305 }
1306 }
1307
1308
1309
1310
1311
1312
1313 fmt.Fprintf(h, "vetflags %q\n", vetFlags)
1314
1315 fmt.Fprintf(h, "pkg %q\n", a.Deps[0].actionID)
1316 for _, a1 := range a.Deps {
1317 if a1.Mode == "vet" && a1.built != "" {
1318 fmt.Fprintf(h, "vetout %q %s\n", a1.Package.ImportPath, b.fileHash(a1.built))
1319 vcfg.PackageVetx[a1.Package.ImportPath] = a1.built
1320 }
1321 }
1322 key := cache.ActionID(h.Sum())
1323
1324 if vcfg.VetxOnly && !cfg.BuildA {
1325 c := cache.Default()
1326 if file, _, err := cache.GetFile(c, key); err == nil {
1327 a.built = file
1328 return nil
1329 }
1330 }
1331
1332 js, err := json.MarshalIndent(vcfg, "", "\t")
1333 if err != nil {
1334 return fmt.Errorf("internal error marshaling vet config: %v", err)
1335 }
1336 js = append(js, '\n')
1337 if err := sh.writeFile(a.Objdir+"vet.cfg", js); err != nil {
1338 return err
1339 }
1340
1341
1342 env := b.cCompilerEnv()
1343 if cfg.BuildToolchainName == "gccgo" {
1344 env = append(env, "GCCGO="+BuildToolchain.compiler())
1345 }
1346
1347 p := a.Package
1348 tool := VetTool
1349 if tool == "" {
1350 tool = base.Tool("vet")
1351 }
1352 runErr := sh.run(p.Dir, p.ImportPath, env, cfg.BuildToolexec, tool, vetFlags, a.Objdir+"vet.cfg")
1353
1354
1355 if f, err := os.Open(vcfg.VetxOutput); err == nil {
1356 a.built = vcfg.VetxOutput
1357 cache.Default().Put(key, f)
1358 f.Close()
1359 }
1360
1361 return runErr
1362 }
1363
1364
1365 func (b *Builder) linkActionID(a *Action) cache.ActionID {
1366 p := a.Package
1367 h := cache.NewHash("link " + p.ImportPath)
1368
1369
1370 fmt.Fprintf(h, "link\n")
1371 fmt.Fprintf(h, "buildmode %s goos %s goarch %s\n", cfg.BuildBuildmode, cfg.Goos, cfg.Goarch)
1372 fmt.Fprintf(h, "import %q\n", p.ImportPath)
1373 fmt.Fprintf(h, "omitdebug %v standard %v local %v prefix %q\n", p.Internal.OmitDebug, p.Standard, p.Internal.Local, p.Internal.LocalPrefix)
1374 if cfg.BuildTrimpath {
1375 fmt.Fprintln(h, "trimpath")
1376 }
1377
1378
1379 b.printLinkerConfig(h, p)
1380
1381
1382 for _, a1 := range a.Deps {
1383 p1 := a1.Package
1384 if p1 != nil {
1385 if a1.built != "" || a1.buildID != "" {
1386 buildID := a1.buildID
1387 if buildID == "" {
1388 buildID = b.buildID(a1.built)
1389 }
1390 fmt.Fprintf(h, "packagefile %s=%s\n", p1.ImportPath, contentID(buildID))
1391 }
1392
1393
1394 if p1.Name == "main" {
1395 fmt.Fprintf(h, "packagemain %s\n", a1.buildID)
1396 }
1397 if p1.Shlib != "" {
1398 fmt.Fprintf(h, "packageshlib %s=%s\n", p1.ImportPath, contentID(b.buildID(p1.Shlib)))
1399 }
1400 }
1401 }
1402
1403 return h.Sum()
1404 }
1405
1406
1407
1408 func (b *Builder) printLinkerConfig(h io.Writer, p *load.Package) {
1409 switch cfg.BuildToolchainName {
1410 default:
1411 base.Fatalf("linkActionID: unknown toolchain %q", cfg.BuildToolchainName)
1412
1413 case "gc":
1414 fmt.Fprintf(h, "link %s %q %s\n", b.toolID("link"), forcedLdflags, ldBuildmode)
1415 if p != nil {
1416 fmt.Fprintf(h, "linkflags %q\n", p.Internal.Ldflags)
1417 }
1418
1419
1420 key, val, _ := cfg.GetArchEnv()
1421 fmt.Fprintf(h, "%s=%s\n", key, val)
1422
1423 if cfg.CleanGOEXPERIMENT != "" {
1424 fmt.Fprintf(h, "GOEXPERIMENT=%q\n", cfg.CleanGOEXPERIMENT)
1425 }
1426
1427
1428
1429 gorootFinal := cfg.GOROOT
1430 if cfg.BuildTrimpath {
1431 gorootFinal = ""
1432 }
1433 fmt.Fprintf(h, "GOROOT=%s\n", gorootFinal)
1434
1435
1436 fmt.Fprintf(h, "GO_EXTLINK_ENABLED=%s\n", cfg.Getenv("GO_EXTLINK_ENABLED"))
1437
1438
1439
1440
1441 case "gccgo":
1442 id, _, err := b.gccToolID(BuildToolchain.linker(), "go")
1443 if err != nil {
1444 base.Fatalf("%v", err)
1445 }
1446 fmt.Fprintf(h, "link %s %s\n", id, ldBuildmode)
1447
1448 }
1449 }
1450
1451
1452
1453 func (b *Builder) link(ctx context.Context, a *Action) (err error) {
1454 if b.useCache(a, b.linkActionID(a), a.Package.Target, !b.IsCmdList) || b.IsCmdList {
1455 return nil
1456 }
1457 defer b.flushOutput(a)
1458
1459 sh := b.Shell(a)
1460 if err := sh.Mkdir(a.Objdir); err != nil {
1461 return err
1462 }
1463
1464 importcfg := a.Objdir + "importcfg.link"
1465 if err := b.writeLinkImportcfg(a, importcfg); err != nil {
1466 return err
1467 }
1468
1469 if err := AllowInstall(a); err != nil {
1470 return err
1471 }
1472
1473
1474 dir, _ := filepath.Split(a.Target)
1475 if dir != "" {
1476 if err := sh.Mkdir(dir); err != nil {
1477 return err
1478 }
1479 }
1480
1481 if err := BuildToolchain.ld(b, a, a.Target, importcfg, a.Deps[0].built); err != nil {
1482 return err
1483 }
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501 if err := b.updateBuildID(a, a.Target, !a.Package.Internal.OmitDebug); err != nil {
1502 return err
1503 }
1504
1505 a.built = a.Target
1506 return nil
1507 }
1508
1509 func (b *Builder) writeLinkImportcfg(a *Action, file string) error {
1510
1511 var icfg bytes.Buffer
1512 for _, a1 := range a.Deps {
1513 p1 := a1.Package
1514 if p1 == nil {
1515 continue
1516 }
1517 fmt.Fprintf(&icfg, "packagefile %s=%s\n", p1.ImportPath, a1.built)
1518 if p1.Shlib != "" {
1519 fmt.Fprintf(&icfg, "packageshlib %s=%s\n", p1.ImportPath, p1.Shlib)
1520 }
1521 }
1522 info := ""
1523 if a.Package.Internal.BuildInfo != nil {
1524 info = a.Package.Internal.BuildInfo.String()
1525 }
1526 fmt.Fprintf(&icfg, "modinfo %q\n", modload.ModInfoData(info))
1527 return b.Shell(a).writeFile(file, icfg.Bytes())
1528 }
1529
1530
1531
1532 func (b *Builder) PkgconfigCmd() string {
1533 return envList("PKG_CONFIG", cfg.DefaultPkgConfig)[0]
1534 }
1535
1536
1537
1538
1539
1540
1541
1542 func splitPkgConfigOutput(out []byte) ([]string, error) {
1543 if len(out) == 0 {
1544 return nil, nil
1545 }
1546 var flags []string
1547 flag := make([]byte, 0, len(out))
1548 didQuote := false
1549 escaped := false
1550 quote := byte(0)
1551
1552 for _, c := range out {
1553 if escaped {
1554 if quote == '"' {
1555
1556
1557
1558 switch c {
1559 case '$', '`', '"', '\\', '\n':
1560
1561 default:
1562
1563 flag = append(flag, '\\', c)
1564 escaped = false
1565 continue
1566 }
1567 }
1568
1569 if c == '\n' {
1570
1571
1572 } else {
1573 flag = append(flag, c)
1574 }
1575 escaped = false
1576 continue
1577 }
1578
1579 if quote != 0 && c == quote {
1580 quote = 0
1581 continue
1582 }
1583 switch quote {
1584 case '\'':
1585
1586 flag = append(flag, c)
1587 continue
1588 case '"':
1589
1590
1591 switch c {
1592 case '`', '$', '\\':
1593 default:
1594 flag = append(flag, c)
1595 continue
1596 }
1597 }
1598
1599
1600
1601 switch c {
1602 case '|', '&', ';', '<', '>', '(', ')', '$', '`':
1603 return nil, fmt.Errorf("unexpected shell character %q in pkgconf output", c)
1604
1605 case '\\':
1606
1607
1608 escaped = true
1609 continue
1610
1611 case '"', '\'':
1612 quote = c
1613 didQuote = true
1614 continue
1615
1616 case ' ', '\t', '\n':
1617 if len(flag) > 0 || didQuote {
1618 flags = append(flags, string(flag))
1619 }
1620 flag, didQuote = flag[:0], false
1621 continue
1622 }
1623
1624 flag = append(flag, c)
1625 }
1626
1627
1628
1629
1630 if quote != 0 {
1631 return nil, errors.New("unterminated quoted string in pkgconf output")
1632 }
1633 if escaped {
1634 return nil, errors.New("broken character escaping in pkgconf output")
1635 }
1636
1637 if len(flag) > 0 || didQuote {
1638 flags = append(flags, string(flag))
1639 }
1640 return flags, nil
1641 }
1642
1643
1644 func (b *Builder) getPkgConfigFlags(a *Action) (cflags, ldflags []string, err error) {
1645 p := a.Package
1646 sh := b.Shell(a)
1647 if pcargs := p.CgoPkgConfig; len(pcargs) > 0 {
1648
1649
1650 var pcflags []string
1651 var pkgs []string
1652 for _, pcarg := range pcargs {
1653 if pcarg == "--" {
1654
1655 } else if strings.HasPrefix(pcarg, "--") {
1656 pcflags = append(pcflags, pcarg)
1657 } else {
1658 pkgs = append(pkgs, pcarg)
1659 }
1660 }
1661 for _, pkg := range pkgs {
1662 if !load.SafeArg(pkg) {
1663 return nil, nil, fmt.Errorf("invalid pkg-config package name: %s", pkg)
1664 }
1665 }
1666 var out []byte
1667 out, err = sh.runOut(p.Dir, nil, b.PkgconfigCmd(), "--cflags", pcflags, "--", pkgs)
1668 if err != nil {
1669 desc := b.PkgconfigCmd() + " --cflags " + strings.Join(pcflags, " ") + " -- " + strings.Join(pkgs, " ")
1670 return nil, nil, sh.reportCmd(desc, "", out, err)
1671 }
1672 if len(out) > 0 {
1673 cflags, err = splitPkgConfigOutput(bytes.TrimSpace(out))
1674 if err != nil {
1675 return nil, nil, err
1676 }
1677 if err := checkCompilerFlags("CFLAGS", "pkg-config --cflags", cflags); err != nil {
1678 return nil, nil, err
1679 }
1680 }
1681 out, err = sh.runOut(p.Dir, nil, b.PkgconfigCmd(), "--libs", pcflags, "--", pkgs)
1682 if err != nil {
1683 desc := b.PkgconfigCmd() + " --libs " + strings.Join(pcflags, " ") + " -- " + strings.Join(pkgs, " ")
1684 return nil, nil, sh.reportCmd(desc, "", out, err)
1685 }
1686 if len(out) > 0 {
1687
1688
1689 ldflags, err = splitPkgConfigOutput(bytes.TrimSpace(out))
1690 if err != nil {
1691 return nil, nil, err
1692 }
1693 if err := checkLinkerFlags("LDFLAGS", "pkg-config --libs", ldflags); err != nil {
1694 return nil, nil, err
1695 }
1696 }
1697 }
1698
1699 return
1700 }
1701
1702 func (b *Builder) installShlibname(ctx context.Context, a *Action) error {
1703 if err := AllowInstall(a); err != nil {
1704 return err
1705 }
1706
1707 sh := b.Shell(a)
1708 a1 := a.Deps[0]
1709 if !cfg.BuildN {
1710 if err := sh.Mkdir(filepath.Dir(a.Target)); err != nil {
1711 return err
1712 }
1713 }
1714 return sh.writeFile(a.Target, []byte(filepath.Base(a1.Target)+"\n"))
1715 }
1716
1717 func (b *Builder) linkSharedActionID(a *Action) cache.ActionID {
1718 h := cache.NewHash("linkShared")
1719
1720
1721 fmt.Fprintf(h, "linkShared\n")
1722 fmt.Fprintf(h, "goos %s goarch %s\n", cfg.Goos, cfg.Goarch)
1723
1724
1725 b.printLinkerConfig(h, nil)
1726
1727
1728 for _, a1 := range a.Deps {
1729 p1 := a1.Package
1730 if a1.built == "" {
1731 continue
1732 }
1733 if p1 != nil {
1734 fmt.Fprintf(h, "packagefile %s=%s\n", p1.ImportPath, contentID(b.buildID(a1.built)))
1735 if p1.Shlib != "" {
1736 fmt.Fprintf(h, "packageshlib %s=%s\n", p1.ImportPath, contentID(b.buildID(p1.Shlib)))
1737 }
1738 }
1739 }
1740
1741 for _, a1 := range a.Deps[0].Deps {
1742 p1 := a1.Package
1743 fmt.Fprintf(h, "top %s=%s\n", p1.ImportPath, contentID(b.buildID(a1.built)))
1744 }
1745
1746 return h.Sum()
1747 }
1748
1749 func (b *Builder) linkShared(ctx context.Context, a *Action) (err error) {
1750 if b.useCache(a, b.linkSharedActionID(a), a.Target, !b.IsCmdList) || b.IsCmdList {
1751 return nil
1752 }
1753 defer b.flushOutput(a)
1754
1755 if err := AllowInstall(a); err != nil {
1756 return err
1757 }
1758
1759 if err := b.Shell(a).Mkdir(a.Objdir); err != nil {
1760 return err
1761 }
1762
1763 importcfg := a.Objdir + "importcfg.link"
1764 if err := b.writeLinkImportcfg(a, importcfg); err != nil {
1765 return err
1766 }
1767
1768
1769
1770 a.built = a.Target
1771 return BuildToolchain.ldShared(b, a, a.Deps[0].Deps, a.Target, importcfg, a.Deps)
1772 }
1773
1774
1775 func BuildInstallFunc(b *Builder, ctx context.Context, a *Action) (err error) {
1776 defer func() {
1777 if err != nil {
1778
1779
1780
1781 sep, path := "", ""
1782 if a.Package != nil {
1783 sep, path = " ", a.Package.ImportPath
1784 }
1785 err = fmt.Errorf("go %s%s%s: %v", cfg.CmdName, sep, path, err)
1786 }
1787 }()
1788 sh := b.Shell(a)
1789
1790 a1 := a.Deps[0]
1791 a.buildID = a1.buildID
1792 if a.json != nil {
1793 a.json.BuildID = a.buildID
1794 }
1795
1796
1797
1798
1799
1800
1801 if a1.built == a.Target {
1802 a.built = a.Target
1803 if !a.buggyInstall {
1804 b.cleanup(a1)
1805 }
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824 if !a.buggyInstall && !b.IsCmdList {
1825 if cfg.BuildN {
1826 sh.ShowCmd("", "touch %s", a.Target)
1827 } else if err := AllowInstall(a); err == nil {
1828 now := time.Now()
1829 os.Chtimes(a.Target, now, now)
1830 }
1831 }
1832 return nil
1833 }
1834
1835
1836
1837 if b.IsCmdList {
1838 a.built = a1.built
1839 return nil
1840 }
1841 if err := AllowInstall(a); err != nil {
1842 return err
1843 }
1844
1845 if err := sh.Mkdir(a.Objdir); err != nil {
1846 return err
1847 }
1848
1849 perm := fs.FileMode(0666)
1850 if a1.Mode == "link" {
1851 switch cfg.BuildBuildmode {
1852 case "c-archive", "c-shared", "plugin":
1853 default:
1854 perm = 0777
1855 }
1856 }
1857
1858
1859 dir, _ := filepath.Split(a.Target)
1860 if dir != "" {
1861 if err := sh.Mkdir(dir); err != nil {
1862 return err
1863 }
1864 }
1865
1866 if !a.buggyInstall {
1867 defer b.cleanup(a1)
1868 }
1869
1870 return sh.moveOrCopyFile(a.Target, a1.built, perm, false)
1871 }
1872
1873
1874
1875
1876
1877
1878 var AllowInstall = func(*Action) error { return nil }
1879
1880
1881
1882
1883
1884 func (b *Builder) cleanup(a *Action) {
1885 if !cfg.BuildWork {
1886 b.Shell(a).RemoveAll(a.Objdir)
1887 }
1888 }
1889
1890
1891 func (b *Builder) installHeader(ctx context.Context, a *Action) error {
1892 sh := b.Shell(a)
1893
1894 src := a.Objdir + "_cgo_install.h"
1895 if _, err := os.Stat(src); os.IsNotExist(err) {
1896
1897
1898
1899
1900
1901 if cfg.BuildX {
1902 sh.ShowCmd("", "# %s not created", src)
1903 }
1904 return nil
1905 }
1906
1907 if err := AllowInstall(a); err != nil {
1908 return err
1909 }
1910
1911 dir, _ := filepath.Split(a.Target)
1912 if dir != "" {
1913 if err := sh.Mkdir(dir); err != nil {
1914 return err
1915 }
1916 }
1917
1918 return sh.moveOrCopyFile(a.Target, src, 0666, true)
1919 }
1920
1921
1922
1923
1924 func (b *Builder) cover(a *Action, dst, src string, varName string) error {
1925 return b.Shell(a).run(a.Objdir, "", nil,
1926 cfg.BuildToolexec,
1927 base.Tool("cover"),
1928 "-mode", a.Package.Internal.Cover.Mode,
1929 "-var", varName,
1930 "-o", dst,
1931 src)
1932 }
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942 func (b *Builder) cover2(a *Action, infiles, outfiles []string, varName string, mode string) ([]string, error) {
1943 pkgcfg := a.Objdir + "pkgcfg.txt"
1944 covoutputs := a.Objdir + "coveroutfiles.txt"
1945 odir := filepath.Dir(outfiles[0])
1946 cv := filepath.Join(odir, "covervars.go")
1947 outfiles = append([]string{cv}, outfiles...)
1948 if err := b.writeCoverPkgInputs(a, pkgcfg, covoutputs, outfiles); err != nil {
1949 return nil, err
1950 }
1951 args := []string{base.Tool("cover"),
1952 "-pkgcfg", pkgcfg,
1953 "-mode", mode,
1954 "-var", varName,
1955 "-outfilelist", covoutputs,
1956 }
1957 args = append(args, infiles...)
1958 if err := b.Shell(a).run(a.Objdir, "", nil,
1959 cfg.BuildToolexec, args); err != nil {
1960 return nil, err
1961 }
1962 return outfiles, nil
1963 }
1964
1965 func (b *Builder) writeCoverPkgInputs(a *Action, pconfigfile string, covoutputsfile string, outfiles []string) error {
1966 sh := b.Shell(a)
1967 p := a.Package
1968 p.Internal.Cover.Cfg = a.Objdir + "coveragecfg"
1969 pcfg := covcmd.CoverPkgConfig{
1970 PkgPath: p.ImportPath,
1971 PkgName: p.Name,
1972
1973
1974
1975
1976 Granularity: "perblock",
1977 OutConfig: p.Internal.Cover.Cfg,
1978 Local: p.Internal.Local,
1979 }
1980 if ba, ok := a.Actor.(*buildActor); ok && ba.covMetaFileName != "" {
1981 pcfg.EmitMetaFile = a.Objdir + ba.covMetaFileName
1982 }
1983 if a.Package.Module != nil {
1984 pcfg.ModulePath = a.Package.Module.Path
1985 }
1986 data, err := json.Marshal(pcfg)
1987 if err != nil {
1988 return err
1989 }
1990 data = append(data, '\n')
1991 if err := sh.writeFile(pconfigfile, data); err != nil {
1992 return err
1993 }
1994 var sb strings.Builder
1995 for i := range outfiles {
1996 fmt.Fprintf(&sb, "%s\n", outfiles[i])
1997 }
1998 return sh.writeFile(covoutputsfile, []byte(sb.String()))
1999 }
2000
2001 var objectMagic = [][]byte{
2002 {'!', '<', 'a', 'r', 'c', 'h', '>', '\n'},
2003 {'<', 'b', 'i', 'g', 'a', 'f', '>', '\n'},
2004 {'\x7F', 'E', 'L', 'F'},
2005 {0xFE, 0xED, 0xFA, 0xCE},
2006 {0xFE, 0xED, 0xFA, 0xCF},
2007 {0xCE, 0xFA, 0xED, 0xFE},
2008 {0xCF, 0xFA, 0xED, 0xFE},
2009 {0x4d, 0x5a, 0x90, 0x00, 0x03, 0x00},
2010 {0x4d, 0x5a, 0x78, 0x00, 0x01, 0x00},
2011 {0x00, 0x00, 0x01, 0xEB},
2012 {0x00, 0x00, 0x8a, 0x97},
2013 {0x00, 0x00, 0x06, 0x47},
2014 {0x00, 0x61, 0x73, 0x6D},
2015 {0x01, 0xDF},
2016 {0x01, 0xF7},
2017 }
2018
2019 func isObject(s string) bool {
2020 f, err := os.Open(s)
2021 if err != nil {
2022 return false
2023 }
2024 defer f.Close()
2025 buf := make([]byte, 64)
2026 io.ReadFull(f, buf)
2027 for _, magic := range objectMagic {
2028 if bytes.HasPrefix(buf, magic) {
2029 return true
2030 }
2031 }
2032 return false
2033 }
2034
2035
2036
2037
2038 func (b *Builder) cCompilerEnv() []string {
2039 return []string{"TERM=dumb"}
2040 }
2041
2042
2043
2044
2045
2046
2047 func mkAbs(dir, f string) string {
2048
2049
2050
2051
2052 if filepath.IsAbs(f) || strings.HasPrefix(f, "$WORK") {
2053 return f
2054 }
2055 return filepath.Join(dir, f)
2056 }
2057
2058 type toolchain interface {
2059
2060
2061 gc(b *Builder, a *Action, archive string, importcfg, embedcfg []byte, symabis string, asmhdr bool, pgoProfile string, gofiles []string) (ofile string, out []byte, err error)
2062
2063
2064 cc(b *Builder, a *Action, ofile, cfile string) error
2065
2066
2067 asm(b *Builder, a *Action, sfiles []string) ([]string, error)
2068
2069
2070 symabis(b *Builder, a *Action, sfiles []string) (string, error)
2071
2072
2073
2074 pack(b *Builder, a *Action, afile string, ofiles []string) error
2075
2076 ld(b *Builder, root *Action, targetPath, importcfg, mainpkg string) error
2077
2078 ldShared(b *Builder, root *Action, toplevelactions []*Action, targetPath, importcfg string, allactions []*Action) error
2079
2080 compiler() string
2081 linker() string
2082 }
2083
2084 type noToolchain struct{}
2085
2086 func noCompiler() error {
2087 log.Fatalf("unknown compiler %q", cfg.BuildContext.Compiler)
2088 return nil
2089 }
2090
2091 func (noToolchain) compiler() string {
2092 noCompiler()
2093 return ""
2094 }
2095
2096 func (noToolchain) linker() string {
2097 noCompiler()
2098 return ""
2099 }
2100
2101 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) {
2102 return "", nil, noCompiler()
2103 }
2104
2105 func (noToolchain) asm(b *Builder, a *Action, sfiles []string) ([]string, error) {
2106 return nil, noCompiler()
2107 }
2108
2109 func (noToolchain) symabis(b *Builder, a *Action, sfiles []string) (string, error) {
2110 return "", noCompiler()
2111 }
2112
2113 func (noToolchain) pack(b *Builder, a *Action, afile string, ofiles []string) error {
2114 return noCompiler()
2115 }
2116
2117 func (noToolchain) ld(b *Builder, root *Action, targetPath, importcfg, mainpkg string) error {
2118 return noCompiler()
2119 }
2120
2121 func (noToolchain) ldShared(b *Builder, root *Action, toplevelactions []*Action, targetPath, importcfg string, allactions []*Action) error {
2122 return noCompiler()
2123 }
2124
2125 func (noToolchain) cc(b *Builder, a *Action, ofile, cfile string) error {
2126 return noCompiler()
2127 }
2128
2129
2130 func (b *Builder) gcc(a *Action, workdir, out string, flags []string, cfile string) error {
2131 p := a.Package
2132 return b.ccompile(a, out, flags, cfile, b.GccCmd(p.Dir, workdir))
2133 }
2134
2135
2136 func (b *Builder) gxx(a *Action, workdir, out string, flags []string, cxxfile string) error {
2137 p := a.Package
2138 return b.ccompile(a, out, flags, cxxfile, b.GxxCmd(p.Dir, workdir))
2139 }
2140
2141
2142 func (b *Builder) gfortran(a *Action, workdir, out string, flags []string, ffile string) error {
2143 p := a.Package
2144 return b.ccompile(a, out, flags, ffile, b.gfortranCmd(p.Dir, workdir))
2145 }
2146
2147
2148 func (b *Builder) ccompile(a *Action, outfile string, flags []string, file string, compiler []string) error {
2149 p := a.Package
2150 sh := b.Shell(a)
2151 file = mkAbs(p.Dir, file)
2152 outfile = mkAbs(p.Dir, outfile)
2153
2154
2155
2156
2157
2158
2159 if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") {
2160 if cfg.BuildTrimpath || p.Goroot {
2161 prefixMapFlag := "-fdebug-prefix-map"
2162 if b.gccSupportsFlag(compiler, "-ffile-prefix-map=a=b") {
2163 prefixMapFlag = "-ffile-prefix-map"
2164 }
2165
2166
2167
2168 var from, toPath string
2169 if m := p.Module; m == nil {
2170 if p.Root == "" {
2171 from = p.Dir
2172 toPath = p.ImportPath
2173 } else if p.Goroot {
2174 from = p.Root
2175 toPath = "GOROOT"
2176 } else {
2177 from = p.Root
2178 toPath = "GOPATH"
2179 }
2180 } else if m.Dir == "" {
2181
2182
2183 from = modload.VendorDir()
2184 toPath = "vendor"
2185 } else {
2186 from = m.Dir
2187 toPath = m.Path
2188 if m.Version != "" {
2189 toPath += "@" + m.Version
2190 }
2191 }
2192
2193
2194
2195 var to string
2196 if cfg.BuildContext.GOOS == "windows" {
2197 to = filepath.Join(`\\_\_`, toPath)
2198 } else {
2199 to = filepath.Join("/_", toPath)
2200 }
2201 flags = append(slices.Clip(flags), prefixMapFlag+"="+from+"="+to)
2202 }
2203 }
2204
2205
2206
2207 if b.gccSupportsFlag(compiler, "-frandom-seed=1") {
2208 flags = append(flags, "-frandom-seed="+buildid.HashToString(a.actionID))
2209 }
2210
2211 overlayPath := file
2212 if p, ok := a.nonGoOverlay[overlayPath]; ok {
2213 overlayPath = p
2214 }
2215 output, err := sh.runOut(filepath.Dir(overlayPath), b.cCompilerEnv(), compiler, flags, "-o", outfile, "-c", filepath.Base(overlayPath))
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225 if bytes.Contains(output, []byte("DWARF2 only supports one section per compilation unit")) {
2226 newFlags := make([]string, 0, len(flags))
2227 for _, f := range flags {
2228 if !strings.HasPrefix(f, "-g") {
2229 newFlags = append(newFlags, f)
2230 }
2231 }
2232 if len(newFlags) < len(flags) {
2233 return b.ccompile(a, outfile, newFlags, file, compiler)
2234 }
2235 }
2236
2237 if len(output) > 0 && err == nil && os.Getenv("GO_BUILDER_NAME") != "" {
2238 output = append(output, "C compiler warning promoted to error on Go builders\n"...)
2239 err = errors.New("warning promoted to error")
2240 }
2241
2242 return sh.reportCmd("", "", output, err)
2243 }
2244
2245
2246
2247 func (b *Builder) gccld(a *Action, objdir, outfile string, flags []string, objs []string) error {
2248 p := a.Package
2249 sh := b.Shell(a)
2250 var cmd []string
2251 if len(p.CXXFiles) > 0 || len(p.SwigCXXFiles) > 0 {
2252 cmd = b.GxxCmd(p.Dir, objdir)
2253 } else {
2254 cmd = b.GccCmd(p.Dir, objdir)
2255 }
2256
2257 cmdargs := []any{cmd, "-o", outfile, objs, flags}
2258 out, err := sh.runOut(base.Cwd(), b.cCompilerEnv(), cmdargs...)
2259
2260 if len(out) > 0 {
2261
2262
2263 var save [][]byte
2264 var skipLines int
2265 for _, line := range bytes.SplitAfter(out, []byte("\n")) {
2266
2267 if bytes.Contains(line, []byte("ld: warning: text-based stub file")) {
2268 continue
2269 }
2270
2271 if skipLines > 0 {
2272 skipLines--
2273 continue
2274 }
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285 if p.ImportPath == "runtime/cgo" && bytes.Contains(line, []byte("ld: 0711-224 WARNING: Duplicate symbol: .main")) {
2286 skipLines = 1
2287 continue
2288 }
2289
2290 save = append(save, line)
2291 }
2292 out = bytes.Join(save, nil)
2293 }
2294
2295
2296 if cfg.BuildN || cfg.BuildX {
2297 sh.reportCmd("", "", out, nil)
2298 }
2299 return err
2300 }
2301
2302
2303
2304 func (b *Builder) GccCmd(incdir, workdir string) []string {
2305 return b.compilerCmd(b.ccExe(), incdir, workdir)
2306 }
2307
2308
2309
2310 func (b *Builder) GxxCmd(incdir, workdir string) []string {
2311 return b.compilerCmd(b.cxxExe(), incdir, workdir)
2312 }
2313
2314
2315 func (b *Builder) gfortranCmd(incdir, workdir string) []string {
2316 return b.compilerCmd(b.fcExe(), incdir, workdir)
2317 }
2318
2319
2320 func (b *Builder) ccExe() []string {
2321 return envList("CC", cfg.DefaultCC(cfg.Goos, cfg.Goarch))
2322 }
2323
2324
2325 func (b *Builder) cxxExe() []string {
2326 return envList("CXX", cfg.DefaultCXX(cfg.Goos, cfg.Goarch))
2327 }
2328
2329
2330 func (b *Builder) fcExe() []string {
2331 return envList("FC", "gfortran")
2332 }
2333
2334
2335
2336 func (b *Builder) compilerCmd(compiler []string, incdir, workdir string) []string {
2337 a := append(compiler, "-I", incdir)
2338
2339
2340
2341 if cfg.Goos != "windows" {
2342 a = append(a, "-fPIC")
2343 }
2344 a = append(a, b.gccArchArgs()...)
2345
2346
2347 if cfg.BuildContext.CgoEnabled {
2348 switch cfg.Goos {
2349 case "windows":
2350 a = append(a, "-mthreads")
2351 default:
2352 a = append(a, "-pthread")
2353 }
2354 }
2355
2356 if cfg.Goos == "aix" {
2357
2358 a = append(a, "-mcmodel=large")
2359 }
2360
2361
2362 if b.gccSupportsFlag(compiler, "-fno-caret-diagnostics") {
2363 a = append(a, "-fno-caret-diagnostics")
2364 }
2365
2366 if b.gccSupportsFlag(compiler, "-Qunused-arguments") {
2367 a = append(a, "-Qunused-arguments")
2368 }
2369
2370
2371
2372
2373 if b.gccSupportsFlag(compiler, "-Wl,--no-gc-sections") {
2374 a = append(a, "-Wl,--no-gc-sections")
2375 }
2376
2377
2378 a = append(a, "-fmessage-length=0")
2379
2380
2381 if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") {
2382 if workdir == "" {
2383 workdir = b.WorkDir
2384 }
2385 workdir = strings.TrimSuffix(workdir, string(filepath.Separator))
2386 if b.gccSupportsFlag(compiler, "-ffile-prefix-map=a=b") {
2387 a = append(a, "-ffile-prefix-map="+workdir+"=/tmp/go-build")
2388 } else {
2389 a = append(a, "-fdebug-prefix-map="+workdir+"=/tmp/go-build")
2390 }
2391 }
2392
2393
2394
2395 if b.gccSupportsFlag(compiler, "-gno-record-gcc-switches") {
2396 a = append(a, "-gno-record-gcc-switches")
2397 }
2398
2399
2400
2401
2402 if cfg.Goos == "darwin" || cfg.Goos == "ios" {
2403 a = append(a, "-fno-common")
2404 }
2405
2406 return a
2407 }
2408
2409
2410
2411
2412
2413 func (b *Builder) gccNoPie(linker []string) string {
2414 if b.gccSupportsFlag(linker, "-no-pie") {
2415 return "-no-pie"
2416 }
2417 if b.gccSupportsFlag(linker, "-nopie") {
2418 return "-nopie"
2419 }
2420 return ""
2421 }
2422
2423
2424 func (b *Builder) gccSupportsFlag(compiler []string, flag string) bool {
2425
2426
2427
2428 sh := b.BackgroundShell()
2429
2430 key := [2]string{compiler[0], flag}
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448 tmp := os.DevNull
2449 if runtime.GOOS == "windows" || runtime.GOOS == "ios" {
2450 f, err := os.CreateTemp(b.WorkDir, "")
2451 if err != nil {
2452 return false
2453 }
2454 f.Close()
2455 tmp = f.Name()
2456 defer os.Remove(tmp)
2457 }
2458
2459 cmdArgs := str.StringList(compiler, flag)
2460 if strings.HasPrefix(flag, "-Wl,") {
2461 ldflags, err := buildFlags("LDFLAGS", DefaultCFlags, nil, checkLinkerFlags)
2462 if err != nil {
2463 return false
2464 }
2465 cmdArgs = append(cmdArgs, ldflags...)
2466 } else {
2467 cflags, err := buildFlags("CFLAGS", DefaultCFlags, nil, checkCompilerFlags)
2468 if err != nil {
2469 return false
2470 }
2471 cmdArgs = append(cmdArgs, cflags...)
2472 cmdArgs = append(cmdArgs, "-c")
2473 }
2474
2475 cmdArgs = append(cmdArgs, "-x", "c", "-", "-o", tmp)
2476
2477 if cfg.BuildN {
2478 sh.ShowCmd(b.WorkDir, "%s || true", joinUnambiguously(cmdArgs))
2479 return false
2480 }
2481
2482
2483 compilerID, cacheOK := b.gccCompilerID(compiler[0])
2484
2485 b.exec.Lock()
2486 defer b.exec.Unlock()
2487 if b, ok := b.flagCache[key]; ok {
2488 return b
2489 }
2490 if b.flagCache == nil {
2491 b.flagCache = make(map[[2]string]bool)
2492 }
2493
2494
2495 var flagID cache.ActionID
2496 if cacheOK {
2497 flagID = cache.Subkey(compilerID, "gccSupportsFlag "+flag)
2498 if data, _, err := cache.GetBytes(cache.Default(), flagID); err == nil {
2499 supported := string(data) == "true"
2500 b.flagCache[key] = supported
2501 return supported
2502 }
2503 }
2504
2505 if cfg.BuildX {
2506 sh.ShowCmd(b.WorkDir, "%s || true", joinUnambiguously(cmdArgs))
2507 }
2508 cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
2509 cmd.Dir = b.WorkDir
2510 cmd.Env = append(cmd.Environ(), "LC_ALL=C")
2511 out, _ := cmd.CombinedOutput()
2512
2513
2514
2515
2516
2517
2518 supported := !bytes.Contains(out, []byte("unrecognized")) &&
2519 !bytes.Contains(out, []byte("unknown")) &&
2520 !bytes.Contains(out, []byte("unrecognised")) &&
2521 !bytes.Contains(out, []byte("is not supported")) &&
2522 !bytes.Contains(out, []byte("not recognized")) &&
2523 !bytes.Contains(out, []byte("unsupported"))
2524
2525 if cacheOK {
2526 s := "false"
2527 if supported {
2528 s = "true"
2529 }
2530 cache.PutBytes(cache.Default(), flagID, []byte(s))
2531 }
2532
2533 b.flagCache[key] = supported
2534 return supported
2535 }
2536
2537
2538 func statString(info os.FileInfo) string {
2539 return fmt.Sprintf("stat %d %x %v %v\n", info.Size(), uint64(info.Mode()), info.ModTime(), info.IsDir())
2540 }
2541
2542
2543
2544
2545
2546
2547 func (b *Builder) gccCompilerID(compiler string) (id cache.ActionID, ok bool) {
2548
2549
2550
2551 sh := b.BackgroundShell()
2552
2553 if cfg.BuildN {
2554 sh.ShowCmd(b.WorkDir, "%s || true", joinUnambiguously([]string{compiler, "--version"}))
2555 return cache.ActionID{}, false
2556 }
2557
2558 b.exec.Lock()
2559 defer b.exec.Unlock()
2560
2561 if id, ok := b.gccCompilerIDCache[compiler]; ok {
2562 return id, ok
2563 }
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579 exe, err := cfg.LookPath(compiler)
2580 if err != nil {
2581 return cache.ActionID{}, false
2582 }
2583
2584 h := cache.NewHash("gccCompilerID")
2585 fmt.Fprintf(h, "gccCompilerID %q", exe)
2586 key := h.Sum()
2587 data, _, err := cache.GetBytes(cache.Default(), key)
2588 if err == nil && len(data) > len(id) {
2589 stats := strings.Split(string(data[:len(data)-len(id)]), "\x00")
2590 if len(stats)%2 != 0 {
2591 goto Miss
2592 }
2593 for i := 0; i+2 <= len(stats); i++ {
2594 info, err := os.Stat(stats[i])
2595 if err != nil || statString(info) != stats[i+1] {
2596 goto Miss
2597 }
2598 }
2599 copy(id[:], data[len(data)-len(id):])
2600 return id, true
2601 Miss:
2602 }
2603
2604
2605
2606
2607
2608
2609 toolID, exe2, err := b.gccToolID(compiler, "c")
2610 if err != nil {
2611 return cache.ActionID{}, false
2612 }
2613
2614 exes := []string{exe, exe2}
2615 str.Uniq(&exes)
2616 fmt.Fprintf(h, "gccCompilerID %q %q\n", exes, toolID)
2617 id = h.Sum()
2618
2619 var buf bytes.Buffer
2620 for _, exe := range exes {
2621 if exe == "" {
2622 continue
2623 }
2624 info, err := os.Stat(exe)
2625 if err != nil {
2626 return cache.ActionID{}, false
2627 }
2628 buf.WriteString(exe)
2629 buf.WriteString("\x00")
2630 buf.WriteString(statString(info))
2631 buf.WriteString("\x00")
2632 }
2633 buf.Write(id[:])
2634
2635 cache.PutBytes(cache.Default(), key, buf.Bytes())
2636 if b.gccCompilerIDCache == nil {
2637 b.gccCompilerIDCache = make(map[string]cache.ActionID)
2638 }
2639 b.gccCompilerIDCache[compiler] = id
2640 return id, true
2641 }
2642
2643
2644 func (b *Builder) gccArchArgs() []string {
2645 switch cfg.Goarch {
2646 case "386":
2647 return []string{"-m32"}
2648 case "amd64":
2649 if cfg.Goos == "darwin" {
2650 return []string{"-arch", "x86_64", "-m64"}
2651 }
2652 return []string{"-m64"}
2653 case "arm64":
2654 if cfg.Goos == "darwin" {
2655 return []string{"-arch", "arm64"}
2656 }
2657 case "arm":
2658 return []string{"-marm"}
2659 case "s390x":
2660 return []string{"-m64", "-march=z196"}
2661 case "mips64", "mips64le":
2662 args := []string{"-mabi=64"}
2663 if cfg.GOMIPS64 == "hardfloat" {
2664 return append(args, "-mhard-float")
2665 } else if cfg.GOMIPS64 == "softfloat" {
2666 return append(args, "-msoft-float")
2667 }
2668 case "mips", "mipsle":
2669 args := []string{"-mabi=32", "-march=mips32"}
2670 if cfg.GOMIPS == "hardfloat" {
2671 return append(args, "-mhard-float", "-mfp32", "-mno-odd-spreg")
2672 } else if cfg.GOMIPS == "softfloat" {
2673 return append(args, "-msoft-float")
2674 }
2675 case "loong64":
2676 return []string{"-mabi=lp64d"}
2677 case "ppc64":
2678 if cfg.Goos == "aix" {
2679 return []string{"-maix64"}
2680 }
2681 }
2682 return nil
2683 }
2684
2685
2686
2687
2688
2689
2690
2691 func envList(key, def string) []string {
2692 v := cfg.Getenv(key)
2693 if v == "" {
2694 v = def
2695 }
2696 args, err := quoted.Split(v)
2697 if err != nil {
2698 panic(fmt.Sprintf("could not parse environment variable %s with value %q: %v", key, v, err))
2699 }
2700 return args
2701 }
2702
2703
2704 func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, ldflags []string, err error) {
2705 if cppflags, err = buildFlags("CPPFLAGS", "", p.CgoCPPFLAGS, checkCompilerFlags); err != nil {
2706 return
2707 }
2708 if cflags, err = buildFlags("CFLAGS", DefaultCFlags, p.CgoCFLAGS, checkCompilerFlags); err != nil {
2709 return
2710 }
2711 if cxxflags, err = buildFlags("CXXFLAGS", DefaultCFlags, p.CgoCXXFLAGS, checkCompilerFlags); err != nil {
2712 return
2713 }
2714 if fflags, err = buildFlags("FFLAGS", DefaultCFlags, p.CgoFFLAGS, checkCompilerFlags); err != nil {
2715 return
2716 }
2717 if ldflags, err = buildFlags("LDFLAGS", DefaultCFlags, p.CgoLDFLAGS, checkLinkerFlags); err != nil {
2718 return
2719 }
2720
2721 return
2722 }
2723
2724 func buildFlags(name, defaults string, fromPackage []string, check func(string, string, []string) error) ([]string, error) {
2725 if err := check(name, "#cgo "+name, fromPackage); err != nil {
2726 return nil, err
2727 }
2728 return str.StringList(envList("CGO_"+name, defaults), fromPackage), nil
2729 }
2730
2731 var cgoRe = lazyregexp.New(`[/\\:]`)
2732
2733 func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) {
2734 p := a.Package
2735 sh := b.Shell(a)
2736
2737 cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS, err := b.CFlags(p)
2738 if err != nil {
2739 return nil, nil, err
2740 }
2741
2742 cgoCPPFLAGS = append(cgoCPPFLAGS, pcCFLAGS...)
2743 cgoLDFLAGS = append(cgoLDFLAGS, pcLDFLAGS...)
2744
2745 if len(mfiles) > 0 {
2746 cgoLDFLAGS = append(cgoLDFLAGS, "-lobjc")
2747 }
2748
2749
2750
2751
2752 if len(ffiles) > 0 {
2753 fc := cfg.Getenv("FC")
2754 if fc == "" {
2755 fc = "gfortran"
2756 }
2757 if strings.Contains(fc, "gfortran") {
2758 cgoLDFLAGS = append(cgoLDFLAGS, "-lgfortran")
2759 }
2760 }
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777 flagSources := []string{"CGO_CFLAGS", "CGO_CXXFLAGS", "CGO_FFLAGS"}
2778 flagLists := [][]string{cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS}
2779 if flagsNotCompatibleWithInternalLinking(flagSources, flagLists) {
2780 tokenFile := objdir + "preferlinkext"
2781 if err := sh.writeFile(tokenFile, nil); err != nil {
2782 return nil, nil, err
2783 }
2784 outObj = append(outObj, tokenFile)
2785 }
2786
2787 if cfg.BuildMSan {
2788 cgoCFLAGS = append([]string{"-fsanitize=memory"}, cgoCFLAGS...)
2789 cgoLDFLAGS = append([]string{"-fsanitize=memory"}, cgoLDFLAGS...)
2790 }
2791 if cfg.BuildASan {
2792 cgoCFLAGS = append([]string{"-fsanitize=address"}, cgoCFLAGS...)
2793 cgoLDFLAGS = append([]string{"-fsanitize=address"}, cgoLDFLAGS...)
2794 }
2795
2796
2797
2798 cgoCPPFLAGS = append(cgoCPPFLAGS, "-I", objdir)
2799
2800
2801
2802 gofiles := []string{objdir + "_cgo_gotypes.go"}
2803 cfiles := []string{"_cgo_export.c"}
2804 for _, fn := range cgofiles {
2805 f := strings.TrimSuffix(filepath.Base(fn), ".go")
2806 gofiles = append(gofiles, objdir+f+".cgo1.go")
2807 cfiles = append(cfiles, f+".cgo2.c")
2808 }
2809
2810
2811
2812 cgoflags := []string{}
2813 if p.Standard && p.ImportPath == "runtime/cgo" {
2814 cgoflags = append(cgoflags, "-import_runtime_cgo=false")
2815 }
2816 if p.Standard && (p.ImportPath == "runtime/race" || p.ImportPath == "runtime/msan" || p.ImportPath == "runtime/cgo" || p.ImportPath == "runtime/asan") {
2817 cgoflags = append(cgoflags, "-import_syscall=false")
2818 }
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830 cgoenv := b.cCompilerEnv()
2831 var ldflagsOption []string
2832 if len(cgoLDFLAGS) > 0 {
2833 flags := make([]string, len(cgoLDFLAGS))
2834 for i, f := range cgoLDFLAGS {
2835 flags[i] = strconv.Quote(f)
2836 }
2837 ldflagsOption = []string{"-ldflags=" + strings.Join(flags, " ")}
2838
2839
2840 cgoenv = append(cgoenv, "CGO_LDFLAGS=")
2841 }
2842
2843 if cfg.BuildToolchainName == "gccgo" {
2844 if b.gccSupportsFlag([]string{BuildToolchain.compiler()}, "-fsplit-stack") {
2845 cgoCFLAGS = append(cgoCFLAGS, "-fsplit-stack")
2846 }
2847 cgoflags = append(cgoflags, "-gccgo")
2848 if pkgpath := gccgoPkgpath(p); pkgpath != "" {
2849 cgoflags = append(cgoflags, "-gccgopkgpath="+pkgpath)
2850 }
2851 if !BuildToolchain.(gccgoToolchain).supportsCgoIncomplete(b, a) {
2852 cgoflags = append(cgoflags, "-gccgo_define_cgoincomplete")
2853 }
2854 }
2855
2856 switch cfg.BuildBuildmode {
2857 case "c-archive", "c-shared":
2858
2859
2860
2861 cgoflags = append(cgoflags, "-exportheader="+objdir+"_cgo_install.h")
2862 }
2863
2864
2865
2866 var trimpath []string
2867 for i := range cgofiles {
2868 path := mkAbs(p.Dir, cgofiles[i])
2869 if opath, ok := fsys.OverlayPath(path); ok {
2870 cgofiles[i] = opath
2871 trimpath = append(trimpath, opath+"=>"+path)
2872 }
2873 }
2874 if len(trimpath) > 0 {
2875 cgoflags = append(cgoflags, "-trimpath", strings.Join(trimpath, ";"))
2876 }
2877
2878 if err := sh.run(p.Dir, p.ImportPath, cgoenv, cfg.BuildToolexec, cgoExe, "-objdir", objdir, "-importpath", p.ImportPath, cgoflags, ldflagsOption, "--", cgoCPPFLAGS, cgoCFLAGS, cgofiles); err != nil {
2879 return nil, nil, err
2880 }
2881 outGo = append(outGo, gofiles...)
2882
2883
2884
2885
2886
2887
2888
2889 oseq := 0
2890 nextOfile := func() string {
2891 oseq++
2892 return objdir + fmt.Sprintf("_x%03d.o", oseq)
2893 }
2894
2895
2896 cflags := str.StringList(cgoCPPFLAGS, cgoCFLAGS)
2897 for _, cfile := range cfiles {
2898 ofile := nextOfile()
2899 if err := b.gcc(a, a.Objdir, ofile, cflags, objdir+cfile); err != nil {
2900 return nil, nil, err
2901 }
2902 outObj = append(outObj, ofile)
2903 }
2904
2905 for _, file := range gccfiles {
2906 ofile := nextOfile()
2907 if err := b.gcc(a, a.Objdir, ofile, cflags, file); err != nil {
2908 return nil, nil, err
2909 }
2910 outObj = append(outObj, ofile)
2911 }
2912
2913 cxxflags := str.StringList(cgoCPPFLAGS, cgoCXXFLAGS)
2914 for _, file := range gxxfiles {
2915 ofile := nextOfile()
2916 if err := b.gxx(a, a.Objdir, ofile, cxxflags, file); err != nil {
2917 return nil, nil, err
2918 }
2919 outObj = append(outObj, ofile)
2920 }
2921
2922 for _, file := range mfiles {
2923 ofile := nextOfile()
2924 if err := b.gcc(a, a.Objdir, ofile, cflags, file); err != nil {
2925 return nil, nil, err
2926 }
2927 outObj = append(outObj, ofile)
2928 }
2929
2930 fflags := str.StringList(cgoCPPFLAGS, cgoFFLAGS)
2931 for _, file := range ffiles {
2932 ofile := nextOfile()
2933 if err := b.gfortran(a, a.Objdir, ofile, fflags, file); err != nil {
2934 return nil, nil, err
2935 }
2936 outObj = append(outObj, ofile)
2937 }
2938
2939 switch cfg.BuildToolchainName {
2940 case "gc":
2941 importGo := objdir + "_cgo_import.go"
2942 dynOutGo, dynOutObj, err := b.dynimport(a, objdir, importGo, cgoExe, cflags, cgoLDFLAGS, outObj)
2943 if err != nil {
2944 return nil, nil, err
2945 }
2946 if dynOutGo != "" {
2947 outGo = append(outGo, dynOutGo)
2948 }
2949 if dynOutObj != "" {
2950 outObj = append(outObj, dynOutObj)
2951 }
2952
2953 case "gccgo":
2954 defunC := objdir + "_cgo_defun.c"
2955 defunObj := objdir + "_cgo_defun.o"
2956 if err := BuildToolchain.cc(b, a, defunObj, defunC); err != nil {
2957 return nil, nil, err
2958 }
2959 outObj = append(outObj, defunObj)
2960
2961 default:
2962 noCompiler()
2963 }
2964
2965
2966
2967
2968
2969
2970 if cfg.BuildToolchainName == "gc" && !cfg.BuildN {
2971 var flags []string
2972 for _, f := range outGo {
2973 if !strings.HasPrefix(filepath.Base(f), "_cgo_") {
2974 continue
2975 }
2976
2977 src, err := os.ReadFile(f)
2978 if err != nil {
2979 return nil, nil, err
2980 }
2981
2982 const cgoLdflag = "//go:cgo_ldflag"
2983 idx := bytes.Index(src, []byte(cgoLdflag))
2984 for idx >= 0 {
2985
2986
2987 start := bytes.LastIndex(src[:idx], []byte("\n"))
2988 if start == -1 {
2989 start = 0
2990 }
2991
2992
2993 end := bytes.Index(src[idx:], []byte("\n"))
2994 if end == -1 {
2995 end = len(src)
2996 } else {
2997 end += idx
2998 }
2999
3000
3001
3002
3003
3004 commentStart := bytes.Index(src[start:], []byte("//"))
3005 commentStart += start
3006
3007
3008 if bytes.HasPrefix(src[commentStart:], []byte(cgoLdflag)) {
3009
3010
3011 flag := string(src[idx+len(cgoLdflag) : end])
3012 flag = strings.TrimSpace(flag)
3013 flag = strings.Trim(flag, `"`)
3014 flags = append(flags, flag)
3015 }
3016 src = src[end:]
3017 idx = bytes.Index(src, []byte(cgoLdflag))
3018 }
3019 }
3020
3021
3022 if len(cgoLDFLAGS) > 0 {
3023 outer:
3024 for i := range flags {
3025 for j, f := range cgoLDFLAGS {
3026 if f != flags[i+j] {
3027 continue outer
3028 }
3029 }
3030 flags = append(flags[:i], flags[i+len(cgoLDFLAGS):]...)
3031 break
3032 }
3033 }
3034
3035 if err := checkLinkerFlags("LDFLAGS", "go:cgo_ldflag", flags); err != nil {
3036 return nil, nil, err
3037 }
3038 }
3039
3040 return outGo, outObj, nil
3041 }
3042
3043
3044
3045
3046
3047
3048
3049
3050 func flagsNotCompatibleWithInternalLinking(sourceList []string, flagListList [][]string) bool {
3051 for i := range sourceList {
3052 sn := sourceList[i]
3053 fll := flagListList[i]
3054 if err := checkCompilerFlagsForInternalLink(sn, sn, fll); err != nil {
3055 return true
3056 }
3057 }
3058 return false
3059 }
3060
3061
3062
3063
3064
3065
3066 func (b *Builder) dynimport(a *Action, objdir, importGo, cgoExe string, cflags, cgoLDFLAGS, outObj []string) (dynOutGo, dynOutObj string, err error) {
3067 p := a.Package
3068 sh := b.Shell(a)
3069
3070 cfile := objdir + "_cgo_main.c"
3071 ofile := objdir + "_cgo_main.o"
3072 if err := b.gcc(a, objdir, ofile, cflags, cfile); err != nil {
3073 return "", "", err
3074 }
3075
3076
3077 var syso []string
3078 seen := make(map[*Action]bool)
3079 var gatherSyso func(*Action)
3080 gatherSyso = func(a1 *Action) {
3081 if seen[a1] {
3082 return
3083 }
3084 seen[a1] = true
3085 if p1 := a1.Package; p1 != nil {
3086 syso = append(syso, mkAbsFiles(p1.Dir, p1.SysoFiles)...)
3087 }
3088 for _, a2 := range a1.Deps {
3089 gatherSyso(a2)
3090 }
3091 }
3092 gatherSyso(a)
3093 sort.Strings(syso)
3094 str.Uniq(&syso)
3095 linkobj := str.StringList(ofile, outObj, syso)
3096 dynobj := objdir + "_cgo_.o"
3097
3098 ldflags := cgoLDFLAGS
3099 if (cfg.Goarch == "arm" && cfg.Goos == "linux") || cfg.Goos == "android" {
3100 if !slices.Contains(ldflags, "-no-pie") {
3101
3102
3103 ldflags = append(ldflags, "-pie")
3104 }
3105 if slices.Contains(ldflags, "-pie") && slices.Contains(ldflags, "-static") {
3106
3107
3108 n := make([]string, 0, len(ldflags)-1)
3109 for _, flag := range ldflags {
3110 if flag != "-static" {
3111 n = append(n, flag)
3112 }
3113 }
3114 ldflags = n
3115 }
3116 }
3117 if err := b.gccld(a, objdir, dynobj, ldflags, linkobj); err != nil {
3118
3119
3120
3121
3122
3123
3124 fail := objdir + "dynimportfail"
3125 if err := sh.writeFile(fail, nil); err != nil {
3126 return "", "", err
3127 }
3128 return "", fail, nil
3129 }
3130
3131
3132 var cgoflags []string
3133 if p.Standard && p.ImportPath == "runtime/cgo" {
3134 cgoflags = []string{"-dynlinker"}
3135 }
3136 err = sh.run(base.Cwd(), p.ImportPath, b.cCompilerEnv(), cfg.BuildToolexec, cgoExe, "-dynpackage", p.Name, "-dynimport", dynobj, "-dynout", importGo, cgoflags)
3137 if err != nil {
3138 return "", "", err
3139 }
3140 return importGo, "", nil
3141 }
3142
3143
3144
3145
3146 func (b *Builder) swig(a *Action, objdir string, pcCFLAGS []string) (outGo, outC, outCXX []string, err error) {
3147 p := a.Package
3148
3149 if err := b.swigVersionCheck(); err != nil {
3150 return nil, nil, nil, err
3151 }
3152
3153 intgosize, err := b.swigIntSize(objdir)
3154 if err != nil {
3155 return nil, nil, nil, err
3156 }
3157
3158 for _, f := range p.SwigFiles {
3159 goFile, cFile, err := b.swigOne(a, f, objdir, pcCFLAGS, false, intgosize)
3160 if err != nil {
3161 return nil, nil, nil, err
3162 }
3163 if goFile != "" {
3164 outGo = append(outGo, goFile)
3165 }
3166 if cFile != "" {
3167 outC = append(outC, cFile)
3168 }
3169 }
3170 for _, f := range p.SwigCXXFiles {
3171 goFile, cxxFile, err := b.swigOne(a, f, objdir, pcCFLAGS, true, intgosize)
3172 if err != nil {
3173 return nil, nil, nil, err
3174 }
3175 if goFile != "" {
3176 outGo = append(outGo, goFile)
3177 }
3178 if cxxFile != "" {
3179 outCXX = append(outCXX, cxxFile)
3180 }
3181 }
3182 return outGo, outC, outCXX, nil
3183 }
3184
3185
3186 var (
3187 swigCheckOnce sync.Once
3188 swigCheck error
3189 )
3190
3191 func (b *Builder) swigDoVersionCheck() error {
3192 sh := b.BackgroundShell()
3193 out, err := sh.runOut(".", nil, "swig", "-version")
3194 if err != nil {
3195 return err
3196 }
3197 re := regexp.MustCompile(`[vV]ersion +(\d+)([.]\d+)?([.]\d+)?`)
3198 matches := re.FindSubmatch(out)
3199 if matches == nil {
3200
3201 return nil
3202 }
3203
3204 major, err := strconv.Atoi(string(matches[1]))
3205 if err != nil {
3206
3207 return nil
3208 }
3209 const errmsg = "must have SWIG version >= 3.0.6"
3210 if major < 3 {
3211 return errors.New(errmsg)
3212 }
3213 if major > 3 {
3214
3215 return nil
3216 }
3217
3218
3219 if len(matches[2]) > 0 {
3220 minor, err := strconv.Atoi(string(matches[2][1:]))
3221 if err != nil {
3222 return nil
3223 }
3224 if minor > 0 {
3225
3226 return nil
3227 }
3228 }
3229
3230
3231 if len(matches[3]) > 0 {
3232 patch, err := strconv.Atoi(string(matches[3][1:]))
3233 if err != nil {
3234 return nil
3235 }
3236 if patch < 6 {
3237
3238 return errors.New(errmsg)
3239 }
3240 }
3241
3242 return nil
3243 }
3244
3245 func (b *Builder) swigVersionCheck() error {
3246 swigCheckOnce.Do(func() {
3247 swigCheck = b.swigDoVersionCheck()
3248 })
3249 return swigCheck
3250 }
3251
3252
3253 var (
3254 swigIntSizeOnce sync.Once
3255 swigIntSize string
3256 swigIntSizeError error
3257 )
3258
3259
3260 const swigIntSizeCode = `
3261 package main
3262 const i int = 1 << 32
3263 `
3264
3265
3266
3267 func (b *Builder) swigDoIntSize(objdir string) (intsize string, err error) {
3268 if cfg.BuildN {
3269 return "$INTBITS", nil
3270 }
3271 src := filepath.Join(b.WorkDir, "swig_intsize.go")
3272 if err = os.WriteFile(src, []byte(swigIntSizeCode), 0666); err != nil {
3273 return
3274 }
3275 srcs := []string{src}
3276
3277 p := load.GoFilesPackage(context.TODO(), load.PackageOpts{}, srcs)
3278
3279 if _, _, e := BuildToolchain.gc(b, &Action{Mode: "swigDoIntSize", Package: p, Objdir: objdir}, "", nil, nil, "", false, "", srcs); e != nil {
3280 return "32", nil
3281 }
3282 return "64", nil
3283 }
3284
3285
3286
3287 func (b *Builder) swigIntSize(objdir string) (intsize string, err error) {
3288 swigIntSizeOnce.Do(func() {
3289 swigIntSize, swigIntSizeError = b.swigDoIntSize(objdir)
3290 })
3291 return swigIntSize, swigIntSizeError
3292 }
3293
3294
3295 func (b *Builder) swigOne(a *Action, file, objdir string, pcCFLAGS []string, cxx bool, intgosize string) (outGo, outC string, err error) {
3296 p := a.Package
3297 sh := b.Shell(a)
3298
3299 cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p)
3300 if err != nil {
3301 return "", "", err
3302 }
3303
3304 var cflags []string
3305 if cxx {
3306 cflags = str.StringList(cgoCPPFLAGS, pcCFLAGS, cgoCXXFLAGS)
3307 } else {
3308 cflags = str.StringList(cgoCPPFLAGS, pcCFLAGS, cgoCFLAGS)
3309 }
3310
3311 n := 5
3312 if cxx {
3313 n = 8
3314 }
3315 base := file[:len(file)-n]
3316 goFile := base + ".go"
3317 gccBase := base + "_wrap."
3318 gccExt := "c"
3319 if cxx {
3320 gccExt = "cxx"
3321 }
3322
3323 gccgo := cfg.BuildToolchainName == "gccgo"
3324
3325
3326 args := []string{
3327 "-go",
3328 "-cgo",
3329 "-intgosize", intgosize,
3330 "-module", base,
3331 "-o", objdir + gccBase + gccExt,
3332 "-outdir", objdir,
3333 }
3334
3335 for _, f := range cflags {
3336 if len(f) > 3 && f[:2] == "-I" {
3337 args = append(args, f)
3338 }
3339 }
3340
3341 if gccgo {
3342 args = append(args, "-gccgo")
3343 if pkgpath := gccgoPkgpath(p); pkgpath != "" {
3344 args = append(args, "-go-pkgpath", pkgpath)
3345 }
3346 }
3347 if cxx {
3348 args = append(args, "-c++")
3349 }
3350
3351 out, err := sh.runOut(p.Dir, nil, "swig", args, file)
3352 if err != nil && (bytes.Contains(out, []byte("-intgosize")) || bytes.Contains(out, []byte("-cgo"))) {
3353 return "", "", errors.New("must have SWIG version >= 3.0.6")
3354 }
3355 if err := sh.reportCmd("", "", out, err); err != nil {
3356 return "", "", err
3357 }
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367 goFile = objdir + goFile
3368 newGoFile := objdir + "_" + base + "_swig.go"
3369 if cfg.BuildX || cfg.BuildN {
3370 sh.ShowCmd("", "mv %s %s", goFile, newGoFile)
3371 }
3372 if !cfg.BuildN {
3373 if err := os.Rename(goFile, newGoFile); err != nil {
3374 return "", "", err
3375 }
3376 }
3377 return newGoFile, objdir + gccBase + gccExt, nil
3378 }
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390 func (b *Builder) disableBuildID(ldflags []string) []string {
3391 switch cfg.Goos {
3392 case "android", "dragonfly", "linux", "netbsd":
3393 ldflags = append(ldflags, "-Wl,--build-id=none")
3394 }
3395 return ldflags
3396 }
3397
3398
3399
3400
3401 func mkAbsFiles(dir string, files []string) []string {
3402 abs := make([]string, len(files))
3403 for i, f := range files {
3404 if !filepath.IsAbs(f) {
3405 f = filepath.Join(dir, f)
3406 }
3407 abs[i] = f
3408 }
3409 return abs
3410 }
3411
3412
3413
3414
3415
3416
3417
3418
3419 func passLongArgsInResponseFiles(cmd *exec.Cmd) (cleanup func()) {
3420 cleanup = func() {}
3421
3422 var argLen int
3423 for _, arg := range cmd.Args {
3424 argLen += len(arg)
3425 }
3426
3427
3428
3429 if !useResponseFile(cmd.Path, argLen) {
3430 return
3431 }
3432
3433 tf, err := os.CreateTemp("", "args")
3434 if err != nil {
3435 log.Fatalf("error writing long arguments to response file: %v", err)
3436 }
3437 cleanup = func() { os.Remove(tf.Name()) }
3438 var buf bytes.Buffer
3439 for _, arg := range cmd.Args[1:] {
3440 fmt.Fprintf(&buf, "%s\n", encodeArg(arg))
3441 }
3442 if _, err := tf.Write(buf.Bytes()); err != nil {
3443 tf.Close()
3444 cleanup()
3445 log.Fatalf("error writing long arguments to response file: %v", err)
3446 }
3447 if err := tf.Close(); err != nil {
3448 cleanup()
3449 log.Fatalf("error writing long arguments to response file: %v", err)
3450 }
3451 cmd.Args = []string{cmd.Args[0], "@" + tf.Name()}
3452 return cleanup
3453 }
3454
3455 func useResponseFile(path string, argLen int) bool {
3456
3457
3458
3459 prog := strings.TrimSuffix(filepath.Base(path), ".exe")
3460 switch prog {
3461 case "compile", "link", "cgo", "asm", "cover":
3462 default:
3463 return false
3464 }
3465
3466 if argLen > sys.ExecArgLengthLimit {
3467 return true
3468 }
3469
3470
3471
3472 isBuilder := os.Getenv("GO_BUILDER_NAME") != ""
3473 if isBuilder && rand.Intn(10) == 0 {
3474 return true
3475 }
3476
3477 return false
3478 }
3479
3480
3481 func encodeArg(arg string) string {
3482
3483 if !strings.ContainsAny(arg, "\\\n") {
3484 return arg
3485 }
3486 var b strings.Builder
3487 for _, r := range arg {
3488 switch r {
3489 case '\\':
3490 b.WriteByte('\\')
3491 b.WriteByte('\\')
3492 case '\n':
3493 b.WriteByte('\\')
3494 b.WriteByte('n')
3495 default:
3496 b.WriteRune(r)
3497 }
3498 }
3499 return b.String()
3500 }
3501
View as plain text