github.com/mattn/go@v0.0.0-20171011075504-07f7db3ea99f/src/go/build/build.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 build
     6  
     7  import (
     8  	"bytes"
     9  	"errors"
    10  	"fmt"
    11  	"go/ast"
    12  	"go/doc"
    13  	"go/parser"
    14  	"go/token"
    15  	"io"
    16  	"io/ioutil"
    17  	"log"
    18  	"os"
    19  	pathpkg "path"
    20  	"path/filepath"
    21  	"runtime"
    22  	"sort"
    23  	"strconv"
    24  	"strings"
    25  	"unicode"
    26  	"unicode/utf8"
    27  )
    28  
    29  // A Context specifies the supporting context for a build.
    30  type Context struct {
    31  	GOARCH      string // target architecture
    32  	GOOS        string // target operating system
    33  	GOROOT      string // Go root
    34  	GOPATH      string // Go path
    35  	CgoEnabled  bool   // whether cgo can be used
    36  	UseAllFiles bool   // use files regardless of +build lines, file names
    37  	Compiler    string // compiler to assume when computing target paths
    38  
    39  	// The build and release tags specify build constraints
    40  	// that should be considered satisfied when processing +build lines.
    41  	// Clients creating a new context may customize BuildTags, which
    42  	// defaults to empty, but it is usually an error to customize ReleaseTags,
    43  	// which defaults to the list of Go releases the current release is compatible with.
    44  	// In addition to the BuildTags and ReleaseTags, build constraints
    45  	// consider the values of GOARCH and GOOS as satisfied tags.
    46  	BuildTags   []string
    47  	ReleaseTags []string
    48  
    49  	// The install suffix specifies a suffix to use in the name of the installation
    50  	// directory. By default it is empty, but custom builds that need to keep
    51  	// their outputs separate can set InstallSuffix to do so. For example, when
    52  	// using the race detector, the go command uses InstallSuffix = "race", so
    53  	// that on a Linux/386 system, packages are written to a directory named
    54  	// "linux_386_race" instead of the usual "linux_386".
    55  	InstallSuffix string
    56  
    57  	// By default, Import uses the operating system's file system calls
    58  	// to read directories and files. To read from other sources,
    59  	// callers can set the following functions. They all have default
    60  	// behaviors that use the local file system, so clients need only set
    61  	// the functions whose behaviors they wish to change.
    62  
    63  	// JoinPath joins the sequence of path fragments into a single path.
    64  	// If JoinPath is nil, Import uses filepath.Join.
    65  	JoinPath func(elem ...string) string
    66  
    67  	// SplitPathList splits the path list into a slice of individual paths.
    68  	// If SplitPathList is nil, Import uses filepath.SplitList.
    69  	SplitPathList func(list string) []string
    70  
    71  	// IsAbsPath reports whether path is an absolute path.
    72  	// If IsAbsPath is nil, Import uses filepath.IsAbs.
    73  	IsAbsPath func(path string) bool
    74  
    75  	// IsDir reports whether the path names a directory.
    76  	// If IsDir is nil, Import calls os.Stat and uses the result's IsDir method.
    77  	IsDir func(path string) bool
    78  
    79  	// HasSubdir reports whether dir is lexically a subdirectory of
    80  	// root, perhaps multiple levels below. It does not try to check
    81  	// whether dir exists.
    82  	// If so, HasSubdir sets rel to a slash-separated path that
    83  	// can be joined to root to produce a path equivalent to dir.
    84  	// If HasSubdir is nil, Import uses an implementation built on
    85  	// filepath.EvalSymlinks.
    86  	HasSubdir func(root, dir string) (rel string, ok bool)
    87  
    88  	// ReadDir returns a slice of os.FileInfo, sorted by Name,
    89  	// describing the content of the named directory.
    90  	// If ReadDir is nil, Import uses ioutil.ReadDir.
    91  	ReadDir func(dir string) ([]os.FileInfo, error)
    92  
    93  	// OpenFile opens a file (not a directory) for reading.
    94  	// If OpenFile is nil, Import uses os.Open.
    95  	OpenFile func(path string) (io.ReadCloser, error)
    96  }
    97  
    98  // joinPath calls ctxt.JoinPath (if not nil) or else filepath.Join.
    99  func (ctxt *Context) joinPath(elem ...string) string {
   100  	if f := ctxt.JoinPath; f != nil {
   101  		return f(elem...)
   102  	}
   103  	return filepath.Join(elem...)
   104  }
   105  
   106  // splitPathList calls ctxt.SplitPathList (if not nil) or else filepath.SplitList.
   107  func (ctxt *Context) splitPathList(s string) []string {
   108  	if f := ctxt.SplitPathList; f != nil {
   109  		return f(s)
   110  	}
   111  	return filepath.SplitList(s)
   112  }
   113  
   114  // isAbsPath calls ctxt.IsAbsPath (if not nil) or else filepath.IsAbs.
   115  func (ctxt *Context) isAbsPath(path string) bool {
   116  	if f := ctxt.IsAbsPath; f != nil {
   117  		return f(path)
   118  	}
   119  	return filepath.IsAbs(path)
   120  }
   121  
   122  // isDir calls ctxt.IsDir (if not nil) or else uses os.Stat.
   123  func (ctxt *Context) isDir(path string) bool {
   124  	if f := ctxt.IsDir; f != nil {
   125  		return f(path)
   126  	}
   127  	fi, err := os.Stat(path)
   128  	return err == nil && fi.IsDir()
   129  }
   130  
   131  // hasSubdir calls ctxt.HasSubdir (if not nil) or else uses
   132  // the local file system to answer the question.
   133  func (ctxt *Context) hasSubdir(root, dir string) (rel string, ok bool) {
   134  	if f := ctxt.HasSubdir; f != nil {
   135  		return f(root, dir)
   136  	}
   137  
   138  	// Try using paths we received.
   139  	if rel, ok = hasSubdir(root, dir); ok {
   140  		return
   141  	}
   142  
   143  	// Try expanding symlinks and comparing
   144  	// expanded against unexpanded and
   145  	// expanded against expanded.
   146  	rootSym, _ := filepath.EvalSymlinks(root)
   147  	dirSym, _ := filepath.EvalSymlinks(dir)
   148  
   149  	if rel, ok = hasSubdir(rootSym, dir); ok {
   150  		return
   151  	}
   152  	if rel, ok = hasSubdir(root, dirSym); ok {
   153  		return
   154  	}
   155  	return hasSubdir(rootSym, dirSym)
   156  }
   157  
   158  // hasSubdir reports if dir is within root by performing lexical analysis only.
   159  func hasSubdir(root, dir string) (rel string, ok bool) {
   160  	const sep = string(filepath.Separator)
   161  	root = filepath.Clean(root)
   162  	if !strings.HasSuffix(root, sep) {
   163  		root += sep
   164  	}
   165  	dir = filepath.Clean(dir)
   166  	if !strings.HasPrefix(dir, root) {
   167  		return "", false
   168  	}
   169  	return filepath.ToSlash(dir[len(root):]), true
   170  }
   171  
   172  // readDir calls ctxt.ReadDir (if not nil) or else ioutil.ReadDir.
   173  func (ctxt *Context) readDir(path string) ([]os.FileInfo, error) {
   174  	if f := ctxt.ReadDir; f != nil {
   175  		return f(path)
   176  	}
   177  	return ioutil.ReadDir(path)
   178  }
   179  
   180  // openFile calls ctxt.OpenFile (if not nil) or else os.Open.
   181  func (ctxt *Context) openFile(path string) (io.ReadCloser, error) {
   182  	if fn := ctxt.OpenFile; fn != nil {
   183  		return fn(path)
   184  	}
   185  
   186  	f, err := os.Open(path)
   187  	if err != nil {
   188  		return nil, err // nil interface
   189  	}
   190  	return f, nil
   191  }
   192  
   193  // isFile determines whether path is a file by trying to open it.
   194  // It reuses openFile instead of adding another function to the
   195  // list in Context.
   196  func (ctxt *Context) isFile(path string) bool {
   197  	f, err := ctxt.openFile(path)
   198  	if err != nil {
   199  		return false
   200  	}
   201  	f.Close()
   202  	return true
   203  }
   204  
   205  // gopath returns the list of Go path directories.
   206  func (ctxt *Context) gopath() []string {
   207  	var all []string
   208  	for _, p := range ctxt.splitPathList(ctxt.GOPATH) {
   209  		if p == "" || p == ctxt.GOROOT {
   210  			// Empty paths are uninteresting.
   211  			// If the path is the GOROOT, ignore it.
   212  			// People sometimes set GOPATH=$GOROOT.
   213  			// Do not get confused by this common mistake.
   214  			continue
   215  		}
   216  		if strings.HasPrefix(p, "~") {
   217  			// Path segments starting with ~ on Unix are almost always
   218  			// users who have incorrectly quoted ~ while setting GOPATH,
   219  			// preventing it from expanding to $HOME.
   220  			// The situation is made more confusing by the fact that
   221  			// bash allows quoted ~ in $PATH (most shells do not).
   222  			// Do not get confused by this, and do not try to use the path.
   223  			// It does not exist, and printing errors about it confuses
   224  			// those users even more, because they think "sure ~ exists!".
   225  			// The go command diagnoses this situation and prints a
   226  			// useful error.
   227  			// On Windows, ~ is used in short names, such as c:\progra~1
   228  			// for c:\program files.
   229  			continue
   230  		}
   231  		all = append(all, p)
   232  	}
   233  	return all
   234  }
   235  
   236  // SrcDirs returns a list of package source root directories.
   237  // It draws from the current Go root and Go path but omits directories
   238  // that do not exist.
   239  func (ctxt *Context) SrcDirs() []string {
   240  	var all []string
   241  	if ctxt.GOROOT != "" {
   242  		dir := ctxt.joinPath(ctxt.GOROOT, "src")
   243  		if ctxt.isDir(dir) {
   244  			all = append(all, dir)
   245  		}
   246  	}
   247  	for _, p := range ctxt.gopath() {
   248  		dir := ctxt.joinPath(p, "src")
   249  		if ctxt.isDir(dir) {
   250  			all = append(all, dir)
   251  		}
   252  	}
   253  	return all
   254  }
   255  
   256  // Default is the default Context for builds.
   257  // It uses the GOARCH, GOOS, GOROOT, and GOPATH environment variables
   258  // if set, or else the compiled code's GOARCH, GOOS, and GOROOT.
   259  var Default Context = defaultContext()
   260  
   261  func defaultGOPATH() string {
   262  	env := "HOME"
   263  	if runtime.GOOS == "windows" {
   264  		env = "USERPROFILE"
   265  	} else if runtime.GOOS == "plan9" {
   266  		env = "home"
   267  	}
   268  	if home := os.Getenv(env); home != "" {
   269  		def := filepath.Join(home, "go")
   270  		if filepath.Clean(def) == filepath.Clean(runtime.GOROOT()) {
   271  			// Don't set the default GOPATH to GOROOT,
   272  			// as that will trigger warnings from the go tool.
   273  			return ""
   274  		}
   275  		return def
   276  	}
   277  	return ""
   278  }
   279  
   280  func defaultContext() Context {
   281  	var c Context
   282  
   283  	c.GOARCH = envOr("GOARCH", runtime.GOARCH)
   284  	c.GOOS = envOr("GOOS", runtime.GOOS)
   285  	c.GOROOT = pathpkg.Clean(runtime.GOROOT())
   286  	c.GOPATH = envOr("GOPATH", defaultGOPATH())
   287  	c.Compiler = runtime.Compiler
   288  
   289  	// Each major Go release in the Go 1.x series should add a tag here.
   290  	// Old tags should not be removed. That is, the go1.x tag is present
   291  	// in all releases >= Go 1.x. Code that requires Go 1.x or later should
   292  	// say "+build go1.x", and code that should only be built before Go 1.x
   293  	// (perhaps it is the stub to use in that case) should say "+build !go1.x".
   294  	// NOTE: If you add to this list, also update the doc comment in doc.go.
   295  	const version = 10 // go1.10
   296  	for i := 1; i <= version; i++ {
   297  		c.ReleaseTags = append(c.ReleaseTags, "go1."+strconv.Itoa(i))
   298  	}
   299  
   300  	env := os.Getenv("CGO_ENABLED")
   301  	if env == "" {
   302  		env = defaultCGO_ENABLED
   303  	}
   304  	switch env {
   305  	case "1":
   306  		c.CgoEnabled = true
   307  	case "0":
   308  		c.CgoEnabled = false
   309  	default:
   310  		// cgo must be explicitly enabled for cross compilation builds
   311  		if runtime.GOARCH == c.GOARCH && runtime.GOOS == c.GOOS {
   312  			c.CgoEnabled = cgoEnabled[c.GOOS+"/"+c.GOARCH]
   313  			break
   314  		}
   315  		c.CgoEnabled = false
   316  	}
   317  
   318  	return c
   319  }
   320  
   321  func envOr(name, def string) string {
   322  	s := os.Getenv(name)
   323  	if s == "" {
   324  		return def
   325  	}
   326  	return s
   327  }
   328  
   329  // An ImportMode controls the behavior of the Import method.
   330  type ImportMode uint
   331  
   332  const (
   333  	// If FindOnly is set, Import stops after locating the directory
   334  	// that should contain the sources for a package. It does not
   335  	// read any files in the directory.
   336  	FindOnly ImportMode = 1 << iota
   337  
   338  	// If AllowBinary is set, Import can be satisfied by a compiled
   339  	// package object without corresponding sources.
   340  	//
   341  	// Deprecated:
   342  	// The supported way to create a compiled-only package is to
   343  	// write source code containing a //go:binary-only-package comment at
   344  	// the top of the file. Such a package will be recognized
   345  	// regardless of this flag setting (because it has source code)
   346  	// and will have BinaryOnly set to true in the returned Package.
   347  	AllowBinary
   348  
   349  	// If ImportComment is set, parse import comments on package statements.
   350  	// Import returns an error if it finds a comment it cannot understand
   351  	// or finds conflicting comments in multiple source files.
   352  	// See golang.org/s/go14customimport for more information.
   353  	ImportComment
   354  
   355  	// By default, Import searches vendor directories
   356  	// that apply in the given source directory before searching
   357  	// the GOROOT and GOPATH roots.
   358  	// If an Import finds and returns a package using a vendor
   359  	// directory, the resulting ImportPath is the complete path
   360  	// to the package, including the path elements leading up
   361  	// to and including "vendor".
   362  	// For example, if Import("y", "x/subdir", 0) finds
   363  	// "x/vendor/y", the returned package's ImportPath is "x/vendor/y",
   364  	// not plain "y".
   365  	// See golang.org/s/go15vendor for more information.
   366  	//
   367  	// Setting IgnoreVendor ignores vendor directories.
   368  	//
   369  	// In contrast to the package's ImportPath,
   370  	// the returned package's Imports, TestImports, and XTestImports
   371  	// are always the exact import paths from the source files:
   372  	// Import makes no attempt to resolve or check those paths.
   373  	IgnoreVendor
   374  )
   375  
   376  // A Package describes the Go package found in a directory.
   377  type Package struct {
   378  	Dir           string   // directory containing package sources
   379  	Name          string   // package name
   380  	ImportComment string   // path in import comment on package statement
   381  	Doc           string   // documentation synopsis
   382  	ImportPath    string   // import path of package ("" if unknown)
   383  	Root          string   // root of Go tree where this package lives
   384  	SrcRoot       string   // package source root directory ("" if unknown)
   385  	PkgRoot       string   // package install root directory ("" if unknown)
   386  	PkgTargetRoot string   // architecture dependent install root directory ("" if unknown)
   387  	BinDir        string   // command install directory ("" if unknown)
   388  	Goroot        bool     // package found in Go root
   389  	PkgObj        string   // installed .a file
   390  	AllTags       []string // tags that can influence file selection in this directory
   391  	ConflictDir   string   // this directory shadows Dir in $GOPATH
   392  	BinaryOnly    bool     // cannot be rebuilt from source (has //go:binary-only-package comment)
   393  
   394  	// Source files
   395  	GoFiles        []string // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles)
   396  	CgoFiles       []string // .go source files that import "C"
   397  	IgnoredGoFiles []string // .go source files ignored for this build
   398  	InvalidGoFiles []string // .go source files with detected problems (parse error, wrong package name, and so on)
   399  	CFiles         []string // .c source files
   400  	CXXFiles       []string // .cc, .cpp and .cxx source files
   401  	MFiles         []string // .m (Objective-C) source files
   402  	HFiles         []string // .h, .hh, .hpp and .hxx source files
   403  	FFiles         []string // .f, .F, .for and .f90 Fortran source files
   404  	SFiles         []string // .s source files
   405  	SwigFiles      []string // .swig files
   406  	SwigCXXFiles   []string // .swigcxx files
   407  	SysoFiles      []string // .syso system object files to add to archive
   408  
   409  	// Cgo directives
   410  	CgoCFLAGS    []string // Cgo CFLAGS directives
   411  	CgoCPPFLAGS  []string // Cgo CPPFLAGS directives
   412  	CgoCXXFLAGS  []string // Cgo CXXFLAGS directives
   413  	CgoFFLAGS    []string // Cgo FFLAGS directives
   414  	CgoLDFLAGS   []string // Cgo LDFLAGS directives
   415  	CgoPkgConfig []string // Cgo pkg-config directives
   416  
   417  	// Dependency information
   418  	Imports   []string                    // import paths from GoFiles, CgoFiles
   419  	ImportPos map[string][]token.Position // line information for Imports
   420  
   421  	// Test information
   422  	TestGoFiles    []string                    // _test.go files in package
   423  	TestImports    []string                    // import paths from TestGoFiles
   424  	TestImportPos  map[string][]token.Position // line information for TestImports
   425  	XTestGoFiles   []string                    // _test.go files outside package
   426  	XTestImports   []string                    // import paths from XTestGoFiles
   427  	XTestImportPos map[string][]token.Position // line information for XTestImports
   428  }
   429  
   430  // IsCommand reports whether the package is considered a
   431  // command to be installed (not just a library).
   432  // Packages named "main" are treated as commands.
   433  func (p *Package) IsCommand() bool {
   434  	return p.Name == "main"
   435  }
   436  
   437  // ImportDir is like Import but processes the Go package found in
   438  // the named directory.
   439  func (ctxt *Context) ImportDir(dir string, mode ImportMode) (*Package, error) {
   440  	return ctxt.Import(".", dir, mode)
   441  }
   442  
   443  // NoGoError is the error used by Import to describe a directory
   444  // containing no buildable Go source files. (It may still contain
   445  // test files, files hidden by build tags, and so on.)
   446  type NoGoError struct {
   447  	Dir string
   448  }
   449  
   450  func (e *NoGoError) Error() string {
   451  	return "no buildable Go source files in " + e.Dir
   452  }
   453  
   454  // MultiplePackageError describes a directory containing
   455  // multiple buildable Go source files for multiple packages.
   456  type MultiplePackageError struct {
   457  	Dir      string   // directory containing files
   458  	Packages []string // package names found
   459  	Files    []string // corresponding files: Files[i] declares package Packages[i]
   460  }
   461  
   462  func (e *MultiplePackageError) Error() string {
   463  	// Error string limited to two entries for compatibility.
   464  	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)
   465  }
   466  
   467  func nameExt(name string) string {
   468  	i := strings.LastIndex(name, ".")
   469  	if i < 0 {
   470  		return ""
   471  	}
   472  	return name[i:]
   473  }
   474  
   475  // Import returns details about the Go package named by the import path,
   476  // interpreting local import paths relative to the srcDir directory.
   477  // If the path is a local import path naming a package that can be imported
   478  // using a standard import path, the returned package will set p.ImportPath
   479  // to that path.
   480  //
   481  // In the directory containing the package, .go, .c, .h, and .s files are
   482  // considered part of the package except for:
   483  //
   484  //	- .go files in package documentation
   485  //	- files starting with _ or . (likely editor temporary files)
   486  //	- files with build constraints not satisfied by the context
   487  //
   488  // If an error occurs, Import returns a non-nil error and a non-nil
   489  // *Package containing partial information.
   490  //
   491  func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Package, error) {
   492  	p := &Package{
   493  		ImportPath: path,
   494  	}
   495  	if path == "" {
   496  		return p, fmt.Errorf("import %q: invalid import path", path)
   497  	}
   498  
   499  	var pkgtargetroot string
   500  	var pkga string
   501  	var pkgerr error
   502  	suffix := ""
   503  	if ctxt.InstallSuffix != "" {
   504  		suffix = "_" + ctxt.InstallSuffix
   505  	}
   506  	switch ctxt.Compiler {
   507  	case "gccgo":
   508  		pkgtargetroot = "pkg/gccgo_" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix
   509  	case "gc":
   510  		pkgtargetroot = "pkg/" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix
   511  	default:
   512  		// Save error for end of function.
   513  		pkgerr = fmt.Errorf("import %q: unknown compiler %q", path, ctxt.Compiler)
   514  	}
   515  	setPkga := func() {
   516  		switch ctxt.Compiler {
   517  		case "gccgo":
   518  			dir, elem := pathpkg.Split(p.ImportPath)
   519  			pkga = pkgtargetroot + "/" + dir + "lib" + elem + ".a"
   520  		case "gc":
   521  			pkga = pkgtargetroot + "/" + p.ImportPath + ".a"
   522  		}
   523  	}
   524  	setPkga()
   525  
   526  	binaryOnly := false
   527  	if IsLocalImport(path) {
   528  		pkga = "" // local imports have no installed path
   529  		if srcDir == "" {
   530  			return p, fmt.Errorf("import %q: import relative to unknown directory", path)
   531  		}
   532  		if !ctxt.isAbsPath(path) {
   533  			p.Dir = ctxt.joinPath(srcDir, path)
   534  		}
   535  		// p.Dir directory may or may not exist. Gather partial information first, check if it exists later.
   536  		// Determine canonical import path, if any.
   537  		// Exclude results where the import path would include /testdata/.
   538  		inTestdata := func(sub string) bool {
   539  			return strings.Contains(sub, "/testdata/") || strings.HasSuffix(sub, "/testdata") || strings.HasPrefix(sub, "testdata/") || sub == "testdata"
   540  		}
   541  		if ctxt.GOROOT != "" {
   542  			root := ctxt.joinPath(ctxt.GOROOT, "src")
   543  			if sub, ok := ctxt.hasSubdir(root, p.Dir); ok && !inTestdata(sub) {
   544  				p.Goroot = true
   545  				p.ImportPath = sub
   546  				p.Root = ctxt.GOROOT
   547  				goto Found
   548  			}
   549  		}
   550  		all := ctxt.gopath()
   551  		for i, root := range all {
   552  			rootsrc := ctxt.joinPath(root, "src")
   553  			if sub, ok := ctxt.hasSubdir(rootsrc, p.Dir); ok && !inTestdata(sub) {
   554  				// We found a potential import path for dir,
   555  				// but check that using it wouldn't find something
   556  				// else first.
   557  				if ctxt.GOROOT != "" {
   558  					if dir := ctxt.joinPath(ctxt.GOROOT, "src", sub); ctxt.isDir(dir) {
   559  						p.ConflictDir = dir
   560  						goto Found
   561  					}
   562  				}
   563  				for _, earlyRoot := range all[:i] {
   564  					if dir := ctxt.joinPath(earlyRoot, "src", sub); ctxt.isDir(dir) {
   565  						p.ConflictDir = dir
   566  						goto Found
   567  					}
   568  				}
   569  
   570  				// sub would not name some other directory instead of this one.
   571  				// Record it.
   572  				p.ImportPath = sub
   573  				p.Root = root
   574  				goto Found
   575  			}
   576  		}
   577  		// It's okay that we didn't find a root containing dir.
   578  		// Keep going with the information we have.
   579  	} else {
   580  		if strings.HasPrefix(path, "/") {
   581  			return p, fmt.Errorf("import %q: cannot import absolute path", path)
   582  		}
   583  
   584  		// tried records the location of unsuccessful package lookups
   585  		var tried struct {
   586  			vendor []string
   587  			goroot string
   588  			gopath []string
   589  		}
   590  		gopath := ctxt.gopath()
   591  
   592  		// Vendor directories get first chance to satisfy import.
   593  		if mode&IgnoreVendor == 0 && srcDir != "" {
   594  			searchVendor := func(root string, isGoroot bool) bool {
   595  				sub, ok := ctxt.hasSubdir(root, srcDir)
   596  				if !ok || !strings.HasPrefix(sub, "src/") || strings.Contains(sub, "/testdata/") {
   597  					return false
   598  				}
   599  				for {
   600  					vendor := ctxt.joinPath(root, sub, "vendor")
   601  					if ctxt.isDir(vendor) {
   602  						dir := ctxt.joinPath(vendor, path)
   603  						if ctxt.isDir(dir) && hasGoFiles(ctxt, dir) {
   604  							p.Dir = dir
   605  							p.ImportPath = strings.TrimPrefix(pathpkg.Join(sub, "vendor", path), "src/")
   606  							p.Goroot = isGoroot
   607  							p.Root = root
   608  							setPkga() // p.ImportPath changed
   609  							return true
   610  						}
   611  						tried.vendor = append(tried.vendor, dir)
   612  					}
   613  					i := strings.LastIndex(sub, "/")
   614  					if i < 0 {
   615  						break
   616  					}
   617  					sub = sub[:i]
   618  				}
   619  				return false
   620  			}
   621  			if searchVendor(ctxt.GOROOT, true) {
   622  				goto Found
   623  			}
   624  			for _, root := range gopath {
   625  				if searchVendor(root, false) {
   626  					goto Found
   627  				}
   628  			}
   629  		}
   630  
   631  		// Determine directory from import path.
   632  		if ctxt.GOROOT != "" {
   633  			dir := ctxt.joinPath(ctxt.GOROOT, "src", path)
   634  			isDir := ctxt.isDir(dir)
   635  			binaryOnly = !isDir && mode&AllowBinary != 0 && pkga != "" && ctxt.isFile(ctxt.joinPath(ctxt.GOROOT, pkga))
   636  			if isDir || binaryOnly {
   637  				p.Dir = dir
   638  				p.Goroot = true
   639  				p.Root = ctxt.GOROOT
   640  				goto Found
   641  			}
   642  			tried.goroot = dir
   643  		}
   644  		for _, root := range gopath {
   645  			dir := ctxt.joinPath(root, "src", path)
   646  			isDir := ctxt.isDir(dir)
   647  			binaryOnly = !isDir && mode&AllowBinary != 0 && pkga != "" && ctxt.isFile(ctxt.joinPath(root, pkga))
   648  			if isDir || binaryOnly {
   649  				p.Dir = dir
   650  				p.Root = root
   651  				goto Found
   652  			}
   653  			tried.gopath = append(tried.gopath, dir)
   654  		}
   655  
   656  		// package was not found
   657  		var paths []string
   658  		format := "\t%s (vendor tree)"
   659  		for _, dir := range tried.vendor {
   660  			paths = append(paths, fmt.Sprintf(format, dir))
   661  			format = "\t%s"
   662  		}
   663  		if tried.goroot != "" {
   664  			paths = append(paths, fmt.Sprintf("\t%s (from $GOROOT)", tried.goroot))
   665  		} else {
   666  			paths = append(paths, "\t($GOROOT not set)")
   667  		}
   668  		format = "\t%s (from $GOPATH)"
   669  		for _, dir := range tried.gopath {
   670  			paths = append(paths, fmt.Sprintf(format, dir))
   671  			format = "\t%s"
   672  		}
   673  		if len(tried.gopath) == 0 {
   674  			paths = append(paths, "\t($GOPATH not set. For more details see: 'go help gopath')")
   675  		}
   676  		return p, fmt.Errorf("cannot find package %q in any of:\n%s", path, strings.Join(paths, "\n"))
   677  	}
   678  
   679  Found:
   680  	if p.Root != "" {
   681  		p.SrcRoot = ctxt.joinPath(p.Root, "src")
   682  		p.PkgRoot = ctxt.joinPath(p.Root, "pkg")
   683  		p.BinDir = ctxt.joinPath(p.Root, "bin")
   684  		if pkga != "" {
   685  			p.PkgTargetRoot = ctxt.joinPath(p.Root, pkgtargetroot)
   686  			p.PkgObj = ctxt.joinPath(p.Root, pkga)
   687  		}
   688  	}
   689  
   690  	// If it's a local import path, by the time we get here, we still haven't checked
   691  	// that p.Dir directory exists. This is the right time to do that check.
   692  	// We can't do it earlier, because we want to gather partial information for the
   693  	// non-nil *Package returned when an error occurs.
   694  	// We need to do this before we return early on FindOnly flag.
   695  	if IsLocalImport(path) && !ctxt.isDir(p.Dir) {
   696  		// package was not found
   697  		return p, fmt.Errorf("cannot find package %q in:\n\t%s", path, p.Dir)
   698  	}
   699  
   700  	if mode&FindOnly != 0 {
   701  		return p, pkgerr
   702  	}
   703  	if binaryOnly && (mode&AllowBinary) != 0 {
   704  		return p, pkgerr
   705  	}
   706  
   707  	dirs, err := ctxt.readDir(p.Dir)
   708  	if err != nil {
   709  		return p, err
   710  	}
   711  
   712  	var badGoError error
   713  	var Sfiles []string // files with ".S" (capital S)
   714  	var firstFile, firstCommentFile string
   715  	imported := make(map[string][]token.Position)
   716  	testImported := make(map[string][]token.Position)
   717  	xTestImported := make(map[string][]token.Position)
   718  	allTags := make(map[string]bool)
   719  	fset := token.NewFileSet()
   720  	for _, d := range dirs {
   721  		if d.IsDir() {
   722  			continue
   723  		}
   724  
   725  		name := d.Name()
   726  		ext := nameExt(name)
   727  
   728  		badFile := func(err error) {
   729  			if badGoError == nil {
   730  				badGoError = err
   731  			}
   732  			p.InvalidGoFiles = append(p.InvalidGoFiles, name)
   733  		}
   734  
   735  		match, data, filename, err := ctxt.matchFile(p.Dir, name, allTags, &p.BinaryOnly)
   736  		if err != nil {
   737  			badFile(err)
   738  			continue
   739  		}
   740  		if !match {
   741  			if ext == ".go" {
   742  				p.IgnoredGoFiles = append(p.IgnoredGoFiles, name)
   743  			}
   744  			continue
   745  		}
   746  
   747  		// Going to save the file. For non-Go files, can stop here.
   748  		switch ext {
   749  		case ".c":
   750  			p.CFiles = append(p.CFiles, name)
   751  			continue
   752  		case ".cc", ".cpp", ".cxx":
   753  			p.CXXFiles = append(p.CXXFiles, name)
   754  			continue
   755  		case ".m":
   756  			p.MFiles = append(p.MFiles, name)
   757  			continue
   758  		case ".h", ".hh", ".hpp", ".hxx":
   759  			p.HFiles = append(p.HFiles, name)
   760  			continue
   761  		case ".f", ".F", ".for", ".f90":
   762  			p.FFiles = append(p.FFiles, name)
   763  			continue
   764  		case ".s":
   765  			p.SFiles = append(p.SFiles, name)
   766  			continue
   767  		case ".S":
   768  			Sfiles = append(Sfiles, name)
   769  			continue
   770  		case ".swig":
   771  			p.SwigFiles = append(p.SwigFiles, name)
   772  			continue
   773  		case ".swigcxx":
   774  			p.SwigCXXFiles = append(p.SwigCXXFiles, name)
   775  			continue
   776  		case ".syso":
   777  			// binary objects to add to package archive
   778  			// Likely of the form foo_windows.syso, but
   779  			// the name was vetted above with goodOSArchFile.
   780  			p.SysoFiles = append(p.SysoFiles, name)
   781  			continue
   782  		}
   783  
   784  		pf, err := parser.ParseFile(fset, filename, data, parser.ImportsOnly|parser.ParseComments)
   785  		if err != nil {
   786  			badFile(err)
   787  			continue
   788  		}
   789  
   790  		pkg := pf.Name.Name
   791  		if pkg == "documentation" {
   792  			p.IgnoredGoFiles = append(p.IgnoredGoFiles, name)
   793  			continue
   794  		}
   795  
   796  		isTest := strings.HasSuffix(name, "_test.go")
   797  		isXTest := false
   798  		if isTest && strings.HasSuffix(pkg, "_test") {
   799  			isXTest = true
   800  			pkg = pkg[:len(pkg)-len("_test")]
   801  		}
   802  
   803  		if p.Name == "" {
   804  			p.Name = pkg
   805  			firstFile = name
   806  		} else if pkg != p.Name {
   807  			badFile(&MultiplePackageError{
   808  				Dir:      p.Dir,
   809  				Packages: []string{p.Name, pkg},
   810  				Files:    []string{firstFile, name},
   811  			})
   812  			p.InvalidGoFiles = append(p.InvalidGoFiles, name)
   813  		}
   814  		if pf.Doc != nil && p.Doc == "" {
   815  			p.Doc = doc.Synopsis(pf.Doc.Text())
   816  		}
   817  
   818  		if mode&ImportComment != 0 {
   819  			qcom, line := findImportComment(data)
   820  			if line != 0 {
   821  				com, err := strconv.Unquote(qcom)
   822  				if err != nil {
   823  					badFile(fmt.Errorf("%s:%d: cannot parse import comment", filename, line))
   824  				} else if p.ImportComment == "" {
   825  					p.ImportComment = com
   826  					firstCommentFile = name
   827  				} else if p.ImportComment != com {
   828  					badFile(fmt.Errorf("found import comments %q (%s) and %q (%s) in %s", p.ImportComment, firstCommentFile, com, name, p.Dir))
   829  				}
   830  			}
   831  		}
   832  
   833  		// Record imports and information about cgo.
   834  		isCgo := false
   835  		for _, decl := range pf.Decls {
   836  			d, ok := decl.(*ast.GenDecl)
   837  			if !ok {
   838  				continue
   839  			}
   840  			for _, dspec := range d.Specs {
   841  				spec, ok := dspec.(*ast.ImportSpec)
   842  				if !ok {
   843  					continue
   844  				}
   845  				quoted := spec.Path.Value
   846  				path, err := strconv.Unquote(quoted)
   847  				if err != nil {
   848  					log.Panicf("%s: parser returned invalid quoted string: <%s>", filename, quoted)
   849  				}
   850  				if isXTest {
   851  					xTestImported[path] = append(xTestImported[path], fset.Position(spec.Pos()))
   852  				} else if isTest {
   853  					testImported[path] = append(testImported[path], fset.Position(spec.Pos()))
   854  				} else {
   855  					imported[path] = append(imported[path], fset.Position(spec.Pos()))
   856  				}
   857  				if path == "C" {
   858  					if isTest {
   859  						badFile(fmt.Errorf("use of cgo in test %s not supported", filename))
   860  					} else {
   861  						cg := spec.Doc
   862  						if cg == nil && len(d.Specs) == 1 {
   863  							cg = d.Doc
   864  						}
   865  						if cg != nil {
   866  							if err := ctxt.saveCgo(filename, p, cg); err != nil {
   867  								badFile(err)
   868  							}
   869  						}
   870  						isCgo = true
   871  					}
   872  				}
   873  			}
   874  		}
   875  		if isCgo {
   876  			allTags["cgo"] = true
   877  			if ctxt.CgoEnabled {
   878  				p.CgoFiles = append(p.CgoFiles, name)
   879  			} else {
   880  				p.IgnoredGoFiles = append(p.IgnoredGoFiles, name)
   881  			}
   882  		} else if isXTest {
   883  			p.XTestGoFiles = append(p.XTestGoFiles, name)
   884  		} else if isTest {
   885  			p.TestGoFiles = append(p.TestGoFiles, name)
   886  		} else {
   887  			p.GoFiles = append(p.GoFiles, name)
   888  		}
   889  	}
   890  	if badGoError != nil {
   891  		return p, badGoError
   892  	}
   893  	if len(p.GoFiles)+len(p.CgoFiles)+len(p.TestGoFiles)+len(p.XTestGoFiles) == 0 {
   894  		return p, &NoGoError{p.Dir}
   895  	}
   896  
   897  	for tag := range allTags {
   898  		p.AllTags = append(p.AllTags, tag)
   899  	}
   900  	sort.Strings(p.AllTags)
   901  
   902  	p.Imports, p.ImportPos = cleanImports(imported)
   903  	p.TestImports, p.TestImportPos = cleanImports(testImported)
   904  	p.XTestImports, p.XTestImportPos = cleanImports(xTestImported)
   905  
   906  	// add the .S files only if we are using cgo
   907  	// (which means gcc will compile them).
   908  	// The standard assemblers expect .s files.
   909  	if len(p.CgoFiles) > 0 {
   910  		p.SFiles = append(p.SFiles, Sfiles...)
   911  		sort.Strings(p.SFiles)
   912  	}
   913  
   914  	return p, pkgerr
   915  }
   916  
   917  // hasGoFiles reports whether dir contains any files with names ending in .go.
   918  // For a vendor check we must exclude directories that contain no .go files.
   919  // Otherwise it is not possible to vendor just a/b/c and still import the
   920  // non-vendored a/b. See golang.org/issue/13832.
   921  func hasGoFiles(ctxt *Context, dir string) bool {
   922  	ents, _ := ctxt.readDir(dir)
   923  	for _, ent := range ents {
   924  		if !ent.IsDir() && strings.HasSuffix(ent.Name(), ".go") {
   925  			return true
   926  		}
   927  	}
   928  	return false
   929  }
   930  
   931  func findImportComment(data []byte) (s string, line int) {
   932  	// expect keyword package
   933  	word, data := parseWord(data)
   934  	if string(word) != "package" {
   935  		return "", 0
   936  	}
   937  
   938  	// expect package name
   939  	_, data = parseWord(data)
   940  
   941  	// now ready for import comment, a // or /* */ comment
   942  	// beginning and ending on the current line.
   943  	for len(data) > 0 && (data[0] == ' ' || data[0] == '\t' || data[0] == '\r') {
   944  		data = data[1:]
   945  	}
   946  
   947  	var comment []byte
   948  	switch {
   949  	case bytes.HasPrefix(data, slashSlash):
   950  		i := bytes.Index(data, newline)
   951  		if i < 0 {
   952  			i = len(data)
   953  		}
   954  		comment = data[2:i]
   955  	case bytes.HasPrefix(data, slashStar):
   956  		data = data[2:]
   957  		i := bytes.Index(data, starSlash)
   958  		if i < 0 {
   959  			// malformed comment
   960  			return "", 0
   961  		}
   962  		comment = data[:i]
   963  		if bytes.Contains(comment, newline) {
   964  			return "", 0
   965  		}
   966  	}
   967  	comment = bytes.TrimSpace(comment)
   968  
   969  	// split comment into `import`, `"pkg"`
   970  	word, arg := parseWord(comment)
   971  	if string(word) != "import" {
   972  		return "", 0
   973  	}
   974  
   975  	line = 1 + bytes.Count(data[:cap(data)-cap(arg)], newline)
   976  	return strings.TrimSpace(string(arg)), line
   977  }
   978  
   979  var (
   980  	slashSlash = []byte("//")
   981  	slashStar  = []byte("/*")
   982  	starSlash  = []byte("*/")
   983  	newline    = []byte("\n")
   984  )
   985  
   986  // skipSpaceOrComment returns data with any leading spaces or comments removed.
   987  func skipSpaceOrComment(data []byte) []byte {
   988  	for len(data) > 0 {
   989  		switch data[0] {
   990  		case ' ', '\t', '\r', '\n':
   991  			data = data[1:]
   992  			continue
   993  		case '/':
   994  			if bytes.HasPrefix(data, slashSlash) {
   995  				i := bytes.Index(data, newline)
   996  				if i < 0 {
   997  					return nil
   998  				}
   999  				data = data[i+1:]
  1000  				continue
  1001  			}
  1002  			if bytes.HasPrefix(data, slashStar) {
  1003  				data = data[2:]
  1004  				i := bytes.Index(data, starSlash)
  1005  				if i < 0 {
  1006  					return nil
  1007  				}
  1008  				data = data[i+2:]
  1009  				continue
  1010  			}
  1011  		}
  1012  		break
  1013  	}
  1014  	return data
  1015  }
  1016  
  1017  // parseWord skips any leading spaces or comments in data
  1018  // and then parses the beginning of data as an identifier or keyword,
  1019  // returning that word and what remains after the word.
  1020  func parseWord(data []byte) (word, rest []byte) {
  1021  	data = skipSpaceOrComment(data)
  1022  
  1023  	// Parse past leading word characters.
  1024  	rest = data
  1025  	for {
  1026  		r, size := utf8.DecodeRune(rest)
  1027  		if unicode.IsLetter(r) || '0' <= r && r <= '9' || r == '_' {
  1028  			rest = rest[size:]
  1029  			continue
  1030  		}
  1031  		break
  1032  	}
  1033  
  1034  	word = data[:len(data)-len(rest)]
  1035  	if len(word) == 0 {
  1036  		return nil, nil
  1037  	}
  1038  
  1039  	return word, rest
  1040  }
  1041  
  1042  // MatchFile reports whether the file with the given name in the given directory
  1043  // matches the context and would be included in a Package created by ImportDir
  1044  // of that directory.
  1045  //
  1046  // MatchFile considers the name of the file and may use ctxt.OpenFile to
  1047  // read some or all of the file's content.
  1048  func (ctxt *Context) MatchFile(dir, name string) (match bool, err error) {
  1049  	match, _, _, err = ctxt.matchFile(dir, name, nil, nil)
  1050  	return
  1051  }
  1052  
  1053  // matchFile determines whether the file with the given name in the given directory
  1054  // should be included in the package being constructed.
  1055  // It returns the data read from the file.
  1056  // If name denotes a Go program, matchFile reads until the end of the
  1057  // imports (and returns that data) even though it only considers text
  1058  // until the first non-comment.
  1059  // If allTags is non-nil, matchFile records any encountered build tag
  1060  // by setting allTags[tag] = true.
  1061  func (ctxt *Context) matchFile(dir, name string, allTags map[string]bool, binaryOnly *bool) (match bool, data []byte, filename string, err error) {
  1062  	if strings.HasPrefix(name, "_") ||
  1063  		strings.HasPrefix(name, ".") {
  1064  		return
  1065  	}
  1066  
  1067  	i := strings.LastIndex(name, ".")
  1068  	if i < 0 {
  1069  		i = len(name)
  1070  	}
  1071  	ext := name[i:]
  1072  
  1073  	if !ctxt.goodOSArchFile(name, allTags) && !ctxt.UseAllFiles {
  1074  		return
  1075  	}
  1076  
  1077  	switch ext {
  1078  	case ".go", ".c", ".cc", ".cxx", ".cpp", ".m", ".s", ".h", ".hh", ".hpp", ".hxx", ".f", ".F", ".f90", ".S", ".swig", ".swigcxx":
  1079  		// tentatively okay - read to make sure
  1080  	case ".syso":
  1081  		// binary, no reading
  1082  		match = true
  1083  		return
  1084  	default:
  1085  		// skip
  1086  		return
  1087  	}
  1088  
  1089  	filename = ctxt.joinPath(dir, name)
  1090  	f, err := ctxt.openFile(filename)
  1091  	if err != nil {
  1092  		return
  1093  	}
  1094  
  1095  	if strings.HasSuffix(filename, ".go") {
  1096  		data, err = readImports(f, false, nil)
  1097  		if strings.HasSuffix(filename, "_test.go") {
  1098  			binaryOnly = nil // ignore //go:binary-only-package comments in _test.go files
  1099  		}
  1100  	} else {
  1101  		binaryOnly = nil // ignore //go:binary-only-package comments in non-Go sources
  1102  		data, err = readComments(f)
  1103  	}
  1104  	f.Close()
  1105  	if err != nil {
  1106  		err = fmt.Errorf("read %s: %v", filename, err)
  1107  		return
  1108  	}
  1109  
  1110  	// Look for +build comments to accept or reject the file.
  1111  	var sawBinaryOnly bool
  1112  	if !ctxt.shouldBuild(data, allTags, &sawBinaryOnly) && !ctxt.UseAllFiles {
  1113  		return
  1114  	}
  1115  
  1116  	if binaryOnly != nil && sawBinaryOnly {
  1117  		*binaryOnly = true
  1118  	}
  1119  	match = true
  1120  	return
  1121  }
  1122  
  1123  func cleanImports(m map[string][]token.Position) ([]string, map[string][]token.Position) {
  1124  	all := make([]string, 0, len(m))
  1125  	for path := range m {
  1126  		all = append(all, path)
  1127  	}
  1128  	sort.Strings(all)
  1129  	return all, m
  1130  }
  1131  
  1132  // Import is shorthand for Default.Import.
  1133  func Import(path, srcDir string, mode ImportMode) (*Package, error) {
  1134  	return Default.Import(path, srcDir, mode)
  1135  }
  1136  
  1137  // ImportDir is shorthand for Default.ImportDir.
  1138  func ImportDir(dir string, mode ImportMode) (*Package, error) {
  1139  	return Default.ImportDir(dir, mode)
  1140  }
  1141  
  1142  var slashslash = []byte("//")
  1143  
  1144  // Special comment denoting a binary-only package.
  1145  // See https://golang.org/design/2775-binary-only-packages
  1146  // for more about the design of binary-only packages.
  1147  var binaryOnlyComment = []byte("//go:binary-only-package")
  1148  
  1149  // shouldBuild reports whether it is okay to use this file,
  1150  // The rule is that in the file's leading run of // comments
  1151  // and blank lines, which must be followed by a blank line
  1152  // (to avoid including a Go package clause doc comment),
  1153  // lines beginning with '// +build' are taken as build directives.
  1154  //
  1155  // The file is accepted only if each such line lists something
  1156  // matching the file. For example:
  1157  //
  1158  //	// +build windows linux
  1159  //
  1160  // marks the file as applicable only on Windows and Linux.
  1161  //
  1162  // If shouldBuild finds a //go:binary-only-package comment in the file,
  1163  // it sets *binaryOnly to true. Otherwise it does not change *binaryOnly.
  1164  //
  1165  func (ctxt *Context) shouldBuild(content []byte, allTags map[string]bool, binaryOnly *bool) bool {
  1166  	sawBinaryOnly := false
  1167  
  1168  	// Pass 1. Identify leading run of // comments and blank lines,
  1169  	// which must be followed by a blank line.
  1170  	end := 0
  1171  	p := content
  1172  	for len(p) > 0 {
  1173  		line := p
  1174  		if i := bytes.IndexByte(line, '\n'); i >= 0 {
  1175  			line, p = line[:i], p[i+1:]
  1176  		} else {
  1177  			p = p[len(p):]
  1178  		}
  1179  		line = bytes.TrimSpace(line)
  1180  		if len(line) == 0 { // Blank line
  1181  			end = len(content) - len(p)
  1182  			continue
  1183  		}
  1184  		if !bytes.HasPrefix(line, slashslash) { // Not comment line
  1185  			break
  1186  		}
  1187  	}
  1188  	content = content[:end]
  1189  
  1190  	// Pass 2.  Process each line in the run.
  1191  	p = content
  1192  	allok := true
  1193  	for len(p) > 0 {
  1194  		line := p
  1195  		if i := bytes.IndexByte(line, '\n'); i >= 0 {
  1196  			line, p = line[:i], p[i+1:]
  1197  		} else {
  1198  			p = p[len(p):]
  1199  		}
  1200  		line = bytes.TrimSpace(line)
  1201  		if !bytes.HasPrefix(line, slashslash) {
  1202  			continue
  1203  		}
  1204  		if bytes.Equal(line, binaryOnlyComment) {
  1205  			sawBinaryOnly = true
  1206  		}
  1207  		line = bytes.TrimSpace(line[len(slashslash):])
  1208  		if len(line) > 0 && line[0] == '+' {
  1209  			// Looks like a comment +line.
  1210  			f := strings.Fields(string(line))
  1211  			if f[0] == "+build" {
  1212  				ok := false
  1213  				for _, tok := range f[1:] {
  1214  					if ctxt.match(tok, allTags) {
  1215  						ok = true
  1216  					}
  1217  				}
  1218  				if !ok {
  1219  					allok = false
  1220  				}
  1221  			}
  1222  		}
  1223  	}
  1224  
  1225  	if binaryOnly != nil && sawBinaryOnly {
  1226  		*binaryOnly = true
  1227  	}
  1228  
  1229  	return allok
  1230  }
  1231  
  1232  // saveCgo saves the information from the #cgo lines in the import "C" comment.
  1233  // These lines set CFLAGS, CPPFLAGS, CXXFLAGS and LDFLAGS and pkg-config directives
  1234  // that affect the way cgo's C code is built.
  1235  func (ctxt *Context) saveCgo(filename string, di *Package, cg *ast.CommentGroup) error {
  1236  	text := cg.Text()
  1237  	for _, line := range strings.Split(text, "\n") {
  1238  		orig := line
  1239  
  1240  		// Line is
  1241  		//	#cgo [GOOS/GOARCH...] LDFLAGS: stuff
  1242  		//
  1243  		line = strings.TrimSpace(line)
  1244  		if len(line) < 5 || line[:4] != "#cgo" || (line[4] != ' ' && line[4] != '\t') {
  1245  			continue
  1246  		}
  1247  
  1248  		// Split at colon.
  1249  		line = strings.TrimSpace(line[4:])
  1250  		i := strings.Index(line, ":")
  1251  		if i < 0 {
  1252  			return fmt.Errorf("%s: invalid #cgo line: %s", filename, orig)
  1253  		}
  1254  		line, argstr := line[:i], line[i+1:]
  1255  
  1256  		// Parse GOOS/GOARCH stuff.
  1257  		f := strings.Fields(line)
  1258  		if len(f) < 1 {
  1259  			return fmt.Errorf("%s: invalid #cgo line: %s", filename, orig)
  1260  		}
  1261  
  1262  		cond, verb := f[:len(f)-1], f[len(f)-1]
  1263  		if len(cond) > 0 {
  1264  			ok := false
  1265  			for _, c := range cond {
  1266  				if ctxt.match(c, nil) {
  1267  					ok = true
  1268  					break
  1269  				}
  1270  			}
  1271  			if !ok {
  1272  				continue
  1273  			}
  1274  		}
  1275  
  1276  		args, err := splitQuoted(argstr)
  1277  		if err != nil {
  1278  			return fmt.Errorf("%s: invalid #cgo line: %s", filename, orig)
  1279  		}
  1280  		var ok bool
  1281  		for i, arg := range args {
  1282  			if arg, ok = expandSrcDir(arg, di.Dir); !ok {
  1283  				return fmt.Errorf("%s: malformed #cgo argument: %s", filename, arg)
  1284  			}
  1285  			args[i] = arg
  1286  		}
  1287  
  1288  		switch verb {
  1289  		case "CFLAGS", "CPPFLAGS", "CXXFLAGS", "FFLAGS", "LDFLAGS":
  1290  			// Change relative paths to absolute.
  1291  			ctxt.makePathsAbsolute(args, di.Dir)
  1292  		}
  1293  
  1294  		switch verb {
  1295  		case "CFLAGS":
  1296  			di.CgoCFLAGS = append(di.CgoCFLAGS, args...)
  1297  		case "CPPFLAGS":
  1298  			di.CgoCPPFLAGS = append(di.CgoCPPFLAGS, args...)
  1299  		case "CXXFLAGS":
  1300  			di.CgoCXXFLAGS = append(di.CgoCXXFLAGS, args...)
  1301  		case "FFLAGS":
  1302  			di.CgoFFLAGS = append(di.CgoFFLAGS, args...)
  1303  		case "LDFLAGS":
  1304  			di.CgoLDFLAGS = append(di.CgoLDFLAGS, args...)
  1305  		case "pkg-config":
  1306  			di.CgoPkgConfig = append(di.CgoPkgConfig, args...)
  1307  		default:
  1308  			return fmt.Errorf("%s: invalid #cgo verb: %s", filename, orig)
  1309  		}
  1310  	}
  1311  	return nil
  1312  }
  1313  
  1314  // expandSrcDir expands any occurrence of ${SRCDIR}, making sure
  1315  // the result is safe for the shell.
  1316  func expandSrcDir(str string, srcdir string) (string, bool) {
  1317  	// "\" delimited paths cause safeCgoName to fail
  1318  	// so convert native paths with a different delimiter
  1319  	// to "/" before starting (eg: on windows).
  1320  	srcdir = filepath.ToSlash(srcdir)
  1321  
  1322  	chunks := strings.Split(str, "${SRCDIR}")
  1323  	if len(chunks) < 2 {
  1324  		return str, safeCgoName(str)
  1325  	}
  1326  	ok := true
  1327  	for _, chunk := range chunks {
  1328  		ok = ok && (chunk == "" || safeCgoName(chunk))
  1329  	}
  1330  	ok = ok && (srcdir == "" || safeCgoName(srcdir))
  1331  	res := strings.Join(chunks, srcdir)
  1332  	return res, ok && res != ""
  1333  }
  1334  
  1335  // makePathsAbsolute looks for compiler options that take paths and
  1336  // makes them absolute. We do this because through the 1.8 release we
  1337  // ran the compiler in the package directory, so any relative -I or -L
  1338  // options would be relative to that directory. In 1.9 we changed to
  1339  // running the compiler in the build directory, to get consistent
  1340  // build results (issue #19964). To keep builds working, we change any
  1341  // relative -I or -L options to be absolute.
  1342  //
  1343  // Using filepath.IsAbs and filepath.Join here means the results will be
  1344  // different on different systems, but that's OK: -I and -L options are
  1345  // inherently system-dependent.
  1346  func (ctxt *Context) makePathsAbsolute(args []string, srcDir string) {
  1347  	nextPath := false
  1348  	for i, arg := range args {
  1349  		if nextPath {
  1350  			if !filepath.IsAbs(arg) {
  1351  				args[i] = filepath.Join(srcDir, arg)
  1352  			}
  1353  			nextPath = false
  1354  		} else if strings.HasPrefix(arg, "-I") || strings.HasPrefix(arg, "-L") {
  1355  			if len(arg) == 2 {
  1356  				nextPath = true
  1357  			} else {
  1358  				if !filepath.IsAbs(arg[2:]) {
  1359  					args[i] = arg[:2] + filepath.Join(srcDir, arg[2:])
  1360  				}
  1361  			}
  1362  		}
  1363  	}
  1364  }
  1365  
  1366  // NOTE: $ is not safe for the shell, but it is allowed here because of linker options like -Wl,$ORIGIN.
  1367  // We never pass these arguments to a shell (just to programs we construct argv for), so this should be okay.
  1368  // See golang.org/issue/6038.
  1369  // The @ is for OS X. See golang.org/issue/13720.
  1370  // The % is for Jenkins. See golang.org/issue/16959.
  1371  const safeString = "+-.,/0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz:$@% "
  1372  
  1373  func safeCgoName(s string) bool {
  1374  	if s == "" {
  1375  		return false
  1376  	}
  1377  	for i := 0; i < len(s); i++ {
  1378  		if c := s[i]; c < utf8.RuneSelf && strings.IndexByte(safeString, c) < 0 {
  1379  			return false
  1380  		}
  1381  	}
  1382  	return true
  1383  }
  1384  
  1385  // splitQuoted splits the string s around each instance of one or more consecutive
  1386  // white space characters while taking into account quotes and escaping, and
  1387  // returns an array of substrings of s or an empty list if s contains only white space.
  1388  // Single quotes and double quotes are recognized to prevent splitting within the
  1389  // quoted region, and are removed from the resulting substrings. If a quote in s
  1390  // isn't closed err will be set and r will have the unclosed argument as the
  1391  // last element. The backslash is used for escaping.
  1392  //
  1393  // For example, the following string:
  1394  //
  1395  //     a b:"c d" 'e''f'  "g\""
  1396  //
  1397  // Would be parsed as:
  1398  //
  1399  //     []string{"a", "b:c d", "ef", `g"`}
  1400  //
  1401  func splitQuoted(s string) (r []string, err error) {
  1402  	var args []string
  1403  	arg := make([]rune, len(s))
  1404  	escaped := false
  1405  	quoted := false
  1406  	quote := '\x00'
  1407  	i := 0
  1408  	for _, rune := range s {
  1409  		switch {
  1410  		case escaped:
  1411  			escaped = false
  1412  		case rune == '\\':
  1413  			escaped = true
  1414  			continue
  1415  		case quote != '\x00':
  1416  			if rune == quote {
  1417  				quote = '\x00'
  1418  				continue
  1419  			}
  1420  		case rune == '"' || rune == '\'':
  1421  			quoted = true
  1422  			quote = rune
  1423  			continue
  1424  		case unicode.IsSpace(rune):
  1425  			if quoted || i > 0 {
  1426  				quoted = false
  1427  				args = append(args, string(arg[:i]))
  1428  				i = 0
  1429  			}
  1430  			continue
  1431  		}
  1432  		arg[i] = rune
  1433  		i++
  1434  	}
  1435  	if quoted || i > 0 {
  1436  		args = append(args, string(arg[:i]))
  1437  	}
  1438  	if quote != 0 {
  1439  		err = errors.New("unclosed quote")
  1440  	} else if escaped {
  1441  		err = errors.New("unfinished escaping")
  1442  	}
  1443  	return args, err
  1444  }
  1445  
  1446  // match reports whether the name is one of:
  1447  //
  1448  //	$GOOS
  1449  //	$GOARCH
  1450  //	cgo (if cgo is enabled)
  1451  //	!cgo (if cgo is disabled)
  1452  //	ctxt.Compiler
  1453  //	!ctxt.Compiler
  1454  //	tag (if tag is listed in ctxt.BuildTags or ctxt.ReleaseTags)
  1455  //	!tag (if tag is not listed in ctxt.BuildTags or ctxt.ReleaseTags)
  1456  //	a comma-separated list of any of these
  1457  //
  1458  func (ctxt *Context) match(name string, allTags map[string]bool) bool {
  1459  	if name == "" {
  1460  		if allTags != nil {
  1461  			allTags[name] = true
  1462  		}
  1463  		return false
  1464  	}
  1465  	if i := strings.Index(name, ","); i >= 0 {
  1466  		// comma-separated list
  1467  		ok1 := ctxt.match(name[:i], allTags)
  1468  		ok2 := ctxt.match(name[i+1:], allTags)
  1469  		return ok1 && ok2
  1470  	}
  1471  	if strings.HasPrefix(name, "!!") { // bad syntax, reject always
  1472  		return false
  1473  	}
  1474  	if strings.HasPrefix(name, "!") { // negation
  1475  		return len(name) > 1 && !ctxt.match(name[1:], allTags)
  1476  	}
  1477  
  1478  	if allTags != nil {
  1479  		allTags[name] = true
  1480  	}
  1481  
  1482  	// Tags must be letters, digits, underscores or dots.
  1483  	// Unlike in Go identifiers, all digits are fine (e.g., "386").
  1484  	for _, c := range name {
  1485  		if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_' && c != '.' {
  1486  			return false
  1487  		}
  1488  	}
  1489  
  1490  	// special tags
  1491  	if ctxt.CgoEnabled && name == "cgo" {
  1492  		return true
  1493  	}
  1494  	if name == ctxt.GOOS || name == ctxt.GOARCH || name == ctxt.Compiler {
  1495  		return true
  1496  	}
  1497  	if ctxt.GOOS == "android" && name == "linux" {
  1498  		return true
  1499  	}
  1500  
  1501  	// other tags
  1502  	for _, tag := range ctxt.BuildTags {
  1503  		if tag == name {
  1504  			return true
  1505  		}
  1506  	}
  1507  	for _, tag := range ctxt.ReleaseTags {
  1508  		if tag == name {
  1509  			return true
  1510  		}
  1511  	}
  1512  
  1513  	return false
  1514  }
  1515  
  1516  // goodOSArchFile returns false if the name contains a $GOOS or $GOARCH
  1517  // suffix which does not match the current system.
  1518  // The recognized name formats are:
  1519  //
  1520  //     name_$(GOOS).*
  1521  //     name_$(GOARCH).*
  1522  //     name_$(GOOS)_$(GOARCH).*
  1523  //     name_$(GOOS)_test.*
  1524  //     name_$(GOARCH)_test.*
  1525  //     name_$(GOOS)_$(GOARCH)_test.*
  1526  //
  1527  // An exception: if GOOS=android, then files with GOOS=linux are also matched.
  1528  func (ctxt *Context) goodOSArchFile(name string, allTags map[string]bool) bool {
  1529  	if dot := strings.Index(name, "."); dot != -1 {
  1530  		name = name[:dot]
  1531  	}
  1532  
  1533  	// Before Go 1.4, a file called "linux.go" would be equivalent to having a
  1534  	// build tag "linux" in that file. For Go 1.4 and beyond, we require this
  1535  	// auto-tagging to apply only to files with a non-empty prefix, so
  1536  	// "foo_linux.go" is tagged but "linux.go" is not. This allows new operating
  1537  	// systems, such as android, to arrive without breaking existing code with
  1538  	// innocuous source code in "android.go". The easiest fix: cut everything
  1539  	// in the name before the initial _.
  1540  	i := strings.Index(name, "_")
  1541  	if i < 0 {
  1542  		return true
  1543  	}
  1544  	name = name[i:] // ignore everything before first _
  1545  
  1546  	l := strings.Split(name, "_")
  1547  	if n := len(l); n > 0 && l[n-1] == "test" {
  1548  		l = l[:n-1]
  1549  	}
  1550  	n := len(l)
  1551  	if n >= 2 && knownOS[l[n-2]] && knownArch[l[n-1]] {
  1552  		if allTags != nil {
  1553  			allTags[l[n-2]] = true
  1554  			allTags[l[n-1]] = true
  1555  		}
  1556  		if l[n-1] != ctxt.GOARCH {
  1557  			return false
  1558  		}
  1559  		if ctxt.GOOS == "android" && l[n-2] == "linux" {
  1560  			return true
  1561  		}
  1562  		return l[n-2] == ctxt.GOOS
  1563  	}
  1564  	if n >= 1 && knownOS[l[n-1]] {
  1565  		if allTags != nil {
  1566  			allTags[l[n-1]] = true
  1567  		}
  1568  		if ctxt.GOOS == "android" && l[n-1] == "linux" {
  1569  			return true
  1570  		}
  1571  		return l[n-1] == ctxt.GOOS
  1572  	}
  1573  	if n >= 1 && knownArch[l[n-1]] {
  1574  		if allTags != nil {
  1575  			allTags[l[n-1]] = true
  1576  		}
  1577  		return l[n-1] == ctxt.GOARCH
  1578  	}
  1579  	return true
  1580  }
  1581  
  1582  var knownOS = make(map[string]bool)
  1583  var knownArch = make(map[string]bool)
  1584  
  1585  func init() {
  1586  	for _, v := range strings.Fields(goosList) {
  1587  		knownOS[v] = true
  1588  	}
  1589  	for _, v := range strings.Fields(goarchList) {
  1590  		knownArch[v] = true
  1591  	}
  1592  }
  1593  
  1594  // ToolDir is the directory containing build tools.
  1595  var ToolDir = filepath.Join(runtime.GOROOT(), "pkg/tool/"+runtime.GOOS+"_"+runtime.GOARCH)
  1596  
  1597  // IsLocalImport reports whether the import path is
  1598  // a local import path, like ".", "..", "./foo", or "../foo".
  1599  func IsLocalImport(path string) bool {
  1600  	return path == "." || path == ".." ||
  1601  		strings.HasPrefix(path, "./") || strings.HasPrefix(path, "../")
  1602  }
  1603  
  1604  // ArchChar returns "?" and an error.
  1605  // In earlier versions of Go, the returned string was used to derive
  1606  // the compiler and linker tool names, the default object file suffix,
  1607  // and the default linker output name. As of Go 1.5, those strings
  1608  // no longer vary by architecture; they are compile, link, .o, and a.out, respectively.
  1609  func ArchChar(goarch string) (string, error) {
  1610  	return "?", errors.New("architecture letter no longer used")
  1611  }