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

View as plain text