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