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  	FixArchive    string            // write fixed files to this zip archive, if non-empty
  1191  
  1192  	SucceedOnTypecheckFailure bool // awful hack; see #18395 and below
  1193  }
  1194  
  1195  func buildVetConfig(a *Action, srcfiles []string) {
  1196  	// Classify files based on .go extension.
  1197  	// srcfiles does not include raw cgo files.
  1198  	var gofiles, nongofiles []string
  1199  	for _, name := range srcfiles {
  1200  		if strings.HasSuffix(name, ".go") {
  1201  			gofiles = append(gofiles, name)
  1202  		} else {
  1203  			nongofiles = append(nongofiles, name)
  1204  		}
  1205  	}
  1206  
  1207  	ignored := str.StringList(a.Package.IgnoredGoFiles, a.Package.IgnoredOtherFiles)
  1208  
  1209  	// Pass list of absolute paths to vet,
  1210  	// so that vet's error messages will use absolute paths,
  1211  	// so that we can reformat them relative to the directory
  1212  	// in which the go command is invoked.
  1213  	vcfg := &vetConfig{
  1214  		ID:           a.Package.ImportPath,
  1215  		Compiler:     cfg.BuildToolchainName,
  1216  		Dir:          a.Package.Dir,
  1217  		GoFiles:      actualFiles(mkAbsFiles(a.Package.Dir, gofiles)),
  1218  		NonGoFiles:   actualFiles(mkAbsFiles(a.Package.Dir, nongofiles)),
  1219  		IgnoredFiles: actualFiles(mkAbsFiles(a.Package.Dir, ignored)),
  1220  		ImportPath:   a.Package.ImportPath,
  1221  		ImportMap:    make(map[string]string),
  1222  		PackageFile:  make(map[string]string),
  1223  		Standard:     make(map[string]bool),
  1224  	}
  1225  	vcfg.GoVersion = "go" + gover.Local()
  1226  	if a.Package.Module != nil {
  1227  		v := a.Package.Module.GoVersion
  1228  		if v == "" {
  1229  			v = gover.DefaultGoModVersion
  1230  		}
  1231  		vcfg.GoVersion = "go" + v
  1232  
  1233  		if a.Package.Module.Error == nil {
  1234  			vcfg.ModulePath = a.Package.Module.Path
  1235  			vcfg.ModuleVersion = a.Package.Module.Version
  1236  		}
  1237  	}
  1238  	a.vetCfg = vcfg
  1239  	for i, raw := range a.Package.Internal.RawImports {
  1240  		final := a.Package.Imports[i]
  1241  		vcfg.ImportMap[raw] = final
  1242  	}
  1243  
  1244  	// Compute the list of mapped imports in the vet config
  1245  	// so that we can add any missing mappings below.
  1246  	vcfgMapped := make(map[string]bool)
  1247  	for _, p := range vcfg.ImportMap {
  1248  		vcfgMapped[p] = true
  1249  	}
  1250  
  1251  	for _, a1 := range a.Deps {
  1252  		p1 := a1.Package
  1253  		if p1 == nil || p1.ImportPath == "" || p1 == a.Package {
  1254  			continue
  1255  		}
  1256  		// Add import mapping if needed
  1257  		// (for imports like "runtime/cgo" that appear only in generated code).
  1258  		if !vcfgMapped[p1.ImportPath] {
  1259  			vcfg.ImportMap[p1.ImportPath] = p1.ImportPath
  1260  		}
  1261  		if a1.built != "" {
  1262  			vcfg.PackageFile[p1.ImportPath] = a1.built
  1263  		}
  1264  		if p1.Standard {
  1265  			vcfg.Standard[p1.ImportPath] = true
  1266  		}
  1267  	}
  1268  }
  1269  
  1270  // VetTool is the path to the effective vet or fix tool binary.
  1271  // The user may specify a non-default value using -{vet,fix}tool.
  1272  // The caller is expected to set it (if needed) before executing any vet actions.
  1273  var VetTool string
  1274  
  1275  // VetFlags are the default flags to pass to vet.
  1276  // The caller is expected to set them before executing any vet actions.
  1277  var VetFlags []string
  1278  
  1279  // VetHandleStdout determines how the stdout output of each vet tool
  1280  // invocation should be handled. The default behavior is to copy it to
  1281  // the go command's stdout, atomically.
  1282  var VetHandleStdout = copyToStdout
  1283  
  1284  // VetExplicit records whether the vet flags (which may include
  1285  // -{vet,fix}tool) were set explicitly on the command line.
  1286  var VetExplicit bool
  1287  
  1288  func (b *Builder) vet(ctx context.Context, a *Action) error {
  1289  	// a.Deps[0] is the build of the package being vetted.
  1290  
  1291  	a.Failed = nil // vet of dependency may have failed but we can still succeed
  1292  
  1293  	if a.Deps[0].Failed != nil {
  1294  		// The build of the package has failed. Skip vet check.
  1295  		// Vet could return export data for non-typecheck errors,
  1296  		// but we ignore it because the package cannot be compiled.
  1297  		return nil
  1298  	}
  1299  
  1300  	vcfg := a.Deps[0].vetCfg
  1301  	if vcfg == nil {
  1302  		// Vet config should only be missing if the build failed.
  1303  		return fmt.Errorf("vet config not found")
  1304  	}
  1305  
  1306  	sh := b.Shell(a)
  1307  
  1308  	// We use "vet" terminology even when building action graphs for go fix.
  1309  	vcfg.VetxOnly = a.VetxOnly
  1310  	vcfg.VetxOutput = a.Objdir + "vet.out"
  1311  	vcfg.Stdout = a.Objdir + "vet.stdout"
  1312  	if a.needFix {
  1313  		vcfg.FixArchive = a.Objdir + "vet.fix.zip"
  1314  	}
  1315  	vcfg.PackageVetx = make(map[string]string)
  1316  
  1317  	h := cache.NewHash("vet " + a.Package.ImportPath)
  1318  	fmt.Fprintf(h, "vet %q\n", b.toolID("vet"))
  1319  
  1320  	vetFlags := VetFlags
  1321  
  1322  	// In GOROOT, we enable all the vet tests during 'go test',
  1323  	// not just the high-confidence subset. This gets us extra
  1324  	// checking for the standard library (at some compliance cost)
  1325  	// and helps us gain experience about how well the checks
  1326  	// work, to help decide which should be turned on by default.
  1327  	// The command-line still wins.
  1328  	//
  1329  	// Note that this flag change applies even when running vet as
  1330  	// a dependency of vetting a package outside std.
  1331  	// (Otherwise we'd have to introduce a whole separate
  1332  	// space of "vet fmt as a dependency of a std top-level vet"
  1333  	// versus "vet fmt as a dependency of a non-std top-level vet".)
  1334  	// This is OK as long as the packages that are farther down the
  1335  	// dependency tree turn on *more* analysis, as here.
  1336  	// (The unsafeptr check does not write any facts for use by
  1337  	// later vet runs, nor does unreachable.)
  1338  	if a.Package.Goroot && !VetExplicit && VetTool == base.Tool("vet") {
  1339  		// Turn off -unsafeptr checks.
  1340  		// There's too much unsafe.Pointer code
  1341  		// that vet doesn't like in low-level packages
  1342  		// like runtime, sync, and reflect.
  1343  		// Note that $GOROOT/src/buildall.bash
  1344  		// does the same
  1345  		// and should be updated if these flags are
  1346  		// changed here.
  1347  		vetFlags = []string{"-unsafeptr=false"}
  1348  
  1349  		// Also turn off -unreachable checks during go test.
  1350  		// During testing it is very common to make changes
  1351  		// like hard-coded forced returns or panics that make
  1352  		// code unreachable. It's unreasonable to insist on files
  1353  		// not having any unreachable code during "go test".
  1354  		// (buildall.bash still has -unreachable enabled
  1355  		// for the overall whole-tree scan.)
  1356  		if cfg.CmdName == "test" {
  1357  			vetFlags = append(vetFlags, "-unreachable=false")
  1358  		}
  1359  	}
  1360  
  1361  	// Note: We could decide that vet should compute export data for
  1362  	// all analyses, in which case we don't need to include the flags here.
  1363  	// But that would mean that if an analysis causes problems like
  1364  	// unexpected crashes there would be no way to turn it off.
  1365  	// It seems better to let the flags disable export analysis too.
  1366  	fmt.Fprintf(h, "vetflags %q\n", vetFlags)
  1367  
  1368  	fmt.Fprintf(h, "pkg %q\n", a.Deps[0].actionID)
  1369  	for _, a1 := range a.Deps {
  1370  		if a1.Mode == "vet" && a1.built != "" {
  1371  			fmt.Fprintf(h, "vetout %q %s\n", a1.Package.ImportPath, b.fileHash(a1.built))
  1372  			vcfg.PackageVetx[a1.Package.ImportPath] = a1.built
  1373  		}
  1374  	}
  1375  	var (
  1376  		id            = cache.ActionID(h.Sum())     // for .vetx file
  1377  		stdoutKey     = cache.Subkey(id, "stdout")  // for .stdout file
  1378  		fixArchiveKey = cache.Subkey(id, "fix.zip") // for .fix.zip file
  1379  	)
  1380  
  1381  	// Check the cache; -a forces a rebuild.
  1382  	if !cfg.BuildA {
  1383  		c := cache.Default()
  1384  
  1385  		// There may be multiple artifacts in the cache.
  1386  		// We need to retrieve them all, or none:
  1387  		// the effect must be transactional.
  1388  		var (
  1389  			vetxFile   string                           // name of cached .vetx file
  1390  			fixArchive string                           // name of cached .fix.zip file
  1391  			stdout     io.Reader = bytes.NewReader(nil) // cached stdout stream
  1392  		)
  1393  
  1394  		// Obtain location of cached .vetx file.
  1395  		vetxFile, _, err := cache.GetFile(c, id)
  1396  		if err != nil {
  1397  			goto cachemiss
  1398  		}
  1399  
  1400  		// Obtain location of cached .fix.zip file (if needed).
  1401  		if a.needFix {
  1402  			file, _, err := cache.GetFile(c, fixArchiveKey)
  1403  			if err != nil {
  1404  				goto cachemiss
  1405  			}
  1406  			fixArchive = file
  1407  		}
  1408  
  1409  		// Copy cached .stdout file to stdout.
  1410  		if file, _, err := cache.GetFile(c, stdoutKey); err == nil {
  1411  			f, err := os.Open(file)
  1412  			if err != nil {
  1413  				goto cachemiss
  1414  			}
  1415  			defer f.Close() // ignore error (can't fail)
  1416  			stdout = f
  1417  		}
  1418  
  1419  		// Cache hit: commit transaction.
  1420  		a.built = vetxFile
  1421  		a.FixArchive = fixArchive
  1422  		if err := VetHandleStdout(stdout); err != nil {
  1423  			return err // internal error (don't fall through to cachemiss)
  1424  		}
  1425  
  1426  		return nil
  1427  	}
  1428  cachemiss:
  1429  
  1430  	js, err := json.MarshalIndent(vcfg, "", "\t")
  1431  	if err != nil {
  1432  		return fmt.Errorf("internal error marshaling vet config: %v", err)
  1433  	}
  1434  	js = append(js, '\n')
  1435  	if err := sh.writeFile(a.Objdir+"vet.cfg", js); err != nil {
  1436  		return err
  1437  	}
  1438  
  1439  	// TODO(rsc): Why do we pass $GCCGO to go vet?
  1440  	env := b.cCompilerEnv()
  1441  	if cfg.BuildToolchainName == "gccgo" {
  1442  		env = append(env, "GCCGO="+BuildToolchain.compiler())
  1443  	}
  1444  
  1445  	p := a.Package
  1446  	tool := VetTool
  1447  	if tool == "" {
  1448  		panic("VetTool unset")
  1449  	}
  1450  
  1451  	if err := sh.run(p.Dir, p.ImportPath, env, cfg.BuildToolexec, tool, vetFlags, a.Objdir+"vet.cfg"); err != nil {
  1452  		return err
  1453  	}
  1454  
  1455  	// Vet tool succeeded, possibly with facts, fixes, or JSON stdout.
  1456  	// Save all in cache.
  1457  
  1458  	// Save facts.
  1459  	if f, err := os.Open(vcfg.VetxOutput); err == nil {
  1460  		defer f.Close() // ignore error
  1461  		a.built = vcfg.VetxOutput
  1462  		cache.Default().Put(id, f) // ignore error
  1463  	}
  1464  
  1465  	// Save fix archive (if any).
  1466  	if a.needFix {
  1467  		if f, err := os.Open(vcfg.FixArchive); err == nil {
  1468  			defer f.Close() // ignore error
  1469  			a.FixArchive = vcfg.FixArchive
  1470  			cache.Default().Put(fixArchiveKey, f) // ignore error
  1471  		}
  1472  	}
  1473  
  1474  	// Save stdout.
  1475  	if f, err := os.Open(vcfg.Stdout); err == nil {
  1476  		defer f.Close() // ignore error
  1477  		if err := VetHandleStdout(f); err != nil {
  1478  			return err
  1479  		}
  1480  		f.Seek(0, io.SeekStart)           // ignore error
  1481  		cache.Default().Put(stdoutKey, f) // ignore error
  1482  	}
  1483  
  1484  	return nil
  1485  }
  1486  
  1487  var stdoutMu sync.Mutex // serializes concurrent writes (of e.g. JSON values) to stdout
  1488  
  1489  // copyToStdout copies the stream to stdout while holding the lock.
  1490  func copyToStdout(r io.Reader) error {
  1491  	stdoutMu.Lock()
  1492  	defer stdoutMu.Unlock()
  1493  	if _, err := io.Copy(os.Stdout, r); err != nil {
  1494  		return fmt.Errorf("copying vet tool stdout: %w", err)
  1495  	}
  1496  	return nil
  1497  }
  1498  
  1499  // linkActionID computes the action ID for a link action.
  1500  func (b *Builder) linkActionID(a *Action) cache.ActionID {
  1501  	p := a.Package
  1502  	h := cache.NewHash("link " + p.ImportPath)
  1503  
  1504  	// Toolchain-independent configuration.
  1505  	fmt.Fprintf(h, "link\n")
  1506  	fmt.Fprintf(h, "buildmode %s goos %s goarch %s\n", cfg.BuildBuildmode, cfg.Goos, cfg.Goarch)
  1507  	fmt.Fprintf(h, "import %q\n", p.ImportPath)
  1508  	fmt.Fprintf(h, "omitdebug %v standard %v local %v prefix %q\n", p.Internal.OmitDebug, p.Standard, p.Internal.Local, p.Internal.LocalPrefix)
  1509  	fmt.Fprintf(h, "defaultgodebug %q\n", p.DefaultGODEBUG)
  1510  	if cfg.BuildTrimpath {
  1511  		fmt.Fprintln(h, "trimpath")
  1512  	}
  1513  
  1514  	// Toolchain-dependent configuration, shared with b.linkSharedActionID.
  1515  	b.printLinkerConfig(h, p)
  1516  
  1517  	// Input files.
  1518  	for _, a1 := range a.Deps {
  1519  		p1 := a1.Package
  1520  		if p1 != nil {
  1521  			if a1.built != "" || a1.buildID != "" {
  1522  				buildID := a1.buildID
  1523  				if buildID == "" {
  1524  					buildID = b.buildID(a1.built)
  1525  				}
  1526  				fmt.Fprintf(h, "packagefile %s=%s\n", p1.ImportPath, contentID(buildID))
  1527  			}
  1528  			// Because we put package main's full action ID into the binary's build ID,
  1529  			// we must also put the full action ID into the binary's action ID hash.
  1530  			if p1.Name == "main" {
  1531  				fmt.Fprintf(h, "packagemain %s\n", a1.buildID)
  1532  			}
  1533  			if p1.Shlib != "" {
  1534  				fmt.Fprintf(h, "packageshlib %s=%s\n", p1.ImportPath, contentID(b.buildID(p1.Shlib)))
  1535  			}
  1536  		}
  1537  	}
  1538  
  1539  	return h.Sum()
  1540  }
  1541  
  1542  // printLinkerConfig prints the linker config into the hash h,
  1543  // as part of the computation of a linker-related action ID.
  1544  func (b *Builder) printLinkerConfig(h io.Writer, p *load.Package) {
  1545  	switch cfg.BuildToolchainName {
  1546  	default:
  1547  		base.Fatalf("linkActionID: unknown toolchain %q", cfg.BuildToolchainName)
  1548  
  1549  	case "gc":
  1550  		fmt.Fprintf(h, "link %s %q %s\n", b.toolID("link"), forcedLdflags, ldBuildmode)
  1551  		if p != nil {
  1552  			fmt.Fprintf(h, "linkflags %q\n", p.Internal.Ldflags)
  1553  		}
  1554  
  1555  		// GOARM, GOMIPS, etc.
  1556  		key, val, _ := cfg.GetArchEnv()
  1557  		fmt.Fprintf(h, "%s=%s\n", key, val)
  1558  
  1559  		if cfg.CleanGOEXPERIMENT != "" {
  1560  			fmt.Fprintf(h, "GOEXPERIMENT=%q\n", cfg.CleanGOEXPERIMENT)
  1561  		}
  1562  
  1563  		// The linker writes source file paths that refer to GOROOT,
  1564  		// but only if -trimpath is not specified (see [gctoolchain.ld] in gc.go).
  1565  		gorootFinal := cfg.GOROOT
  1566  		if cfg.BuildTrimpath {
  1567  			gorootFinal = ""
  1568  		}
  1569  		fmt.Fprintf(h, "GOROOT=%s\n", gorootFinal)
  1570  
  1571  		// GO_EXTLINK_ENABLED controls whether the external linker is used.
  1572  		fmt.Fprintf(h, "GO_EXTLINK_ENABLED=%s\n", cfg.Getenv("GO_EXTLINK_ENABLED"))
  1573  
  1574  		// TODO(rsc): Do cgo settings and flags need to be included?
  1575  		// Or external linker settings and flags?
  1576  
  1577  	case "gccgo":
  1578  		id, _, err := b.gccToolID(BuildToolchain.linker(), "go")
  1579  		if err != nil {
  1580  			base.Fatalf("%v", err)
  1581  		}
  1582  		fmt.Fprintf(h, "link %s %s\n", id, ldBuildmode)
  1583  		// TODO(iant): Should probably include cgo flags here.
  1584  	}
  1585  }
  1586  
  1587  // link is the action for linking a single command.
  1588  // Note that any new influence on this logic must be reported in b.linkActionID above as well.
  1589  func (b *Builder) link(ctx context.Context, a *Action) (err error) {
  1590  	if b.useCache(a, b.linkActionID(a), a.Package.Target, !b.IsCmdList) || b.IsCmdList {
  1591  		return nil
  1592  	}
  1593  	defer b.flushOutput(a)
  1594  
  1595  	sh := b.Shell(a)
  1596  	if err := sh.Mkdir(a.Objdir); err != nil {
  1597  		return err
  1598  	}
  1599  
  1600  	importcfg := a.Objdir + "importcfg.link"
  1601  	if err := b.writeLinkImportcfg(a, importcfg); err != nil {
  1602  		return err
  1603  	}
  1604  
  1605  	if err := AllowInstall(a); err != nil {
  1606  		return err
  1607  	}
  1608  
  1609  	// make target directory
  1610  	dir, _ := filepath.Split(a.Target)
  1611  	if dir != "" {
  1612  		if err := sh.Mkdir(dir); err != nil {
  1613  			return err
  1614  		}
  1615  	}
  1616  
  1617  	if err := BuildToolchain.ld(b, a, a.Target, importcfg, a.Deps[0].built); err != nil {
  1618  		return err
  1619  	}
  1620  
  1621  	// Update the binary with the final build ID.
  1622  	if err := b.updateBuildID(a, a.Target); err != nil {
  1623  		return err
  1624  	}
  1625  
  1626  	a.built = a.Target
  1627  	return nil
  1628  }
  1629  
  1630  func (b *Builder) writeLinkImportcfg(a *Action, file string) error {
  1631  	// Prepare Go import cfg.
  1632  	var icfg bytes.Buffer
  1633  	for _, a1 := range a.Deps {
  1634  		p1 := a1.Package
  1635  		if p1 == nil {
  1636  			continue
  1637  		}
  1638  		fmt.Fprintf(&icfg, "packagefile %s=%s\n", p1.ImportPath, a1.built)
  1639  		if p1.Shlib != "" {
  1640  			fmt.Fprintf(&icfg, "packageshlib %s=%s\n", p1.ImportPath, p1.Shlib)
  1641  		}
  1642  	}
  1643  	info := ""
  1644  	if a.Package.Internal.BuildInfo != nil {
  1645  		info = a.Package.Internal.BuildInfo.String()
  1646  	}
  1647  	fmt.Fprintf(&icfg, "modinfo %q\n", modload.ModInfoData(info))
  1648  	return b.Shell(a).writeFile(file, icfg.Bytes())
  1649  }
  1650  
  1651  // PkgconfigCmd returns a pkg-config binary name
  1652  // defaultPkgConfig is defined in zdefaultcc.go, written by cmd/dist.
  1653  func (b *Builder) PkgconfigCmd() string {
  1654  	return envList("PKG_CONFIG", cfg.DefaultPkgConfig)[0]
  1655  }
  1656  
  1657  // splitPkgConfigOutput parses the pkg-config output into a slice of flags.
  1658  // This implements the shell quoting semantics described in
  1659  // https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02,
  1660  // except that it does not support parameter or arithmetic expansion or command
  1661  // substitution and hard-codes the <blank> delimiters instead of reading them
  1662  // from LC_LOCALE.
  1663  func splitPkgConfigOutput(out []byte) ([]string, error) {
  1664  	if len(out) == 0 {
  1665  		return nil, nil
  1666  	}
  1667  	var flags []string
  1668  	flag := make([]byte, 0, len(out))
  1669  	didQuote := false // was the current flag parsed from a quoted string?
  1670  	escaped := false  // did we just read `\` in a non-single-quoted context?
  1671  	quote := byte(0)  // what is the quote character around the current string?
  1672  
  1673  	for _, c := range out {
  1674  		if escaped {
  1675  			if quote == '"' {
  1676  				// “The <backslash> shall retain its special meaning as an escape
  1677  				// character … only when followed by one of the following characters
  1678  				// when considered special:”
  1679  				switch c {
  1680  				case '$', '`', '"', '\\', '\n':
  1681  					// Handle the escaped character normally.
  1682  				default:
  1683  					// Not an escape character after all.
  1684  					flag = append(flag, '\\', c)
  1685  					escaped = false
  1686  					continue
  1687  				}
  1688  			}
  1689  
  1690  			if c == '\n' {
  1691  				// “If a <newline> follows the <backslash>, the shell shall interpret
  1692  				// this as line continuation.”
  1693  			} else {
  1694  				flag = append(flag, c)
  1695  			}
  1696  			escaped = false
  1697  			continue
  1698  		}
  1699  
  1700  		if quote != 0 && c == quote {
  1701  			quote = 0
  1702  			continue
  1703  		}
  1704  		switch quote {
  1705  		case '\'':
  1706  			// “preserve the literal value of each character”
  1707  			flag = append(flag, c)
  1708  			continue
  1709  		case '"':
  1710  			// “preserve the literal value of all characters within the double-quotes,
  1711  			// with the exception of …”
  1712  			switch c {
  1713  			case '`', '$', '\\':
  1714  			default:
  1715  				flag = append(flag, c)
  1716  				continue
  1717  			}
  1718  		}
  1719  
  1720  		// “The application shall quote the following characters if they are to
  1721  		// represent themselves:”
  1722  		switch c {
  1723  		case '|', '&', ';', '<', '>', '(', ')', '$', '`':
  1724  			return nil, fmt.Errorf("unexpected shell character %q in pkgconf output", c)
  1725  
  1726  		case '\\':
  1727  			// “A <backslash> that is not quoted shall preserve the literal value of
  1728  			// the following character, with the exception of a <newline>.”
  1729  			escaped = true
  1730  			continue
  1731  
  1732  		case '"', '\'':
  1733  			quote = c
  1734  			didQuote = true
  1735  			continue
  1736  
  1737  		case ' ', '\t', '\n':
  1738  			if len(flag) > 0 || didQuote {
  1739  				flags = append(flags, string(flag))
  1740  			}
  1741  			flag, didQuote = flag[:0], false
  1742  			continue
  1743  		}
  1744  
  1745  		flag = append(flag, c)
  1746  	}
  1747  
  1748  	// Prefer to report a missing quote instead of a missing escape. If the string
  1749  	// is something like `"foo\`, it's ambiguous as to whether the trailing
  1750  	// backslash is really an escape at all.
  1751  	if quote != 0 {
  1752  		return nil, errors.New("unterminated quoted string in pkgconf output")
  1753  	}
  1754  	if escaped {
  1755  		return nil, errors.New("broken character escaping in pkgconf output")
  1756  	}
  1757  
  1758  	if len(flag) > 0 || didQuote {
  1759  		flags = append(flags, string(flag))
  1760  	}
  1761  	return flags, nil
  1762  }
  1763  
  1764  // Calls pkg-config if needed and returns the cflags/ldflags needed to build a's package.
  1765  func (b *Builder) getPkgConfigFlags(a *Action, p *load.Package) (cflags, ldflags []string, err error) {
  1766  	sh := b.Shell(a)
  1767  	if pcargs := p.CgoPkgConfig; len(pcargs) > 0 {
  1768  		// pkg-config permits arguments to appear anywhere in
  1769  		// the command line. Move them all to the front, before --.
  1770  		var pcflags []string
  1771  		var pkgs []string
  1772  		for _, pcarg := range pcargs {
  1773  			if pcarg == "--" {
  1774  				// We're going to add our own "--" argument.
  1775  			} else if strings.HasPrefix(pcarg, "--") {
  1776  				pcflags = append(pcflags, pcarg)
  1777  			} else {
  1778  				pkgs = append(pkgs, pcarg)
  1779  			}
  1780  		}
  1781  		for _, pkg := range pkgs {
  1782  			if !load.SafeArg(pkg) {
  1783  				return nil, nil, fmt.Errorf("invalid pkg-config package name: %s", pkg)
  1784  			}
  1785  		}
  1786  		var out []byte
  1787  		out, err = sh.runOut(p.Dir, nil, b.PkgconfigCmd(), "--cflags", pcflags, "--", pkgs)
  1788  		if err != nil {
  1789  			desc := b.PkgconfigCmd() + " --cflags " + strings.Join(pcflags, " ") + " -- " + strings.Join(pkgs, " ")
  1790  			return nil, nil, sh.reportCmd(desc, "", out, err)
  1791  		}
  1792  		if len(out) > 0 {
  1793  			cflags, err = splitPkgConfigOutput(bytes.TrimSpace(out))
  1794  			if err != nil {
  1795  				return nil, nil, err
  1796  			}
  1797  			if err := checkCompilerFlags("CFLAGS", "pkg-config --cflags", cflags); err != nil {
  1798  				return nil, nil, err
  1799  			}
  1800  		}
  1801  		out, err = sh.runOut(p.Dir, nil, b.PkgconfigCmd(), "--libs", pcflags, "--", pkgs)
  1802  		if err != nil {
  1803  			desc := b.PkgconfigCmd() + " --libs " + strings.Join(pcflags, " ") + " -- " + strings.Join(pkgs, " ")
  1804  			return nil, nil, sh.reportCmd(desc, "", out, err)
  1805  		}
  1806  		if len(out) > 0 {
  1807  			// We need to handle path with spaces so that C:/Program\ Files can pass
  1808  			// checkLinkerFlags. Use splitPkgConfigOutput here just like we treat cflags.
  1809  			ldflags, err = splitPkgConfigOutput(bytes.TrimSpace(out))
  1810  			if err != nil {
  1811  				return nil, nil, err
  1812  			}
  1813  			if err := checkLinkerFlags("LDFLAGS", "pkg-config --libs", ldflags); err != nil {
  1814  				return nil, nil, err
  1815  			}
  1816  		}
  1817  	}
  1818  
  1819  	return
  1820  }
  1821  
  1822  func (b *Builder) installShlibname(ctx context.Context, a *Action) error {
  1823  	if err := AllowInstall(a); err != nil {
  1824  		return err
  1825  	}
  1826  
  1827  	sh := b.Shell(a)
  1828  	a1 := a.Deps[0]
  1829  	if !cfg.BuildN {
  1830  		if err := sh.Mkdir(filepath.Dir(a.Target)); err != nil {
  1831  			return err
  1832  		}
  1833  	}
  1834  	return sh.writeFile(a.Target, []byte(filepath.Base(a1.Target)+"\n"))
  1835  }
  1836  
  1837  func (b *Builder) linkSharedActionID(a *Action) cache.ActionID {
  1838  	h := cache.NewHash("linkShared")
  1839  
  1840  	// Toolchain-independent configuration.
  1841  	fmt.Fprintf(h, "linkShared\n")
  1842  	fmt.Fprintf(h, "goos %s goarch %s\n", cfg.Goos, cfg.Goarch)
  1843  
  1844  	// Toolchain-dependent configuration, shared with b.linkActionID.
  1845  	b.printLinkerConfig(h, nil)
  1846  
  1847  	// Input files.
  1848  	for _, a1 := range a.Deps {
  1849  		p1 := a1.Package
  1850  		if a1.built == "" {
  1851  			continue
  1852  		}
  1853  		if p1 != nil {
  1854  			fmt.Fprintf(h, "packagefile %s=%s\n", p1.ImportPath, contentID(b.buildID(a1.built)))
  1855  			if p1.Shlib != "" {
  1856  				fmt.Fprintf(h, "packageshlib %s=%s\n", p1.ImportPath, contentID(b.buildID(p1.Shlib)))
  1857  			}
  1858  		}
  1859  	}
  1860  	// Files named on command line are special.
  1861  	for _, a1 := range a.Deps[0].Deps {
  1862  		p1 := a1.Package
  1863  		fmt.Fprintf(h, "top %s=%s\n", p1.ImportPath, contentID(b.buildID(a1.built)))
  1864  	}
  1865  
  1866  	return h.Sum()
  1867  }
  1868  
  1869  func (b *Builder) linkShared(ctx context.Context, a *Action) (err error) {
  1870  	if b.useCache(a, b.linkSharedActionID(a), a.Target, !b.IsCmdList) || b.IsCmdList {
  1871  		return nil
  1872  	}
  1873  	defer b.flushOutput(a)
  1874  
  1875  	if err := AllowInstall(a); err != nil {
  1876  		return err
  1877  	}
  1878  
  1879  	if err := b.Shell(a).Mkdir(a.Objdir); err != nil {
  1880  		return err
  1881  	}
  1882  
  1883  	importcfg := a.Objdir + "importcfg.link"
  1884  	if err := b.writeLinkImportcfg(a, importcfg); err != nil {
  1885  		return err
  1886  	}
  1887  
  1888  	// TODO(rsc): There is a missing updateBuildID here,
  1889  	// but we have to decide where to store the build ID in these files.
  1890  	a.built = a.Target
  1891  	return BuildToolchain.ldShared(b, a, a.Deps[0].Deps, a.Target, importcfg, a.Deps)
  1892  }
  1893  
  1894  // BuildInstallFunc is the action for installing a single package or executable.
  1895  func BuildInstallFunc(b *Builder, ctx context.Context, a *Action) (err error) {
  1896  	defer func() {
  1897  		if err != nil {
  1898  			// a.Package == nil is possible for the go install -buildmode=shared
  1899  			// action that installs libmangledname.so, which corresponds to
  1900  			// a list of packages, not just one.
  1901  			sep, path := "", ""
  1902  			if a.Package != nil {
  1903  				sep, path = " ", a.Package.ImportPath
  1904  			}
  1905  			err = fmt.Errorf("go %s%s%s: %v", cfg.CmdName, sep, path, err)
  1906  		}
  1907  	}()
  1908  	sh := b.Shell(a)
  1909  
  1910  	a1 := a.Deps[0]
  1911  	a.buildID = a1.buildID
  1912  	if a.json != nil {
  1913  		a.json.BuildID = a.buildID
  1914  	}
  1915  
  1916  	// If we are using the eventual install target as an up-to-date
  1917  	// cached copy of the thing we built, then there's no need to
  1918  	// copy it into itself (and that would probably fail anyway).
  1919  	// In this case a1.built == a.Target because a1.built == p.Target,
  1920  	// so the built target is not in the a1.Objdir tree that b.cleanup(a1) removes.
  1921  	if a1.built == a.Target {
  1922  		a.built = a.Target
  1923  		if !a.buggyInstall {
  1924  			b.cleanup(a1)
  1925  		}
  1926  		// Whether we're smart enough to avoid a complete rebuild
  1927  		// depends on exactly what the staleness and rebuild algorithms
  1928  		// are, as well as potentially the state of the Go build cache.
  1929  		// We don't really want users to be able to infer (or worse start depending on)
  1930  		// those details from whether the modification time changes during
  1931  		// "go install", so do a best-effort update of the file times to make it
  1932  		// look like we rewrote a.Target even if we did not. Updating the mtime
  1933  		// may also help other mtime-based systems that depend on our
  1934  		// previous mtime updates that happened more often.
  1935  		// This is still not perfect - we ignore the error result, and if the file was
  1936  		// unwritable for some reason then pretending to have written it is also
  1937  		// confusing - but it's probably better than not doing the mtime update.
  1938  		//
  1939  		// But don't do that for the special case where building an executable
  1940  		// with -linkshared implicitly installs all its dependent libraries.
  1941  		// We want to hide that awful detail as much as possible, so don't
  1942  		// advertise it by touching the mtimes (usually the libraries are up
  1943  		// to date).
  1944  		if !a.buggyInstall && !b.IsCmdList {
  1945  			if cfg.BuildN {
  1946  				sh.ShowCmd("", "touch %s", a.Target)
  1947  			} else if err := AllowInstall(a); err == nil {
  1948  				now := time.Now()
  1949  				os.Chtimes(a.Target, now, now)
  1950  			}
  1951  		}
  1952  		return nil
  1953  	}
  1954  
  1955  	// If we're building for go list -export,
  1956  	// never install anything; just keep the cache reference.
  1957  	if b.IsCmdList {
  1958  		a.built = a1.built
  1959  		return nil
  1960  	}
  1961  	if err := AllowInstall(a); err != nil {
  1962  		return err
  1963  	}
  1964  
  1965  	if err := sh.Mkdir(a.Objdir); err != nil {
  1966  		return err
  1967  	}
  1968  
  1969  	perm := fs.FileMode(0666)
  1970  	if a1.Mode == "link" {
  1971  		switch cfg.BuildBuildmode {
  1972  		case "c-archive", "c-shared", "plugin":
  1973  		default:
  1974  			perm = 0777
  1975  		}
  1976  	}
  1977  
  1978  	// make target directory
  1979  	dir, _ := filepath.Split(a.Target)
  1980  	if dir != "" {
  1981  		if err := sh.Mkdir(dir); err != nil {
  1982  			return err
  1983  		}
  1984  	}
  1985  
  1986  	if !a.buggyInstall {
  1987  		defer b.cleanup(a1)
  1988  	}
  1989  
  1990  	return sh.moveOrCopyFile(a.Target, a1.built, perm, false)
  1991  }
  1992  
  1993  // AllowInstall returns a non-nil error if this invocation of the go command is
  1994  // allowed to install a.Target.
  1995  //
  1996  // The build of cmd/go running under its own test is forbidden from installing
  1997  // to its original GOROOT. The var is exported so it can be set by TestMain.
  1998  var AllowInstall = func(*Action) error { return nil }
  1999  
  2000  // cleanup removes a's object dir to keep the amount of
  2001  // on-disk garbage down in a large build. On an operating system
  2002  // with aggressive buffering, cleaning incrementally like
  2003  // this keeps the intermediate objects from hitting the disk.
  2004  func (b *Builder) cleanup(a *Action) {
  2005  	if !cfg.BuildWork {
  2006  		b.Shell(a).RemoveAll(a.Objdir)
  2007  	}
  2008  }
  2009  
  2010  // Install the cgo export header file, if there is one.
  2011  func (b *Builder) installHeader(ctx context.Context, a *Action) error {
  2012  	sh := b.Shell(a)
  2013  
  2014  	src := a.Objdir + "_cgo_install.h"
  2015  	if _, err := os.Stat(src); os.IsNotExist(err) {
  2016  		// If the file does not exist, there are no exported
  2017  		// functions, and we do not install anything.
  2018  		// TODO(rsc): Once we know that caching is rebuilding
  2019  		// at the right times (not missing rebuilds), here we should
  2020  		// probably delete the installed header, if any.
  2021  		if cfg.BuildX {
  2022  			sh.ShowCmd("", "# %s not created", src)
  2023  		}
  2024  		return nil
  2025  	}
  2026  
  2027  	if err := AllowInstall(a); err != nil {
  2028  		return err
  2029  	}
  2030  
  2031  	dir, _ := filepath.Split(a.Target)
  2032  	if dir != "" {
  2033  		if err := sh.Mkdir(dir); err != nil {
  2034  			return err
  2035  		}
  2036  	}
  2037  
  2038  	return sh.moveOrCopyFile(a.Target, src, 0666, true)
  2039  }
  2040  
  2041  // cover runs, in effect,
  2042  //
  2043  //	go tool cover -pkgcfg=<config file> -mode=b.coverMode -var="varName" -o <outfiles> <infiles>
  2044  //
  2045  // Return value is an updated output files list; in addition to the
  2046  // regular outputs (instrumented source files) the cover tool also
  2047  // writes a separate file (appearing first in the list of outputs)
  2048  // that will contain coverage counters and meta-data.
  2049  func (b *Builder) cover(a *Action, infiles, outfiles []string, varName string, mode string) ([]string, error) {
  2050  	pkgcfg := a.Objdir + "pkgcfg.txt"
  2051  	covoutputs := a.Objdir + "coveroutfiles.txt"
  2052  	odir := filepath.Dir(outfiles[0])
  2053  	cv := filepath.Join(odir, "covervars.go")
  2054  	outfiles = append([]string{cv}, outfiles...)
  2055  	if err := b.writeCoverPkgInputs(a, pkgcfg, covoutputs, outfiles); err != nil {
  2056  		return nil, err
  2057  	}
  2058  	args := []string{base.Tool("cover"),
  2059  		"-pkgcfg", pkgcfg,
  2060  		"-mode", mode,
  2061  		"-var", varName,
  2062  		"-outfilelist", covoutputs,
  2063  	}
  2064  	args = append(args, infiles...)
  2065  	if err := b.Shell(a).run(a.Objdir, "", nil,
  2066  		cfg.BuildToolexec, args); err != nil {
  2067  		return nil, err
  2068  	}
  2069  	return outfiles, nil
  2070  }
  2071  
  2072  func (b *Builder) writeCoverPkgInputs(a *Action, pconfigfile string, covoutputsfile string, outfiles []string) error {
  2073  	sh := b.Shell(a)
  2074  	p := a.Package
  2075  	p.Internal.Cover.Cfg = a.Objdir + "coveragecfg"
  2076  	pcfg := covcmd.CoverPkgConfig{
  2077  		PkgPath: p.ImportPath,
  2078  		PkgName: p.Name,
  2079  		// Note: coverage granularity is currently hard-wired to
  2080  		// 'perblock'; there isn't a way using "go build -cover" or "go
  2081  		// test -cover" to select it. This may change in the future
  2082  		// depending on user demand.
  2083  		Granularity: "perblock",
  2084  		OutConfig:   p.Internal.Cover.Cfg,
  2085  		Local:       p.Internal.Local,
  2086  	}
  2087  	if ca, ok := a.Actor.(*coverActor); ok && ca.covMetaFileName != "" {
  2088  		pcfg.EmitMetaFile = a.Objdir + ca.covMetaFileName
  2089  	}
  2090  	if a.Package.Module != nil {
  2091  		pcfg.ModulePath = a.Package.Module.Path
  2092  	}
  2093  	data, err := json.Marshal(pcfg)
  2094  	if err != nil {
  2095  		return err
  2096  	}
  2097  	data = append(data, '\n')
  2098  	if err := sh.writeFile(pconfigfile, data); err != nil {
  2099  		return err
  2100  	}
  2101  	var sb strings.Builder
  2102  	for i := range outfiles {
  2103  		fmt.Fprintf(&sb, "%s\n", outfiles[i])
  2104  	}
  2105  	return sh.writeFile(covoutputsfile, []byte(sb.String()))
  2106  }
  2107  
  2108  var objectMagic = [][]byte{
  2109  	{'!', '<', 'a', 'r', 'c', 'h', '>', '\n'}, // Package archive
  2110  	{'<', 'b', 'i', 'g', 'a', 'f', '>', '\n'}, // Package AIX big archive
  2111  	{'\x7F', 'E', 'L', 'F'},                   // ELF
  2112  	{0xFE, 0xED, 0xFA, 0xCE},                  // Mach-O big-endian 32-bit
  2113  	{0xFE, 0xED, 0xFA, 0xCF},                  // Mach-O big-endian 64-bit
  2114  	{0xCE, 0xFA, 0xED, 0xFE},                  // Mach-O little-endian 32-bit
  2115  	{0xCF, 0xFA, 0xED, 0xFE},                  // Mach-O little-endian 64-bit
  2116  	{0x4d, 0x5a, 0x90, 0x00, 0x03, 0x00},      // PE (Windows) as generated by 6l/8l and gcc
  2117  	{0x4d, 0x5a, 0x78, 0x00, 0x01, 0x00},      // PE (Windows) as generated by llvm for dll
  2118  	{0x00, 0x00, 0x01, 0xEB},                  // Plan 9 i386
  2119  	{0x00, 0x00, 0x8a, 0x97},                  // Plan 9 amd64
  2120  	{0x00, 0x00, 0x06, 0x47},                  // Plan 9 arm
  2121  	{0x00, 0x61, 0x73, 0x6D},                  // WASM
  2122  	{0x01, 0xDF},                              // XCOFF 32bit
  2123  	{0x01, 0xF7},                              // XCOFF 64bit
  2124  }
  2125  
  2126  func isObject(s string) bool {
  2127  	f, err := os.Open(s)
  2128  	if err != nil {
  2129  		return false
  2130  	}
  2131  	defer f.Close()
  2132  	buf := make([]byte, 64)
  2133  	io.ReadFull(f, buf)
  2134  	for _, magic := range objectMagic {
  2135  		if bytes.HasPrefix(buf, magic) {
  2136  			return true
  2137  		}
  2138  	}
  2139  	return false
  2140  }
  2141  
  2142  // cCompilerEnv returns environment variables to set when running the
  2143  // C compiler. This is needed to disable escape codes in clang error
  2144  // messages that confuse tools like cgo.
  2145  func (b *Builder) cCompilerEnv() []string {
  2146  	return []string{"TERM=dumb"}
  2147  }
  2148  
  2149  // mkAbs returns an absolute path corresponding to
  2150  // evaluating f in the directory dir.
  2151  // We always pass absolute paths of source files so that
  2152  // the error messages will include the full path to a file
  2153  // in need of attention.
  2154  func mkAbs(dir, f string) string {
  2155  	// Leave absolute paths alone.
  2156  	// Also, during -n mode we use the pseudo-directory $WORK
  2157  	// instead of creating an actual work directory that won't be used.
  2158  	// Leave paths beginning with $WORK alone too.
  2159  	if filepath.IsAbs(f) || strings.HasPrefix(f, "$WORK") {
  2160  		return f
  2161  	}
  2162  	return filepath.Join(dir, f)
  2163  }
  2164  
  2165  type toolchain interface {
  2166  	// gc runs the compiler in a specific directory on a set of files
  2167  	// and returns the name of the generated output file.
  2168  	gc(b *Builder, a *Action, archive string, importcfg, embedcfg []byte, symabis string, asmhdr bool, pgoProfile string, gofiles []string) (ofile string, out []byte, err error)
  2169  	// cc runs the toolchain's C compiler in a directory on a C file
  2170  	// to produce an output file.
  2171  	cc(b *Builder, a *Action, ofile, cfile string) error
  2172  	// asm runs the assembler in a specific directory on specific files
  2173  	// and returns a list of named output files.
  2174  	asm(b *Builder, a *Action, sfiles []string) ([]string, error)
  2175  	// symabis scans the symbol ABIs from sfiles and returns the
  2176  	// path to the output symbol ABIs file, or "" if none.
  2177  	symabis(b *Builder, a *Action, sfiles []string) (string, error)
  2178  	// pack runs the archive packer in a specific directory to create
  2179  	// an archive from a set of object files.
  2180  	// typically it is run in the object directory.
  2181  	pack(b *Builder, a *Action, afile string, ofiles []string) error
  2182  	// ld runs the linker to create an executable starting at mainpkg.
  2183  	ld(b *Builder, root *Action, targetPath, importcfg, mainpkg string) error
  2184  	// ldShared runs the linker to create a shared library containing the pkgs built by toplevelactions
  2185  	ldShared(b *Builder, root *Action, toplevelactions []*Action, targetPath, importcfg string, allactions []*Action) error
  2186  
  2187  	compiler() string
  2188  	linker() string
  2189  }
  2190  
  2191  type noToolchain struct{}
  2192  
  2193  func noCompiler() error {
  2194  	log.Fatalf("unknown compiler %q", cfg.BuildContext.Compiler)
  2195  	return nil
  2196  }
  2197  
  2198  func (noToolchain) compiler() string {
  2199  	noCompiler()
  2200  	return ""
  2201  }
  2202  
  2203  func (noToolchain) linker() string {
  2204  	noCompiler()
  2205  	return ""
  2206  }
  2207  
  2208  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) {
  2209  	return "", nil, noCompiler()
  2210  }
  2211  
  2212  func (noToolchain) asm(b *Builder, a *Action, sfiles []string) ([]string, error) {
  2213  	return nil, noCompiler()
  2214  }
  2215  
  2216  func (noToolchain) symabis(b *Builder, a *Action, sfiles []string) (string, error) {
  2217  	return "", noCompiler()
  2218  }
  2219  
  2220  func (noToolchain) pack(b *Builder, a *Action, afile string, ofiles []string) error {
  2221  	return noCompiler()
  2222  }
  2223  
  2224  func (noToolchain) ld(b *Builder, root *Action, targetPath, importcfg, mainpkg string) error {
  2225  	return noCompiler()
  2226  }
  2227  
  2228  func (noToolchain) ldShared(b *Builder, root *Action, toplevelactions []*Action, targetPath, importcfg string, allactions []*Action) error {
  2229  	return noCompiler()
  2230  }
  2231  
  2232  func (noToolchain) cc(b *Builder, a *Action, ofile, cfile string) error {
  2233  	return noCompiler()
  2234  }
  2235  
  2236  // gcc runs the gcc C compiler to create an object from a single C file.
  2237  func (b *Builder) gcc(a *Action, workdir, out string, flags []string, cfile string) error {
  2238  	p := a.Package
  2239  	return b.ccompile(a, out, flags, cfile, b.GccCmd(p.Dir, workdir))
  2240  }
  2241  
  2242  // gas runs the gcc c compiler to create an object file from a single C assembly file.
  2243  func (b *Builder) gas(a *Action, workdir, out string, flags []string, sfile string) error {
  2244  	p := a.Package
  2245  	data, err := os.ReadFile(filepath.Join(p.Dir, sfile))
  2246  	if err == nil {
  2247  		if bytes.HasPrefix(data, []byte("TEXT")) || bytes.Contains(data, []byte("\nTEXT")) ||
  2248  			bytes.HasPrefix(data, []byte("DATA")) || bytes.Contains(data, []byte("\nDATA")) ||
  2249  			bytes.HasPrefix(data, []byte("GLOBL")) || bytes.Contains(data, []byte("\nGLOBL")) {
  2250  			return fmt.Errorf("package using cgo has Go assembly file %s", sfile)
  2251  		}
  2252  	}
  2253  	return b.ccompile(a, out, flags, sfile, b.GccCmd(p.Dir, workdir))
  2254  }
  2255  
  2256  // gxx runs the g++ C++ compiler to create an object from a single C++ file.
  2257  func (b *Builder) gxx(a *Action, workdir, out string, flags []string, cxxfile string) error {
  2258  	p := a.Package
  2259  	return b.ccompile(a, out, flags, cxxfile, b.GxxCmd(p.Dir, workdir))
  2260  }
  2261  
  2262  // gfortran runs the gfortran Fortran compiler to create an object from a single Fortran file.
  2263  func (b *Builder) gfortran(a *Action, workdir, out string, flags []string, ffile string) error {
  2264  	p := a.Package
  2265  	return b.ccompile(a, out, flags, ffile, b.gfortranCmd(p.Dir, workdir))
  2266  }
  2267  
  2268  // ccompile runs the given C or C++ compiler and creates an object from a single source file.
  2269  func (b *Builder) ccompile(a *Action, outfile string, flags []string, file string, compiler []string) error {
  2270  	p := a.Package
  2271  	sh := b.Shell(a)
  2272  	file = mkAbs(p.Dir, file)
  2273  	outfile = mkAbs(p.Dir, outfile)
  2274  
  2275  	flags = slices.Clip(flags) // If we append to flags, write to a new slice that we own.
  2276  
  2277  	// Elide source directory paths if -trimpath is set.
  2278  	// This is needed for source files (e.g., a .c file in a package directory).
  2279  	// TODO(golang.org/issue/36072): cgo also generates files with #line
  2280  	// directives pointing to the source directory. It should not generate those
  2281  	// when -trimpath is enabled.
  2282  	if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") {
  2283  		if cfg.BuildTrimpath || p.Goroot {
  2284  			prefixMapFlag := "-fdebug-prefix-map"
  2285  			if b.gccSupportsFlag(compiler, "-ffile-prefix-map=a=b") {
  2286  				prefixMapFlag = "-ffile-prefix-map"
  2287  			}
  2288  			// Keep in sync with Action.trimpath.
  2289  			// The trimmed paths are a little different, but we need to trim in mostly the
  2290  			// same situations.
  2291  			var from, toPath string
  2292  			if m := p.Module; m == nil {
  2293  				if p.Root == "" { // command-line-arguments in GOPATH mode, maybe?
  2294  					from = p.Dir
  2295  					toPath = p.ImportPath
  2296  				} else if p.Goroot {
  2297  					from = p.Root
  2298  					toPath = "GOROOT"
  2299  				} else {
  2300  					from = p.Root
  2301  					toPath = "GOPATH"
  2302  				}
  2303  			} else if m.Dir == "" {
  2304  				// The module is in the vendor directory. Replace the entire vendor
  2305  				// directory path, because the module's Dir is not filled in.
  2306  				from = b.getVendorDir()
  2307  				toPath = "vendor"
  2308  			} else {
  2309  				from = m.Dir
  2310  				toPath = m.Path
  2311  				if m.Version != "" {
  2312  					toPath += "@" + m.Version
  2313  				}
  2314  			}
  2315  			// -fdebug-prefix-map (or -ffile-prefix-map) requires an absolute "to"
  2316  			// path (or it joins the path  with the working directory). Pick something
  2317  			// that makes sense for the target platform.
  2318  			var to string
  2319  			if cfg.BuildContext.GOOS == "windows" {
  2320  				to = filepath.Join(`\\_\_`, toPath)
  2321  			} else {
  2322  				to = filepath.Join("/_", toPath)
  2323  			}
  2324  			flags = append(slices.Clip(flags), prefixMapFlag+"="+from+"="+to)
  2325  		}
  2326  	}
  2327  
  2328  	// Tell gcc to not insert truly random numbers into the build process
  2329  	// this ensures LTO won't create random numbers for symbols.
  2330  	if b.gccSupportsFlag(compiler, "-frandom-seed=1") {
  2331  		flags = append(flags, "-frandom-seed="+buildid.HashToString(a.actionID))
  2332  	}
  2333  
  2334  	overlayPath := file
  2335  	if p, ok := a.nonGoOverlay[overlayPath]; ok {
  2336  		overlayPath = p
  2337  	}
  2338  	output, err := sh.runOut(filepath.Dir(overlayPath), b.cCompilerEnv(), compiler, flags, "-o", outfile, "-c", filepath.Base(overlayPath))
  2339  
  2340  	// On FreeBSD 11, when we pass -g to clang 3.8 it
  2341  	// invokes its internal assembler with -dwarf-version=2.
  2342  	// When it sees .section .note.GNU-stack, it warns
  2343  	// "DWARF2 only supports one section per compilation unit".
  2344  	// This warning makes no sense, since the section is empty,
  2345  	// but it confuses people.
  2346  	// We work around the problem by detecting the warning
  2347  	// and dropping -g and trying again.
  2348  	if bytes.Contains(output, []byte("DWARF2 only supports one section per compilation unit")) {
  2349  		newFlags := make([]string, 0, len(flags))
  2350  		for _, f := range flags {
  2351  			if !strings.HasPrefix(f, "-g") {
  2352  				newFlags = append(newFlags, f)
  2353  			}
  2354  		}
  2355  		if len(newFlags) < len(flags) {
  2356  			return b.ccompile(a, outfile, newFlags, file, compiler)
  2357  		}
  2358  	}
  2359  
  2360  	if len(output) > 0 && err == nil && os.Getenv("GO_BUILDER_NAME") != "" {
  2361  		output = append(output, "C compiler warning promoted to error on Go builders\n"...)
  2362  		err = errors.New("warning promoted to error")
  2363  	}
  2364  
  2365  	return sh.reportCmd("", "", output, err)
  2366  }
  2367  
  2368  // gccld runs the gcc linker to create an executable from a set of object files.
  2369  func (b *Builder) gccld(a *Action, objdir, outfile string, flags []string, objs []string) error {
  2370  	p := a.Package
  2371  	sh := b.Shell(a)
  2372  	var cmd []string
  2373  	if len(p.CXXFiles) > 0 || len(p.SwigCXXFiles) > 0 {
  2374  		cmd = b.GxxCmd(p.Dir, objdir)
  2375  	} else {
  2376  		cmd = b.GccCmd(p.Dir, objdir)
  2377  	}
  2378  
  2379  	cmdargs := []any{cmd, "-o", outfile, objs, flags}
  2380  	_, err := sh.runOut(base.Cwd(), b.cCompilerEnv(), cmdargs...)
  2381  
  2382  	// Note that failure is an expected outcome here, so we report output only
  2383  	// in debug mode and don't report the error.
  2384  	if cfg.BuildN || cfg.BuildX {
  2385  		saw := "succeeded"
  2386  		if err != nil {
  2387  			saw = "failed"
  2388  		}
  2389  		sh.ShowCmd("", "%s # test for internal linking errors (%s)", joinUnambiguously(str.StringList(cmdargs...)), saw)
  2390  	}
  2391  
  2392  	return err
  2393  }
  2394  
  2395  // GccCmd returns a gcc command line prefix
  2396  // defaultCC is defined in zdefaultcc.go, written by cmd/dist.
  2397  func (b *Builder) GccCmd(incdir, workdir string) []string {
  2398  	return b.compilerCmd(b.ccExe(), incdir, workdir)
  2399  }
  2400  
  2401  // GxxCmd returns a g++ command line prefix
  2402  // defaultCXX is defined in zdefaultcc.go, written by cmd/dist.
  2403  func (b *Builder) GxxCmd(incdir, workdir string) []string {
  2404  	return b.compilerCmd(b.cxxExe(), incdir, workdir)
  2405  }
  2406  
  2407  // gfortranCmd returns a gfortran command line prefix.
  2408  func (b *Builder) gfortranCmd(incdir, workdir string) []string {
  2409  	return b.compilerCmd(b.fcExe(), incdir, workdir)
  2410  }
  2411  
  2412  // ccExe returns the CC compiler setting without all the extra flags we add implicitly.
  2413  func (b *Builder) ccExe() []string {
  2414  	return envList("CC", cfg.DefaultCC(cfg.Goos, cfg.Goarch))
  2415  }
  2416  
  2417  // cxxExe returns the CXX compiler setting without all the extra flags we add implicitly.
  2418  func (b *Builder) cxxExe() []string {
  2419  	return envList("CXX", cfg.DefaultCXX(cfg.Goos, cfg.Goarch))
  2420  }
  2421  
  2422  // fcExe returns the FC compiler setting without all the extra flags we add implicitly.
  2423  func (b *Builder) fcExe() []string {
  2424  	return envList("FC", "gfortran")
  2425  }
  2426  
  2427  // compilerCmd returns a command line prefix for the given environment
  2428  // variable and using the default command when the variable is empty.
  2429  func (b *Builder) compilerCmd(compiler []string, incdir, workdir string) []string {
  2430  	a := append(compiler, "-I", incdir)
  2431  
  2432  	// Definitely want -fPIC but on Windows gcc complains
  2433  	// "-fPIC ignored for target (all code is position independent)"
  2434  	if cfg.Goos != "windows" {
  2435  		a = append(a, "-fPIC")
  2436  	}
  2437  	a = append(a, b.gccArchArgs()...)
  2438  	// gcc-4.5 and beyond require explicit "-pthread" flag
  2439  	// for multithreading with pthread library.
  2440  	if cfg.BuildContext.CgoEnabled {
  2441  		switch cfg.Goos {
  2442  		case "windows":
  2443  			a = append(a, "-mthreads")
  2444  		default:
  2445  			a = append(a, "-pthread")
  2446  		}
  2447  	}
  2448  
  2449  	if cfg.Goos == "aix" {
  2450  		// mcmodel=large must always be enabled to allow large TOC.
  2451  		a = append(a, "-mcmodel=large")
  2452  	}
  2453  
  2454  	// disable ASCII art in clang errors, if possible
  2455  	if b.gccSupportsFlag(compiler, "-fno-caret-diagnostics") {
  2456  		a = append(a, "-fno-caret-diagnostics")
  2457  	}
  2458  	// clang is too smart about command-line arguments
  2459  	if b.gccSupportsFlag(compiler, "-Qunused-arguments") {
  2460  		a = append(a, "-Qunused-arguments")
  2461  	}
  2462  
  2463  	// zig cc passes --gc-sections to the underlying linker, which then causes
  2464  	// undefined symbol errors when compiling with cgo but without C code.
  2465  	// https://github.com/golang/go/issues/52690
  2466  	if b.gccSupportsFlag(compiler, "-Wl,--no-gc-sections") {
  2467  		a = append(a, "-Wl,--no-gc-sections")
  2468  	}
  2469  
  2470  	// disable word wrapping in error messages
  2471  	a = append(a, "-fmessage-length=0")
  2472  
  2473  	// Tell gcc not to include the work directory in object files.
  2474  	if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") {
  2475  		if workdir == "" {
  2476  			workdir = b.WorkDir
  2477  		}
  2478  		workdir = strings.TrimSuffix(workdir, string(filepath.Separator))
  2479  		if b.gccSupportsFlag(compiler, "-ffile-prefix-map=a=b") {
  2480  			a = append(a, "-ffile-prefix-map="+workdir+"=/tmp/go-build")
  2481  		} else {
  2482  			a = append(a, "-fdebug-prefix-map="+workdir+"=/tmp/go-build")
  2483  		}
  2484  	}
  2485  
  2486  	// Tell gcc not to include flags in object files, which defeats the
  2487  	// point of -fdebug-prefix-map above.
  2488  	if b.gccSupportsFlag(compiler, "-gno-record-gcc-switches") {
  2489  		a = append(a, "-gno-record-gcc-switches")
  2490  	}
  2491  
  2492  	// On OS X, some of the compilers behave as if -fno-common
  2493  	// is always set, and the Mach-O linker in 6l/8l assumes this.
  2494  	// See https://golang.org/issue/3253.
  2495  	if cfg.Goos == "darwin" || cfg.Goos == "ios" {
  2496  		a = append(a, "-fno-common")
  2497  	}
  2498  
  2499  	return a
  2500  }
  2501  
  2502  // gccNoPie returns the flag to use to request non-PIE. On systems
  2503  // with PIE (position independent executables) enabled by default,
  2504  // -no-pie must be passed when doing a partial link with -Wl,-r.
  2505  // But -no-pie is not supported by all compilers, and clang spells it -nopie.
  2506  func (b *Builder) gccNoPie(linker []string) string {
  2507  	if b.gccSupportsFlag(linker, "-no-pie") {
  2508  		return "-no-pie"
  2509  	}
  2510  	if b.gccSupportsFlag(linker, "-nopie") {
  2511  		return "-nopie"
  2512  	}
  2513  	return ""
  2514  }
  2515  
  2516  // gccSupportsFlag checks to see if the compiler supports a flag.
  2517  func (b *Builder) gccSupportsFlag(compiler []string, flag string) bool {
  2518  	// We use the background shell for operations here because, while this is
  2519  	// triggered by some Action, it's not really about that Action, and often we
  2520  	// just get the results from the global cache.
  2521  	sh := b.BackgroundShell()
  2522  
  2523  	key := [2]string{compiler[0], flag}
  2524  
  2525  	// We used to write an empty C file, but that gets complicated with go
  2526  	// build -n. We tried using a file that does not exist, but that fails on
  2527  	// systems with GCC version 4.2.1; that is the last GPLv2 version of GCC,
  2528  	// so some systems have frozen on it. Now we pass an empty file on stdin,
  2529  	// which should work at least for GCC and clang.
  2530  	//
  2531  	// If the argument is "-Wl,", then it is testing the linker. In that case,
  2532  	// skip "-c". If it's not "-Wl,", then we are testing the compiler and can
  2533  	// omit the linking step with "-c".
  2534  	//
  2535  	// Using the same CFLAGS/LDFLAGS here and for building the program.
  2536  
  2537  	// On the iOS builder the command
  2538  	//   $CC -Wl,--no-gc-sections -x c - -o /dev/null < /dev/null
  2539  	// is failing with:
  2540  	//   Unable to remove existing file: Invalid argument
  2541  	tmp := os.DevNull
  2542  	if runtime.GOOS == "windows" || runtime.GOOS == "ios" {
  2543  		f, err := os.CreateTemp(b.WorkDir, "")
  2544  		if err != nil {
  2545  			return false
  2546  		}
  2547  		f.Close()
  2548  		tmp = f.Name()
  2549  		defer os.Remove(tmp)
  2550  	}
  2551  
  2552  	cmdArgs := str.StringList(compiler, flag)
  2553  	if strings.HasPrefix(flag, "-Wl,") /* linker flag */ {
  2554  		ldflags, err := buildFlags("LDFLAGS", DefaultCFlags, nil, checkLinkerFlags)
  2555  		if err != nil {
  2556  			return false
  2557  		}
  2558  		cmdArgs = append(cmdArgs, ldflags...)
  2559  	} else { /* compiler flag, add "-c" */
  2560  		cflags, err := buildFlags("CFLAGS", DefaultCFlags, nil, checkCompilerFlags)
  2561  		if err != nil {
  2562  			return false
  2563  		}
  2564  		cmdArgs = append(cmdArgs, cflags...)
  2565  		cmdArgs = append(cmdArgs, "-c")
  2566  	}
  2567  
  2568  	cmdArgs = append(cmdArgs, "-x", "c", "-", "-o", tmp)
  2569  
  2570  	if cfg.BuildN {
  2571  		sh.ShowCmd(b.WorkDir, "%s || true", joinUnambiguously(cmdArgs))
  2572  		return false
  2573  	}
  2574  
  2575  	// gccCompilerID acquires b.exec, so do before acquiring lock.
  2576  	compilerID, cacheOK := b.gccCompilerID(compiler[0])
  2577  
  2578  	b.exec.Lock()
  2579  	defer b.exec.Unlock()
  2580  	if b, ok := b.flagCache[key]; ok {
  2581  		return b
  2582  	}
  2583  	if b.flagCache == nil {
  2584  		b.flagCache = make(map[[2]string]bool)
  2585  	}
  2586  
  2587  	// Look in build cache.
  2588  	var flagID cache.ActionID
  2589  	if cacheOK {
  2590  		flagID = cache.Subkey(compilerID, "gccSupportsFlag "+flag)
  2591  		if data, _, err := cache.GetBytes(cache.Default(), flagID); err == nil {
  2592  			supported := string(data) == "true"
  2593  			b.flagCache[key] = supported
  2594  			return supported
  2595  		}
  2596  	}
  2597  
  2598  	if cfg.BuildX {
  2599  		sh.ShowCmd(b.WorkDir, "%s || true", joinUnambiguously(cmdArgs))
  2600  	}
  2601  	cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
  2602  	cmd.Dir = b.WorkDir
  2603  	cmd.Env = append(cmd.Environ(), "LC_ALL=C")
  2604  	out, _ := cmd.CombinedOutput()
  2605  	// GCC says "unrecognized command line option".
  2606  	// clang says "unknown argument".
  2607  	// tcc says "unsupported"
  2608  	// AIX says "not recognized"
  2609  	// Older versions of GCC say "unrecognised debug output level".
  2610  	// For -fsplit-stack GCC says "'-fsplit-stack' is not supported".
  2611  	supported := !bytes.Contains(out, []byte("unrecognized")) &&
  2612  		!bytes.Contains(out, []byte("unknown")) &&
  2613  		!bytes.Contains(out, []byte("unrecognised")) &&
  2614  		!bytes.Contains(out, []byte("is not supported")) &&
  2615  		!bytes.Contains(out, []byte("not recognized")) &&
  2616  		!bytes.Contains(out, []byte("unsupported"))
  2617  
  2618  	if cacheOK {
  2619  		s := "false"
  2620  		if supported {
  2621  			s = "true"
  2622  		}
  2623  		cache.PutBytes(cache.Default(), flagID, []byte(s))
  2624  	}
  2625  
  2626  	b.flagCache[key] = supported
  2627  	return supported
  2628  }
  2629  
  2630  // statString returns a string form of an os.FileInfo, for serializing and comparison.
  2631  func statString(info os.FileInfo) string {
  2632  	return fmt.Sprintf("stat %d %x %v %v\n", info.Size(), uint64(info.Mode()), info.ModTime(), info.IsDir())
  2633  }
  2634  
  2635  // gccCompilerID returns a build cache key for the current gcc,
  2636  // as identified by running 'compiler'.
  2637  // The caller can use subkeys of the key.
  2638  // Other parts of cmd/go can use the id as a hash
  2639  // of the installed compiler version.
  2640  func (b *Builder) gccCompilerID(compiler string) (id cache.ActionID, ok bool) {
  2641  	// We use the background shell for operations here because, while this is
  2642  	// triggered by some Action, it's not really about that Action, and often we
  2643  	// just get the results from the global cache.
  2644  	sh := b.BackgroundShell()
  2645  
  2646  	if cfg.BuildN {
  2647  		sh.ShowCmd(b.WorkDir, "%s || true", joinUnambiguously([]string{compiler, "--version"}))
  2648  		return cache.ActionID{}, false
  2649  	}
  2650  
  2651  	b.exec.Lock()
  2652  	defer b.exec.Unlock()
  2653  
  2654  	if id, ok := b.gccCompilerIDCache[compiler]; ok {
  2655  		return id, ok
  2656  	}
  2657  
  2658  	// We hash the compiler's full path to get a cache entry key.
  2659  	// That cache entry holds a validation description,
  2660  	// which is of the form:
  2661  	//
  2662  	//	filename \x00 statinfo \x00
  2663  	//	...
  2664  	//	compiler id
  2665  	//
  2666  	// If os.Stat of each filename matches statinfo,
  2667  	// then the entry is still valid, and we can use the
  2668  	// compiler id without any further expense.
  2669  	//
  2670  	// Otherwise, we compute a new validation description
  2671  	// and compiler id (below).
  2672  	exe, err := pathcache.LookPath(compiler)
  2673  	if err != nil {
  2674  		return cache.ActionID{}, false
  2675  	}
  2676  
  2677  	h := cache.NewHash("gccCompilerID")
  2678  	fmt.Fprintf(h, "gccCompilerID %q", exe)
  2679  	key := h.Sum()
  2680  	data, _, err := cache.GetBytes(cache.Default(), key)
  2681  	if err == nil && len(data) > len(id) {
  2682  		stats := strings.Split(string(data[:len(data)-len(id)]), "\x00")
  2683  		if len(stats)%2 != 0 {
  2684  			goto Miss
  2685  		}
  2686  		for i := 0; i+2 <= len(stats); i++ {
  2687  			info, err := os.Stat(stats[i])
  2688  			if err != nil || statString(info) != stats[i+1] {
  2689  				goto Miss
  2690  			}
  2691  		}
  2692  		copy(id[:], data[len(data)-len(id):])
  2693  		return id, true
  2694  	Miss:
  2695  	}
  2696  
  2697  	// Validation failed. Compute a new description (in buf) and compiler ID (in h).
  2698  	// For now, there are only at most two filenames in the stat information.
  2699  	// The first one is the compiler executable we invoke.
  2700  	// The second is the underlying compiler as reported by -v -###
  2701  	// (see b.gccToolID implementation in buildid.go).
  2702  	toolID, exe2, err := b.gccToolID(compiler, "c")
  2703  	if err != nil {
  2704  		return cache.ActionID{}, false
  2705  	}
  2706  
  2707  	exes := []string{exe, exe2}
  2708  	str.Uniq(&exes)
  2709  	fmt.Fprintf(h, "gccCompilerID %q %q\n", exes, toolID)
  2710  	id = h.Sum()
  2711  
  2712  	var buf bytes.Buffer
  2713  	for _, exe := range exes {
  2714  		if exe == "" {
  2715  			continue
  2716  		}
  2717  		info, err := os.Stat(exe)
  2718  		if err != nil {
  2719  			return cache.ActionID{}, false
  2720  		}
  2721  		buf.WriteString(exe)
  2722  		buf.WriteString("\x00")
  2723  		buf.WriteString(statString(info))
  2724  		buf.WriteString("\x00")
  2725  	}
  2726  	buf.Write(id[:])
  2727  
  2728  	cache.PutBytes(cache.Default(), key, buf.Bytes())
  2729  	if b.gccCompilerIDCache == nil {
  2730  		b.gccCompilerIDCache = make(map[string]cache.ActionID)
  2731  	}
  2732  	b.gccCompilerIDCache[compiler] = id
  2733  	return id, true
  2734  }
  2735  
  2736  // gccArchArgs returns arguments to pass to gcc based on the architecture.
  2737  func (b *Builder) gccArchArgs() []string {
  2738  	switch cfg.Goarch {
  2739  	case "386":
  2740  		return []string{"-m32"}
  2741  	case "amd64":
  2742  		if cfg.Goos == "darwin" {
  2743  			return []string{"-arch", "x86_64", "-m64"}
  2744  		}
  2745  		return []string{"-m64"}
  2746  	case "arm64":
  2747  		if cfg.Goos == "darwin" {
  2748  			return []string{"-arch", "arm64"}
  2749  		}
  2750  	case "arm":
  2751  		return []string{"-marm"} // not thumb
  2752  	case "s390x":
  2753  		// minimum supported s390x version on Go is z13
  2754  		return []string{"-m64", "-march=z13"}
  2755  	case "mips64", "mips64le":
  2756  		args := []string{"-mabi=64"}
  2757  		if cfg.GOMIPS64 == "hardfloat" {
  2758  			return append(args, "-mhard-float")
  2759  		} else if cfg.GOMIPS64 == "softfloat" {
  2760  			return append(args, "-msoft-float")
  2761  		}
  2762  	case "mips", "mipsle":
  2763  		args := []string{"-mabi=32", "-march=mips32"}
  2764  		if cfg.GOMIPS == "hardfloat" {
  2765  			return append(args, "-mhard-float", "-mfp32", "-mno-odd-spreg")
  2766  		} else if cfg.GOMIPS == "softfloat" {
  2767  			return append(args, "-msoft-float")
  2768  		}
  2769  	case "loong64":
  2770  		return []string{"-mabi=lp64d"}
  2771  	case "ppc64":
  2772  		if cfg.Goos == "aix" {
  2773  			return []string{"-maix64"}
  2774  		}
  2775  	}
  2776  	return nil
  2777  }
  2778  
  2779  // envList returns the value of the given environment variable broken
  2780  // into fields, using the default value when the variable is empty.
  2781  //
  2782  // The environment variable must be quoted correctly for
  2783  // quoted.Split. This should be done before building
  2784  // anything, for example, in BuildInit.
  2785  func envList(key, def string) []string {
  2786  	v := cfg.Getenv(key)
  2787  	if v == "" {
  2788  		v = def
  2789  	}
  2790  	args, err := quoted.Split(v)
  2791  	if err != nil {
  2792  		panic(fmt.Sprintf("could not parse environment variable %s with value %q: %v", key, v, err))
  2793  	}
  2794  	return args
  2795  }
  2796  
  2797  // CFlags returns the flags to use when invoking the C, C++ or Fortran compilers, or cgo.
  2798  func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, ldflags []string, err error) {
  2799  	if cppflags, err = buildFlags("CPPFLAGS", "", p.CgoCPPFLAGS, checkCompilerFlags); err != nil {
  2800  		return
  2801  	}
  2802  	if cflags, err = buildFlags("CFLAGS", DefaultCFlags, p.CgoCFLAGS, checkCompilerFlags); err != nil {
  2803  		return
  2804  	}
  2805  	if cxxflags, err = buildFlags("CXXFLAGS", DefaultCFlags, p.CgoCXXFLAGS, checkCompilerFlags); err != nil {
  2806  		return
  2807  	}
  2808  	if fflags, err = buildFlags("FFLAGS", DefaultCFlags, p.CgoFFLAGS, checkCompilerFlags); err != nil {
  2809  		return
  2810  	}
  2811  	if ldflags, err = buildFlags("LDFLAGS", DefaultCFlags, p.CgoLDFLAGS, checkLinkerFlags); err != nil {
  2812  		return
  2813  	}
  2814  
  2815  	return
  2816  }
  2817  
  2818  func buildFlags(name, defaults string, fromPackage []string, check func(string, string, []string) error) ([]string, error) {
  2819  	if err := check(name, "#cgo "+name, fromPackage); err != nil {
  2820  		return nil, err
  2821  	}
  2822  	return str.StringList(envList("CGO_"+name, defaults), fromPackage), nil
  2823  }
  2824  
  2825  var cgoRe = lazyregexp.New(`[/\\:]`)
  2826  
  2827  type runCgoProvider struct {
  2828  	CFLAGS, CXXFLAGS, FFLAGS, LDFLAGS []string
  2829  	notCompatibleForInternalLinking   bool
  2830  	nonGoOverlay                      map[string]string
  2831  	goFiles                           []string // processed cgo files for the compiler
  2832  }
  2833  
  2834  func (pr *runCgoProvider) cflags() []string {
  2835  	return pr.CFLAGS
  2836  }
  2837  
  2838  func (pr *runCgoProvider) cxxflags() []string {
  2839  	return pr.CXXFLAGS
  2840  }
  2841  
  2842  func (pr *runCgoProvider) fflags() []string {
  2843  	return pr.CXXFLAGS
  2844  }
  2845  
  2846  func (pr *runCgoProvider) ldflags() []string {
  2847  	return pr.LDFLAGS
  2848  }
  2849  
  2850  func mustGetCoverInfo(a *Action) *coverProvider {
  2851  	for _, dep := range a.Deps {
  2852  		if dep.Mode == "cover" {
  2853  			return dep.Provider.(*coverProvider)
  2854  		}
  2855  	}
  2856  	base.Fatalf("internal error: cover provider not found")
  2857  	panic("unreachable")
  2858  }
  2859  
  2860  func (b *Builder) runCgo(ctx context.Context, a *Action) error {
  2861  	p := a.Package
  2862  	sh := b.Shell(a)
  2863  	objdir := a.Objdir
  2864  
  2865  	if err := sh.Mkdir(objdir); err != nil {
  2866  		return err
  2867  	}
  2868  
  2869  	nonGoFileLists := [][]string{p.CFiles, p.SFiles, p.CXXFiles, p.HFiles, p.FFiles}
  2870  	if err := b.computeNonGoOverlay(a, p, sh, objdir, nonGoFileLists); err != nil {
  2871  		return err
  2872  	}
  2873  
  2874  	cgofiles := slices.Clip(p.CgoFiles)
  2875  	if a.Package.Internal.Cover.Mode != "" {
  2876  		cp := mustGetCoverInfo(a)
  2877  		cgofiles = cp.cgoSources
  2878  	}
  2879  
  2880  	pcCFLAGS, pcLDFLAGS, err := b.getPkgConfigFlags(a, p)
  2881  	if err != nil {
  2882  		return err
  2883  	}
  2884  
  2885  	// Run SWIG on each .swig and .swigcxx file.
  2886  	// Each run will generate two files, a .go file and a .c or .cxx file.
  2887  	// The .go file will use import "C" and is to be processed by cgo.
  2888  	// For -cover test or build runs, this needs to happen after the cover
  2889  	// tool is run; we don't want to instrument swig-generated Go files,
  2890  	// see issue #64661.
  2891  	if p.UsesSwig() {
  2892  		if err := b.swig(a, objdir, pcCFLAGS); err != nil {
  2893  			return err
  2894  		}
  2895  		outGo, _, _ := b.swigOutputs(p, objdir)
  2896  		cgofiles = append(cgofiles, outGo...)
  2897  	}
  2898  
  2899  	cgoExe := base.Tool("cgo")
  2900  	cgofiles = mkAbsFiles(p.Dir, cgofiles)
  2901  
  2902  	cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS, err := b.CFlags(p)
  2903  	if err != nil {
  2904  		return err
  2905  	}
  2906  
  2907  	cgoCPPFLAGS = append(cgoCPPFLAGS, pcCFLAGS...)
  2908  	cgoLDFLAGS = append(cgoLDFLAGS, pcLDFLAGS...)
  2909  	// If we are compiling Objective-C code, then we need to link against libobjc
  2910  	if len(p.MFiles) > 0 {
  2911  		cgoLDFLAGS = append(cgoLDFLAGS, "-lobjc")
  2912  	}
  2913  
  2914  	// Likewise for Fortran, except there are many Fortran compilers.
  2915  	// Support gfortran out of the box and let others pass the correct link options
  2916  	// via CGO_LDFLAGS
  2917  	if len(p.FFiles) > 0 {
  2918  		fc := cfg.Getenv("FC")
  2919  		if fc == "" {
  2920  			fc = "gfortran"
  2921  		}
  2922  		if strings.Contains(fc, "gfortran") {
  2923  			cgoLDFLAGS = append(cgoLDFLAGS, "-lgfortran")
  2924  		}
  2925  	}
  2926  
  2927  	// Scrutinize CFLAGS and related for flags that might cause
  2928  	// problems if we are using internal linking (for example, use of
  2929  	// plugins, LTO, etc) by calling a helper routine that builds on
  2930  	// the existing CGO flags allow-lists. If we see anything
  2931  	// suspicious, emit a special token file "preferlinkext" (known to
  2932  	// the linker) in the object file to signal the that it should not
  2933  	// try to link internally and should revert to external linking.
  2934  	// The token we pass is a suggestion, not a mandate; if a user is
  2935  	// explicitly asking for a specific linkmode via the "-linkmode"
  2936  	// flag, the token will be ignored. NB: in theory we could ditch
  2937  	// the token approach and just pass a flag to the linker when we
  2938  	// eventually invoke it, and the linker flag could then be
  2939  	// documented (although coming up with a simple explanation of the
  2940  	// flag might be challenging). For more context see issues #58619,
  2941  	// #58620, and #58848.
  2942  	flagSources := []string{"CGO_CFLAGS", "CGO_CXXFLAGS", "CGO_FFLAGS"}
  2943  	flagLists := [][]string{cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS}
  2944  	notCompatibleWithInternalLinking := flagsNotCompatibleWithInternalLinking(flagSources, flagLists)
  2945  
  2946  	if cfg.BuildMSan {
  2947  		cgoCFLAGS = append([]string{"-fsanitize=memory"}, cgoCFLAGS...)
  2948  		cgoLDFLAGS = append([]string{"-fsanitize=memory"}, cgoLDFLAGS...)
  2949  	}
  2950  	if cfg.BuildASan {
  2951  		cgoCFLAGS = append([]string{"-fsanitize=address"}, cgoCFLAGS...)
  2952  		cgoLDFLAGS = append([]string{"-fsanitize=address"}, cgoLDFLAGS...)
  2953  	}
  2954  
  2955  	// Allows including _cgo_export.h, as well as the user's .h files,
  2956  	// from .[ch] files in the package.
  2957  	cgoCPPFLAGS = append(cgoCPPFLAGS, "-I", objdir)
  2958  
  2959  	// cgo
  2960  	// TODO: CGO_FLAGS?
  2961  	gofiles := []string{objdir + "_cgo_gotypes.go"}
  2962  	cfiles := []string{objdir + "_cgo_export.c"}
  2963  	for _, fn := range cgofiles {
  2964  		f := strings.TrimSuffix(filepath.Base(fn), ".go")
  2965  		gofiles = append(gofiles, objdir+f+".cgo1.go")
  2966  		cfiles = append(cfiles, objdir+f+".cgo2.c")
  2967  	}
  2968  
  2969  	// TODO: make cgo not depend on $GOARCH?
  2970  
  2971  	cgoflags := []string{}
  2972  	if p.Standard && p.ImportPath == "runtime/cgo" {
  2973  		cgoflags = append(cgoflags, "-import_runtime_cgo=false")
  2974  	}
  2975  	if p.Standard && (p.ImportPath == "runtime/race" || p.ImportPath == "runtime/msan" || p.ImportPath == "runtime/cgo" || p.ImportPath == "runtime/asan") {
  2976  		cgoflags = append(cgoflags, "-import_syscall=false")
  2977  	}
  2978  
  2979  	// cgoLDFLAGS, which includes p.CgoLDFLAGS, can be very long.
  2980  	// Pass it to cgo on the command line, so that we use a
  2981  	// response file if necessary.
  2982  	//
  2983  	// These flags are recorded in the generated _cgo_gotypes.go file
  2984  	// using //go:cgo_ldflag directives, the compiler records them in the
  2985  	// object file for the package, and then the Go linker passes them
  2986  	// along to the host linker. At this point in the code, cgoLDFLAGS
  2987  	// consists of the original $CGO_LDFLAGS (unchecked) and all the
  2988  	// flags put together from source code (checked).
  2989  	cgoenv := b.cCompilerEnv()
  2990  	cgoenv = append(cgoenv, cfgChangedEnv...)
  2991  	var ldflagsOption []string
  2992  	if len(cgoLDFLAGS) > 0 {
  2993  		flags := make([]string, len(cgoLDFLAGS))
  2994  		for i, f := range cgoLDFLAGS {
  2995  			flags[i] = strconv.Quote(f)
  2996  		}
  2997  		ldflagsOption = []string{"-ldflags=" + strings.Join(flags, " ")}
  2998  
  2999  		// Remove CGO_LDFLAGS from the environment.
  3000  		cgoenv = append(cgoenv, "CGO_LDFLAGS=")
  3001  	}
  3002  
  3003  	if cfg.BuildToolchainName == "gccgo" {
  3004  		if b.gccSupportsFlag([]string{BuildToolchain.compiler()}, "-fsplit-stack") {
  3005  			cgoCFLAGS = append(cgoCFLAGS, "-fsplit-stack")
  3006  		}
  3007  		cgoflags = append(cgoflags, "-gccgo")
  3008  		if pkgpath := gccgoPkgpath(p); pkgpath != "" {
  3009  			cgoflags = append(cgoflags, "-gccgopkgpath="+pkgpath)
  3010  		}
  3011  		if !BuildToolchain.(gccgoToolchain).supportsCgoIncomplete(b, a) {
  3012  			cgoflags = append(cgoflags, "-gccgo_define_cgoincomplete")
  3013  		}
  3014  	}
  3015  
  3016  	switch cfg.BuildBuildmode {
  3017  	case "c-archive", "c-shared":
  3018  		// Tell cgo that if there are any exported functions
  3019  		// it should generate a header file that C code can
  3020  		// #include.
  3021  		cgoflags = append(cgoflags, "-exportheader="+objdir+"_cgo_install.h")
  3022  	}
  3023  
  3024  	// Rewrite overlaid paths in cgo files.
  3025  	// cgo adds //line and #line pragmas in generated files with these paths.
  3026  	var trimpath []string
  3027  	for i := range cgofiles {
  3028  		path := mkAbs(p.Dir, cgofiles[i])
  3029  		if fsys.Replaced(path) {
  3030  			actual := fsys.Actual(path)
  3031  			cgofiles[i] = actual
  3032  			trimpath = append(trimpath, actual+"=>"+path)
  3033  		}
  3034  	}
  3035  	if len(trimpath) > 0 {
  3036  		cgoflags = append(cgoflags, "-trimpath", strings.Join(trimpath, ";"))
  3037  	}
  3038  
  3039  	if err := sh.run(p.Dir, p.ImportPath, cgoenv, cfg.BuildToolexec, cgoExe, "-objdir", objdir, "-importpath", p.ImportPath, cgoflags, ldflagsOption, "--", cgoCPPFLAGS, cgoCFLAGS, cgofiles); err != nil {
  3040  		return err
  3041  	}
  3042  
  3043  	a.Provider = &runCgoProvider{
  3044  		CFLAGS:                          str.StringList(cgoCPPFLAGS, cgoCFLAGS),
  3045  		CXXFLAGS:                        str.StringList(cgoCPPFLAGS, cgoCXXFLAGS),
  3046  		FFLAGS:                          str.StringList(cgoCPPFLAGS, cgoFFLAGS),
  3047  		LDFLAGS:                         cgoLDFLAGS,
  3048  		notCompatibleForInternalLinking: notCompatibleWithInternalLinking,
  3049  		nonGoOverlay:                    a.nonGoOverlay,
  3050  		goFiles:                         gofiles,
  3051  	}
  3052  
  3053  	return nil
  3054  }
  3055  
  3056  func (b *Builder) processCgoOutputs(a *Action, runCgoProvider *runCgoProvider, cgoExe, objdir string) (outGo, outObj []string, err error) {
  3057  	outGo = slices.Clip(runCgoProvider.goFiles)
  3058  
  3059  	// TODO(matloob): Pretty much the only thing this function is doing is
  3060  	// producing the dynimport go files. But we should be able to compile
  3061  	// those separately from the package itself: we just need to get the
  3062  	// compiled output to the linker. That means that we can remove the
  3063  	// dependency of this build action on the outputs of the cgo compile actions
  3064  	// (though we'd still need to depend on the runCgo action of course).
  3065  
  3066  	sh := b.Shell(a)
  3067  
  3068  	// Output the preferlinkext file if the run cgo action determined this package
  3069  	// was not compatible for internal linking based on CFLAGS, CXXFLAGS, or FFLAGS.
  3070  	if runCgoProvider.notCompatibleForInternalLinking {
  3071  		tokenFile := objdir + "preferlinkext"
  3072  		if err := sh.writeFile(tokenFile, nil); err != nil {
  3073  			return nil, nil, err
  3074  		}
  3075  		outObj = append(outObj, tokenFile)
  3076  	}
  3077  
  3078  	var collectAction *Action
  3079  	for _, dep := range a.Deps {
  3080  		if dep.Mode == "collect cgo" {
  3081  			collectAction = dep
  3082  		}
  3083  	}
  3084  	if collectAction == nil {
  3085  		base.Fatalf("internal error: no cgo collect action")
  3086  	}
  3087  	for _, dep := range collectAction.Deps {
  3088  		outObj = append(outObj, dep.Target)
  3089  	}
  3090  
  3091  	switch cfg.BuildToolchainName {
  3092  	case "gc":
  3093  		importGo := objdir + "_cgo_import.go"
  3094  		dynOutGo, dynOutObj, err := b.dynimport(a, objdir, importGo, cgoExe, runCgoProvider.CFLAGS, runCgoProvider.LDFLAGS, outObj)
  3095  		if err != nil {
  3096  			return nil, nil, err
  3097  		}
  3098  		if dynOutGo != "" {
  3099  			outGo = append(outGo, dynOutGo)
  3100  		}
  3101  		if dynOutObj != "" {
  3102  			outObj = append(outObj, dynOutObj)
  3103  		}
  3104  
  3105  	case "gccgo":
  3106  		defunC := objdir + "_cgo_defun.c"
  3107  		defunObj := objdir + "_cgo_defun.o"
  3108  		if err := BuildToolchain.cc(b, a, defunObj, defunC); err != nil {
  3109  			return nil, nil, err
  3110  		}
  3111  		outObj = append(outObj, defunObj)
  3112  
  3113  	default:
  3114  		noCompiler()
  3115  	}
  3116  
  3117  	// Double check the //go:cgo_ldflag comments in the generated files.
  3118  	// The compiler only permits such comments in files whose base name
  3119  	// starts with "_cgo_". Make sure that the comments in those files
  3120  	// are safe. This is a backstop against people somehow smuggling
  3121  	// such a comment into a file generated by cgo.
  3122  	if cfg.BuildToolchainName == "gc" && !cfg.BuildN {
  3123  		var flags []string
  3124  		for _, f := range outGo {
  3125  			if !strings.HasPrefix(filepath.Base(f), "_cgo_") {
  3126  				continue
  3127  			}
  3128  
  3129  			src, err := os.ReadFile(f)
  3130  			if err != nil {
  3131  				return nil, nil, err
  3132  			}
  3133  
  3134  			const cgoLdflag = "//go:cgo_ldflag"
  3135  			idx := bytes.Index(src, []byte(cgoLdflag))
  3136  			for idx >= 0 {
  3137  				// We are looking at //go:cgo_ldflag.
  3138  				// Find start of line.
  3139  				start := bytes.LastIndex(src[:idx], []byte("\n"))
  3140  				if start == -1 {
  3141  					start = 0
  3142  				}
  3143  
  3144  				// Find end of line.
  3145  				end := bytes.Index(src[idx:], []byte("\n"))
  3146  				if end == -1 {
  3147  					end = len(src)
  3148  				} else {
  3149  					end += idx
  3150  				}
  3151  
  3152  				// Check for first line comment in line.
  3153  				// We don't worry about /* */ comments,
  3154  				// which normally won't appear in files
  3155  				// generated by cgo.
  3156  				commentStart := bytes.Index(src[start:], []byte("//"))
  3157  				commentStart += start
  3158  				// If that line comment is //go:cgo_ldflag,
  3159  				// it's a match.
  3160  				if bytes.HasPrefix(src[commentStart:], []byte(cgoLdflag)) {
  3161  					// Pull out the flag, and unquote it.
  3162  					// This is what the compiler does.
  3163  					flag := string(src[idx+len(cgoLdflag) : end])
  3164  					flag = strings.TrimSpace(flag)
  3165  					flag = strings.Trim(flag, `"`)
  3166  					flags = append(flags, flag)
  3167  				}
  3168  				src = src[end:]
  3169  				idx = bytes.Index(src, []byte(cgoLdflag))
  3170  			}
  3171  		}
  3172  
  3173  		// We expect to find the contents of cgoLDFLAGS used when running the CGO action in flags.
  3174  		if len(runCgoProvider.LDFLAGS) > 0 {
  3175  		outer:
  3176  			for i := range flags {
  3177  				for j, f := range runCgoProvider.LDFLAGS {
  3178  					if f != flags[i+j] {
  3179  						continue outer
  3180  					}
  3181  				}
  3182  				flags = append(flags[:i], flags[i+len(runCgoProvider.LDFLAGS):]...)
  3183  				break
  3184  			}
  3185  		}
  3186  
  3187  		if err := checkLinkerFlags("LDFLAGS", "go:cgo_ldflag", flags); err != nil {
  3188  			return nil, nil, err
  3189  		}
  3190  	}
  3191  
  3192  	return outGo, outObj, nil
  3193  }
  3194  
  3195  // flagsNotCompatibleWithInternalLinking scans the list of cgo
  3196  // compiler flags (C/C++/Fortran) looking for flags that might cause
  3197  // problems if the build in question uses internal linking. The
  3198  // primary culprits are use of plugins or use of LTO, but we err on
  3199  // the side of caution, supporting only those flags that are on the
  3200  // allow-list for safe flags from security perspective. Return is TRUE
  3201  // if a sensitive flag is found, FALSE otherwise.
  3202  func flagsNotCompatibleWithInternalLinking(sourceList []string, flagListList [][]string) bool {
  3203  	for i := range sourceList {
  3204  		sn := sourceList[i]
  3205  		fll := flagListList[i]
  3206  		if err := checkCompilerFlagsForInternalLink(sn, sn, fll); err != nil {
  3207  			return true
  3208  		}
  3209  	}
  3210  	return false
  3211  }
  3212  
  3213  // dynimport creates a Go source file named importGo containing
  3214  // //go:cgo_import_dynamic directives for each symbol or library
  3215  // dynamically imported by the object files outObj.
  3216  // dynOutGo, if not empty, is a new Go file to build as part of the package.
  3217  // dynOutObj, if not empty, is a new file to add to the generated archive.
  3218  func (b *Builder) dynimport(a *Action, objdir, importGo, cgoExe string, cflags, cgoLDFLAGS, outObj []string) (dynOutGo, dynOutObj string, err error) {
  3219  	p := a.Package
  3220  	sh := b.Shell(a)
  3221  
  3222  	cfile := objdir + "_cgo_main.c"
  3223  	ofile := objdir + "_cgo_main.o"
  3224  	if err := b.gcc(a, objdir, ofile, cflags, cfile); err != nil {
  3225  		return "", "", err
  3226  	}
  3227  
  3228  	// Gather .syso files from this package and all (transitive) dependencies.
  3229  	var syso []string
  3230  	seen := make(map[*Action]bool)
  3231  	var gatherSyso func(*Action)
  3232  	gatherSyso = func(a1 *Action) {
  3233  		if seen[a1] {
  3234  			return
  3235  		}
  3236  		seen[a1] = true
  3237  		if p1 := a1.Package; p1 != nil {
  3238  			syso = append(syso, mkAbsFiles(p1.Dir, p1.SysoFiles)...)
  3239  		}
  3240  		for _, a2 := range a1.Deps {
  3241  			gatherSyso(a2)
  3242  		}
  3243  	}
  3244  	gatherSyso(a)
  3245  	sort.Strings(syso)
  3246  	str.Uniq(&syso)
  3247  	linkobj := str.StringList(ofile, outObj, syso)
  3248  	dynobj := objdir + "_cgo_.o"
  3249  
  3250  	ldflags := cgoLDFLAGS
  3251  	if (cfg.Goarch == "arm" && cfg.Goos == "linux") || cfg.Goos == "android" {
  3252  		if !slices.Contains(ldflags, "-no-pie") {
  3253  			// we need to use -pie for Linux/ARM to get accurate imported sym (added in https://golang.org/cl/5989058)
  3254  			// this seems to be outdated, but we don't want to break existing builds depending on this (Issue 45940)
  3255  			ldflags = append(ldflags, "-pie")
  3256  		}
  3257  		if slices.Contains(ldflags, "-pie") && slices.Contains(ldflags, "-static") {
  3258  			// -static -pie doesn't make sense, and causes link errors.
  3259  			// Issue 26197.
  3260  			n := make([]string, 0, len(ldflags)-1)
  3261  			for _, flag := range ldflags {
  3262  				if flag != "-static" {
  3263  					n = append(n, flag)
  3264  				}
  3265  			}
  3266  			ldflags = n
  3267  		}
  3268  	}
  3269  	if err := b.gccld(a, objdir, dynobj, ldflags, linkobj); err != nil {
  3270  		// We only need this information for internal linking.
  3271  		// If this link fails, mark the object as requiring
  3272  		// external linking. This link can fail for things like
  3273  		// syso files that have unexpected dependencies.
  3274  		// cmd/link explicitly looks for the name "dynimportfail".
  3275  		// See issue #52863.
  3276  		fail := objdir + "dynimportfail"
  3277  		if err := sh.writeFile(fail, nil); err != nil {
  3278  			return "", "", err
  3279  		}
  3280  		return "", fail, nil
  3281  	}
  3282  
  3283  	// cgo -dynimport
  3284  	var cgoflags []string
  3285  	if p.Standard && p.ImportPath == "runtime/cgo" {
  3286  		cgoflags = []string{"-dynlinker"} // record path to dynamic linker
  3287  	}
  3288  	err = sh.run(base.Cwd(), p.ImportPath, b.cCompilerEnv(), cfg.BuildToolexec, cgoExe, "-dynpackage", p.Name, "-dynimport", dynobj, "-dynout", importGo, cgoflags)
  3289  	if err != nil {
  3290  		return "", "", err
  3291  	}
  3292  	return importGo, "", nil
  3293  }
  3294  
  3295  // Run SWIG on all SWIG input files.
  3296  // TODO: Don't build a shared library, once SWIG emits the necessary
  3297  // pragmas for external linking.
  3298  func (b *Builder) swig(a *Action, objdir string, pcCFLAGS []string) error {
  3299  	p := a.Package
  3300  
  3301  	if err := b.swigVersionCheck(); err != nil {
  3302  		return err
  3303  	}
  3304  
  3305  	intgosize, err := b.swigIntSize(objdir)
  3306  	if err != nil {
  3307  		return err
  3308  	}
  3309  
  3310  	for _, f := range p.SwigFiles {
  3311  		if err := b.swigOne(a, f, objdir, pcCFLAGS, false, intgosize); err != nil {
  3312  			return err
  3313  		}
  3314  	}
  3315  	for _, f := range p.SwigCXXFiles {
  3316  		if b.swigOne(a, f, objdir, pcCFLAGS, true, intgosize); err != nil {
  3317  			return err
  3318  		}
  3319  	}
  3320  	return nil
  3321  }
  3322  
  3323  func (b *Builder) swigOutputs(p *load.Package, objdir string) (outGo, outC, outCXX []string) {
  3324  	for _, f := range p.SwigFiles {
  3325  		goFile, cFile := swigOneOutputs(f, objdir, false)
  3326  		outGo = append(outGo, goFile)
  3327  		outC = append(outC, cFile)
  3328  	}
  3329  	for _, f := range p.SwigCXXFiles {
  3330  		goFile, cxxFile := swigOneOutputs(f, objdir, true)
  3331  		outGo = append(outGo, goFile)
  3332  		outCXX = append(outCXX, cxxFile)
  3333  	}
  3334  	return outGo, outC, outCXX
  3335  }
  3336  
  3337  // Make sure SWIG is new enough.
  3338  var (
  3339  	swigCheckOnce sync.Once
  3340  	swigCheck     error
  3341  )
  3342  
  3343  func (b *Builder) swigDoVersionCheck() error {
  3344  	sh := b.BackgroundShell()
  3345  	out, err := sh.runOut(".", nil, "swig", "-version")
  3346  	if err != nil {
  3347  		return err
  3348  	}
  3349  	re := regexp.MustCompile(`[vV]ersion +(\d+)([.]\d+)?([.]\d+)?`)
  3350  	matches := re.FindSubmatch(out)
  3351  	if matches == nil {
  3352  		// Can't find version number; hope for the best.
  3353  		return nil
  3354  	}
  3355  
  3356  	major, err := strconv.Atoi(string(matches[1]))
  3357  	if err != nil {
  3358  		// Can't find version number; hope for the best.
  3359  		return nil
  3360  	}
  3361  	const errmsg = "must have SWIG version >= 3.0.6"
  3362  	if major < 3 {
  3363  		return errors.New(errmsg)
  3364  	}
  3365  	if major > 3 {
  3366  		// 4.0 or later
  3367  		return nil
  3368  	}
  3369  
  3370  	// We have SWIG version 3.x.
  3371  	if len(matches[2]) > 0 {
  3372  		minor, err := strconv.Atoi(string(matches[2][1:]))
  3373  		if err != nil {
  3374  			return nil
  3375  		}
  3376  		if minor > 0 {
  3377  			// 3.1 or later
  3378  			return nil
  3379  		}
  3380  	}
  3381  
  3382  	// We have SWIG version 3.0.x.
  3383  	if len(matches[3]) > 0 {
  3384  		patch, err := strconv.Atoi(string(matches[3][1:]))
  3385  		if err != nil {
  3386  			return nil
  3387  		}
  3388  		if patch < 6 {
  3389  			// Before 3.0.6.
  3390  			return errors.New(errmsg)
  3391  		}
  3392  	}
  3393  
  3394  	return nil
  3395  }
  3396  
  3397  func (b *Builder) swigVersionCheck() error {
  3398  	swigCheckOnce.Do(func() {
  3399  		swigCheck = b.swigDoVersionCheck()
  3400  	})
  3401  	return swigCheck
  3402  }
  3403  
  3404  // Find the value to pass for the -intgosize option to swig.
  3405  var (
  3406  	swigIntSizeOnce  sync.Once
  3407  	swigIntSize      string
  3408  	swigIntSizeError error
  3409  )
  3410  
  3411  // This code fails to build if sizeof(int) <= 32
  3412  const swigIntSizeCode = `
  3413  package main
  3414  const i int = 1 << 32
  3415  `
  3416  
  3417  // Determine the size of int on the target system for the -intgosize option
  3418  // of swig >= 2.0.9. Run only once.
  3419  func (b *Builder) swigDoIntSize(objdir string) (intsize string, err error) {
  3420  	if cfg.BuildN {
  3421  		return "$INTBITS", nil
  3422  	}
  3423  	src := filepath.Join(b.WorkDir, "swig_intsize.go")
  3424  	if err = os.WriteFile(src, []byte(swigIntSizeCode), 0666); err != nil {
  3425  		return
  3426  	}
  3427  	srcs := []string{src}
  3428  
  3429  	p := load.GoFilesPackage(modload.NewState(), context.TODO(), load.PackageOpts{}, srcs)
  3430  
  3431  	if _, _, e := BuildToolchain.gc(b, &Action{Mode: "swigDoIntSize", Package: p, Objdir: objdir}, "", nil, nil, "", false, "", srcs); e != nil {
  3432  		return "32", nil
  3433  	}
  3434  	return "64", nil
  3435  }
  3436  
  3437  // Determine the size of int on the target system for the -intgosize option
  3438  // of swig >= 2.0.9.
  3439  func (b *Builder) swigIntSize(objdir string) (intsize string, err error) {
  3440  	swigIntSizeOnce.Do(func() {
  3441  		swigIntSize, swigIntSizeError = b.swigDoIntSize(objdir)
  3442  	})
  3443  	return swigIntSize, swigIntSizeError
  3444  }
  3445  
  3446  // Run SWIG on one SWIG input file.
  3447  func (b *Builder) swigOne(a *Action, file, objdir string, pcCFLAGS []string, cxx bool, intgosize string) error {
  3448  	p := a.Package
  3449  	sh := b.Shell(a)
  3450  
  3451  	cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p)
  3452  	if err != nil {
  3453  		return err
  3454  	}
  3455  
  3456  	var cflags []string
  3457  	if cxx {
  3458  		cflags = str.StringList(cgoCPPFLAGS, pcCFLAGS, cgoCXXFLAGS)
  3459  	} else {
  3460  		cflags = str.StringList(cgoCPPFLAGS, pcCFLAGS, cgoCFLAGS)
  3461  	}
  3462  
  3463  	base := swigBase(file, cxx)
  3464  	newGoFile, outC := swigOneOutputs(file, objdir, cxx)
  3465  
  3466  	gccgo := cfg.BuildToolchainName == "gccgo"
  3467  
  3468  	// swig
  3469  	args := []string{
  3470  		"-go",
  3471  		"-cgo",
  3472  		"-intgosize", intgosize,
  3473  		"-module", base,
  3474  		"-o", outC,
  3475  		"-outdir", objdir,
  3476  	}
  3477  
  3478  	for _, f := range cflags {
  3479  		if len(f) > 3 && f[:2] == "-I" {
  3480  			args = append(args, f)
  3481  		}
  3482  	}
  3483  
  3484  	if gccgo {
  3485  		args = append(args, "-gccgo")
  3486  		if pkgpath := gccgoPkgpath(p); pkgpath != "" {
  3487  			args = append(args, "-go-pkgpath", pkgpath)
  3488  		}
  3489  	}
  3490  	if cxx {
  3491  		args = append(args, "-c++")
  3492  	}
  3493  
  3494  	out, err := sh.runOut(p.Dir, nil, "swig", args, file)
  3495  	if err != nil && (bytes.Contains(out, []byte("-intgosize")) || bytes.Contains(out, []byte("-cgo"))) {
  3496  		return errors.New("must have SWIG version >= 3.0.6")
  3497  	}
  3498  	if err := sh.reportCmd("", "", out, err); err != nil {
  3499  		return err
  3500  	}
  3501  
  3502  	// If the input was x.swig, the output is x.go in the objdir.
  3503  	// But there might be an x.go in the original dir too, and if it
  3504  	// uses cgo as well, cgo will be processing both and will
  3505  	// translate both into x.cgo1.go in the objdir, overwriting one.
  3506  	// Rename x.go to _x_swig.go (newGoFile) to avoid this problem.
  3507  	// We ignore files in the original dir that begin with underscore
  3508  	// so _x_swig.go cannot conflict with an original file we were
  3509  	// going to compile.
  3510  	goFile := objdir + base + ".go"
  3511  	if cfg.BuildX || cfg.BuildN {
  3512  		sh.ShowCmd("", "mv %s %s", goFile, newGoFile)
  3513  	}
  3514  	if !cfg.BuildN {
  3515  		if err := os.Rename(goFile, newGoFile); err != nil {
  3516  			return err
  3517  		}
  3518  	}
  3519  
  3520  	return nil
  3521  }
  3522  
  3523  func swigBase(file string, cxx bool) string {
  3524  	n := 5 // length of ".swig"
  3525  	if cxx {
  3526  		n = 8 // length of ".swigcxx"
  3527  	}
  3528  	return file[:len(file)-n]
  3529  }
  3530  
  3531  func swigOneOutputs(file, objdir string, cxx bool) (outGo, outC string) {
  3532  	base := swigBase(file, cxx)
  3533  	gccBase := base + "_wrap."
  3534  	gccExt := "c"
  3535  	if cxx {
  3536  		gccExt = "cxx"
  3537  	}
  3538  
  3539  	newGoFile := objdir + "_" + base + "_swig.go"
  3540  	cFile := objdir + gccBase + gccExt
  3541  	return newGoFile, cFile
  3542  }
  3543  
  3544  // disableBuildID adjusts a linker command line to avoid creating a
  3545  // build ID when creating an object file rather than an executable or
  3546  // shared library. Some systems, such as Ubuntu, always add
  3547  // --build-id to every link, but we don't want a build ID when we are
  3548  // producing an object file. On some of those system a plain -r (not
  3549  // -Wl,-r) will turn off --build-id, but clang 3.0 doesn't support a
  3550  // plain -r. I don't know how to turn off --build-id when using clang
  3551  // other than passing a trailing --build-id=none. So that is what we
  3552  // do, but only on systems likely to support it, which is to say,
  3553  // systems that normally use gold or the GNU linker.
  3554  func (b *Builder) disableBuildID(ldflags []string) []string {
  3555  	switch cfg.Goos {
  3556  	case "android", "dragonfly", "linux", "netbsd":
  3557  		ldflags = append(ldflags, "-Wl,--build-id=none")
  3558  	}
  3559  	return ldflags
  3560  }
  3561  
  3562  // mkAbsFiles converts files into a list of absolute files,
  3563  // assuming they were originally relative to dir,
  3564  // and returns that new list.
  3565  func mkAbsFiles(dir string, files []string) []string {
  3566  	abs := make([]string, len(files))
  3567  	for i, f := range files {
  3568  		if !filepath.IsAbs(f) {
  3569  			f = filepath.Join(dir, f)
  3570  		}
  3571  		abs[i] = f
  3572  	}
  3573  	return abs
  3574  }
  3575  
  3576  // actualFiles applies fsys.Actual to the list of files.
  3577  func actualFiles(files []string) []string {
  3578  	a := make([]string, len(files))
  3579  	for i, f := range files {
  3580  		a[i] = fsys.Actual(f)
  3581  	}
  3582  	return a
  3583  }
  3584  
  3585  // passLongArgsInResponseFiles modifies cmd such that, for
  3586  // certain programs, long arguments are passed in "response files", a
  3587  // file on disk with the arguments, with one arg per line. An actual
  3588  // argument starting with '@' means that the rest of the argument is
  3589  // a filename of arguments to expand.
  3590  //
  3591  // See issues 18468 (Windows) and 37768 (Darwin).
  3592  func passLongArgsInResponseFiles(cmd *exec.Cmd) (cleanup func()) {
  3593  	cleanup = func() {} // no cleanup by default
  3594  
  3595  	var argLen int
  3596  	for _, arg := range cmd.Args {
  3597  		argLen += len(arg)
  3598  	}
  3599  
  3600  	// If we're not approaching 32KB of args, just pass args normally.
  3601  	// (use 30KB instead to be conservative; not sure how accounting is done)
  3602  	if !useResponseFile(cmd.Path, argLen) {
  3603  		return
  3604  	}
  3605  
  3606  	tf, err := os.CreateTemp("", "args")
  3607  	if err != nil {
  3608  		log.Fatalf("error writing long arguments to response file: %v", err)
  3609  	}
  3610  	cleanup = func() { os.Remove(tf.Name()) }
  3611  	var buf bytes.Buffer
  3612  	for _, arg := range cmd.Args[1:] {
  3613  		fmt.Fprintf(&buf, "%s\n", encodeArg(arg))
  3614  	}
  3615  	if _, err := tf.Write(buf.Bytes()); err != nil {
  3616  		tf.Close()
  3617  		cleanup()
  3618  		log.Fatalf("error writing long arguments to response file: %v", err)
  3619  	}
  3620  	if err := tf.Close(); err != nil {
  3621  		cleanup()
  3622  		log.Fatalf("error writing long arguments to response file: %v", err)
  3623  	}
  3624  	cmd.Args = []string{cmd.Args[0], "@" + tf.Name()}
  3625  	return cleanup
  3626  }
  3627  
  3628  func useResponseFile(path string, argLen int) bool {
  3629  	// Unless the program uses objabi.Flagparse, which understands
  3630  	// response files, don't use response files.
  3631  	// TODO: Note that other toolchains like CC are missing here for now.
  3632  	prog := strings.TrimSuffix(filepath.Base(path), ".exe")
  3633  	switch prog {
  3634  	case "compile", "link", "cgo", "asm", "cover":
  3635  	default:
  3636  		return false
  3637  	}
  3638  
  3639  	if argLen > sys.ExecArgLengthLimit {
  3640  		return true
  3641  	}
  3642  
  3643  	// On the Go build system, use response files about 10% of the
  3644  	// time, just to exercise this codepath.
  3645  	isBuilder := os.Getenv("GO_BUILDER_NAME") != ""
  3646  	if isBuilder && rand.Intn(10) == 0 {
  3647  		return true
  3648  	}
  3649  
  3650  	return false
  3651  }
  3652  
  3653  // encodeArg encodes an argument for response file writing.
  3654  func encodeArg(arg string) string {
  3655  	// If there aren't any characters we need to reencode, fastpath out.
  3656  	if !strings.ContainsAny(arg, "\\\n") {
  3657  		return arg
  3658  	}
  3659  	var b strings.Builder
  3660  	for _, r := range arg {
  3661  		switch r {
  3662  		case '\\':
  3663  			b.WriteByte('\\')
  3664  			b.WriteByte('\\')
  3665  		case '\n':
  3666  			b.WriteByte('\\')
  3667  			b.WriteByte('n')
  3668  		default:
  3669  			b.WriteRune(r)
  3670  		}
  3671  	}
  3672  	return b.String()
  3673  }
  3674  

View as plain text