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