github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/cmd/go/internal/work/exec.go (about)

     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  	"encoding/json"
    12  	"errors"
    13  	"fmt"
    14  	"io"
    15  	"io/ioutil"
    16  	"log"
    17  	"math/rand"
    18  	"os"
    19  	"os/exec"
    20  	"path/filepath"
    21  	"regexp"
    22  	"runtime"
    23  	"strconv"
    24  	"strings"
    25  	"sync"
    26  	"time"
    27  
    28  	"cmd/go/internal/base"
    29  	"cmd/go/internal/cache"
    30  	"cmd/go/internal/cfg"
    31  	"cmd/go/internal/load"
    32  	"cmd/go/internal/str"
    33  )
    34  
    35  // actionList returns the list of actions in the dag rooted at root
    36  // as visited in a depth-first post-order traversal.
    37  func actionList(root *Action) []*Action {
    38  	seen := map[*Action]bool{}
    39  	all := []*Action{}
    40  	var walk func(*Action)
    41  	walk = func(a *Action) {
    42  		if seen[a] {
    43  			return
    44  		}
    45  		seen[a] = true
    46  		for _, a1 := range a.Deps {
    47  			walk(a1)
    48  		}
    49  		all = append(all, a)
    50  	}
    51  	walk(root)
    52  	return all
    53  }
    54  
    55  // do runs the action graph rooted at root.
    56  func (b *Builder) Do(root *Action) {
    57  	if c := cache.Default(); c != nil && !b.IsCmdList {
    58  		// If we're doing real work, take time at the end to trim the cache.
    59  		defer c.Trim()
    60  	}
    61  
    62  	// Build list of all actions, assigning depth-first post-order priority.
    63  	// The original implementation here was a true queue
    64  	// (using a channel) but it had the effect of getting
    65  	// distracted by low-level leaf actions to the detriment
    66  	// of completing higher-level actions. The order of
    67  	// work does not matter much to overall execution time,
    68  	// but when running "go test std" it is nice to see each test
    69  	// results as soon as possible. The priorities assigned
    70  	// ensure that, all else being equal, the execution prefers
    71  	// to do what it would have done first in a simple depth-first
    72  	// dependency order traversal.
    73  	all := actionList(root)
    74  	for i, a := range all {
    75  		a.priority = i
    76  	}
    77  
    78  	if cfg.DebugActiongraph != "" {
    79  		js := actionGraphJSON(root)
    80  		if err := ioutil.WriteFile(cfg.DebugActiongraph, []byte(js), 0666); err != nil {
    81  			fmt.Fprintf(os.Stderr, "go: writing action graph: %v\n", err)
    82  			base.SetExitStatus(1)
    83  		}
    84  	}
    85  
    86  	b.readySema = make(chan bool, len(all))
    87  
    88  	// Initialize per-action execution state.
    89  	for _, a := range all {
    90  		for _, a1 := range a.Deps {
    91  			a1.triggers = append(a1.triggers, a)
    92  		}
    93  		a.pending = len(a.Deps)
    94  		if a.pending == 0 {
    95  			b.ready.push(a)
    96  			b.readySema <- true
    97  		}
    98  	}
    99  
   100  	// Handle runs a single action and takes care of triggering
   101  	// any actions that are runnable as a result.
   102  	handle := func(a *Action) {
   103  		var err error
   104  
   105  		if a.Func != nil && (!a.Failed || a.IgnoreFail) {
   106  			if err == nil {
   107  				err = a.Func(b, a)
   108  			}
   109  		}
   110  
   111  		// The actions run in parallel but all the updates to the
   112  		// shared work state are serialized through b.exec.
   113  		b.exec.Lock()
   114  		defer b.exec.Unlock()
   115  
   116  		if err != nil {
   117  			if err == errPrintedOutput {
   118  				base.SetExitStatus(2)
   119  			} else {
   120  				base.Errorf("%s", err)
   121  			}
   122  			a.Failed = true
   123  		}
   124  
   125  		for _, a0 := range a.triggers {
   126  			if a.Failed {
   127  				a0.Failed = true
   128  			}
   129  			if a0.pending--; a0.pending == 0 {
   130  				b.ready.push(a0)
   131  				b.readySema <- true
   132  			}
   133  		}
   134  
   135  		if a == root {
   136  			close(b.readySema)
   137  		}
   138  	}
   139  
   140  	var wg sync.WaitGroup
   141  
   142  	// Kick off goroutines according to parallelism.
   143  	// If we are using the -n flag (just printing commands)
   144  	// drop the parallelism to 1, both to make the output
   145  	// deterministic and because there is no real work anyway.
   146  	par := cfg.BuildP
   147  	if cfg.BuildN {
   148  		par = 1
   149  	}
   150  	for i := 0; i < par; i++ {
   151  		wg.Add(1)
   152  		go func() {
   153  			defer wg.Done()
   154  			for {
   155  				select {
   156  				case _, ok := <-b.readySema:
   157  					if !ok {
   158  						return
   159  					}
   160  					// Receiving a value from b.readySema entitles
   161  					// us to take from the ready queue.
   162  					b.exec.Lock()
   163  					a := b.ready.pop()
   164  					b.exec.Unlock()
   165  					handle(a)
   166  				case <-base.Interrupted:
   167  					base.SetExitStatus(1)
   168  					return
   169  				}
   170  			}
   171  		}()
   172  	}
   173  
   174  	wg.Wait()
   175  }
   176  
   177  // buildActionID computes the action ID for a build action.
   178  func (b *Builder) buildActionID(a *Action) cache.ActionID {
   179  	p := a.Package
   180  	h := cache.NewHash("build " + p.ImportPath)
   181  
   182  	// Configuration independent of compiler toolchain.
   183  	// Note: buildmode has already been accounted for in buildGcflags
   184  	// and should not be inserted explicitly. Most buildmodes use the
   185  	// same compiler settings and can reuse each other's results.
   186  	// If not, the reason is already recorded in buildGcflags.
   187  	fmt.Fprintf(h, "compile\n")
   188  	// The compiler hides the exact value of $GOROOT
   189  	// when building things in GOROOT,
   190  	// but it does not hide the exact value of $GOPATH.
   191  	// Include the full dir in that case.
   192  	// Assume b.WorkDir is being trimmed properly.
   193  	if !p.Goroot && !strings.HasPrefix(p.Dir, b.WorkDir) {
   194  		fmt.Fprintf(h, "dir %s\n", p.Dir)
   195  	}
   196  	fmt.Fprintf(h, "goos %s goarch %s\n", cfg.Goos, cfg.Goarch)
   197  	fmt.Fprintf(h, "import %q\n", p.ImportPath)
   198  	fmt.Fprintf(h, "omitdebug %v standard %v local %v prefix %q\n", p.Internal.OmitDebug, p.Standard, p.Internal.Local, p.Internal.LocalPrefix)
   199  	if p.Internal.ForceLibrary {
   200  		fmt.Fprintf(h, "forcelibrary\n")
   201  	}
   202  	if len(p.CgoFiles)+len(p.SwigFiles) > 0 {
   203  		fmt.Fprintf(h, "cgo %q\n", b.toolID("cgo"))
   204  		cppflags, cflags, cxxflags, fflags, ldflags, _ := b.CFlags(p)
   205  		fmt.Fprintf(h, "CC=%q %q %q %q\n", b.ccExe(), cppflags, cflags, ldflags)
   206  		if len(p.CXXFiles)+len(p.SwigFiles) > 0 {
   207  			fmt.Fprintf(h, "CXX=%q %q\n", b.cxxExe(), cxxflags)
   208  		}
   209  		if len(p.FFiles) > 0 {
   210  			fmt.Fprintf(h, "FC=%q %q\n", b.fcExe(), fflags)
   211  		}
   212  		// TODO(rsc): Should we include the SWIG version or Fortran/GCC/G++/Objective-C compiler versions?
   213  	}
   214  	if p.Internal.CoverMode != "" {
   215  		fmt.Fprintf(h, "cover %q %q\n", p.Internal.CoverMode, b.toolID("cover"))
   216  	}
   217  	fmt.Fprintf(h, "modinfo %q\n", p.Internal.BuildInfo)
   218  
   219  	// Configuration specific to compiler toolchain.
   220  	switch cfg.BuildToolchainName {
   221  	default:
   222  		base.Fatalf("buildActionID: unknown build toolchain %q", cfg.BuildToolchainName)
   223  	case "gc":
   224  		fmt.Fprintf(h, "compile %s %q %q\n", b.toolID("compile"), forcedGcflags, p.Internal.Gcflags)
   225  		if len(p.SFiles) > 0 {
   226  			fmt.Fprintf(h, "asm %q %q %q\n", b.toolID("asm"), forcedAsmflags, p.Internal.Asmflags)
   227  		}
   228  		// GO386, GOARM, GOMIPS, etc.
   229  		baseArch := strings.TrimSuffix(cfg.BuildContext.GOARCH, "le")
   230  		fmt.Fprintf(h, "GO$GOARCH=%s\n", os.Getenv("GO"+strings.ToUpper(baseArch)))
   231  
   232  		// TODO(rsc): Convince compiler team not to add more magic environment variables,
   233  		// or perhaps restrict the environment variables passed to subprocesses.
   234  		magic := []string{
   235  			"GOCLOBBERDEADHASH",
   236  			"GOSSAFUNC",
   237  			"GO_SSA_PHI_LOC_CUTOFF",
   238  			"GOSSAHASH",
   239  		}
   240  		for _, env := range magic {
   241  			if x := os.Getenv(env); x != "" {
   242  				fmt.Fprintf(h, "magic %s=%s\n", env, x)
   243  			}
   244  		}
   245  		if os.Getenv("GOSSAHASH") != "" {
   246  			for i := 0; ; i++ {
   247  				env := fmt.Sprintf("GOSSAHASH%d", i)
   248  				x := os.Getenv(env)
   249  				if x == "" {
   250  					break
   251  				}
   252  				fmt.Fprintf(h, "magic %s=%s\n", env, x)
   253  			}
   254  		}
   255  		if os.Getenv("GSHS_LOGFILE") != "" {
   256  			// Clumsy hack. Compiler writes to this log file,
   257  			// so do not allow use of cache at all.
   258  			// We will still write to the cache but it will be
   259  			// essentially unfindable.
   260  			fmt.Fprintf(h, "nocache %d\n", time.Now().UnixNano())
   261  		}
   262  
   263  	case "gccgo":
   264  		id, err := b.gccgoToolID(BuildToolchain.compiler(), "go")
   265  		if err != nil {
   266  			base.Fatalf("%v", err)
   267  		}
   268  		fmt.Fprintf(h, "compile %s %q %q\n", id, forcedGccgoflags, p.Internal.Gccgoflags)
   269  		fmt.Fprintf(h, "pkgpath %s\n", gccgoPkgpath(p))
   270  		if len(p.SFiles) > 0 {
   271  			id, err = b.gccgoToolID(BuildToolchain.compiler(), "assembler-with-cpp")
   272  			// Ignore error; different assembler versions
   273  			// are unlikely to make any difference anyhow.
   274  			fmt.Fprintf(h, "asm %q\n", id)
   275  		}
   276  	}
   277  
   278  	// Input files.
   279  	inputFiles := str.StringList(
   280  		p.GoFiles,
   281  		p.CgoFiles,
   282  		p.CFiles,
   283  		p.CXXFiles,
   284  		p.FFiles,
   285  		p.MFiles,
   286  		p.HFiles,
   287  		p.SFiles,
   288  		p.SysoFiles,
   289  		p.SwigFiles,
   290  		p.SwigCXXFiles,
   291  	)
   292  	for _, file := range inputFiles {
   293  		fmt.Fprintf(h, "file %s %s\n", file, b.fileHash(filepath.Join(p.Dir, file)))
   294  	}
   295  	for _, a1 := range a.Deps {
   296  		p1 := a1.Package
   297  		if p1 != nil {
   298  			fmt.Fprintf(h, "import %s %s\n", p1.ImportPath, contentID(a1.buildID))
   299  		}
   300  	}
   301  
   302  	return h.Sum()
   303  }
   304  
   305  // needCgoHdr reports whether the actions triggered by this one
   306  // expect to be able to access the cgo-generated header file.
   307  func (b *Builder) needCgoHdr(a *Action) bool {
   308  	// If this build triggers a header install, run cgo to get the header.
   309  	if !b.IsCmdList && (a.Package.UsesCgo() || a.Package.UsesSwig()) && (cfg.BuildBuildmode == "c-archive" || cfg.BuildBuildmode == "c-shared") {
   310  		for _, t1 := range a.triggers {
   311  			if t1.Mode == "install header" {
   312  				return true
   313  			}
   314  		}
   315  		for _, t1 := range a.triggers {
   316  			for _, t2 := range t1.triggers {
   317  				if t2.Mode == "install header" {
   318  					return true
   319  				}
   320  			}
   321  		}
   322  	}
   323  	return false
   324  }
   325  
   326  // allowedVersion reports whether the version v is an allowed version of go
   327  // (one that we can compile).
   328  // v is known to be of the form "1.23".
   329  func allowedVersion(v string) bool {
   330  	// Special case: no requirement.
   331  	if v == "" {
   332  		return true
   333  	}
   334  	// Special case "1.0" means "go1", which is OK.
   335  	if v == "1.0" {
   336  		return true
   337  	}
   338  	// Otherwise look through release tags of form "go1.23" for one that matches.
   339  	for _, tag := range cfg.BuildContext.ReleaseTags {
   340  		if strings.HasPrefix(tag, "go") && tag[2:] == v {
   341  			return true
   342  		}
   343  	}
   344  	return false
   345  }
   346  
   347  const (
   348  	needBuild uint32 = 1 << iota
   349  	needCgoHdr
   350  	needVet
   351  	needCompiledGoFiles
   352  	needStale
   353  )
   354  
   355  // build is the action for building a single package.
   356  // Note that any new influence on this logic must be reported in b.buildActionID above as well.
   357  func (b *Builder) build(a *Action) (err error) {
   358  	p := a.Package
   359  
   360  	bit := func(x uint32, b bool) uint32 {
   361  		if b {
   362  			return x
   363  		}
   364  		return 0
   365  	}
   366  
   367  	cached := false
   368  	need := bit(needBuild, !b.IsCmdList || b.NeedExport) |
   369  		bit(needCgoHdr, b.needCgoHdr(a)) |
   370  		bit(needVet, a.needVet) |
   371  		bit(needCompiledGoFiles, b.NeedCompiledGoFiles)
   372  
   373  	if !p.BinaryOnly {
   374  		if b.useCache(a, p, b.buildActionID(a), p.Target) {
   375  			// We found the main output in the cache.
   376  			// If we don't need any other outputs, we can stop.
   377  			need &^= needBuild
   378  			if b.NeedExport {
   379  				p.Export = a.built
   380  			}
   381  			if need&needCompiledGoFiles != 0 && b.loadCachedSrcFiles(a) {
   382  				need &^= needCompiledGoFiles
   383  			}
   384  			// Otherwise, we need to write files to a.Objdir (needVet, needCgoHdr).
   385  			// Remember that we might have them in cache
   386  			// and check again after we create a.Objdir.
   387  			cached = true
   388  			a.output = []byte{} // start saving output in case we miss any cache results
   389  		}
   390  
   391  		// Source files might be cached, even if the full action is not
   392  		// (e.g., go list -compiled -find).
   393  		if !cached && need&needCompiledGoFiles != 0 && b.loadCachedSrcFiles(a) {
   394  			need &^= needCompiledGoFiles
   395  		}
   396  
   397  		if need == 0 {
   398  			return nil
   399  		}
   400  		defer b.flushOutput(a)
   401  	}
   402  
   403  	defer func() {
   404  		if err != nil && err != errPrintedOutput {
   405  			err = fmt.Errorf("go build %s: %v", a.Package.ImportPath, err)
   406  		}
   407  		if err != nil && b.IsCmdList && b.NeedError && p.Error == nil {
   408  			p.Error = &load.PackageError{Err: err.Error()}
   409  		}
   410  	}()
   411  	if cfg.BuildN {
   412  		// In -n mode, print a banner between packages.
   413  		// The banner is five lines so that when changes to
   414  		// different sections of the bootstrap script have to
   415  		// be merged, the banners give patch something
   416  		// to use to find its context.
   417  		b.Print("\n#\n# " + a.Package.ImportPath + "\n#\n\n")
   418  	}
   419  
   420  	if cfg.BuildV {
   421  		b.Print(a.Package.ImportPath + "\n")
   422  	}
   423  
   424  	if a.Package.BinaryOnly {
   425  		_, err := os.Stat(a.Package.Target)
   426  		if err == nil {
   427  			a.built = a.Package.Target
   428  			a.Target = a.Package.Target
   429  			if b.NeedExport {
   430  				a.Package.Export = a.Package.Target
   431  			}
   432  			a.buildID = b.fileHash(a.Package.Target)
   433  			a.Package.Stale = false
   434  			a.Package.StaleReason = "binary-only package"
   435  			return nil
   436  		}
   437  		a.Package.Stale = true
   438  		a.Package.StaleReason = "missing or invalid binary-only package"
   439  		if b.IsCmdList {
   440  			return nil
   441  		}
   442  		return fmt.Errorf("missing or invalid binary-only package; expected file %q", a.Package.Target)
   443  	}
   444  
   445  	if err := b.Mkdir(a.Objdir); err != nil {
   446  		return err
   447  	}
   448  	objdir := a.Objdir
   449  
   450  	if cached {
   451  		if need&needCgoHdr != 0 && b.loadCachedCgoHdr(a) {
   452  			need &^= needCgoHdr
   453  		}
   454  
   455  		// Load cached vet config, but only if that's all we have left
   456  		// (need == needVet, not testing just the one bit).
   457  		// If we are going to do a full build anyway,
   458  		// we're going to regenerate the files below anyway.
   459  		if need == needVet && b.loadCachedVet(a) {
   460  			need &^= needVet
   461  		}
   462  		if need == 0 {
   463  			return nil
   464  		}
   465  	}
   466  
   467  	// make target directory
   468  	dir, _ := filepath.Split(a.Target)
   469  	if dir != "" {
   470  		if err := b.Mkdir(dir); err != nil {
   471  			return err
   472  		}
   473  	}
   474  
   475  	gofiles := str.StringList(a.Package.GoFiles)
   476  	cgofiles := str.StringList(a.Package.CgoFiles)
   477  	cfiles := str.StringList(a.Package.CFiles)
   478  	sfiles := str.StringList(a.Package.SFiles)
   479  	cxxfiles := str.StringList(a.Package.CXXFiles)
   480  	var objects, cgoObjects, pcCFLAGS, pcLDFLAGS []string
   481  
   482  	if a.Package.UsesCgo() || a.Package.UsesSwig() {
   483  		if pcCFLAGS, pcLDFLAGS, err = b.getPkgConfigFlags(a.Package); err != nil {
   484  			return
   485  		}
   486  	}
   487  
   488  	// Run SWIG on each .swig and .swigcxx file.
   489  	// Each run will generate two files, a .go file and a .c or .cxx file.
   490  	// The .go file will use import "C" and is to be processed by cgo.
   491  	if a.Package.UsesSwig() {
   492  		outGo, outC, outCXX, err := b.swig(a, a.Package, objdir, pcCFLAGS)
   493  		if err != nil {
   494  			return err
   495  		}
   496  		cgofiles = append(cgofiles, outGo...)
   497  		cfiles = append(cfiles, outC...)
   498  		cxxfiles = append(cxxfiles, outCXX...)
   499  	}
   500  
   501  	// If we're doing coverage, preprocess the .go files and put them in the work directory
   502  	if a.Package.Internal.CoverMode != "" {
   503  		for i, file := range str.StringList(gofiles, cgofiles) {
   504  			var sourceFile string
   505  			var coverFile string
   506  			var key string
   507  			if strings.HasSuffix(file, ".cgo1.go") {
   508  				// cgo files have absolute paths
   509  				base := filepath.Base(file)
   510  				sourceFile = file
   511  				coverFile = objdir + base
   512  				key = strings.TrimSuffix(base, ".cgo1.go") + ".go"
   513  			} else {
   514  				sourceFile = filepath.Join(a.Package.Dir, file)
   515  				coverFile = objdir + file
   516  				key = file
   517  			}
   518  			coverFile = strings.TrimSuffix(coverFile, ".go") + ".cover.go"
   519  			cover := a.Package.Internal.CoverVars[key]
   520  			if cover == nil || base.IsTestFile(file) {
   521  				// Not covering this file.
   522  				continue
   523  			}
   524  			if err := b.cover(a, coverFile, sourceFile, cover.Var); err != nil {
   525  				return err
   526  			}
   527  			if i < len(gofiles) {
   528  				gofiles[i] = coverFile
   529  			} else {
   530  				cgofiles[i-len(gofiles)] = coverFile
   531  			}
   532  		}
   533  	}
   534  
   535  	// Run cgo.
   536  	if a.Package.UsesCgo() || a.Package.UsesSwig() {
   537  		// In a package using cgo, cgo compiles the C, C++ and assembly files with gcc.
   538  		// There is one exception: runtime/cgo's job is to bridge the
   539  		// cgo and non-cgo worlds, so it necessarily has files in both.
   540  		// In that case gcc only gets the gcc_* files.
   541  		var gccfiles []string
   542  		gccfiles = append(gccfiles, cfiles...)
   543  		cfiles = nil
   544  		if a.Package.Standard && a.Package.ImportPath == "runtime/cgo" {
   545  			filter := func(files, nongcc, gcc []string) ([]string, []string) {
   546  				for _, f := range files {
   547  					if strings.HasPrefix(f, "gcc_") {
   548  						gcc = append(gcc, f)
   549  					} else {
   550  						nongcc = append(nongcc, f)
   551  					}
   552  				}
   553  				return nongcc, gcc
   554  			}
   555  			sfiles, gccfiles = filter(sfiles, sfiles[:0], gccfiles)
   556  		} else {
   557  			for _, sfile := range sfiles {
   558  				data, err := ioutil.ReadFile(filepath.Join(a.Package.Dir, sfile))
   559  				if err == nil {
   560  					if bytes.HasPrefix(data, []byte("TEXT")) || bytes.Contains(data, []byte("\nTEXT")) ||
   561  						bytes.HasPrefix(data, []byte("DATA")) || bytes.Contains(data, []byte("\nDATA")) ||
   562  						bytes.HasPrefix(data, []byte("GLOBL")) || bytes.Contains(data, []byte("\nGLOBL")) {
   563  						return fmt.Errorf("package using cgo has Go assembly file %s", sfile)
   564  					}
   565  				}
   566  			}
   567  			gccfiles = append(gccfiles, sfiles...)
   568  			sfiles = nil
   569  		}
   570  
   571  		outGo, outObj, err := b.cgo(a, base.Tool("cgo"), objdir, pcCFLAGS, pcLDFLAGS, mkAbsFiles(a.Package.Dir, cgofiles), gccfiles, cxxfiles, a.Package.MFiles, a.Package.FFiles)
   572  		if err != nil {
   573  			return err
   574  		}
   575  		if cfg.BuildToolchainName == "gccgo" {
   576  			cgoObjects = append(cgoObjects, a.Objdir+"_cgo_flags")
   577  		}
   578  		cgoObjects = append(cgoObjects, outObj...)
   579  		gofiles = append(gofiles, outGo...)
   580  
   581  		switch cfg.BuildBuildmode {
   582  		case "c-archive", "c-shared":
   583  			b.cacheCgoHdr(a)
   584  		}
   585  	}
   586  
   587  	var srcfiles []string // .go and non-.go
   588  	srcfiles = append(srcfiles, gofiles...)
   589  	srcfiles = append(srcfiles, sfiles...)
   590  	srcfiles = append(srcfiles, cfiles...)
   591  	srcfiles = append(srcfiles, cxxfiles...)
   592  	b.cacheSrcFiles(a, srcfiles)
   593  
   594  	// Running cgo generated the cgo header.
   595  	need &^= needCgoHdr
   596  
   597  	// Sanity check only, since Package.load already checked as well.
   598  	if len(gofiles) == 0 {
   599  		return &load.NoGoError{Package: a.Package}
   600  	}
   601  
   602  	// Prepare Go vet config if needed.
   603  	if need&needVet != 0 {
   604  		buildVetConfig(a, srcfiles)
   605  		need &^= needVet
   606  	}
   607  	if need&needCompiledGoFiles != 0 {
   608  		if !b.loadCachedSrcFiles(a) {
   609  			return fmt.Errorf("failed to cache compiled Go files")
   610  		}
   611  		need &^= needCompiledGoFiles
   612  	}
   613  	if need == 0 {
   614  		// Nothing left to do.
   615  		return nil
   616  	}
   617  
   618  	// Collect symbol ABI requirements from assembly.
   619  	symabis, err := BuildToolchain.symabis(b, a, sfiles)
   620  	if err != nil {
   621  		return err
   622  	}
   623  
   624  	// Prepare Go import config.
   625  	// We start it off with a comment so it can't be empty, so icfg.Bytes() below is never nil.
   626  	// It should never be empty anyway, but there have been bugs in the past that resulted
   627  	// in empty configs, which then unfortunately turn into "no config passed to compiler",
   628  	// and the compiler falls back to looking in pkg itself, which mostly works,
   629  	// except when it doesn't.
   630  	var icfg bytes.Buffer
   631  	fmt.Fprintf(&icfg, "# import config\n")
   632  	for i, raw := range a.Package.Internal.RawImports {
   633  		final := a.Package.Imports[i]
   634  		if final != raw {
   635  			fmt.Fprintf(&icfg, "importmap %s=%s\n", raw, final)
   636  		}
   637  	}
   638  	for _, a1 := range a.Deps {
   639  		p1 := a1.Package
   640  		if p1 == nil || p1.ImportPath == "" || a1.built == "" {
   641  			continue
   642  		}
   643  		fmt.Fprintf(&icfg, "packagefile %s=%s\n", p1.ImportPath, a1.built)
   644  	}
   645  
   646  	if p.Internal.BuildInfo != "" && cfg.ModulesEnabled {
   647  		if err := b.writeFile(objdir+"_gomod_.go", load.ModInfoProg(p.Internal.BuildInfo)); err != nil {
   648  			return err
   649  		}
   650  		gofiles = append(gofiles, objdir+"_gomod_.go")
   651  	}
   652  
   653  	// Compile Go.
   654  	objpkg := objdir + "_pkg_.a"
   655  	ofile, out, err := BuildToolchain.gc(b, a, objpkg, icfg.Bytes(), symabis, len(sfiles) > 0, gofiles)
   656  	if len(out) > 0 {
   657  		output := b.processOutput(out)
   658  		if p.Module != nil && !allowedVersion(p.Module.GoVersion) {
   659  			output += "note: module requires Go " + p.Module.GoVersion + "\n"
   660  		}
   661  		b.showOutput(a, a.Package.Dir, a.Package.Desc(), output)
   662  		if err != nil {
   663  			return errPrintedOutput
   664  		}
   665  	}
   666  	if err != nil {
   667  		if p.Module != nil && !allowedVersion(p.Module.GoVersion) {
   668  			b.showOutput(a, a.Package.Dir, a.Package.Desc(), "note: module requires Go "+p.Module.GoVersion)
   669  		}
   670  		return err
   671  	}
   672  	if ofile != objpkg {
   673  		objects = append(objects, ofile)
   674  	}
   675  
   676  	// Copy .h files named for goos or goarch or goos_goarch
   677  	// to names using GOOS and GOARCH.
   678  	// For example, defs_linux_amd64.h becomes defs_GOOS_GOARCH.h.
   679  	_goos_goarch := "_" + cfg.Goos + "_" + cfg.Goarch
   680  	_goos := "_" + cfg.Goos
   681  	_goarch := "_" + cfg.Goarch
   682  	for _, file := range a.Package.HFiles {
   683  		name, ext := fileExtSplit(file)
   684  		switch {
   685  		case strings.HasSuffix(name, _goos_goarch):
   686  			targ := file[:len(name)-len(_goos_goarch)] + "_GOOS_GOARCH." + ext
   687  			if err := b.copyFile(objdir+targ, filepath.Join(a.Package.Dir, file), 0666, true); err != nil {
   688  				return err
   689  			}
   690  		case strings.HasSuffix(name, _goarch):
   691  			targ := file[:len(name)-len(_goarch)] + "_GOARCH." + ext
   692  			if err := b.copyFile(objdir+targ, filepath.Join(a.Package.Dir, file), 0666, true); err != nil {
   693  				return err
   694  			}
   695  		case strings.HasSuffix(name, _goos):
   696  			targ := file[:len(name)-len(_goos)] + "_GOOS." + ext
   697  			if err := b.copyFile(objdir+targ, filepath.Join(a.Package.Dir, file), 0666, true); err != nil {
   698  				return err
   699  			}
   700  		}
   701  	}
   702  
   703  	for _, file := range cfiles {
   704  		out := file[:len(file)-len(".c")] + ".o"
   705  		if err := BuildToolchain.cc(b, a, objdir+out, file); err != nil {
   706  			return err
   707  		}
   708  		objects = append(objects, out)
   709  	}
   710  
   711  	// Assemble .s files.
   712  	if len(sfiles) > 0 {
   713  		ofiles, err := BuildToolchain.asm(b, a, sfiles)
   714  		if err != nil {
   715  			return err
   716  		}
   717  		objects = append(objects, ofiles...)
   718  	}
   719  
   720  	// For gccgo on ELF systems, we write the build ID as an assembler file.
   721  	// This lets us set the SHF_EXCLUDE flag.
   722  	// This is read by readGccgoArchive in cmd/internal/buildid/buildid.go.
   723  	if a.buildID != "" && cfg.BuildToolchainName == "gccgo" {
   724  		switch cfg.Goos {
   725  		case "aix", "android", "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "solaris":
   726  			asmfile, err := b.gccgoBuildIDFile(a)
   727  			if err != nil {
   728  				return err
   729  			}
   730  			ofiles, err := BuildToolchain.asm(b, a, []string{asmfile})
   731  			if err != nil {
   732  				return err
   733  			}
   734  			objects = append(objects, ofiles...)
   735  		}
   736  	}
   737  
   738  	// NOTE(rsc): On Windows, it is critically important that the
   739  	// gcc-compiled objects (cgoObjects) be listed after the ordinary
   740  	// objects in the archive. I do not know why this is.
   741  	// https://golang.org/issue/2601
   742  	objects = append(objects, cgoObjects...)
   743  
   744  	// Add system object files.
   745  	for _, syso := range a.Package.SysoFiles {
   746  		objects = append(objects, filepath.Join(a.Package.Dir, syso))
   747  	}
   748  
   749  	// Pack into archive in objdir directory.
   750  	// If the Go compiler wrote an archive, we only need to add the
   751  	// object files for non-Go sources to the archive.
   752  	// If the Go compiler wrote an archive and the package is entirely
   753  	// Go sources, there is no pack to execute at all.
   754  	if len(objects) > 0 {
   755  		if err := BuildToolchain.pack(b, a, objpkg, objects); err != nil {
   756  			return err
   757  		}
   758  	}
   759  
   760  	if err := b.updateBuildID(a, objpkg, true); err != nil {
   761  		return err
   762  	}
   763  
   764  	a.built = objpkg
   765  	return nil
   766  }
   767  
   768  func (b *Builder) cacheObjdirFile(a *Action, c *cache.Cache, name string) error {
   769  	f, err := os.Open(a.Objdir + name)
   770  	if err != nil {
   771  		return err
   772  	}
   773  	defer f.Close()
   774  	_, _, err = c.Put(cache.Subkey(a.actionID, name), f)
   775  	return err
   776  }
   777  
   778  func (b *Builder) findCachedObjdirFile(a *Action, c *cache.Cache, name string) (string, error) {
   779  	file, _, err := c.GetFile(cache.Subkey(a.actionID, name))
   780  	if err != nil {
   781  		return "", err
   782  	}
   783  	return file, nil
   784  }
   785  
   786  func (b *Builder) loadCachedObjdirFile(a *Action, c *cache.Cache, name string) error {
   787  	cached, err := b.findCachedObjdirFile(a, c, name)
   788  	if err != nil {
   789  		return err
   790  	}
   791  	return b.copyFile(a.Objdir+name, cached, 0666, true)
   792  }
   793  
   794  func (b *Builder) cacheCgoHdr(a *Action) {
   795  	c := cache.Default()
   796  	if c == nil {
   797  		return
   798  	}
   799  	b.cacheObjdirFile(a, c, "_cgo_install.h")
   800  }
   801  
   802  func (b *Builder) loadCachedCgoHdr(a *Action) bool {
   803  	c := cache.Default()
   804  	if c == nil {
   805  		return false
   806  	}
   807  	err := b.loadCachedObjdirFile(a, c, "_cgo_install.h")
   808  	return err == nil
   809  }
   810  
   811  func (b *Builder) cacheSrcFiles(a *Action, srcfiles []string) {
   812  	c := cache.Default()
   813  	if c == nil {
   814  		return
   815  	}
   816  	var buf bytes.Buffer
   817  	for _, file := range srcfiles {
   818  		if !strings.HasPrefix(file, a.Objdir) {
   819  			// not generated
   820  			buf.WriteString("./")
   821  			buf.WriteString(file)
   822  			buf.WriteString("\n")
   823  			continue
   824  		}
   825  		name := file[len(a.Objdir):]
   826  		buf.WriteString(name)
   827  		buf.WriteString("\n")
   828  		if err := b.cacheObjdirFile(a, c, name); err != nil {
   829  			return
   830  		}
   831  	}
   832  	c.PutBytes(cache.Subkey(a.actionID, "srcfiles"), buf.Bytes())
   833  }
   834  
   835  func (b *Builder) loadCachedVet(a *Action) bool {
   836  	c := cache.Default()
   837  	if c == nil {
   838  		return false
   839  	}
   840  	list, _, err := c.GetBytes(cache.Subkey(a.actionID, "srcfiles"))
   841  	if err != nil {
   842  		return false
   843  	}
   844  	var srcfiles []string
   845  	for _, name := range strings.Split(string(list), "\n") {
   846  		if name == "" { // end of list
   847  			continue
   848  		}
   849  		if strings.HasPrefix(name, "./") {
   850  			srcfiles = append(srcfiles, name[2:])
   851  			continue
   852  		}
   853  		if err := b.loadCachedObjdirFile(a, c, name); err != nil {
   854  			return false
   855  		}
   856  		srcfiles = append(srcfiles, a.Objdir+name)
   857  	}
   858  	buildVetConfig(a, srcfiles)
   859  	return true
   860  }
   861  
   862  func (b *Builder) loadCachedSrcFiles(a *Action) bool {
   863  	c := cache.Default()
   864  	if c == nil {
   865  		return false
   866  	}
   867  	list, _, err := c.GetBytes(cache.Subkey(a.actionID, "srcfiles"))
   868  	if err != nil {
   869  		return false
   870  	}
   871  	var files []string
   872  	for _, name := range strings.Split(string(list), "\n") {
   873  		if name == "" { // end of list
   874  			continue
   875  		}
   876  		if strings.HasPrefix(name, "./") {
   877  			files = append(files, name[len("./"):])
   878  			continue
   879  		}
   880  		file, err := b.findCachedObjdirFile(a, c, name)
   881  		if err != nil {
   882  			return false
   883  		}
   884  		files = append(files, file)
   885  	}
   886  	a.Package.CompiledGoFiles = files
   887  	return true
   888  }
   889  
   890  // vetConfig is the configuration passed to vet describing a single package.
   891  type vetConfig struct {
   892  	ID         string   // package ID (example: "fmt [fmt.test]")
   893  	Compiler   string   // compiler name (gc, gccgo)
   894  	Dir        string   // directory containing package
   895  	ImportPath string   // canonical import path ("package path")
   896  	GoFiles    []string // absolute paths to package source files
   897  	NonGoFiles []string // absolute paths to package non-Go files
   898  
   899  	ImportMap   map[string]string // map import path in source code to package path
   900  	PackageFile map[string]string // map package path to .a file with export data
   901  	Standard    map[string]bool   // map package path to whether it's in the standard library
   902  	PackageVetx map[string]string // map package path to vetx data from earlier vet run
   903  	VetxOnly    bool              // only compute vetx data; don't report detected problems
   904  	VetxOutput  string            // write vetx data to this output file
   905  
   906  	SucceedOnTypecheckFailure bool // awful hack; see #18395 and below
   907  }
   908  
   909  func buildVetConfig(a *Action, srcfiles []string) {
   910  	// Classify files based on .go extension.
   911  	// srcfiles does not include raw cgo files.
   912  	var gofiles, nongofiles []string
   913  	for _, name := range srcfiles {
   914  		if strings.HasSuffix(name, ".go") {
   915  			gofiles = append(gofiles, name)
   916  		} else {
   917  			nongofiles = append(nongofiles, name)
   918  		}
   919  	}
   920  
   921  	// Pass list of absolute paths to vet,
   922  	// so that vet's error messages will use absolute paths,
   923  	// so that we can reformat them relative to the directory
   924  	// in which the go command is invoked.
   925  	vcfg := &vetConfig{
   926  		ID:          a.Package.ImportPath,
   927  		Compiler:    cfg.BuildToolchainName,
   928  		Dir:         a.Package.Dir,
   929  		GoFiles:     mkAbsFiles(a.Package.Dir, gofiles),
   930  		NonGoFiles:  mkAbsFiles(a.Package.Dir, nongofiles),
   931  		ImportPath:  a.Package.ImportPath,
   932  		ImportMap:   make(map[string]string),
   933  		PackageFile: make(map[string]string),
   934  		Standard:    make(map[string]bool),
   935  	}
   936  	a.vetCfg = vcfg
   937  	for i, raw := range a.Package.Internal.RawImports {
   938  		final := a.Package.Imports[i]
   939  		vcfg.ImportMap[raw] = final
   940  	}
   941  
   942  	// Compute the list of mapped imports in the vet config
   943  	// so that we can add any missing mappings below.
   944  	vcfgMapped := make(map[string]bool)
   945  	for _, p := range vcfg.ImportMap {
   946  		vcfgMapped[p] = true
   947  	}
   948  
   949  	for _, a1 := range a.Deps {
   950  		p1 := a1.Package
   951  		if p1 == nil || p1.ImportPath == "" {
   952  			continue
   953  		}
   954  		// Add import mapping if needed
   955  		// (for imports like "runtime/cgo" that appear only in generated code).
   956  		if !vcfgMapped[p1.ImportPath] {
   957  			vcfg.ImportMap[p1.ImportPath] = p1.ImportPath
   958  		}
   959  		if a1.built != "" {
   960  			vcfg.PackageFile[p1.ImportPath] = a1.built
   961  		}
   962  		if p1.Standard {
   963  			vcfg.Standard[p1.ImportPath] = true
   964  		}
   965  	}
   966  }
   967  
   968  // VetTool is the path to an alternate vet tool binary.
   969  // The caller is expected to set it (if needed) before executing any vet actions.
   970  var VetTool string
   971  
   972  // VetFlags are the flags to pass to vet.
   973  // The caller is expected to set them before executing any vet actions.
   974  var VetFlags []string
   975  
   976  func (b *Builder) vet(a *Action) error {
   977  	// a.Deps[0] is the build of the package being vetted.
   978  	// a.Deps[1] is the build of the "fmt" package.
   979  
   980  	a.Failed = false // vet of dependency may have failed but we can still succeed
   981  
   982  	if a.Deps[0].Failed {
   983  		// The build of the package has failed. Skip vet check.
   984  		// Vet could return export data for non-typecheck errors,
   985  		// but we ignore it because the package cannot be compiled.
   986  		return nil
   987  	}
   988  
   989  	vcfg := a.Deps[0].vetCfg
   990  	if vcfg == nil {
   991  		// Vet config should only be missing if the build failed.
   992  		return fmt.Errorf("vet config not found")
   993  	}
   994  
   995  	vcfg.VetxOnly = a.VetxOnly
   996  	vcfg.VetxOutput = a.Objdir + "vet.out"
   997  	vcfg.PackageVetx = make(map[string]string)
   998  
   999  	h := cache.NewHash("vet " + a.Package.ImportPath)
  1000  	fmt.Fprintf(h, "vet %q\n", b.toolID("vet"))
  1001  
  1002  	// Note: We could decide that vet should compute export data for
  1003  	// all analyses, in which case we don't need to include the flags here.
  1004  	// But that would mean that if an analysis causes problems like
  1005  	// unexpected crashes there would be no way to turn it off.
  1006  	// It seems better to let the flags disable export analysis too.
  1007  	fmt.Fprintf(h, "vetflags %q\n", VetFlags)
  1008  
  1009  	fmt.Fprintf(h, "pkg %q\n", a.Deps[0].actionID)
  1010  	for _, a1 := range a.Deps {
  1011  		if a1.Mode == "vet" && a1.built != "" {
  1012  			fmt.Fprintf(h, "vetout %q %s\n", a1.Package.ImportPath, b.fileHash(a1.built))
  1013  			vcfg.PackageVetx[a1.Package.ImportPath] = a1.built
  1014  		}
  1015  	}
  1016  	key := cache.ActionID(h.Sum())
  1017  
  1018  	if vcfg.VetxOnly {
  1019  		if c := cache.Default(); c != nil && !cfg.BuildA {
  1020  			if file, _, err := c.GetFile(key); err == nil {
  1021  				a.built = file
  1022  				return nil
  1023  			}
  1024  		}
  1025  	}
  1026  
  1027  	// TODO(adonovan): delete this when we use the new vet printf checker.
  1028  	// https://github.com/golang/go/issues/28756
  1029  	if vcfg.ImportMap["fmt"] == "" {
  1030  		a1 := a.Deps[1]
  1031  		vcfg.ImportMap["fmt"] = "fmt"
  1032  		if a1.built != "" {
  1033  			vcfg.PackageFile["fmt"] = a1.built
  1034  		}
  1035  		vcfg.Standard["fmt"] = true
  1036  	}
  1037  
  1038  	// During go test, ignore type-checking failures during vet.
  1039  	// We only run vet if the compilation has succeeded,
  1040  	// so at least for now assume the bug is in vet.
  1041  	// We know of at least #18395.
  1042  	// TODO(rsc,gri): Try to remove this for Go 1.11.
  1043  	//
  1044  	// Disabled 2018-04-20. Let's see if we can do without it.
  1045  	// vcfg.SucceedOnTypecheckFailure = cfg.CmdName == "test"
  1046  
  1047  	js, err := json.MarshalIndent(vcfg, "", "\t")
  1048  	if err != nil {
  1049  		return fmt.Errorf("internal error marshaling vet config: %v", err)
  1050  	}
  1051  	js = append(js, '\n')
  1052  	if err := b.writeFile(a.Objdir+"vet.cfg", js); err != nil {
  1053  		return err
  1054  	}
  1055  
  1056  	env := b.cCompilerEnv()
  1057  	if cfg.BuildToolchainName == "gccgo" {
  1058  		env = append(env, "GCCGO="+BuildToolchain.compiler())
  1059  	}
  1060  
  1061  	p := a.Package
  1062  	tool := VetTool
  1063  	if tool == "" {
  1064  		tool = base.Tool("vet")
  1065  	}
  1066  	runErr := b.run(a, p.Dir, p.ImportPath, env, cfg.BuildToolexec, tool, VetFlags, a.Objdir+"vet.cfg")
  1067  
  1068  	// If vet wrote export data, save it for input to future vets.
  1069  	if f, err := os.Open(vcfg.VetxOutput); err == nil {
  1070  		a.built = vcfg.VetxOutput
  1071  		if c := cache.Default(); c != nil {
  1072  			c.Put(key, f)
  1073  		}
  1074  		f.Close()
  1075  	}
  1076  
  1077  	return runErr
  1078  }
  1079  
  1080  // linkActionID computes the action ID for a link action.
  1081  func (b *Builder) linkActionID(a *Action) cache.ActionID {
  1082  	p := a.Package
  1083  	h := cache.NewHash("link " + p.ImportPath)
  1084  
  1085  	// Toolchain-independent configuration.
  1086  	fmt.Fprintf(h, "link\n")
  1087  	fmt.Fprintf(h, "buildmode %s goos %s goarch %s\n", cfg.BuildBuildmode, cfg.Goos, cfg.Goarch)
  1088  	fmt.Fprintf(h, "import %q\n", p.ImportPath)
  1089  	fmt.Fprintf(h, "omitdebug %v standard %v local %v prefix %q\n", p.Internal.OmitDebug, p.Standard, p.Internal.Local, p.Internal.LocalPrefix)
  1090  
  1091  	// Toolchain-dependent configuration, shared with b.linkSharedActionID.
  1092  	b.printLinkerConfig(h, p)
  1093  
  1094  	// Input files.
  1095  	for _, a1 := range a.Deps {
  1096  		p1 := a1.Package
  1097  		if p1 != nil {
  1098  			if a1.built != "" || a1.buildID != "" {
  1099  				buildID := a1.buildID
  1100  				if buildID == "" {
  1101  					buildID = b.buildID(a1.built)
  1102  				}
  1103  				fmt.Fprintf(h, "packagefile %s=%s\n", p1.ImportPath, contentID(buildID))
  1104  			}
  1105  			// Because we put package main's full action ID into the binary's build ID,
  1106  			// we must also put the full action ID into the binary's action ID hash.
  1107  			if p1.Name == "main" {
  1108  				fmt.Fprintf(h, "packagemain %s\n", a1.buildID)
  1109  			}
  1110  			if p1.Shlib != "" {
  1111  				fmt.Fprintf(h, "packageshlib %s=%s\n", p1.ImportPath, contentID(b.buildID(p1.Shlib)))
  1112  			}
  1113  		}
  1114  	}
  1115  
  1116  	return h.Sum()
  1117  }
  1118  
  1119  // printLinkerConfig prints the linker config into the hash h,
  1120  // as part of the computation of a linker-related action ID.
  1121  func (b *Builder) printLinkerConfig(h io.Writer, p *load.Package) {
  1122  	switch cfg.BuildToolchainName {
  1123  	default:
  1124  		base.Fatalf("linkActionID: unknown toolchain %q", cfg.BuildToolchainName)
  1125  
  1126  	case "gc":
  1127  		fmt.Fprintf(h, "link %s %q %s\n", b.toolID("link"), forcedLdflags, ldBuildmode)
  1128  		if p != nil {
  1129  			fmt.Fprintf(h, "linkflags %q\n", p.Internal.Ldflags)
  1130  		}
  1131  		fmt.Fprintf(h, "GO$GOARCH=%s\n", os.Getenv("GO"+strings.ToUpper(cfg.BuildContext.GOARCH))) // GO386, GOARM, etc
  1132  
  1133  		// The linker writes source file paths that say GOROOT_FINAL.
  1134  		fmt.Fprintf(h, "GOROOT=%s\n", cfg.GOROOT_FINAL)
  1135  
  1136  		// TODO(rsc): Convince linker team not to add more magic environment variables,
  1137  		// or perhaps restrict the environment variables passed to subprocesses.
  1138  		magic := []string{
  1139  			"GO_EXTLINK_ENABLED",
  1140  		}
  1141  		for _, env := range magic {
  1142  			if x := os.Getenv(env); x != "" {
  1143  				fmt.Fprintf(h, "magic %s=%s\n", env, x)
  1144  			}
  1145  		}
  1146  
  1147  		// TODO(rsc): Do cgo settings and flags need to be included?
  1148  		// Or external linker settings and flags?
  1149  
  1150  	case "gccgo":
  1151  		id, err := b.gccgoToolID(BuildToolchain.linker(), "go")
  1152  		if err != nil {
  1153  			base.Fatalf("%v", err)
  1154  		}
  1155  		fmt.Fprintf(h, "link %s %s\n", id, ldBuildmode)
  1156  		// TODO(iant): Should probably include cgo flags here.
  1157  	}
  1158  }
  1159  
  1160  // link is the action for linking a single command.
  1161  // Note that any new influence on this logic must be reported in b.linkActionID above as well.
  1162  func (b *Builder) link(a *Action) (err error) {
  1163  	if b.useCache(a, a.Package, b.linkActionID(a), a.Package.Target) || b.IsCmdList {
  1164  		return nil
  1165  	}
  1166  	defer b.flushOutput(a)
  1167  
  1168  	if err := b.Mkdir(a.Objdir); err != nil {
  1169  		return err
  1170  	}
  1171  
  1172  	importcfg := a.Objdir + "importcfg.link"
  1173  	if err := b.writeLinkImportcfg(a, importcfg); err != nil {
  1174  		return err
  1175  	}
  1176  
  1177  	// make target directory
  1178  	dir, _ := filepath.Split(a.Target)
  1179  	if dir != "" {
  1180  		if err := b.Mkdir(dir); err != nil {
  1181  			return err
  1182  		}
  1183  	}
  1184  
  1185  	if err := BuildToolchain.ld(b, a, a.Target, importcfg, a.Deps[0].built); err != nil {
  1186  		return err
  1187  	}
  1188  
  1189  	// Update the binary with the final build ID.
  1190  	// But if OmitDebug is set, don't rewrite the binary, because we set OmitDebug
  1191  	// on binaries that we are going to run and then delete.
  1192  	// There's no point in doing work on such a binary.
  1193  	// Worse, opening the binary for write here makes it
  1194  	// essentially impossible to safely fork+exec due to a fundamental
  1195  	// incompatibility between ETXTBSY and threads on modern Unix systems.
  1196  	// See golang.org/issue/22220.
  1197  	// We still call updateBuildID to update a.buildID, which is important
  1198  	// for test result caching, but passing rewrite=false (final arg)
  1199  	// means we don't actually rewrite the binary, nor store the
  1200  	// result into the cache. That's probably a net win:
  1201  	// less cache space wasted on large binaries we are not likely to
  1202  	// need again. (On the other hand it does make repeated go test slower.)
  1203  	// It also makes repeated go run slower, which is a win in itself:
  1204  	// we don't want people to treat go run like a scripting environment.
  1205  	if err := b.updateBuildID(a, a.Target, !a.Package.Internal.OmitDebug); err != nil {
  1206  		return err
  1207  	}
  1208  
  1209  	a.built = a.Target
  1210  	return nil
  1211  }
  1212  
  1213  func (b *Builder) writeLinkImportcfg(a *Action, file string) error {
  1214  	// Prepare Go import cfg.
  1215  	var icfg bytes.Buffer
  1216  	for _, a1 := range a.Deps {
  1217  		p1 := a1.Package
  1218  		if p1 == nil {
  1219  			continue
  1220  		}
  1221  		fmt.Fprintf(&icfg, "packagefile %s=%s\n", p1.ImportPath, a1.built)
  1222  		if p1.Shlib != "" {
  1223  			fmt.Fprintf(&icfg, "packageshlib %s=%s\n", p1.ImportPath, p1.Shlib)
  1224  		}
  1225  	}
  1226  	return b.writeFile(file, icfg.Bytes())
  1227  }
  1228  
  1229  // PkgconfigCmd returns a pkg-config binary name
  1230  // defaultPkgConfig is defined in zdefaultcc.go, written by cmd/dist.
  1231  func (b *Builder) PkgconfigCmd() string {
  1232  	return envList("PKG_CONFIG", cfg.DefaultPkgConfig)[0]
  1233  }
  1234  
  1235  // splitPkgConfigOutput parses the pkg-config output into a slice of
  1236  // flags. This implements the algorithm from pkgconf/libpkgconf/argvsplit.c.
  1237  func splitPkgConfigOutput(out []byte) ([]string, error) {
  1238  	if len(out) == 0 {
  1239  		return nil, nil
  1240  	}
  1241  	var flags []string
  1242  	flag := make([]byte, 0, len(out))
  1243  	escaped := false
  1244  	quote := byte(0)
  1245  
  1246  	for _, c := range out {
  1247  		if escaped {
  1248  			if quote != 0 {
  1249  				switch c {
  1250  				case '$', '`', '"', '\\':
  1251  				default:
  1252  					flag = append(flag, '\\')
  1253  				}
  1254  				flag = append(flag, c)
  1255  			} else {
  1256  				flag = append(flag, c)
  1257  			}
  1258  			escaped = false
  1259  		} else if quote != 0 {
  1260  			if c == quote {
  1261  				quote = 0
  1262  			} else {
  1263  				switch c {
  1264  				case '\\':
  1265  					escaped = true
  1266  				default:
  1267  					flag = append(flag, c)
  1268  				}
  1269  			}
  1270  		} else if strings.IndexByte(" \t\n\v\f\r", c) < 0 {
  1271  			switch c {
  1272  			case '\\':
  1273  				escaped = true
  1274  			case '\'', '"':
  1275  				quote = c
  1276  			default:
  1277  				flag = append(flag, c)
  1278  			}
  1279  		} else if len(flag) != 0 {
  1280  			flags = append(flags, string(flag))
  1281  			flag = flag[:0]
  1282  		}
  1283  	}
  1284  	if escaped {
  1285  		return nil, errors.New("broken character escaping in pkgconf output ")
  1286  	}
  1287  	if quote != 0 {
  1288  		return nil, errors.New("unterminated quoted string in pkgconf output ")
  1289  	} else if len(flag) != 0 {
  1290  		flags = append(flags, string(flag))
  1291  	}
  1292  
  1293  	return flags, nil
  1294  }
  1295  
  1296  // Calls pkg-config if needed and returns the cflags/ldflags needed to build the package.
  1297  func (b *Builder) getPkgConfigFlags(p *load.Package) (cflags, ldflags []string, err error) {
  1298  	if pcargs := p.CgoPkgConfig; len(pcargs) > 0 {
  1299  		// pkg-config permits arguments to appear anywhere in
  1300  		// the command line. Move them all to the front, before --.
  1301  		var pcflags []string
  1302  		var pkgs []string
  1303  		for _, pcarg := range pcargs {
  1304  			if pcarg == "--" {
  1305  				// We're going to add our own "--" argument.
  1306  			} else if strings.HasPrefix(pcarg, "--") {
  1307  				pcflags = append(pcflags, pcarg)
  1308  			} else {
  1309  				pkgs = append(pkgs, pcarg)
  1310  			}
  1311  		}
  1312  		for _, pkg := range pkgs {
  1313  			if !load.SafeArg(pkg) {
  1314  				return nil, nil, fmt.Errorf("invalid pkg-config package name: %s", pkg)
  1315  			}
  1316  		}
  1317  		var out []byte
  1318  		out, err = b.runOut(p.Dir, nil, b.PkgconfigCmd(), "--cflags", pcflags, "--", pkgs)
  1319  		if err != nil {
  1320  			b.showOutput(nil, p.Dir, b.PkgconfigCmd()+" --cflags "+strings.Join(pcflags, " ")+" -- "+strings.Join(pkgs, " "), string(out))
  1321  			b.Print(err.Error() + "\n")
  1322  			return nil, nil, errPrintedOutput
  1323  		}
  1324  		if len(out) > 0 {
  1325  			cflags, err = splitPkgConfigOutput(out)
  1326  			if err != nil {
  1327  				return nil, nil, err
  1328  			}
  1329  			if err := checkCompilerFlags("CFLAGS", "pkg-config --cflags", cflags); err != nil {
  1330  				return nil, nil, err
  1331  			}
  1332  		}
  1333  		out, err = b.runOut(p.Dir, nil, b.PkgconfigCmd(), "--libs", pcflags, "--", pkgs)
  1334  		if err != nil {
  1335  			b.showOutput(nil, p.Dir, b.PkgconfigCmd()+" --libs "+strings.Join(pcflags, " ")+" -- "+strings.Join(pkgs, " "), string(out))
  1336  			b.Print(err.Error() + "\n")
  1337  			return nil, nil, errPrintedOutput
  1338  		}
  1339  		if len(out) > 0 {
  1340  			ldflags = strings.Fields(string(out))
  1341  			if err := checkLinkerFlags("LDFLAGS", "pkg-config --libs", ldflags); err != nil {
  1342  				return nil, nil, err
  1343  			}
  1344  		}
  1345  	}
  1346  
  1347  	return
  1348  }
  1349  
  1350  func (b *Builder) installShlibname(a *Action) error {
  1351  	// TODO: BuildN
  1352  	a1 := a.Deps[0]
  1353  	err := ioutil.WriteFile(a.Target, []byte(filepath.Base(a1.Target)+"\n"), 0666)
  1354  	if err != nil {
  1355  		return err
  1356  	}
  1357  	if cfg.BuildX {
  1358  		b.Showcmd("", "echo '%s' > %s # internal", filepath.Base(a1.Target), a.Target)
  1359  	}
  1360  	return nil
  1361  }
  1362  
  1363  func (b *Builder) linkSharedActionID(a *Action) cache.ActionID {
  1364  	h := cache.NewHash("linkShared")
  1365  
  1366  	// Toolchain-independent configuration.
  1367  	fmt.Fprintf(h, "linkShared\n")
  1368  	fmt.Fprintf(h, "goos %s goarch %s\n", cfg.Goos, cfg.Goarch)
  1369  
  1370  	// Toolchain-dependent configuration, shared with b.linkActionID.
  1371  	b.printLinkerConfig(h, nil)
  1372  
  1373  	// Input files.
  1374  	for _, a1 := range a.Deps {
  1375  		p1 := a1.Package
  1376  		if a1.built == "" {
  1377  			continue
  1378  		}
  1379  		if p1 != nil {
  1380  			fmt.Fprintf(h, "packagefile %s=%s\n", p1.ImportPath, contentID(b.buildID(a1.built)))
  1381  			if p1.Shlib != "" {
  1382  				fmt.Fprintf(h, "packageshlib %s=%s\n", p1.ImportPath, contentID(b.buildID(p1.Shlib)))
  1383  			}
  1384  		}
  1385  	}
  1386  	// Files named on command line are special.
  1387  	for _, a1 := range a.Deps[0].Deps {
  1388  		p1 := a1.Package
  1389  		fmt.Fprintf(h, "top %s=%s\n", p1.ImportPath, contentID(b.buildID(a1.built)))
  1390  	}
  1391  
  1392  	return h.Sum()
  1393  }
  1394  
  1395  func (b *Builder) linkShared(a *Action) (err error) {
  1396  	if b.useCache(a, nil, b.linkSharedActionID(a), a.Target) || b.IsCmdList {
  1397  		return nil
  1398  	}
  1399  	defer b.flushOutput(a)
  1400  
  1401  	if err := b.Mkdir(a.Objdir); err != nil {
  1402  		return err
  1403  	}
  1404  
  1405  	importcfg := a.Objdir + "importcfg.link"
  1406  	if err := b.writeLinkImportcfg(a, importcfg); err != nil {
  1407  		return err
  1408  	}
  1409  
  1410  	// TODO(rsc): There is a missing updateBuildID here,
  1411  	// but we have to decide where to store the build ID in these files.
  1412  	a.built = a.Target
  1413  	return BuildToolchain.ldShared(b, a, a.Deps[0].Deps, a.Target, importcfg, a.Deps)
  1414  }
  1415  
  1416  // BuildInstallFunc is the action for installing a single package or executable.
  1417  func BuildInstallFunc(b *Builder, a *Action) (err error) {
  1418  	defer func() {
  1419  		if err != nil && err != errPrintedOutput {
  1420  			// a.Package == nil is possible for the go install -buildmode=shared
  1421  			// action that installs libmangledname.so, which corresponds to
  1422  			// a list of packages, not just one.
  1423  			sep, path := "", ""
  1424  			if a.Package != nil {
  1425  				sep, path = " ", a.Package.ImportPath
  1426  			}
  1427  			err = fmt.Errorf("go %s%s%s: %v", cfg.CmdName, sep, path, err)
  1428  		}
  1429  	}()
  1430  
  1431  	a1 := a.Deps[0]
  1432  	a.buildID = a1.buildID
  1433  
  1434  	// If we are using the eventual install target as an up-to-date
  1435  	// cached copy of the thing we built, then there's no need to
  1436  	// copy it into itself (and that would probably fail anyway).
  1437  	// In this case a1.built == a.Target because a1.built == p.Target,
  1438  	// so the built target is not in the a1.Objdir tree that b.cleanup(a1) removes.
  1439  	if a1.built == a.Target {
  1440  		a.built = a.Target
  1441  		if !a.buggyInstall {
  1442  			b.cleanup(a1)
  1443  		}
  1444  		// Whether we're smart enough to avoid a complete rebuild
  1445  		// depends on exactly what the staleness and rebuild algorithms
  1446  		// are, as well as potentially the state of the Go build cache.
  1447  		// We don't really want users to be able to infer (or worse start depending on)
  1448  		// those details from whether the modification time changes during
  1449  		// "go install", so do a best-effort update of the file times to make it
  1450  		// look like we rewrote a.Target even if we did not. Updating the mtime
  1451  		// may also help other mtime-based systems that depend on our
  1452  		// previous mtime updates that happened more often.
  1453  		// This is still not perfect - we ignore the error result, and if the file was
  1454  		// unwritable for some reason then pretending to have written it is also
  1455  		// confusing - but it's probably better than not doing the mtime update.
  1456  		//
  1457  		// But don't do that for the special case where building an executable
  1458  		// with -linkshared implicitly installs all its dependent libraries.
  1459  		// We want to hide that awful detail as much as possible, so don't
  1460  		// advertise it by touching the mtimes (usually the libraries are up
  1461  		// to date).
  1462  		if !a.buggyInstall && !b.IsCmdList {
  1463  			now := time.Now()
  1464  			os.Chtimes(a.Target, now, now)
  1465  		}
  1466  		return nil
  1467  	}
  1468  
  1469  	// If we're building for go list -export,
  1470  	// never install anything; just keep the cache reference.
  1471  	if b.IsCmdList {
  1472  		a.built = a1.built
  1473  		return nil
  1474  	}
  1475  
  1476  	if err := b.Mkdir(a.Objdir); err != nil {
  1477  		return err
  1478  	}
  1479  
  1480  	perm := os.FileMode(0666)
  1481  	if a1.Mode == "link" {
  1482  		switch cfg.BuildBuildmode {
  1483  		case "c-archive", "c-shared", "plugin":
  1484  		default:
  1485  			perm = 0777
  1486  		}
  1487  	}
  1488  
  1489  	// make target directory
  1490  	dir, _ := filepath.Split(a.Target)
  1491  	if dir != "" {
  1492  		if err := b.Mkdir(dir); err != nil {
  1493  			return err
  1494  		}
  1495  	}
  1496  
  1497  	if !a.buggyInstall {
  1498  		defer b.cleanup(a1)
  1499  	}
  1500  
  1501  	return b.moveOrCopyFile(a.Target, a1.built, perm, false)
  1502  }
  1503  
  1504  // cleanup removes a's object dir to keep the amount of
  1505  // on-disk garbage down in a large build. On an operating system
  1506  // with aggressive buffering, cleaning incrementally like
  1507  // this keeps the intermediate objects from hitting the disk.
  1508  func (b *Builder) cleanup(a *Action) {
  1509  	if !cfg.BuildWork {
  1510  		if cfg.BuildX {
  1511  			// Don't say we are removing the directory if
  1512  			// we never created it.
  1513  			if _, err := os.Stat(a.Objdir); err == nil || cfg.BuildN {
  1514  				b.Showcmd("", "rm -r %s", a.Objdir)
  1515  			}
  1516  		}
  1517  		os.RemoveAll(a.Objdir)
  1518  	}
  1519  }
  1520  
  1521  // moveOrCopyFile is like 'mv src dst' or 'cp src dst'.
  1522  func (b *Builder) moveOrCopyFile(dst, src string, perm os.FileMode, force bool) error {
  1523  	if cfg.BuildN {
  1524  		b.Showcmd("", "mv %s %s", src, dst)
  1525  		return nil
  1526  	}
  1527  
  1528  	// If we can update the mode and rename to the dst, do it.
  1529  	// Otherwise fall back to standard copy.
  1530  
  1531  	// If the source is in the build cache, we need to copy it.
  1532  	if strings.HasPrefix(src, cache.DefaultDir()) {
  1533  		return b.copyFile(dst, src, perm, force)
  1534  	}
  1535  
  1536  	// On Windows, always copy the file, so that we respect the NTFS
  1537  	// permissions of the parent folder. https://golang.org/issue/22343.
  1538  	// What matters here is not cfg.Goos (the system we are building
  1539  	// for) but runtime.GOOS (the system we are building on).
  1540  	if runtime.GOOS == "windows" {
  1541  		return b.copyFile(dst, src, perm, force)
  1542  	}
  1543  
  1544  	// If the destination directory has the group sticky bit set,
  1545  	// we have to copy the file to retain the correct permissions.
  1546  	// https://golang.org/issue/18878
  1547  	if fi, err := os.Stat(filepath.Dir(dst)); err == nil {
  1548  		if fi.IsDir() && (fi.Mode()&os.ModeSetgid) != 0 {
  1549  			return b.copyFile(dst, src, perm, force)
  1550  		}
  1551  	}
  1552  
  1553  	// The perm argument is meant to be adjusted according to umask,
  1554  	// but we don't know what the umask is.
  1555  	// Create a dummy file to find out.
  1556  	// This avoids build tags and works even on systems like Plan 9
  1557  	// where the file mask computation incorporates other information.
  1558  	mode := perm
  1559  	f, err := os.OpenFile(filepath.Clean(dst)+"-go-tmp-umask", os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
  1560  	if err == nil {
  1561  		fi, err := f.Stat()
  1562  		if err == nil {
  1563  			mode = fi.Mode() & 0777
  1564  		}
  1565  		name := f.Name()
  1566  		f.Close()
  1567  		os.Remove(name)
  1568  	}
  1569  
  1570  	if err := os.Chmod(src, mode); err == nil {
  1571  		if err := os.Rename(src, dst); err == nil {
  1572  			if cfg.BuildX {
  1573  				b.Showcmd("", "mv %s %s", src, dst)
  1574  			}
  1575  			return nil
  1576  		}
  1577  	}
  1578  
  1579  	return b.copyFile(dst, src, perm, force)
  1580  }
  1581  
  1582  // copyFile is like 'cp src dst'.
  1583  func (b *Builder) copyFile(dst, src string, perm os.FileMode, force bool) error {
  1584  	if cfg.BuildN || cfg.BuildX {
  1585  		b.Showcmd("", "cp %s %s", src, dst)
  1586  		if cfg.BuildN {
  1587  			return nil
  1588  		}
  1589  	}
  1590  
  1591  	sf, err := os.Open(src)
  1592  	if err != nil {
  1593  		return err
  1594  	}
  1595  	defer sf.Close()
  1596  
  1597  	// Be careful about removing/overwriting dst.
  1598  	// Do not remove/overwrite if dst exists and is a directory
  1599  	// or a non-object file.
  1600  	if fi, err := os.Stat(dst); err == nil {
  1601  		if fi.IsDir() {
  1602  			return fmt.Errorf("build output %q already exists and is a directory", dst)
  1603  		}
  1604  		if !force && fi.Mode().IsRegular() && !isObject(dst) {
  1605  			return fmt.Errorf("build output %q already exists and is not an object file", dst)
  1606  		}
  1607  	}
  1608  
  1609  	// On Windows, remove lingering ~ file from last attempt.
  1610  	if base.ToolIsWindows {
  1611  		if _, err := os.Stat(dst + "~"); err == nil {
  1612  			os.Remove(dst + "~")
  1613  		}
  1614  	}
  1615  
  1616  	mayberemovefile(dst)
  1617  	df, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
  1618  	if err != nil && base.ToolIsWindows {
  1619  		// Windows does not allow deletion of a binary file
  1620  		// while it is executing. Try to move it out of the way.
  1621  		// If the move fails, which is likely, we'll try again the
  1622  		// next time we do an install of this binary.
  1623  		if err := os.Rename(dst, dst+"~"); err == nil {
  1624  			os.Remove(dst + "~")
  1625  		}
  1626  		df, err = os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
  1627  	}
  1628  	if err != nil {
  1629  		return err
  1630  	}
  1631  
  1632  	_, err = io.Copy(df, sf)
  1633  	df.Close()
  1634  	if err != nil {
  1635  		mayberemovefile(dst)
  1636  		return fmt.Errorf("copying %s to %s: %v", src, dst, err)
  1637  	}
  1638  	return nil
  1639  }
  1640  
  1641  // writeFile writes the text to file.
  1642  func (b *Builder) writeFile(file string, text []byte) error {
  1643  	if cfg.BuildN || cfg.BuildX {
  1644  		b.Showcmd("", "cat >%s << 'EOF' # internal\n%sEOF", file, text)
  1645  	}
  1646  	if cfg.BuildN {
  1647  		return nil
  1648  	}
  1649  	return ioutil.WriteFile(file, text, 0666)
  1650  }
  1651  
  1652  // appendFile appends the text to file.
  1653  func (b *Builder) appendFile(file string, text []byte) error {
  1654  	if cfg.BuildN || cfg.BuildX {
  1655  		b.Showcmd("", "cat >>%s << 'EOF' # internal\n%sEOF", file, text)
  1656  	}
  1657  	if cfg.BuildN {
  1658  		return nil
  1659  	}
  1660  	f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
  1661  	if err != nil {
  1662  		return err
  1663  	}
  1664  	defer f.Close()
  1665  	if _, err = f.Write(text); err != nil {
  1666  		return err
  1667  	}
  1668  	return f.Close()
  1669  }
  1670  
  1671  // Install the cgo export header file, if there is one.
  1672  func (b *Builder) installHeader(a *Action) error {
  1673  	src := a.Objdir + "_cgo_install.h"
  1674  	if _, err := os.Stat(src); os.IsNotExist(err) {
  1675  		// If the file does not exist, there are no exported
  1676  		// functions, and we do not install anything.
  1677  		// TODO(rsc): Once we know that caching is rebuilding
  1678  		// at the right times (not missing rebuilds), here we should
  1679  		// probably delete the installed header, if any.
  1680  		if cfg.BuildX {
  1681  			b.Showcmd("", "# %s not created", src)
  1682  		}
  1683  		return nil
  1684  	}
  1685  
  1686  	dir, _ := filepath.Split(a.Target)
  1687  	if dir != "" {
  1688  		if err := b.Mkdir(dir); err != nil {
  1689  			return err
  1690  		}
  1691  	}
  1692  
  1693  	return b.moveOrCopyFile(a.Target, src, 0666, true)
  1694  }
  1695  
  1696  // cover runs, in effect,
  1697  //	go tool cover -mode=b.coverMode -var="varName" -o dst.go src.go
  1698  func (b *Builder) cover(a *Action, dst, src string, varName string) error {
  1699  	return b.run(a, a.Objdir, "cover "+a.Package.ImportPath, nil,
  1700  		cfg.BuildToolexec,
  1701  		base.Tool("cover"),
  1702  		"-mode", a.Package.Internal.CoverMode,
  1703  		"-var", varName,
  1704  		"-o", dst,
  1705  		src)
  1706  }
  1707  
  1708  var objectMagic = [][]byte{
  1709  	{'!', '<', 'a', 'r', 'c', 'h', '>', '\n'}, // Package archive
  1710  	{'<', 'b', 'i', 'g', 'a', 'f', '>', '\n'}, // Package AIX big archive
  1711  	{'\x7F', 'E', 'L', 'F'},                   // ELF
  1712  	{0xFE, 0xED, 0xFA, 0xCE},                  // Mach-O big-endian 32-bit
  1713  	{0xFE, 0xED, 0xFA, 0xCF},                  // Mach-O big-endian 64-bit
  1714  	{0xCE, 0xFA, 0xED, 0xFE},                  // Mach-O little-endian 32-bit
  1715  	{0xCF, 0xFA, 0xED, 0xFE},                  // Mach-O little-endian 64-bit
  1716  	{0x4d, 0x5a, 0x90, 0x00, 0x03, 0x00},      // PE (Windows) as generated by 6l/8l and gcc
  1717  	{0x00, 0x00, 0x01, 0xEB},                  // Plan 9 i386
  1718  	{0x00, 0x00, 0x8a, 0x97},                  // Plan 9 amd64
  1719  	{0x00, 0x00, 0x06, 0x47},                  // Plan 9 arm
  1720  	{0x00, 0x61, 0x73, 0x6D},                  // WASM
  1721  	{0x01, 0xDF},                              // XCOFF 32bit
  1722  	{0x01, 0xF7},                              // XCOFF 64bit
  1723  }
  1724  
  1725  func isObject(s string) bool {
  1726  	f, err := os.Open(s)
  1727  	if err != nil {
  1728  		return false
  1729  	}
  1730  	defer f.Close()
  1731  	buf := make([]byte, 64)
  1732  	io.ReadFull(f, buf)
  1733  	for _, magic := range objectMagic {
  1734  		if bytes.HasPrefix(buf, magic) {
  1735  			return true
  1736  		}
  1737  	}
  1738  	return false
  1739  }
  1740  
  1741  // mayberemovefile removes a file only if it is a regular file
  1742  // When running as a user with sufficient privileges, we may delete
  1743  // even device files, for example, which is not intended.
  1744  func mayberemovefile(s string) {
  1745  	if fi, err := os.Lstat(s); err == nil && !fi.Mode().IsRegular() {
  1746  		return
  1747  	}
  1748  	os.Remove(s)
  1749  }
  1750  
  1751  // fmtcmd formats a command in the manner of fmt.Sprintf but also:
  1752  //
  1753  //	If dir is non-empty and the script is not in dir right now,
  1754  //	fmtcmd inserts "cd dir\n" before the command.
  1755  //
  1756  //	fmtcmd replaces the value of b.WorkDir with $WORK.
  1757  //	fmtcmd replaces the value of goroot with $GOROOT.
  1758  //	fmtcmd replaces the value of b.gobin with $GOBIN.
  1759  //
  1760  //	fmtcmd replaces the name of the current directory with dot (.)
  1761  //	but only when it is at the beginning of a space-separated token.
  1762  //
  1763  func (b *Builder) fmtcmd(dir string, format string, args ...interface{}) string {
  1764  	cmd := fmt.Sprintf(format, args...)
  1765  	if dir != "" && dir != "/" {
  1766  		dot := " ."
  1767  		if dir[len(dir)-1] == filepath.Separator {
  1768  			dot += string(filepath.Separator)
  1769  		}
  1770  		cmd = strings.ReplaceAll(" "+cmd, " "+dir, dot)[1:]
  1771  		if b.scriptDir != dir {
  1772  			b.scriptDir = dir
  1773  			cmd = "cd " + dir + "\n" + cmd
  1774  		}
  1775  	}
  1776  	if b.WorkDir != "" {
  1777  		cmd = strings.ReplaceAll(cmd, b.WorkDir, "$WORK")
  1778  	}
  1779  	return cmd
  1780  }
  1781  
  1782  // showcmd prints the given command to standard output
  1783  // for the implementation of -n or -x.
  1784  func (b *Builder) Showcmd(dir string, format string, args ...interface{}) {
  1785  	b.output.Lock()
  1786  	defer b.output.Unlock()
  1787  	b.Print(b.fmtcmd(dir, format, args...) + "\n")
  1788  }
  1789  
  1790  // showOutput prints "# desc" followed by the given output.
  1791  // The output is expected to contain references to 'dir', usually
  1792  // the source directory for the package that has failed to build.
  1793  // showOutput rewrites mentions of dir with a relative path to dir
  1794  // when the relative path is shorter. This is usually more pleasant.
  1795  // For example, if fmt doesn't compile and we are in src/html,
  1796  // the output is
  1797  //
  1798  //	$ go build
  1799  //	# fmt
  1800  //	../fmt/print.go:1090: undefined: asdf
  1801  //	$
  1802  //
  1803  // instead of
  1804  //
  1805  //	$ go build
  1806  //	# fmt
  1807  //	/usr/gopher/go/src/fmt/print.go:1090: undefined: asdf
  1808  //	$
  1809  //
  1810  // showOutput also replaces references to the work directory with $WORK.
  1811  //
  1812  // If a is not nil and a.output is not nil, showOutput appends to that slice instead of
  1813  // printing to b.Print.
  1814  //
  1815  func (b *Builder) showOutput(a *Action, dir, desc, out string) {
  1816  	prefix := "# " + desc
  1817  	suffix := "\n" + out
  1818  	if reldir := base.ShortPath(dir); reldir != dir {
  1819  		suffix = strings.ReplaceAll(suffix, " "+dir, " "+reldir)
  1820  		suffix = strings.ReplaceAll(suffix, "\n"+dir, "\n"+reldir)
  1821  	}
  1822  	suffix = strings.ReplaceAll(suffix, " "+b.WorkDir, " $WORK")
  1823  
  1824  	if a != nil && a.output != nil {
  1825  		a.output = append(a.output, prefix...)
  1826  		a.output = append(a.output, suffix...)
  1827  		return
  1828  	}
  1829  
  1830  	b.output.Lock()
  1831  	defer b.output.Unlock()
  1832  	b.Print(prefix, suffix)
  1833  }
  1834  
  1835  // errPrintedOutput is a special error indicating that a command failed
  1836  // but that it generated output as well, and that output has already
  1837  // been printed, so there's no point showing 'exit status 1' or whatever
  1838  // the wait status was. The main executor, builder.do, knows not to
  1839  // print this error.
  1840  var errPrintedOutput = errors.New("already printed output - no need to show error")
  1841  
  1842  var cgoLine = regexp.MustCompile(`\[[^\[\]]+\.(cgo1|cover)\.go:[0-9]+(:[0-9]+)?\]`)
  1843  var cgoTypeSigRe = regexp.MustCompile(`\b_C2?(type|func|var|macro)_\B`)
  1844  
  1845  // run runs the command given by cmdline in the directory dir.
  1846  // If the command fails, run prints information about the failure
  1847  // and returns a non-nil error.
  1848  func (b *Builder) run(a *Action, dir string, desc string, env []string, cmdargs ...interface{}) error {
  1849  	out, err := b.runOut(dir, env, cmdargs...)
  1850  	if len(out) > 0 {
  1851  		if desc == "" {
  1852  			desc = b.fmtcmd(dir, "%s", strings.Join(str.StringList(cmdargs...), " "))
  1853  		}
  1854  		b.showOutput(a, dir, desc, b.processOutput(out))
  1855  		if err != nil {
  1856  			err = errPrintedOutput
  1857  		}
  1858  	}
  1859  	return err
  1860  }
  1861  
  1862  // processOutput prepares the output of runOut to be output to the console.
  1863  func (b *Builder) processOutput(out []byte) string {
  1864  	if out[len(out)-1] != '\n' {
  1865  		out = append(out, '\n')
  1866  	}
  1867  	messages := string(out)
  1868  	// Fix up output referring to cgo-generated code to be more readable.
  1869  	// Replace x.go:19[/tmp/.../x.cgo1.go:18] with x.go:19.
  1870  	// Replace *[100]_Ctype_foo with *[100]C.foo.
  1871  	// If we're using -x, assume we're debugging and want the full dump, so disable the rewrite.
  1872  	if !cfg.BuildX && cgoLine.MatchString(messages) {
  1873  		messages = cgoLine.ReplaceAllString(messages, "")
  1874  		messages = cgoTypeSigRe.ReplaceAllString(messages, "C.")
  1875  	}
  1876  	return messages
  1877  }
  1878  
  1879  // runOut runs the command given by cmdline in the directory dir.
  1880  // It returns the command output and any errors that occurred.
  1881  func (b *Builder) runOut(dir string, env []string, cmdargs ...interface{}) ([]byte, error) {
  1882  	cmdline := str.StringList(cmdargs...)
  1883  
  1884  	for _, arg := range cmdline {
  1885  		// GNU binutils commands, including gcc and gccgo, interpret an argument
  1886  		// @foo anywhere in the command line (even following --) as meaning
  1887  		// "read and insert arguments from the file named foo."
  1888  		// Don't say anything that might be misinterpreted that way.
  1889  		if strings.HasPrefix(arg, "@") {
  1890  			return nil, fmt.Errorf("invalid command-line argument %s in command: %s", arg, joinUnambiguously(cmdline))
  1891  		}
  1892  	}
  1893  
  1894  	if cfg.BuildN || cfg.BuildX {
  1895  		var envcmdline string
  1896  		for _, e := range env {
  1897  			if j := strings.IndexByte(e, '='); j != -1 {
  1898  				if strings.ContainsRune(e[j+1:], '\'') {
  1899  					envcmdline += fmt.Sprintf("%s=%q", e[:j], e[j+1:])
  1900  				} else {
  1901  					envcmdline += fmt.Sprintf("%s='%s'", e[:j], e[j+1:])
  1902  				}
  1903  				envcmdline += " "
  1904  			}
  1905  		}
  1906  		envcmdline += joinUnambiguously(cmdline)
  1907  		b.Showcmd(dir, "%s", envcmdline)
  1908  		if cfg.BuildN {
  1909  			return nil, nil
  1910  		}
  1911  	}
  1912  
  1913  	var buf bytes.Buffer
  1914  	cmd := exec.Command(cmdline[0], cmdline[1:]...)
  1915  	cmd.Stdout = &buf
  1916  	cmd.Stderr = &buf
  1917  	cleanup := passLongArgsInResponseFiles(cmd)
  1918  	defer cleanup()
  1919  	cmd.Dir = dir
  1920  	cmd.Env = base.MergeEnvLists(env, base.EnvForDir(cmd.Dir, os.Environ()))
  1921  	err := cmd.Run()
  1922  
  1923  	// err can be something like 'exit status 1'.
  1924  	// Add information about what program was running.
  1925  	// Note that if buf.Bytes() is non-empty, the caller usually
  1926  	// shows buf.Bytes() and does not print err at all, so the
  1927  	// prefix here does not make most output any more verbose.
  1928  	if err != nil {
  1929  		err = errors.New(cmdline[0] + ": " + err.Error())
  1930  	}
  1931  	return buf.Bytes(), err
  1932  }
  1933  
  1934  // joinUnambiguously prints the slice, quoting where necessary to make the
  1935  // output unambiguous.
  1936  // TODO: See issue 5279. The printing of commands needs a complete redo.
  1937  func joinUnambiguously(a []string) string {
  1938  	var buf bytes.Buffer
  1939  	for i, s := range a {
  1940  		if i > 0 {
  1941  			buf.WriteByte(' ')
  1942  		}
  1943  		q := strconv.Quote(s)
  1944  		// A gccgo command line can contain -( and -).
  1945  		// Make sure we quote them since they are special to the shell.
  1946  		if s == "" || strings.ContainsAny(s, " ()") || len(q) > len(s)+2 {
  1947  			buf.WriteString(q)
  1948  		} else {
  1949  			buf.WriteString(s)
  1950  		}
  1951  	}
  1952  	return buf.String()
  1953  }
  1954  
  1955  // cCompilerEnv returns environment variables to set when running the
  1956  // C compiler. This is needed to disable escape codes in clang error
  1957  // messages that confuse tools like cgo.
  1958  func (b *Builder) cCompilerEnv() []string {
  1959  	return []string{"TERM=dumb"}
  1960  }
  1961  
  1962  // mkdir makes the named directory.
  1963  func (b *Builder) Mkdir(dir string) error {
  1964  	// Make Mkdir(a.Objdir) a no-op instead of an error when a.Objdir == "".
  1965  	if dir == "" {
  1966  		return nil
  1967  	}
  1968  
  1969  	b.exec.Lock()
  1970  	defer b.exec.Unlock()
  1971  	// We can be a little aggressive about being
  1972  	// sure directories exist. Skip repeated calls.
  1973  	if b.mkdirCache[dir] {
  1974  		return nil
  1975  	}
  1976  	b.mkdirCache[dir] = true
  1977  
  1978  	if cfg.BuildN || cfg.BuildX {
  1979  		b.Showcmd("", "mkdir -p %s", dir)
  1980  		if cfg.BuildN {
  1981  			return nil
  1982  		}
  1983  	}
  1984  
  1985  	if err := os.MkdirAll(dir, 0777); err != nil {
  1986  		return err
  1987  	}
  1988  	return nil
  1989  }
  1990  
  1991  // symlink creates a symlink newname -> oldname.
  1992  func (b *Builder) Symlink(oldname, newname string) error {
  1993  	// It's not an error to try to recreate an existing symlink.
  1994  	if link, err := os.Readlink(newname); err == nil && link == oldname {
  1995  		return nil
  1996  	}
  1997  
  1998  	if cfg.BuildN || cfg.BuildX {
  1999  		b.Showcmd("", "ln -s %s %s", oldname, newname)
  2000  		if cfg.BuildN {
  2001  			return nil
  2002  		}
  2003  	}
  2004  	return os.Symlink(oldname, newname)
  2005  }
  2006  
  2007  // mkAbs returns an absolute path corresponding to
  2008  // evaluating f in the directory dir.
  2009  // We always pass absolute paths of source files so that
  2010  // the error messages will include the full path to a file
  2011  // in need of attention.
  2012  func mkAbs(dir, f string) string {
  2013  	// Leave absolute paths alone.
  2014  	// Also, during -n mode we use the pseudo-directory $WORK
  2015  	// instead of creating an actual work directory that won't be used.
  2016  	// Leave paths beginning with $WORK alone too.
  2017  	if filepath.IsAbs(f) || strings.HasPrefix(f, "$WORK") {
  2018  		return f
  2019  	}
  2020  	return filepath.Join(dir, f)
  2021  }
  2022  
  2023  type toolchain interface {
  2024  	// gc runs the compiler in a specific directory on a set of files
  2025  	// and returns the name of the generated output file.
  2026  	//
  2027  	// TODO: This argument list is long. Consider putting it in a struct.
  2028  	gc(b *Builder, a *Action, archive string, importcfg []byte, symabis string, asmhdr bool, gofiles []string) (ofile string, out []byte, err error)
  2029  	// cc runs the toolchain's C compiler in a directory on a C file
  2030  	// to produce an output file.
  2031  	cc(b *Builder, a *Action, ofile, cfile string) error
  2032  	// asm runs the assembler in a specific directory on specific files
  2033  	// and returns a list of named output files.
  2034  	asm(b *Builder, a *Action, sfiles []string) ([]string, error)
  2035  	// symabis scans the symbol ABIs from sfiles and returns the
  2036  	// path to the output symbol ABIs file, or "" if none.
  2037  	symabis(b *Builder, a *Action, sfiles []string) (string, error)
  2038  	// pack runs the archive packer in a specific directory to create
  2039  	// an archive from a set of object files.
  2040  	// typically it is run in the object directory.
  2041  	pack(b *Builder, a *Action, afile string, ofiles []string) error
  2042  	// ld runs the linker to create an executable starting at mainpkg.
  2043  	ld(b *Builder, root *Action, out, importcfg, mainpkg string) error
  2044  	// ldShared runs the linker to create a shared library containing the pkgs built by toplevelactions
  2045  	ldShared(b *Builder, root *Action, toplevelactions []*Action, out, importcfg string, allactions []*Action) error
  2046  
  2047  	compiler() string
  2048  	linker() string
  2049  }
  2050  
  2051  type noToolchain struct{}
  2052  
  2053  func noCompiler() error {
  2054  	log.Fatalf("unknown compiler %q", cfg.BuildContext.Compiler)
  2055  	return nil
  2056  }
  2057  
  2058  func (noToolchain) compiler() string {
  2059  	noCompiler()
  2060  	return ""
  2061  }
  2062  
  2063  func (noToolchain) linker() string {
  2064  	noCompiler()
  2065  	return ""
  2066  }
  2067  
  2068  func (noToolchain) gc(b *Builder, a *Action, archive string, importcfg []byte, symabis string, asmhdr bool, gofiles []string) (ofile string, out []byte, err error) {
  2069  	return "", nil, noCompiler()
  2070  }
  2071  
  2072  func (noToolchain) asm(b *Builder, a *Action, sfiles []string) ([]string, error) {
  2073  	return nil, noCompiler()
  2074  }
  2075  
  2076  func (noToolchain) symabis(b *Builder, a *Action, sfiles []string) (string, error) {
  2077  	return "", noCompiler()
  2078  }
  2079  
  2080  func (noToolchain) pack(b *Builder, a *Action, afile string, ofiles []string) error {
  2081  	return noCompiler()
  2082  }
  2083  
  2084  func (noToolchain) ld(b *Builder, root *Action, out, importcfg, mainpkg string) error {
  2085  	return noCompiler()
  2086  }
  2087  
  2088  func (noToolchain) ldShared(b *Builder, root *Action, toplevelactions []*Action, out, importcfg string, allactions []*Action) error {
  2089  	return noCompiler()
  2090  }
  2091  
  2092  func (noToolchain) cc(b *Builder, a *Action, ofile, cfile string) error {
  2093  	return noCompiler()
  2094  }
  2095  
  2096  // gcc runs the gcc C compiler to create an object from a single C file.
  2097  func (b *Builder) gcc(a *Action, p *load.Package, workdir, out string, flags []string, cfile string) error {
  2098  	return b.ccompile(a, p, out, flags, cfile, b.GccCmd(p.Dir, workdir))
  2099  }
  2100  
  2101  // gxx runs the g++ C++ compiler to create an object from a single C++ file.
  2102  func (b *Builder) gxx(a *Action, p *load.Package, workdir, out string, flags []string, cxxfile string) error {
  2103  	return b.ccompile(a, p, out, flags, cxxfile, b.GxxCmd(p.Dir, workdir))
  2104  }
  2105  
  2106  // gfortran runs the gfortran Fortran compiler to create an object from a single Fortran file.
  2107  func (b *Builder) gfortran(a *Action, p *load.Package, workdir, out string, flags []string, ffile string) error {
  2108  	return b.ccompile(a, p, out, flags, ffile, b.gfortranCmd(p.Dir, workdir))
  2109  }
  2110  
  2111  // ccompile runs the given C or C++ compiler and creates an object from a single source file.
  2112  func (b *Builder) ccompile(a *Action, p *load.Package, outfile string, flags []string, file string, compiler []string) error {
  2113  	file = mkAbs(p.Dir, file)
  2114  	desc := p.ImportPath
  2115  	if !filepath.IsAbs(outfile) {
  2116  		outfile = filepath.Join(p.Dir, outfile)
  2117  	}
  2118  	output, err := b.runOut(filepath.Dir(file), b.cCompilerEnv(), compiler, flags, "-o", outfile, "-c", filepath.Base(file))
  2119  	if len(output) > 0 {
  2120  		// On FreeBSD 11, when we pass -g to clang 3.8 it
  2121  		// invokes its internal assembler with -dwarf-version=2.
  2122  		// When it sees .section .note.GNU-stack, it warns
  2123  		// "DWARF2 only supports one section per compilation unit".
  2124  		// This warning makes no sense, since the section is empty,
  2125  		// but it confuses people.
  2126  		// We work around the problem by detecting the warning
  2127  		// and dropping -g and trying again.
  2128  		if bytes.Contains(output, []byte("DWARF2 only supports one section per compilation unit")) {
  2129  			newFlags := make([]string, 0, len(flags))
  2130  			for _, f := range flags {
  2131  				if !strings.HasPrefix(f, "-g") {
  2132  					newFlags = append(newFlags, f)
  2133  				}
  2134  			}
  2135  			if len(newFlags) < len(flags) {
  2136  				return b.ccompile(a, p, outfile, newFlags, file, compiler)
  2137  			}
  2138  		}
  2139  
  2140  		b.showOutput(a, p.Dir, desc, b.processOutput(output))
  2141  		if err != nil {
  2142  			err = errPrintedOutput
  2143  		} else if os.Getenv("GO_BUILDER_NAME") != "" {
  2144  			return errors.New("C compiler warning promoted to error on Go builders")
  2145  		}
  2146  	}
  2147  	return err
  2148  }
  2149  
  2150  // gccld runs the gcc linker to create an executable from a set of object files.
  2151  func (b *Builder) gccld(p *load.Package, objdir, outfile string, flags []string, objs []string) error {
  2152  	var cmd []string
  2153  	if len(p.CXXFiles) > 0 || len(p.SwigCXXFiles) > 0 {
  2154  		cmd = b.GxxCmd(p.Dir, objdir)
  2155  	} else {
  2156  		cmd = b.GccCmd(p.Dir, objdir)
  2157  	}
  2158  
  2159  	cmdargs := []interface{}{cmd, "-o", outfile, objs, flags}
  2160  	dir := p.Dir
  2161  	out, err := b.runOut(dir, b.cCompilerEnv(), cmdargs...)
  2162  	if len(out) > 0 {
  2163  		// Filter out useless linker warnings caused by bugs outside Go.
  2164  		// See also cmd/link/internal/ld's hostlink method.
  2165  		var save [][]byte
  2166  		for _, line := range bytes.SplitAfter(out, []byte("\n")) {
  2167  			// golang.org/issue/26073 - Apple Xcode bug
  2168  			if bytes.Contains(line, []byte("ld: warning: text-based stub file")) {
  2169  				continue
  2170  			}
  2171  			save = append(save, line)
  2172  		}
  2173  		out = bytes.Join(save, nil)
  2174  		if len(out) > 0 {
  2175  			b.showOutput(nil, dir, p.ImportPath, b.processOutput(out))
  2176  			if err != nil {
  2177  				err = errPrintedOutput
  2178  			}
  2179  		}
  2180  	}
  2181  	return err
  2182  }
  2183  
  2184  // Grab these before main helpfully overwrites them.
  2185  var (
  2186  	origCC  = os.Getenv("CC")
  2187  	origCXX = os.Getenv("CXX")
  2188  )
  2189  
  2190  // gccCmd returns a gcc command line prefix
  2191  // defaultCC is defined in zdefaultcc.go, written by cmd/dist.
  2192  func (b *Builder) GccCmd(incdir, workdir string) []string {
  2193  	return b.compilerCmd(b.ccExe(), incdir, workdir)
  2194  }
  2195  
  2196  // gxxCmd returns a g++ command line prefix
  2197  // defaultCXX is defined in zdefaultcc.go, written by cmd/dist.
  2198  func (b *Builder) GxxCmd(incdir, workdir string) []string {
  2199  	return b.compilerCmd(b.cxxExe(), incdir, workdir)
  2200  }
  2201  
  2202  // gfortranCmd returns a gfortran command line prefix.
  2203  func (b *Builder) gfortranCmd(incdir, workdir string) []string {
  2204  	return b.compilerCmd(b.fcExe(), incdir, workdir)
  2205  }
  2206  
  2207  // ccExe returns the CC compiler setting without all the extra flags we add implicitly.
  2208  func (b *Builder) ccExe() []string {
  2209  	return b.compilerExe(origCC, cfg.DefaultCC(cfg.Goos, cfg.Goarch))
  2210  }
  2211  
  2212  // cxxExe returns the CXX compiler setting without all the extra flags we add implicitly.
  2213  func (b *Builder) cxxExe() []string {
  2214  	return b.compilerExe(origCXX, cfg.DefaultCXX(cfg.Goos, cfg.Goarch))
  2215  }
  2216  
  2217  // fcExe returns the FC compiler setting without all the extra flags we add implicitly.
  2218  func (b *Builder) fcExe() []string {
  2219  	return b.compilerExe(os.Getenv("FC"), "gfortran")
  2220  }
  2221  
  2222  // compilerExe returns the compiler to use given an
  2223  // environment variable setting (the value not the name)
  2224  // and a default. The resulting slice is usually just the name
  2225  // of the compiler but can have additional arguments if they
  2226  // were present in the environment value.
  2227  // For example if CC="gcc -DGOPHER" then the result is ["gcc", "-DGOPHER"].
  2228  func (b *Builder) compilerExe(envValue string, def string) []string {
  2229  	compiler := strings.Fields(envValue)
  2230  	if len(compiler) == 0 {
  2231  		compiler = []string{def}
  2232  	}
  2233  	return compiler
  2234  }
  2235  
  2236  // compilerCmd returns a command line prefix for the given environment
  2237  // variable and using the default command when the variable is empty.
  2238  func (b *Builder) compilerCmd(compiler []string, incdir, workdir string) []string {
  2239  	// NOTE: env.go's mkEnv knows that the first three
  2240  	// strings returned are "gcc", "-I", incdir (and cuts them off).
  2241  	a := []string{compiler[0], "-I", incdir}
  2242  	a = append(a, compiler[1:]...)
  2243  
  2244  	// Definitely want -fPIC but on Windows gcc complains
  2245  	// "-fPIC ignored for target (all code is position independent)"
  2246  	if cfg.Goos != "windows" {
  2247  		a = append(a, "-fPIC")
  2248  	}
  2249  	a = append(a, b.gccArchArgs()...)
  2250  	// gcc-4.5 and beyond require explicit "-pthread" flag
  2251  	// for multithreading with pthread library.
  2252  	if cfg.BuildContext.CgoEnabled {
  2253  		switch cfg.Goos {
  2254  		case "windows":
  2255  			a = append(a, "-mthreads")
  2256  		default:
  2257  			a = append(a, "-pthread")
  2258  		}
  2259  	}
  2260  
  2261  	// disable ASCII art in clang errors, if possible
  2262  	if b.gccSupportsFlag(compiler, "-fno-caret-diagnostics") {
  2263  		a = append(a, "-fno-caret-diagnostics")
  2264  	}
  2265  	// clang is too smart about command-line arguments
  2266  	if b.gccSupportsFlag(compiler, "-Qunused-arguments") {
  2267  		a = append(a, "-Qunused-arguments")
  2268  	}
  2269  
  2270  	// disable word wrapping in error messages
  2271  	a = append(a, "-fmessage-length=0")
  2272  
  2273  	// Tell gcc not to include the work directory in object files.
  2274  	if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") {
  2275  		if workdir == "" {
  2276  			workdir = b.WorkDir
  2277  		}
  2278  		workdir = strings.TrimSuffix(workdir, string(filepath.Separator))
  2279  		a = append(a, "-fdebug-prefix-map="+workdir+"=/tmp/go-build")
  2280  	}
  2281  
  2282  	// Tell gcc not to include flags in object files, which defeats the
  2283  	// point of -fdebug-prefix-map above.
  2284  	if b.gccSupportsFlag(compiler, "-gno-record-gcc-switches") {
  2285  		a = append(a, "-gno-record-gcc-switches")
  2286  	}
  2287  
  2288  	// On OS X, some of the compilers behave as if -fno-common
  2289  	// is always set, and the Mach-O linker in 6l/8l assumes this.
  2290  	// See https://golang.org/issue/3253.
  2291  	if cfg.Goos == "darwin" {
  2292  		a = append(a, "-fno-common")
  2293  	}
  2294  
  2295  	return a
  2296  }
  2297  
  2298  // gccNoPie returns the flag to use to request non-PIE. On systems
  2299  // with PIE (position independent executables) enabled by default,
  2300  // -no-pie must be passed when doing a partial link with -Wl,-r.
  2301  // But -no-pie is not supported by all compilers, and clang spells it -nopie.
  2302  func (b *Builder) gccNoPie(linker []string) string {
  2303  	if b.gccSupportsFlag(linker, "-no-pie") {
  2304  		return "-no-pie"
  2305  	}
  2306  	if b.gccSupportsFlag(linker, "-nopie") {
  2307  		return "-nopie"
  2308  	}
  2309  	return ""
  2310  }
  2311  
  2312  // gccSupportsFlag checks to see if the compiler supports a flag.
  2313  func (b *Builder) gccSupportsFlag(compiler []string, flag string) bool {
  2314  	key := [2]string{compiler[0], flag}
  2315  
  2316  	b.exec.Lock()
  2317  	defer b.exec.Unlock()
  2318  	if b, ok := b.flagCache[key]; ok {
  2319  		return b
  2320  	}
  2321  	if b.flagCache == nil {
  2322  		b.flagCache = make(map[[2]string]bool)
  2323  	}
  2324  	// We used to write an empty C file, but that gets complicated with
  2325  	// go build -n. We tried using a file that does not exist, but that
  2326  	// fails on systems with GCC version 4.2.1; that is the last GPLv2
  2327  	// version of GCC, so some systems have frozen on it.
  2328  	// Now we pass an empty file on stdin, which should work at least for
  2329  	// GCC and clang.
  2330  	cmdArgs := str.StringList(compiler, flag, "-c", "-x", "c", "-")
  2331  	if cfg.BuildN || cfg.BuildX {
  2332  		b.Showcmd(b.WorkDir, "%s || true", joinUnambiguously(cmdArgs))
  2333  		if cfg.BuildN {
  2334  			return false
  2335  		}
  2336  	}
  2337  	cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
  2338  	cmd.Dir = b.WorkDir
  2339  	cmd.Env = base.MergeEnvLists([]string{"LC_ALL=C"}, base.EnvForDir(cmd.Dir, os.Environ()))
  2340  	out, _ := cmd.CombinedOutput()
  2341  	// GCC says "unrecognized command line option".
  2342  	// clang says "unknown argument".
  2343  	// Older versions of GCC say "unrecognised debug output level".
  2344  	// For -fsplit-stack GCC says "'-fsplit-stack' is not supported".
  2345  	supported := !bytes.Contains(out, []byte("unrecognized")) &&
  2346  		!bytes.Contains(out, []byte("unknown")) &&
  2347  		!bytes.Contains(out, []byte("unrecognised")) &&
  2348  		!bytes.Contains(out, []byte("is not supported"))
  2349  	b.flagCache[key] = supported
  2350  	return supported
  2351  }
  2352  
  2353  // gccArchArgs returns arguments to pass to gcc based on the architecture.
  2354  func (b *Builder) gccArchArgs() []string {
  2355  	switch cfg.Goarch {
  2356  	case "386":
  2357  		return []string{"-m32"}
  2358  	case "amd64", "amd64p32":
  2359  		return []string{"-m64"}
  2360  	case "arm":
  2361  		return []string{"-marm"} // not thumb
  2362  	case "s390x":
  2363  		return []string{"-m64", "-march=z196"}
  2364  	case "mips64", "mips64le":
  2365  		return []string{"-mabi=64"}
  2366  	case "mips", "mipsle":
  2367  		return []string{"-mabi=32", "-march=mips32"}
  2368  	case "ppc64":
  2369  		if cfg.Goos == "aix" {
  2370  			return []string{"-maix64"}
  2371  		}
  2372  	}
  2373  	return nil
  2374  }
  2375  
  2376  // envList returns the value of the given environment variable broken
  2377  // into fields, using the default value when the variable is empty.
  2378  func envList(key, def string) []string {
  2379  	v := os.Getenv(key)
  2380  	if v == "" {
  2381  		v = def
  2382  	}
  2383  	return strings.Fields(v)
  2384  }
  2385  
  2386  // CFlags returns the flags to use when invoking the C, C++ or Fortran compilers, or cgo.
  2387  func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, ldflags []string, err error) {
  2388  	defaults := "-g -O2"
  2389  
  2390  	if cppflags, err = buildFlags("CPPFLAGS", "", p.CgoCPPFLAGS, checkCompilerFlags); err != nil {
  2391  		return
  2392  	}
  2393  	if cflags, err = buildFlags("CFLAGS", defaults, p.CgoCFLAGS, checkCompilerFlags); err != nil {
  2394  		return
  2395  	}
  2396  	if cxxflags, err = buildFlags("CXXFLAGS", defaults, p.CgoCXXFLAGS, checkCompilerFlags); err != nil {
  2397  		return
  2398  	}
  2399  	if fflags, err = buildFlags("FFLAGS", defaults, p.CgoFFLAGS, checkCompilerFlags); err != nil {
  2400  		return
  2401  	}
  2402  	if ldflags, err = buildFlags("LDFLAGS", defaults, p.CgoLDFLAGS, checkLinkerFlags); err != nil {
  2403  		return
  2404  	}
  2405  
  2406  	return
  2407  }
  2408  
  2409  func buildFlags(name, defaults string, fromPackage []string, check func(string, string, []string) error) ([]string, error) {
  2410  	if err := check(name, "#cgo "+name, fromPackage); err != nil {
  2411  		return nil, err
  2412  	}
  2413  	return str.StringList(envList("CGO_"+name, defaults), fromPackage), nil
  2414  }
  2415  
  2416  var cgoRe = regexp.MustCompile(`[/\\:]`)
  2417  
  2418  func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) {
  2419  	p := a.Package
  2420  	cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS, err := b.CFlags(p)
  2421  	if err != nil {
  2422  		return nil, nil, err
  2423  	}
  2424  
  2425  	cgoCPPFLAGS = append(cgoCPPFLAGS, pcCFLAGS...)
  2426  	cgoLDFLAGS = append(cgoLDFLAGS, pcLDFLAGS...)
  2427  	// If we are compiling Objective-C code, then we need to link against libobjc
  2428  	if len(mfiles) > 0 {
  2429  		cgoLDFLAGS = append(cgoLDFLAGS, "-lobjc")
  2430  	}
  2431  
  2432  	// Likewise for Fortran, except there are many Fortran compilers.
  2433  	// Support gfortran out of the box and let others pass the correct link options
  2434  	// via CGO_LDFLAGS
  2435  	if len(ffiles) > 0 {
  2436  		fc := os.Getenv("FC")
  2437  		if fc == "" {
  2438  			fc = "gfortran"
  2439  		}
  2440  		if strings.Contains(fc, "gfortran") {
  2441  			cgoLDFLAGS = append(cgoLDFLAGS, "-lgfortran")
  2442  		}
  2443  	}
  2444  
  2445  	if cfg.BuildMSan {
  2446  		cgoCFLAGS = append([]string{"-fsanitize=memory"}, cgoCFLAGS...)
  2447  		cgoLDFLAGS = append([]string{"-fsanitize=memory"}, cgoLDFLAGS...)
  2448  	}
  2449  
  2450  	// Allows including _cgo_export.h from .[ch] files in the package.
  2451  	cgoCPPFLAGS = append(cgoCPPFLAGS, "-I", objdir)
  2452  
  2453  	// cgo
  2454  	// TODO: CGO_FLAGS?
  2455  	gofiles := []string{objdir + "_cgo_gotypes.go"}
  2456  	cfiles := []string{"_cgo_export.c"}
  2457  	for _, fn := range cgofiles {
  2458  		f := strings.TrimSuffix(filepath.Base(fn), ".go")
  2459  		gofiles = append(gofiles, objdir+f+".cgo1.go")
  2460  		cfiles = append(cfiles, f+".cgo2.c")
  2461  	}
  2462  
  2463  	// TODO: make cgo not depend on $GOARCH?
  2464  
  2465  	cgoflags := []string{}
  2466  	if p.Standard && p.ImportPath == "runtime/cgo" {
  2467  		cgoflags = append(cgoflags, "-import_runtime_cgo=false")
  2468  	}
  2469  	if p.Standard && (p.ImportPath == "runtime/race" || p.ImportPath == "runtime/msan" || p.ImportPath == "runtime/cgo") {
  2470  		cgoflags = append(cgoflags, "-import_syscall=false")
  2471  	}
  2472  
  2473  	// Update $CGO_LDFLAGS with p.CgoLDFLAGS.
  2474  	// These flags are recorded in the generated _cgo_gotypes.go file
  2475  	// using //go:cgo_ldflag directives, the compiler records them in the
  2476  	// object file for the package, and then the Go linker passes them
  2477  	// along to the host linker. At this point in the code, cgoLDFLAGS
  2478  	// consists of the original $CGO_LDFLAGS (unchecked) and all the
  2479  	// flags put together from source code (checked).
  2480  	cgoenv := b.cCompilerEnv()
  2481  	if len(cgoLDFLAGS) > 0 {
  2482  		flags := make([]string, len(cgoLDFLAGS))
  2483  		for i, f := range cgoLDFLAGS {
  2484  			flags[i] = strconv.Quote(f)
  2485  		}
  2486  		cgoenv = []string{"CGO_LDFLAGS=" + strings.Join(flags, " ")}
  2487  	}
  2488  
  2489  	if cfg.BuildToolchainName == "gccgo" {
  2490  		switch cfg.Goarch {
  2491  		case "386", "amd64":
  2492  			cgoCFLAGS = append(cgoCFLAGS, "-fsplit-stack")
  2493  		}
  2494  		cgoflags = append(cgoflags, "-gccgo")
  2495  		if pkgpath := gccgoPkgpath(p); pkgpath != "" {
  2496  			cgoflags = append(cgoflags, "-gccgopkgpath="+pkgpath)
  2497  		}
  2498  	}
  2499  
  2500  	switch cfg.BuildBuildmode {
  2501  	case "c-archive", "c-shared":
  2502  		// Tell cgo that if there are any exported functions
  2503  		// it should generate a header file that C code can
  2504  		// #include.
  2505  		cgoflags = append(cgoflags, "-exportheader="+objdir+"_cgo_install.h")
  2506  	}
  2507  
  2508  	if err := b.run(a, p.Dir, p.ImportPath, cgoenv, cfg.BuildToolexec, cgoExe, "-objdir", objdir, "-importpath", p.ImportPath, cgoflags, "--", cgoCPPFLAGS, cgoCFLAGS, cgofiles); err != nil {
  2509  		return nil, nil, err
  2510  	}
  2511  	outGo = append(outGo, gofiles...)
  2512  
  2513  	// Use sequential object file names to keep them distinct
  2514  	// and short enough to fit in the .a header file name slots.
  2515  	// We no longer collect them all into _all.o, and we'd like
  2516  	// tools to see both the .o suffix and unique names, so
  2517  	// we need to make them short enough not to be truncated
  2518  	// in the final archive.
  2519  	oseq := 0
  2520  	nextOfile := func() string {
  2521  		oseq++
  2522  		return objdir + fmt.Sprintf("_x%03d.o", oseq)
  2523  	}
  2524  
  2525  	// gcc
  2526  	cflags := str.StringList(cgoCPPFLAGS, cgoCFLAGS)
  2527  	for _, cfile := range cfiles {
  2528  		ofile := nextOfile()
  2529  		if err := b.gcc(a, p, a.Objdir, ofile, cflags, objdir+cfile); err != nil {
  2530  			return nil, nil, err
  2531  		}
  2532  		outObj = append(outObj, ofile)
  2533  	}
  2534  
  2535  	for _, file := range gccfiles {
  2536  		ofile := nextOfile()
  2537  		if err := b.gcc(a, p, a.Objdir, ofile, cflags, file); err != nil {
  2538  			return nil, nil, err
  2539  		}
  2540  		outObj = append(outObj, ofile)
  2541  	}
  2542  
  2543  	cxxflags := str.StringList(cgoCPPFLAGS, cgoCXXFLAGS)
  2544  	for _, file := range gxxfiles {
  2545  		ofile := nextOfile()
  2546  		if err := b.gxx(a, p, a.Objdir, ofile, cxxflags, file); err != nil {
  2547  			return nil, nil, err
  2548  		}
  2549  		outObj = append(outObj, ofile)
  2550  	}
  2551  
  2552  	for _, file := range mfiles {
  2553  		ofile := nextOfile()
  2554  		if err := b.gcc(a, p, a.Objdir, ofile, cflags, file); err != nil {
  2555  			return nil, nil, err
  2556  		}
  2557  		outObj = append(outObj, ofile)
  2558  	}
  2559  
  2560  	fflags := str.StringList(cgoCPPFLAGS, cgoFFLAGS)
  2561  	for _, file := range ffiles {
  2562  		ofile := nextOfile()
  2563  		if err := b.gfortran(a, p, a.Objdir, ofile, fflags, file); err != nil {
  2564  			return nil, nil, err
  2565  		}
  2566  		outObj = append(outObj, ofile)
  2567  	}
  2568  
  2569  	switch cfg.BuildToolchainName {
  2570  	case "gc":
  2571  		importGo := objdir + "_cgo_import.go"
  2572  		if err := b.dynimport(a, p, objdir, importGo, cgoExe, cflags, cgoLDFLAGS, outObj); err != nil {
  2573  			return nil, nil, err
  2574  		}
  2575  		outGo = append(outGo, importGo)
  2576  
  2577  	case "gccgo":
  2578  		defunC := objdir + "_cgo_defun.c"
  2579  		defunObj := objdir + "_cgo_defun.o"
  2580  		if err := BuildToolchain.cc(b, a, defunObj, defunC); err != nil {
  2581  			return nil, nil, err
  2582  		}
  2583  		outObj = append(outObj, defunObj)
  2584  
  2585  	default:
  2586  		noCompiler()
  2587  	}
  2588  
  2589  	return outGo, outObj, nil
  2590  }
  2591  
  2592  // dynimport creates a Go source file named importGo containing
  2593  // //go:cgo_import_dynamic directives for each symbol or library
  2594  // dynamically imported by the object files outObj.
  2595  func (b *Builder) dynimport(a *Action, p *load.Package, objdir, importGo, cgoExe string, cflags, cgoLDFLAGS, outObj []string) error {
  2596  	cfile := objdir + "_cgo_main.c"
  2597  	ofile := objdir + "_cgo_main.o"
  2598  	if err := b.gcc(a, p, objdir, ofile, cflags, cfile); err != nil {
  2599  		return err
  2600  	}
  2601  
  2602  	linkobj := str.StringList(ofile, outObj, p.SysoFiles)
  2603  	dynobj := objdir + "_cgo_.o"
  2604  
  2605  	// we need to use -pie for Linux/ARM to get accurate imported sym
  2606  	ldflags := cgoLDFLAGS
  2607  	if (cfg.Goarch == "arm" && cfg.Goos == "linux") || cfg.Goos == "android" {
  2608  		// -static -pie doesn't make sense, and causes link errors.
  2609  		// Issue 26197.
  2610  		n := make([]string, 0, len(ldflags))
  2611  		for _, flag := range ldflags {
  2612  			if flag != "-static" {
  2613  				n = append(n, flag)
  2614  			}
  2615  		}
  2616  		ldflags = append(n, "-pie")
  2617  	}
  2618  	if err := b.gccld(p, objdir, dynobj, ldflags, linkobj); err != nil {
  2619  		return err
  2620  	}
  2621  
  2622  	// cgo -dynimport
  2623  	var cgoflags []string
  2624  	if p.Standard && p.ImportPath == "runtime/cgo" {
  2625  		cgoflags = []string{"-dynlinker"} // record path to dynamic linker
  2626  	}
  2627  	return b.run(a, p.Dir, p.ImportPath, b.cCompilerEnv(), cfg.BuildToolexec, cgoExe, "-dynpackage", p.Name, "-dynimport", dynobj, "-dynout", importGo, cgoflags)
  2628  }
  2629  
  2630  // Run SWIG on all SWIG input files.
  2631  // TODO: Don't build a shared library, once SWIG emits the necessary
  2632  // pragmas for external linking.
  2633  func (b *Builder) swig(a *Action, p *load.Package, objdir string, pcCFLAGS []string) (outGo, outC, outCXX []string, err error) {
  2634  	if err := b.swigVersionCheck(); err != nil {
  2635  		return nil, nil, nil, err
  2636  	}
  2637  
  2638  	intgosize, err := b.swigIntSize(objdir)
  2639  	if err != nil {
  2640  		return nil, nil, nil, err
  2641  	}
  2642  
  2643  	for _, f := range p.SwigFiles {
  2644  		goFile, cFile, err := b.swigOne(a, p, f, objdir, pcCFLAGS, false, intgosize)
  2645  		if err != nil {
  2646  			return nil, nil, nil, err
  2647  		}
  2648  		if goFile != "" {
  2649  			outGo = append(outGo, goFile)
  2650  		}
  2651  		if cFile != "" {
  2652  			outC = append(outC, cFile)
  2653  		}
  2654  	}
  2655  	for _, f := range p.SwigCXXFiles {
  2656  		goFile, cxxFile, err := b.swigOne(a, p, f, objdir, pcCFLAGS, true, intgosize)
  2657  		if err != nil {
  2658  			return nil, nil, nil, err
  2659  		}
  2660  		if goFile != "" {
  2661  			outGo = append(outGo, goFile)
  2662  		}
  2663  		if cxxFile != "" {
  2664  			outCXX = append(outCXX, cxxFile)
  2665  		}
  2666  	}
  2667  	return outGo, outC, outCXX, nil
  2668  }
  2669  
  2670  // Make sure SWIG is new enough.
  2671  var (
  2672  	swigCheckOnce sync.Once
  2673  	swigCheck     error
  2674  )
  2675  
  2676  func (b *Builder) swigDoVersionCheck() error {
  2677  	out, err := b.runOut("", nil, "swig", "-version")
  2678  	if err != nil {
  2679  		return err
  2680  	}
  2681  	re := regexp.MustCompile(`[vV]ersion +([\d]+)([.][\d]+)?([.][\d]+)?`)
  2682  	matches := re.FindSubmatch(out)
  2683  	if matches == nil {
  2684  		// Can't find version number; hope for the best.
  2685  		return nil
  2686  	}
  2687  
  2688  	major, err := strconv.Atoi(string(matches[1]))
  2689  	if err != nil {
  2690  		// Can't find version number; hope for the best.
  2691  		return nil
  2692  	}
  2693  	const errmsg = "must have SWIG version >= 3.0.6"
  2694  	if major < 3 {
  2695  		return errors.New(errmsg)
  2696  	}
  2697  	if major > 3 {
  2698  		// 4.0 or later
  2699  		return nil
  2700  	}
  2701  
  2702  	// We have SWIG version 3.x.
  2703  	if len(matches[2]) > 0 {
  2704  		minor, err := strconv.Atoi(string(matches[2][1:]))
  2705  		if err != nil {
  2706  			return nil
  2707  		}
  2708  		if minor > 0 {
  2709  			// 3.1 or later
  2710  			return nil
  2711  		}
  2712  	}
  2713  
  2714  	// We have SWIG version 3.0.x.
  2715  	if len(matches[3]) > 0 {
  2716  		patch, err := strconv.Atoi(string(matches[3][1:]))
  2717  		if err != nil {
  2718  			return nil
  2719  		}
  2720  		if patch < 6 {
  2721  			// Before 3.0.6.
  2722  			return errors.New(errmsg)
  2723  		}
  2724  	}
  2725  
  2726  	return nil
  2727  }
  2728  
  2729  func (b *Builder) swigVersionCheck() error {
  2730  	swigCheckOnce.Do(func() {
  2731  		swigCheck = b.swigDoVersionCheck()
  2732  	})
  2733  	return swigCheck
  2734  }
  2735  
  2736  // Find the value to pass for the -intgosize option to swig.
  2737  var (
  2738  	swigIntSizeOnce  sync.Once
  2739  	swigIntSize      string
  2740  	swigIntSizeError error
  2741  )
  2742  
  2743  // This code fails to build if sizeof(int) <= 32
  2744  const swigIntSizeCode = `
  2745  package main
  2746  const i int = 1 << 32
  2747  `
  2748  
  2749  // Determine the size of int on the target system for the -intgosize option
  2750  // of swig >= 2.0.9. Run only once.
  2751  func (b *Builder) swigDoIntSize(objdir string) (intsize string, err error) {
  2752  	if cfg.BuildN {
  2753  		return "$INTBITS", nil
  2754  	}
  2755  	src := filepath.Join(b.WorkDir, "swig_intsize.go")
  2756  	if err = ioutil.WriteFile(src, []byte(swigIntSizeCode), 0666); err != nil {
  2757  		return
  2758  	}
  2759  	srcs := []string{src}
  2760  
  2761  	p := load.GoFilesPackage(srcs)
  2762  
  2763  	if _, _, e := BuildToolchain.gc(b, &Action{Mode: "swigDoIntSize", Package: p, Objdir: objdir}, "", nil, "", false, srcs); e != nil {
  2764  		return "32", nil
  2765  	}
  2766  	return "64", nil
  2767  }
  2768  
  2769  // Determine the size of int on the target system for the -intgosize option
  2770  // of swig >= 2.0.9.
  2771  func (b *Builder) swigIntSize(objdir string) (intsize string, err error) {
  2772  	swigIntSizeOnce.Do(func() {
  2773  		swigIntSize, swigIntSizeError = b.swigDoIntSize(objdir)
  2774  	})
  2775  	return swigIntSize, swigIntSizeError
  2776  }
  2777  
  2778  // Run SWIG on one SWIG input file.
  2779  func (b *Builder) swigOne(a *Action, p *load.Package, file, objdir string, pcCFLAGS []string, cxx bool, intgosize string) (outGo, outC string, err error) {
  2780  	cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p)
  2781  	if err != nil {
  2782  		return "", "", err
  2783  	}
  2784  
  2785  	var cflags []string
  2786  	if cxx {
  2787  		cflags = str.StringList(cgoCPPFLAGS, pcCFLAGS, cgoCXXFLAGS)
  2788  	} else {
  2789  		cflags = str.StringList(cgoCPPFLAGS, pcCFLAGS, cgoCFLAGS)
  2790  	}
  2791  
  2792  	n := 5 // length of ".swig"
  2793  	if cxx {
  2794  		n = 8 // length of ".swigcxx"
  2795  	}
  2796  	base := file[:len(file)-n]
  2797  	goFile := base + ".go"
  2798  	gccBase := base + "_wrap."
  2799  	gccExt := "c"
  2800  	if cxx {
  2801  		gccExt = "cxx"
  2802  	}
  2803  
  2804  	gccgo := cfg.BuildToolchainName == "gccgo"
  2805  
  2806  	// swig
  2807  	args := []string{
  2808  		"-go",
  2809  		"-cgo",
  2810  		"-intgosize", intgosize,
  2811  		"-module", base,
  2812  		"-o", objdir + gccBase + gccExt,
  2813  		"-outdir", objdir,
  2814  	}
  2815  
  2816  	for _, f := range cflags {
  2817  		if len(f) > 3 && f[:2] == "-I" {
  2818  			args = append(args, f)
  2819  		}
  2820  	}
  2821  
  2822  	if gccgo {
  2823  		args = append(args, "-gccgo")
  2824  		if pkgpath := gccgoPkgpath(p); pkgpath != "" {
  2825  			args = append(args, "-go-pkgpath", pkgpath)
  2826  		}
  2827  	}
  2828  	if cxx {
  2829  		args = append(args, "-c++")
  2830  	}
  2831  
  2832  	out, err := b.runOut(p.Dir, nil, "swig", args, file)
  2833  	if err != nil {
  2834  		if len(out) > 0 {
  2835  			if bytes.Contains(out, []byte("-intgosize")) || bytes.Contains(out, []byte("-cgo")) {
  2836  				return "", "", errors.New("must have SWIG version >= 3.0.6")
  2837  			}
  2838  			b.showOutput(a, p.Dir, p.Desc(), b.processOutput(out)) // swig error
  2839  			return "", "", errPrintedOutput
  2840  		}
  2841  		return "", "", err
  2842  	}
  2843  	if len(out) > 0 {
  2844  		b.showOutput(a, p.Dir, p.Desc(), b.processOutput(out)) // swig warning
  2845  	}
  2846  
  2847  	// If the input was x.swig, the output is x.go in the objdir.
  2848  	// But there might be an x.go in the original dir too, and if it
  2849  	// uses cgo as well, cgo will be processing both and will
  2850  	// translate both into x.cgo1.go in the objdir, overwriting one.
  2851  	// Rename x.go to _x_swig.go to avoid this problem.
  2852  	// We ignore files in the original dir that begin with underscore
  2853  	// so _x_swig.go cannot conflict with an original file we were
  2854  	// going to compile.
  2855  	goFile = objdir + goFile
  2856  	newGoFile := objdir + "_" + base + "_swig.go"
  2857  	if err := os.Rename(goFile, newGoFile); err != nil {
  2858  		return "", "", err
  2859  	}
  2860  	return newGoFile, objdir + gccBase + gccExt, nil
  2861  }
  2862  
  2863  // disableBuildID adjusts a linker command line to avoid creating a
  2864  // build ID when creating an object file rather than an executable or
  2865  // shared library. Some systems, such as Ubuntu, always add
  2866  // --build-id to every link, but we don't want a build ID when we are
  2867  // producing an object file. On some of those system a plain -r (not
  2868  // -Wl,-r) will turn off --build-id, but clang 3.0 doesn't support a
  2869  // plain -r. I don't know how to turn off --build-id when using clang
  2870  // other than passing a trailing --build-id=none. So that is what we
  2871  // do, but only on systems likely to support it, which is to say,
  2872  // systems that normally use gold or the GNU linker.
  2873  func (b *Builder) disableBuildID(ldflags []string) []string {
  2874  	switch cfg.Goos {
  2875  	case "android", "dragonfly", "linux", "netbsd":
  2876  		ldflags = append(ldflags, "-Wl,--build-id=none")
  2877  	}
  2878  	return ldflags
  2879  }
  2880  
  2881  // mkAbsFiles converts files into a list of absolute files,
  2882  // assuming they were originally relative to dir,
  2883  // and returns that new list.
  2884  func mkAbsFiles(dir string, files []string) []string {
  2885  	abs := make([]string, len(files))
  2886  	for i, f := range files {
  2887  		if !filepath.IsAbs(f) {
  2888  			f = filepath.Join(dir, f)
  2889  		}
  2890  		abs[i] = f
  2891  	}
  2892  	return abs
  2893  }
  2894  
  2895  // passLongArgsInResponseFiles modifies cmd on Windows such that, for
  2896  // certain programs, long arguments are passed in "response files", a
  2897  // file on disk with the arguments, with one arg per line. An actual
  2898  // argument starting with '@' means that the rest of the argument is
  2899  // a filename of arguments to expand.
  2900  //
  2901  // See Issue 18468.
  2902  func passLongArgsInResponseFiles(cmd *exec.Cmd) (cleanup func()) {
  2903  	cleanup = func() {} // no cleanup by default
  2904  
  2905  	var argLen int
  2906  	for _, arg := range cmd.Args {
  2907  		argLen += len(arg)
  2908  	}
  2909  
  2910  	// If we're not approaching 32KB of args, just pass args normally.
  2911  	// (use 30KB instead to be conservative; not sure how accounting is done)
  2912  	if !useResponseFile(cmd.Path, argLen) {
  2913  		return
  2914  	}
  2915  
  2916  	tf, err := ioutil.TempFile("", "args")
  2917  	if err != nil {
  2918  		log.Fatalf("error writing long arguments to response file: %v", err)
  2919  	}
  2920  	cleanup = func() { os.Remove(tf.Name()) }
  2921  	var buf bytes.Buffer
  2922  	for _, arg := range cmd.Args[1:] {
  2923  		fmt.Fprintf(&buf, "%s\n", arg)
  2924  	}
  2925  	if _, err := tf.Write(buf.Bytes()); err != nil {
  2926  		tf.Close()
  2927  		cleanup()
  2928  		log.Fatalf("error writing long arguments to response file: %v", err)
  2929  	}
  2930  	if err := tf.Close(); err != nil {
  2931  		cleanup()
  2932  		log.Fatalf("error writing long arguments to response file: %v", err)
  2933  	}
  2934  	cmd.Args = []string{cmd.Args[0], "@" + tf.Name()}
  2935  	return cleanup
  2936  }
  2937  
  2938  func useResponseFile(path string, argLen int) bool {
  2939  	// Unless we're on Windows, don't use response files.
  2940  	if runtime.GOOS != "windows" {
  2941  		return false
  2942  	}
  2943  
  2944  	// Unless the program uses objabi.Flagparse, which understands
  2945  	// response files, don't use response files.
  2946  	// TODO: do we need more commands? asm? cgo? For now, no.
  2947  	prog := strings.TrimSuffix(filepath.Base(path), ".exe")
  2948  	switch prog {
  2949  	case "compile", "link":
  2950  	default:
  2951  		return false
  2952  	}
  2953  
  2954  	// Windows has a limit of 32 KB arguments. To be conservative and not
  2955  	// worry about whether that includes spaces or not, just use 30 KB.
  2956  	if argLen > (30 << 10) {
  2957  		return true
  2958  	}
  2959  
  2960  	// On the Go build system, use response files about 10% of the
  2961  	// time, just to exercise this codepath.
  2962  	isBuilder := os.Getenv("GO_BUILDER_NAME") != ""
  2963  	if isBuilder && rand.Intn(10) == 0 {
  2964  		return true
  2965  	}
  2966  
  2967  	return false
  2968  }