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