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

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

View as plain text