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

View as plain text