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

View as plain text