Source file src/go/build/build.go

     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 build
     6  
     7  import (
     8  	"bytes"
     9  	"errors"
    10  	"fmt"
    11  	"go/ast"
    12  	"go/build/constraint"
    13  	"go/doc"
    14  	"go/token"
    15  	"internal/buildcfg"
    16  	"internal/goroot"
    17  	"internal/goversion"
    18  	"io"
    19  	"io/fs"
    20  	"os"
    21  	"os/exec"
    22  	pathpkg "path"
    23  	"path/filepath"
    24  	"runtime"
    25  	"sort"
    26  	"strconv"
    27  	"strings"
    28  	"unicode"
    29  	"unicode/utf8"
    30  )
    31  
    32  // A Context specifies the supporting context for a build.
    33  type Context struct {
    34  	GOARCH string // target architecture
    35  	GOOS   string // target operating system
    36  	GOROOT string // Go root
    37  	GOPATH string // Go paths
    38  
    39  	// Dir is the caller's working directory, or the empty string to use
    40  	// the current directory of the running process. In module mode, this is used
    41  	// to locate the main module.
    42  	//
    43  	// If Dir is non-empty, directories passed to Import and ImportDir must
    44  	// be absolute.
    45  	Dir string
    46  
    47  	CgoEnabled  bool   // whether cgo files are included
    48  	UseAllFiles bool   // use files regardless of +build lines, file names
    49  	Compiler    string // compiler to assume when computing target paths
    50  
    51  	// The build, tool, and release tags specify build constraints
    52  	// that should be considered satisfied when processing +build lines.
    53  	// Clients creating a new context may customize BuildTags, which
    54  	// defaults to empty, but it is usually an error to customize ToolTags or ReleaseTags.
    55  	// ToolTags defaults to build tags appropriate to the current Go toolchain configuration.
    56  	// ReleaseTags defaults to the list of Go releases the current release is compatible with.
    57  	// BuildTags is not set for the Default build Context.
    58  	// In addition to the BuildTags, ToolTags, and ReleaseTags, build constraints
    59  	// consider the values of GOARCH and GOOS as satisfied tags.
    60  	// The last element in ReleaseTags is assumed to be the current release.
    61  	BuildTags   []string
    62  	ToolTags    []string
    63  	ReleaseTags []string
    64  
    65  	// The install suffix specifies a suffix to use in the name of the installation
    66  	// directory. By default it is empty, but custom builds that need to keep
    67  	// their outputs separate can set InstallSuffix to do so. For example, when
    68  	// using the race detector, the go command uses InstallSuffix = "race", so
    69  	// that on a Linux/386 system, packages are written to a directory named
    70  	// "linux_386_race" instead of the usual "linux_386".
    71  	InstallSuffix string
    72  
    73  	// By default, Import uses the operating system's file system calls
    74  	// to read directories and files. To read from other sources,
    75  	// callers can set the following functions. They all have default
    76  	// behaviors that use the local file system, so clients need only set
    77  	// the functions whose behaviors they wish to change.
    78  
    79  	// JoinPath joins the sequence of path fragments into a single path.
    80  	// If JoinPath is nil, Import uses filepath.Join.
    81  	JoinPath func(elem ...string) string
    82  
    83  	// SplitPathList splits the path list into a slice of individual paths.
    84  	// If SplitPathList is nil, Import uses filepath.SplitList.
    85  	SplitPathList func(list string) []string
    86  
    87  	// IsAbsPath reports whether path is an absolute path.
    88  	// If IsAbsPath is nil, Import uses filepath.IsAbs.
    89  	IsAbsPath func(path string) bool
    90  
    91  	// IsDir reports whether the path names a directory.
    92  	// If IsDir is nil, Import calls os.Stat and uses the result's IsDir method.
    93  	IsDir func(path string) bool
    94  
    95  	// HasSubdir reports whether dir is lexically a subdirectory of
    96  	// root, perhaps multiple levels below. It does not try to check
    97  	// whether dir exists.
    98  	// If so, HasSubdir sets rel to a slash-separated path that
    99  	// can be joined to root to produce a path equivalent to dir.
   100  	// If HasSubdir is nil, Import uses an implementation built on
   101  	// filepath.EvalSymlinks.
   102  	HasSubdir func(root, dir string) (rel string, ok bool)
   103  
   104  	// ReadDir returns a slice of fs.FileInfo, sorted by Name,
   105  	// describing the content of the named directory.
   106  	// If ReadDir is nil, Import uses ioutil.ReadDir.
   107  	ReadDir func(dir string) ([]fs.FileInfo, error)
   108  
   109  	// OpenFile opens a file (not a directory) for reading.
   110  	// If OpenFile is nil, Import uses os.Open.
   111  	OpenFile func(path string) (io.ReadCloser, error)
   112  }
   113  
   114  // joinPath calls ctxt.JoinPath (if not nil) or else filepath.Join.
   115  func (ctxt *Context) joinPath(elem ...string) string {
   116  	if f := ctxt.JoinPath; f != nil {
   117  		return f(elem...)
   118  	}
   119  	return filepath.Join(elem...)
   120  }
   121  
   122  // splitPathList calls ctxt.SplitPathList (if not nil) or else filepath.SplitList.
   123  func (ctxt *Context) splitPathList(s string) []string {
   124  	if f := ctxt.SplitPathList; f != nil {
   125  		return f(s)
   126  	}
   127  	return filepath.SplitList(s)
   128  }
   129  
   130  // isAbsPath calls ctxt.IsAbsPath (if not nil) or else filepath.IsAbs.
   131  func (ctxt *Context) isAbsPath(path string) bool {
   132  	if f := ctxt.IsAbsPath; f != nil {
   133  		return f(path)
   134  	}
   135  	return filepath.IsAbs(path)
   136  }
   137  
   138  // isDir calls ctxt.IsDir (if not nil) or else uses os.Stat.
   139  func (ctxt *Context) isDir(path string) bool {
   140  	if f := ctxt.IsDir; f != nil {
   141  		return f(path)
   142  	}
   143  	fi, err := os.Stat(path)
   144  	return err == nil && fi.IsDir()
   145  }
   146  
   147  // hasSubdir calls ctxt.HasSubdir (if not nil) or else uses
   148  // the local file system to answer the question.
   149  func (ctxt *Context) hasSubdir(root, dir string) (rel string, ok bool) {
   150  	if f := ctxt.HasSubdir; f != nil {
   151  		return f(root, dir)
   152  	}
   153  
   154  	// Try using paths we received.
   155  	if rel, ok = hasSubdir(root, dir); ok {
   156  		return
   157  	}
   158  
   159  	// Try expanding symlinks and comparing
   160  	// expanded against unexpanded and
   161  	// expanded against expanded.
   162  	rootSym, _ := filepath.EvalSymlinks(root)
   163  	dirSym, _ := filepath.EvalSymlinks(dir)
   164  
   165  	if rel, ok = hasSubdir(rootSym, dir); ok {
   166  		return
   167  	}
   168  	if rel, ok = hasSubdir(root, dirSym); ok {
   169  		return
   170  	}
   171  	return hasSubdir(rootSym, dirSym)
   172  }
   173  
   174  // hasSubdir reports if dir is within root by performing lexical analysis only.
   175  func hasSubdir(root, dir string) (rel string, ok bool) {
   176  	const sep = string(filepath.Separator)
   177  	root = filepath.Clean(root)
   178  	if !strings.HasSuffix(root, sep) {
   179  		root += sep
   180  	}
   181  	dir = filepath.Clean(dir)
   182  	if !strings.HasPrefix(dir, root) {
   183  		return "", false
   184  	}
   185  	return filepath.ToSlash(dir[len(root):]), true
   186  }
   187  
   188  // readDir calls ctxt.ReadDir (if not nil) or else os.ReadDir.
   189  func (ctxt *Context) readDir(path string) ([]fs.DirEntry, error) {
   190  	// TODO: add a fs.DirEntry version of Context.ReadDir
   191  	if f := ctxt.ReadDir; f != nil {
   192  		fis, err := f(path)
   193  		if err != nil {
   194  			return nil, err
   195  		}
   196  		des := make([]fs.DirEntry, len(fis))
   197  		for i, fi := range fis {
   198  			des[i] = fs.FileInfoToDirEntry(fi)
   199  		}
   200  		return des, nil
   201  	}
   202  	return os.ReadDir(path)
   203  }
   204  
   205  // openFile calls ctxt.OpenFile (if not nil) or else os.Open.
   206  func (ctxt *Context) openFile(path string) (io.ReadCloser, error) {
   207  	if fn := ctxt.OpenFile; fn != nil {
   208  		return fn(path)
   209  	}
   210  
   211  	f, err := os.Open(path)
   212  	if err != nil {
   213  		return nil, err // nil interface
   214  	}
   215  	return f, nil
   216  }
   217  
   218  // isFile determines whether path is a file by trying to open it.
   219  // It reuses openFile instead of adding another function to the
   220  // list in Context.
   221  func (ctxt *Context) isFile(path string) bool {
   222  	f, err := ctxt.openFile(path)
   223  	if err != nil {
   224  		return false
   225  	}
   226  	f.Close()
   227  	return true
   228  }
   229  
   230  // gopath returns the list of Go path directories.
   231  func (ctxt *Context) gopath() []string {
   232  	var all []string
   233  	for _, p := range ctxt.splitPathList(ctxt.GOPATH) {
   234  		if p == "" || p == ctxt.GOROOT {
   235  			// Empty paths are uninteresting.
   236  			// If the path is the GOROOT, ignore it.
   237  			// People sometimes set GOPATH=$GOROOT.
   238  			// Do not get confused by this common mistake.
   239  			continue
   240  		}
   241  		if strings.HasPrefix(p, "~") {
   242  			// Path segments starting with ~ on Unix are almost always
   243  			// users who have incorrectly quoted ~ while setting GOPATH,
   244  			// preventing it from expanding to $HOME.
   245  			// The situation is made more confusing by the fact that
   246  			// bash allows quoted ~ in $PATH (most shells do not).
   247  			// Do not get confused by this, and do not try to use the path.
   248  			// It does not exist, and printing errors about it confuses
   249  			// those users even more, because they think "sure ~ exists!".
   250  			// The go command diagnoses this situation and prints a
   251  			// useful error.
   252  			// On Windows, ~ is used in short names, such as c:\progra~1
   253  			// for c:\program files.
   254  			continue
   255  		}
   256  		all = append(all, p)
   257  	}
   258  	return all
   259  }
   260  
   261  // SrcDirs returns a list of package source root directories.
   262  // It draws from the current Go root and Go path but omits directories
   263  // that do not exist.
   264  func (ctxt *Context) SrcDirs() []string {
   265  	var all []string
   266  	if ctxt.GOROOT != "" && ctxt.Compiler != "gccgo" {
   267  		dir := ctxt.joinPath(ctxt.GOROOT, "src")
   268  		if ctxt.isDir(dir) {
   269  			all = append(all, dir)
   270  		}
   271  	}
   272  	for _, p := range ctxt.gopath() {
   273  		dir := ctxt.joinPath(p, "src")
   274  		if ctxt.isDir(dir) {
   275  			all = append(all, dir)
   276  		}
   277  	}
   278  	return all
   279  }
   280  
   281  // Default is the default Context for builds.
   282  // It uses the GOARCH, GOOS, GOROOT, and GOPATH environment variables
   283  // if set, or else the compiled code's GOARCH, GOOS, and GOROOT.
   284  var Default Context = defaultContext()
   285  
   286  func defaultGOPATH() string {
   287  	env := "HOME"
   288  	if runtime.GOOS == "windows" {
   289  		env = "USERPROFILE"
   290  	} else if runtime.GOOS == "plan9" {
   291  		env = "home"
   292  	}
   293  	if home := os.Getenv(env); home != "" {
   294  		def := filepath.Join(home, "go")
   295  		if filepath.Clean(def) == filepath.Clean(runtime.GOROOT()) {
   296  			// Don't set the default GOPATH to GOROOT,
   297  			// as that will trigger warnings from the go tool.
   298  			return ""
   299  		}
   300  		return def
   301  	}
   302  	return ""
   303  }
   304  
   305  var defaultToolTags, defaultReleaseTags []string
   306  
   307  func defaultContext() Context {
   308  	var c Context
   309  
   310  	c.GOARCH = buildcfg.GOARCH
   311  	c.GOOS = buildcfg.GOOS
   312  	if goroot := runtime.GOROOT(); goroot != "" {
   313  		c.GOROOT = filepath.Clean(goroot)
   314  	}
   315  	c.GOPATH = envOr("GOPATH", defaultGOPATH())
   316  	c.Compiler = runtime.Compiler
   317  
   318  	// For each experiment that has been enabled in the toolchain, define a
   319  	// build tag with the same name but prefixed by "goexperiment." which can be
   320  	// used for compiling alternative files for the experiment. This allows
   321  	// changes for the experiment, like extra struct fields in the runtime,
   322  	// without affecting the base non-experiment code at all.
   323  	for _, exp := range buildcfg.Experiment.Enabled() {
   324  		c.ToolTags = append(c.ToolTags, "goexperiment."+exp)
   325  	}
   326  	defaultToolTags = append([]string{}, c.ToolTags...) // our own private copy
   327  
   328  	// Each major Go release in the Go 1.x series adds a new
   329  	// "go1.x" release tag. That is, the go1.x tag is present in
   330  	// all releases >= Go 1.x. Code that requires Go 1.x or later
   331  	// should say "+build go1.x", and code that should only be
   332  	// built before Go 1.x (perhaps it is the stub to use in that
   333  	// case) should say "+build !go1.x".
   334  	// The last element in ReleaseTags is the current release.
   335  	for i := 1; i <= goversion.Version; i++ {
   336  		c.ReleaseTags = append(c.ReleaseTags, "go1."+strconv.Itoa(i))
   337  	}
   338  
   339  	defaultReleaseTags = append([]string{}, c.ReleaseTags...) // our own private copy
   340  
   341  	env := os.Getenv("CGO_ENABLED")
   342  	if env == "" {
   343  		env = defaultCGO_ENABLED
   344  	}
   345  	switch env {
   346  	case "1":
   347  		c.CgoEnabled = true
   348  	case "0":
   349  		c.CgoEnabled = false
   350  	default:
   351  		// cgo must be explicitly enabled for cross compilation builds
   352  		if runtime.GOARCH == c.GOARCH && runtime.GOOS == c.GOOS {
   353  			c.CgoEnabled = cgoEnabled[c.GOOS+"/"+c.GOARCH]
   354  			break
   355  		}
   356  		c.CgoEnabled = false
   357  	}
   358  
   359  	return c
   360  }
   361  
   362  func envOr(name, def string) string {
   363  	s := os.Getenv(name)
   364  	if s == "" {
   365  		return def
   366  	}
   367  	return s
   368  }
   369  
   370  // An ImportMode controls the behavior of the Import method.
   371  type ImportMode uint
   372  
   373  const (
   374  	// If FindOnly is set, Import stops after locating the directory
   375  	// that should contain the sources for a package. It does not
   376  	// read any files in the directory.
   377  	FindOnly ImportMode = 1 << iota
   378  
   379  	// If AllowBinary is set, Import can be satisfied by a compiled
   380  	// package object without corresponding sources.
   381  	//
   382  	// Deprecated:
   383  	// The supported way to create a compiled-only package is to
   384  	// write source code containing a //go:binary-only-package comment at
   385  	// the top of the file. Such a package will be recognized
   386  	// regardless of this flag setting (because it has source code)
   387  	// and will have BinaryOnly set to true in the returned Package.
   388  	AllowBinary
   389  
   390  	// If ImportComment is set, parse import comments on package statements.
   391  	// Import returns an error if it finds a comment it cannot understand
   392  	// or finds conflicting comments in multiple source files.
   393  	// See golang.org/s/go14customimport for more information.
   394  	ImportComment
   395  
   396  	// By default, Import searches vendor directories
   397  	// that apply in the given source directory before searching
   398  	// the GOROOT and GOPATH roots.
   399  	// If an Import finds and returns a package using a vendor
   400  	// directory, the resulting ImportPath is the complete path
   401  	// to the package, including the path elements leading up
   402  	// to and including "vendor".
   403  	// For example, if Import("y", "x/subdir", 0) finds
   404  	// "x/vendor/y", the returned package's ImportPath is "x/vendor/y",
   405  	// not plain "y".
   406  	// See golang.org/s/go15vendor for more information.
   407  	//
   408  	// Setting IgnoreVendor ignores vendor directories.
   409  	//
   410  	// In contrast to the package's ImportPath,
   411  	// the returned package's Imports, TestImports, and XTestImports
   412  	// are always the exact import paths from the source files:
   413  	// Import makes no attempt to resolve or check those paths.
   414  	IgnoreVendor
   415  )
   416  
   417  // A Package describes the Go package found in a directory.
   418  type Package struct {
   419  	Dir           string   // directory containing package sources
   420  	Name          string   // package name
   421  	ImportComment string   // path in import comment on package statement
   422  	Doc           string   // documentation synopsis
   423  	ImportPath    string   // import path of package ("" if unknown)
   424  	Root          string   // root of Go tree where this package lives
   425  	SrcRoot       string   // package source root directory ("" if unknown)
   426  	PkgRoot       string   // package install root directory ("" if unknown)
   427  	PkgTargetRoot string   // architecture dependent install root directory ("" if unknown)
   428  	BinDir        string   // command install directory ("" if unknown)
   429  	Goroot        bool     // package found in Go root
   430  	PkgObj        string   // installed .a file
   431  	AllTags       []string // tags that can influence file selection in this directory
   432  	ConflictDir   string   // this directory shadows Dir in $GOPATH
   433  	BinaryOnly    bool     // cannot be rebuilt from source (has //go:binary-only-package comment)
   434  
   435  	// Source files
   436  	GoFiles           []string // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles)
   437  	CgoFiles          []string // .go source files that import "C"
   438  	IgnoredGoFiles    []string // .go source files ignored for this build (including ignored _test.go files)
   439  	InvalidGoFiles    []string // .go source files with detected problems (parse error, wrong package name, and so on)
   440  	IgnoredOtherFiles []string // non-.go source files ignored for this build
   441  	CFiles            []string // .c source files
   442  	CXXFiles          []string // .cc, .cpp and .cxx source files
   443  	MFiles            []string // .m (Objective-C) source files
   444  	HFiles            []string // .h, .hh, .hpp and .hxx source files
   445  	FFiles            []string // .f, .F, .for and .f90 Fortran source files
   446  	SFiles            []string // .s source files
   447  	SwigFiles         []string // .swig files
   448  	SwigCXXFiles      []string // .swigcxx files
   449  	SysoFiles         []string // .syso system object files to add to archive
   450  
   451  	// Cgo directives
   452  	CgoCFLAGS    []string // Cgo CFLAGS directives
   453  	CgoCPPFLAGS  []string // Cgo CPPFLAGS directives
   454  	CgoCXXFLAGS  []string // Cgo CXXFLAGS directives
   455  	CgoFFLAGS    []string // Cgo FFLAGS directives
   456  	CgoLDFLAGS   []string // Cgo LDFLAGS directives
   457  	CgoPkgConfig []string // Cgo pkg-config directives
   458  
   459  	// Test information
   460  	TestGoFiles  []string // _test.go files in package
   461  	XTestGoFiles []string // _test.go files outside package
   462  
   463  	// Dependency information
   464  	Imports        []string                    // import paths from GoFiles, CgoFiles
   465  	ImportPos      map[string][]token.Position // line information for Imports
   466  	TestImports    []string                    // import paths from TestGoFiles
   467  	TestImportPos  map[string][]token.Position // line information for TestImports
   468  	XTestImports   []string                    // import paths from XTestGoFiles
   469  	XTestImportPos map[string][]token.Position // line information for XTestImports
   470  
   471  	// //go:embed patterns found in Go source files
   472  	// For example, if a source file says
   473  	//	//go:embed a* b.c
   474  	// then the list will contain those two strings as separate entries.
   475  	// (See package embed for more details about //go:embed.)
   476  	EmbedPatterns        []string                    // patterns from GoFiles, CgoFiles
   477  	EmbedPatternPos      map[string][]token.Position // line information for EmbedPatterns
   478  	TestEmbedPatterns    []string                    // patterns from TestGoFiles
   479  	TestEmbedPatternPos  map[string][]token.Position // line information for TestEmbedPatterns
   480  	XTestEmbedPatterns   []string                    // patterns from XTestGoFiles
   481  	XTestEmbedPatternPos map[string][]token.Position // line information for XTestEmbedPatternPos
   482  }
   483  
   484  // IsCommand reports whether the package is considered a
   485  // command to be installed (not just a library).
   486  // Packages named "main" are treated as commands.
   487  func (p *Package) IsCommand() bool {
   488  	return p.Name == "main"
   489  }
   490  
   491  // ImportDir is like Import but processes the Go package found in
   492  // the named directory.
   493  func (ctxt *Context) ImportDir(dir string, mode ImportMode) (*Package, error) {
   494  	return ctxt.Import(".", dir, mode)
   495  }
   496  
   497  // NoGoError is the error used by Import to describe a directory
   498  // containing no buildable Go source files. (It may still contain
   499  // test files, files hidden by build tags, and so on.)
   500  type NoGoError struct {
   501  	Dir string
   502  }
   503  
   504  func (e *NoGoError) Error() string {
   505  	return "no buildable Go source files in " + e.Dir
   506  }
   507  
   508  // MultiplePackageError describes a directory containing
   509  // multiple buildable Go source files for multiple packages.
   510  type MultiplePackageError struct {
   511  	Dir      string   // directory containing files
   512  	Packages []string // package names found
   513  	Files    []string // corresponding files: Files[i] declares package Packages[i]
   514  }
   515  
   516  func (e *MultiplePackageError) Error() string {
   517  	// Error string limited to two entries for compatibility.
   518  	return fmt.Sprintf("found packages %s (%s) and %s (%s) in %s", e.Packages[0], e.Files[0], e.Packages[1], e.Files[1], e.Dir)
   519  }
   520  
   521  func nameExt(name string) string {
   522  	i := strings.LastIndex(name, ".")
   523  	if i < 0 {
   524  		return ""
   525  	}
   526  	return name[i:]
   527  }
   528  
   529  // Import returns details about the Go package named by the import path,
   530  // interpreting local import paths relative to the srcDir directory.
   531  // If the path is a local import path naming a package that can be imported
   532  // using a standard import path, the returned package will set p.ImportPath
   533  // to that path.
   534  //
   535  // In the directory containing the package, .go, .c, .h, and .s files are
   536  // considered part of the package except for:
   537  //
   538  //   - .go files in package documentation
   539  //   - files starting with _ or . (likely editor temporary files)
   540  //   - files with build constraints not satisfied by the context
   541  //
   542  // If an error occurs, Import returns a non-nil error and a non-nil
   543  // *Package containing partial information.
   544  func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Package, error) {
   545  	p := &Package{
   546  		ImportPath: path,
   547  	}
   548  	if path == "" {
   549  		return p, fmt.Errorf("import %q: invalid import path", path)
   550  	}
   551  
   552  	var pkgtargetroot string
   553  	var pkga string
   554  	var pkgerr error
   555  	suffix := ""
   556  	if ctxt.InstallSuffix != "" {
   557  		suffix = "_" + ctxt.InstallSuffix
   558  	}
   559  	switch ctxt.Compiler {
   560  	case "gccgo":
   561  		pkgtargetroot = "pkg/gccgo_" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix
   562  	case "gc":
   563  		pkgtargetroot = "pkg/" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix
   564  	default:
   565  		// Save error for end of function.
   566  		pkgerr = fmt.Errorf("import %q: unknown compiler %q", path, ctxt.Compiler)
   567  	}
   568  	setPkga := func() {
   569  		switch ctxt.Compiler {
   570  		case "gccgo":
   571  			dir, elem := pathpkg.Split(p.ImportPath)
   572  			pkga = pkgtargetroot + "/" + dir + "lib" + elem + ".a"
   573  		case "gc":
   574  			pkga = pkgtargetroot + "/" + p.ImportPath + ".a"
   575  		}
   576  	}
   577  	setPkga()
   578  
   579  	binaryOnly := false
   580  	if IsLocalImport(path) {
   581  		pkga = "" // local imports have no installed path
   582  		if srcDir == "" {
   583  			return p, fmt.Errorf("import %q: import relative to unknown directory", path)
   584  		}
   585  		if !ctxt.isAbsPath(path) {
   586  			p.Dir = ctxt.joinPath(srcDir, path)
   587  		}
   588  		// p.Dir directory may or may not exist. Gather partial information first, check if it exists later.
   589  		// Determine canonical import path, if any.
   590  		// Exclude results where the import path would include /testdata/.
   591  		inTestdata := func(sub string) bool {
   592  			return strings.Contains(sub, "/testdata/") || strings.HasSuffix(sub, "/testdata") || strings.HasPrefix(sub, "testdata/") || sub == "testdata"
   593  		}
   594  		if ctxt.GOROOT != "" {
   595  			root := ctxt.joinPath(ctxt.GOROOT, "src")
   596  			if sub, ok := ctxt.hasSubdir(root, p.Dir); ok && !inTestdata(sub) {
   597  				p.Goroot = true
   598  				p.ImportPath = sub
   599  				p.Root = ctxt.GOROOT
   600  				setPkga() // p.ImportPath changed
   601  				goto Found
   602  			}
   603  		}
   604  		all := ctxt.gopath()
   605  		for i, root := range all {
   606  			rootsrc := ctxt.joinPath(root, "src")
   607  			if sub, ok := ctxt.hasSubdir(rootsrc, p.Dir); ok && !inTestdata(sub) {
   608  				// We found a potential import path for dir,
   609  				// but check that using it wouldn't find something
   610  				// else first.
   611  				if ctxt.GOROOT != "" && ctxt.Compiler != "gccgo" {
   612  					if dir := ctxt.joinPath(ctxt.GOROOT, "src", sub); ctxt.isDir(dir) {
   613  						p.ConflictDir = dir
   614  						goto Found
   615  					}
   616  				}
   617  				for _, earlyRoot := range all[:i] {
   618  					if dir := ctxt.joinPath(earlyRoot, "src", sub); ctxt.isDir(dir) {
   619  						p.ConflictDir = dir
   620  						goto Found
   621  					}
   622  				}
   623  
   624  				// sub would not name some other directory instead of this one.
   625  				// Record it.
   626  				p.ImportPath = sub
   627  				p.Root = root
   628  				setPkga() // p.ImportPath changed
   629  				goto Found
   630  			}
   631  		}
   632  		// It's okay that we didn't find a root containing dir.
   633  		// Keep going with the information we have.
   634  	} else {
   635  		if strings.HasPrefix(path, "/") {
   636  			return p, fmt.Errorf("import %q: cannot import absolute path", path)
   637  		}
   638  
   639  		if err := ctxt.importGo(p, path, srcDir, mode); err == nil {
   640  			goto Found
   641  		} else if err != errNoModules {
   642  			return p, err
   643  		}
   644  
   645  		gopath := ctxt.gopath() // needed twice below; avoid computing many times
   646  
   647  		// tried records the location of unsuccessful package lookups
   648  		var tried struct {
   649  			vendor []string
   650  			goroot string
   651  			gopath []string
   652  		}
   653  
   654  		// Vendor directories get first chance to satisfy import.
   655  		if mode&IgnoreVendor == 0 && srcDir != "" {
   656  			searchVendor := func(root string, isGoroot bool) bool {
   657  				sub, ok := ctxt.hasSubdir(root, srcDir)
   658  				if !ok || !strings.HasPrefix(sub, "src/") || strings.Contains(sub, "/testdata/") {
   659  					return false
   660  				}
   661  				for {
   662  					vendor := ctxt.joinPath(root, sub, "vendor")
   663  					if ctxt.isDir(vendor) {
   664  						dir := ctxt.joinPath(vendor, path)
   665  						if ctxt.isDir(dir) && hasGoFiles(ctxt, dir) {
   666  							p.Dir = dir
   667  							p.ImportPath = strings.TrimPrefix(pathpkg.Join(sub, "vendor", path), "src/")
   668  							p.Goroot = isGoroot
   669  							p.Root = root
   670  							setPkga() // p.ImportPath changed
   671  							return true
   672  						}
   673  						tried.vendor = append(tried.vendor, dir)
   674  					}
   675  					i := strings.LastIndex(sub, "/")
   676  					if i < 0 {
   677  						break
   678  					}
   679  					sub = sub[:i]
   680  				}
   681  				return false
   682  			}
   683  			if ctxt.Compiler != "gccgo" && ctxt.GOROOT != "" && searchVendor(ctxt.GOROOT, true) {
   684  				goto Found
   685  			}
   686  			for _, root := range gopath {
   687  				if searchVendor(root, false) {
   688  					goto Found
   689  				}
   690  			}
   691  		}
   692  
   693  		// Determine directory from import path.
   694  		if ctxt.GOROOT != "" {
   695  			// If the package path starts with "vendor/", only search GOROOT before
   696  			// GOPATH if the importer is also within GOROOT. That way, if the user has
   697  			// vendored in a package that is subsequently included in the standard
   698  			// distribution, they'll continue to pick up their own vendored copy.
   699  			gorootFirst := srcDir == "" || !strings.HasPrefix(path, "vendor/")
   700  			if !gorootFirst {
   701  				_, gorootFirst = ctxt.hasSubdir(ctxt.GOROOT, srcDir)
   702  			}
   703  			if gorootFirst {
   704  				dir := ctxt.joinPath(ctxt.GOROOT, "src", path)
   705  				if ctxt.Compiler != "gccgo" {
   706  					isDir := ctxt.isDir(dir)
   707  					binaryOnly = !isDir && mode&AllowBinary != 0 && pkga != "" && ctxt.isFile(ctxt.joinPath(ctxt.GOROOT, pkga))
   708  					if isDir || binaryOnly {
   709  						p.Dir = dir
   710  						p.Goroot = true
   711  						p.Root = ctxt.GOROOT
   712  						goto Found
   713  					}
   714  				}
   715  				tried.goroot = dir
   716  			}
   717  			if ctxt.Compiler == "gccgo" && goroot.IsStandardPackage(ctxt.GOROOT, ctxt.Compiler, path) {
   718  				// TODO(bcmills): Setting p.Dir here is misleading, because gccgo
   719  				// doesn't actually load its standard-library packages from this
   720  				// directory. See if we can leave it unset.
   721  				p.Dir = ctxt.joinPath(ctxt.GOROOT, "src", path)
   722  				p.Goroot = true
   723  				p.Root = ctxt.GOROOT
   724  				goto Found
   725  			}
   726  		}
   727  		for _, root := range gopath {
   728  			dir := ctxt.joinPath(root, "src", path)
   729  			isDir := ctxt.isDir(dir)
   730  			binaryOnly = !isDir && mode&AllowBinary != 0 && pkga != "" && ctxt.isFile(ctxt.joinPath(root, pkga))
   731  			if isDir || binaryOnly {
   732  				p.Dir = dir
   733  				p.Root = root
   734  				goto Found
   735  			}
   736  			tried.gopath = append(tried.gopath, dir)
   737  		}
   738  
   739  		// If we tried GOPATH first due to a "vendor/" prefix, fall back to GOPATH.
   740  		// That way, the user can still get useful results from 'go list' for
   741  		// standard-vendored paths passed on the command line.
   742  		if ctxt.GOROOT != "" && tried.goroot == "" {
   743  			dir := ctxt.joinPath(ctxt.GOROOT, "src", path)
   744  			if ctxt.Compiler != "gccgo" {
   745  				isDir := ctxt.isDir(dir)
   746  				binaryOnly = !isDir && mode&AllowBinary != 0 && pkga != "" && ctxt.isFile(ctxt.joinPath(ctxt.GOROOT, pkga))
   747  				if isDir || binaryOnly {
   748  					p.Dir = dir
   749  					p.Goroot = true
   750  					p.Root = ctxt.GOROOT
   751  					goto Found
   752  				}
   753  			}
   754  			tried.goroot = dir
   755  		}
   756  
   757  		// package was not found
   758  		var paths []string
   759  		format := "\t%s (vendor tree)"
   760  		for _, dir := range tried.vendor {
   761  			paths = append(paths, fmt.Sprintf(format, dir))
   762  			format = "\t%s"
   763  		}
   764  		if tried.goroot != "" {
   765  			paths = append(paths, fmt.Sprintf("\t%s (from $GOROOT)", tried.goroot))
   766  		} else {
   767  			paths = append(paths, "\t($GOROOT not set)")
   768  		}
   769  		format = "\t%s (from $GOPATH)"
   770  		for _, dir := range tried.gopath {
   771  			paths = append(paths, fmt.Sprintf(format, dir))
   772  			format = "\t%s"
   773  		}
   774  		if len(tried.gopath) == 0 {
   775  			paths = append(paths, "\t($GOPATH not set. For more details see: 'go help gopath')")
   776  		}
   777  		return p, fmt.Errorf("cannot find package %q in any of:\n%s", path, strings.Join(paths, "\n"))
   778  	}
   779  
   780  Found:
   781  	if p.Root != "" {
   782  		p.SrcRoot = ctxt.joinPath(p.Root, "src")
   783  		p.PkgRoot = ctxt.joinPath(p.Root, "pkg")
   784  		p.BinDir = ctxt.joinPath(p.Root, "bin")
   785  		if pkga != "" {
   786  			p.PkgTargetRoot = ctxt.joinPath(p.Root, pkgtargetroot)
   787  			p.PkgObj = ctxt.joinPath(p.Root, pkga)
   788  		}
   789  	}
   790  
   791  	// If it's a local import path, by the time we get here, we still haven't checked
   792  	// that p.Dir directory exists. This is the right time to do that check.
   793  	// We can't do it earlier, because we want to gather partial information for the
   794  	// non-nil *Package returned when an error occurs.
   795  	// We need to do this before we return early on FindOnly flag.
   796  	if IsLocalImport(path) && !ctxt.isDir(p.Dir) {
   797  		if ctxt.Compiler == "gccgo" && p.Goroot {
   798  			// gccgo has no sources for GOROOT packages.
   799  			return p, nil
   800  		}
   801  
   802  		// package was not found
   803  		return p, fmt.Errorf("cannot find package %q in:\n\t%s", p.ImportPath, p.Dir)
   804  	}
   805  
   806  	if mode&FindOnly != 0 {
   807  		return p, pkgerr
   808  	}
   809  	if binaryOnly && (mode&AllowBinary) != 0 {
   810  		return p, pkgerr
   811  	}
   812  
   813  	if ctxt.Compiler == "gccgo" && p.Goroot {
   814  		// gccgo has no sources for GOROOT packages.
   815  		return p, nil
   816  	}
   817  
   818  	dirs, err := ctxt.readDir(p.Dir)
   819  	if err != nil {
   820  		return p, err
   821  	}
   822  
   823  	var badGoError error
   824  	badFiles := make(map[string]bool)
   825  	badFile := func(name string, err error) {
   826  		if badGoError == nil {
   827  			badGoError = err
   828  		}
   829  		if !badFiles[name] {
   830  			p.InvalidGoFiles = append(p.InvalidGoFiles, name)
   831  			badFiles[name] = true
   832  		}
   833  	}
   834  
   835  	var Sfiles []string // files with ".S"(capital S)/.sx(capital s equivalent for case insensitive filesystems)
   836  	var firstFile, firstCommentFile string
   837  	embedPos := make(map[string][]token.Position)
   838  	testEmbedPos := make(map[string][]token.Position)
   839  	xTestEmbedPos := make(map[string][]token.Position)
   840  	importPos := make(map[string][]token.Position)
   841  	testImportPos := make(map[string][]token.Position)
   842  	xTestImportPos := make(map[string][]token.Position)
   843  	allTags := make(map[string]bool)
   844  	fset := token.NewFileSet()
   845  	for _, d := range dirs {
   846  		if d.IsDir() {
   847  			continue
   848  		}
   849  		if d.Type() == fs.ModeSymlink {
   850  			if ctxt.isDir(ctxt.joinPath(p.Dir, d.Name())) {
   851  				// Symlinks to directories are not source files.
   852  				continue
   853  			}
   854  		}
   855  
   856  		name := d.Name()
   857  		ext := nameExt(name)
   858  
   859  		info, err := ctxt.matchFile(p.Dir, name, allTags, &p.BinaryOnly, fset)
   860  		if err != nil {
   861  			badFile(name, err)
   862  			continue
   863  		}
   864  		if info == nil {
   865  			if strings.HasPrefix(name, "_") || strings.HasPrefix(name, ".") {
   866  				// not due to build constraints - don't report
   867  			} else if ext == ".go" {
   868  				p.IgnoredGoFiles = append(p.IgnoredGoFiles, name)
   869  			} else if fileListForExt(p, ext) != nil {
   870  				p.IgnoredOtherFiles = append(p.IgnoredOtherFiles, name)
   871  			}
   872  			continue
   873  		}
   874  		data, filename := info.header, info.name
   875  
   876  		// Going to save the file. For non-Go files, can stop here.
   877  		switch ext {
   878  		case ".go":
   879  			// keep going
   880  		case ".S", ".sx":
   881  			// special case for cgo, handled at end
   882  			Sfiles = append(Sfiles, name)
   883  			continue
   884  		default:
   885  			if list := fileListForExt(p, ext); list != nil {
   886  				*list = append(*list, name)
   887  			}
   888  			continue
   889  		}
   890  
   891  		if info.parseErr != nil {
   892  			badFile(name, info.parseErr)
   893  			// Fall through: we might still have a partial AST in info.parsed,
   894  			// and we want to list files with parse errors anyway.
   895  		}
   896  
   897  		var pkg string
   898  		if info.parsed != nil {
   899  			pkg = info.parsed.Name.Name
   900  			if pkg == "documentation" {
   901  				p.IgnoredGoFiles = append(p.IgnoredGoFiles, name)
   902  				continue
   903  			}
   904  		}
   905  
   906  		isTest := strings.HasSuffix(name, "_test.go")
   907  		isXTest := false
   908  		if isTest && strings.HasSuffix(pkg, "_test") && p.Name != pkg {
   909  			isXTest = true
   910  			pkg = pkg[:len(pkg)-len("_test")]
   911  		}
   912  
   913  		if p.Name == "" {
   914  			p.Name = pkg
   915  			firstFile = name
   916  		} else if pkg != p.Name {
   917  			// TODO(#45999): The choice of p.Name is arbitrary based on file iteration
   918  			// order. Instead of resolving p.Name arbitrarily, we should clear out the
   919  			// existing name and mark the existing files as also invalid.
   920  			badFile(name, &MultiplePackageError{
   921  				Dir:      p.Dir,
   922  				Packages: []string{p.Name, pkg},
   923  				Files:    []string{firstFile, name},
   924  			})
   925  		}
   926  		// Grab the first package comment as docs, provided it is not from a test file.
   927  		if info.parsed != nil && info.parsed.Doc != nil && p.Doc == "" && !isTest && !isXTest {
   928  			p.Doc = doc.Synopsis(info.parsed.Doc.Text())
   929  		}
   930  
   931  		if mode&ImportComment != 0 {
   932  			qcom, line := findImportComment(data)
   933  			if line != 0 {
   934  				com, err := strconv.Unquote(qcom)
   935  				if err != nil {
   936  					badFile(name, fmt.Errorf("%s:%d: cannot parse import comment", filename, line))
   937  				} else if p.ImportComment == "" {
   938  					p.ImportComment = com
   939  					firstCommentFile = name
   940  				} else if p.ImportComment != com {
   941  					badFile(name, fmt.Errorf("found import comments %q (%s) and %q (%s) in %s", p.ImportComment, firstCommentFile, com, name, p.Dir))
   942  				}
   943  			}
   944  		}
   945  
   946  		// Record imports and information about cgo.
   947  		isCgo := false
   948  		for _, imp := range info.imports {
   949  			if imp.path == "C" {
   950  				if isTest {
   951  					badFile(name, fmt.Errorf("use of cgo in test %s not supported", filename))
   952  					continue
   953  				}
   954  				isCgo = true
   955  				if imp.doc != nil {
   956  					if err := ctxt.saveCgo(filename, p, imp.doc); err != nil {
   957  						badFile(name, err)
   958  					}
   959  				}
   960  			}
   961  		}
   962  
   963  		var fileList *[]string
   964  		var importMap, embedMap map[string][]token.Position
   965  		switch {
   966  		case isCgo:
   967  			allTags["cgo"] = true
   968  			if ctxt.CgoEnabled {
   969  				fileList = &p.CgoFiles
   970  				importMap = importPos
   971  				embedMap = embedPos
   972  			} else {
   973  				// Ignore imports and embeds from cgo files if cgo is disabled.
   974  				fileList = &p.IgnoredGoFiles
   975  			}
   976  		case isXTest:
   977  			fileList = &p.XTestGoFiles
   978  			importMap = xTestImportPos
   979  			embedMap = xTestEmbedPos
   980  		case isTest:
   981  			fileList = &p.TestGoFiles
   982  			importMap = testImportPos
   983  			embedMap = testEmbedPos
   984  		default:
   985  			fileList = &p.GoFiles
   986  			importMap = importPos
   987  			embedMap = embedPos
   988  		}
   989  		*fileList = append(*fileList, name)
   990  		if importMap != nil {
   991  			for _, imp := range info.imports {
   992  				importMap[imp.path] = append(importMap[imp.path], fset.Position(imp.pos))
   993  			}
   994  		}
   995  		if embedMap != nil {
   996  			for _, emb := range info.embeds {
   997  				embedMap[emb.pattern] = append(embedMap[emb.pattern], emb.pos)
   998  			}
   999  		}
  1000  	}
  1001  
  1002  	for tag := range allTags {
  1003  		p.AllTags = append(p.AllTags, tag)
  1004  	}
  1005  	sort.Strings(p.AllTags)
  1006  
  1007  	p.EmbedPatterns, p.EmbedPatternPos = cleanDecls(embedPos)
  1008  	p.TestEmbedPatterns, p.TestEmbedPatternPos = cleanDecls(testEmbedPos)
  1009  	p.XTestEmbedPatterns, p.XTestEmbedPatternPos = cleanDecls(xTestEmbedPos)
  1010  
  1011  	p.Imports, p.ImportPos = cleanDecls(importPos)
  1012  	p.TestImports, p.TestImportPos = cleanDecls(testImportPos)
  1013  	p.XTestImports, p.XTestImportPos = cleanDecls(xTestImportPos)
  1014  
  1015  	// add the .S/.sx files only if we are using cgo
  1016  	// (which means gcc will compile them).
  1017  	// The standard assemblers expect .s files.
  1018  	if len(p.CgoFiles) > 0 {
  1019  		p.SFiles = append(p.SFiles, Sfiles...)
  1020  		sort.Strings(p.SFiles)
  1021  	} else {
  1022  		p.IgnoredOtherFiles = append(p.IgnoredOtherFiles, Sfiles...)
  1023  		sort.Strings(p.IgnoredOtherFiles)
  1024  	}
  1025  
  1026  	if badGoError != nil {
  1027  		return p, badGoError
  1028  	}
  1029  	if len(p.GoFiles)+len(p.CgoFiles)+len(p.TestGoFiles)+len(p.XTestGoFiles) == 0 {
  1030  		return p, &NoGoError{p.Dir}
  1031  	}
  1032  	return p, pkgerr
  1033  }
  1034  
  1035  func fileListForExt(p *Package, ext string) *[]string {
  1036  	switch ext {
  1037  	case ".c":
  1038  		return &p.CFiles
  1039  	case ".cc", ".cpp", ".cxx":
  1040  		return &p.CXXFiles
  1041  	case ".m":
  1042  		return &p.MFiles
  1043  	case ".h", ".hh", ".hpp", ".hxx":
  1044  		return &p.HFiles
  1045  	case ".f", ".F", ".for", ".f90":
  1046  		return &p.FFiles
  1047  	case ".s", ".S", ".sx":
  1048  		return &p.SFiles
  1049  	case ".swig":
  1050  		return &p.SwigFiles
  1051  	case ".swigcxx":
  1052  		return &p.SwigCXXFiles
  1053  	case ".syso":
  1054  		return &p.SysoFiles
  1055  	}
  1056  	return nil
  1057  }
  1058  
  1059  func uniq(list []string) []string {
  1060  	if list == nil {
  1061  		return nil
  1062  	}
  1063  	out := make([]string, len(list))
  1064  	copy(out, list)
  1065  	sort.Strings(out)
  1066  	uniq := out[:0]
  1067  	for _, x := range out {
  1068  		if len(uniq) == 0 || uniq[len(uniq)-1] != x {
  1069  			uniq = append(uniq, x)
  1070  		}
  1071  	}
  1072  	return uniq
  1073  }
  1074  
  1075  var errNoModules = errors.New("not using modules")
  1076  
  1077  // importGo checks whether it can use the go command to find the directory for path.
  1078  // If using the go command is not appropriate, importGo returns errNoModules.
  1079  // Otherwise, importGo tries using the go command and reports whether that succeeded.
  1080  // Using the go command lets build.Import and build.Context.Import find code
  1081  // in Go modules. In the long term we want tools to use go/packages (currently golang.org/x/tools/go/packages),
  1082  // which will also use the go command.
  1083  // Invoking the go command here is not very efficient in that it computes information
  1084  // about the requested package and all dependencies and then only reports about the requested package.
  1085  // Then we reinvoke it for every dependency. But this is still better than not working at all.
  1086  // See golang.org/issue/26504.
  1087  func (ctxt *Context) importGo(p *Package, path, srcDir string, mode ImportMode) error {
  1088  	// To invoke the go command,
  1089  	// we must not being doing special things like AllowBinary or IgnoreVendor,
  1090  	// and all the file system callbacks must be nil (we're meant to use the local file system).
  1091  	if mode&AllowBinary != 0 || mode&IgnoreVendor != 0 ||
  1092  		ctxt.JoinPath != nil || ctxt.SplitPathList != nil || ctxt.IsAbsPath != nil || ctxt.IsDir != nil || ctxt.HasSubdir != nil || ctxt.ReadDir != nil || ctxt.OpenFile != nil || !equal(ctxt.ToolTags, defaultToolTags) || !equal(ctxt.ReleaseTags, defaultReleaseTags) {
  1093  		return errNoModules
  1094  	}
  1095  
  1096  	// If ctxt.GOROOT is not set, we don't know which go command to invoke,
  1097  	// and even if we did we might return packages in GOROOT that we wouldn't otherwise find
  1098  	// (because we don't know to search in 'go env GOROOT' otherwise).
  1099  	if ctxt.GOROOT == "" {
  1100  		return errNoModules
  1101  	}
  1102  
  1103  	// Predict whether module aware mode is enabled by checking the value of
  1104  	// GO111MODULE and looking for a go.mod file in the source directory or
  1105  	// one of its parents. Running 'go env GOMOD' in the source directory would
  1106  	// give a canonical answer, but we'd prefer not to execute another command.
  1107  	go111Module := os.Getenv("GO111MODULE")
  1108  	switch go111Module {
  1109  	case "off":
  1110  		return errNoModules
  1111  	default: // "", "on", "auto", anything else
  1112  		// Maybe use modules.
  1113  	}
  1114  
  1115  	if srcDir != "" {
  1116  		var absSrcDir string
  1117  		if filepath.IsAbs(srcDir) {
  1118  			absSrcDir = srcDir
  1119  		} else if ctxt.Dir != "" {
  1120  			return fmt.Errorf("go/build: Dir is non-empty, so relative srcDir is not allowed: %v", srcDir)
  1121  		} else {
  1122  			// Find the absolute source directory. hasSubdir does not handle
  1123  			// relative paths (and can't because the callbacks don't support this).
  1124  			var err error
  1125  			absSrcDir, err = filepath.Abs(srcDir)
  1126  			if err != nil {
  1127  				return errNoModules
  1128  			}
  1129  		}
  1130  
  1131  		// If the source directory is in GOROOT, then the in-process code works fine
  1132  		// and we should keep using it. Moreover, the 'go list' approach below doesn't
  1133  		// take standard-library vendoring into account and will fail.
  1134  		if _, ok := ctxt.hasSubdir(filepath.Join(ctxt.GOROOT, "src"), absSrcDir); ok {
  1135  			return errNoModules
  1136  		}
  1137  	}
  1138  
  1139  	// For efficiency, if path is a standard library package, let the usual lookup code handle it.
  1140  	if dir := ctxt.joinPath(ctxt.GOROOT, "src", path); ctxt.isDir(dir) {
  1141  		return errNoModules
  1142  	}
  1143  
  1144  	// If GO111MODULE=auto, look to see if there is a go.mod.
  1145  	// Since go1.13, it doesn't matter if we're inside GOPATH.
  1146  	if go111Module == "auto" {
  1147  		var (
  1148  			parent string
  1149  			err    error
  1150  		)
  1151  		if ctxt.Dir == "" {
  1152  			parent, err = os.Getwd()
  1153  			if err != nil {
  1154  				// A nonexistent working directory can't be in a module.
  1155  				return errNoModules
  1156  			}
  1157  		} else {
  1158  			parent, err = filepath.Abs(ctxt.Dir)
  1159  			if err != nil {
  1160  				// If the caller passed a bogus Dir explicitly, that's materially
  1161  				// different from not having modules enabled.
  1162  				return err
  1163  			}
  1164  		}
  1165  		for {
  1166  			if f, err := ctxt.openFile(ctxt.joinPath(parent, "go.mod")); err == nil {
  1167  				buf := make([]byte, 100)
  1168  				_, err := f.Read(buf)
  1169  				f.Close()
  1170  				if err == nil || err == io.EOF {
  1171  					// go.mod exists and is readable (is a file, not a directory).
  1172  					break
  1173  				}
  1174  			}
  1175  			d := filepath.Dir(parent)
  1176  			if len(d) >= len(parent) {
  1177  				return errNoModules // reached top of file system, no go.mod
  1178  			}
  1179  			parent = d
  1180  		}
  1181  	}
  1182  
  1183  	goCmd := filepath.Join(ctxt.GOROOT, "bin", "go")
  1184  	cmd := exec.Command(goCmd, "list", "-e", "-compiler="+ctxt.Compiler, "-tags="+strings.Join(ctxt.BuildTags, ","), "-installsuffix="+ctxt.InstallSuffix, "-f={{.Dir}}\n{{.ImportPath}}\n{{.Root}}\n{{.Goroot}}\n{{if .Error}}{{.Error}}{{end}}\n", "--", path)
  1185  
  1186  	if ctxt.Dir != "" {
  1187  		cmd.Dir = ctxt.Dir
  1188  	}
  1189  
  1190  	var stdout, stderr strings.Builder
  1191  	cmd.Stdout = &stdout
  1192  	cmd.Stderr = &stderr
  1193  
  1194  	cgo := "0"
  1195  	if ctxt.CgoEnabled {
  1196  		cgo = "1"
  1197  	}
  1198  	cmd.Env = append(cmd.Environ(),
  1199  		"GOOS="+ctxt.GOOS,
  1200  		"GOARCH="+ctxt.GOARCH,
  1201  		"GOROOT="+ctxt.GOROOT,
  1202  		"GOPATH="+ctxt.GOPATH,
  1203  		"CGO_ENABLED="+cgo,
  1204  	)
  1205  
  1206  	if err := cmd.Run(); err != nil {
  1207  		return fmt.Errorf("go/build: go list %s: %v\n%s\n", path, err, stderr.String())
  1208  	}
  1209  
  1210  	f := strings.SplitN(stdout.String(), "\n", 5)
  1211  	if len(f) != 5 {
  1212  		return fmt.Errorf("go/build: importGo %s: unexpected output:\n%s\n", path, stdout.String())
  1213  	}
  1214  	dir := f[0]
  1215  	errStr := strings.TrimSpace(f[4])
  1216  	if errStr != "" && dir == "" {
  1217  		// If 'go list' could not locate the package (dir is empty),
  1218  		// return the same error that 'go list' reported.
  1219  		return errors.New(errStr)
  1220  	}
  1221  
  1222  	// If 'go list' did locate the package, ignore the error.
  1223  	// It was probably related to loading source files, and we'll
  1224  	// encounter it ourselves shortly if the FindOnly flag isn't set.
  1225  	p.Dir = dir
  1226  	p.ImportPath = f[1]
  1227  	p.Root = f[2]
  1228  	p.Goroot = f[3] == "true"
  1229  	return nil
  1230  }
  1231  
  1232  func equal(x, y []string) bool {
  1233  	if len(x) != len(y) {
  1234  		return false
  1235  	}
  1236  	for i, xi := range x {
  1237  		if xi != y[i] {
  1238  			return false
  1239  		}
  1240  	}
  1241  	return true
  1242  }
  1243  
  1244  // hasGoFiles reports whether dir contains any files with names ending in .go.
  1245  // For a vendor check we must exclude directories that contain no .go files.
  1246  // Otherwise it is not possible to vendor just a/b/c and still import the
  1247  // non-vendored a/b. See golang.org/issue/13832.
  1248  func hasGoFiles(ctxt *Context, dir string) bool {
  1249  	ents, _ := ctxt.readDir(dir)
  1250  	for _, ent := range ents {
  1251  		if !ent.IsDir() && strings.HasSuffix(ent.Name(), ".go") {
  1252  			return true
  1253  		}
  1254  	}
  1255  	return false
  1256  }
  1257  
  1258  func findImportComment(data []byte) (s string, line int) {
  1259  	// expect keyword package
  1260  	word, data := parseWord(data)
  1261  	if string(word) != "package" {
  1262  		return "", 0
  1263  	}
  1264  
  1265  	// expect package name
  1266  	_, data = parseWord(data)
  1267  
  1268  	// now ready for import comment, a // or /* */ comment
  1269  	// beginning and ending on the current line.
  1270  	for len(data) > 0 && (data[0] == ' ' || data[0] == '\t' || data[0] == '\r') {
  1271  		data = data[1:]
  1272  	}
  1273  
  1274  	var comment []byte
  1275  	switch {
  1276  	case bytes.HasPrefix(data, slashSlash):
  1277  		comment, _, _ = bytes.Cut(data[2:], newline)
  1278  	case bytes.HasPrefix(data, slashStar):
  1279  		var ok bool
  1280  		comment, _, ok = bytes.Cut(data[2:], starSlash)
  1281  		if !ok {
  1282  			// malformed comment
  1283  			return "", 0
  1284  		}
  1285  		if bytes.Contains(comment, newline) {
  1286  			return "", 0
  1287  		}
  1288  	}
  1289  	comment = bytes.TrimSpace(comment)
  1290  
  1291  	// split comment into `import`, `"pkg"`
  1292  	word, arg := parseWord(comment)
  1293  	if string(word) != "import" {
  1294  		return "", 0
  1295  	}
  1296  
  1297  	line = 1 + bytes.Count(data[:cap(data)-cap(arg)], newline)
  1298  	return strings.TrimSpace(string(arg)), line
  1299  }
  1300  
  1301  var (
  1302  	slashSlash = []byte("//")
  1303  	slashStar  = []byte("/*")
  1304  	starSlash  = []byte("*/")
  1305  	newline    = []byte("\n")
  1306  )
  1307  
  1308  // skipSpaceOrComment returns data with any leading spaces or comments removed.
  1309  func skipSpaceOrComment(data []byte) []byte {
  1310  	for len(data) > 0 {
  1311  		switch data[0] {
  1312  		case ' ', '\t', '\r', '\n':
  1313  			data = data[1:]
  1314  			continue
  1315  		case '/':
  1316  			if bytes.HasPrefix(data, slashSlash) {
  1317  				i := bytes.Index(data, newline)
  1318  				if i < 0 {
  1319  					return nil
  1320  				}
  1321  				data = data[i+1:]
  1322  				continue
  1323  			}
  1324  			if bytes.HasPrefix(data, slashStar) {
  1325  				data = data[2:]
  1326  				i := bytes.Index(data, starSlash)
  1327  				if i < 0 {
  1328  					return nil
  1329  				}
  1330  				data = data[i+2:]
  1331  				continue
  1332  			}
  1333  		}
  1334  		break
  1335  	}
  1336  	return data
  1337  }
  1338  
  1339  // parseWord skips any leading spaces or comments in data
  1340  // and then parses the beginning of data as an identifier or keyword,
  1341  // returning that word and what remains after the word.
  1342  func parseWord(data []byte) (word, rest []byte) {
  1343  	data = skipSpaceOrComment(data)
  1344  
  1345  	// Parse past leading word characters.
  1346  	rest = data
  1347  	for {
  1348  		r, size := utf8.DecodeRune(rest)
  1349  		if unicode.IsLetter(r) || '0' <= r && r <= '9' || r == '_' {
  1350  			rest = rest[size:]
  1351  			continue
  1352  		}
  1353  		break
  1354  	}
  1355  
  1356  	word = data[:len(data)-len(rest)]
  1357  	if len(word) == 0 {
  1358  		return nil, nil
  1359  	}
  1360  
  1361  	return word, rest
  1362  }
  1363  
  1364  // MatchFile reports whether the file with the given name in the given directory
  1365  // matches the context and would be included in a Package created by ImportDir
  1366  // of that directory.
  1367  //
  1368  // MatchFile considers the name of the file and may use ctxt.OpenFile to
  1369  // read some or all of the file's content.
  1370  func (ctxt *Context) MatchFile(dir, name string) (match bool, err error) {
  1371  	info, err := ctxt.matchFile(dir, name, nil, nil, nil)
  1372  	return info != nil, err
  1373  }
  1374  
  1375  var dummyPkg Package
  1376  
  1377  // fileInfo records information learned about a file included in a build.
  1378  type fileInfo struct {
  1379  	name     string // full name including dir
  1380  	header   []byte
  1381  	fset     *token.FileSet
  1382  	parsed   *ast.File
  1383  	parseErr error
  1384  	imports  []fileImport
  1385  	embeds   []fileEmbed
  1386  }
  1387  
  1388  type fileImport struct {
  1389  	path string
  1390  	pos  token.Pos
  1391  	doc  *ast.CommentGroup
  1392  }
  1393  
  1394  type fileEmbed struct {
  1395  	pattern string
  1396  	pos     token.Position
  1397  }
  1398  
  1399  // matchFile determines whether the file with the given name in the given directory
  1400  // should be included in the package being constructed.
  1401  // If the file should be included, matchFile returns a non-nil *fileInfo (and a nil error).
  1402  // Non-nil errors are reserved for unexpected problems.
  1403  //
  1404  // If name denotes a Go program, matchFile reads until the end of the
  1405  // imports and returns that section of the file in the fileInfo's header field,
  1406  // even though it only considers text until the first non-comment
  1407  // for +build lines.
  1408  //
  1409  // If allTags is non-nil, matchFile records any encountered build tag
  1410  // by setting allTags[tag] = true.
  1411  func (ctxt *Context) matchFile(dir, name string, allTags map[string]bool, binaryOnly *bool, fset *token.FileSet) (*fileInfo, error) {
  1412  	if strings.HasPrefix(name, "_") ||
  1413  		strings.HasPrefix(name, ".") {
  1414  		return nil, nil
  1415  	}
  1416  
  1417  	i := strings.LastIndex(name, ".")
  1418  	if i < 0 {
  1419  		i = len(name)
  1420  	}
  1421  	ext := name[i:]
  1422  
  1423  	if !ctxt.goodOSArchFile(name, allTags) && !ctxt.UseAllFiles {
  1424  		return nil, nil
  1425  	}
  1426  
  1427  	if ext != ".go" && fileListForExt(&dummyPkg, ext) == nil {
  1428  		// skip
  1429  		return nil, nil
  1430  	}
  1431  
  1432  	info := &fileInfo{name: ctxt.joinPath(dir, name), fset: fset}
  1433  	if ext == ".syso" {
  1434  		// binary, no reading
  1435  		return info, nil
  1436  	}
  1437  
  1438  	f, err := ctxt.openFile(info.name)
  1439  	if err != nil {
  1440  		return nil, err
  1441  	}
  1442  
  1443  	if strings.HasSuffix(name, ".go") {
  1444  		err = readGoInfo(f, info)
  1445  		if strings.HasSuffix(name, "_test.go") {
  1446  			binaryOnly = nil // ignore //go:binary-only-package comments in _test.go files
  1447  		}
  1448  	} else {
  1449  		binaryOnly = nil // ignore //go:binary-only-package comments in non-Go sources
  1450  		info.header, err = readComments(f)
  1451  	}
  1452  	f.Close()
  1453  	if err != nil {
  1454  		return nil, fmt.Errorf("read %s: %v", info.name, err)
  1455  	}
  1456  
  1457  	// Look for +build comments to accept or reject the file.
  1458  	ok, sawBinaryOnly, err := ctxt.shouldBuild(info.header, allTags)
  1459  	if err != nil {
  1460  		return nil, fmt.Errorf("%s: %v", name, err)
  1461  	}
  1462  	if !ok && !ctxt.UseAllFiles {
  1463  		return nil, nil
  1464  	}
  1465  
  1466  	if binaryOnly != nil && sawBinaryOnly {
  1467  		*binaryOnly = true
  1468  	}
  1469  
  1470  	return info, nil
  1471  }
  1472  
  1473  func cleanDecls(m map[string][]token.Position) ([]string, map[string][]token.Position) {
  1474  	all := make([]string, 0, len(m))
  1475  	for path := range m {
  1476  		all = append(all, path)
  1477  	}
  1478  	sort.Strings(all)
  1479  	return all, m
  1480  }
  1481  
  1482  // Import is shorthand for Default.Import.
  1483  func Import(path, srcDir string, mode ImportMode) (*Package, error) {
  1484  	return Default.Import(path, srcDir, mode)
  1485  }
  1486  
  1487  // ImportDir is shorthand for Default.ImportDir.
  1488  func ImportDir(dir string, mode ImportMode) (*Package, error) {
  1489  	return Default.ImportDir(dir, mode)
  1490  }
  1491  
  1492  var (
  1493  	bSlashSlash = []byte(slashSlash)
  1494  	bStarSlash  = []byte(starSlash)
  1495  	bSlashStar  = []byte(slashStar)
  1496  	bPlusBuild  = []byte("+build")
  1497  
  1498  	goBuildComment = []byte("//go:build")
  1499  
  1500  	errGoBuildWithoutBuild = errors.New("//go:build comment without // +build comment")
  1501  	errMultipleGoBuild     = errors.New("multiple //go:build comments")
  1502  )
  1503  
  1504  func isGoBuildComment(line []byte) bool {
  1505  	if !bytes.HasPrefix(line, goBuildComment) {
  1506  		return false
  1507  	}
  1508  	line = bytes.TrimSpace(line)
  1509  	rest := line[len(goBuildComment):]
  1510  	return len(rest) == 0 || len(bytes.TrimSpace(rest)) < len(rest)
  1511  }
  1512  
  1513  // Special comment denoting a binary-only package.
  1514  // See https://golang.org/design/2775-binary-only-packages
  1515  // for more about the design of binary-only packages.
  1516  var binaryOnlyComment = []byte("//go:binary-only-package")
  1517  
  1518  // shouldBuild reports whether it is okay to use this file,
  1519  // The rule is that in the file's leading run of // comments
  1520  // and blank lines, which must be followed by a blank line
  1521  // (to avoid including a Go package clause doc comment),
  1522  // lines beginning with '// +build' are taken as build directives.
  1523  //
  1524  // The file is accepted only if each such line lists something
  1525  // matching the file. For example:
  1526  //
  1527  //	// +build windows linux
  1528  //
  1529  // marks the file as applicable only on Windows and Linux.
  1530  //
  1531  // For each build tag it consults, shouldBuild sets allTags[tag] = true.
  1532  //
  1533  // shouldBuild reports whether the file should be built
  1534  // and whether a //go:binary-only-package comment was found.
  1535  func (ctxt *Context) shouldBuild(content []byte, allTags map[string]bool) (shouldBuild, binaryOnly bool, err error) {
  1536  	// Identify leading run of // comments and blank lines,
  1537  	// which must be followed by a blank line.
  1538  	// Also identify any //go:build comments.
  1539  	content, goBuild, sawBinaryOnly, err := parseFileHeader(content)
  1540  	if err != nil {
  1541  		return false, false, err
  1542  	}
  1543  
  1544  	// If //go:build line is present, it controls.
  1545  	// Otherwise fall back to +build processing.
  1546  	switch {
  1547  	case goBuild != nil:
  1548  		x, err := constraint.Parse(string(goBuild))
  1549  		if err != nil {
  1550  			return false, false, fmt.Errorf("parsing //go:build line: %v", err)
  1551  		}
  1552  		shouldBuild = ctxt.eval(x, allTags)
  1553  
  1554  	default:
  1555  		shouldBuild = true
  1556  		p := content
  1557  		for len(p) > 0 {
  1558  			line := p
  1559  			if i := bytes.IndexByte(line, '\n'); i >= 0 {
  1560  				line, p = line[:i], p[i+1:]
  1561  			} else {
  1562  				p = p[len(p):]
  1563  			}
  1564  			line = bytes.TrimSpace(line)
  1565  			if !bytes.HasPrefix(line, bSlashSlash) || !bytes.Contains(line, bPlusBuild) {
  1566  				continue
  1567  			}
  1568  			text := string(line)
  1569  			if !constraint.IsPlusBuild(text) {
  1570  				continue
  1571  			}
  1572  			if x, err := constraint.Parse(text); err == nil {
  1573  				if !ctxt.eval(x, allTags) {
  1574  					shouldBuild = false
  1575  				}
  1576  			}
  1577  		}
  1578  	}
  1579  
  1580  	return shouldBuild, sawBinaryOnly, nil
  1581  }
  1582  
  1583  func parseFileHeader(content []byte) (trimmed, goBuild []byte, sawBinaryOnly bool, err error) {
  1584  	end := 0
  1585  	p := content
  1586  	ended := false       // found non-blank, non-// line, so stopped accepting // +build lines
  1587  	inSlashStar := false // in /* */ comment
  1588  
  1589  Lines:
  1590  	for len(p) > 0 {
  1591  		line := p
  1592  		if i := bytes.IndexByte(line, '\n'); i >= 0 {
  1593  			line, p = line[:i], p[i+1:]
  1594  		} else {
  1595  			p = p[len(p):]
  1596  		}
  1597  		line = bytes.TrimSpace(line)
  1598  		if len(line) == 0 && !ended { // Blank line
  1599  			// Remember position of most recent blank line.
  1600  			// When we find the first non-blank, non-// line,
  1601  			// this "end" position marks the latest file position
  1602  			// where a // +build line can appear.
  1603  			// (It must appear _before_ a blank line before the non-blank, non-// line.
  1604  			// Yes, that's confusing, which is part of why we moved to //go:build lines.)
  1605  			// Note that ended==false here means that inSlashStar==false,
  1606  			// since seeing a /* would have set ended==true.
  1607  			end = len(content) - len(p)
  1608  			continue Lines
  1609  		}
  1610  		if !bytes.HasPrefix(line, slashSlash) { // Not comment line
  1611  			ended = true
  1612  		}
  1613  
  1614  		if !inSlashStar && isGoBuildComment(line) {
  1615  			if goBuild != nil {
  1616  				return nil, nil, false, errMultipleGoBuild
  1617  			}
  1618  			goBuild = line
  1619  		}
  1620  		if !inSlashStar && bytes.Equal(line, binaryOnlyComment) {
  1621  			sawBinaryOnly = true
  1622  		}
  1623  
  1624  	Comments:
  1625  		for len(line) > 0 {
  1626  			if inSlashStar {
  1627  				if i := bytes.Index(line, starSlash); i >= 0 {
  1628  					inSlashStar = false
  1629  					line = bytes.TrimSpace(line[i+len(starSlash):])
  1630  					continue Comments
  1631  				}
  1632  				continue Lines
  1633  			}
  1634  			if bytes.HasPrefix(line, bSlashSlash) {
  1635  				continue Lines
  1636  			}
  1637  			if bytes.HasPrefix(line, bSlashStar) {
  1638  				inSlashStar = true
  1639  				line = bytes.TrimSpace(line[len(bSlashStar):])
  1640  				continue Comments
  1641  			}
  1642  			// Found non-comment text.
  1643  			break Lines
  1644  		}
  1645  	}
  1646  
  1647  	return content[:end], goBuild, sawBinaryOnly, nil
  1648  }
  1649  
  1650  // saveCgo saves the information from the #cgo lines in the import "C" comment.
  1651  // These lines set CFLAGS, CPPFLAGS, CXXFLAGS and LDFLAGS and pkg-config directives
  1652  // that affect the way cgo's C code is built.
  1653  func (ctxt *Context) saveCgo(filename string, di *Package, cg *ast.CommentGroup) error {
  1654  	text := cg.Text()
  1655  	for _, line := range strings.Split(text, "\n") {
  1656  		orig := line
  1657  
  1658  		// Line is
  1659  		//	#cgo [GOOS/GOARCH...] LDFLAGS: stuff
  1660  		//
  1661  		line = strings.TrimSpace(line)
  1662  		if len(line) < 5 || line[:4] != "#cgo" || (line[4] != ' ' && line[4] != '\t') {
  1663  			continue
  1664  		}
  1665  
  1666  		// Split at colon.
  1667  		line, argstr, ok := strings.Cut(strings.TrimSpace(line[4:]), ":")
  1668  		if !ok {
  1669  			return fmt.Errorf("%s: invalid #cgo line: %s", filename, orig)
  1670  		}
  1671  
  1672  		// Parse GOOS/GOARCH stuff.
  1673  		f := strings.Fields(line)
  1674  		if len(f) < 1 {
  1675  			return fmt.Errorf("%s: invalid #cgo line: %s", filename, orig)
  1676  		}
  1677  
  1678  		cond, verb := f[:len(f)-1], f[len(f)-1]
  1679  		if len(cond) > 0 {
  1680  			ok := false
  1681  			for _, c := range cond {
  1682  				if ctxt.matchAuto(c, nil) {
  1683  					ok = true
  1684  					break
  1685  				}
  1686  			}
  1687  			if !ok {
  1688  				continue
  1689  			}
  1690  		}
  1691  
  1692  		args, err := splitQuoted(argstr)
  1693  		if err != nil {
  1694  			return fmt.Errorf("%s: invalid #cgo line: %s", filename, orig)
  1695  		}
  1696  		for i, arg := range args {
  1697  			if arg, ok = expandSrcDir(arg, di.Dir); !ok {
  1698  				return fmt.Errorf("%s: malformed #cgo argument: %s", filename, arg)
  1699  			}
  1700  			args[i] = arg
  1701  		}
  1702  
  1703  		switch verb {
  1704  		case "CFLAGS", "CPPFLAGS", "CXXFLAGS", "FFLAGS", "LDFLAGS":
  1705  			// Change relative paths to absolute.
  1706  			ctxt.makePathsAbsolute(args, di.Dir)
  1707  		}
  1708  
  1709  		switch verb {
  1710  		case "CFLAGS":
  1711  			di.CgoCFLAGS = append(di.CgoCFLAGS, args...)
  1712  		case "CPPFLAGS":
  1713  			di.CgoCPPFLAGS = append(di.CgoCPPFLAGS, args...)
  1714  		case "CXXFLAGS":
  1715  			di.CgoCXXFLAGS = append(di.CgoCXXFLAGS, args...)
  1716  		case "FFLAGS":
  1717  			di.CgoFFLAGS = append(di.CgoFFLAGS, args...)
  1718  		case "LDFLAGS":
  1719  			di.CgoLDFLAGS = append(di.CgoLDFLAGS, args...)
  1720  		case "pkg-config":
  1721  			di.CgoPkgConfig = append(di.CgoPkgConfig, args...)
  1722  		default:
  1723  			return fmt.Errorf("%s: invalid #cgo verb: %s", filename, orig)
  1724  		}
  1725  	}
  1726  	return nil
  1727  }
  1728  
  1729  // expandSrcDir expands any occurrence of ${SRCDIR}, making sure
  1730  // the result is safe for the shell.
  1731  func expandSrcDir(str string, srcdir string) (string, bool) {
  1732  	// "\" delimited paths cause safeCgoName to fail
  1733  	// so convert native paths with a different delimiter
  1734  	// to "/" before starting (eg: on windows).
  1735  	srcdir = filepath.ToSlash(srcdir)
  1736  
  1737  	chunks := strings.Split(str, "${SRCDIR}")
  1738  	if len(chunks) < 2 {
  1739  		return str, safeCgoName(str)
  1740  	}
  1741  	ok := true
  1742  	for _, chunk := range chunks {
  1743  		ok = ok && (chunk == "" || safeCgoName(chunk))
  1744  	}
  1745  	ok = ok && (srcdir == "" || safeCgoName(srcdir))
  1746  	res := strings.Join(chunks, srcdir)
  1747  	return res, ok && res != ""
  1748  }
  1749  
  1750  // makePathsAbsolute looks for compiler options that take paths and
  1751  // makes them absolute. We do this because through the 1.8 release we
  1752  // ran the compiler in the package directory, so any relative -I or -L
  1753  // options would be relative to that directory. In 1.9 we changed to
  1754  // running the compiler in the build directory, to get consistent
  1755  // build results (issue #19964). To keep builds working, we change any
  1756  // relative -I or -L options to be absolute.
  1757  //
  1758  // Using filepath.IsAbs and filepath.Join here means the results will be
  1759  // different on different systems, but that's OK: -I and -L options are
  1760  // inherently system-dependent.
  1761  func (ctxt *Context) makePathsAbsolute(args []string, srcDir string) {
  1762  	nextPath := false
  1763  	for i, arg := range args {
  1764  		if nextPath {
  1765  			if !filepath.IsAbs(arg) {
  1766  				args[i] = filepath.Join(srcDir, arg)
  1767  			}
  1768  			nextPath = false
  1769  		} else if strings.HasPrefix(arg, "-I") || strings.HasPrefix(arg, "-L") {
  1770  			if len(arg) == 2 {
  1771  				nextPath = true
  1772  			} else {
  1773  				if !filepath.IsAbs(arg[2:]) {
  1774  					args[i] = arg[:2] + filepath.Join(srcDir, arg[2:])
  1775  				}
  1776  			}
  1777  		}
  1778  	}
  1779  }
  1780  
  1781  // NOTE: $ is not safe for the shell, but it is allowed here because of linker options like -Wl,$ORIGIN.
  1782  // We never pass these arguments to a shell (just to programs we construct argv for), so this should be okay.
  1783  // See golang.org/issue/6038.
  1784  // The @ is for OS X. See golang.org/issue/13720.
  1785  // The % is for Jenkins. See golang.org/issue/16959.
  1786  // The ! is because module paths may use them. See golang.org/issue/26716.
  1787  // The ~ and ^ are for sr.ht. See golang.org/issue/32260.
  1788  const safeString = "+-.,/0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz:$@%! ~^"
  1789  
  1790  func safeCgoName(s string) bool {
  1791  	if s == "" {
  1792  		return false
  1793  	}
  1794  	for i := 0; i < len(s); i++ {
  1795  		if c := s[i]; c < utf8.RuneSelf && strings.IndexByte(safeString, c) < 0 {
  1796  			return false
  1797  		}
  1798  	}
  1799  	return true
  1800  }
  1801  
  1802  // splitQuoted splits the string s around each instance of one or more consecutive
  1803  // white space characters while taking into account quotes and escaping, and
  1804  // returns an array of substrings of s or an empty list if s contains only white space.
  1805  // Single quotes and double quotes are recognized to prevent splitting within the
  1806  // quoted region, and are removed from the resulting substrings. If a quote in s
  1807  // isn't closed err will be set and r will have the unclosed argument as the
  1808  // last element. The backslash is used for escaping.
  1809  //
  1810  // For example, the following string:
  1811  //
  1812  //	a b:"c d" 'e''f'  "g\""
  1813  //
  1814  // Would be parsed as:
  1815  //
  1816  //	[]string{"a", "b:c d", "ef", `g"`}
  1817  func splitQuoted(s string) (r []string, err error) {
  1818  	var args []string
  1819  	arg := make([]rune, len(s))
  1820  	escaped := false
  1821  	quoted := false
  1822  	quote := '\x00'
  1823  	i := 0
  1824  	for _, rune := range s {
  1825  		switch {
  1826  		case escaped:
  1827  			escaped = false
  1828  		case rune == '\\':
  1829  			escaped = true
  1830  			continue
  1831  		case quote != '\x00':
  1832  			if rune == quote {
  1833  				quote = '\x00'
  1834  				continue
  1835  			}
  1836  		case rune == '"' || rune == '\'':
  1837  			quoted = true
  1838  			quote = rune
  1839  			continue
  1840  		case unicode.IsSpace(rune):
  1841  			if quoted || i > 0 {
  1842  				quoted = false
  1843  				args = append(args, string(arg[:i]))
  1844  				i = 0
  1845  			}
  1846  			continue
  1847  		}
  1848  		arg[i] = rune
  1849  		i++
  1850  	}
  1851  	if quoted || i > 0 {
  1852  		args = append(args, string(arg[:i]))
  1853  	}
  1854  	if quote != 0 {
  1855  		err = errors.New("unclosed quote")
  1856  	} else if escaped {
  1857  		err = errors.New("unfinished escaping")
  1858  	}
  1859  	return args, err
  1860  }
  1861  
  1862  // matchAuto interprets text as either a +build or //go:build expression (whichever works),
  1863  // reporting whether the expression matches the build context.
  1864  //
  1865  // matchAuto is only used for testing of tag evaluation
  1866  // and in #cgo lines, which accept either syntax.
  1867  func (ctxt *Context) matchAuto(text string, allTags map[string]bool) bool {
  1868  	if strings.ContainsAny(text, "&|()") {
  1869  		text = "//go:build " + text
  1870  	} else {
  1871  		text = "// +build " + text
  1872  	}
  1873  	x, err := constraint.Parse(text)
  1874  	if err != nil {
  1875  		return false
  1876  	}
  1877  	return ctxt.eval(x, allTags)
  1878  }
  1879  
  1880  func (ctxt *Context) eval(x constraint.Expr, allTags map[string]bool) bool {
  1881  	return x.Eval(func(tag string) bool { return ctxt.matchTag(tag, allTags) })
  1882  }
  1883  
  1884  // matchTag reports whether the name is one of:
  1885  //
  1886  //	cgo (if cgo is enabled)
  1887  //	$GOOS
  1888  //	$GOARCH
  1889  //	ctxt.Compiler
  1890  //	linux (if GOOS = android)
  1891  //	solaris (if GOOS = illumos)
  1892  //	darwin (if GOOS = ios)
  1893  //	unix (if this is a Unix GOOS)
  1894  //	boringcrypto (if GOEXPERIMENT=boringcrypto is enabled)
  1895  //	tag (if tag is listed in ctxt.BuildTags, ctxt.ToolTags, or ctxt.ReleaseTags)
  1896  //
  1897  // It records all consulted tags in allTags.
  1898  func (ctxt *Context) matchTag(name string, allTags map[string]bool) bool {
  1899  	if allTags != nil {
  1900  		allTags[name] = true
  1901  	}
  1902  
  1903  	// special tags
  1904  	if ctxt.CgoEnabled && name == "cgo" {
  1905  		return true
  1906  	}
  1907  	if name == ctxt.GOOS || name == ctxt.GOARCH || name == ctxt.Compiler {
  1908  		return true
  1909  	}
  1910  	if ctxt.GOOS == "android" && name == "linux" {
  1911  		return true
  1912  	}
  1913  	if ctxt.GOOS == "illumos" && name == "solaris" {
  1914  		return true
  1915  	}
  1916  	if ctxt.GOOS == "ios" && name == "darwin" {
  1917  		return true
  1918  	}
  1919  	if name == "unix" && unixOS[ctxt.GOOS] {
  1920  		return true
  1921  	}
  1922  	if name == "boringcrypto" {
  1923  		name = "goexperiment.boringcrypto" // boringcrypto is an old name for goexperiment.boringcrypto
  1924  	}
  1925  
  1926  	// other tags
  1927  	for _, tag := range ctxt.BuildTags {
  1928  		if tag == name {
  1929  			return true
  1930  		}
  1931  	}
  1932  	for _, tag := range ctxt.ToolTags {
  1933  		if tag == name {
  1934  			return true
  1935  		}
  1936  	}
  1937  	for _, tag := range ctxt.ReleaseTags {
  1938  		if tag == name {
  1939  			return true
  1940  		}
  1941  	}
  1942  
  1943  	return false
  1944  }
  1945  
  1946  // goodOSArchFile returns false if the name contains a $GOOS or $GOARCH
  1947  // suffix which does not match the current system.
  1948  // The recognized name formats are:
  1949  //
  1950  //	name_$(GOOS).*
  1951  //	name_$(GOARCH).*
  1952  //	name_$(GOOS)_$(GOARCH).*
  1953  //	name_$(GOOS)_test.*
  1954  //	name_$(GOARCH)_test.*
  1955  //	name_$(GOOS)_$(GOARCH)_test.*
  1956  //
  1957  // Exceptions:
  1958  // if GOOS=android, then files with GOOS=linux are also matched.
  1959  // if GOOS=illumos, then files with GOOS=solaris are also matched.
  1960  // if GOOS=ios, then files with GOOS=darwin are also matched.
  1961  func (ctxt *Context) goodOSArchFile(name string, allTags map[string]bool) bool {
  1962  	name, _, _ = strings.Cut(name, ".")
  1963  
  1964  	// Before Go 1.4, a file called "linux.go" would be equivalent to having a
  1965  	// build tag "linux" in that file. For Go 1.4 and beyond, we require this
  1966  	// auto-tagging to apply only to files with a non-empty prefix, so
  1967  	// "foo_linux.go" is tagged but "linux.go" is not. This allows new operating
  1968  	// systems, such as android, to arrive without breaking existing code with
  1969  	// innocuous source code in "android.go". The easiest fix: cut everything
  1970  	// in the name before the initial _.
  1971  	i := strings.Index(name, "_")
  1972  	if i < 0 {
  1973  		return true
  1974  	}
  1975  	name = name[i:] // ignore everything before first _
  1976  
  1977  	l := strings.Split(name, "_")
  1978  	if n := len(l); n > 0 && l[n-1] == "test" {
  1979  		l = l[:n-1]
  1980  	}
  1981  	n := len(l)
  1982  	if n >= 2 && knownOS[l[n-2]] && knownArch[l[n-1]] {
  1983  		if allTags != nil {
  1984  			// In case we short-circuit on l[n-1].
  1985  			allTags[l[n-2]] = true
  1986  		}
  1987  		return ctxt.matchTag(l[n-1], allTags) && ctxt.matchTag(l[n-2], allTags)
  1988  	}
  1989  	if n >= 1 && (knownOS[l[n-1]] || knownArch[l[n-1]]) {
  1990  		return ctxt.matchTag(l[n-1], allTags)
  1991  	}
  1992  	return true
  1993  }
  1994  
  1995  // ToolDir is the directory containing build tools.
  1996  var ToolDir = getToolDir()
  1997  
  1998  // IsLocalImport reports whether the import path is
  1999  // a local import path, like ".", "..", "./foo", or "../foo".
  2000  func IsLocalImport(path string) bool {
  2001  	return path == "." || path == ".." ||
  2002  		strings.HasPrefix(path, "./") || strings.HasPrefix(path, "../")
  2003  }
  2004  
  2005  // ArchChar returns "?" and an error.
  2006  // In earlier versions of Go, the returned string was used to derive
  2007  // the compiler and linker tool names, the default object file suffix,
  2008  // and the default linker output name. As of Go 1.5, those strings
  2009  // no longer vary by architecture; they are compile, link, .o, and a.out, respectively.
  2010  func ArchChar(goarch string) (string, error) {
  2011  	return "?", errors.New("architecture letter no longer used")
  2012  }
  2013  

View as plain text