github.com/freddyisaac/sicortex-golang@v0.0.0-20231019035217-e03519e66f60/src/cmd/go/pkg.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  package main
     6  
     7  import (
     8  	"bytes"
     9  	"crypto/sha1"
    10  	"errors"
    11  	"fmt"
    12  	"go/build"
    13  	"go/scanner"
    14  	"go/token"
    15  	"io"
    16  	"io/ioutil"
    17  	"os"
    18  	pathpkg "path"
    19  	"path/filepath"
    20  	"runtime"
    21  	"sort"
    22  	"strconv"
    23  	"strings"
    24  	"unicode"
    25  	"unicode/utf8"
    26  )
    27  
    28  var ignoreImports bool // control whether we ignore imports in packages
    29  
    30  // A Package describes a single package found in a directory.
    31  type Package struct {
    32  	// Note: These fields are part of the go command's public API.
    33  	// See list.go. It is okay to add fields, but not to change or
    34  	// remove existing ones. Keep in sync with list.go
    35  	Dir           string `json:",omitempty"` // directory containing package sources
    36  	ImportPath    string `json:",omitempty"` // import path of package in dir
    37  	ImportComment string `json:",omitempty"` // path in import comment on package statement
    38  	Name          string `json:",omitempty"` // package name
    39  	Doc           string `json:",omitempty"` // package documentation string
    40  	Target        string `json:",omitempty"` // install path
    41  	Shlib         string `json:",omitempty"` // the shared library that contains this package (only set when -linkshared)
    42  	Goroot        bool   `json:",omitempty"` // is this package found in the Go root?
    43  	Standard      bool   `json:",omitempty"` // is this package part of the standard Go library?
    44  	Stale         bool   `json:",omitempty"` // would 'go install' do anything for this package?
    45  	StaleReason   string `json:",omitempty"` // why is Stale true?
    46  	Root          string `json:",omitempty"` // Go root or Go path dir containing this package
    47  	ConflictDir   string `json:",omitempty"` // Dir is hidden by this other directory
    48  	BinaryOnly    bool   `json:",omitempty"` // package cannot be recompiled
    49  
    50  	// Source files
    51  	// If you add to this list you MUST add to p.AllFiles (below) too.
    52  	// Otherwise file name security lists will not apply to any new additions.
    53  	GoFiles        []string `json:",omitempty"` // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles)
    54  	CgoFiles       []string `json:",omitempty"` // .go sources files that import "C"
    55  	IgnoredGoFiles []string `json:",omitempty"` // .go sources ignored due to build constraints
    56  	CFiles         []string `json:",omitempty"` // .c source files
    57  	CXXFiles       []string `json:",omitempty"` // .cc, .cpp and .cxx source files
    58  	MFiles         []string `json:",omitempty"` // .m source files
    59  	HFiles         []string `json:",omitempty"` // .h, .hh, .hpp and .hxx source files
    60  	FFiles         []string `json:",omitempty"` // .f, .F, .for and .f90 Fortran source files
    61  	SFiles         []string `json:",omitempty"` // .s source files
    62  	SwigFiles      []string `json:",omitempty"` // .swig files
    63  	SwigCXXFiles   []string `json:",omitempty"` // .swigcxx files
    64  	SysoFiles      []string `json:",omitempty"` // .syso system object files added to package
    65  
    66  	// Cgo directives
    67  	CgoCFLAGS    []string `json:",omitempty"` // cgo: flags for C compiler
    68  	CgoCPPFLAGS  []string `json:",omitempty"` // cgo: flags for C preprocessor
    69  	CgoCXXFLAGS  []string `json:",omitempty"` // cgo: flags for C++ compiler
    70  	CgoFFLAGS    []string `json:",omitempty"` // cgo: flags for Fortran compiler
    71  	CgoLDFLAGS   []string `json:",omitempty"` // cgo: flags for linker
    72  	CgoPkgConfig []string `json:",omitempty"` // cgo: pkg-config names
    73  
    74  	// Dependency information
    75  	Imports []string `json:",omitempty"` // import paths used by this package
    76  	Deps    []string `json:",omitempty"` // all (recursively) imported dependencies
    77  
    78  	// Error information
    79  	Incomplete bool            `json:",omitempty"` // was there an error loading this package or dependencies?
    80  	Error      *PackageError   `json:",omitempty"` // error loading this package (not dependencies)
    81  	DepsErrors []*PackageError `json:",omitempty"` // errors loading dependencies
    82  
    83  	// Test information
    84  	// If you add to this list you MUST add to p.AllFiles (below) too.
    85  	// Otherwise file name security lists will not apply to any new additions.
    86  	TestGoFiles  []string `json:",omitempty"` // _test.go files in package
    87  	TestImports  []string `json:",omitempty"` // imports from TestGoFiles
    88  	XTestGoFiles []string `json:",omitempty"` // _test.go files outside package
    89  	XTestImports []string `json:",omitempty"` // imports from XTestGoFiles
    90  
    91  	// Unexported fields are not part of the public API.
    92  	build        *build.Package
    93  	pkgdir       string // overrides build.PkgDir
    94  	imports      []*Package
    95  	deps         []*Package
    96  	gofiles      []string // GoFiles+CgoFiles+TestGoFiles+XTestGoFiles files, absolute paths
    97  	sfiles       []string
    98  	allgofiles   []string             // gofiles + IgnoredGoFiles, absolute paths
    99  	target       string               // installed file for this package (may be executable)
   100  	fake         bool                 // synthesized package
   101  	external     bool                 // synthesized external test package
   102  	forceLibrary bool                 // this package is a library (even if named "main")
   103  	cmdline      bool                 // defined by files listed on command line
   104  	local        bool                 // imported via local path (./ or ../)
   105  	localPrefix  string               // interpret ./ and ../ imports relative to this prefix
   106  	exeName      string               // desired name for temporary executable
   107  	coverMode    string               // preprocess Go source files with the coverage tool in this mode
   108  	coverVars    map[string]*CoverVar // variables created by coverage analysis
   109  	omitDWARF    bool                 // tell linker not to write DWARF information
   110  	buildID      string               // expected build ID for generated package
   111  	gobinSubdir  bool                 // install target would be subdir of GOBIN
   112  }
   113  
   114  // allFiles returns the names of all the files considered for the package.
   115  // This is used for sanity and security checks, so we include all files,
   116  // even IgnoredGoFiles, because some subcommands consider them.
   117  // The go/build package filtered others out (like foo_wrongGOARCH.s)
   118  // and that's OK.
   119  func (p *Package) allFiles() []string {
   120  	return stringList(
   121  		p.GoFiles,
   122  		p.CgoFiles,
   123  		p.IgnoredGoFiles,
   124  		p.CFiles,
   125  		p.CXXFiles,
   126  		p.MFiles,
   127  		p.HFiles,
   128  		p.FFiles,
   129  		p.SFiles,
   130  		p.SwigFiles,
   131  		p.SwigCXXFiles,
   132  		p.SysoFiles,
   133  		p.TestGoFiles,
   134  		p.XTestGoFiles,
   135  	)
   136  }
   137  
   138  // vendored returns the vendor-resolved version of imports,
   139  // which should be p.TestImports or p.XTestImports, NOT p.Imports.
   140  // The imports in p.TestImports and p.XTestImports are not recursively
   141  // loaded during the initial load of p, so they list the imports found in
   142  // the source file, but most processing should be over the vendor-resolved
   143  // import paths. We do this resolution lazily both to avoid file system work
   144  // and because the eventual real load of the test imports (during 'go test')
   145  // can produce better error messages if it starts with the original paths.
   146  // The initial load of p loads all the non-test imports and rewrites
   147  // the vendored paths, so nothing should ever call p.vendored(p.Imports).
   148  func (p *Package) vendored(imports []string) []string {
   149  	if len(imports) > 0 && len(p.Imports) > 0 && &imports[0] == &p.Imports[0] {
   150  		panic("internal error: p.vendored(p.Imports) called")
   151  	}
   152  	seen := make(map[string]bool)
   153  	var all []string
   154  	for _, path := range imports {
   155  		path = vendoredImportPath(p, path)
   156  		if !seen[path] {
   157  			seen[path] = true
   158  			all = append(all, path)
   159  		}
   160  	}
   161  	sort.Strings(all)
   162  	return all
   163  }
   164  
   165  // CoverVar holds the name of the generated coverage variables targeting the named file.
   166  type CoverVar struct {
   167  	File string // local file name
   168  	Var  string // name of count struct
   169  }
   170  
   171  func (p *Package) copyBuild(pp *build.Package) {
   172  	p.build = pp
   173  
   174  	if pp.PkgTargetRoot != "" && buildPkgdir != "" {
   175  		old := pp.PkgTargetRoot
   176  		pp.PkgRoot = buildPkgdir
   177  		pp.PkgTargetRoot = buildPkgdir
   178  		pp.PkgObj = filepath.Join(buildPkgdir, strings.TrimPrefix(pp.PkgObj, old))
   179  	}
   180  
   181  	p.Dir = pp.Dir
   182  	p.ImportPath = pp.ImportPath
   183  	p.ImportComment = pp.ImportComment
   184  	p.Name = pp.Name
   185  	p.Doc = pp.Doc
   186  	p.Root = pp.Root
   187  	p.ConflictDir = pp.ConflictDir
   188  	p.BinaryOnly = pp.BinaryOnly
   189  
   190  	// TODO? Target
   191  	p.Goroot = pp.Goroot
   192  	p.Standard = p.Goroot && p.ImportPath != "" && isStandardImportPath(p.ImportPath)
   193  	p.GoFiles = pp.GoFiles
   194  	p.CgoFiles = pp.CgoFiles
   195  	p.IgnoredGoFiles = pp.IgnoredGoFiles
   196  	p.CFiles = pp.CFiles
   197  	p.CXXFiles = pp.CXXFiles
   198  	p.MFiles = pp.MFiles
   199  	p.HFiles = pp.HFiles
   200  	p.FFiles = pp.FFiles
   201  	p.SFiles = pp.SFiles
   202  	p.SwigFiles = pp.SwigFiles
   203  	p.SwigCXXFiles = pp.SwigCXXFiles
   204  	p.SysoFiles = pp.SysoFiles
   205  	p.CgoCFLAGS = pp.CgoCFLAGS
   206  	p.CgoCPPFLAGS = pp.CgoCPPFLAGS
   207  	p.CgoCXXFLAGS = pp.CgoCXXFLAGS
   208  	p.CgoLDFLAGS = pp.CgoLDFLAGS
   209  	p.CgoPkgConfig = pp.CgoPkgConfig
   210  	// We modify p.Imports in place, so make copy now.
   211  	p.Imports = make([]string, len(pp.Imports))
   212  	copy(p.Imports, pp.Imports)
   213  	p.TestGoFiles = pp.TestGoFiles
   214  	p.TestImports = pp.TestImports
   215  	p.XTestGoFiles = pp.XTestGoFiles
   216  	p.XTestImports = pp.XTestImports
   217  	if ignoreImports {
   218  		p.Imports = nil
   219  		p.TestImports = nil
   220  		p.XTestImports = nil
   221  	}
   222  }
   223  
   224  // isStandardImportPath reports whether $GOROOT/src/path should be considered
   225  // part of the standard distribution. For historical reasons we allow people to add
   226  // their own code to $GOROOT instead of using $GOPATH, but we assume that
   227  // code will start with a domain name (dot in the first element).
   228  func isStandardImportPath(path string) bool {
   229  	i := strings.Index(path, "/")
   230  	if i < 0 {
   231  		i = len(path)
   232  	}
   233  	elem := path[:i]
   234  	return !strings.Contains(elem, ".")
   235  }
   236  
   237  // A PackageError describes an error loading information about a package.
   238  type PackageError struct {
   239  	ImportStack   []string // shortest path from package named on command line to this one
   240  	Pos           string   // position of error
   241  	Err           string   // the error itself
   242  	isImportCycle bool     // the error is an import cycle
   243  	hard          bool     // whether the error is soft or hard; soft errors are ignored in some places
   244  }
   245  
   246  func (p *PackageError) Error() string {
   247  	// Import cycles deserve special treatment.
   248  	if p.isImportCycle {
   249  		return fmt.Sprintf("%s\npackage %s\n", p.Err, strings.Join(p.ImportStack, "\n\timports "))
   250  	}
   251  	if p.Pos != "" {
   252  		// Omit import stack. The full path to the file where the error
   253  		// is the most important thing.
   254  		return p.Pos + ": " + p.Err
   255  	}
   256  	if len(p.ImportStack) == 0 {
   257  		return p.Err
   258  	}
   259  	return "package " + strings.Join(p.ImportStack, "\n\timports ") + ": " + p.Err
   260  }
   261  
   262  // An importStack is a stack of import paths.
   263  type importStack []string
   264  
   265  func (s *importStack) push(p string) {
   266  	*s = append(*s, p)
   267  }
   268  
   269  func (s *importStack) pop() {
   270  	*s = (*s)[0 : len(*s)-1]
   271  }
   272  
   273  func (s *importStack) copy() []string {
   274  	return append([]string{}, *s...)
   275  }
   276  
   277  // shorterThan reports whether sp is shorter than t.
   278  // We use this to record the shortest import sequence
   279  // that leads to a particular package.
   280  func (sp *importStack) shorterThan(t []string) bool {
   281  	s := *sp
   282  	if len(s) != len(t) {
   283  		return len(s) < len(t)
   284  	}
   285  	// If they are the same length, settle ties using string ordering.
   286  	for i := range s {
   287  		if s[i] != t[i] {
   288  			return s[i] < t[i]
   289  		}
   290  	}
   291  	return false // they are equal
   292  }
   293  
   294  // packageCache is a lookup cache for loadPackage,
   295  // so that if we look up a package multiple times
   296  // we return the same pointer each time.
   297  var packageCache = map[string]*Package{}
   298  
   299  // reloadPackage is like loadPackage but makes sure
   300  // not to use the package cache.
   301  func reloadPackage(arg string, stk *importStack) *Package {
   302  	p := packageCache[arg]
   303  	if p != nil {
   304  		delete(packageCache, p.Dir)
   305  		delete(packageCache, p.ImportPath)
   306  	}
   307  	return loadPackage(arg, stk)
   308  }
   309  
   310  // dirToImportPath returns the pseudo-import path we use for a package
   311  // outside the Go path. It begins with _/ and then contains the full path
   312  // to the directory. If the package lives in c:\home\gopher\my\pkg then
   313  // the pseudo-import path is _/c_/home/gopher/my/pkg.
   314  // Using a pseudo-import path like this makes the ./ imports no longer
   315  // a special case, so that all the code to deal with ordinary imports works
   316  // automatically.
   317  func dirToImportPath(dir string) string {
   318  	return pathpkg.Join("_", strings.Map(makeImportValid, filepath.ToSlash(dir)))
   319  }
   320  
   321  func makeImportValid(r rune) rune {
   322  	// Should match Go spec, compilers, and ../../go/parser/parser.go:/isValidImport.
   323  	const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
   324  	if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
   325  		return '_'
   326  	}
   327  	return r
   328  }
   329  
   330  // Mode flags for loadImport and download (in get.go).
   331  const (
   332  	// useVendor means that loadImport should do vendor expansion
   333  	// (provided the vendoring experiment is enabled).
   334  	// That is, useVendor means that the import path came from
   335  	// a source file and has not been vendor-expanded yet.
   336  	// Every import path should be loaded initially with useVendor,
   337  	// and then the expanded version (with the /vendor/ in it) gets
   338  	// recorded as the canonical import path. At that point, future loads
   339  	// of that package must not pass useVendor, because
   340  	// disallowVendor will reject direct use of paths containing /vendor/.
   341  	useVendor = 1 << iota
   342  
   343  	// getTestDeps is for download (part of "go get") and indicates
   344  	// that test dependencies should be fetched too.
   345  	getTestDeps
   346  )
   347  
   348  // loadImport scans the directory named by path, which must be an import path,
   349  // but possibly a local import path (an absolute file system path or one beginning
   350  // with ./ or ../).  A local relative path is interpreted relative to srcDir.
   351  // It returns a *Package describing the package found in that directory.
   352  func loadImport(path, srcDir string, parent *Package, stk *importStack, importPos []token.Position, mode int) *Package {
   353  	stk.push(path)
   354  	defer stk.pop()
   355  
   356  	// Determine canonical identifier for this package.
   357  	// For a local import the identifier is the pseudo-import path
   358  	// we create from the full directory to the package.
   359  	// Otherwise it is the usual import path.
   360  	// For vendored imports, it is the expanded form.
   361  	importPath := path
   362  	origPath := path
   363  	isLocal := build.IsLocalImport(path)
   364  	if isLocal {
   365  		importPath = dirToImportPath(filepath.Join(srcDir, path))
   366  	} else if mode&useVendor != 0 {
   367  		// We do our own vendor resolution, because we want to
   368  		// find out the key to use in packageCache without the
   369  		// overhead of repeated calls to buildContext.Import.
   370  		// The code is also needed in a few other places anyway.
   371  		path = vendoredImportPath(parent, path)
   372  		importPath = path
   373  	}
   374  
   375  	p := packageCache[importPath]
   376  	if p != nil {
   377  		p = reusePackage(p, stk)
   378  	} else {
   379  		p = new(Package)
   380  		p.local = isLocal
   381  		p.ImportPath = importPath
   382  		packageCache[importPath] = p
   383  
   384  		// Load package.
   385  		// Import always returns bp != nil, even if an error occurs,
   386  		// in order to return partial information.
   387  		//
   388  		// TODO: After Go 1, decide when to pass build.AllowBinary here.
   389  		// See issue 3268 for mistakes to avoid.
   390  		buildMode := build.ImportComment
   391  		if mode&useVendor == 0 || path != origPath {
   392  			// Not vendoring, or we already found the vendored path.
   393  			buildMode |= build.IgnoreVendor
   394  		}
   395  		bp, err := buildContext.Import(path, srcDir, buildMode)
   396  		bp.ImportPath = importPath
   397  		if gobin != "" {
   398  			bp.BinDir = gobin
   399  		}
   400  		if err == nil && !isLocal && bp.ImportComment != "" && bp.ImportComment != path &&
   401  			!strings.Contains(path, "/vendor/") && !strings.HasPrefix(path, "vendor/") {
   402  			err = fmt.Errorf("code in directory %s expects import %q", bp.Dir, bp.ImportComment)
   403  		}
   404  		p.load(stk, bp, err)
   405  		if p.Error != nil && p.Error.Pos == "" {
   406  			p = setErrorPos(p, importPos)
   407  		}
   408  
   409  		if origPath != cleanImport(origPath) {
   410  			p.Error = &PackageError{
   411  				ImportStack: stk.copy(),
   412  				Err:         fmt.Sprintf("non-canonical import path: %q should be %q", origPath, pathpkg.Clean(origPath)),
   413  			}
   414  			p.Incomplete = true
   415  		}
   416  	}
   417  
   418  	// Checked on every import because the rules depend on the code doing the importing.
   419  	if perr := disallowInternal(srcDir, p, stk); perr != p {
   420  		return setErrorPos(perr, importPos)
   421  	}
   422  	if mode&useVendor != 0 {
   423  		if perr := disallowVendor(srcDir, origPath, p, stk); perr != p {
   424  			return setErrorPos(perr, importPos)
   425  		}
   426  	}
   427  
   428  	if p.Name == "main" && parent != nil && parent.Dir != p.Dir {
   429  		perr := *p
   430  		perr.Error = &PackageError{
   431  			ImportStack: stk.copy(),
   432  			Err:         fmt.Sprintf("import %q is a program, not an importable package", path),
   433  		}
   434  		return setErrorPos(&perr, importPos)
   435  	}
   436  
   437  	if p.local && parent != nil && !parent.local {
   438  		perr := *p
   439  		perr.Error = &PackageError{
   440  			ImportStack: stk.copy(),
   441  			Err:         fmt.Sprintf("local import %q in non-local package", path),
   442  		}
   443  		return setErrorPos(&perr, importPos)
   444  	}
   445  
   446  	return p
   447  }
   448  
   449  func setErrorPos(p *Package, importPos []token.Position) *Package {
   450  	if len(importPos) > 0 {
   451  		pos := importPos[0]
   452  		pos.Filename = shortPath(pos.Filename)
   453  		p.Error.Pos = pos.String()
   454  	}
   455  	return p
   456  }
   457  
   458  func cleanImport(path string) string {
   459  	orig := path
   460  	path = pathpkg.Clean(path)
   461  	if strings.HasPrefix(orig, "./") && path != ".." && !strings.HasPrefix(path, "../") {
   462  		path = "./" + path
   463  	}
   464  	return path
   465  }
   466  
   467  var isDirCache = map[string]bool{}
   468  
   469  func isDir(path string) bool {
   470  	result, ok := isDirCache[path]
   471  	if ok {
   472  		return result
   473  	}
   474  
   475  	fi, err := os.Stat(path)
   476  	result = err == nil && fi.IsDir()
   477  	isDirCache[path] = result
   478  	return result
   479  }
   480  
   481  // vendoredImportPath returns the expansion of path when it appears in parent.
   482  // If parent is x/y/z, then path might expand to x/y/z/vendor/path, x/y/vendor/path,
   483  // x/vendor/path, vendor/path, or else stay path if none of those exist.
   484  // vendoredImportPath returns the expanded path or, if no expansion is found, the original.
   485  func vendoredImportPath(parent *Package, path string) (found string) {
   486  	if parent == nil || parent.Root == "" {
   487  		return path
   488  	}
   489  
   490  	dir := filepath.Clean(parent.Dir)
   491  	root := filepath.Join(parent.Root, "src")
   492  	if !hasFilePathPrefix(dir, root) || parent.ImportPath != "command-line-arguments" && filepath.Join(root, parent.ImportPath) != dir {
   493  		// Look for symlinks before reporting error.
   494  		dir = expandPath(dir)
   495  		root = expandPath(root)
   496  	}
   497  
   498  	if !hasFilePathPrefix(dir, root) || len(dir) <= len(root) || dir[len(root)] != filepath.Separator || parent.ImportPath != "command-line-arguments" && !parent.local && filepath.Join(root, parent.ImportPath) != dir {
   499  		fatalf("unexpected directory layout:\n"+
   500  			"	import path: %s\n"+
   501  			"	root: %s\n"+
   502  			"	dir: %s\n"+
   503  			"	expand root: %s\n"+
   504  			"	expand dir: %s\n"+
   505  			"	separator: %s",
   506  			parent.ImportPath,
   507  			filepath.Join(parent.Root, "src"),
   508  			filepath.Clean(parent.Dir),
   509  			root,
   510  			dir,
   511  			string(filepath.Separator))
   512  	}
   513  
   514  	vpath := "vendor/" + path
   515  	for i := len(dir); i >= len(root); i-- {
   516  		if i < len(dir) && dir[i] != filepath.Separator {
   517  			continue
   518  		}
   519  		// Note: checking for the vendor directory before checking
   520  		// for the vendor/path directory helps us hit the
   521  		// isDir cache more often. It also helps us prepare a more useful
   522  		// list of places we looked, to report when an import is not found.
   523  		if !isDir(filepath.Join(dir[:i], "vendor")) {
   524  			continue
   525  		}
   526  		targ := filepath.Join(dir[:i], vpath)
   527  		if isDir(targ) && hasGoFiles(targ) {
   528  			importPath := parent.ImportPath
   529  			if importPath == "command-line-arguments" {
   530  				// If parent.ImportPath is 'command-line-arguments'.
   531  				// set to relative directory to root (also chopped root directory)
   532  				importPath = dir[len(root)+1:]
   533  			}
   534  			// We started with parent's dir c:\gopath\src\foo\bar\baz\quux\xyzzy.
   535  			// We know the import path for parent's dir.
   536  			// We chopped off some number of path elements and
   537  			// added vendor\path to produce c:\gopath\src\foo\bar\baz\vendor\path.
   538  			// Now we want to know the import path for that directory.
   539  			// Construct it by chopping the same number of path elements
   540  			// (actually the same number of bytes) from parent's import path
   541  			// and then append /vendor/path.
   542  			chopped := len(dir) - i
   543  			if chopped == len(importPath)+1 {
   544  				// We walked up from c:\gopath\src\foo\bar
   545  				// and found c:\gopath\src\vendor\path.
   546  				// We chopped \foo\bar (length 8) but the import path is "foo/bar" (length 7).
   547  				// Use "vendor/path" without any prefix.
   548  				return vpath
   549  			}
   550  			return importPath[:len(importPath)-chopped] + "/" + vpath
   551  		}
   552  	}
   553  	return path
   554  }
   555  
   556  // hasGoFiles reports whether dir contains any files with names ending in .go.
   557  // For a vendor check we must exclude directories that contain no .go files.
   558  // Otherwise it is not possible to vendor just a/b/c and still import the
   559  // non-vendored a/b. See golang.org/issue/13832.
   560  func hasGoFiles(dir string) bool {
   561  	fis, _ := ioutil.ReadDir(dir)
   562  	for _, fi := range fis {
   563  		if !fi.IsDir() && strings.HasSuffix(fi.Name(), ".go") {
   564  			return true
   565  		}
   566  	}
   567  	return false
   568  }
   569  
   570  // reusePackage reuses package p to satisfy the import at the top
   571  // of the import stack stk. If this use causes an import loop,
   572  // reusePackage updates p's error information to record the loop.
   573  func reusePackage(p *Package, stk *importStack) *Package {
   574  	// We use p.imports==nil to detect a package that
   575  	// is in the midst of its own loadPackage call
   576  	// (all the recursion below happens before p.imports gets set).
   577  	if p.imports == nil {
   578  		if p.Error == nil {
   579  			p.Error = &PackageError{
   580  				ImportStack:   stk.copy(),
   581  				Err:           "import cycle not allowed",
   582  				isImportCycle: true,
   583  			}
   584  		}
   585  		p.Incomplete = true
   586  	}
   587  	// Don't rewrite the import stack in the error if we have an import cycle.
   588  	// If we do, we'll lose the path that describes the cycle.
   589  	if p.Error != nil && !p.Error.isImportCycle && stk.shorterThan(p.Error.ImportStack) {
   590  		p.Error.ImportStack = stk.copy()
   591  	}
   592  	return p
   593  }
   594  
   595  // disallowInternal checks that srcDir is allowed to import p.
   596  // If the import is allowed, disallowInternal returns the original package p.
   597  // If not, it returns a new package containing just an appropriate error.
   598  func disallowInternal(srcDir string, p *Package, stk *importStack) *Package {
   599  	// golang.org/s/go14internal:
   600  	// An import of a path containing the element “internal”
   601  	// is disallowed if the importing code is outside the tree
   602  	// rooted at the parent of the “internal” directory.
   603  
   604  	// There was an error loading the package; stop here.
   605  	if p.Error != nil {
   606  		return p
   607  	}
   608  
   609  	// The generated 'testmain' package is allowed to access testing/internal/...,
   610  	// as if it were generated into the testing directory tree
   611  	// (it's actually in a temporary directory outside any Go tree).
   612  	// This cleans up a former kludge in passing functionality to the testing package.
   613  	if strings.HasPrefix(p.ImportPath, "testing/internal") && len(*stk) >= 2 && (*stk)[len(*stk)-2] == "testmain" {
   614  		return p
   615  	}
   616  
   617  	// We can't check standard packages with gccgo.
   618  	if buildContext.Compiler == "gccgo" && p.Standard {
   619  		return p
   620  	}
   621  
   622  	// The stack includes p.ImportPath.
   623  	// If that's the only thing on the stack, we started
   624  	// with a name given on the command line, not an
   625  	// import. Anything listed on the command line is fine.
   626  	if len(*stk) == 1 {
   627  		return p
   628  	}
   629  
   630  	// Check for "internal" element: three cases depending on begin of string and/or end of string.
   631  	i, ok := findInternal(p.ImportPath)
   632  	if !ok {
   633  		return p
   634  	}
   635  
   636  	// Internal is present.
   637  	// Map import path back to directory corresponding to parent of internal.
   638  	if i > 0 {
   639  		i-- // rewind over slash in ".../internal"
   640  	}
   641  	parent := p.Dir[:i+len(p.Dir)-len(p.ImportPath)]
   642  	if hasFilePathPrefix(filepath.Clean(srcDir), filepath.Clean(parent)) {
   643  		return p
   644  	}
   645  
   646  	// Look for symlinks before reporting error.
   647  	srcDir = expandPath(srcDir)
   648  	parent = expandPath(parent)
   649  	if hasFilePathPrefix(filepath.Clean(srcDir), filepath.Clean(parent)) {
   650  		return p
   651  	}
   652  
   653  	// Internal is present, and srcDir is outside parent's tree. Not allowed.
   654  	perr := *p
   655  	perr.Error = &PackageError{
   656  		ImportStack: stk.copy(),
   657  		Err:         "use of internal package not allowed",
   658  	}
   659  	perr.Incomplete = true
   660  	return &perr
   661  }
   662  
   663  // findInternal looks for the final "internal" path element in the given import path.
   664  // If there isn't one, findInternal returns ok=false.
   665  // Otherwise, findInternal returns ok=true and the index of the "internal".
   666  func findInternal(path string) (index int, ok bool) {
   667  	// Three cases, depending on internal at start/end of string or not.
   668  	// The order matters: we must return the index of the final element,
   669  	// because the final one produces the most restrictive requirement
   670  	// on the importer.
   671  	switch {
   672  	case strings.HasSuffix(path, "/internal"):
   673  		return len(path) - len("internal"), true
   674  	case strings.Contains(path, "/internal/"):
   675  		return strings.LastIndex(path, "/internal/") + 1, true
   676  	case path == "internal", strings.HasPrefix(path, "internal/"):
   677  		return 0, true
   678  	}
   679  	return 0, false
   680  }
   681  
   682  // disallowVendor checks that srcDir is allowed to import p as path.
   683  // If the import is allowed, disallowVendor returns the original package p.
   684  // If not, it returns a new package containing just an appropriate error.
   685  func disallowVendor(srcDir, path string, p *Package, stk *importStack) *Package {
   686  	// The stack includes p.ImportPath.
   687  	// If that's the only thing on the stack, we started
   688  	// with a name given on the command line, not an
   689  	// import. Anything listed on the command line is fine.
   690  	if len(*stk) == 1 {
   691  		return p
   692  	}
   693  
   694  	if perr := disallowVendorVisibility(srcDir, p, stk); perr != p {
   695  		return perr
   696  	}
   697  
   698  	// Paths like x/vendor/y must be imported as y, never as x/vendor/y.
   699  	if i, ok := findVendor(path); ok {
   700  		perr := *p
   701  		perr.Error = &PackageError{
   702  			ImportStack: stk.copy(),
   703  			Err:         "must be imported as " + path[i+len("vendor/"):],
   704  		}
   705  		perr.Incomplete = true
   706  		return &perr
   707  	}
   708  
   709  	return p
   710  }
   711  
   712  // disallowVendorVisibility checks that srcDir is allowed to import p.
   713  // The rules are the same as for /internal/ except that a path ending in /vendor
   714  // is not subject to the rules, only subdirectories of vendor.
   715  // This allows people to have packages and commands named vendor,
   716  // for maximal compatibility with existing source trees.
   717  func disallowVendorVisibility(srcDir string, p *Package, stk *importStack) *Package {
   718  	// The stack includes p.ImportPath.
   719  	// If that's the only thing on the stack, we started
   720  	// with a name given on the command line, not an
   721  	// import. Anything listed on the command line is fine.
   722  	if len(*stk) == 1 {
   723  		return p
   724  	}
   725  
   726  	// Check for "vendor" element.
   727  	i, ok := findVendor(p.ImportPath)
   728  	if !ok {
   729  		return p
   730  	}
   731  
   732  	// Vendor is present.
   733  	// Map import path back to directory corresponding to parent of vendor.
   734  	if i > 0 {
   735  		i-- // rewind over slash in ".../vendor"
   736  	}
   737  	truncateTo := i + len(p.Dir) - len(p.ImportPath)
   738  	if truncateTo < 0 || len(p.Dir) < truncateTo {
   739  		return p
   740  	}
   741  	parent := p.Dir[:truncateTo]
   742  	if hasFilePathPrefix(filepath.Clean(srcDir), filepath.Clean(parent)) {
   743  		return p
   744  	}
   745  
   746  	// Look for symlinks before reporting error.
   747  	srcDir = expandPath(srcDir)
   748  	parent = expandPath(parent)
   749  	if hasFilePathPrefix(filepath.Clean(srcDir), filepath.Clean(parent)) {
   750  		return p
   751  	}
   752  
   753  	// Vendor is present, and srcDir is outside parent's tree. Not allowed.
   754  	perr := *p
   755  	perr.Error = &PackageError{
   756  		ImportStack: stk.copy(),
   757  		Err:         "use of vendored package not allowed",
   758  	}
   759  	perr.Incomplete = true
   760  	return &perr
   761  }
   762  
   763  // findVendor looks for the last non-terminating "vendor" path element in the given import path.
   764  // If there isn't one, findVendor returns ok=false.
   765  // Otherwise, findVendor returns ok=true and the index of the "vendor".
   766  //
   767  // Note that terminating "vendor" elements don't count: "x/vendor" is its own package,
   768  // not the vendored copy of an import "" (the empty import path).
   769  // This will allow people to have packages or commands named vendor.
   770  // This may help reduce breakage, or it may just be confusing. We'll see.
   771  func findVendor(path string) (index int, ok bool) {
   772  	// Two cases, depending on internal at start of string or not.
   773  	// The order matters: we must return the index of the final element,
   774  	// because the final one is where the effective import path starts.
   775  	switch {
   776  	case strings.Contains(path, "/vendor/"):
   777  		return strings.LastIndex(path, "/vendor/") + 1, true
   778  	case strings.HasPrefix(path, "vendor/"):
   779  		return 0, true
   780  	}
   781  	return 0, false
   782  }
   783  
   784  type targetDir int
   785  
   786  const (
   787  	toRoot    targetDir = iota // to bin dir inside package root (default)
   788  	toTool                     // GOROOT/pkg/tool
   789  	stalePath                  // the old import path; fail to build
   790  )
   791  
   792  // goTools is a map of Go program import path to install target directory.
   793  var goTools = map[string]targetDir{
   794  	"cmd/addr2line": toTool,
   795  	"cmd/api":       toTool,
   796  	"cmd/asm":       toTool,
   797  	"cmd/compile":   toTool,
   798  	"cmd/cgo":       toTool,
   799  	"cmd/cover":     toTool,
   800  	"cmd/dist":      toTool,
   801  	"cmd/doc":       toTool,
   802  	"cmd/fix":       toTool,
   803  	"cmd/link":      toTool,
   804  	"cmd/newlink":   toTool,
   805  	"cmd/nm":        toTool,
   806  	"cmd/objdump":   toTool,
   807  	"cmd/pack":      toTool,
   808  	"cmd/pprof":     toTool,
   809  	"cmd/trace":     toTool,
   810  	"cmd/vet":       toTool,
   811  	"code.google.com/p/go.tools/cmd/cover": stalePath,
   812  	"code.google.com/p/go.tools/cmd/godoc": stalePath,
   813  	"code.google.com/p/go.tools/cmd/vet":   stalePath,
   814  }
   815  
   816  // expandScanner expands a scanner.List error into all the errors in the list.
   817  // The default Error method only shows the first error.
   818  func expandScanner(err error) error {
   819  	// Look for parser errors.
   820  	if err, ok := err.(scanner.ErrorList); ok {
   821  		// Prepare error with \n before each message.
   822  		// When printed in something like context: %v
   823  		// this will put the leading file positions each on
   824  		// its own line. It will also show all the errors
   825  		// instead of just the first, as err.Error does.
   826  		var buf bytes.Buffer
   827  		for _, e := range err {
   828  			e.Pos.Filename = shortPath(e.Pos.Filename)
   829  			buf.WriteString("\n")
   830  			buf.WriteString(e.Error())
   831  		}
   832  		return errors.New(buf.String())
   833  	}
   834  	return err
   835  }
   836  
   837  var raceExclude = map[string]bool{
   838  	"runtime/race": true,
   839  	"runtime/msan": true,
   840  	"runtime/cgo":  true,
   841  	"cmd/cgo":      true,
   842  	"syscall":      true,
   843  	"errors":       true,
   844  }
   845  
   846  var cgoExclude = map[string]bool{
   847  	"runtime/cgo": true,
   848  }
   849  
   850  var cgoSyscallExclude = map[string]bool{
   851  	"runtime/cgo":  true,
   852  	"runtime/race": true,
   853  	"runtime/msan": true,
   854  }
   855  
   856  // load populates p using information from bp, err, which should
   857  // be the result of calling build.Context.Import.
   858  func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package {
   859  	p.copyBuild(bp)
   860  
   861  	// The localPrefix is the path we interpret ./ imports relative to.
   862  	// Synthesized main packages sometimes override this.
   863  	p.localPrefix = dirToImportPath(p.Dir)
   864  
   865  	if err != nil {
   866  		p.Incomplete = true
   867  		err = expandScanner(err)
   868  		p.Error = &PackageError{
   869  			ImportStack: stk.copy(),
   870  			Err:         err.Error(),
   871  		}
   872  		return p
   873  	}
   874  
   875  	useBindir := p.Name == "main"
   876  	if !p.Standard {
   877  		switch buildBuildmode {
   878  		case "c-archive", "c-shared", "plugin":
   879  			useBindir = false
   880  		}
   881  	}
   882  
   883  	if useBindir {
   884  		// Report an error when the old code.google.com/p/go.tools paths are used.
   885  		if goTools[p.ImportPath] == stalePath {
   886  			newPath := strings.Replace(p.ImportPath, "code.google.com/p/go.", "golang.org/x/", 1)
   887  			e := fmt.Sprintf("the %v command has moved; use %v instead.", p.ImportPath, newPath)
   888  			p.Error = &PackageError{Err: e}
   889  			return p
   890  		}
   891  		_, elem := filepath.Split(p.Dir)
   892  		full := buildContext.GOOS + "_" + buildContext.GOARCH + "/" + elem
   893  		if buildContext.GOOS != toolGOOS || buildContext.GOARCH != toolGOARCH {
   894  			// Install cross-compiled binaries to subdirectories of bin.
   895  			elem = full
   896  		}
   897  		if p.build.BinDir != "" {
   898  			// Install to GOBIN or bin of GOPATH entry.
   899  			p.target = filepath.Join(p.build.BinDir, elem)
   900  			if !p.Goroot && strings.Contains(elem, "/") && gobin != "" {
   901  				// Do not create $GOBIN/goos_goarch/elem.
   902  				p.target = ""
   903  				p.gobinSubdir = true
   904  			}
   905  		}
   906  		if goTools[p.ImportPath] == toTool {
   907  			// This is for 'go tool'.
   908  			// Override all the usual logic and force it into the tool directory.
   909  			p.target = filepath.Join(gorootPkg, "tool", full)
   910  		}
   911  		if p.target != "" && buildContext.GOOS == "windows" {
   912  			p.target += ".exe"
   913  		}
   914  	} else if p.local {
   915  		// Local import turned into absolute path.
   916  		// No permanent install target.
   917  		p.target = ""
   918  	} else {
   919  		p.target = p.build.PkgObj
   920  		if buildLinkshared {
   921  			shlibnamefile := p.target[:len(p.target)-2] + ".shlibname"
   922  			shlib, err := ioutil.ReadFile(shlibnamefile)
   923  			if err == nil {
   924  				libname := strings.TrimSpace(string(shlib))
   925  				if buildContext.Compiler == "gccgo" {
   926  					p.Shlib = filepath.Join(p.build.PkgTargetRoot, "shlibs", libname)
   927  				} else {
   928  					p.Shlib = filepath.Join(p.build.PkgTargetRoot, libname)
   929  
   930  				}
   931  			} else if !os.IsNotExist(err) {
   932  				fatalf("unexpected error reading %s: %v", shlibnamefile, err)
   933  			}
   934  		}
   935  	}
   936  
   937  	importPaths := p.Imports
   938  	// Packages that use cgo import runtime/cgo implicitly.
   939  	// Packages that use cgo also import syscall implicitly,
   940  	// to wrap errno.
   941  	// Exclude certain packages to avoid circular dependencies.
   942  	if len(p.CgoFiles) > 0 && (!p.Standard || !cgoExclude[p.ImportPath]) {
   943  		importPaths = append(importPaths, "runtime/cgo")
   944  	}
   945  	if len(p.CgoFiles) > 0 && (!p.Standard || !cgoSyscallExclude[p.ImportPath]) {
   946  		importPaths = append(importPaths, "syscall")
   947  	}
   948  
   949  	if buildContext.CgoEnabled && p.Name == "main" && !p.Goroot {
   950  		// Currently build modes c-shared, pie (on systems that do not
   951  		// support PIE with internal linking mode), plugin, and
   952  		// -linkshared force external linking mode, as of course does
   953  		// -ldflags=-linkmode=external. External linking mode forces
   954  		// an import of runtime/cgo.
   955  		pieCgo := buildBuildmode == "pie" && (buildContext.GOOS != "linux" || buildContext.GOARCH != "amd64")
   956  		linkmodeExternal := false
   957  		for i, a := range buildLdflags {
   958  			if a == "-linkmode=external" {
   959  				linkmodeExternal = true
   960  			}
   961  			if a == "-linkmode" && i+1 < len(buildLdflags) && buildLdflags[i+1] == "external" {
   962  				linkmodeExternal = true
   963  			}
   964  		}
   965  		if buildBuildmode == "c-shared" || buildBuildmode == "plugin" || pieCgo || buildLinkshared || linkmodeExternal {
   966  			importPaths = append(importPaths, "runtime/cgo")
   967  		}
   968  	}
   969  
   970  	// Everything depends on runtime, except runtime, its internal
   971  	// subpackages, and unsafe.
   972  	if !p.Standard || (p.ImportPath != "runtime" && !strings.HasPrefix(p.ImportPath, "runtime/internal/") && p.ImportPath != "unsafe") {
   973  		importPaths = append(importPaths, "runtime")
   974  		// When race detection enabled everything depends on runtime/race.
   975  		// Exclude certain packages to avoid circular dependencies.
   976  		if buildRace && (!p.Standard || !raceExclude[p.ImportPath]) {
   977  			importPaths = append(importPaths, "runtime/race")
   978  		}
   979  		// MSan uses runtime/msan.
   980  		if buildMSan && (!p.Standard || !raceExclude[p.ImportPath]) {
   981  			importPaths = append(importPaths, "runtime/msan")
   982  		}
   983  		// On ARM with GOARM=5, everything depends on math for the link.
   984  		if p.Name == "main" && goarch == "arm" {
   985  			importPaths = append(importPaths, "math")
   986  		}
   987  	}
   988  
   989  	// Runtime and its internal packages depend on runtime/internal/sys,
   990  	// so that they pick up the generated zversion.go file.
   991  	// This can be an issue particularly for runtime/internal/atomic;
   992  	// see issue 13655.
   993  	if p.Standard && (p.ImportPath == "runtime" || strings.HasPrefix(p.ImportPath, "runtime/internal/")) && p.ImportPath != "runtime/internal/sys" {
   994  		importPaths = append(importPaths, "runtime/internal/sys")
   995  	}
   996  
   997  	// Build list of full paths to all Go files in the package,
   998  	// for use by commands like go fmt.
   999  	p.gofiles = stringList(p.GoFiles, p.CgoFiles, p.TestGoFiles, p.XTestGoFiles)
  1000  	for i := range p.gofiles {
  1001  		p.gofiles[i] = filepath.Join(p.Dir, p.gofiles[i])
  1002  	}
  1003  	sort.Strings(p.gofiles)
  1004  
  1005  	p.sfiles = stringList(p.SFiles)
  1006  	for i := range p.sfiles {
  1007  		p.sfiles[i] = filepath.Join(p.Dir, p.sfiles[i])
  1008  	}
  1009  	sort.Strings(p.sfiles)
  1010  
  1011  	p.allgofiles = stringList(p.IgnoredGoFiles)
  1012  	for i := range p.allgofiles {
  1013  		p.allgofiles[i] = filepath.Join(p.Dir, p.allgofiles[i])
  1014  	}
  1015  	p.allgofiles = append(p.allgofiles, p.gofiles...)
  1016  	sort.Strings(p.allgofiles)
  1017  
  1018  	// Check for case-insensitive collision of input files.
  1019  	// To avoid problems on case-insensitive files, we reject any package
  1020  	// where two different input files have equal names under a case-insensitive
  1021  	// comparison.
  1022  	inputs := p.allFiles()
  1023  	f1, f2 := foldDup(inputs)
  1024  	if f1 != "" {
  1025  		p.Error = &PackageError{
  1026  			ImportStack: stk.copy(),
  1027  			Err:         fmt.Sprintf("case-insensitive file name collision: %q and %q", f1, f2),
  1028  		}
  1029  		return p
  1030  	}
  1031  
  1032  	// If first letter of input file is ASCII, it must be alphanumeric.
  1033  	// This avoids files turning into flags when invoking commands,
  1034  	// and other problems we haven't thought of yet.
  1035  	// Also, _cgo_ files must be generated by us, not supplied.
  1036  	// They are allowed to have //go:cgo_ldflag directives.
  1037  	// The directory scan ignores files beginning with _,
  1038  	// so we shouldn't see any _cgo_ files anyway, but just be safe.
  1039  	for _, file := range inputs {
  1040  		if !SafeArg(file) || strings.HasPrefix(file, "_cgo_") {
  1041  			p.Error = &PackageError{
  1042  				ImportStack: stk.copy(),
  1043  				Err:         fmt.Sprintf("invalid input file name %q", file),
  1044  			}
  1045  			return p
  1046  		}
  1047  	}
  1048  	if name := pathpkg.Base(p.ImportPath); !SafeArg(name) {
  1049  		p.Error = &PackageError{
  1050  			ImportStack: stk.copy(),
  1051  			Err:         fmt.Sprintf("invalid input directory name %q", name),
  1052  		}
  1053  		return p
  1054  	}
  1055  	if !SafeArg(p.ImportPath) {
  1056  		p.Error = &PackageError{
  1057  			ImportStack: stk.copy(),
  1058  			Err:         fmt.Sprintf("invalid import path %q", p.ImportPath),
  1059  		}
  1060  		return p
  1061  	}
  1062  
  1063  	// Build list of imported packages and full dependency list.
  1064  	imports := make([]*Package, 0, len(p.Imports))
  1065  	deps := make(map[string]*Package)
  1066  	save := func(path string, p1 *Package) {
  1067  		// The same import path could produce an error or not,
  1068  		// depending on what tries to import it.
  1069  		// Prefer to record entries with errors, so we can report them.
  1070  		p0 := deps[path]
  1071  		if p0 == nil || p1.Error != nil && (p0.Error == nil || len(p0.Error.ImportStack) > len(p1.Error.ImportStack)) {
  1072  			deps[path] = p1
  1073  		}
  1074  	}
  1075  
  1076  	for i, path := range importPaths {
  1077  		if path == "C" {
  1078  			continue
  1079  		}
  1080  		p1 := loadImport(path, p.Dir, p, stk, p.build.ImportPos[path], useVendor)
  1081  		if p.Standard && p.Error == nil && !p1.Standard && p1.Error == nil {
  1082  			p.Error = &PackageError{
  1083  				ImportStack: stk.copy(),
  1084  				Err:         fmt.Sprintf("non-standard import %q in standard package %q", path, p.ImportPath),
  1085  			}
  1086  			pos := p.build.ImportPos[path]
  1087  			if len(pos) > 0 {
  1088  				p.Error.Pos = pos[0].String()
  1089  			}
  1090  		}
  1091  
  1092  		path = p1.ImportPath
  1093  		importPaths[i] = path
  1094  		if i < len(p.Imports) {
  1095  			p.Imports[i] = path
  1096  		}
  1097  
  1098  		save(path, p1)
  1099  		imports = append(imports, p1)
  1100  		for _, dep := range p1.deps {
  1101  			save(dep.ImportPath, dep)
  1102  		}
  1103  		if p1.Incomplete {
  1104  			p.Incomplete = true
  1105  		}
  1106  	}
  1107  	p.imports = imports
  1108  
  1109  	p.Deps = make([]string, 0, len(deps))
  1110  	for dep := range deps {
  1111  		p.Deps = append(p.Deps, dep)
  1112  	}
  1113  	sort.Strings(p.Deps)
  1114  	for _, dep := range p.Deps {
  1115  		p1 := deps[dep]
  1116  		if p1 == nil {
  1117  			panic("impossible: missing entry in package cache for " + dep + " imported by " + p.ImportPath)
  1118  		}
  1119  		p.deps = append(p.deps, p1)
  1120  		if p1.Error != nil {
  1121  			p.DepsErrors = append(p.DepsErrors, p1.Error)
  1122  		}
  1123  	}
  1124  
  1125  	// unsafe is a fake package.
  1126  	if p.Standard && (p.ImportPath == "unsafe" || buildContext.Compiler == "gccgo") {
  1127  		p.target = ""
  1128  	}
  1129  	p.Target = p.target
  1130  
  1131  	// If cgo is not enabled, ignore cgo supporting sources
  1132  	// just as we ignore go files containing import "C".
  1133  	if !buildContext.CgoEnabled {
  1134  		p.CFiles = nil
  1135  		p.CXXFiles = nil
  1136  		p.MFiles = nil
  1137  		p.SwigFiles = nil
  1138  		p.SwigCXXFiles = nil
  1139  		// Note that SFiles are okay (they go to the Go assembler)
  1140  		// and HFiles are okay (they might be used by the SFiles).
  1141  		// Also Sysofiles are okay (they might not contain object
  1142  		// code; see issue #16050).
  1143  	}
  1144  
  1145  	// The gc toolchain only permits C source files with cgo.
  1146  	if len(p.CFiles) > 0 && !p.usesCgo() && !p.usesSwig() && buildContext.Compiler == "gc" {
  1147  		p.Error = &PackageError{
  1148  			ImportStack: stk.copy(),
  1149  			Err:         fmt.Sprintf("C source files not allowed when not using cgo or SWIG: %s", strings.Join(p.CFiles, " ")),
  1150  		}
  1151  		return p
  1152  	}
  1153  
  1154  	// In the absence of errors lower in the dependency tree,
  1155  	// check for case-insensitive collisions of import paths.
  1156  	if len(p.DepsErrors) == 0 {
  1157  		dep1, dep2 := foldDup(p.Deps)
  1158  		if dep1 != "" {
  1159  			p.Error = &PackageError{
  1160  				ImportStack: stk.copy(),
  1161  				Err:         fmt.Sprintf("case-insensitive import collision: %q and %q", dep1, dep2),
  1162  			}
  1163  			return p
  1164  		}
  1165  	}
  1166  
  1167  	if p.BinaryOnly {
  1168  		// For binary-only package, use build ID from supplied package binary.
  1169  		buildID, err := readBuildID(p)
  1170  		if err == nil {
  1171  			p.buildID = buildID
  1172  		}
  1173  	} else {
  1174  		computeBuildID(p)
  1175  	}
  1176  	return p
  1177  }
  1178  
  1179  // SafeArg reports whether arg is a "safe" command-line argument,
  1180  // meaning that when it appears in a command-line, it probably
  1181  // doesn't have some special meaning other than its own name.
  1182  // Obviously args beginning with - are not safe (they look like flags).
  1183  // Less obviously, args beginning with @ are not safe (they look like
  1184  // GNU binutils flagfile specifiers, sometimes called "response files").
  1185  // To be conservative, we reject almost any arg beginning with non-alphanumeric ASCII.
  1186  // We accept leading . _ and / as likely in file system paths.
  1187  func SafeArg(name string) bool {
  1188  	if name == "" {
  1189  		return false
  1190  	}
  1191  	c := name[0]
  1192  	return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || c == '.' || c == '_' || c == '/' || c >= utf8.RuneSelf
  1193  }
  1194  
  1195  // usesSwig reports whether the package needs to run SWIG.
  1196  func (p *Package) usesSwig() bool {
  1197  	return len(p.SwigFiles) > 0 || len(p.SwigCXXFiles) > 0
  1198  }
  1199  
  1200  // usesCgo reports whether the package needs to run cgo
  1201  func (p *Package) usesCgo() bool {
  1202  	return len(p.CgoFiles) > 0
  1203  }
  1204  
  1205  // packageList returns the list of packages in the dag rooted at roots
  1206  // as visited in a depth-first post-order traversal.
  1207  func packageList(roots []*Package) []*Package {
  1208  	seen := map[*Package]bool{}
  1209  	all := []*Package{}
  1210  	var walk func(*Package)
  1211  	walk = func(p *Package) {
  1212  		if seen[p] {
  1213  			return
  1214  		}
  1215  		seen[p] = true
  1216  		for _, p1 := range p.imports {
  1217  			walk(p1)
  1218  		}
  1219  		all = append(all, p)
  1220  	}
  1221  	for _, root := range roots {
  1222  		walk(root)
  1223  	}
  1224  	return all
  1225  }
  1226  
  1227  // computeStale computes the Stale flag in the package dag that starts
  1228  // at the named pkgs (command-line arguments).
  1229  func computeStale(pkgs ...*Package) {
  1230  	for _, p := range packageList(pkgs) {
  1231  		p.Stale, p.StaleReason = isStale(p)
  1232  	}
  1233  }
  1234  
  1235  // The runtime version string takes one of two forms:
  1236  // "go1.X[.Y]" for Go releases, and "devel +hash" at tip.
  1237  // Determine whether we are in a released copy by
  1238  // inspecting the version.
  1239  var isGoRelease = strings.HasPrefix(runtime.Version(), "go1")
  1240  
  1241  // isStale and computeBuildID
  1242  //
  1243  // Theory of Operation
  1244  //
  1245  // There is an installed copy of the package (or binary).
  1246  // Can we reuse the installed copy, or do we need to build a new one?
  1247  //
  1248  // We can use the installed copy if it matches what we'd get
  1249  // by building a new one. The hard part is predicting that without
  1250  // actually running a build.
  1251  //
  1252  // To start, we must know the set of inputs to the build process that can
  1253  // affect the generated output. At a minimum, that includes the source
  1254  // files for the package and also any compiled packages imported by those
  1255  // source files. The *Package has these, and we use them. One might also
  1256  // argue for including in the input set: the build tags, whether the race
  1257  // detector is in use, the target operating system and architecture, the
  1258  // compiler and linker binaries being used, the additional flags being
  1259  // passed to those, the cgo binary being used, the additional flags cgo
  1260  // passes to the host C compiler, the host C compiler being used, the set
  1261  // of host C include files and installed C libraries, and so on.
  1262  // We include some but not all of this information.
  1263  //
  1264  // Once we have decided on a set of inputs, we must next decide how to
  1265  // tell whether the content of that set has changed since the last build
  1266  // of p. If there have been no changes, then we assume a new build would
  1267  // produce the same result and reuse the installed package or binary.
  1268  // But if there have been changes, then we assume a new build might not
  1269  // produce the same result, so we rebuild.
  1270  //
  1271  // There are two common ways to decide whether the content of the set has
  1272  // changed: modification times and content hashes. We use a mixture of both.
  1273  //
  1274  // The use of modification times (mtimes) was pioneered by make:
  1275  // assuming that a file's mtime is an accurate record of when that file was last written,
  1276  // and assuming that the modification time of an installed package or
  1277  // binary is the time that it was built, if the mtimes of the inputs
  1278  // predate the mtime of the installed object, then the build of that
  1279  // object saw those versions of the files, and therefore a rebuild using
  1280  // those same versions would produce the same object. In contrast, if any
  1281  // mtime of an input is newer than the mtime of the installed object, a
  1282  // change has occurred since the build, and the build should be redone.
  1283  //
  1284  // Modification times are attractive because the logic is easy to
  1285  // understand and the file system maintains the mtimes automatically
  1286  // (less work for us). Unfortunately, there are a variety of ways in
  1287  // which the mtime approach fails to detect a change and reuses a stale
  1288  // object file incorrectly. (Making the opposite mistake, rebuilding
  1289  // unnecessarily, is only a performance problem and not a correctness
  1290  // problem, so we ignore that one.)
  1291  //
  1292  // As a warmup, one problem is that to be perfectly precise, we need to
  1293  // compare the input mtimes against the time at the beginning of the
  1294  // build, but the object file time is the time at the end of the build.
  1295  // If an input file changes after being read but before the object is
  1296  // written, the next build will see an object newer than the input and
  1297  // will incorrectly decide that the object is up to date. We make no
  1298  // attempt to detect or solve this problem.
  1299  //
  1300  // Another problem is that due to file system imprecision, an input and
  1301  // output that are actually ordered in time have the same mtime.
  1302  // This typically happens on file systems with 1-second (or, worse,
  1303  // 2-second) mtime granularity and with automated scripts that write an
  1304  // input and then immediately run a build, or vice versa. If an input and
  1305  // an output have the same mtime, the conservative behavior is to treat
  1306  // the output as out-of-date and rebuild. This can cause one or more
  1307  // spurious rebuilds, but only for 1 second, until the object finally has
  1308  // an mtime later than the input.
  1309  //
  1310  // Another problem is that binary distributions often set the mtime on
  1311  // all files to the same time. If the distribution includes both inputs
  1312  // and cached build outputs, the conservative solution to the previous
  1313  // problem will cause unnecessary rebuilds. Worse, in such a binary
  1314  // distribution, those rebuilds might not even have permission to update
  1315  // the cached build output. To avoid these write errors, if an input and
  1316  // output have the same mtime, we assume the output is up-to-date.
  1317  // This is the opposite of what the previous problem would have us do,
  1318  // but binary distributions are more common than instances of the
  1319  // previous problem.
  1320  //
  1321  // A variant of the last problem is that some binary distributions do not
  1322  // set the mtime on all files to the same time. Instead they let the file
  1323  // system record mtimes as the distribution is unpacked. If the outputs
  1324  // are unpacked before the inputs, they'll be older and a build will try
  1325  // to rebuild them. That rebuild might hit the same write errors as in
  1326  // the last scenario. We don't make any attempt to solve this, and we
  1327  // haven't had many reports of it. Perhaps the only time this happens is
  1328  // when people manually unpack the distribution, and most of the time
  1329  // that's done as the same user who will be using it, so an initial
  1330  // rebuild on first use succeeds quietly.
  1331  //
  1332  // More generally, people and programs change mtimes on files. The last
  1333  // few problems were specific examples of this, but it's a general problem.
  1334  // For example, instead of a binary distribution, copying a home
  1335  // directory from one directory or machine to another might copy files
  1336  // but not preserve mtimes. If the inputs are new than the outputs on the
  1337  // first machine but copied first, they end up older than the outputs on
  1338  // the second machine.
  1339  //
  1340  // Because many other build systems have the same sensitivity to mtimes,
  1341  // most programs manipulating source code take pains not to break the
  1342  // mtime assumptions. For example, Git does not set the mtime of files
  1343  // during a checkout operation, even when checking out an old version of
  1344  // the code. This decision was made specifically to work well with
  1345  // mtime-based build systems.
  1346  //
  1347  // The killer problem, though, for mtime-based build systems is that the
  1348  // build only has access to the mtimes of the inputs that still exist.
  1349  // If it is possible to remove an input without changing any other inputs,
  1350  // a later build will think the object is up-to-date when it is not.
  1351  // This happens for Go because a package is made up of all source
  1352  // files in a directory. If a source file is removed, there is no newer
  1353  // mtime available recording that fact. The mtime on the directory could
  1354  // be used, but it also changes when unrelated files are added to or
  1355  // removed from the directory, so including the directory mtime would
  1356  // cause unnecessary rebuilds, possibly many. It would also exacerbate
  1357  // the problems mentioned earlier, since even programs that are careful
  1358  // to maintain mtimes on files rarely maintain mtimes on directories.
  1359  //
  1360  // A variant of the last problem is when the inputs change for other
  1361  // reasons. For example, Go 1.4 and Go 1.5 both install $GOPATH/src/mypkg
  1362  // into the same target, $GOPATH/pkg/$GOOS_$GOARCH/mypkg.a.
  1363  // If Go 1.4 has built mypkg into mypkg.a, a build using Go 1.5 must
  1364  // rebuild mypkg.a, but from mtimes alone mypkg.a looks up-to-date.
  1365  // If Go 1.5 has just been installed, perhaps the compiler will have a
  1366  // newer mtime; since the compiler is considered an input, that would
  1367  // trigger a rebuild. But only once, and only the last Go 1.4 build of
  1368  // mypkg.a happened before Go 1.5 was installed. If a user has the two
  1369  // versions installed in different locations and flips back and forth,
  1370  // mtimes alone cannot tell what to do. Changing the toolchain is
  1371  // changing the set of inputs, without affecting any mtimes.
  1372  //
  1373  // To detect the set of inputs changing, we turn away from mtimes and to
  1374  // an explicit data comparison. Specifically, we build a list of the
  1375  // inputs to the build, compute its SHA1 hash, and record that as the
  1376  // ``build ID'' in the generated object. At the next build, we can
  1377  // recompute the build ID and compare it to the one in the generated
  1378  // object. If they differ, the list of inputs has changed, so the object
  1379  // is out of date and must be rebuilt.
  1380  //
  1381  // Because this build ID is computed before the build begins, the
  1382  // comparison does not have the race that mtime comparison does.
  1383  //
  1384  // Making the build sensitive to changes in other state is
  1385  // straightforward: include the state in the build ID hash, and if it
  1386  // changes, so does the build ID, triggering a rebuild.
  1387  //
  1388  // To detect changes in toolchain, we include the toolchain version in
  1389  // the build ID hash for package runtime, and then we include the build
  1390  // IDs of all imported packages in the build ID for p.
  1391  //
  1392  // It is natural to think about including build tags in the build ID, but
  1393  // the naive approach of just dumping the tags into the hash would cause
  1394  // spurious rebuilds. For example, 'go install' and 'go install -tags neverusedtag'
  1395  // produce the same binaries (assuming neverusedtag is never used).
  1396  // A more precise approach would be to include only tags that have an
  1397  // effect on the build. But the effect of a tag on the build is to
  1398  // include or exclude a file from the compilation, and that file list is
  1399  // already in the build ID hash. So the build ID is already tag-sensitive
  1400  // in a perfectly precise way. So we do NOT explicitly add build tags to
  1401  // the build ID hash.
  1402  //
  1403  // We do not include as part of the build ID the operating system,
  1404  // architecture, or whether the race detector is enabled, even though all
  1405  // three have an effect on the output, because that information is used
  1406  // to decide the install location. Binaries for linux and binaries for
  1407  // darwin are written to different directory trees; including that
  1408  // information in the build ID is unnecessary (although it would be
  1409  // harmless).
  1410  //
  1411  // TODO(rsc): Investigate the cost of putting source file content into
  1412  // the build ID hash as a replacement for the use of mtimes. Using the
  1413  // file content would avoid all the mtime problems, but it does require
  1414  // reading all the source files, something we avoid today (we read the
  1415  // beginning to find the build tags and the imports, but we stop as soon
  1416  // as we see the import block is over). If the package is stale, the compiler
  1417  // is going to read the files anyway. But if the package is up-to-date, the
  1418  // read is overhead.
  1419  //
  1420  // TODO(rsc): Investigate the complexity of making the build more
  1421  // precise about when individual results are needed. To be fully precise,
  1422  // there are two results of a compilation: the entire .a file used by the link
  1423  // and the subpiece used by later compilations (__.PKGDEF only).
  1424  // If a rebuild is needed but produces the previous __.PKGDEF, then
  1425  // no more recompilation due to the rebuilt package is needed, only
  1426  // relinking. To date, there is nothing in the Go command to express this.
  1427  //
  1428  // Special Cases
  1429  //
  1430  // When the go command makes the wrong build decision and does not
  1431  // rebuild something it should, users fall back to adding the -a flag.
  1432  // Any common use of the -a flag should be considered prima facie evidence
  1433  // that isStale is returning an incorrect false result in some important case.
  1434  // Bugs reported in the behavior of -a itself should prompt the question
  1435  // ``Why is -a being used at all? What bug does that indicate?''
  1436  //
  1437  // There is a long history of changes to isStale to try to make -a into a
  1438  // suitable workaround for bugs in the mtime-based decisions.
  1439  // It is worth recording that history to inform (and, as much as possible, deter) future changes.
  1440  //
  1441  // (1) Before the build IDs were introduced, building with alternate tags
  1442  // would happily reuse installed objects built without those tags.
  1443  // For example, "go build -tags netgo myprog.go" would use the installed
  1444  // copy of package net, even if that copy had been built without netgo.
  1445  // (The netgo tag controls whether package net uses cgo or pure Go for
  1446  // functionality such as name resolution.)
  1447  // Using the installed non-netgo package defeats the purpose.
  1448  //
  1449  // Users worked around this with "go build -tags netgo -a myprog.go".
  1450  //
  1451  // Build IDs have made that workaround unnecessary:
  1452  // "go build -tags netgo myprog.go"
  1453  // cannot use a non-netgo copy of package net.
  1454  //
  1455  // (2) Before the build IDs were introduced, building with different toolchains,
  1456  // especially changing between toolchains, tried to reuse objects stored in
  1457  // $GOPATH/pkg, resulting in link-time errors about object file mismatches.
  1458  //
  1459  // Users worked around this with "go install -a ./...".
  1460  //
  1461  // Build IDs have made that workaround unnecessary:
  1462  // "go install ./..." will rebuild any objects it finds that were built against
  1463  // a different toolchain.
  1464  //
  1465  // (3) The common use of "go install -a ./..." led to reports of problems
  1466  // when the -a forced the rebuild of the standard library, which for some
  1467  // users was not writable. Because we didn't understand that the real
  1468  // problem was the bug -a was working around, we changed -a not to
  1469  // apply to the standard library.
  1470  //
  1471  // (4) The common use of "go build -tags netgo -a myprog.go" broke
  1472  // when we changed -a not to apply to the standard library, because
  1473  // if go build doesn't rebuild package net, it uses the non-netgo version.
  1474  //
  1475  // Users worked around this with "go build -tags netgo -installsuffix barf myprog.go".
  1476  // The -installsuffix here is making the go command look for packages
  1477  // in pkg/$GOOS_$GOARCH_barf instead of pkg/$GOOS_$GOARCH.
  1478  // Since the former presumably doesn't exist, go build decides to rebuild
  1479  // everything, including the standard library. Since go build doesn't
  1480  // install anything it builds, nothing is ever written to pkg/$GOOS_$GOARCH_barf,
  1481  // so repeated invocations continue to work.
  1482  //
  1483  // If the use of -a wasn't a red flag, the use of -installsuffix to point to
  1484  // a non-existent directory in a command that installs nothing should
  1485  // have been.
  1486  //
  1487  // (5) Now that (1) and (2) no longer need -a, we have removed the kludge
  1488  // introduced in (3): once again, -a means ``rebuild everything,'' not
  1489  // ``rebuild everything except the standard library.'' Only Go 1.4 had
  1490  // the restricted meaning.
  1491  //
  1492  // In addition to these cases trying to trigger rebuilds, there are
  1493  // special cases trying NOT to trigger rebuilds. The main one is that for
  1494  // a variety of reasons (see above), the install process for a Go release
  1495  // cannot be relied upon to set the mtimes such that the go command will
  1496  // think the standard library is up to date. So the mtime evidence is
  1497  // ignored for the standard library if we find ourselves in a release
  1498  // version of Go. Build ID-based staleness checks still apply to the
  1499  // standard library, even in release versions. This makes
  1500  // 'go build -tags netgo' work, among other things.
  1501  
  1502  // isStale reports whether package p needs to be rebuilt,
  1503  // along with the reason why.
  1504  func isStale(p *Package) (bool, string) {
  1505  	if p.Standard && (p.ImportPath == "unsafe" || buildContext.Compiler == "gccgo") {
  1506  		// fake, builtin package
  1507  		return false, "builtin package"
  1508  	}
  1509  	if p.Error != nil {
  1510  		return true, "errors loading package"
  1511  	}
  1512  	if p.Stale {
  1513  		return true, p.StaleReason
  1514  	}
  1515  
  1516  	// If this is a package with no source code, it cannot be rebuilt.
  1517  	// If the binary is missing, we mark the package stale so that
  1518  	// if a rebuild is needed, that rebuild attempt will produce a useful error.
  1519  	// (Some commands, such as 'go list', do not attempt to rebuild.)
  1520  	if p.BinaryOnly {
  1521  		if p.target == "" {
  1522  			// Fail if a build is attempted.
  1523  			return true, "no source code for package, but no install target"
  1524  		}
  1525  		if _, err := os.Stat(p.target); err != nil {
  1526  			// Fail if a build is attempted.
  1527  			return true, "no source code for package, but cannot access install target: " + err.Error()
  1528  		}
  1529  		return false, "no source code for package"
  1530  	}
  1531  
  1532  	// If the -a flag is given, rebuild everything.
  1533  	if buildA {
  1534  		return true, "build -a flag in use"
  1535  	}
  1536  
  1537  	// If there's no install target, we have to rebuild.
  1538  	if p.target == "" {
  1539  		return true, "no install target"
  1540  	}
  1541  
  1542  	// Package is stale if completely unbuilt.
  1543  	fi, err := os.Stat(p.target)
  1544  	if err != nil {
  1545  		return true, "cannot stat install target"
  1546  	}
  1547  
  1548  	// Package is stale if the expected build ID differs from the
  1549  	// recorded build ID. This catches changes like a source file
  1550  	// being removed from a package directory. See issue 3895.
  1551  	// It also catches changes in build tags that affect the set of
  1552  	// files being compiled. See issue 9369.
  1553  	// It also catches changes in toolchain, like when flipping between
  1554  	// two versions of Go compiling a single GOPATH.
  1555  	// See issue 8290 and issue 10702.
  1556  	targetBuildID, err := readBuildID(p)
  1557  	if err == nil && targetBuildID != p.buildID {
  1558  		return true, "build ID mismatch"
  1559  	}
  1560  
  1561  	// Package is stale if a dependency is.
  1562  	for _, p1 := range p.deps {
  1563  		if p1.Stale {
  1564  			return true, "stale dependency"
  1565  		}
  1566  	}
  1567  
  1568  	// The checks above are content-based staleness.
  1569  	// We assume they are always accurate.
  1570  	//
  1571  	// The checks below are mtime-based staleness.
  1572  	// We hope they are accurate, but we know that they fail in the case of
  1573  	// prebuilt Go installations that don't preserve the build mtimes
  1574  	// (for example, if the pkg/ mtimes are before the src/ mtimes).
  1575  	// See the large comment above isStale for details.
  1576  
  1577  	// If we are running a release copy of Go and didn't find a content-based
  1578  	// reason to rebuild the standard packages, do not rebuild them.
  1579  	// They may not be writable anyway, but they are certainly not changing.
  1580  	// This makes 'go build' skip the standard packages when
  1581  	// using an official release, even when the mtimes have been changed.
  1582  	// See issue 3036, issue 3149, issue 4106, issue 8290.
  1583  	// (If a change to a release tree must be made by hand, the way to force the
  1584  	// install is to run make.bash, which will remove the old package archives
  1585  	// before rebuilding.)
  1586  	if p.Standard && isGoRelease {
  1587  		return false, "standard package in Go release distribution"
  1588  	}
  1589  
  1590  	// Time-based staleness.
  1591  
  1592  	built := fi.ModTime()
  1593  
  1594  	olderThan := func(file string) bool {
  1595  		fi, err := os.Stat(file)
  1596  		return err != nil || fi.ModTime().After(built)
  1597  	}
  1598  
  1599  	// Package is stale if a dependency is, or if a dependency is newer.
  1600  	for _, p1 := range p.deps {
  1601  		if p1.target != "" && olderThan(p1.target) {
  1602  			return true, "newer dependency"
  1603  		}
  1604  	}
  1605  
  1606  	// As a courtesy to developers installing new versions of the compiler
  1607  	// frequently, define that packages are stale if they are
  1608  	// older than the compiler, and commands if they are older than
  1609  	// the linker. This heuristic will not work if the binaries are
  1610  	// back-dated, as some binary distributions may do, but it does handle
  1611  	// a very common case.
  1612  	// See issue 3036.
  1613  	// Exclude $GOROOT, under the assumption that people working on
  1614  	// the compiler may want to control when everything gets rebuilt,
  1615  	// and people updating the Go repository will run make.bash or all.bash
  1616  	// and get a full rebuild anyway.
  1617  	// Excluding $GOROOT used to also fix issue 4106, but that's now
  1618  	// taken care of above (at least when the installed Go is a released version).
  1619  	if p.Root != goroot {
  1620  		if olderThan(buildToolchain.compiler()) {
  1621  			return true, "newer compiler"
  1622  		}
  1623  		if p.build.IsCommand() && olderThan(buildToolchain.linker()) {
  1624  			return true, "newer linker"
  1625  		}
  1626  	}
  1627  
  1628  	// Note: Until Go 1.5, we had an additional shortcut here.
  1629  	// We built a list of the workspace roots ($GOROOT, each $GOPATH)
  1630  	// containing targets directly named on the command line,
  1631  	// and if p were not in any of those, it would be treated as up-to-date
  1632  	// as long as it is built. The goal was to avoid rebuilding a system-installed
  1633  	// $GOROOT, unless something from $GOROOT were explicitly named
  1634  	// on the command line (like go install math).
  1635  	// That's now handled by the isGoRelease clause above.
  1636  	// The other effect of the shortcut was to isolate different entries in
  1637  	// $GOPATH from each other. This had the unfortunate effect that
  1638  	// if you had (say), GOPATH listing two entries, one for commands
  1639  	// and one for libraries, and you did a 'git pull' in the library one
  1640  	// and then tried 'go install commands/...', it would build the new libraries
  1641  	// during the first build (because they wouldn't have been installed at all)
  1642  	// but then subsequent builds would not rebuild the libraries, even if the
  1643  	// mtimes indicate they are stale, because the different GOPATH entries
  1644  	// were treated differently. This behavior was confusing when using
  1645  	// non-trivial GOPATHs, which were particularly common with some
  1646  	// code management conventions, like the original godep.
  1647  	// Since the $GOROOT case (the original motivation) is handled separately,
  1648  	// we no longer put a barrier between the different $GOPATH entries.
  1649  	//
  1650  	// One implication of this is that if there is a system directory for
  1651  	// non-standard Go packages that is included in $GOPATH, the mtimes
  1652  	// on those compiled packages must be no earlier than the mtimes
  1653  	// on the source files. Since most distributions use the same mtime
  1654  	// for all files in a tree, they will be unaffected. People using plain
  1655  	// tar x to extract system-installed packages will need to adjust mtimes,
  1656  	// but it's better to force them to get the mtimes right than to ignore
  1657  	// the mtimes and thereby do the wrong thing in common use cases.
  1658  	//
  1659  	// So there is no GOPATH vs GOPATH shortcut here anymore.
  1660  	//
  1661  	// If something needs to come back here, we could try writing a dummy
  1662  	// file with a random name to the $GOPATH/pkg directory (and removing it)
  1663  	// to test for write access, and then skip GOPATH roots we don't have write
  1664  	// access to. But hopefully we can just use the mtimes always.
  1665  
  1666  	srcs := stringList(p.GoFiles, p.CFiles, p.CXXFiles, p.MFiles, p.HFiles, p.FFiles, p.SFiles, p.CgoFiles, p.SysoFiles, p.SwigFiles, p.SwigCXXFiles)
  1667  	for _, src := range srcs {
  1668  		if olderThan(filepath.Join(p.Dir, src)) {
  1669  			return true, "newer source file"
  1670  		}
  1671  	}
  1672  
  1673  	return false, ""
  1674  }
  1675  
  1676  // computeBuildID computes the build ID for p, leaving it in p.buildID.
  1677  // Build ID is a hash of the information we want to detect changes in.
  1678  // See the long comment in isStale for details.
  1679  func computeBuildID(p *Package) {
  1680  	h := sha1.New()
  1681  
  1682  	// Include the list of files compiled as part of the package.
  1683  	// This lets us detect removed files. See issue 3895.
  1684  	inputFiles := stringList(
  1685  		p.GoFiles,
  1686  		p.CgoFiles,
  1687  		p.CFiles,
  1688  		p.CXXFiles,
  1689  		p.MFiles,
  1690  		p.HFiles,
  1691  		p.SFiles,
  1692  		p.SysoFiles,
  1693  		p.SwigFiles,
  1694  		p.SwigCXXFiles,
  1695  	)
  1696  	for _, file := range inputFiles {
  1697  		fmt.Fprintf(h, "file %s\n", file)
  1698  	}
  1699  
  1700  	// Include the content of runtime/internal/sys/zversion.go in the hash
  1701  	// for package runtime. This will give package runtime a
  1702  	// different build ID in each Go release.
  1703  	if p.Standard && p.ImportPath == "runtime/internal/sys" && buildContext.Compiler != "gccgo" {
  1704  		data, err := ioutil.ReadFile(filepath.Join(p.Dir, "zversion.go"))
  1705  		if err != nil {
  1706  			fatalf("go: %s", err)
  1707  		}
  1708  		fmt.Fprintf(h, "zversion %q\n", string(data))
  1709  	}
  1710  
  1711  	// Include the build IDs of any dependencies in the hash.
  1712  	// This, combined with the runtime/zversion content,
  1713  	// will cause packages to have different build IDs when
  1714  	// compiled with different Go releases.
  1715  	// This helps the go command know to recompile when
  1716  	// people use the same GOPATH but switch between
  1717  	// different Go releases. See issue 10702.
  1718  	// This is also a better fix for issue 8290.
  1719  	for _, p1 := range p.deps {
  1720  		fmt.Fprintf(h, "dep %s %s\n", p1.ImportPath, p1.buildID)
  1721  	}
  1722  
  1723  	p.buildID = fmt.Sprintf("%x", h.Sum(nil))
  1724  }
  1725  
  1726  var cwd, _ = os.Getwd()
  1727  
  1728  var cmdCache = map[string]*Package{}
  1729  
  1730  // loadPackage is like loadImport but is used for command-line arguments,
  1731  // not for paths found in import statements. In addition to ordinary import paths,
  1732  // loadPackage accepts pseudo-paths beginning with cmd/ to denote commands
  1733  // in the Go command directory, as well as paths to those directories.
  1734  func loadPackage(arg string, stk *importStack) *Package {
  1735  	if build.IsLocalImport(arg) {
  1736  		dir := arg
  1737  		if !filepath.IsAbs(dir) {
  1738  			if abs, err := filepath.Abs(dir); err == nil {
  1739  				// interpret relative to current directory
  1740  				dir = abs
  1741  			}
  1742  		}
  1743  		if sub, ok := hasSubdir(gorootSrc, dir); ok && strings.HasPrefix(sub, "cmd/") && !strings.Contains(sub[4:], "/") {
  1744  			arg = sub
  1745  		}
  1746  	}
  1747  	if strings.HasPrefix(arg, "cmd/") && !strings.Contains(arg[4:], "/") {
  1748  		if p := cmdCache[arg]; p != nil {
  1749  			return p
  1750  		}
  1751  		stk.push(arg)
  1752  		defer stk.pop()
  1753  
  1754  		bp, err := buildContext.ImportDir(filepath.Join(gorootSrc, arg), 0)
  1755  		bp.ImportPath = arg
  1756  		bp.Goroot = true
  1757  		bp.BinDir = gorootBin
  1758  		if gobin != "" {
  1759  			bp.BinDir = gobin
  1760  		}
  1761  		bp.Root = goroot
  1762  		bp.SrcRoot = gorootSrc
  1763  		p := new(Package)
  1764  		cmdCache[arg] = p
  1765  		p.load(stk, bp, err)
  1766  		if p.Error == nil && p.Name != "main" {
  1767  			p.Error = &PackageError{
  1768  				ImportStack: stk.copy(),
  1769  				Err:         fmt.Sprintf("expected package main but found package %s in %s", p.Name, p.Dir),
  1770  			}
  1771  		}
  1772  		return p
  1773  	}
  1774  
  1775  	// Wasn't a command; must be a package.
  1776  	// If it is a local import path but names a standard package,
  1777  	// we treat it as if the user specified the standard package.
  1778  	// This lets you run go test ./ioutil in package io and be
  1779  	// referring to io/ioutil rather than a hypothetical import of
  1780  	// "./ioutil".
  1781  	if build.IsLocalImport(arg) {
  1782  		bp, _ := buildContext.ImportDir(filepath.Join(cwd, arg), build.FindOnly)
  1783  		if bp.ImportPath != "" && bp.ImportPath != "." {
  1784  			arg = bp.ImportPath
  1785  		}
  1786  	}
  1787  
  1788  	return loadImport(arg, cwd, nil, stk, nil, 0)
  1789  }
  1790  
  1791  // packages returns the packages named by the
  1792  // command line arguments 'args'.  If a named package
  1793  // cannot be loaded at all (for example, if the directory does not exist),
  1794  // then packages prints an error and does not include that
  1795  // package in the results. However, if errors occur trying
  1796  // to load dependencies of a named package, the named
  1797  // package is still returned, with p.Incomplete = true
  1798  // and details in p.DepsErrors.
  1799  func packages(args []string) []*Package {
  1800  	var pkgs []*Package
  1801  	for _, pkg := range packagesAndErrors(args) {
  1802  		if pkg.Error != nil {
  1803  			errorf("can't load package: %s", pkg.Error)
  1804  			continue
  1805  		}
  1806  		pkgs = append(pkgs, pkg)
  1807  	}
  1808  	return pkgs
  1809  }
  1810  
  1811  // packagesAndErrors is like 'packages' but returns a
  1812  // *Package for every argument, even the ones that
  1813  // cannot be loaded at all.
  1814  // The packages that fail to load will have p.Error != nil.
  1815  func packagesAndErrors(args []string) []*Package {
  1816  	if len(args) > 0 && strings.HasSuffix(args[0], ".go") {
  1817  		return []*Package{goFilesPackage(args)}
  1818  	}
  1819  
  1820  	args = importPaths(args)
  1821  	var (
  1822  		pkgs    []*Package
  1823  		stk     importStack
  1824  		seenArg = make(map[string]bool)
  1825  		seenPkg = make(map[*Package]bool)
  1826  	)
  1827  
  1828  	for _, arg := range args {
  1829  		if seenArg[arg] {
  1830  			continue
  1831  		}
  1832  		seenArg[arg] = true
  1833  		pkg := loadPackage(arg, &stk)
  1834  		if seenPkg[pkg] {
  1835  			continue
  1836  		}
  1837  		seenPkg[pkg] = true
  1838  		pkgs = append(pkgs, pkg)
  1839  	}
  1840  	computeStale(pkgs...)
  1841  
  1842  	return pkgs
  1843  }
  1844  
  1845  // packagesForBuild is like 'packages' but fails if any of
  1846  // the packages or their dependencies have errors
  1847  // (cannot be built).
  1848  func packagesForBuild(args []string) []*Package {
  1849  	pkgs := packagesAndErrors(args)
  1850  	printed := map[*PackageError]bool{}
  1851  	for _, pkg := range pkgs {
  1852  		if pkg.Error != nil {
  1853  			errorf("can't load package: %s", pkg.Error)
  1854  		}
  1855  		for _, err := range pkg.DepsErrors {
  1856  			// Since these are errors in dependencies,
  1857  			// the same error might show up multiple times,
  1858  			// once in each package that depends on it.
  1859  			// Only print each once.
  1860  			if !printed[err] {
  1861  				printed[err] = true
  1862  				errorf("%s", err)
  1863  			}
  1864  		}
  1865  	}
  1866  	exitIfErrors()
  1867  
  1868  	// Check for duplicate loads of the same package.
  1869  	// That should be impossible, but if it does happen then
  1870  	// we end up trying to build the same package twice,
  1871  	// usually in parallel overwriting the same files,
  1872  	// which doesn't work very well.
  1873  	seen := map[string]bool{}
  1874  	reported := map[string]bool{}
  1875  	for _, pkg := range packageList(pkgs) {
  1876  		if seen[pkg.ImportPath] && !reported[pkg.ImportPath] {
  1877  			reported[pkg.ImportPath] = true
  1878  			errorf("internal error: duplicate loads of %s", pkg.ImportPath)
  1879  		}
  1880  		seen[pkg.ImportPath] = true
  1881  	}
  1882  	exitIfErrors()
  1883  
  1884  	return pkgs
  1885  }
  1886  
  1887  // hasSubdir reports whether dir is a subdirectory of
  1888  // (possibly multiple levels below) root.
  1889  // If so, it sets rel to the path fragment that must be
  1890  // appended to root to reach dir.
  1891  func hasSubdir(root, dir string) (rel string, ok bool) {
  1892  	if p, err := filepath.EvalSymlinks(root); err == nil {
  1893  		root = p
  1894  	}
  1895  	if p, err := filepath.EvalSymlinks(dir); err == nil {
  1896  		dir = p
  1897  	}
  1898  	const sep = string(filepath.Separator)
  1899  	root = filepath.Clean(root)
  1900  	if !strings.HasSuffix(root, sep) {
  1901  		root += sep
  1902  	}
  1903  	dir = filepath.Clean(dir)
  1904  	if !strings.HasPrefix(dir, root) {
  1905  		return "", false
  1906  	}
  1907  	return filepath.ToSlash(dir[len(root):]), true
  1908  }
  1909  
  1910  var (
  1911  	errBuildIDToolchain = fmt.Errorf("build ID only supported in gc toolchain")
  1912  	errBuildIDMalformed = fmt.Errorf("malformed object file")
  1913  	errBuildIDUnknown   = fmt.Errorf("lost build ID")
  1914  )
  1915  
  1916  var (
  1917  	bangArch = []byte("!<arch>")
  1918  	pkgdef   = []byte("__.PKGDEF")
  1919  	goobject = []byte("go object ")
  1920  	buildid  = []byte("build id ")
  1921  )
  1922  
  1923  // readBuildID reads the build ID from an archive or binary.
  1924  // It only supports the gc toolchain.
  1925  // Other toolchain maintainers should adjust this function.
  1926  func readBuildID(p *Package) (id string, err error) {
  1927  	if buildToolchain != (gcToolchain{}) {
  1928  		return "", errBuildIDToolchain
  1929  	}
  1930  
  1931  	// For commands, read build ID directly from binary.
  1932  	if p.Name == "main" {
  1933  		return ReadBuildIDFromBinary(p.Target)
  1934  	}
  1935  
  1936  	// Otherwise, we expect to have an archive (.a) file,
  1937  	// and we can read the build ID from the Go export data.
  1938  	if !strings.HasSuffix(p.Target, ".a") {
  1939  		return "", &os.PathError{Op: "parse", Path: p.Target, Err: errBuildIDUnknown}
  1940  	}
  1941  
  1942  	// Read just enough of the target to fetch the build ID.
  1943  	// The archive is expected to look like:
  1944  	//
  1945  	//	!<arch>
  1946  	//	__.PKGDEF       0           0     0     644     7955      `
  1947  	//	go object darwin amd64 devel X:none
  1948  	//	build id "b41e5c45250e25c9fd5e9f9a1de7857ea0d41224"
  1949  	//
  1950  	// The variable-sized strings are GOOS, GOARCH, and the experiment list (X:none).
  1951  	// Reading the first 1024 bytes should be plenty.
  1952  	f, err := os.Open(p.Target)
  1953  	if err != nil {
  1954  		return "", err
  1955  	}
  1956  	data := make([]byte, 1024)
  1957  	n, err := io.ReadFull(f, data)
  1958  	f.Close()
  1959  
  1960  	if err != nil && n == 0 {
  1961  		return "", err
  1962  	}
  1963  
  1964  	bad := func() (string, error) {
  1965  		return "", &os.PathError{Op: "parse", Path: p.Target, Err: errBuildIDMalformed}
  1966  	}
  1967  
  1968  	// Archive header.
  1969  	for i := 0; ; i++ { // returns during i==3
  1970  		j := bytes.IndexByte(data, '\n')
  1971  		if j < 0 {
  1972  			return bad()
  1973  		}
  1974  		line := data[:j]
  1975  		data = data[j+1:]
  1976  		switch i {
  1977  		case 0:
  1978  			if !bytes.Equal(line, bangArch) {
  1979  				return bad()
  1980  			}
  1981  		case 1:
  1982  			if !bytes.HasPrefix(line, pkgdef) {
  1983  				return bad()
  1984  			}
  1985  		case 2:
  1986  			if !bytes.HasPrefix(line, goobject) {
  1987  				return bad()
  1988  			}
  1989  		case 3:
  1990  			if !bytes.HasPrefix(line, buildid) {
  1991  				// Found the object header, just doesn't have a build id line.
  1992  				// Treat as successful, with empty build id.
  1993  				return "", nil
  1994  			}
  1995  			id, err := strconv.Unquote(string(line[len(buildid):]))
  1996  			if err != nil {
  1997  				return bad()
  1998  			}
  1999  			return id, nil
  2000  		}
  2001  	}
  2002  }
  2003  
  2004  var (
  2005  	goBuildPrefix = []byte("\xff Go build ID: \"")
  2006  	goBuildEnd    = []byte("\"\n \xff")
  2007  
  2008  	elfPrefix = []byte("\x7fELF")
  2009  
  2010  	machoPrefixes = [][]byte{
  2011  		{0xfe, 0xed, 0xfa, 0xce},
  2012  		{0xfe, 0xed, 0xfa, 0xcf},
  2013  		{0xce, 0xfa, 0xed, 0xfe},
  2014  		{0xcf, 0xfa, 0xed, 0xfe},
  2015  	}
  2016  )
  2017  
  2018  var BuildIDReadSize = 32 * 1024 // changed for testing
  2019  
  2020  // ReadBuildIDFromBinary reads the build ID from a binary.
  2021  //
  2022  // ELF binaries store the build ID in a proper PT_NOTE section.
  2023  //
  2024  // Other binary formats are not so flexible. For those, the linker
  2025  // stores the build ID as non-instruction bytes at the very beginning
  2026  // of the text segment, which should appear near the beginning
  2027  // of the file. This is clumsy but fairly portable. Custom locations
  2028  // can be added for other binary types as needed, like we did for ELF.
  2029  func ReadBuildIDFromBinary(filename string) (id string, err error) {
  2030  	if filename == "" {
  2031  		return "", &os.PathError{Op: "parse", Path: filename, Err: errBuildIDUnknown}
  2032  	}
  2033  
  2034  	// Read the first 32 kB of the binary file.
  2035  	// That should be enough to find the build ID.
  2036  	// In ELF files, the build ID is in the leading headers,
  2037  	// which are typically less than 4 kB, not to mention 32 kB.
  2038  	// In Mach-O files, there's no limit, so we have to parse the file.
  2039  	// On other systems, we're trying to read enough that
  2040  	// we get the beginning of the text segment in the read.
  2041  	// The offset where the text segment begins in a hello
  2042  	// world compiled for each different object format today:
  2043  	//
  2044  	//	Plan 9: 0x20
  2045  	//	Windows: 0x600
  2046  	//
  2047  	f, err := os.Open(filename)
  2048  	if err != nil {
  2049  		return "", err
  2050  	}
  2051  	defer f.Close()
  2052  
  2053  	data := make([]byte, BuildIDReadSize)
  2054  	_, err = io.ReadFull(f, data)
  2055  	if err == io.ErrUnexpectedEOF {
  2056  		err = nil
  2057  	}
  2058  	if err != nil {
  2059  		return "", err
  2060  	}
  2061  
  2062  	if bytes.HasPrefix(data, elfPrefix) {
  2063  		return readELFGoBuildID(filename, f, data)
  2064  	}
  2065  	for _, m := range machoPrefixes {
  2066  		if bytes.HasPrefix(data, m) {
  2067  			return readMachoGoBuildID(filename, f, data)
  2068  		}
  2069  	}
  2070  
  2071  	return readRawGoBuildID(filename, data)
  2072  }
  2073  
  2074  // readRawGoBuildID finds the raw build ID stored in text segment data.
  2075  func readRawGoBuildID(filename string, data []byte) (id string, err error) {
  2076  	i := bytes.Index(data, goBuildPrefix)
  2077  	if i < 0 {
  2078  		// Missing. Treat as successful but build ID empty.
  2079  		return "", nil
  2080  	}
  2081  
  2082  	j := bytes.Index(data[i+len(goBuildPrefix):], goBuildEnd)
  2083  	if j < 0 {
  2084  		return "", &os.PathError{Op: "parse", Path: filename, Err: errBuildIDMalformed}
  2085  	}
  2086  
  2087  	quoted := data[i+len(goBuildPrefix)-1 : i+len(goBuildPrefix)+j+1]
  2088  	id, err = strconv.Unquote(string(quoted))
  2089  	if err != nil {
  2090  		return "", &os.PathError{Op: "parse", Path: filename, Err: errBuildIDMalformed}
  2091  	}
  2092  
  2093  	return id, nil
  2094  }