Help us shape the future of Go! Take the 2026 Go Developer Survey

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

View as plain text