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