Source file src/cmd/go/internal/work/exec.go

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Action graph execution.
     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  // actionList returns the list of actions in the dag rooted at root
    53  // as visited in a depth-first post-order traversal.
    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  // Do runs the action graph rooted at root.
    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  		// If we're doing real work, take time at the end to trim the cache.
    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  	// Build list of all actions, assigning depth-first post-order priority.
    88  	// The original implementation here was a true queue
    89  	// (using a channel) but it had the effect of getting
    90  	// distracted by low-level leaf actions to the detriment
    91  	// of completing higher-level actions. The order of
    92  	// work does not matter much to overall execution time,
    93  	// but when running "go test std" it is nice to see each test
    94  	// results as soon as possible. The priorities assigned
    95  	// ensure that, all else being equal, the execution prefers
    96  	// to do what it would have done first in a simple depth-first
    97  	// dependency order traversal.
    98  	all := actionList(root)
    99  	for i, a := range all {
   100  		a.priority = i
   101  	}
   102  
   103  	// Write action graph, without timing information, in case we fail and exit early.
   104  	writeActionGraph := func() {
   105  		if file := cfg.DebugActiongraph; file != "" {
   106  			if strings.HasSuffix(file, ".go") {
   107  				// Do not overwrite Go source code in:
   108  				//	go build -debug-actiongraph x.go
   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  	// Initialize per-action execution state.
   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  	// Handle runs a single action and takes care of triggering
   135  	// any actions that are runnable as a result.
   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  			// TODO(matloob): Better action descriptions
   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  		// The actions run in parallel but all the updates to the
   161  		// shared work state are serialized through b.exec.
   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  				if a.Package != nil {
   173  					if ipe, ok := errors.AsType[load.ImportPathError](err); !ok || ipe.ImportPath() != a.Package.ImportPath {
   174  						err = fmt.Errorf("%s: %v", a.Package.ImportPath, err)
   175  					}
   176  				}
   177  				sh := b.Shell(a)
   178  				sh.Errorf("%s", err)
   179  			}
   180  			if a.Failed == nil {
   181  				a.Failed = a
   182  			}
   183  		}
   184  
   185  		for _, a0 := range a.triggers {
   186  			if a.Failed != nil {
   187  				if a0.Mode == "test barrier" {
   188  					// If this action was triggered by a test, there
   189  					// will be a test barrier action in between the test
   190  					// and the true trigger. But there will be other
   191  					// triggers that are other barriers that are waiting
   192  					// for this one. Propagate the failure to the true
   193  					// trigger, but not to the other barriers.
   194  					for _, bt := range a0.triggers {
   195  						if bt.Mode != "test barrier" {
   196  							bt.Failed = a.Failed
   197  						}
   198  					}
   199  				} else {
   200  					a0.Failed = a.Failed
   201  				}
   202  			}
   203  			if a0.pending--; a0.pending == 0 {
   204  				b.ready.push(a0)
   205  				b.readySema <- true
   206  			}
   207  		}
   208  
   209  		if a == root {
   210  			close(b.readySema)
   211  		}
   212  	}
   213  
   214  	var wg sync.WaitGroup
   215  
   216  	// Kick off goroutines according to parallelism.
   217  	// If we are using the -n flag (just printing commands)
   218  	// drop the parallelism to 1, both to make the output
   219  	// deterministic and because there is no real work anyway.
   220  	par := cfg.BuildP
   221  	if cfg.BuildN {
   222  		par = 1
   223  	}
   224  	for i := 0; i < par; i++ {
   225  		wg.Add(1)
   226  		go func() {
   227  			ctx := trace.StartGoroutine(ctx)
   228  			defer wg.Done()
   229  			for {
   230  				select {
   231  				case _, ok := <-b.readySema:
   232  					if !ok {
   233  						return
   234  					}
   235  					// Receiving a value from b.readySema entitles
   236  					// us to take from the ready queue.
   237  					b.exec.Lock()
   238  					a := b.ready.pop()
   239  					b.exec.Unlock()
   240  					handle(ctx, a)
   241  				case <-base.Interrupted:
   242  					base.SetExitStatus(1)
   243  					return
   244  				}
   245  			}
   246  		}()
   247  	}
   248  
   249  	wg.Wait()
   250  
   251  	// Write action graph again, this time with timing information.
   252  	writeActionGraph()
   253  }
   254  
   255  // buildActionID computes the action ID for a build action.
   256  func (b *Builder) buildActionID(a *Action) cache.ActionID {
   257  	p := a.Package
   258  	h := cache.NewHash("build " + p.ImportPath)
   259  
   260  	// Configuration independent of compiler toolchain.
   261  	// Note: buildmode has already been accounted for in buildGcflags
   262  	// and should not be inserted explicitly. Most buildmodes use the
   263  	// same compiler settings and can reuse each other's results.
   264  	// If not, the reason is already recorded in buildGcflags.
   265  	fmt.Fprintf(h, "compile\n")
   266  
   267  	// Include information about the origin of the package that
   268  	// may be embedded in the debug info for the object file.
   269  	if cfg.BuildTrimpath {
   270  		// When -trimpath is used with a package built from the module cache,
   271  		// its debug information refers to the module path and version
   272  		// instead of the directory.
   273  		if p.Module != nil {
   274  			fmt.Fprintf(h, "module %s@%s\n", p.Module.Path, p.Module.Version)
   275  		}
   276  	} else if p.Goroot {
   277  		// The Go compiler always hides the exact value of $GOROOT
   278  		// when building things in GOROOT.
   279  		//
   280  		// The C compiler does not, but for packages in GOROOT we rewrite the path
   281  		// as though -trimpath were set. This used to be so that we did not invalidate
   282  		// the build cache (and especially precompiled archive files) when changing
   283  		// GOROOT_FINAL, but we no longer ship precompiled archive files as of Go 1.20
   284  		// (https://go.dev/issue/47257) and no longer support GOROOT_FINAL
   285  		// (https://go.dev/issue/62047).
   286  		// TODO(bcmills): Figure out whether this behavior is still useful.
   287  		//
   288  		// b.WorkDir is always either trimmed or rewritten to
   289  		// the literal string "/tmp/go-build".
   290  	} else if !strings.HasPrefix(p.Dir, b.WorkDir) {
   291  		// -trimpath is not set and no other rewrite rules apply,
   292  		// so the object file may refer to the absolute directory
   293  		// containing the package.
   294  		fmt.Fprintf(h, "dir %s\n", p.Dir)
   295  	}
   296  
   297  	if p.Module != nil {
   298  		fmt.Fprintf(h, "go %s\n", p.Module.GoVersion)
   299  	}
   300  	fmt.Fprintf(h, "goos %s goarch %s\n", cfg.Goos, cfg.Goarch)
   301  	fmt.Fprintf(h, "import %q\n", p.ImportPath)
   302  	fmt.Fprintf(h, "omitdebug %v standard %v local %v prefix %q\n", p.Internal.OmitDebug, p.Standard, p.Internal.Local, p.Internal.LocalPrefix)
   303  	if cfg.BuildTrimpath {
   304  		fmt.Fprintln(h, "trimpath")
   305  	}
   306  	if p.Internal.ForceLibrary {
   307  		fmt.Fprintf(h, "forcelibrary\n")
   308  	}
   309  	if len(p.CgoFiles)+len(p.SwigFiles)+len(p.SwigCXXFiles) > 0 {
   310  		fmt.Fprintf(h, "cgo %q\n", b.toolID("cgo"))
   311  		cppflags, cflags, cxxflags, fflags, ldflags, _ := b.CFlags(p)
   312  
   313  		ccExe := b.ccExe()
   314  		fmt.Fprintf(h, "CC=%q %q %q %q\n", ccExe, cppflags, cflags, ldflags)
   315  		// Include the C compiler tool ID so that if the C
   316  		// compiler changes we rebuild the package.
   317  		if ccID, _, err := b.gccToolID(ccExe[0], "c"); err == nil {
   318  			fmt.Fprintf(h, "CC ID=%q\n", ccID)
   319  		} else {
   320  			fmt.Fprintf(h, "CC ID ERROR=%q\n", err)
   321  		}
   322  		if len(p.CXXFiles)+len(p.SwigCXXFiles) > 0 {
   323  			cxxExe := b.cxxExe()
   324  			fmt.Fprintf(h, "CXX=%q %q\n", cxxExe, cxxflags)
   325  			if cxxID, _, err := b.gccToolID(cxxExe[0], "c++"); err == nil {
   326  				fmt.Fprintf(h, "CXX ID=%q\n", cxxID)
   327  			} else {
   328  				fmt.Fprintf(h, "CXX ID ERROR=%q\n", err)
   329  			}
   330  		}
   331  		if len(p.FFiles) > 0 {
   332  			fcExe := b.fcExe()
   333  			fmt.Fprintf(h, "FC=%q %q\n", fcExe, fflags)
   334  			if fcID, _, err := b.gccToolID(fcExe[0], "f95"); err == nil {
   335  				fmt.Fprintf(h, "FC ID=%q\n", fcID)
   336  			} else {
   337  				fmt.Fprintf(h, "FC ID ERROR=%q\n", err)
   338  			}
   339  		}
   340  		// TODO(rsc): Should we include the SWIG version?
   341  	}
   342  	if p.Internal.Cover.Mode != "" {
   343  		fmt.Fprintf(h, "cover %q %q\n", p.Internal.Cover.Mode, b.toolID("cover"))
   344  	}
   345  	if p.Internal.FuzzInstrument {
   346  		if fuzzFlags := fuzzInstrumentFlags(); fuzzFlags != nil {
   347  			fmt.Fprintf(h, "fuzz %q\n", fuzzFlags)
   348  		}
   349  	}
   350  	if p.Internal.BuildInfo != nil {
   351  		fmt.Fprintf(h, "modinfo %q\n", p.Internal.BuildInfo.String())
   352  	}
   353  
   354  	// Configuration specific to compiler toolchain.
   355  	switch cfg.BuildToolchainName {
   356  	default:
   357  		base.Fatalf("buildActionID: unknown build toolchain %q", cfg.BuildToolchainName)
   358  	case "gc":
   359  		fmt.Fprintf(h, "compile %s %q %q\n", b.toolID("compile"), forcedGcflags, p.Internal.Gcflags)
   360  		if len(p.SFiles) > 0 {
   361  			fmt.Fprintf(h, "asm %q %q %q\n", b.toolID("asm"), forcedAsmflags, p.Internal.Asmflags)
   362  		}
   363  
   364  		// GOARM, GOMIPS, etc.
   365  		key, val, _ := cfg.GetArchEnv()
   366  		fmt.Fprintf(h, "%s=%s\n", key, val)
   367  
   368  		if cfg.CleanGOEXPERIMENT != "" {
   369  			fmt.Fprintf(h, "GOEXPERIMENT=%q\n", cfg.CleanGOEXPERIMENT)
   370  		}
   371  
   372  		// TODO(rsc): Convince compiler team not to add more magic environment variables,
   373  		// or perhaps restrict the environment variables passed to subprocesses.
   374  		// Because these are clumsy, undocumented special-case hacks
   375  		// for debugging the compiler, they are not settable using 'go env -w',
   376  		// and so here we use os.Getenv, not cfg.Getenv.
   377  		magic := []string{
   378  			"GOCLOBBERDEADHASH",
   379  			"GOSSAFUNC",
   380  			"GOSSADIR",
   381  			"GOCOMPILEDEBUG",
   382  		}
   383  		for _, env := range magic {
   384  			if x := os.Getenv(env); x != "" {
   385  				fmt.Fprintf(h, "magic %s=%s\n", env, x)
   386  			}
   387  		}
   388  
   389  	case "gccgo":
   390  		id, _, err := b.gccToolID(BuildToolchain.compiler(), "go")
   391  		if err != nil {
   392  			base.Fatalf("%v", err)
   393  		}
   394  		fmt.Fprintf(h, "compile %s %q %q\n", id, forcedGccgoflags, p.Internal.Gccgoflags)
   395  		fmt.Fprintf(h, "pkgpath %s\n", gccgoPkgpath(p))
   396  		fmt.Fprintf(h, "ar %q\n", BuildToolchain.(gccgoToolchain).ar())
   397  		if len(p.SFiles) > 0 {
   398  			id, _, _ = b.gccToolID(BuildToolchain.compiler(), "assembler-with-cpp")
   399  			// Ignore error; different assembler versions
   400  			// are unlikely to make any difference anyhow.
   401  			fmt.Fprintf(h, "asm %q\n", id)
   402  		}
   403  	}
   404  
   405  	// Input files.
   406  	inputFiles := str.StringList(
   407  		p.GoFiles,
   408  		p.CgoFiles,
   409  		p.CFiles,
   410  		p.CXXFiles,
   411  		p.FFiles,
   412  		p.MFiles,
   413  		p.HFiles,
   414  		p.SFiles,
   415  		p.SysoFiles,
   416  		p.SwigFiles,
   417  		p.SwigCXXFiles,
   418  		p.EmbedFiles,
   419  	)
   420  	for _, file := range inputFiles {
   421  		fmt.Fprintf(h, "file %s %s\n", file, b.fileHash(filepath.Join(p.Dir, file)))
   422  	}
   423  	for _, a1 := range a.Deps {
   424  		p1 := a1.Package
   425  		if p1 != nil {
   426  			fmt.Fprintf(h, "import %s %s\n", p1.ImportPath, contentID(a1.buildID))
   427  		}
   428  		if a1.Mode == "preprocess PGO profile" {
   429  			fmt.Fprintf(h, "pgofile %s\n", b.fileHash(a1.built))
   430  		}
   431  	}
   432  
   433  	return h.Sum()
   434  }
   435  
   436  // needCgoHdr reports whether the actions triggered by this one
   437  // expect to be able to access the cgo-generated header file.
   438  func (b *Builder) needCgoHdr(a *Action) bool {
   439  	// If this build triggers a header install, run cgo to get the header.
   440  	if !b.IsCmdList && (a.Package.UsesCgo() || a.Package.UsesSwig()) && (cfg.BuildBuildmode == "c-archive" || cfg.BuildBuildmode == "c-shared") {
   441  		for _, t1 := range a.triggers {
   442  			if t1.Mode == "install header" {
   443  				return true
   444  			}
   445  		}
   446  		for _, t1 := range a.triggers {
   447  			for _, t2 := range t1.triggers {
   448  				if t2.Mode == "install header" {
   449  					return true
   450  				}
   451  			}
   452  		}
   453  	}
   454  	return false
   455  }
   456  
   457  // allowedVersion reports whether the version v is an allowed version of go
   458  // (one that we can compile).
   459  // v is known to be of the form "1.23".
   460  func allowedVersion(v string) bool {
   461  	// Special case: no requirement.
   462  	if v == "" {
   463  		return true
   464  	}
   465  	return gover.Compare(gover.Local(), v) >= 0
   466  }
   467  
   468  func (b *Builder) computeNonGoOverlay(a *Action, p *load.Package, sh *Shell, objdir string, nonGoFileLists [][]string) error {
   469  OverlayLoop:
   470  	for _, fs := range nonGoFileLists {
   471  		for _, f := range fs {
   472  			if fsys.Replaced(mkAbs(p.Dir, f)) {
   473  				a.nonGoOverlay = make(map[string]string)
   474  				break OverlayLoop
   475  			}
   476  		}
   477  	}
   478  	if a.nonGoOverlay != nil {
   479  		for _, fs := range nonGoFileLists {
   480  			for i := range fs {
   481  				from := mkAbs(p.Dir, fs[i])
   482  				dst := objdir + filepath.Base(fs[i])
   483  				if err := sh.CopyFile(dst, fsys.Actual(from), 0666, false); err != nil {
   484  					return err
   485  				}
   486  				a.nonGoOverlay[from] = dst
   487  			}
   488  		}
   489  	}
   490  
   491  	return nil
   492  }
   493  
   494  // needsBuild reports whether the Action (which must be mode "build") needs
   495  // to produce the built output.
   496  func (b *Builder) needsBuild(a *Action) bool {
   497  	return !b.IsCmdList && a.needBuild || b.NeedExport
   498  }
   499  
   500  const (
   501  	needBuild uint32 = 1 << iota
   502  	needCgoHdr
   503  	needVet
   504  	needCompiledGoFiles
   505  	needCovMetaFile
   506  	needStale
   507  )
   508  
   509  // checkCacheForBuild checks the cache for the outputs of the buildAction to determine
   510  // what work needs to be done by it and the actions preceding it. a is the action
   511  // currently being run, which has an actor of type *checkCacheActor and is a dependency
   512  // of the buildAction.
   513  func (b *Builder) checkCacheForBuild(a, buildAction *Action, covMetaFileName string) (_ *checkCacheProvider, err error) {
   514  	p := buildAction.Package
   515  	sh := b.Shell(a)
   516  
   517  	bit := func(x uint32, b bool) uint32 {
   518  		if b {
   519  			return x
   520  		}
   521  		return 0
   522  	}
   523  
   524  	cachedBuild := false
   525  	needCovMeta := p.Internal.Cover.GenMeta
   526  	need := bit(needBuild, !b.IsCmdList && buildAction.needBuild || b.NeedExport) |
   527  		bit(needCgoHdr, b.needCgoHdr(buildAction)) |
   528  		bit(needVet, buildAction.needVet) |
   529  		bit(needCovMetaFile, needCovMeta) |
   530  		bit(needCompiledGoFiles, b.NeedCompiledGoFiles)
   531  
   532  	if !p.BinaryOnly {
   533  		// We pass 'a' (this checkCacheAction) to buildActionID so that we use its dependencies,
   534  		// which are the actual package dependencies, rather than the buildAction's dependencies
   535  		// which also includes this action and the cover action.
   536  		if b.useCache(buildAction, b.buildActionID(a), p.Target, need&needBuild != 0) {
   537  			// We found the main output in the cache.
   538  			// If we don't need any other outputs, we can stop.
   539  			// Otherwise, we need to write files to a.Objdir (needVet, needCgoHdr).
   540  			// Remember that we might have them in cache
   541  			// and check again after we create a.Objdir.
   542  			cachedBuild = true
   543  			buildAction.output = []byte{} // start saving output in case we miss any cache results
   544  			need &^= needBuild
   545  			if b.NeedExport {
   546  				p.Export = buildAction.built
   547  				p.BuildID = buildAction.buildID
   548  			}
   549  			if need&needCompiledGoFiles != 0 {
   550  				if err := b.loadCachedCompiledGoFiles(buildAction); err == nil {
   551  					need &^= needCompiledGoFiles
   552  				}
   553  			}
   554  		}
   555  
   556  		// Source files might be cached, even if the full action is not
   557  		// (e.g., go list -compiled -find).
   558  		if !cachedBuild && need&needCompiledGoFiles != 0 {
   559  			if err := b.loadCachedCompiledGoFiles(buildAction); err == nil {
   560  				need &^= needCompiledGoFiles
   561  			}
   562  		}
   563  
   564  		if need == 0 {
   565  			return &checkCacheProvider{need: need}, nil
   566  		}
   567  		defer b.flushOutput(a)
   568  	}
   569  
   570  	defer func() {
   571  		if err != nil && b.IsCmdList && b.NeedError && p.Error == nil {
   572  			p.Error = &load.PackageError{Err: err}
   573  		}
   574  	}()
   575  
   576  	if p.Error != nil {
   577  		// Don't try to build anything for packages with errors. There may be a
   578  		// problem with the inputs that makes the package unsafe to build.
   579  		return nil, p.Error
   580  	}
   581  
   582  	// TODO(matloob): return early for binary-only packages so that we don't need to indent
   583  	// the core of this function in the if !p.BinaryOnly block above.
   584  	if p.BinaryOnly {
   585  		p.Stale = true
   586  		p.StaleReason = "binary-only packages are no longer supported"
   587  		if b.IsCmdList {
   588  			return &checkCacheProvider{need: 0}, nil
   589  		}
   590  		return nil, errors.New("binary-only packages are no longer supported")
   591  	}
   592  
   593  	if p.Module != nil && !allowedVersion(p.Module.GoVersion) {
   594  		return nil, errors.New("module requires Go " + p.Module.GoVersion + " or later")
   595  	}
   596  
   597  	if err := b.checkDirectives(buildAction); err != nil {
   598  		return nil, err
   599  	}
   600  
   601  	if err := sh.Mkdir(buildAction.Objdir); err != nil {
   602  		return nil, err
   603  	}
   604  
   605  	// Load cached cgo header, but only if we're skipping the main build (cachedBuild==true).
   606  	if cachedBuild && need&needCgoHdr != 0 {
   607  		if err := b.loadCachedCgoHdr(buildAction); err == nil {
   608  			need &^= needCgoHdr
   609  		}
   610  	}
   611  
   612  	// Load cached coverage meta-data file fragment, but only if we're
   613  	// skipping the main build (cachedBuild==true).
   614  	if cachedBuild && need&needCovMetaFile != 0 {
   615  		if err := b.loadCachedObjdirFile(buildAction, cache.Default(), covMetaFileName); err == nil {
   616  			need &^= needCovMetaFile
   617  		}
   618  	}
   619  
   620  	// Load cached vet config, but only if that's all we have left
   621  	// (need == needVet, not testing just the one bit).
   622  	// If we are going to do a full build anyway,
   623  	// we're going to regenerate the files in the build action anyway.
   624  	if need == needVet {
   625  		if err := b.loadCachedVet(buildAction); err == nil {
   626  			need &^= needVet
   627  		}
   628  	}
   629  
   630  	return &checkCacheProvider{need: need}, nil
   631  }
   632  
   633  func (b *Builder) runCover(a, buildAction *Action, objdir string, gofiles, cgofiles []string) (*coverProvider, error) {
   634  	p := a.Package
   635  	sh := b.Shell(a)
   636  
   637  	var cacheProvider *checkCacheProvider
   638  	for _, dep := range a.Deps {
   639  		if pr, ok := dep.Provider.(*checkCacheProvider); ok {
   640  			cacheProvider = pr
   641  		}
   642  	}
   643  	if cacheProvider == nil {
   644  		base.Fatalf("internal error: could not find checkCacheProvider")
   645  	}
   646  	need := cacheProvider.need
   647  
   648  	if need == 0 {
   649  		return nil, nil
   650  	}
   651  
   652  	if err := sh.Mkdir(a.Objdir); err != nil {
   653  		return nil, err
   654  	}
   655  
   656  	gofiles = slices.Clone(gofiles)
   657  	cgofiles = slices.Clone(cgofiles)
   658  
   659  	outfiles := []string{}
   660  	infiles := []string{}
   661  	for i, file := range str.StringList(gofiles, cgofiles) {
   662  		if base.IsTestFile(file) {
   663  			continue // Not covering this file.
   664  		}
   665  
   666  		var sourceFile string
   667  		var coverFile string
   668  		if base, found := strings.CutSuffix(file, ".cgo1.go"); found {
   669  			// cgo files have absolute paths
   670  			base = filepath.Base(base)
   671  			sourceFile = file
   672  			coverFile = objdir + base + ".cgo1.go"
   673  		} else {
   674  			sourceFile = filepath.Join(p.Dir, file)
   675  			coverFile = objdir + file
   676  		}
   677  		coverFile = strings.TrimSuffix(coverFile, ".go") + ".cover.go"
   678  		infiles = append(infiles, sourceFile)
   679  		outfiles = append(outfiles, coverFile)
   680  		if i < len(gofiles) {
   681  			gofiles[i] = coverFile
   682  		} else {
   683  			cgofiles[i-len(gofiles)] = coverFile
   684  		}
   685  	}
   686  
   687  	if len(infiles) != 0 {
   688  		// Coverage instrumentation creates new top level
   689  		// variables in the target package for things like
   690  		// meta-data containers, counter vars, etc. To avoid
   691  		// collisions with user variables, suffix the var name
   692  		// with 12 hex digits from the SHA-256 hash of the
   693  		// import path. Choice of 12 digits is historical/arbitrary,
   694  		// we just need enough of the hash to avoid accidents,
   695  		// as opposed to precluding determined attempts by
   696  		// users to break things.
   697  		sum := sha256.Sum256([]byte(a.Package.ImportPath))
   698  		coverVar := fmt.Sprintf("goCover_%x_", sum[:6])
   699  		mode := a.Package.Internal.Cover.Mode
   700  		if mode == "" {
   701  			panic("covermode should be set at this point")
   702  		}
   703  		if newoutfiles, err := b.cover(a, infiles, outfiles, coverVar, mode); err != nil {
   704  			return nil, err
   705  		} else {
   706  			outfiles = newoutfiles
   707  			gofiles = append([]string{newoutfiles[0]}, gofiles...)
   708  		}
   709  		if ca, ok := a.Actor.(*coverActor); ok && ca.covMetaFileName != "" {
   710  			b.cacheObjdirFile(buildAction, cache.Default(), ca.covMetaFileName)
   711  		}
   712  	}
   713  	return &coverProvider{gofiles, cgofiles}, nil
   714  }
   715  
   716  // build is the action for building a single package.
   717  // Note that any new influence on this logic must be reported in b.buildActionID above as well.
   718  func (b *Builder) build(ctx context.Context, a *Action) (err error) {
   719  	p := a.Package
   720  	sh := b.Shell(a)
   721  
   722  	var cacheProvider *checkCacheProvider
   723  	var coverPr *coverProvider
   724  	var runCgoPr *runCgoProvider
   725  	for _, dep := range a.Deps {
   726  		switch pr := dep.Provider.(type) {
   727  		case *coverProvider:
   728  			coverPr = pr
   729  		case *checkCacheProvider:
   730  			cacheProvider = pr
   731  		case *runCgoProvider:
   732  			runCgoPr = pr
   733  		}
   734  	}
   735  	if cacheProvider == nil {
   736  		base.Fatalf("internal error: could not find checkCacheProvider")
   737  	}
   738  
   739  	need := cacheProvider.need
   740  	need &^= needCovMetaFile // handled by cover action
   741  	need &^= needCgoHdr      // handled by run cgo action // TODO: accumulate "negative" need bits from actions
   742  
   743  	if need == 0 {
   744  		return
   745  	}
   746  	defer b.flushOutput(a)
   747  
   748  	if cfg.BuildN {
   749  		// In -n mode, print a banner between packages.
   750  		// The banner is five lines so that when changes to
   751  		// different sections of the bootstrap script have to
   752  		// be merged, the banners give patch something
   753  		// to use to find its context.
   754  		sh.Printf("\n#\n# %s\n#\n\n", p.ImportPath)
   755  	}
   756  
   757  	if cfg.BuildV {
   758  		sh.Printf("%s\n", p.ImportPath)
   759  	}
   760  
   761  	objdir := a.Objdir
   762  
   763  	if err := AllowInstall(a); err != nil {
   764  		return err
   765  	}
   766  
   767  	// make target directory
   768  	dir, _ := filepath.Split(a.Target)
   769  	if dir != "" {
   770  		if err := sh.Mkdir(dir); err != nil {
   771  			return err
   772  		}
   773  	}
   774  
   775  	gofiles := str.StringList(p.GoFiles)
   776  	cfiles := str.StringList(p.CFiles)
   777  	sfiles := str.StringList(p.SFiles)
   778  	var objects, cgoObjects []string
   779  
   780  	// If we're doing coverage, preprocess the .go files and put them in the work directory
   781  	if p.Internal.Cover.Mode != "" {
   782  		gofiles = coverPr.goSources
   783  	}
   784  
   785  	if p.UsesCgo() || p.UsesSwig() {
   786  		if runCgoPr == nil {
   787  			base.Fatalf("internal error: could not find runCgoProvider")
   788  		}
   789  
   790  		// In a package using cgo, cgo compiles the C, C++ and assembly files with gcc.
   791  		// There is one exception: runtime/cgo's job is to bridge the
   792  		// cgo and non-cgo worlds, so it necessarily has files in both.
   793  		// In that case gcc only gets the gcc_* files.
   794  		cfiles = nil
   795  		if p.Standard && p.ImportPath == "runtime/cgo" {
   796  			// filter to the non-gcc files.
   797  			i := 0
   798  			for _, f := range sfiles {
   799  				if !strings.HasPrefix(f, "gcc_") {
   800  					sfiles[i] = f
   801  					i++
   802  				}
   803  			}
   804  			sfiles = sfiles[:i]
   805  		} else {
   806  			sfiles = nil
   807  		}
   808  
   809  		outGo, outObj, err := b.processCgoOutputs(a, runCgoPr, base.Tool("cgo"), objdir)
   810  
   811  		if err != nil {
   812  			return err
   813  		}
   814  		if cfg.BuildToolchainName == "gccgo" {
   815  			cgoObjects = append(cgoObjects, a.Objdir+"_cgo_flags")
   816  		}
   817  		cgoObjects = append(cgoObjects, outObj...)
   818  		gofiles = append(gofiles, outGo...)
   819  
   820  		switch cfg.BuildBuildmode {
   821  		case "c-archive", "c-shared":
   822  			b.cacheCgoHdr(a)
   823  		}
   824  	}
   825  
   826  	var srcfiles []string // .go and non-.go
   827  	srcfiles = append(srcfiles, gofiles...)
   828  	srcfiles = append(srcfiles, sfiles...)
   829  	srcfiles = append(srcfiles, cfiles...)
   830  	b.cacheSrcFiles(a, srcfiles)
   831  
   832  	// Sanity check only, since Package.load already checked as well.
   833  	if len(gofiles) == 0 {
   834  		return &load.NoGoError{Package: p}
   835  	}
   836  
   837  	// Prepare Go vet config if needed.
   838  	if need&needVet != 0 {
   839  		buildVetConfig(a, srcfiles)
   840  		need &^= needVet
   841  	}
   842  	if need&needCompiledGoFiles != 0 {
   843  		if err := b.loadCachedCompiledGoFiles(a); err != nil {
   844  			return fmt.Errorf("loading compiled Go files from cache: %w", err)
   845  		}
   846  		need &^= needCompiledGoFiles
   847  	}
   848  	if need == 0 {
   849  		// Nothing left to do.
   850  		return nil
   851  	}
   852  
   853  	// Collect symbol ABI requirements from assembly.
   854  	symabis, err := BuildToolchain.symabis(b, a, sfiles)
   855  	if err != nil {
   856  		return err
   857  	}
   858  
   859  	// Prepare Go import config.
   860  	// We start it off with a comment so it can't be empty, so icfg.Bytes() below is never nil.
   861  	// It should never be empty anyway, but there have been bugs in the past that resulted
   862  	// in empty configs, which then unfortunately turn into "no config passed to compiler",
   863  	// and the compiler falls back to looking in pkg itself, which mostly works,
   864  	// except when it doesn't.
   865  	var icfg bytes.Buffer
   866  	fmt.Fprintf(&icfg, "# import config\n")
   867  	for i, raw := range p.Internal.RawImports {
   868  		final := p.Imports[i]
   869  		if final != raw {
   870  			fmt.Fprintf(&icfg, "importmap %s=%s\n", raw, final)
   871  		}
   872  	}
   873  	for _, a1 := range a.Deps {
   874  		p1 := a1.Package
   875  		if p1 == nil || p1.ImportPath == "" || a1.built == "" {
   876  			continue
   877  		}
   878  		fmt.Fprintf(&icfg, "packagefile %s=%s\n", p1.ImportPath, a1.built)
   879  	}
   880  
   881  	// Prepare Go embed config if needed.
   882  	// Unlike the import config, it's okay for the embed config to be empty.
   883  	var embedcfg []byte
   884  	if len(p.Internal.Embed) > 0 {
   885  		var embed struct {
   886  			Patterns map[string][]string
   887  			Files    map[string]string
   888  		}
   889  		embed.Patterns = p.Internal.Embed
   890  		embed.Files = make(map[string]string)
   891  		for _, file := range p.EmbedFiles {
   892  			embed.Files[file] = fsys.Actual(filepath.Join(p.Dir, file))
   893  		}
   894  		js, err := json.MarshalIndent(&embed, "", "\t")
   895  		if err != nil {
   896  			return fmt.Errorf("marshal embedcfg: %v", err)
   897  		}
   898  		embedcfg = js
   899  	}
   900  
   901  	// Find PGO profile if needed.
   902  	var pgoProfile string
   903  	for _, a1 := range a.Deps {
   904  		if a1.Mode != "preprocess PGO profile" {
   905  			continue
   906  		}
   907  		if pgoProfile != "" {
   908  			return fmt.Errorf("action contains multiple PGO profile dependencies")
   909  		}
   910  		pgoProfile = a1.built
   911  	}
   912  
   913  	if p.Internal.BuildInfo != nil && cfg.ModulesEnabled {
   914  		prog := modload.ModInfoProg(p.Internal.BuildInfo.String(), cfg.BuildToolchainName == "gccgo")
   915  		if len(prog) > 0 {
   916  			if err := sh.writeFile(objdir+"_gomod_.go", prog); err != nil {
   917  				return err
   918  			}
   919  			gofiles = append(gofiles, objdir+"_gomod_.go")
   920  		}
   921  	}
   922  
   923  	// Compile Go.
   924  	objpkg := objdir + "_pkg_.a"
   925  	ofile, out, err := BuildToolchain.gc(b, a, objpkg, icfg.Bytes(), embedcfg, symabis, len(sfiles) > 0, pgoProfile, gofiles)
   926  	if err := sh.reportCmd("", "", out, err); err != nil {
   927  		return err
   928  	}
   929  	if ofile != objpkg {
   930  		objects = append(objects, ofile)
   931  	}
   932  
   933  	// Copy .h files named for goos or goarch or goos_goarch
   934  	// to names using GOOS and GOARCH.
   935  	// For example, defs_linux_amd64.h becomes defs_GOOS_GOARCH.h.
   936  	_goos_goarch := "_" + cfg.Goos + "_" + cfg.Goarch
   937  	_goos := "_" + cfg.Goos
   938  	_goarch := "_" + cfg.Goarch
   939  	for _, file := range p.HFiles {
   940  		name, ext := fileExtSplit(file)
   941  		switch {
   942  		case strings.HasSuffix(name, _goos_goarch):
   943  			targ := file[:len(name)-len(_goos_goarch)] + "_GOOS_GOARCH." + ext
   944  			if err := sh.CopyFile(objdir+targ, filepath.Join(p.Dir, file), 0666, true); err != nil {
   945  				return err
   946  			}
   947  		case strings.HasSuffix(name, _goarch):
   948  			targ := file[:len(name)-len(_goarch)] + "_GOARCH." + ext
   949  			if err := sh.CopyFile(objdir+targ, filepath.Join(p.Dir, file), 0666, true); err != nil {
   950  				return err
   951  			}
   952  		case strings.HasSuffix(name, _goos):
   953  			targ := file[:len(name)-len(_goos)] + "_GOOS." + ext
   954  			if err := sh.CopyFile(objdir+targ, filepath.Join(p.Dir, file), 0666, true); err != nil {
   955  				return err
   956  			}
   957  		}
   958  	}
   959  
   960  	if err := b.computeNonGoOverlay(a, p, sh, objdir, [][]string{cfiles}); err != nil {
   961  		return err
   962  	}
   963  
   964  	// Compile C files in a package being built with gccgo. We disallow
   965  	// C files when compiling with gc unless swig or cgo is used.
   966  	for _, file := range cfiles {
   967  		out := file[:len(file)-len(".c")] + ".o"
   968  		if err := BuildToolchain.cc(b, a, objdir+out, file); err != nil {
   969  			return err
   970  		}
   971  		objects = append(objects, out)
   972  	}
   973  
   974  	// Assemble .s files.
   975  	if len(sfiles) > 0 {
   976  		ofiles, err := BuildToolchain.asm(b, a, sfiles)
   977  		if err != nil {
   978  			return err
   979  		}
   980  		objects = append(objects, ofiles...)
   981  	}
   982  
   983  	// For gccgo on ELF systems, we write the build ID as an assembler file.
   984  	// This lets us set the SHF_EXCLUDE flag.
   985  	// This is read by readGccgoArchive in cmd/internal/buildid/buildid.go.
   986  	if a.buildID != "" && cfg.BuildToolchainName == "gccgo" {
   987  		switch cfg.Goos {
   988  		case "aix", "android", "dragonfly", "freebsd", "illumos", "linux", "netbsd", "openbsd", "solaris":
   989  			asmfile, err := b.gccgoBuildIDFile(a)
   990  			if err != nil {
   991  				return err
   992  			}
   993  			ofiles, err := BuildToolchain.asm(b, a, []string{asmfile})
   994  			if err != nil {
   995  				return err
   996  			}
   997  			objects = append(objects, ofiles...)
   998  		}
   999  	}
  1000  
  1001  	// NOTE(rsc): On Windows, it is critically important that the
  1002  	// gcc-compiled objects (cgoObjects) be listed after the ordinary
  1003  	// objects in the archive. I do not know why this is.
  1004  	// https://golang.org/issue/2601
  1005  	objects = append(objects, cgoObjects...)
  1006  
  1007  	// Add system object files.
  1008  	for _, syso := range p.SysoFiles {
  1009  		objects = append(objects, filepath.Join(p.Dir, syso))
  1010  	}
  1011  
  1012  	// Pack into archive in objdir directory.
  1013  	// If the Go compiler wrote an archive, we only need to add the
  1014  	// object files for non-Go sources to the archive.
  1015  	// If the Go compiler wrote an archive and the package is entirely
  1016  	// Go sources, there is no pack to execute at all.
  1017  	if len(objects) > 0 {
  1018  		if err := BuildToolchain.pack(b, a, objpkg, objects); err != nil {
  1019  			return err
  1020  		}
  1021  	}
  1022  
  1023  	if err := b.updateBuildID(a, objpkg); err != nil {
  1024  		return err
  1025  	}
  1026  
  1027  	a.built = objpkg
  1028  	return nil
  1029  }
  1030  
  1031  func (b *Builder) checkDirectives(a *Action) error {
  1032  	var msg []byte
  1033  	p := a.Package
  1034  	var seen map[string]token.Position
  1035  	for _, d := range p.Internal.Build.Directives {
  1036  		if strings.HasPrefix(d.Text, "//go:debug") {
  1037  			key, _, err := load.ParseGoDebug(d.Text)
  1038  			if err != nil && err != load.ErrNotGoDebug {
  1039  				msg = fmt.Appendf(msg, "%s: invalid //go:debug: %v\n", d.Pos, err)
  1040  				continue
  1041  			}
  1042  			if pos, ok := seen[key]; ok {
  1043  				msg = fmt.Appendf(msg, "%s: repeated //go:debug for %v\n\t%s: previous //go:debug\n", d.Pos, key, pos)
  1044  				continue
  1045  			}
  1046  			if seen == nil {
  1047  				seen = make(map[string]token.Position)
  1048  			}
  1049  			seen[key] = d.Pos
  1050  		}
  1051  	}
  1052  	if len(msg) > 0 {
  1053  		// We pass a non-nil error to reportCmd to trigger the failure reporting
  1054  		// path, but the content of the error doesn't matter because msg is
  1055  		// non-empty.
  1056  		err := errors.New("invalid directive")
  1057  		return b.Shell(a).reportCmd("", "", msg, err)
  1058  	}
  1059  	return nil
  1060  }
  1061  
  1062  func (b *Builder) cacheObjdirFile(a *Action, c cache.Cache, name string) error {
  1063  	f, err := os.Open(a.Objdir + name)
  1064  	if err != nil {
  1065  		return err
  1066  	}
  1067  	defer f.Close()
  1068  	_, _, err = c.Put(cache.Subkey(a.actionID, name), f)
  1069  	return err
  1070  }
  1071  
  1072  func (b *Builder) findCachedObjdirFile(a *Action, c cache.Cache, name string) (string, error) {
  1073  	file, _, err := cache.GetFile(c, cache.Subkey(a.actionID, name))
  1074  	if err != nil {
  1075  		return "", fmt.Errorf("loading cached file %s: %w", name, err)
  1076  	}
  1077  	return file, nil
  1078  }
  1079  
  1080  func (b *Builder) loadCachedObjdirFile(a *Action, c cache.Cache, name string) error {
  1081  	cached, err := b.findCachedObjdirFile(a, c, name)
  1082  	if err != nil {
  1083  		return err
  1084  	}
  1085  	return b.Shell(a).CopyFile(a.Objdir+name, cached, 0666, true)
  1086  }
  1087  
  1088  func (b *Builder) cacheCgoHdr(a *Action) {
  1089  	c := cache.Default()
  1090  	b.cacheObjdirFile(a, c, "_cgo_install.h")
  1091  }
  1092  
  1093  func (b *Builder) loadCachedCgoHdr(a *Action) error {
  1094  	c := cache.Default()
  1095  	return b.loadCachedObjdirFile(a, c, "_cgo_install.h")
  1096  }
  1097  
  1098  func (b *Builder) cacheSrcFiles(a *Action, srcfiles []string) {
  1099  	c := cache.Default()
  1100  	var buf bytes.Buffer
  1101  	for _, file := range srcfiles {
  1102  		if !strings.HasPrefix(file, a.Objdir) {
  1103  			// not generated
  1104  			buf.WriteString("./")
  1105  			buf.WriteString(file)
  1106  			buf.WriteString("\n")
  1107  			continue
  1108  		}
  1109  		name := file[len(a.Objdir):]
  1110  		buf.WriteString(name)
  1111  		buf.WriteString("\n")
  1112  		if err := b.cacheObjdirFile(a, c, name); err != nil {
  1113  			return
  1114  		}
  1115  	}
  1116  	cache.PutBytes(c, cache.Subkey(a.actionID, "srcfiles"), buf.Bytes())
  1117  }
  1118  
  1119  func (b *Builder) loadCachedVet(a *Action) error {
  1120  	c := cache.Default()
  1121  	list, _, err := cache.GetBytes(c, cache.Subkey(a.actionID, "srcfiles"))
  1122  	if err != nil {
  1123  		return fmt.Errorf("reading srcfiles list: %w", err)
  1124  	}
  1125  	var srcfiles []string
  1126  	for name := range strings.SplitSeq(string(list), "\n") {
  1127  		if name == "" { // end of list
  1128  			continue
  1129  		}
  1130  		if strings.HasPrefix(name, "./") {
  1131  			srcfiles = append(srcfiles, name[2:])
  1132  			continue
  1133  		}
  1134  		if err := b.loadCachedObjdirFile(a, c, name); err != nil {
  1135  			return err
  1136  		}
  1137  		srcfiles = append(srcfiles, a.Objdir+name)
  1138  	}
  1139  	buildVetConfig(a, srcfiles)
  1140  	return nil
  1141  }
  1142  
  1143  func (b *Builder) loadCachedCompiledGoFiles(a *Action) error {
  1144  	c := cache.Default()
  1145  	list, _, err := cache.GetBytes(c, cache.Subkey(a.actionID, "srcfiles"))
  1146  	if err != nil {
  1147  		return fmt.Errorf("reading srcfiles list: %w", err)
  1148  	}
  1149  	var gofiles []string
  1150  	for name := range strings.SplitSeq(string(list), "\n") {
  1151  		if name == "" { // end of list
  1152  			continue
  1153  		} else if !strings.HasSuffix(name, ".go") {
  1154  			continue
  1155  		}
  1156  		if strings.HasPrefix(name, "./") {
  1157  			gofiles = append(gofiles, name[len("./"):])
  1158  			continue
  1159  		}
  1160  		file, err := b.findCachedObjdirFile(a, c, name)
  1161  		if err != nil {
  1162  			return fmt.Errorf("finding %s: %w", name, err)
  1163  		}
  1164  		gofiles = append(gofiles, file)
  1165  	}
  1166  	a.Package.CompiledGoFiles = gofiles
  1167  	return nil
  1168  }
  1169  
  1170  // vetConfig is the configuration passed to vet describing a single package.
  1171  type vetConfig struct {
  1172  	ID           string   // package ID (example: "fmt [fmt.test]")
  1173  	Compiler     string   // compiler name (gc, gccgo)
  1174  	Dir          string   // directory containing package
  1175  	ImportPath   string   // canonical import path ("package path")
  1176  	GoFiles      []string // absolute paths to package source files
  1177  	NonGoFiles   []string // absolute paths to package non-Go files
  1178  	IgnoredFiles []string // absolute paths to ignored source files
  1179  
  1180  	ModulePath    string            // module path (may be "" on module error)
  1181  	ModuleVersion string            // module version (may be "" on main module or module error)
  1182  	ImportMap     map[string]string // map import path in source code to package path
  1183  	PackageFile   map[string]string // map package path to .a file with export data
  1184  	Standard      map[string]bool   // map package path to whether it's in the standard library
  1185  	PackageVetx   map[string]string // map package path to vetx data from earlier vet run
  1186  	VetxOnly      bool              // only compute vetx data; don't report detected problems
  1187  	VetxOutput    string            // write vetx data to this output file
  1188  	Stdout        string            // write stdout (JSON, unified diff) to this output file
  1189  	GoVersion     string            // Go version for package
  1190  
  1191  	SucceedOnTypecheckFailure bool // awful hack; see #18395 and below
  1192  }
  1193  
  1194  func buildVetConfig(a *Action, srcfiles []string) {
  1195  	// Classify files based on .go extension.
  1196  	// srcfiles does not include raw cgo files.
  1197  	var gofiles, nongofiles []string
  1198  	for _, name := range srcfiles {
  1199  		if strings.HasSuffix(name, ".go") {
  1200  			gofiles = append(gofiles, name)
  1201  		} else {
  1202  			nongofiles = append(nongofiles, name)
  1203  		}
  1204  	}
  1205  
  1206  	ignored := str.StringList(a.Package.IgnoredGoFiles, a.Package.IgnoredOtherFiles)
  1207  
  1208  	// Pass list of absolute paths to vet,
  1209  	// so that vet's error messages will use absolute paths,
  1210  	// so that we can reformat them relative to the directory
  1211  	// in which the go command is invoked.
  1212  	vcfg := &vetConfig{
  1213  		ID:           a.Package.ImportPath,
  1214  		Compiler:     cfg.BuildToolchainName,
  1215  		Dir:          a.Package.Dir,
  1216  		GoFiles:      actualFiles(mkAbsFiles(a.Package.Dir, gofiles)),
  1217  		NonGoFiles:   actualFiles(mkAbsFiles(a.Package.Dir, nongofiles)),
  1218  		IgnoredFiles: actualFiles(mkAbsFiles(a.Package.Dir, ignored)),
  1219  		ImportPath:   a.Package.ImportPath,
  1220  		ImportMap:    make(map[string]string),
  1221  		PackageFile:  make(map[string]string),
  1222  		Standard:     make(map[string]bool),
  1223  	}
  1224  	vcfg.GoVersion = "go" + gover.Local()
  1225  	if a.Package.Module != nil {
  1226  		v := a.Package.Module.GoVersion
  1227  		if v == "" {
  1228  			v = gover.DefaultGoModVersion
  1229  		}
  1230  		vcfg.GoVersion = "go" + v
  1231  
  1232  		if a.Package.Module.Error == nil {
  1233  			vcfg.ModulePath = a.Package.Module.Path
  1234  			vcfg.ModuleVersion = a.Package.Module.Version
  1235  		}
  1236  	}
  1237  	a.vetCfg = vcfg
  1238  	for i, raw := range a.Package.Internal.RawImports {
  1239  		final := a.Package.Imports[i]
  1240  		vcfg.ImportMap[raw] = final
  1241  	}
  1242  
  1243  	// Compute the list of mapped imports in the vet config
  1244  	// so that we can add any missing mappings below.
  1245  	vcfgMapped := make(map[string]bool)
  1246  	for _, p := range vcfg.ImportMap {
  1247  		vcfgMapped[p] = true
  1248  	}
  1249  
  1250  	for _, a1 := range a.Deps {
  1251  		p1 := a1.Package
  1252  		if p1 == nil || p1.ImportPath == "" || p1 == a.Package {
  1253  			continue
  1254  		}
  1255  		// Add import mapping if needed
  1256  		// (for imports like "runtime/cgo" that appear only in generated code).
  1257  		if !vcfgMapped[p1.ImportPath] {
  1258  			vcfg.ImportMap[p1.ImportPath] = p1.ImportPath
  1259  		}
  1260  		if a1.built != "" {
  1261  			vcfg.PackageFile[p1.ImportPath] = a1.built
  1262  		}
  1263  		if p1.Standard {
  1264  			vcfg.Standard[p1.ImportPath] = true
  1265  		}
  1266  	}
  1267  }
  1268  
  1269  // VetTool is the path to the effective vet or fix tool binary.
  1270  // The user may specify a non-default value using -{vet,fix}tool.
  1271  // The caller is expected to set it (if needed) before executing any vet actions.
  1272  var VetTool string
  1273  
  1274  // VetFlags are the default flags to pass to vet.
  1275  // The caller is expected to set them before executing any vet actions.
  1276  var VetFlags []string
  1277  
  1278  // VetHandleStdout determines how the stdout output of each vet tool
  1279  // invocation should be handled. The default behavior is to copy it to
  1280  // the go command's stdout, atomically.
  1281  var VetHandleStdout = copyToStdout
  1282  
  1283  // VetExplicit records whether the vet flags (which may include
  1284  // -{vet,fix}tool) were set explicitly on the command line.
  1285  var VetExplicit bool
  1286  
  1287  func (b *Builder) vet(ctx context.Context, a *Action) error {
  1288  	// a.Deps[0] is the build of the package being vetted.
  1289  
  1290  	a.Failed = nil // vet of dependency may have failed but we can still succeed
  1291  
  1292  	if a.Deps[0].Failed != nil {
  1293  		// The build of the package has failed. Skip vet check.
  1294  		// Vet could return export data for non-typecheck errors,
  1295  		// but we ignore it because the package cannot be compiled.
  1296  		return nil
  1297  	}
  1298  
  1299  	vcfg := a.Deps[0].vetCfg
  1300  	if vcfg == nil {
  1301  		// Vet config should only be missing if the build failed.
  1302  		return fmt.Errorf("vet config not found")
  1303  	}
  1304  
  1305  	sh := b.Shell(a)
  1306  
  1307  	// We use "vet" terminology even when building action graphs for go fix.
  1308  	vcfg.VetxOnly = a.VetxOnly
  1309  	vcfg.VetxOutput = a.Objdir + "vet.out"
  1310  	vcfg.Stdout = a.Objdir + "vet.stdout"
  1311  	vcfg.PackageVetx = make(map[string]string)
  1312  
  1313  	h := cache.NewHash("vet " + a.Package.ImportPath)
  1314  	fmt.Fprintf(h, "vet %q\n", b.toolID("vet"))
  1315  
  1316  	vetFlags := VetFlags
  1317  
  1318  	// In GOROOT, we enable all the vet tests during 'go test',
  1319  	// not just the high-confidence subset. This gets us extra
  1320  	// checking for the standard library (at some compliance cost)
  1321  	// and helps us gain experience about how well the checks
  1322  	// work, to help decide which should be turned on by default.
  1323  	// The command-line still wins.
  1324  	//
  1325  	// Note that this flag change applies even when running vet as
  1326  	// a dependency of vetting a package outside std.
  1327  	// (Otherwise we'd have to introduce a whole separate
  1328  	// space of "vet fmt as a dependency of a std top-level vet"
  1329  	// versus "vet fmt as a dependency of a non-std top-level vet".)
  1330  	// This is OK as long as the packages that are farther down the
  1331  	// dependency tree turn on *more* analysis, as here.
  1332  	// (The unsafeptr check does not write any facts for use by
  1333  	// later vet runs, nor does unreachable.)
  1334  	if a.Package.Goroot && !VetExplicit && VetTool == base.Tool("vet") {
  1335  		// Turn off -unsafeptr checks.
  1336  		// There's too much unsafe.Pointer code
  1337  		// that vet doesn't like in low-level packages
  1338  		// like runtime, sync, and reflect.
  1339  		// Note that $GOROOT/src/buildall.bash
  1340  		// does the same
  1341  		// and should be updated if these flags are
  1342  		// changed here.
  1343  		vetFlags = []string{"-unsafeptr=false"}
  1344  
  1345  		// Also turn off -unreachable checks during go test.
  1346  		// During testing it is very common to make changes
  1347  		// like hard-coded forced returns or panics that make
  1348  		// code unreachable. It's unreasonable to insist on files
  1349  		// not having any unreachable code during "go test".
  1350  		// (buildall.bash still has -unreachable enabled
  1351  		// for the overall whole-tree scan.)
  1352  		if cfg.CmdName == "test" {
  1353  			vetFlags = append(vetFlags, "-unreachable=false")
  1354  		}
  1355  	}
  1356  
  1357  	// Note: We could decide that vet should compute export data for
  1358  	// all analyses, in which case we don't need to include the flags here.
  1359  	// But that would mean that if an analysis causes problems like
  1360  	// unexpected crashes there would be no way to turn it off.
  1361  	// It seems better to let the flags disable export analysis too.
  1362  	fmt.Fprintf(h, "vetflags %q\n", vetFlags)
  1363  
  1364  	fmt.Fprintf(h, "pkg %q\n", a.Deps[0].actionID)
  1365  	for _, a1 := range a.Deps {
  1366  		if a1.Mode == "vet" && a1.built != "" {
  1367  			fmt.Fprintf(h, "vetout %q %s\n", a1.Package.ImportPath, b.fileHash(a1.built))
  1368  			vcfg.PackageVetx[a1.Package.ImportPath] = a1.built
  1369  		}
  1370  	}
  1371  	vetxKey := cache.ActionID(h.Sum()) // for .vetx file
  1372  
  1373  	fmt.Fprintf(h, "stdout\n")
  1374  	stdoutKey := cache.ActionID(h.Sum()) // for .stdout file
  1375  
  1376  	// Check the cache; -a forces a rebuild.
  1377  	if !cfg.BuildA {
  1378  		c := cache.Default()
  1379  		if vcfg.VetxOnly {
  1380  			if file, _, err := cache.GetFile(c, vetxKey); err == nil {
  1381  				a.built = file
  1382  				return nil
  1383  			}
  1384  		} else {
  1385  			// Copy cached vet.std files to stdout.
  1386  			if file, _, err := cache.GetFile(c, stdoutKey); err == nil {
  1387  				f, err := os.Open(file)
  1388  				if err != nil {
  1389  					return err
  1390  				}
  1391  				defer f.Close() // ignore error (can't fail)
  1392  				return VetHandleStdout(f)
  1393  			}
  1394  		}
  1395  	}
  1396  
  1397  	js, err := json.MarshalIndent(vcfg, "", "\t")
  1398  	if err != nil {
  1399  		return fmt.Errorf("internal error marshaling vet config: %v", err)
  1400  	}
  1401  	js = append(js, '\n')
  1402  	if err := sh.writeFile(a.Objdir+"vet.cfg", js); err != nil {
  1403  		return err
  1404  	}
  1405  
  1406  	// TODO(rsc): Why do we pass $GCCGO to go vet?
  1407  	env := b.cCompilerEnv()
  1408  	if cfg.BuildToolchainName == "gccgo" {
  1409  		env = append(env, "GCCGO="+BuildToolchain.compiler())
  1410  	}
  1411  
  1412  	p := a.Package
  1413  	tool := VetTool
  1414  	if tool == "" {
  1415  		panic("VetTool unset")
  1416  	}
  1417  
  1418  	if err := sh.run(p.Dir, p.ImportPath, env, cfg.BuildToolexec, tool, vetFlags, a.Objdir+"vet.cfg"); err != nil {
  1419  		return err
  1420  	}
  1421  
  1422  	// Vet tool succeeded, possibly with facts and JSON stdout. Save both in cache.
  1423  
  1424  	// Save facts
  1425  	if f, err := os.Open(vcfg.VetxOutput); err == nil {
  1426  		defer f.Close() // ignore error
  1427  		a.built = vcfg.VetxOutput
  1428  		cache.Default().Put(vetxKey, f) // ignore error
  1429  	}
  1430  
  1431  	// Save stdout.
  1432  	if f, err := os.Open(vcfg.Stdout); err == nil {
  1433  		defer f.Close() // ignore error
  1434  		if err := VetHandleStdout(f); err != nil {
  1435  			return err
  1436  		}
  1437  		f.Seek(0, io.SeekStart)           // ignore error
  1438  		cache.Default().Put(stdoutKey, f) // ignore error
  1439  	}
  1440  
  1441  	return nil
  1442  }
  1443  
  1444  var stdoutMu sync.Mutex // serializes concurrent writes (of e.g. JSON values) to stdout
  1445  
  1446  // copyToStdout copies the stream to stdout while holding the lock.
  1447  func copyToStdout(r io.Reader) error {
  1448  	stdoutMu.Lock()
  1449  	defer stdoutMu.Unlock()
  1450  	if _, err := io.Copy(os.Stdout, r); err != nil {
  1451  		return fmt.Errorf("copying vet tool stdout: %w", err)
  1452  	}
  1453  	return nil
  1454  }
  1455  
  1456  // linkActionID computes the action ID for a link action.
  1457  func (b *Builder) linkActionID(a *Action) cache.ActionID {
  1458  	p := a.Package
  1459  	h := cache.NewHash("link " + p.ImportPath)
  1460  
  1461  	// Toolchain-independent configuration.
  1462  	fmt.Fprintf(h, "link\n")
  1463  	fmt.Fprintf(h, "buildmode %s goos %s goarch %s\n", cfg.BuildBuildmode, cfg.Goos, cfg.Goarch)
  1464  	fmt.Fprintf(h, "import %q\n", p.ImportPath)
  1465  	fmt.Fprintf(h, "omitdebug %v standard %v local %v prefix %q\n", p.Internal.OmitDebug, p.Standard, p.Internal.Local, p.Internal.LocalPrefix)
  1466  	fmt.Fprintf(h, "defaultgodebug %q\n", p.DefaultGODEBUG)
  1467  	if cfg.BuildTrimpath {
  1468  		fmt.Fprintln(h, "trimpath")
  1469  	}
  1470  
  1471  	// Toolchain-dependent configuration, shared with b.linkSharedActionID.
  1472  	b.printLinkerConfig(h, p)
  1473  
  1474  	// Input files.
  1475  	for _, a1 := range a.Deps {
  1476  		p1 := a1.Package
  1477  		if p1 != nil {
  1478  			if a1.built != "" || a1.buildID != "" {
  1479  				buildID := a1.buildID
  1480  				if buildID == "" {
  1481  					buildID = b.buildID(a1.built)
  1482  				}
  1483  				fmt.Fprintf(h, "packagefile %s=%s\n", p1.ImportPath, contentID(buildID))
  1484  			}
  1485  			// Because we put package main's full action ID into the binary's build ID,
  1486  			// we must also put the full action ID into the binary's action ID hash.
  1487  			if p1.Name == "main" {
  1488  				fmt.Fprintf(h, "packagemain %s\n", a1.buildID)
  1489  			}
  1490  			if p1.Shlib != "" {
  1491  				fmt.Fprintf(h, "packageshlib %s=%s\n", p1.ImportPath, contentID(b.buildID(p1.Shlib)))
  1492  			}
  1493  		}
  1494  	}
  1495  
  1496  	return h.Sum()
  1497  }
  1498  
  1499  // printLinkerConfig prints the linker config into the hash h,
  1500  // as part of the computation of a linker-related action ID.
  1501  func (b *Builder) printLinkerConfig(h io.Writer, p *load.Package) {
  1502  	switch cfg.BuildToolchainName {
  1503  	default:
  1504  		base.Fatalf("linkActionID: unknown toolchain %q", cfg.BuildToolchainName)
  1505  
  1506  	case "gc":
  1507  		fmt.Fprintf(h, "link %s %q %s\n", b.toolID("link"), forcedLdflags, ldBuildmode)
  1508  		if p != nil {
  1509  			fmt.Fprintf(h, "linkflags %q\n", p.Internal.Ldflags)
  1510  		}
  1511  
  1512  		// GOARM, GOMIPS, etc.
  1513  		key, val, _ := cfg.GetArchEnv()
  1514  		fmt.Fprintf(h, "%s=%s\n", key, val)
  1515  
  1516  		if cfg.CleanGOEXPERIMENT != "" {
  1517  			fmt.Fprintf(h, "GOEXPERIMENT=%q\n", cfg.CleanGOEXPERIMENT)
  1518  		}
  1519  
  1520  		// The linker writes source file paths that refer to GOROOT,
  1521  		// but only if -trimpath is not specified (see [gctoolchain.ld] in gc.go).
  1522  		gorootFinal := cfg.GOROOT
  1523  		if cfg.BuildTrimpath {
  1524  			gorootFinal = ""
  1525  		}
  1526  		fmt.Fprintf(h, "GOROOT=%s\n", gorootFinal)
  1527  
  1528  		// GO_EXTLINK_ENABLED controls whether the external linker is used.
  1529  		fmt.Fprintf(h, "GO_EXTLINK_ENABLED=%s\n", cfg.Getenv("GO_EXTLINK_ENABLED"))
  1530  
  1531  		// TODO(rsc): Do cgo settings and flags need to be included?
  1532  		// Or external linker settings and flags?
  1533  
  1534  	case "gccgo":
  1535  		id, _, err := b.gccToolID(BuildToolchain.linker(), "go")
  1536  		if err != nil {
  1537  			base.Fatalf("%v", err)
  1538  		}
  1539  		fmt.Fprintf(h, "link %s %s\n", id, ldBuildmode)
  1540  		// TODO(iant): Should probably include cgo flags here.
  1541  	}
  1542  }
  1543  
  1544  // link is the action for linking a single command.
  1545  // Note that any new influence on this logic must be reported in b.linkActionID above as well.
  1546  func (b *Builder) link(ctx context.Context, a *Action) (err error) {
  1547  	if b.useCache(a, b.linkActionID(a), a.Package.Target, !b.IsCmdList) || b.IsCmdList {
  1548  		return nil
  1549  	}
  1550  	defer b.flushOutput(a)
  1551  
  1552  	sh := b.Shell(a)
  1553  	if err := sh.Mkdir(a.Objdir); err != nil {
  1554  		return err
  1555  	}
  1556  
  1557  	importcfg := a.Objdir + "importcfg.link"
  1558  	if err := b.writeLinkImportcfg(a, importcfg); err != nil {
  1559  		return err
  1560  	}
  1561  
  1562  	if err := AllowInstall(a); err != nil {
  1563  		return err
  1564  	}
  1565  
  1566  	// make target directory
  1567  	dir, _ := filepath.Split(a.Target)
  1568  	if dir != "" {
  1569  		if err := sh.Mkdir(dir); err != nil {
  1570  			return err
  1571  		}
  1572  	}
  1573  
  1574  	if err := BuildToolchain.ld(b, a, a.Target, importcfg, a.Deps[0].built); err != nil {
  1575  		return err
  1576  	}
  1577  
  1578  	// Update the binary with the final build ID.
  1579  	if err := b.updateBuildID(a, a.Target); err != nil {
  1580  		return err
  1581  	}
  1582  
  1583  	a.built = a.Target
  1584  	return nil
  1585  }
  1586  
  1587  func (b *Builder) writeLinkImportcfg(a *Action, file string) error {
  1588  	// Prepare Go import cfg.
  1589  	var icfg bytes.Buffer
  1590  	for _, a1 := range a.Deps {
  1591  		p1 := a1.Package
  1592  		if p1 == nil {
  1593  			continue
  1594  		}
  1595  		fmt.Fprintf(&icfg, "packagefile %s=%s\n", p1.ImportPath, a1.built)
  1596  		if p1.Shlib != "" {
  1597  			fmt.Fprintf(&icfg, "packageshlib %s=%s\n", p1.ImportPath, p1.Shlib)
  1598  		}
  1599  	}
  1600  	info := ""
  1601  	if a.Package.Internal.BuildInfo != nil {
  1602  		info = a.Package.Internal.BuildInfo.String()
  1603  	}
  1604  	fmt.Fprintf(&icfg, "modinfo %q\n", modload.ModInfoData(info))
  1605  	return b.Shell(a).writeFile(file, icfg.Bytes())
  1606  }
  1607  
  1608  // PkgconfigCmd returns a pkg-config binary name
  1609  // defaultPkgConfig is defined in zdefaultcc.go, written by cmd/dist.
  1610  func (b *Builder) PkgconfigCmd() string {
  1611  	return envList("PKG_CONFIG", cfg.DefaultPkgConfig)[0]
  1612  }
  1613  
  1614  // splitPkgConfigOutput parses the pkg-config output into a slice of flags.
  1615  // This implements the shell quoting semantics described in
  1616  // https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02,
  1617  // except that it does not support parameter or arithmetic expansion or command
  1618  // substitution and hard-codes the <blank> delimiters instead of reading them
  1619  // from LC_LOCALE.
  1620  func splitPkgConfigOutput(out []byte) ([]string, error) {
  1621  	if len(out) == 0 {
  1622  		return nil, nil
  1623  	}
  1624  	var flags []string
  1625  	flag := make([]byte, 0, len(out))
  1626  	didQuote := false // was the current flag parsed from a quoted string?
  1627  	escaped := false  // did we just read `\` in a non-single-quoted context?
  1628  	quote := byte(0)  // what is the quote character around the current string?
  1629  
  1630  	for _, c := range out {
  1631  		if escaped {
  1632  			if quote == '"' {
  1633  				// “The <backslash> shall retain its special meaning as an escape
  1634  				// character … only when followed by one of the following characters
  1635  				// when considered special:”
  1636  				switch c {
  1637  				case '$', '`', '"', '\\', '\n':
  1638  					// Handle the escaped character normally.
  1639  				default:
  1640  					// Not an escape character after all.
  1641  					flag = append(flag, '\\', c)
  1642  					escaped = false
  1643  					continue
  1644  				}
  1645  			}
  1646  
  1647  			if c == '\n' {
  1648  				// “If a <newline> follows the <backslash>, the shell shall interpret
  1649  				// this as line continuation.”
  1650  			} else {
  1651  				flag = append(flag, c)
  1652  			}
  1653  			escaped = false
  1654  			continue
  1655  		}
  1656  
  1657  		if quote != 0 && c == quote {
  1658  			quote = 0
  1659  			continue
  1660  		}
  1661  		switch quote {
  1662  		case '\'':
  1663  			// “preserve the literal value of each character”
  1664  			flag = append(flag, c)
  1665  			continue
  1666  		case '"':
  1667  			// “preserve the literal value of all characters within the double-quotes,
  1668  			// with the exception of …”
  1669  			switch c {
  1670  			case '`', '$', '\\':
  1671  			default:
  1672  				flag = append(flag, c)
  1673  				continue
  1674  			}
  1675  		}
  1676  
  1677  		// “The application shall quote the following characters if they are to
  1678  		// represent themselves:”
  1679  		switch c {
  1680  		case '|', '&', ';', '<', '>', '(', ')', '$', '`':
  1681  			return nil, fmt.Errorf("unexpected shell character %q in pkgconf output", c)
  1682  
  1683  		case '\\':
  1684  			// “A <backslash> that is not quoted shall preserve the literal value of
  1685  			// the following character, with the exception of a <newline>.”
  1686  			escaped = true
  1687  			continue
  1688  
  1689  		case '"', '\'':
  1690  			quote = c
  1691  			didQuote = true
  1692  			continue
  1693  
  1694  		case ' ', '\t', '\n':
  1695  			if len(flag) > 0 || didQuote {
  1696  				flags = append(flags, string(flag))
  1697  			}
  1698  			flag, didQuote = flag[:0], false
  1699  			continue
  1700  		}
  1701  
  1702  		flag = append(flag, c)
  1703  	}
  1704  
  1705  	// Prefer to report a missing quote instead of a missing escape. If the string
  1706  	// is something like `"foo\`, it's ambiguous as to whether the trailing
  1707  	// backslash is really an escape at all.
  1708  	if quote != 0 {
  1709  		return nil, errors.New("unterminated quoted string in pkgconf output")
  1710  	}
  1711  	if escaped {
  1712  		return nil, errors.New("broken character escaping in pkgconf output")
  1713  	}
  1714  
  1715  	if len(flag) > 0 || didQuote {
  1716  		flags = append(flags, string(flag))
  1717  	}
  1718  	return flags, nil
  1719  }
  1720  
  1721  // Calls pkg-config if needed and returns the cflags/ldflags needed to build a's package.
  1722  func (b *Builder) getPkgConfigFlags(a *Action, p *load.Package) (cflags, ldflags []string, err error) {
  1723  	sh := b.Shell(a)
  1724  	if pcargs := p.CgoPkgConfig; len(pcargs) > 0 {
  1725  		// pkg-config permits arguments to appear anywhere in
  1726  		// the command line. Move them all to the front, before --.
  1727  		var pcflags []string
  1728  		var pkgs []string
  1729  		for _, pcarg := range pcargs {
  1730  			if pcarg == "--" {
  1731  				// We're going to add our own "--" argument.
  1732  			} else if strings.HasPrefix(pcarg, "--") {
  1733  				pcflags = append(pcflags, pcarg)
  1734  			} else {
  1735  				pkgs = append(pkgs, pcarg)
  1736  			}
  1737  		}
  1738  		for _, pkg := range pkgs {
  1739  			if !load.SafeArg(pkg) {
  1740  				return nil, nil, fmt.Errorf("invalid pkg-config package name: %s", pkg)
  1741  			}
  1742  		}
  1743  		var out []byte
  1744  		out, err = sh.runOut(p.Dir, nil, b.PkgconfigCmd(), "--cflags", pcflags, "--", pkgs)
  1745  		if err != nil {
  1746  			desc := b.PkgconfigCmd() + " --cflags " + strings.Join(pcflags, " ") + " -- " + strings.Join(pkgs, " ")
  1747  			return nil, nil, sh.reportCmd(desc, "", out, err)
  1748  		}
  1749  		if len(out) > 0 {
  1750  			cflags, err = splitPkgConfigOutput(bytes.TrimSpace(out))
  1751  			if err != nil {
  1752  				return nil, nil, err
  1753  			}
  1754  			if err := checkCompilerFlags("CFLAGS", "pkg-config --cflags", cflags); err != nil {
  1755  				return nil, nil, err
  1756  			}
  1757  		}
  1758  		out, err = sh.runOut(p.Dir, nil, b.PkgconfigCmd(), "--libs", pcflags, "--", pkgs)
  1759  		if err != nil {
  1760  			desc := b.PkgconfigCmd() + " --libs " + strings.Join(pcflags, " ") + " -- " + strings.Join(pkgs, " ")
  1761  			return nil, nil, sh.reportCmd(desc, "", out, err)
  1762  		}
  1763  		if len(out) > 0 {
  1764  			// We need to handle path with spaces so that C:/Program\ Files can pass
  1765  			// checkLinkerFlags. Use splitPkgConfigOutput here just like we treat cflags.
  1766  			ldflags, err = splitPkgConfigOutput(bytes.TrimSpace(out))
  1767  			if err != nil {
  1768  				return nil, nil, err
  1769  			}
  1770  			if err := checkLinkerFlags("LDFLAGS", "pkg-config --libs", ldflags); err != nil {
  1771  				return nil, nil, err
  1772  			}
  1773  		}
  1774  	}
  1775  
  1776  	return
  1777  }
  1778  
  1779  func (b *Builder) installShlibname(ctx context.Context, a *Action) error {
  1780  	if err := AllowInstall(a); err != nil {
  1781  		return err
  1782  	}
  1783  
  1784  	sh := b.Shell(a)
  1785  	a1 := a.Deps[0]
  1786  	if !cfg.BuildN {
  1787  		if err := sh.Mkdir(filepath.Dir(a.Target)); err != nil {
  1788  			return err
  1789  		}
  1790  	}
  1791  	return sh.writeFile(a.Target, []byte(filepath.Base(a1.Target)+"\n"))
  1792  }
  1793  
  1794  func (b *Builder) linkSharedActionID(a *Action) cache.ActionID {
  1795  	h := cache.NewHash("linkShared")
  1796  
  1797  	// Toolchain-independent configuration.
  1798  	fmt.Fprintf(h, "linkShared\n")
  1799  	fmt.Fprintf(h, "goos %s goarch %s\n", cfg.Goos, cfg.Goarch)
  1800  
  1801  	// Toolchain-dependent configuration, shared with b.linkActionID.
  1802  	b.printLinkerConfig(h, nil)
  1803  
  1804  	// Input files.
  1805  	for _, a1 := range a.Deps {
  1806  		p1 := a1.Package
  1807  		if a1.built == "" {
  1808  			continue
  1809  		}
  1810  		if p1 != nil {
  1811  			fmt.Fprintf(h, "packagefile %s=%s\n", p1.ImportPath, contentID(b.buildID(a1.built)))
  1812  			if p1.Shlib != "" {
  1813  				fmt.Fprintf(h, "packageshlib %s=%s\n", p1.ImportPath, contentID(b.buildID(p1.Shlib)))
  1814  			}
  1815  		}
  1816  	}
  1817  	// Files named on command line are special.
  1818  	for _, a1 := range a.Deps[0].Deps {
  1819  		p1 := a1.Package
  1820  		fmt.Fprintf(h, "top %s=%s\n", p1.ImportPath, contentID(b.buildID(a1.built)))
  1821  	}
  1822  
  1823  	return h.Sum()
  1824  }
  1825  
  1826  func (b *Builder) linkShared(ctx context.Context, a *Action) (err error) {
  1827  	if b.useCache(a, b.linkSharedActionID(a), a.Target, !b.IsCmdList) || b.IsCmdList {
  1828  		return nil
  1829  	}
  1830  	defer b.flushOutput(a)
  1831  
  1832  	if err := AllowInstall(a); err != nil {
  1833  		return err
  1834  	}
  1835  
  1836  	if err := b.Shell(a).Mkdir(a.Objdir); err != nil {
  1837  		return err
  1838  	}
  1839  
  1840  	importcfg := a.Objdir + "importcfg.link"
  1841  	if err := b.writeLinkImportcfg(a, importcfg); err != nil {
  1842  		return err
  1843  	}
  1844  
  1845  	// TODO(rsc): There is a missing updateBuildID here,
  1846  	// but we have to decide where to store the build ID in these files.
  1847  	a.built = a.Target
  1848  	return BuildToolchain.ldShared(b, a, a.Deps[0].Deps, a.Target, importcfg, a.Deps)
  1849  }
  1850  
  1851  // BuildInstallFunc is the action for installing a single package or executable.
  1852  func BuildInstallFunc(b *Builder, ctx context.Context, a *Action) (err error) {
  1853  	defer func() {
  1854  		if err != nil {
  1855  			// a.Package == nil is possible for the go install -buildmode=shared
  1856  			// action that installs libmangledname.so, which corresponds to
  1857  			// a list of packages, not just one.
  1858  			sep, path := "", ""
  1859  			if a.Package != nil {
  1860  				sep, path = " ", a.Package.ImportPath
  1861  			}
  1862  			err = fmt.Errorf("go %s%s%s: %v", cfg.CmdName, sep, path, err)
  1863  		}
  1864  	}()
  1865  	sh := b.Shell(a)
  1866  
  1867  	a1 := a.Deps[0]
  1868  	a.buildID = a1.buildID
  1869  	if a.json != nil {
  1870  		a.json.BuildID = a.buildID
  1871  	}
  1872  
  1873  	// If we are using the eventual install target as an up-to-date
  1874  	// cached copy of the thing we built, then there's no need to
  1875  	// copy it into itself (and that would probably fail anyway).
  1876  	// In this case a1.built == a.Target because a1.built == p.Target,
  1877  	// so the built target is not in the a1.Objdir tree that b.cleanup(a1) removes.
  1878  	if a1.built == a.Target {
  1879  		a.built = a.Target
  1880  		if !a.buggyInstall {
  1881  			b.cleanup(a1)
  1882  		}
  1883  		// Whether we're smart enough to avoid a complete rebuild
  1884  		// depends on exactly what the staleness and rebuild algorithms
  1885  		// are, as well as potentially the state of the Go build cache.
  1886  		// We don't really want users to be able to infer (or worse start depending on)
  1887  		// those details from whether the modification time changes during
  1888  		// "go install", so do a best-effort update of the file times to make it
  1889  		// look like we rewrote a.Target even if we did not. Updating the mtime
  1890  		// may also help other mtime-based systems that depend on our
  1891  		// previous mtime updates that happened more often.
  1892  		// This is still not perfect - we ignore the error result, and if the file was
  1893  		// unwritable for some reason then pretending to have written it is also
  1894  		// confusing - but it's probably better than not doing the mtime update.
  1895  		//
  1896  		// But don't do that for the special case where building an executable
  1897  		// with -linkshared implicitly installs all its dependent libraries.
  1898  		// We want to hide that awful detail as much as possible, so don't
  1899  		// advertise it by touching the mtimes (usually the libraries are up
  1900  		// to date).
  1901  		if !a.buggyInstall && !b.IsCmdList {
  1902  			if cfg.BuildN {
  1903  				sh.ShowCmd("", "touch %s", a.Target)
  1904  			} else if err := AllowInstall(a); err == nil {
  1905  				now := time.Now()
  1906  				os.Chtimes(a.Target, now, now)
  1907  			}
  1908  		}
  1909  		return nil
  1910  	}
  1911  
  1912  	// If we're building for go list -export,
  1913  	// never install anything; just keep the cache reference.
  1914  	if b.IsCmdList {
  1915  		a.built = a1.built
  1916  		return nil
  1917  	}
  1918  	if err := AllowInstall(a); err != nil {
  1919  		return err
  1920  	}
  1921  
  1922  	if err := sh.Mkdir(a.Objdir); err != nil {
  1923  		return err
  1924  	}
  1925  
  1926  	perm := fs.FileMode(0666)
  1927  	if a1.Mode == "link" {
  1928  		switch cfg.BuildBuildmode {
  1929  		case "c-archive", "c-shared", "plugin":
  1930  		default:
  1931  			perm = 0777
  1932  		}
  1933  	}
  1934  
  1935  	// make target directory
  1936  	dir, _ := filepath.Split(a.Target)
  1937  	if dir != "" {
  1938  		if err := sh.Mkdir(dir); err != nil {
  1939  			return err
  1940  		}
  1941  	}
  1942  
  1943  	if !a.buggyInstall {
  1944  		defer b.cleanup(a1)
  1945  	}
  1946  
  1947  	return sh.moveOrCopyFile(a.Target, a1.built, perm, false)
  1948  }
  1949  
  1950  // AllowInstall returns a non-nil error if this invocation of the go command is
  1951  // allowed to install a.Target.
  1952  //
  1953  // The build of cmd/go running under its own test is forbidden from installing
  1954  // to its original GOROOT. The var is exported so it can be set by TestMain.
  1955  var AllowInstall = func(*Action) error { return nil }
  1956  
  1957  // cleanup removes a's object dir to keep the amount of
  1958  // on-disk garbage down in a large build. On an operating system
  1959  // with aggressive buffering, cleaning incrementally like
  1960  // this keeps the intermediate objects from hitting the disk.
  1961  func (b *Builder) cleanup(a *Action) {
  1962  	if !cfg.BuildWork {
  1963  		b.Shell(a).RemoveAll(a.Objdir)
  1964  	}
  1965  }
  1966  
  1967  // Install the cgo export header file, if there is one.
  1968  func (b *Builder) installHeader(ctx context.Context, a *Action) error {
  1969  	sh := b.Shell(a)
  1970  
  1971  	src := a.Objdir + "_cgo_install.h"
  1972  	if _, err := os.Stat(src); os.IsNotExist(err) {
  1973  		// If the file does not exist, there are no exported
  1974  		// functions, and we do not install anything.
  1975  		// TODO(rsc): Once we know that caching is rebuilding
  1976  		// at the right times (not missing rebuilds), here we should
  1977  		// probably delete the installed header, if any.
  1978  		if cfg.BuildX {
  1979  			sh.ShowCmd("", "# %s not created", src)
  1980  		}
  1981  		return nil
  1982  	}
  1983  
  1984  	if err := AllowInstall(a); err != nil {
  1985  		return err
  1986  	}
  1987  
  1988  	dir, _ := filepath.Split(a.Target)
  1989  	if dir != "" {
  1990  		if err := sh.Mkdir(dir); err != nil {
  1991  			return err
  1992  		}
  1993  	}
  1994  
  1995  	return sh.moveOrCopyFile(a.Target, src, 0666, true)
  1996  }
  1997  
  1998  // cover runs, in effect,
  1999  //
  2000  //	go tool cover -pkgcfg=<config file> -mode=b.coverMode -var="varName" -o <outfiles> <infiles>
  2001  //
  2002  // Return value is an updated output files list; in addition to the
  2003  // regular outputs (instrumented source files) the cover tool also
  2004  // writes a separate file (appearing first in the list of outputs)
  2005  // that will contain coverage counters and meta-data.
  2006  func (b *Builder) cover(a *Action, infiles, outfiles []string, varName string, mode string) ([]string, error) {
  2007  	pkgcfg := a.Objdir + "pkgcfg.txt"
  2008  	covoutputs := a.Objdir + "coveroutfiles.txt"
  2009  	odir := filepath.Dir(outfiles[0])
  2010  	cv := filepath.Join(odir, "covervars.go")
  2011  	outfiles = append([]string{cv}, outfiles...)
  2012  	if err := b.writeCoverPkgInputs(a, pkgcfg, covoutputs, outfiles); err != nil {
  2013  		return nil, err
  2014  	}
  2015  	args := []string{base.Tool("cover"),
  2016  		"-pkgcfg", pkgcfg,
  2017  		"-mode", mode,
  2018  		"-var", varName,
  2019  		"-outfilelist", covoutputs,
  2020  	}
  2021  	args = append(args, infiles...)
  2022  	if err := b.Shell(a).run(a.Objdir, "", nil,
  2023  		cfg.BuildToolexec, args); err != nil {
  2024  		return nil, err
  2025  	}
  2026  	return outfiles, nil
  2027  }
  2028  
  2029  func (b *Builder) writeCoverPkgInputs(a *Action, pconfigfile string, covoutputsfile string, outfiles []string) error {
  2030  	sh := b.Shell(a)
  2031  	p := a.Package
  2032  	p.Internal.Cover.Cfg = a.Objdir + "coveragecfg"
  2033  	pcfg := covcmd.CoverPkgConfig{
  2034  		PkgPath: p.ImportPath,
  2035  		PkgName: p.Name,
  2036  		// Note: coverage granularity is currently hard-wired to
  2037  		// 'perblock'; there isn't a way using "go build -cover" or "go
  2038  		// test -cover" to select it. This may change in the future
  2039  		// depending on user demand.
  2040  		Granularity: "perblock",
  2041  		OutConfig:   p.Internal.Cover.Cfg,
  2042  		Local:       p.Internal.Local,
  2043  	}
  2044  	if ca, ok := a.Actor.(*coverActor); ok && ca.covMetaFileName != "" {
  2045  		pcfg.EmitMetaFile = a.Objdir + ca.covMetaFileName
  2046  	}
  2047  	if a.Package.Module != nil {
  2048  		pcfg.ModulePath = a.Package.Module.Path
  2049  	}
  2050  	data, err := json.Marshal(pcfg)
  2051  	if err != nil {
  2052  		return err
  2053  	}
  2054  	data = append(data, '\n')
  2055  	if err := sh.writeFile(pconfigfile, data); err != nil {
  2056  		return err
  2057  	}
  2058  	var sb strings.Builder
  2059  	for i := range outfiles {
  2060  		fmt.Fprintf(&sb, "%s\n", outfiles[i])
  2061  	}
  2062  	return sh.writeFile(covoutputsfile, []byte(sb.String()))
  2063  }
  2064  
  2065  var objectMagic = [][]byte{
  2066  	{'!', '<', 'a', 'r', 'c', 'h', '>', '\n'}, // Package archive
  2067  	{'<', 'b', 'i', 'g', 'a', 'f', '>', '\n'}, // Package AIX big archive
  2068  	{'\x7F', 'E', 'L', 'F'},                   // ELF
  2069  	{0xFE, 0xED, 0xFA, 0xCE},                  // Mach-O big-endian 32-bit
  2070  	{0xFE, 0xED, 0xFA, 0xCF},                  // Mach-O big-endian 64-bit
  2071  	{0xCE, 0xFA, 0xED, 0xFE},                  // Mach-O little-endian 32-bit
  2072  	{0xCF, 0xFA, 0xED, 0xFE},                  // Mach-O little-endian 64-bit
  2073  	{0x4d, 0x5a, 0x90, 0x00, 0x03, 0x00},      // PE (Windows) as generated by 6l/8l and gcc
  2074  	{0x4d, 0x5a, 0x78, 0x00, 0x01, 0x00},      // PE (Windows) as generated by llvm for dll
  2075  	{0x00, 0x00, 0x01, 0xEB},                  // Plan 9 i386
  2076  	{0x00, 0x00, 0x8a, 0x97},                  // Plan 9 amd64
  2077  	{0x00, 0x00, 0x06, 0x47},                  // Plan 9 arm
  2078  	{0x00, 0x61, 0x73, 0x6D},                  // WASM
  2079  	{0x01, 0xDF},                              // XCOFF 32bit
  2080  	{0x01, 0xF7},                              // XCOFF 64bit
  2081  }
  2082  
  2083  func isObject(s string) bool {
  2084  	f, err := os.Open(s)
  2085  	if err != nil {
  2086  		return false
  2087  	}
  2088  	defer f.Close()
  2089  	buf := make([]byte, 64)
  2090  	io.ReadFull(f, buf)
  2091  	for _, magic := range objectMagic {
  2092  		if bytes.HasPrefix(buf, magic) {
  2093  			return true
  2094  		}
  2095  	}
  2096  	return false
  2097  }
  2098  
  2099  // cCompilerEnv returns environment variables to set when running the
  2100  // C compiler. This is needed to disable escape codes in clang error
  2101  // messages that confuse tools like cgo.
  2102  func (b *Builder) cCompilerEnv() []string {
  2103  	return []string{"TERM=dumb"}
  2104  }
  2105  
  2106  // mkAbs returns an absolute path corresponding to
  2107  // evaluating f in the directory dir.
  2108  // We always pass absolute paths of source files so that
  2109  // the error messages will include the full path to a file
  2110  // in need of attention.
  2111  func mkAbs(dir, f string) string {
  2112  	// Leave absolute paths alone.
  2113  	// Also, during -n mode we use the pseudo-directory $WORK
  2114  	// instead of creating an actual work directory that won't be used.
  2115  	// Leave paths beginning with $WORK alone too.
  2116  	if filepath.IsAbs(f) || strings.HasPrefix(f, "$WORK") {
  2117  		return f
  2118  	}
  2119  	return filepath.Join(dir, f)
  2120  }
  2121  
  2122  type toolchain interface {
  2123  	// gc runs the compiler in a specific directory on a set of files
  2124  	// and returns the name of the generated output file.
  2125  	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  	// cc runs the toolchain's C compiler in a directory on a C file
  2127  	// to produce an output file.
  2128  	cc(b *Builder, a *Action, ofile, cfile string) error
  2129  	// asm runs the assembler in a specific directory on specific files
  2130  	// and returns a list of named output files.
  2131  	asm(b *Builder, a *Action, sfiles []string) ([]string, error)
  2132  	// symabis scans the symbol ABIs from sfiles and returns the
  2133  	// path to the output symbol ABIs file, or "" if none.
  2134  	symabis(b *Builder, a *Action, sfiles []string) (string, error)
  2135  	// pack runs the archive packer in a specific directory to create
  2136  	// an archive from a set of object files.
  2137  	// typically it is run in the object directory.
  2138  	pack(b *Builder, a *Action, afile string, ofiles []string) error
  2139  	// ld runs the linker to create an executable starting at mainpkg.
  2140  	ld(b *Builder, root *Action, targetPath, importcfg, mainpkg string) error
  2141  	// ldShared runs the linker to create a shared library containing the pkgs built by toplevelactions
  2142  	ldShared(b *Builder, root *Action, toplevelactions []*Action, targetPath, importcfg string, allactions []*Action) error
  2143  
  2144  	compiler() string
  2145  	linker() string
  2146  }
  2147  
  2148  type noToolchain struct{}
  2149  
  2150  func noCompiler() error {
  2151  	log.Fatalf("unknown compiler %q", cfg.BuildContext.Compiler)
  2152  	return nil
  2153  }
  2154  
  2155  func (noToolchain) compiler() string {
  2156  	noCompiler()
  2157  	return ""
  2158  }
  2159  
  2160  func (noToolchain) linker() string {
  2161  	noCompiler()
  2162  	return ""
  2163  }
  2164  
  2165  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) {
  2166  	return "", nil, noCompiler()
  2167  }
  2168  
  2169  func (noToolchain) asm(b *Builder, a *Action, sfiles []string) ([]string, error) {
  2170  	return nil, noCompiler()
  2171  }
  2172  
  2173  func (noToolchain) symabis(b *Builder, a *Action, sfiles []string) (string, error) {
  2174  	return "", noCompiler()
  2175  }
  2176  
  2177  func (noToolchain) pack(b *Builder, a *Action, afile string, ofiles []string) error {
  2178  	return noCompiler()
  2179  }
  2180  
  2181  func (noToolchain) ld(b *Builder, root *Action, targetPath, importcfg, mainpkg string) error {
  2182  	return noCompiler()
  2183  }
  2184  
  2185  func (noToolchain) ldShared(b *Builder, root *Action, toplevelactions []*Action, targetPath, importcfg string, allactions []*Action) error {
  2186  	return noCompiler()
  2187  }
  2188  
  2189  func (noToolchain) cc(b *Builder, a *Action, ofile, cfile string) error {
  2190  	return noCompiler()
  2191  }
  2192  
  2193  // gcc runs the gcc C compiler to create an object from a single C file.
  2194  func (b *Builder) gcc(a *Action, workdir, out string, flags []string, cfile string) error {
  2195  	p := a.Package
  2196  	return b.ccompile(modload.LoaderState, a, out, flags, cfile, b.GccCmd(p.Dir, workdir))
  2197  }
  2198  
  2199  // gas runs the gcc c compiler to create an object file from a single C assembly file.
  2200  func (b *Builder) gas(a *Action, workdir, out string, flags []string, sfile string) error {
  2201  	p := a.Package
  2202  	data, err := os.ReadFile(filepath.Join(p.Dir, sfile))
  2203  	if err == nil {
  2204  		if bytes.HasPrefix(data, []byte("TEXT")) || bytes.Contains(data, []byte("\nTEXT")) ||
  2205  			bytes.HasPrefix(data, []byte("DATA")) || bytes.Contains(data, []byte("\nDATA")) ||
  2206  			bytes.HasPrefix(data, []byte("GLOBL")) || bytes.Contains(data, []byte("\nGLOBL")) {
  2207  			return fmt.Errorf("package using cgo has Go assembly file %s", sfile)
  2208  		}
  2209  	}
  2210  	return b.ccompile(modload.LoaderState, a, out, flags, sfile, b.GccCmd(p.Dir, workdir))
  2211  }
  2212  
  2213  // gxx runs the g++ C++ compiler to create an object from a single C++ file.
  2214  func (b *Builder) gxx(a *Action, workdir, out string, flags []string, cxxfile string) error {
  2215  	p := a.Package
  2216  	return b.ccompile(modload.LoaderState, a, out, flags, cxxfile, b.GxxCmd(p.Dir, workdir))
  2217  }
  2218  
  2219  // gfortran runs the gfortran Fortran compiler to create an object from a single Fortran file.
  2220  func (b *Builder) gfortran(a *Action, workdir, out string, flags []string, ffile string) error {
  2221  	p := a.Package
  2222  	return b.ccompile(modload.LoaderState, a, out, flags, ffile, b.gfortranCmd(p.Dir, workdir))
  2223  }
  2224  
  2225  // ccompile runs the given C or C++ compiler and creates an object from a single source file.
  2226  func (b *Builder) ccompile(loaderstate *modload.State, a *Action, outfile string, flags []string, file string, compiler []string) error {
  2227  	p := a.Package
  2228  	sh := b.Shell(a)
  2229  	file = mkAbs(p.Dir, file)
  2230  	outfile = mkAbs(p.Dir, outfile)
  2231  
  2232  	flags = slices.Clip(flags) // If we append to flags, write to a new slice that we own.
  2233  
  2234  	// Elide source directory paths if -trimpath is set.
  2235  	// This is needed for source files (e.g., a .c file in a package directory).
  2236  	// TODO(golang.org/issue/36072): cgo also generates files with #line
  2237  	// directives pointing to the source directory. It should not generate those
  2238  	// when -trimpath is enabled.
  2239  	if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") {
  2240  		if cfg.BuildTrimpath || p.Goroot {
  2241  			prefixMapFlag := "-fdebug-prefix-map"
  2242  			if b.gccSupportsFlag(compiler, "-ffile-prefix-map=a=b") {
  2243  				prefixMapFlag = "-ffile-prefix-map"
  2244  			}
  2245  			// Keep in sync with Action.trimpath.
  2246  			// The trimmed paths are a little different, but we need to trim in mostly the
  2247  			// same situations.
  2248  			var from, toPath string
  2249  			if m := p.Module; m == nil {
  2250  				if p.Root == "" { // command-line-arguments in GOPATH mode, maybe?
  2251  					from = p.Dir
  2252  					toPath = p.ImportPath
  2253  				} else if p.Goroot {
  2254  					from = p.Root
  2255  					toPath = "GOROOT"
  2256  				} else {
  2257  					from = p.Root
  2258  					toPath = "GOPATH"
  2259  				}
  2260  			} else if m.Dir == "" {
  2261  				// The module is in the vendor directory. Replace the entire vendor
  2262  				// directory path, because the module's Dir is not filled in.
  2263  				from = modload.VendorDir(loaderstate)
  2264  				toPath = "vendor"
  2265  			} else {
  2266  				from = m.Dir
  2267  				toPath = m.Path
  2268  				if m.Version != "" {
  2269  					toPath += "@" + m.Version
  2270  				}
  2271  			}
  2272  			// -fdebug-prefix-map (or -ffile-prefix-map) requires an absolute "to"
  2273  			// path (or it joins the path  with the working directory). Pick something
  2274  			// that makes sense for the target platform.
  2275  			var to string
  2276  			if cfg.BuildContext.GOOS == "windows" {
  2277  				to = filepath.Join(`\\_\_`, toPath)
  2278  			} else {
  2279  				to = filepath.Join("/_", toPath)
  2280  			}
  2281  			flags = append(slices.Clip(flags), prefixMapFlag+"="+from+"="+to)
  2282  		}
  2283  	}
  2284  
  2285  	// Tell gcc to not insert truly random numbers into the build process
  2286  	// this ensures LTO won't create random numbers for symbols.
  2287  	if b.gccSupportsFlag(compiler, "-frandom-seed=1") {
  2288  		flags = append(flags, "-frandom-seed="+buildid.HashToString(a.actionID))
  2289  	}
  2290  
  2291  	overlayPath := file
  2292  	if p, ok := a.nonGoOverlay[overlayPath]; ok {
  2293  		overlayPath = p
  2294  	}
  2295  	output, err := sh.runOut(filepath.Dir(overlayPath), b.cCompilerEnv(), compiler, flags, "-o", outfile, "-c", filepath.Base(overlayPath))
  2296  
  2297  	// On FreeBSD 11, when we pass -g to clang 3.8 it
  2298  	// invokes its internal assembler with -dwarf-version=2.
  2299  	// When it sees .section .note.GNU-stack, it warns
  2300  	// "DWARF2 only supports one section per compilation unit".
  2301  	// This warning makes no sense, since the section is empty,
  2302  	// but it confuses people.
  2303  	// We work around the problem by detecting the warning
  2304  	// and dropping -g and trying again.
  2305  	if bytes.Contains(output, []byte("DWARF2 only supports one section per compilation unit")) {
  2306  		newFlags := make([]string, 0, len(flags))
  2307  		for _, f := range flags {
  2308  			if !strings.HasPrefix(f, "-g") {
  2309  				newFlags = append(newFlags, f)
  2310  			}
  2311  		}
  2312  		if len(newFlags) < len(flags) {
  2313  			return b.ccompile(loaderstate, a, outfile, newFlags, file, compiler)
  2314  		}
  2315  	}
  2316  
  2317  	if len(output) > 0 && err == nil && os.Getenv("GO_BUILDER_NAME") != "" {
  2318  		output = append(output, "C compiler warning promoted to error on Go builders\n"...)
  2319  		err = errors.New("warning promoted to error")
  2320  	}
  2321  
  2322  	return sh.reportCmd("", "", output, err)
  2323  }
  2324  
  2325  // gccld runs the gcc linker to create an executable from a set of object files.
  2326  func (b *Builder) gccld(a *Action, objdir, outfile string, flags []string, objs []string) error {
  2327  	p := a.Package
  2328  	sh := b.Shell(a)
  2329  	var cmd []string
  2330  	if len(p.CXXFiles) > 0 || len(p.SwigCXXFiles) > 0 {
  2331  		cmd = b.GxxCmd(p.Dir, objdir)
  2332  	} else {
  2333  		cmd = b.GccCmd(p.Dir, objdir)
  2334  	}
  2335  
  2336  	cmdargs := []any{cmd, "-o", outfile, objs, flags}
  2337  	_, err := sh.runOut(base.Cwd(), b.cCompilerEnv(), cmdargs...)
  2338  
  2339  	// Note that failure is an expected outcome here, so we report output only
  2340  	// in debug mode and don't report the error.
  2341  	if cfg.BuildN || cfg.BuildX {
  2342  		saw := "succeeded"
  2343  		if err != nil {
  2344  			saw = "failed"
  2345  		}
  2346  		sh.ShowCmd("", "%s # test for internal linking errors (%s)", joinUnambiguously(str.StringList(cmdargs...)), saw)
  2347  	}
  2348  
  2349  	return err
  2350  }
  2351  
  2352  // GccCmd returns a gcc command line prefix
  2353  // defaultCC is defined in zdefaultcc.go, written by cmd/dist.
  2354  func (b *Builder) GccCmd(incdir, workdir string) []string {
  2355  	return b.compilerCmd(b.ccExe(), incdir, workdir)
  2356  }
  2357  
  2358  // GxxCmd returns a g++ command line prefix
  2359  // defaultCXX is defined in zdefaultcc.go, written by cmd/dist.
  2360  func (b *Builder) GxxCmd(incdir, workdir string) []string {
  2361  	return b.compilerCmd(b.cxxExe(), incdir, workdir)
  2362  }
  2363  
  2364  // gfortranCmd returns a gfortran command line prefix.
  2365  func (b *Builder) gfortranCmd(incdir, workdir string) []string {
  2366  	return b.compilerCmd(b.fcExe(), incdir, workdir)
  2367  }
  2368  
  2369  // ccExe returns the CC compiler setting without all the extra flags we add implicitly.
  2370  func (b *Builder) ccExe() []string {
  2371  	return envList("CC", cfg.DefaultCC(cfg.Goos, cfg.Goarch))
  2372  }
  2373  
  2374  // cxxExe returns the CXX compiler setting without all the extra flags we add implicitly.
  2375  func (b *Builder) cxxExe() []string {
  2376  	return envList("CXX", cfg.DefaultCXX(cfg.Goos, cfg.Goarch))
  2377  }
  2378  
  2379  // fcExe returns the FC compiler setting without all the extra flags we add implicitly.
  2380  func (b *Builder) fcExe() []string {
  2381  	return envList("FC", "gfortran")
  2382  }
  2383  
  2384  // compilerCmd returns a command line prefix for the given environment
  2385  // variable and using the default command when the variable is empty.
  2386  func (b *Builder) compilerCmd(compiler []string, incdir, workdir string) []string {
  2387  	a := append(compiler, "-I", incdir)
  2388  
  2389  	// Definitely want -fPIC but on Windows gcc complains
  2390  	// "-fPIC ignored for target (all code is position independent)"
  2391  	if cfg.Goos != "windows" {
  2392  		a = append(a, "-fPIC")
  2393  	}
  2394  	a = append(a, b.gccArchArgs()...)
  2395  	// gcc-4.5 and beyond require explicit "-pthread" flag
  2396  	// for multithreading with pthread library.
  2397  	if cfg.BuildContext.CgoEnabled {
  2398  		switch cfg.Goos {
  2399  		case "windows":
  2400  			a = append(a, "-mthreads")
  2401  		default:
  2402  			a = append(a, "-pthread")
  2403  		}
  2404  	}
  2405  
  2406  	if cfg.Goos == "aix" {
  2407  		// mcmodel=large must always be enabled to allow large TOC.
  2408  		a = append(a, "-mcmodel=large")
  2409  	}
  2410  
  2411  	// disable ASCII art in clang errors, if possible
  2412  	if b.gccSupportsFlag(compiler, "-fno-caret-diagnostics") {
  2413  		a = append(a, "-fno-caret-diagnostics")
  2414  	}
  2415  	// clang is too smart about command-line arguments
  2416  	if b.gccSupportsFlag(compiler, "-Qunused-arguments") {
  2417  		a = append(a, "-Qunused-arguments")
  2418  	}
  2419  
  2420  	// zig cc passes --gc-sections to the underlying linker, which then causes
  2421  	// undefined symbol errors when compiling with cgo but without C code.
  2422  	// https://github.com/golang/go/issues/52690
  2423  	if b.gccSupportsFlag(compiler, "-Wl,--no-gc-sections") {
  2424  		a = append(a, "-Wl,--no-gc-sections")
  2425  	}
  2426  
  2427  	// disable word wrapping in error messages
  2428  	a = append(a, "-fmessage-length=0")
  2429  
  2430  	// Tell gcc not to include the work directory in object files.
  2431  	if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") {
  2432  		if workdir == "" {
  2433  			workdir = b.WorkDir
  2434  		}
  2435  		workdir = strings.TrimSuffix(workdir, string(filepath.Separator))
  2436  		if b.gccSupportsFlag(compiler, "-ffile-prefix-map=a=b") {
  2437  			a = append(a, "-ffile-prefix-map="+workdir+"=/tmp/go-build")
  2438  		} else {
  2439  			a = append(a, "-fdebug-prefix-map="+workdir+"=/tmp/go-build")
  2440  		}
  2441  	}
  2442  
  2443  	// Tell gcc not to include flags in object files, which defeats the
  2444  	// point of -fdebug-prefix-map above.
  2445  	if b.gccSupportsFlag(compiler, "-gno-record-gcc-switches") {
  2446  		a = append(a, "-gno-record-gcc-switches")
  2447  	}
  2448  
  2449  	// On OS X, some of the compilers behave as if -fno-common
  2450  	// is always set, and the Mach-O linker in 6l/8l assumes this.
  2451  	// See https://golang.org/issue/3253.
  2452  	if cfg.Goos == "darwin" || cfg.Goos == "ios" {
  2453  		a = append(a, "-fno-common")
  2454  	}
  2455  
  2456  	return a
  2457  }
  2458  
  2459  // gccNoPie returns the flag to use to request non-PIE. On systems
  2460  // with PIE (position independent executables) enabled by default,
  2461  // -no-pie must be passed when doing a partial link with -Wl,-r.
  2462  // But -no-pie is not supported by all compilers, and clang spells it -nopie.
  2463  func (b *Builder) gccNoPie(linker []string) string {
  2464  	if b.gccSupportsFlag(linker, "-no-pie") {
  2465  		return "-no-pie"
  2466  	}
  2467  	if b.gccSupportsFlag(linker, "-nopie") {
  2468  		return "-nopie"
  2469  	}
  2470  	return ""
  2471  }
  2472  
  2473  // gccSupportsFlag checks to see if the compiler supports a flag.
  2474  func (b *Builder) gccSupportsFlag(compiler []string, flag string) bool {
  2475  	// We use the background shell for operations here because, while this is
  2476  	// triggered by some Action, it's not really about that Action, and often we
  2477  	// just get the results from the global cache.
  2478  	sh := b.BackgroundShell()
  2479  
  2480  	key := [2]string{compiler[0], flag}
  2481  
  2482  	// We used to write an empty C file, but that gets complicated with go
  2483  	// build -n. We tried using a file that does not exist, but that fails on
  2484  	// systems with GCC version 4.2.1; that is the last GPLv2 version of GCC,
  2485  	// so some systems have frozen on it. Now we pass an empty file on stdin,
  2486  	// which should work at least for GCC and clang.
  2487  	//
  2488  	// If the argument is "-Wl,", then it is testing the linker. In that case,
  2489  	// skip "-c". If it's not "-Wl,", then we are testing the compiler and can
  2490  	// omit the linking step with "-c".
  2491  	//
  2492  	// Using the same CFLAGS/LDFLAGS here and for building the program.
  2493  
  2494  	// On the iOS builder the command
  2495  	//   $CC -Wl,--no-gc-sections -x c - -o /dev/null < /dev/null
  2496  	// is failing with:
  2497  	//   Unable to remove existing file: Invalid argument
  2498  	tmp := os.DevNull
  2499  	if runtime.GOOS == "windows" || runtime.GOOS == "ios" {
  2500  		f, err := os.CreateTemp(b.WorkDir, "")
  2501  		if err != nil {
  2502  			return false
  2503  		}
  2504  		f.Close()
  2505  		tmp = f.Name()
  2506  		defer os.Remove(tmp)
  2507  	}
  2508  
  2509  	cmdArgs := str.StringList(compiler, flag)
  2510  	if strings.HasPrefix(flag, "-Wl,") /* linker flag */ {
  2511  		ldflags, err := buildFlags("LDFLAGS", DefaultCFlags, nil, checkLinkerFlags)
  2512  		if err != nil {
  2513  			return false
  2514  		}
  2515  		cmdArgs = append(cmdArgs, ldflags...)
  2516  	} else { /* compiler flag, add "-c" */
  2517  		cflags, err := buildFlags("CFLAGS", DefaultCFlags, nil, checkCompilerFlags)
  2518  		if err != nil {
  2519  			return false
  2520  		}
  2521  		cmdArgs = append(cmdArgs, cflags...)
  2522  		cmdArgs = append(cmdArgs, "-c")
  2523  	}
  2524  
  2525  	cmdArgs = append(cmdArgs, "-x", "c", "-", "-o", tmp)
  2526  
  2527  	if cfg.BuildN {
  2528  		sh.ShowCmd(b.WorkDir, "%s || true", joinUnambiguously(cmdArgs))
  2529  		return false
  2530  	}
  2531  
  2532  	// gccCompilerID acquires b.exec, so do before acquiring lock.
  2533  	compilerID, cacheOK := b.gccCompilerID(compiler[0])
  2534  
  2535  	b.exec.Lock()
  2536  	defer b.exec.Unlock()
  2537  	if b, ok := b.flagCache[key]; ok {
  2538  		return b
  2539  	}
  2540  	if b.flagCache == nil {
  2541  		b.flagCache = make(map[[2]string]bool)
  2542  	}
  2543  
  2544  	// Look in build cache.
  2545  	var flagID cache.ActionID
  2546  	if cacheOK {
  2547  		flagID = cache.Subkey(compilerID, "gccSupportsFlag "+flag)
  2548  		if data, _, err := cache.GetBytes(cache.Default(), flagID); err == nil {
  2549  			supported := string(data) == "true"
  2550  			b.flagCache[key] = supported
  2551  			return supported
  2552  		}
  2553  	}
  2554  
  2555  	if cfg.BuildX {
  2556  		sh.ShowCmd(b.WorkDir, "%s || true", joinUnambiguously(cmdArgs))
  2557  	}
  2558  	cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
  2559  	cmd.Dir = b.WorkDir
  2560  	cmd.Env = append(cmd.Environ(), "LC_ALL=C")
  2561  	out, _ := cmd.CombinedOutput()
  2562  	// GCC says "unrecognized command line option".
  2563  	// clang says "unknown argument".
  2564  	// tcc says "unsupported"
  2565  	// AIX says "not recognized"
  2566  	// Older versions of GCC say "unrecognised debug output level".
  2567  	// For -fsplit-stack GCC says "'-fsplit-stack' is not supported".
  2568  	supported := !bytes.Contains(out, []byte("unrecognized")) &&
  2569  		!bytes.Contains(out, []byte("unknown")) &&
  2570  		!bytes.Contains(out, []byte("unrecognised")) &&
  2571  		!bytes.Contains(out, []byte("is not supported")) &&
  2572  		!bytes.Contains(out, []byte("not recognized")) &&
  2573  		!bytes.Contains(out, []byte("unsupported"))
  2574  
  2575  	if cacheOK {
  2576  		s := "false"
  2577  		if supported {
  2578  			s = "true"
  2579  		}
  2580  		cache.PutBytes(cache.Default(), flagID, []byte(s))
  2581  	}
  2582  
  2583  	b.flagCache[key] = supported
  2584  	return supported
  2585  }
  2586  
  2587  // statString returns a string form of an os.FileInfo, for serializing and comparison.
  2588  func statString(info os.FileInfo) string {
  2589  	return fmt.Sprintf("stat %d %x %v %v\n", info.Size(), uint64(info.Mode()), info.ModTime(), info.IsDir())
  2590  }
  2591  
  2592  // gccCompilerID returns a build cache key for the current gcc,
  2593  // as identified by running 'compiler'.
  2594  // The caller can use subkeys of the key.
  2595  // Other parts of cmd/go can use the id as a hash
  2596  // of the installed compiler version.
  2597  func (b *Builder) gccCompilerID(compiler string) (id cache.ActionID, ok bool) {
  2598  	// We use the background shell for operations here because, while this is
  2599  	// triggered by some Action, it's not really about that Action, and often we
  2600  	// just get the results from the global cache.
  2601  	sh := b.BackgroundShell()
  2602  
  2603  	if cfg.BuildN {
  2604  		sh.ShowCmd(b.WorkDir, "%s || true", joinUnambiguously([]string{compiler, "--version"}))
  2605  		return cache.ActionID{}, false
  2606  	}
  2607  
  2608  	b.exec.Lock()
  2609  	defer b.exec.Unlock()
  2610  
  2611  	if id, ok := b.gccCompilerIDCache[compiler]; ok {
  2612  		return id, ok
  2613  	}
  2614  
  2615  	// We hash the compiler's full path to get a cache entry key.
  2616  	// That cache entry holds a validation description,
  2617  	// which is of the form:
  2618  	//
  2619  	//	filename \x00 statinfo \x00
  2620  	//	...
  2621  	//	compiler id
  2622  	//
  2623  	// If os.Stat of each filename matches statinfo,
  2624  	// then the entry is still valid, and we can use the
  2625  	// compiler id without any further expense.
  2626  	//
  2627  	// Otherwise, we compute a new validation description
  2628  	// and compiler id (below).
  2629  	exe, err := pathcache.LookPath(compiler)
  2630  	if err != nil {
  2631  		return cache.ActionID{}, false
  2632  	}
  2633  
  2634  	h := cache.NewHash("gccCompilerID")
  2635  	fmt.Fprintf(h, "gccCompilerID %q", exe)
  2636  	key := h.Sum()
  2637  	data, _, err := cache.GetBytes(cache.Default(), key)
  2638  	if err == nil && len(data) > len(id) {
  2639  		stats := strings.Split(string(data[:len(data)-len(id)]), "\x00")
  2640  		if len(stats)%2 != 0 {
  2641  			goto Miss
  2642  		}
  2643  		for i := 0; i+2 <= len(stats); i++ {
  2644  			info, err := os.Stat(stats[i])
  2645  			if err != nil || statString(info) != stats[i+1] {
  2646  				goto Miss
  2647  			}
  2648  		}
  2649  		copy(id[:], data[len(data)-len(id):])
  2650  		return id, true
  2651  	Miss:
  2652  	}
  2653  
  2654  	// Validation failed. Compute a new description (in buf) and compiler ID (in h).
  2655  	// For now, there are only at most two filenames in the stat information.
  2656  	// The first one is the compiler executable we invoke.
  2657  	// The second is the underlying compiler as reported by -v -###
  2658  	// (see b.gccToolID implementation in buildid.go).
  2659  	toolID, exe2, err := b.gccToolID(compiler, "c")
  2660  	if err != nil {
  2661  		return cache.ActionID{}, false
  2662  	}
  2663  
  2664  	exes := []string{exe, exe2}
  2665  	str.Uniq(&exes)
  2666  	fmt.Fprintf(h, "gccCompilerID %q %q\n", exes, toolID)
  2667  	id = h.Sum()
  2668  
  2669  	var buf bytes.Buffer
  2670  	for _, exe := range exes {
  2671  		if exe == "" {
  2672  			continue
  2673  		}
  2674  		info, err := os.Stat(exe)
  2675  		if err != nil {
  2676  			return cache.ActionID{}, false
  2677  		}
  2678  		buf.WriteString(exe)
  2679  		buf.WriteString("\x00")
  2680  		buf.WriteString(statString(info))
  2681  		buf.WriteString("\x00")
  2682  	}
  2683  	buf.Write(id[:])
  2684  
  2685  	cache.PutBytes(cache.Default(), key, buf.Bytes())
  2686  	if b.gccCompilerIDCache == nil {
  2687  		b.gccCompilerIDCache = make(map[string]cache.ActionID)
  2688  	}
  2689  	b.gccCompilerIDCache[compiler] = id
  2690  	return id, true
  2691  }
  2692  
  2693  // gccArchArgs returns arguments to pass to gcc based on the architecture.
  2694  func (b *Builder) gccArchArgs() []string {
  2695  	switch cfg.Goarch {
  2696  	case "386":
  2697  		return []string{"-m32"}
  2698  	case "amd64":
  2699  		if cfg.Goos == "darwin" {
  2700  			return []string{"-arch", "x86_64", "-m64"}
  2701  		}
  2702  		return []string{"-m64"}
  2703  	case "arm64":
  2704  		if cfg.Goos == "darwin" {
  2705  			return []string{"-arch", "arm64"}
  2706  		}
  2707  	case "arm":
  2708  		return []string{"-marm"} // not thumb
  2709  	case "s390x":
  2710  		// minimum supported s390x version on Go is z13
  2711  		return []string{"-m64", "-march=z13"}
  2712  	case "mips64", "mips64le":
  2713  		args := []string{"-mabi=64"}
  2714  		if cfg.GOMIPS64 == "hardfloat" {
  2715  			return append(args, "-mhard-float")
  2716  		} else if cfg.GOMIPS64 == "softfloat" {
  2717  			return append(args, "-msoft-float")
  2718  		}
  2719  	case "mips", "mipsle":
  2720  		args := []string{"-mabi=32", "-march=mips32"}
  2721  		if cfg.GOMIPS == "hardfloat" {
  2722  			return append(args, "-mhard-float", "-mfp32", "-mno-odd-spreg")
  2723  		} else if cfg.GOMIPS == "softfloat" {
  2724  			return append(args, "-msoft-float")
  2725  		}
  2726  	case "loong64":
  2727  		return []string{"-mabi=lp64d"}
  2728  	case "ppc64":
  2729  		if cfg.Goos == "aix" {
  2730  			return []string{"-maix64"}
  2731  		}
  2732  	}
  2733  	return nil
  2734  }
  2735  
  2736  // envList returns the value of the given environment variable broken
  2737  // into fields, using the default value when the variable is empty.
  2738  //
  2739  // The environment variable must be quoted correctly for
  2740  // quoted.Split. This should be done before building
  2741  // anything, for example, in BuildInit.
  2742  func envList(key, def string) []string {
  2743  	v := cfg.Getenv(key)
  2744  	if v == "" {
  2745  		v = def
  2746  	}
  2747  	args, err := quoted.Split(v)
  2748  	if err != nil {
  2749  		panic(fmt.Sprintf("could not parse environment variable %s with value %q: %v", key, v, err))
  2750  	}
  2751  	return args
  2752  }
  2753  
  2754  // CFlags returns the flags to use when invoking the C, C++ or Fortran compilers, or cgo.
  2755  func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, ldflags []string, err error) {
  2756  	if cppflags, err = buildFlags("CPPFLAGS", "", p.CgoCPPFLAGS, checkCompilerFlags); err != nil {
  2757  		return
  2758  	}
  2759  	if cflags, err = buildFlags("CFLAGS", DefaultCFlags, p.CgoCFLAGS, checkCompilerFlags); err != nil {
  2760  		return
  2761  	}
  2762  	if cxxflags, err = buildFlags("CXXFLAGS", DefaultCFlags, p.CgoCXXFLAGS, checkCompilerFlags); err != nil {
  2763  		return
  2764  	}
  2765  	if fflags, err = buildFlags("FFLAGS", DefaultCFlags, p.CgoFFLAGS, checkCompilerFlags); err != nil {
  2766  		return
  2767  	}
  2768  	if ldflags, err = buildFlags("LDFLAGS", DefaultCFlags, p.CgoLDFLAGS, checkLinkerFlags); err != nil {
  2769  		return
  2770  	}
  2771  
  2772  	return
  2773  }
  2774  
  2775  func buildFlags(name, defaults string, fromPackage []string, check func(string, string, []string) error) ([]string, error) {
  2776  	if err := check(name, "#cgo "+name, fromPackage); err != nil {
  2777  		return nil, err
  2778  	}
  2779  	return str.StringList(envList("CGO_"+name, defaults), fromPackage), nil
  2780  }
  2781  
  2782  var cgoRe = lazyregexp.New(`[/\\:]`)
  2783  
  2784  type runCgoProvider struct {
  2785  	CFLAGS, CXXFLAGS, FFLAGS, LDFLAGS []string
  2786  	notCompatibleForInternalLinking   bool
  2787  	nonGoOverlay                      map[string]string
  2788  	goFiles                           []string // processed cgo files for the compiler
  2789  }
  2790  
  2791  func (pr *runCgoProvider) cflags() []string {
  2792  	return pr.CFLAGS
  2793  }
  2794  
  2795  func (pr *runCgoProvider) cxxflags() []string {
  2796  	return pr.CXXFLAGS
  2797  }
  2798  
  2799  func (pr *runCgoProvider) fflags() []string {
  2800  	return pr.CXXFLAGS
  2801  }
  2802  
  2803  func (pr *runCgoProvider) ldflags() []string {
  2804  	return pr.LDFLAGS
  2805  }
  2806  
  2807  func mustGetCoverInfo(a *Action) *coverProvider {
  2808  	for _, dep := range a.Deps {
  2809  		if dep.Mode == "cover" {
  2810  			return dep.Provider.(*coverProvider)
  2811  		}
  2812  	}
  2813  	base.Fatalf("internal error: cover provider not found")
  2814  	panic("unreachable")
  2815  }
  2816  
  2817  func (b *Builder) runCgo(ctx context.Context, a *Action) error {
  2818  	p := a.Package
  2819  	sh := b.Shell(a)
  2820  	objdir := a.Objdir
  2821  
  2822  	if err := sh.Mkdir(objdir); err != nil {
  2823  		return err
  2824  	}
  2825  
  2826  	nonGoFileLists := [][]string{p.CFiles, p.SFiles, p.CXXFiles, p.HFiles, p.FFiles}
  2827  	if err := b.computeNonGoOverlay(a, p, sh, objdir, nonGoFileLists); err != nil {
  2828  		return err
  2829  	}
  2830  
  2831  	cgofiles := slices.Clip(p.CgoFiles)
  2832  	if a.Package.Internal.Cover.Mode != "" {
  2833  		cp := mustGetCoverInfo(a)
  2834  		cgofiles = cp.cgoSources
  2835  	}
  2836  
  2837  	pcCFLAGS, pcLDFLAGS, err := b.getPkgConfigFlags(a, p)
  2838  	if err != nil {
  2839  		return err
  2840  	}
  2841  
  2842  	// Run SWIG on each .swig and .swigcxx file.
  2843  	// Each run will generate two files, a .go file and a .c or .cxx file.
  2844  	// The .go file will use import "C" and is to be processed by cgo.
  2845  	// For -cover test or build runs, this needs to happen after the cover
  2846  	// tool is run; we don't want to instrument swig-generated Go files,
  2847  	// see issue #64661.
  2848  	if p.UsesSwig() {
  2849  		if err := b.swig(a, objdir, pcCFLAGS); err != nil {
  2850  			return err
  2851  		}
  2852  		outGo, _, _ := b.swigOutputs(p, objdir)
  2853  		cgofiles = append(cgofiles, outGo...)
  2854  	}
  2855  
  2856  	cgoExe := base.Tool("cgo")
  2857  	cgofiles = mkAbsFiles(p.Dir, cgofiles)
  2858  
  2859  	cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS, err := b.CFlags(p)
  2860  	if err != nil {
  2861  		return err
  2862  	}
  2863  
  2864  	cgoCPPFLAGS = append(cgoCPPFLAGS, pcCFLAGS...)
  2865  	cgoLDFLAGS = append(cgoLDFLAGS, pcLDFLAGS...)
  2866  	// If we are compiling Objective-C code, then we need to link against libobjc
  2867  	if len(p.MFiles) > 0 {
  2868  		cgoLDFLAGS = append(cgoLDFLAGS, "-lobjc")
  2869  	}
  2870  
  2871  	// Likewise for Fortran, except there are many Fortran compilers.
  2872  	// Support gfortran out of the box and let others pass the correct link options
  2873  	// via CGO_LDFLAGS
  2874  	if len(p.FFiles) > 0 {
  2875  		fc := cfg.Getenv("FC")
  2876  		if fc == "" {
  2877  			fc = "gfortran"
  2878  		}
  2879  		if strings.Contains(fc, "gfortran") {
  2880  			cgoLDFLAGS = append(cgoLDFLAGS, "-lgfortran")
  2881  		}
  2882  	}
  2883  
  2884  	// Scrutinize CFLAGS and related for flags that might cause
  2885  	// problems if we are using internal linking (for example, use of
  2886  	// plugins, LTO, etc) by calling a helper routine that builds on
  2887  	// the existing CGO flags allow-lists. If we see anything
  2888  	// suspicious, emit a special token file "preferlinkext" (known to
  2889  	// the linker) in the object file to signal the that it should not
  2890  	// try to link internally and should revert to external linking.
  2891  	// The token we pass is a suggestion, not a mandate; if a user is
  2892  	// explicitly asking for a specific linkmode via the "-linkmode"
  2893  	// flag, the token will be ignored. NB: in theory we could ditch
  2894  	// the token approach and just pass a flag to the linker when we
  2895  	// eventually invoke it, and the linker flag could then be
  2896  	// documented (although coming up with a simple explanation of the
  2897  	// flag might be challenging). For more context see issues #58619,
  2898  	// #58620, and #58848.
  2899  	flagSources := []string{"CGO_CFLAGS", "CGO_CXXFLAGS", "CGO_FFLAGS"}
  2900  	flagLists := [][]string{cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS}
  2901  	notCompatibleWithInternalLinking := flagsNotCompatibleWithInternalLinking(flagSources, flagLists)
  2902  
  2903  	if cfg.BuildMSan {
  2904  		cgoCFLAGS = append([]string{"-fsanitize=memory"}, cgoCFLAGS...)
  2905  		cgoLDFLAGS = append([]string{"-fsanitize=memory"}, cgoLDFLAGS...)
  2906  	}
  2907  	if cfg.BuildASan {
  2908  		cgoCFLAGS = append([]string{"-fsanitize=address"}, cgoCFLAGS...)
  2909  		cgoLDFLAGS = append([]string{"-fsanitize=address"}, cgoLDFLAGS...)
  2910  	}
  2911  
  2912  	// Allows including _cgo_export.h, as well as the user's .h files,
  2913  	// from .[ch] files in the package.
  2914  	cgoCPPFLAGS = append(cgoCPPFLAGS, "-I", objdir)
  2915  
  2916  	// cgo
  2917  	// TODO: CGO_FLAGS?
  2918  	gofiles := []string{objdir + "_cgo_gotypes.go"}
  2919  	cfiles := []string{objdir + "_cgo_export.c"}
  2920  	for _, fn := range cgofiles {
  2921  		f := strings.TrimSuffix(filepath.Base(fn), ".go")
  2922  		gofiles = append(gofiles, objdir+f+".cgo1.go")
  2923  		cfiles = append(cfiles, objdir+f+".cgo2.c")
  2924  	}
  2925  
  2926  	// TODO: make cgo not depend on $GOARCH?
  2927  
  2928  	cgoflags := []string{}
  2929  	if p.Standard && p.ImportPath == "runtime/cgo" {
  2930  		cgoflags = append(cgoflags, "-import_runtime_cgo=false")
  2931  	}
  2932  	if p.Standard && (p.ImportPath == "runtime/race" || p.ImportPath == "runtime/msan" || p.ImportPath == "runtime/cgo" || p.ImportPath == "runtime/asan") {
  2933  		cgoflags = append(cgoflags, "-import_syscall=false")
  2934  	}
  2935  
  2936  	// cgoLDFLAGS, which includes p.CgoLDFLAGS, can be very long.
  2937  	// Pass it to cgo on the command line, so that we use a
  2938  	// response file if necessary.
  2939  	//
  2940  	// These flags are recorded in the generated _cgo_gotypes.go file
  2941  	// using //go:cgo_ldflag directives, the compiler records them in the
  2942  	// object file for the package, and then the Go linker passes them
  2943  	// along to the host linker. At this point in the code, cgoLDFLAGS
  2944  	// consists of the original $CGO_LDFLAGS (unchecked) and all the
  2945  	// flags put together from source code (checked).
  2946  	cgoenv := b.cCompilerEnv()
  2947  	cgoenv = append(cgoenv, cfgChangedEnv...)
  2948  	var ldflagsOption []string
  2949  	if len(cgoLDFLAGS) > 0 {
  2950  		flags := make([]string, len(cgoLDFLAGS))
  2951  		for i, f := range cgoLDFLAGS {
  2952  			flags[i] = strconv.Quote(f)
  2953  		}
  2954  		ldflagsOption = []string{"-ldflags=" + strings.Join(flags, " ")}
  2955  
  2956  		// Remove CGO_LDFLAGS from the environment.
  2957  		cgoenv = append(cgoenv, "CGO_LDFLAGS=")
  2958  	}
  2959  
  2960  	if cfg.BuildToolchainName == "gccgo" {
  2961  		if b.gccSupportsFlag([]string{BuildToolchain.compiler()}, "-fsplit-stack") {
  2962  			cgoCFLAGS = append(cgoCFLAGS, "-fsplit-stack")
  2963  		}
  2964  		cgoflags = append(cgoflags, "-gccgo")
  2965  		if pkgpath := gccgoPkgpath(p); pkgpath != "" {
  2966  			cgoflags = append(cgoflags, "-gccgopkgpath="+pkgpath)
  2967  		}
  2968  		if !BuildToolchain.(gccgoToolchain).supportsCgoIncomplete(b, a) {
  2969  			cgoflags = append(cgoflags, "-gccgo_define_cgoincomplete")
  2970  		}
  2971  	}
  2972  
  2973  	switch cfg.BuildBuildmode {
  2974  	case "c-archive", "c-shared":
  2975  		// Tell cgo that if there are any exported functions
  2976  		// it should generate a header file that C code can
  2977  		// #include.
  2978  		cgoflags = append(cgoflags, "-exportheader="+objdir+"_cgo_install.h")
  2979  	}
  2980  
  2981  	// Rewrite overlaid paths in cgo files.
  2982  	// cgo adds //line and #line pragmas in generated files with these paths.
  2983  	var trimpath []string
  2984  	for i := range cgofiles {
  2985  		path := mkAbs(p.Dir, cgofiles[i])
  2986  		if fsys.Replaced(path) {
  2987  			actual := fsys.Actual(path)
  2988  			cgofiles[i] = actual
  2989  			trimpath = append(trimpath, actual+"=>"+path)
  2990  		}
  2991  	}
  2992  	if len(trimpath) > 0 {
  2993  		cgoflags = append(cgoflags, "-trimpath", strings.Join(trimpath, ";"))
  2994  	}
  2995  
  2996  	if err := sh.run(p.Dir, p.ImportPath, cgoenv, cfg.BuildToolexec, cgoExe, "-objdir", objdir, "-importpath", p.ImportPath, cgoflags, ldflagsOption, "--", cgoCPPFLAGS, cgoCFLAGS, cgofiles); err != nil {
  2997  		return err
  2998  	}
  2999  
  3000  	a.Provider = &runCgoProvider{
  3001  		CFLAGS:                          str.StringList(cgoCPPFLAGS, cgoCFLAGS),
  3002  		CXXFLAGS:                        str.StringList(cgoCPPFLAGS, cgoCXXFLAGS),
  3003  		FFLAGS:                          str.StringList(cgoCPPFLAGS, cgoFFLAGS),
  3004  		LDFLAGS:                         cgoLDFLAGS,
  3005  		notCompatibleForInternalLinking: notCompatibleWithInternalLinking,
  3006  		nonGoOverlay:                    a.nonGoOverlay,
  3007  		goFiles:                         gofiles,
  3008  	}
  3009  
  3010  	return nil
  3011  }
  3012  
  3013  func (b *Builder) processCgoOutputs(a *Action, runCgoProvider *runCgoProvider, cgoExe, objdir string) (outGo, outObj []string, err error) {
  3014  	outGo = slices.Clip(runCgoProvider.goFiles)
  3015  
  3016  	// TODO(matloob): Pretty much the only thing this function is doing is
  3017  	// producing the dynimport go files. But we should be able to compile
  3018  	// those separately from the package itself: we just need to get the
  3019  	// compiled output to the linker. That means that we can remove the
  3020  	// dependency of this build action on the outputs of the cgo compile actions
  3021  	// (though we'd still need to depend on the runCgo action of course).
  3022  
  3023  	sh := b.Shell(a)
  3024  
  3025  	// Output the preferlinkext file if the run cgo action determined this package
  3026  	// was not compatible for internal linking based on CFLAGS, CXXFLAGS, or FFLAGS.
  3027  	if runCgoProvider.notCompatibleForInternalLinking {
  3028  		tokenFile := objdir + "preferlinkext"
  3029  		if err := sh.writeFile(tokenFile, nil); err != nil {
  3030  			return nil, nil, err
  3031  		}
  3032  		outObj = append(outObj, tokenFile)
  3033  	}
  3034  
  3035  	var collectAction *Action
  3036  	for _, dep := range a.Deps {
  3037  		if dep.Mode == "collect cgo" {
  3038  			collectAction = dep
  3039  		}
  3040  	}
  3041  	if collectAction == nil {
  3042  		base.Fatalf("internal error: no cgo collect action")
  3043  	}
  3044  	for _, dep := range collectAction.Deps {
  3045  		outObj = append(outObj, dep.Target)
  3046  	}
  3047  
  3048  	switch cfg.BuildToolchainName {
  3049  	case "gc":
  3050  		importGo := objdir + "_cgo_import.go"
  3051  		dynOutGo, dynOutObj, err := b.dynimport(a, objdir, importGo, cgoExe, runCgoProvider.CFLAGS, runCgoProvider.LDFLAGS, outObj)
  3052  		if err != nil {
  3053  			return nil, nil, err
  3054  		}
  3055  		if dynOutGo != "" {
  3056  			outGo = append(outGo, dynOutGo)
  3057  		}
  3058  		if dynOutObj != "" {
  3059  			outObj = append(outObj, dynOutObj)
  3060  		}
  3061  
  3062  	case "gccgo":
  3063  		defunC := objdir + "_cgo_defun.c"
  3064  		defunObj := objdir + "_cgo_defun.o"
  3065  		if err := BuildToolchain.cc(b, a, defunObj, defunC); err != nil {
  3066  			return nil, nil, err
  3067  		}
  3068  		outObj = append(outObj, defunObj)
  3069  
  3070  	default:
  3071  		noCompiler()
  3072  	}
  3073  
  3074  	// Double check the //go:cgo_ldflag comments in the generated files.
  3075  	// The compiler only permits such comments in files whose base name
  3076  	// starts with "_cgo_". Make sure that the comments in those files
  3077  	// are safe. This is a backstop against people somehow smuggling
  3078  	// such a comment into a file generated by cgo.
  3079  	if cfg.BuildToolchainName == "gc" && !cfg.BuildN {
  3080  		var flags []string
  3081  		for _, f := range outGo {
  3082  			if !strings.HasPrefix(filepath.Base(f), "_cgo_") {
  3083  				continue
  3084  			}
  3085  
  3086  			src, err := os.ReadFile(f)
  3087  			if err != nil {
  3088  				return nil, nil, err
  3089  			}
  3090  
  3091  			const cgoLdflag = "//go:cgo_ldflag"
  3092  			idx := bytes.Index(src, []byte(cgoLdflag))
  3093  			for idx >= 0 {
  3094  				// We are looking at //go:cgo_ldflag.
  3095  				// Find start of line.
  3096  				start := bytes.LastIndex(src[:idx], []byte("\n"))
  3097  				if start == -1 {
  3098  					start = 0
  3099  				}
  3100  
  3101  				// Find end of line.
  3102  				end := bytes.Index(src[idx:], []byte("\n"))
  3103  				if end == -1 {
  3104  					end = len(src)
  3105  				} else {
  3106  					end += idx
  3107  				}
  3108  
  3109  				// Check for first line comment in line.
  3110  				// We don't worry about /* */ comments,
  3111  				// which normally won't appear in files
  3112  				// generated by cgo.
  3113  				commentStart := bytes.Index(src[start:], []byte("//"))
  3114  				commentStart += start
  3115  				// If that line comment is //go:cgo_ldflag,
  3116  				// it's a match.
  3117  				if bytes.HasPrefix(src[commentStart:], []byte(cgoLdflag)) {
  3118  					// Pull out the flag, and unquote it.
  3119  					// This is what the compiler does.
  3120  					flag := string(src[idx+len(cgoLdflag) : end])
  3121  					flag = strings.TrimSpace(flag)
  3122  					flag = strings.Trim(flag, `"`)
  3123  					flags = append(flags, flag)
  3124  				}
  3125  				src = src[end:]
  3126  				idx = bytes.Index(src, []byte(cgoLdflag))
  3127  			}
  3128  		}
  3129  
  3130  		// We expect to find the contents of cgoLDFLAGS used when running the CGO action in flags.
  3131  		if len(runCgoProvider.LDFLAGS) > 0 {
  3132  		outer:
  3133  			for i := range flags {
  3134  				for j, f := range runCgoProvider.LDFLAGS {
  3135  					if f != flags[i+j] {
  3136  						continue outer
  3137  					}
  3138  				}
  3139  				flags = append(flags[:i], flags[i+len(runCgoProvider.LDFLAGS):]...)
  3140  				break
  3141  			}
  3142  		}
  3143  
  3144  		if err := checkLinkerFlags("LDFLAGS", "go:cgo_ldflag", flags); err != nil {
  3145  			return nil, nil, err
  3146  		}
  3147  	}
  3148  
  3149  	return outGo, outObj, nil
  3150  }
  3151  
  3152  // flagsNotCompatibleWithInternalLinking scans the list of cgo
  3153  // compiler flags (C/C++/Fortran) looking for flags that might cause
  3154  // problems if the build in question uses internal linking. The
  3155  // primary culprits are use of plugins or use of LTO, but we err on
  3156  // the side of caution, supporting only those flags that are on the
  3157  // allow-list for safe flags from security perspective. Return is TRUE
  3158  // if a sensitive flag is found, FALSE otherwise.
  3159  func flagsNotCompatibleWithInternalLinking(sourceList []string, flagListList [][]string) bool {
  3160  	for i := range sourceList {
  3161  		sn := sourceList[i]
  3162  		fll := flagListList[i]
  3163  		if err := checkCompilerFlagsForInternalLink(sn, sn, fll); err != nil {
  3164  			return true
  3165  		}
  3166  	}
  3167  	return false
  3168  }
  3169  
  3170  // dynimport creates a Go source file named importGo containing
  3171  // //go:cgo_import_dynamic directives for each symbol or library
  3172  // dynamically imported by the object files outObj.
  3173  // dynOutGo, if not empty, is a new Go file to build as part of the package.
  3174  // dynOutObj, if not empty, is a new file to add to the generated archive.
  3175  func (b *Builder) dynimport(a *Action, objdir, importGo, cgoExe string, cflags, cgoLDFLAGS, outObj []string) (dynOutGo, dynOutObj string, err error) {
  3176  	p := a.Package
  3177  	sh := b.Shell(a)
  3178  
  3179  	cfile := objdir + "_cgo_main.c"
  3180  	ofile := objdir + "_cgo_main.o"
  3181  	if err := b.gcc(a, objdir, ofile, cflags, cfile); err != nil {
  3182  		return "", "", err
  3183  	}
  3184  
  3185  	// Gather .syso files from this package and all (transitive) dependencies.
  3186  	var syso []string
  3187  	seen := make(map[*Action]bool)
  3188  	var gatherSyso func(*Action)
  3189  	gatherSyso = func(a1 *Action) {
  3190  		if seen[a1] {
  3191  			return
  3192  		}
  3193  		seen[a1] = true
  3194  		if p1 := a1.Package; p1 != nil {
  3195  			syso = append(syso, mkAbsFiles(p1.Dir, p1.SysoFiles)...)
  3196  		}
  3197  		for _, a2 := range a1.Deps {
  3198  			gatherSyso(a2)
  3199  		}
  3200  	}
  3201  	gatherSyso(a)
  3202  	sort.Strings(syso)
  3203  	str.Uniq(&syso)
  3204  	linkobj := str.StringList(ofile, outObj, syso)
  3205  	dynobj := objdir + "_cgo_.o"
  3206  
  3207  	ldflags := cgoLDFLAGS
  3208  	if (cfg.Goarch == "arm" && cfg.Goos == "linux") || cfg.Goos == "android" {
  3209  		if !slices.Contains(ldflags, "-no-pie") {
  3210  			// we need to use -pie for Linux/ARM to get accurate imported sym (added in https://golang.org/cl/5989058)
  3211  			// this seems to be outdated, but we don't want to break existing builds depending on this (Issue 45940)
  3212  			ldflags = append(ldflags, "-pie")
  3213  		}
  3214  		if slices.Contains(ldflags, "-pie") && slices.Contains(ldflags, "-static") {
  3215  			// -static -pie doesn't make sense, and causes link errors.
  3216  			// Issue 26197.
  3217  			n := make([]string, 0, len(ldflags)-1)
  3218  			for _, flag := range ldflags {
  3219  				if flag != "-static" {
  3220  					n = append(n, flag)
  3221  				}
  3222  			}
  3223  			ldflags = n
  3224  		}
  3225  	}
  3226  	if err := b.gccld(a, objdir, dynobj, ldflags, linkobj); err != nil {
  3227  		// We only need this information for internal linking.
  3228  		// If this link fails, mark the object as requiring
  3229  		// external linking. This link can fail for things like
  3230  		// syso files that have unexpected dependencies.
  3231  		// cmd/link explicitly looks for the name "dynimportfail".
  3232  		// See issue #52863.
  3233  		fail := objdir + "dynimportfail"
  3234  		if err := sh.writeFile(fail, nil); err != nil {
  3235  			return "", "", err
  3236  		}
  3237  		return "", fail, nil
  3238  	}
  3239  
  3240  	// cgo -dynimport
  3241  	var cgoflags []string
  3242  	if p.Standard && p.ImportPath == "runtime/cgo" {
  3243  		cgoflags = []string{"-dynlinker"} // record path to dynamic linker
  3244  	}
  3245  	err = sh.run(base.Cwd(), p.ImportPath, b.cCompilerEnv(), cfg.BuildToolexec, cgoExe, "-dynpackage", p.Name, "-dynimport", dynobj, "-dynout", importGo, cgoflags)
  3246  	if err != nil {
  3247  		return "", "", err
  3248  	}
  3249  	return importGo, "", nil
  3250  }
  3251  
  3252  // Run SWIG on all SWIG input files.
  3253  // TODO: Don't build a shared library, once SWIG emits the necessary
  3254  // pragmas for external linking.
  3255  func (b *Builder) swig(a *Action, objdir string, pcCFLAGS []string) error {
  3256  	p := a.Package
  3257  
  3258  	if err := b.swigVersionCheck(); err != nil {
  3259  		return err
  3260  	}
  3261  
  3262  	intgosize, err := b.swigIntSize(objdir)
  3263  	if err != nil {
  3264  		return err
  3265  	}
  3266  
  3267  	for _, f := range p.SwigFiles {
  3268  		if err := b.swigOne(a, f, objdir, pcCFLAGS, false, intgosize); err != nil {
  3269  			return err
  3270  		}
  3271  	}
  3272  	for _, f := range p.SwigCXXFiles {
  3273  		if b.swigOne(a, f, objdir, pcCFLAGS, true, intgosize); err != nil {
  3274  			return err
  3275  		}
  3276  	}
  3277  	return nil
  3278  }
  3279  
  3280  func (b *Builder) swigOutputs(p *load.Package, objdir string) (outGo, outC, outCXX []string) {
  3281  	for _, f := range p.SwigFiles {
  3282  		goFile, cFile := swigOneOutputs(f, objdir, false)
  3283  		outGo = append(outGo, goFile)
  3284  		outC = append(outC, cFile)
  3285  	}
  3286  	for _, f := range p.SwigCXXFiles {
  3287  		goFile, cxxFile := swigOneOutputs(f, objdir, true)
  3288  		outGo = append(outGo, goFile)
  3289  		outCXX = append(outCXX, cxxFile)
  3290  	}
  3291  	return outGo, outC, outCXX
  3292  }
  3293  
  3294  // Make sure SWIG is new enough.
  3295  var (
  3296  	swigCheckOnce sync.Once
  3297  	swigCheck     error
  3298  )
  3299  
  3300  func (b *Builder) swigDoVersionCheck() error {
  3301  	sh := b.BackgroundShell()
  3302  	out, err := sh.runOut(".", nil, "swig", "-version")
  3303  	if err != nil {
  3304  		return err
  3305  	}
  3306  	re := regexp.MustCompile(`[vV]ersion +(\d+)([.]\d+)?([.]\d+)?`)
  3307  	matches := re.FindSubmatch(out)
  3308  	if matches == nil {
  3309  		// Can't find version number; hope for the best.
  3310  		return nil
  3311  	}
  3312  
  3313  	major, err := strconv.Atoi(string(matches[1]))
  3314  	if err != nil {
  3315  		// Can't find version number; hope for the best.
  3316  		return nil
  3317  	}
  3318  	const errmsg = "must have SWIG version >= 3.0.6"
  3319  	if major < 3 {
  3320  		return errors.New(errmsg)
  3321  	}
  3322  	if major > 3 {
  3323  		// 4.0 or later
  3324  		return nil
  3325  	}
  3326  
  3327  	// We have SWIG version 3.x.
  3328  	if len(matches[2]) > 0 {
  3329  		minor, err := strconv.Atoi(string(matches[2][1:]))
  3330  		if err != nil {
  3331  			return nil
  3332  		}
  3333  		if minor > 0 {
  3334  			// 3.1 or later
  3335  			return nil
  3336  		}
  3337  	}
  3338  
  3339  	// We have SWIG version 3.0.x.
  3340  	if len(matches[3]) > 0 {
  3341  		patch, err := strconv.Atoi(string(matches[3][1:]))
  3342  		if err != nil {
  3343  			return nil
  3344  		}
  3345  		if patch < 6 {
  3346  			// Before 3.0.6.
  3347  			return errors.New(errmsg)
  3348  		}
  3349  	}
  3350  
  3351  	return nil
  3352  }
  3353  
  3354  func (b *Builder) swigVersionCheck() error {
  3355  	swigCheckOnce.Do(func() {
  3356  		swigCheck = b.swigDoVersionCheck()
  3357  	})
  3358  	return swigCheck
  3359  }
  3360  
  3361  // Find the value to pass for the -intgosize option to swig.
  3362  var (
  3363  	swigIntSizeOnce  sync.Once
  3364  	swigIntSize      string
  3365  	swigIntSizeError error
  3366  )
  3367  
  3368  // This code fails to build if sizeof(int) <= 32
  3369  const swigIntSizeCode = `
  3370  package main
  3371  const i int = 1 << 32
  3372  `
  3373  
  3374  // Determine the size of int on the target system for the -intgosize option
  3375  // of swig >= 2.0.9. Run only once.
  3376  func (b *Builder) swigDoIntSize(objdir string) (intsize string, err error) {
  3377  	if cfg.BuildN {
  3378  		return "$INTBITS", nil
  3379  	}
  3380  	src := filepath.Join(b.WorkDir, "swig_intsize.go")
  3381  	if err = os.WriteFile(src, []byte(swigIntSizeCode), 0666); err != nil {
  3382  		return
  3383  	}
  3384  	srcs := []string{src}
  3385  
  3386  	p := load.GoFilesPackage(modload.LoaderState, context.TODO(), load.PackageOpts{}, srcs)
  3387  
  3388  	if _, _, e := BuildToolchain.gc(b, &Action{Mode: "swigDoIntSize", Package: p, Objdir: objdir}, "", nil, nil, "", false, "", srcs); e != nil {
  3389  		return "32", nil
  3390  	}
  3391  	return "64", nil
  3392  }
  3393  
  3394  // Determine the size of int on the target system for the -intgosize option
  3395  // of swig >= 2.0.9.
  3396  func (b *Builder) swigIntSize(objdir string) (intsize string, err error) {
  3397  	swigIntSizeOnce.Do(func() {
  3398  		swigIntSize, swigIntSizeError = b.swigDoIntSize(objdir)
  3399  	})
  3400  	return swigIntSize, swigIntSizeError
  3401  }
  3402  
  3403  // Run SWIG on one SWIG input file.
  3404  func (b *Builder) swigOne(a *Action, file, objdir string, pcCFLAGS []string, cxx bool, intgosize string) error {
  3405  	p := a.Package
  3406  	sh := b.Shell(a)
  3407  
  3408  	cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p)
  3409  	if err != nil {
  3410  		return err
  3411  	}
  3412  
  3413  	var cflags []string
  3414  	if cxx {
  3415  		cflags = str.StringList(cgoCPPFLAGS, pcCFLAGS, cgoCXXFLAGS)
  3416  	} else {
  3417  		cflags = str.StringList(cgoCPPFLAGS, pcCFLAGS, cgoCFLAGS)
  3418  	}
  3419  
  3420  	base := swigBase(file, cxx)
  3421  	newGoFile, outC := swigOneOutputs(file, objdir, cxx)
  3422  
  3423  	gccgo := cfg.BuildToolchainName == "gccgo"
  3424  
  3425  	// swig
  3426  	args := []string{
  3427  		"-go",
  3428  		"-cgo",
  3429  		"-intgosize", intgosize,
  3430  		"-module", base,
  3431  		"-o", outC,
  3432  		"-outdir", objdir,
  3433  	}
  3434  
  3435  	for _, f := range cflags {
  3436  		if len(f) > 3 && f[:2] == "-I" {
  3437  			args = append(args, f)
  3438  		}
  3439  	}
  3440  
  3441  	if gccgo {
  3442  		args = append(args, "-gccgo")
  3443  		if pkgpath := gccgoPkgpath(p); pkgpath != "" {
  3444  			args = append(args, "-go-pkgpath", pkgpath)
  3445  		}
  3446  	}
  3447  	if cxx {
  3448  		args = append(args, "-c++")
  3449  	}
  3450  
  3451  	out, err := sh.runOut(p.Dir, nil, "swig", args, file)
  3452  	if err != nil && (bytes.Contains(out, []byte("-intgosize")) || bytes.Contains(out, []byte("-cgo"))) {
  3453  		return errors.New("must have SWIG version >= 3.0.6")
  3454  	}
  3455  	if err := sh.reportCmd("", "", out, err); err != nil {
  3456  		return err
  3457  	}
  3458  
  3459  	// If the input was x.swig, the output is x.go in the objdir.
  3460  	// But there might be an x.go in the original dir too, and if it
  3461  	// uses cgo as well, cgo will be processing both and will
  3462  	// translate both into x.cgo1.go in the objdir, overwriting one.
  3463  	// Rename x.go to _x_swig.go (newGoFile) to avoid this problem.
  3464  	// We ignore files in the original dir that begin with underscore
  3465  	// so _x_swig.go cannot conflict with an original file we were
  3466  	// going to compile.
  3467  	goFile := objdir + base + ".go"
  3468  	if cfg.BuildX || cfg.BuildN {
  3469  		sh.ShowCmd("", "mv %s %s", goFile, newGoFile)
  3470  	}
  3471  	if !cfg.BuildN {
  3472  		if err := os.Rename(goFile, newGoFile); err != nil {
  3473  			return err
  3474  		}
  3475  	}
  3476  
  3477  	return nil
  3478  }
  3479  
  3480  func swigBase(file string, cxx bool) string {
  3481  	n := 5 // length of ".swig"
  3482  	if cxx {
  3483  		n = 8 // length of ".swigcxx"
  3484  	}
  3485  	return file[:len(file)-n]
  3486  }
  3487  
  3488  func swigOneOutputs(file, objdir string, cxx bool) (outGo, outC string) {
  3489  	base := swigBase(file, cxx)
  3490  	gccBase := base + "_wrap."
  3491  	gccExt := "c"
  3492  	if cxx {
  3493  		gccExt = "cxx"
  3494  	}
  3495  
  3496  	newGoFile := objdir + "_" + base + "_swig.go"
  3497  	cFile := objdir + gccBase + gccExt
  3498  	return newGoFile, cFile
  3499  }
  3500  
  3501  // disableBuildID adjusts a linker command line to avoid creating a
  3502  // build ID when creating an object file rather than an executable or
  3503  // shared library. Some systems, such as Ubuntu, always add
  3504  // --build-id to every link, but we don't want a build ID when we are
  3505  // producing an object file. On some of those system a plain -r (not
  3506  // -Wl,-r) will turn off --build-id, but clang 3.0 doesn't support a
  3507  // plain -r. I don't know how to turn off --build-id when using clang
  3508  // other than passing a trailing --build-id=none. So that is what we
  3509  // do, but only on systems likely to support it, which is to say,
  3510  // systems that normally use gold or the GNU linker.
  3511  func (b *Builder) disableBuildID(ldflags []string) []string {
  3512  	switch cfg.Goos {
  3513  	case "android", "dragonfly", "linux", "netbsd":
  3514  		ldflags = append(ldflags, "-Wl,--build-id=none")
  3515  	}
  3516  	return ldflags
  3517  }
  3518  
  3519  // mkAbsFiles converts files into a list of absolute files,
  3520  // assuming they were originally relative to dir,
  3521  // and returns that new list.
  3522  func mkAbsFiles(dir string, files []string) []string {
  3523  	abs := make([]string, len(files))
  3524  	for i, f := range files {
  3525  		if !filepath.IsAbs(f) {
  3526  			f = filepath.Join(dir, f)
  3527  		}
  3528  		abs[i] = f
  3529  	}
  3530  	return abs
  3531  }
  3532  
  3533  // actualFiles applies fsys.Actual to the list of files.
  3534  func actualFiles(files []string) []string {
  3535  	a := make([]string, len(files))
  3536  	for i, f := range files {
  3537  		a[i] = fsys.Actual(f)
  3538  	}
  3539  	return a
  3540  }
  3541  
  3542  // passLongArgsInResponseFiles modifies cmd such that, for
  3543  // certain programs, long arguments are passed in "response files", a
  3544  // file on disk with the arguments, with one arg per line. An actual
  3545  // argument starting with '@' means that the rest of the argument is
  3546  // a filename of arguments to expand.
  3547  //
  3548  // See issues 18468 (Windows) and 37768 (Darwin).
  3549  func passLongArgsInResponseFiles(cmd *exec.Cmd) (cleanup func()) {
  3550  	cleanup = func() {} // no cleanup by default
  3551  
  3552  	var argLen int
  3553  	for _, arg := range cmd.Args {
  3554  		argLen += len(arg)
  3555  	}
  3556  
  3557  	// If we're not approaching 32KB of args, just pass args normally.
  3558  	// (use 30KB instead to be conservative; not sure how accounting is done)
  3559  	if !useResponseFile(cmd.Path, argLen) {
  3560  		return
  3561  	}
  3562  
  3563  	tf, err := os.CreateTemp("", "args")
  3564  	if err != nil {
  3565  		log.Fatalf("error writing long arguments to response file: %v", err)
  3566  	}
  3567  	cleanup = func() { os.Remove(tf.Name()) }
  3568  	var buf bytes.Buffer
  3569  	for _, arg := range cmd.Args[1:] {
  3570  		fmt.Fprintf(&buf, "%s\n", encodeArg(arg))
  3571  	}
  3572  	if _, err := tf.Write(buf.Bytes()); err != nil {
  3573  		tf.Close()
  3574  		cleanup()
  3575  		log.Fatalf("error writing long arguments to response file: %v", err)
  3576  	}
  3577  	if err := tf.Close(); err != nil {
  3578  		cleanup()
  3579  		log.Fatalf("error writing long arguments to response file: %v", err)
  3580  	}
  3581  	cmd.Args = []string{cmd.Args[0], "@" + tf.Name()}
  3582  	return cleanup
  3583  }
  3584  
  3585  func useResponseFile(path string, argLen int) bool {
  3586  	// Unless the program uses objabi.Flagparse, which understands
  3587  	// response files, don't use response files.
  3588  	// TODO: Note that other toolchains like CC are missing here for now.
  3589  	prog := strings.TrimSuffix(filepath.Base(path), ".exe")
  3590  	switch prog {
  3591  	case "compile", "link", "cgo", "asm", "cover":
  3592  	default:
  3593  		return false
  3594  	}
  3595  
  3596  	if argLen > sys.ExecArgLengthLimit {
  3597  		return true
  3598  	}
  3599  
  3600  	// On the Go build system, use response files about 10% of the
  3601  	// time, just to exercise this codepath.
  3602  	isBuilder := os.Getenv("GO_BUILDER_NAME") != ""
  3603  	if isBuilder && rand.Intn(10) == 0 {
  3604  		return true
  3605  	}
  3606  
  3607  	return false
  3608  }
  3609  
  3610  // encodeArg encodes an argument for response file writing.
  3611  func encodeArg(arg string) string {
  3612  	// If there aren't any characters we need to reencode, fastpath out.
  3613  	if !strings.ContainsAny(arg, "\\\n") {
  3614  		return arg
  3615  	}
  3616  	var b strings.Builder
  3617  	for _, r := range arg {
  3618  		switch r {
  3619  		case '\\':
  3620  			b.WriteByte('\\')
  3621  			b.WriteByte('\\')
  3622  		case '\n':
  3623  			b.WriteByte('\\')
  3624  			b.WriteByte('n')
  3625  		default:
  3626  			b.WriteRune(r)
  3627  		}
  3628  	}
  3629  	return b.String()
  3630  }
  3631  

View as plain text