Run Format

Source file src/pkg/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/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	)
    27	
    28	// A Context specifies the supporting context for a build.
    29	type Context struct {
    30		GOARCH      string // target architecture
    31		GOOS        string // target operating system
    32		GOROOT      string // Go root
    33		GOPATH      string // Go path
    34		CgoEnabled  bool   // whether cgo can be used
    35		UseAllFiles bool   // use files regardless of +build lines, file names
    36		Compiler    string // compiler to assume when computing target paths
    37	
    38		// The build and release tags specify build constraints
    39		// that should be considered satisfied when processing +build lines.
    40		// Clients creating a new context may customize BuildTags, which
    41		// defaults to empty, but it is usually an error to customize ReleaseTags,
    42		// which defaults to the list of Go releases the current release is compatible with.
    43		// In addition to the BuildTags and ReleaseTags, build constraints
    44		// consider the values of GOARCH and GOOS as satisfied tags.
    45		BuildTags   []string
    46		ReleaseTags []string
    47	
    48		// The install suffix specifies a suffix to use in the name of the installation
    49		// directory. By default it is empty, but custom builds that need to keep
    50		// their outputs separate can set InstallSuffix to do so. For example, when
    51		// using the race detector, the go command uses InstallSuffix = "race", so
    52		// that on a Linux/386 system, packages are written to a directory named
    53		// "linux_386_race" instead of the usual "linux_386".
    54		InstallSuffix string
    55	
    56		// By default, Import uses the operating system's file system calls
    57		// to read directories and files.  To read from other sources,
    58		// callers can set the following functions.  They all have default
    59		// behaviors that use the local file system, so clients need only set
    60		// the functions whose behaviors they wish to change.
    61	
    62		// JoinPath joins the sequence of path fragments into a single path.
    63		// If JoinPath is nil, Import uses filepath.Join.
    64		JoinPath func(elem ...string) string
    65	
    66		// SplitPathList splits the path list into a slice of individual paths.
    67		// If SplitPathList is nil, Import uses filepath.SplitList.
    68		SplitPathList func(list string) []string
    69	
    70		// IsAbsPath reports whether path is an absolute path.
    71		// If IsAbsPath is nil, Import uses filepath.IsAbs.
    72		IsAbsPath func(path string) bool
    73	
    74		// IsDir reports whether the path names a directory.
    75		// If IsDir is nil, Import calls os.Stat and uses the result's IsDir method.
    76		IsDir func(path string) bool
    77	
    78		// HasSubdir reports whether dir is a subdirectory of
    79		// (perhaps multiple levels below) root.
    80		// If so, HasSubdir sets rel to a slash-separated path that
    81		// can be joined to root to produce a path equivalent to dir.
    82		// If HasSubdir is nil, Import uses an implementation built on
    83		// filepath.EvalSymlinks.
    84		HasSubdir func(root, dir string) (rel string, ok bool)
    85	
    86		// ReadDir returns a slice of os.FileInfo, sorted by Name,
    87		// describing the content of the named directory.
    88		// If ReadDir is nil, Import uses ioutil.ReadDir.
    89		ReadDir func(dir string) (fi []os.FileInfo, err error)
    90	
    91		// OpenFile opens a file (not a directory) for reading.
    92		// If OpenFile is nil, Import uses os.Open.
    93		OpenFile func(path string) (r io.ReadCloser, err error)
    94	}
    95	
    96	// joinPath calls ctxt.JoinPath (if not nil) or else filepath.Join.
    97	func (ctxt *Context) joinPath(elem ...string) string {
    98		if f := ctxt.JoinPath; f != nil {
    99			return f(elem...)
   100		}
   101		return filepath.Join(elem...)
   102	}
   103	
   104	// splitPathList calls ctxt.SplitPathList (if not nil) or else filepath.SplitList.
   105	func (ctxt *Context) splitPathList(s string) []string {
   106		if f := ctxt.SplitPathList; f != nil {
   107			return f(s)
   108		}
   109		return filepath.SplitList(s)
   110	}
   111	
   112	// isAbsPath calls ctxt.IsAbsSPath (if not nil) or else filepath.IsAbs.
   113	func (ctxt *Context) isAbsPath(path string) bool {
   114		if f := ctxt.IsAbsPath; f != nil {
   115			return f(path)
   116		}
   117		return filepath.IsAbs(path)
   118	}
   119	
   120	// isDir calls ctxt.IsDir (if not nil) or else uses os.Stat.
   121	func (ctxt *Context) isDir(path string) bool {
   122		if f := ctxt.IsDir; f != nil {
   123			return f(path)
   124		}
   125		fi, err := os.Stat(path)
   126		return err == nil && fi.IsDir()
   127	}
   128	
   129	// hasSubdir calls ctxt.HasSubdir (if not nil) or else uses
   130	// the local file system to answer the question.
   131	func (ctxt *Context) hasSubdir(root, dir string) (rel string, ok bool) {
   132		if f := ctxt.HasSubdir; f != nil {
   133			return f(root, dir)
   134		}
   135	
   136		// Try using paths we received.
   137		if rel, ok = hasSubdir(root, dir); ok {
   138			return
   139		}
   140	
   141		// Try expanding symlinks and comparing
   142		// expanded against unexpanded and
   143		// expanded against expanded.
   144		rootSym, _ := filepath.EvalSymlinks(root)
   145		dirSym, _ := filepath.EvalSymlinks(dir)
   146	
   147		if rel, ok = hasSubdir(rootSym, dir); ok {
   148			return
   149		}
   150		if rel, ok = hasSubdir(root, dirSym); ok {
   151			return
   152		}
   153		return hasSubdir(rootSym, dirSym)
   154	}
   155	
   156	func hasSubdir(root, dir string) (rel string, ok bool) {
   157		const sep = string(filepath.Separator)
   158		root = filepath.Clean(root)
   159		if !strings.HasSuffix(root, sep) {
   160			root += sep
   161		}
   162		dir = filepath.Clean(dir)
   163		if !strings.HasPrefix(dir, root) {
   164			return "", false
   165		}
   166		return filepath.ToSlash(dir[len(root):]), true
   167	}
   168	
   169	// readDir calls ctxt.ReadDir (if not nil) or else ioutil.ReadDir.
   170	func (ctxt *Context) readDir(path string) ([]os.FileInfo, error) {
   171		if f := ctxt.ReadDir; f != nil {
   172			return f(path)
   173		}
   174		return ioutil.ReadDir(path)
   175	}
   176	
   177	// openFile calls ctxt.OpenFile (if not nil) or else os.Open.
   178	func (ctxt *Context) openFile(path string) (io.ReadCloser, error) {
   179		if fn := ctxt.OpenFile; fn != nil {
   180			return fn(path)
   181		}
   182	
   183		f, err := os.Open(path)
   184		if err != nil {
   185			return nil, err // nil interface
   186		}
   187		return f, nil
   188	}
   189	
   190	// isFile determines whether path is a file by trying to open it.
   191	// It reuses openFile instead of adding another function to the
   192	// list in Context.
   193	func (ctxt *Context) isFile(path string) bool {
   194		f, err := ctxt.openFile(path)
   195		if err != nil {
   196			return false
   197		}
   198		f.Close()
   199		return true
   200	}
   201	
   202	// gopath returns the list of Go path directories.
   203	func (ctxt *Context) gopath() []string {
   204		var all []string
   205		for _, p := range ctxt.splitPathList(ctxt.GOPATH) {
   206			if p == "" || p == ctxt.GOROOT {
   207				// Empty paths are uninteresting.
   208				// If the path is the GOROOT, ignore it.
   209				// People sometimes set GOPATH=$GOROOT, which is useless
   210				// but would cause us to find packages with import paths
   211				// like "pkg/math".
   212				// Do not get confused by this common mistake.
   213				continue
   214			}
   215			if strings.HasPrefix(p, "~") {
   216				// Path segments starting with ~ on Unix are almost always
   217				// users who have incorrectly quoted ~ while setting GOPATH,
   218				// preventing it from expanding to $HOME.
   219				// The situation is made more confusing by the fact that
   220				// bash allows quoted ~ in $PATH (most shells do not).
   221				// Do not get confused by this, and do not try to use the path.
   222				// It does not exist, and printing errors about it confuses
   223				// those users even more, because they think "sure ~ exists!".
   224				// The go command diagnoses this situation and prints a
   225				// useful error.
   226				// On Windows, ~ is used in short names, such as c:\progra~1
   227				// for c:\program files.
   228				continue
   229			}
   230			all = append(all, p)
   231		}
   232		return all
   233	}
   234	
   235	// SrcDirs returns a list of package source root directories.
   236	// It draws from the current Go root and Go path but omits directories
   237	// that do not exist.
   238	func (ctxt *Context) SrcDirs() []string {
   239		var all []string
   240		if ctxt.GOROOT != "" {
   241			dir := ctxt.joinPath(ctxt.GOROOT, "src", "pkg")
   242			if ctxt.isDir(dir) {
   243				all = append(all, dir)
   244			}
   245		}
   246		for _, p := range ctxt.gopath() {
   247			dir := ctxt.joinPath(p, "src")
   248			if ctxt.isDir(dir) {
   249				all = append(all, dir)
   250			}
   251		}
   252		return all
   253	}
   254	
   255	// Default is the default Context for builds.
   256	// It uses the GOARCH, GOOS, GOROOT, and GOPATH environment variables
   257	// if set, or else the compiled code's GOARCH, GOOS, and GOROOT.
   258	var Default Context = defaultContext()
   259	
   260	var cgoEnabled = map[string]bool{
   261		"darwin/386":      true,
   262		"darwin/amd64":    true,
   263		"dragonfly/386":   true,
   264		"dragonfly/amd64": true,
   265		"freebsd/386":     true,
   266		"freebsd/amd64":   true,
   267		"freebsd/arm":     true,
   268		"linux/386":       true,
   269		"linux/amd64":     true,
   270		"linux/arm":       true,
   271		"netbsd/386":      true,
   272		"netbsd/amd64":    true,
   273		"netbsd/arm":      true,
   274		"openbsd/386":     true,
   275		"openbsd/amd64":   true,
   276		"windows/386":     true,
   277		"windows/amd64":   true,
   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 = runtime.GOROOT()
   286		c.GOPATH = envOr("GOPATH", "")
   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		//
   295		// When we reach Go 1.3 the line will read
   296		//	c.ReleaseTags = []string{"go1.1", "go1.2", "go1.3"}
   297		// and so on.
   298		c.ReleaseTags = []string{"go1.1", "go1.2"}
   299	
   300		switch os.Getenv("CGO_ENABLED") {
   301		case "1":
   302			c.CgoEnabled = true
   303		case "0":
   304			c.CgoEnabled = false
   305		default:
   306			// golang.org/issue/5141
   307			// cgo should be disabled for cross compilation builds
   308			if runtime.GOARCH == c.GOARCH && runtime.GOOS == c.GOOS {
   309				c.CgoEnabled = cgoEnabled[c.GOOS+"/"+c.GOARCH]
   310				break
   311			}
   312			c.CgoEnabled = false
   313		}
   314	
   315		return c
   316	}
   317	
   318	func envOr(name, def string) string {
   319		s := os.Getenv(name)
   320		if s == "" {
   321			return def
   322		}
   323		return s
   324	}
   325	
   326	// An ImportMode controls the behavior of the Import method.
   327	type ImportMode uint
   328	
   329	const (
   330		// If FindOnly is set, Import stops after locating the directory
   331		// that should contain the sources for a package.  It does not
   332		// read any files in the directory.
   333		FindOnly ImportMode = 1 << iota
   334	
   335		// If AllowBinary is set, Import can be satisfied by a compiled
   336		// package object without corresponding sources.
   337		AllowBinary
   338	)
   339	
   340	// A Package describes the Go package found in a directory.
   341	type Package struct {
   342		Dir         string   // directory containing package sources
   343		Name        string   // package name
   344		Doc         string   // documentation synopsis
   345		ImportPath  string   // import path of package ("" if unknown)
   346		Root        string   // root of Go tree where this package lives
   347		SrcRoot     string   // package source root directory ("" if unknown)
   348		PkgRoot     string   // package install root directory ("" if unknown)
   349		BinDir      string   // command install directory ("" if unknown)
   350		Goroot      bool     // package found in Go root
   351		PkgObj      string   // installed .a file
   352		AllTags     []string // tags that can influence file selection in this directory
   353		ConflictDir string   // this directory shadows Dir in $GOPATH
   354	
   355		// Source files
   356		GoFiles        []string // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles)
   357		CgoFiles       []string // .go source files that import "C"
   358		IgnoredGoFiles []string // .go source files ignored for this build
   359		CFiles         []string // .c source files
   360		CXXFiles       []string // .cc, .cpp and .cxx source files
   361		HFiles         []string // .h, .hh, .hpp and .hxx source files
   362		SFiles         []string // .s source files
   363		SwigFiles      []string // .swig files
   364		SwigCXXFiles   []string // .swigcxx files
   365		SysoFiles      []string // .syso system object files to add to archive
   366	
   367		// Cgo directives
   368		CgoCFLAGS    []string // Cgo CFLAGS directives
   369		CgoCPPFLAGS  []string // Cgo CPPFLAGS directives
   370		CgoCXXFLAGS  []string // Cgo CXXFLAGS directives
   371		CgoLDFLAGS   []string // Cgo LDFLAGS directives
   372		CgoPkgConfig []string // Cgo pkg-config directives
   373	
   374		// Dependency information
   375		Imports   []string                    // imports from GoFiles, CgoFiles
   376		ImportPos map[string][]token.Position // line information for Imports
   377	
   378		// Test information
   379		TestGoFiles    []string                    // _test.go files in package
   380		TestImports    []string                    // imports from TestGoFiles
   381		TestImportPos  map[string][]token.Position // line information for TestImports
   382		XTestGoFiles   []string                    // _test.go files outside package
   383		XTestImports   []string                    // imports from XTestGoFiles
   384		XTestImportPos map[string][]token.Position // line information for XTestImports
   385	}
   386	
   387	// IsCommand reports whether the package is considered a
   388	// command to be installed (not just a library).
   389	// Packages named "main" are treated as commands.
   390	func (p *Package) IsCommand() bool {
   391		return p.Name == "main"
   392	}
   393	
   394	// ImportDir is like Import but processes the Go package found in
   395	// the named directory.
   396	func (ctxt *Context) ImportDir(dir string, mode ImportMode) (*Package, error) {
   397		return ctxt.Import(".", dir, mode)
   398	}
   399	
   400	// NoGoError is the error used by Import to describe a directory
   401	// containing no buildable Go source files. (It may still contain
   402	// test files, files hidden by build tags, and so on.)
   403	type NoGoError struct {
   404		Dir string
   405	}
   406	
   407	func (e *NoGoError) Error() string {
   408		return "no buildable Go source files in " + e.Dir
   409	}
   410	
   411	func nameExt(name string) string {
   412		i := strings.LastIndex(name, ".")
   413		if i < 0 {
   414			return ""
   415		}
   416		return name[i:]
   417	}
   418	
   419	// Import returns details about the Go package named by the import path,
   420	// interpreting local import paths relative to the srcDir directory.
   421	// If the path is a local import path naming a package that can be imported
   422	// using a standard import path, the returned package will set p.ImportPath
   423	// to that path.
   424	//
   425	// In the directory containing the package, .go, .c, .h, and .s files are
   426	// considered part of the package except for:
   427	//
   428	//	- .go files in package documentation
   429	//	- files starting with _ or . (likely editor temporary files)
   430	//	- files with build constraints not satisfied by the context
   431	//
   432	// If an error occurs, Import returns a non-nil error and a non-nil
   433	// *Package containing partial information.
   434	//
   435	func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Package, error) {
   436		p := &Package{
   437			ImportPath: path,
   438		}
   439		if path == "" {
   440			return p, fmt.Errorf("import %q: invalid import path", path)
   441		}
   442	
   443		var pkga string
   444		var pkgerr error
   445		switch ctxt.Compiler {
   446		case "gccgo":
   447			dir, elem := pathpkg.Split(p.ImportPath)
   448			pkga = "pkg/gccgo_" + ctxt.GOOS + "_" + ctxt.GOARCH + "/" + dir + "lib" + elem + ".a"
   449		case "gc":
   450			suffix := ""
   451			if ctxt.InstallSuffix != "" {
   452				suffix = "_" + ctxt.InstallSuffix
   453			}
   454			pkga = "pkg/" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix + "/" + p.ImportPath + ".a"
   455		default:
   456			// Save error for end of function.
   457			pkgerr = fmt.Errorf("import %q: unknown compiler %q", path, ctxt.Compiler)
   458		}
   459	
   460		binaryOnly := false
   461		if IsLocalImport(path) {
   462			pkga = "" // local imports have no installed path
   463			if srcDir == "" {
   464				return p, fmt.Errorf("import %q: import relative to unknown directory", path)
   465			}
   466			if !ctxt.isAbsPath(path) {
   467				p.Dir = ctxt.joinPath(srcDir, path)
   468			}
   469			// Determine canonical import path, if any.
   470			if ctxt.GOROOT != "" {
   471				root := ctxt.joinPath(ctxt.GOROOT, "src", "pkg")
   472				if sub, ok := ctxt.hasSubdir(root, p.Dir); ok {
   473					p.Goroot = true
   474					p.ImportPath = sub
   475					p.Root = ctxt.GOROOT
   476					goto Found
   477				}
   478			}
   479			all := ctxt.gopath()
   480			for i, root := range all {
   481				rootsrc := ctxt.joinPath(root, "src")
   482				if sub, ok := ctxt.hasSubdir(rootsrc, p.Dir); ok {
   483					// We found a potential import path for dir,
   484					// but check that using it wouldn't find something
   485					// else first.
   486					if ctxt.GOROOT != "" {
   487						if dir := ctxt.joinPath(ctxt.GOROOT, "src", "pkg", sub); ctxt.isDir(dir) {
   488							p.ConflictDir = dir
   489							goto Found
   490						}
   491					}
   492					for _, earlyRoot := range all[:i] {
   493						if dir := ctxt.joinPath(earlyRoot, "src", sub); ctxt.isDir(dir) {
   494							p.ConflictDir = dir
   495							goto Found
   496						}
   497					}
   498	
   499					// sub would not name some other directory instead of this one.
   500					// Record it.
   501					p.ImportPath = sub
   502					p.Root = root
   503					goto Found
   504				}
   505			}
   506			// It's okay that we didn't find a root containing dir.
   507			// Keep going with the information we have.
   508		} else {
   509			if strings.HasPrefix(path, "/") {
   510				return p, fmt.Errorf("import %q: cannot import absolute path", path)
   511			}
   512	
   513			// tried records the location of unsuccessful package lookups
   514			var tried struct {
   515				goroot string
   516				gopath []string
   517			}
   518	
   519			// Determine directory from import path.
   520			if ctxt.GOROOT != "" {
   521				dir := ctxt.joinPath(ctxt.GOROOT, "src", "pkg", path)
   522				isDir := ctxt.isDir(dir)
   523				binaryOnly = !isDir && mode&AllowBinary != 0 && pkga != "" && ctxt.isFile(ctxt.joinPath(ctxt.GOROOT, pkga))
   524				if isDir || binaryOnly {
   525					p.Dir = dir
   526					p.Goroot = true
   527					p.Root = ctxt.GOROOT
   528					goto Found
   529				}
   530				tried.goroot = dir
   531			}
   532			for _, root := range ctxt.gopath() {
   533				dir := ctxt.joinPath(root, "src", path)
   534				isDir := ctxt.isDir(dir)
   535				binaryOnly = !isDir && mode&AllowBinary != 0 && pkga != "" && ctxt.isFile(ctxt.joinPath(root, pkga))
   536				if isDir || binaryOnly {
   537					p.Dir = dir
   538					p.Root = root
   539					goto Found
   540				}
   541				tried.gopath = append(tried.gopath, dir)
   542			}
   543	
   544			// package was not found
   545			var paths []string
   546			if tried.goroot != "" {
   547				paths = append(paths, fmt.Sprintf("\t%s (from $GOROOT)", tried.goroot))
   548			} else {
   549				paths = append(paths, "\t($GOROOT not set)")
   550			}
   551			var i int
   552			var format = "\t%s (from $GOPATH)"
   553			for ; i < len(tried.gopath); i++ {
   554				if i > 0 {
   555					format = "\t%s"
   556				}
   557				paths = append(paths, fmt.Sprintf(format, tried.gopath[i]))
   558			}
   559			if i == 0 {
   560				paths = append(paths, "\t($GOPATH not set)")
   561			}
   562			return p, fmt.Errorf("cannot find package %q in any of:\n%s", path, strings.Join(paths, "\n"))
   563		}
   564	
   565	Found:
   566		if p.Root != "" {
   567			if p.Goroot {
   568				p.SrcRoot = ctxt.joinPath(p.Root, "src", "pkg")
   569			} else {
   570				p.SrcRoot = ctxt.joinPath(p.Root, "src")
   571			}
   572			p.PkgRoot = ctxt.joinPath(p.Root, "pkg")
   573			p.BinDir = ctxt.joinPath(p.Root, "bin")
   574			if pkga != "" {
   575				p.PkgObj = ctxt.joinPath(p.Root, pkga)
   576			}
   577		}
   578	
   579		if mode&FindOnly != 0 {
   580			return p, pkgerr
   581		}
   582		if binaryOnly && (mode&AllowBinary) != 0 {
   583			return p, pkgerr
   584		}
   585	
   586		dirs, err := ctxt.readDir(p.Dir)
   587		if err != nil {
   588			return p, err
   589		}
   590	
   591		var Sfiles []string // files with ".S" (capital S)
   592		var firstFile string
   593		imported := make(map[string][]token.Position)
   594		testImported := make(map[string][]token.Position)
   595		xTestImported := make(map[string][]token.Position)
   596		allTags := make(map[string]bool)
   597		fset := token.NewFileSet()
   598		for _, d := range dirs {
   599			if d.IsDir() {
   600				continue
   601			}
   602	
   603			name := d.Name()
   604			ext := nameExt(name)
   605	
   606			match, data, filename, err := ctxt.matchFile(p.Dir, name, true, allTags)
   607			if err != nil {
   608				return p, err
   609			}
   610			if !match {
   611				if ext == ".go" {
   612					p.IgnoredGoFiles = append(p.IgnoredGoFiles, name)
   613				}
   614				continue
   615			}
   616	
   617			// Going to save the file.  For non-Go files, can stop here.
   618			switch ext {
   619			case ".c":
   620				p.CFiles = append(p.CFiles, name)
   621				continue
   622			case ".cc", ".cpp", ".cxx":
   623				p.CXXFiles = append(p.CXXFiles, name)
   624				continue
   625			case ".h", ".hh", ".hpp", ".hxx":
   626				p.HFiles = append(p.HFiles, name)
   627				continue
   628			case ".s":
   629				p.SFiles = append(p.SFiles, name)
   630				continue
   631			case ".S":
   632				Sfiles = append(Sfiles, name)
   633				continue
   634			case ".swig":
   635				p.SwigFiles = append(p.SwigFiles, name)
   636				continue
   637			case ".swigcxx":
   638				p.SwigCXXFiles = append(p.SwigCXXFiles, name)
   639				continue
   640			case ".syso":
   641				// binary objects to add to package archive
   642				// Likely of the form foo_windows.syso, but
   643				// the name was vetted above with goodOSArchFile.
   644				p.SysoFiles = append(p.SysoFiles, name)
   645				continue
   646			}
   647	
   648			pf, err := parser.ParseFile(fset, filename, data, parser.ImportsOnly|parser.ParseComments)
   649			if err != nil {
   650				return p, err
   651			}
   652	
   653			pkg := pf.Name.Name
   654			if pkg == "documentation" {
   655				p.IgnoredGoFiles = append(p.IgnoredGoFiles, name)
   656				continue
   657			}
   658	
   659			isTest := strings.HasSuffix(name, "_test.go")
   660			isXTest := false
   661			if isTest && strings.HasSuffix(pkg, "_test") {
   662				isXTest = true
   663				pkg = pkg[:len(pkg)-len("_test")]
   664			}
   665	
   666			if p.Name == "" {
   667				p.Name = pkg
   668				firstFile = name
   669			} else if pkg != p.Name {
   670				return p, fmt.Errorf("found packages %s (%s) and %s (%s) in %s", p.Name, firstFile, pkg, name, p.Dir)
   671			}
   672			if pf.Doc != nil && p.Doc == "" {
   673				p.Doc = doc.Synopsis(pf.Doc.Text())
   674			}
   675	
   676			// Record imports and information about cgo.
   677			isCgo := false
   678			for _, decl := range pf.Decls {
   679				d, ok := decl.(*ast.GenDecl)
   680				if !ok {
   681					continue
   682				}
   683				for _, dspec := range d.Specs {
   684					spec, ok := dspec.(*ast.ImportSpec)
   685					if !ok {
   686						continue
   687					}
   688					quoted := spec.Path.Value
   689					path, err := strconv.Unquote(quoted)
   690					if err != nil {
   691						log.Panicf("%s: parser returned invalid quoted string: <%s>", filename, quoted)
   692					}
   693					if isXTest {
   694						xTestImported[path] = append(xTestImported[path], fset.Position(spec.Pos()))
   695					} else if isTest {
   696						testImported[path] = append(testImported[path], fset.Position(spec.Pos()))
   697					} else {
   698						imported[path] = append(imported[path], fset.Position(spec.Pos()))
   699					}
   700					if path == "C" {
   701						if isTest {
   702							return p, fmt.Errorf("use of cgo in test %s not supported", filename)
   703						}
   704						cg := spec.Doc
   705						if cg == nil && len(d.Specs) == 1 {
   706							cg = d.Doc
   707						}
   708						if cg != nil {
   709							if err := ctxt.saveCgo(filename, p, cg); err != nil {
   710								return p, err
   711							}
   712						}
   713						isCgo = true
   714					}
   715				}
   716			}
   717			if isCgo {
   718				allTags["cgo"] = true
   719				if ctxt.CgoEnabled {
   720					p.CgoFiles = append(p.CgoFiles, name)
   721				} else {
   722					p.IgnoredGoFiles = append(p.IgnoredGoFiles, name)
   723				}
   724			} else if isXTest {
   725				p.XTestGoFiles = append(p.XTestGoFiles, name)
   726			} else if isTest {
   727				p.TestGoFiles = append(p.TestGoFiles, name)
   728			} else {
   729				p.GoFiles = append(p.GoFiles, name)
   730			}
   731		}
   732		if len(p.GoFiles)+len(p.CgoFiles)+len(p.TestGoFiles)+len(p.XTestGoFiles) == 0 {
   733			return p, &NoGoError{p.Dir}
   734		}
   735	
   736		for tag := range allTags {
   737			p.AllTags = append(p.AllTags, tag)
   738		}
   739		sort.Strings(p.AllTags)
   740	
   741		p.Imports, p.ImportPos = cleanImports(imported)
   742		p.TestImports, p.TestImportPos = cleanImports(testImported)
   743		p.XTestImports, p.XTestImportPos = cleanImports(xTestImported)
   744	
   745		// add the .S files only if we are using cgo
   746		// (which means gcc will compile them).
   747		// The standard assemblers expect .s files.
   748		if len(p.CgoFiles) > 0 {
   749			p.SFiles = append(p.SFiles, Sfiles...)
   750			sort.Strings(p.SFiles)
   751		}
   752	
   753		return p, pkgerr
   754	}
   755	
   756	// MatchFile reports whether the file with the given name in the given directory
   757	// matches the context and would be included in a Package created by ImportDir
   758	// of that directory.
   759	//
   760	// MatchFile considers the name of the file and may use ctxt.OpenFile to
   761	// read some or all of the file's content.
   762	func (ctxt *Context) MatchFile(dir, name string) (match bool, err error) {
   763		match, _, _, err = ctxt.matchFile(dir, name, false, nil)
   764		return
   765	}
   766	
   767	// matchFile determines whether the file with the given name in the given directory
   768	// should be included in the package being constructed.
   769	// It returns the data read from the file.
   770	// If returnImports is true and name denotes a Go program, matchFile reads
   771	// until the end of the imports (and returns that data) even though it only
   772	// considers text until the first non-comment.
   773	// If allTags is non-nil, matchFile records any encountered build tag
   774	// by setting allTags[tag] = true.
   775	func (ctxt *Context) matchFile(dir, name string, returnImports bool, allTags map[string]bool) (match bool, data []byte, filename string, err error) {
   776		if strings.HasPrefix(name, "_") ||
   777			strings.HasPrefix(name, ".") {
   778			return
   779		}
   780	
   781		i := strings.LastIndex(name, ".")
   782		if i < 0 {
   783			i = len(name)
   784		}
   785		ext := name[i:]
   786	
   787		if !ctxt.goodOSArchFile(name, allTags) && !ctxt.UseAllFiles {
   788			return
   789		}
   790	
   791		switch ext {
   792		case ".go", ".c", ".cc", ".cxx", ".cpp", ".s", ".h", ".hh", ".hpp", ".hxx", ".S", ".swig", ".swigcxx":
   793			// tentatively okay - read to make sure
   794		case ".syso":
   795			// binary, no reading
   796			match = true
   797			return
   798		default:
   799			// skip
   800			return
   801		}
   802	
   803		filename = ctxt.joinPath(dir, name)
   804		f, err := ctxt.openFile(filename)
   805		if err != nil {
   806			return
   807		}
   808	
   809		if strings.HasSuffix(filename, ".go") {
   810			data, err = readImports(f, false)
   811		} else {
   812			data, err = readComments(f)
   813		}
   814		f.Close()
   815		if err != nil {
   816			err = fmt.Errorf("read %s: %v", filename, err)
   817			return
   818		}
   819	
   820		// Look for +build comments to accept or reject the file.
   821		if !ctxt.shouldBuild(data, allTags) && !ctxt.UseAllFiles {
   822			return
   823		}
   824	
   825		match = true
   826		return
   827	}
   828	
   829	func cleanImports(m map[string][]token.Position) ([]string, map[string][]token.Position) {
   830		all := make([]string, 0, len(m))
   831		for path := range m {
   832			all = append(all, path)
   833		}
   834		sort.Strings(all)
   835		return all, m
   836	}
   837	
   838	// Import is shorthand for Default.Import.
   839	func Import(path, srcDir string, mode ImportMode) (*Package, error) {
   840		return Default.Import(path, srcDir, mode)
   841	}
   842	
   843	// ImportDir is shorthand for Default.ImportDir.
   844	func ImportDir(dir string, mode ImportMode) (*Package, error) {
   845		return Default.ImportDir(dir, mode)
   846	}
   847	
   848	var slashslash = []byte("//")
   849	
   850	// shouldBuild reports whether it is okay to use this file,
   851	// The rule is that in the file's leading run of // comments
   852	// and blank lines, which must be followed by a blank line
   853	// (to avoid including a Go package clause doc comment),
   854	// lines beginning with '// +build' are taken as build directives.
   855	//
   856	// The file is accepted only if each such line lists something
   857	// matching the file.  For example:
   858	//
   859	//	// +build windows linux
   860	//
   861	// marks the file as applicable only on Windows and Linux.
   862	//
   863	func (ctxt *Context) shouldBuild(content []byte, allTags map[string]bool) bool {
   864		// Pass 1. Identify leading run of // comments and blank lines,
   865		// which must be followed by a blank line.
   866		end := 0
   867		p := content
   868		for len(p) > 0 {
   869			line := p
   870			if i := bytes.IndexByte(line, '\n'); i >= 0 {
   871				line, p = line[:i], p[i+1:]
   872			} else {
   873				p = p[len(p):]
   874			}
   875			line = bytes.TrimSpace(line)
   876			if len(line) == 0 { // Blank line
   877				end = len(content) - len(p)
   878				continue
   879			}
   880			if !bytes.HasPrefix(line, slashslash) { // Not comment line
   881				break
   882			}
   883		}
   884		content = content[:end]
   885	
   886		// Pass 2.  Process each line in the run.
   887		p = content
   888		allok := true
   889		for len(p) > 0 {
   890			line := p
   891			if i := bytes.IndexByte(line, '\n'); i >= 0 {
   892				line, p = line[:i], p[i+1:]
   893			} else {
   894				p = p[len(p):]
   895			}
   896			line = bytes.TrimSpace(line)
   897			if bytes.HasPrefix(line, slashslash) {
   898				line = bytes.TrimSpace(line[len(slashslash):])
   899				if len(line) > 0 && line[0] == '+' {
   900					// Looks like a comment +line.
   901					f := strings.Fields(string(line))
   902					if f[0] == "+build" {
   903						ok := false
   904						for _, tok := range f[1:] {
   905							if ctxt.match(tok, allTags) {
   906								ok = true
   907							}
   908						}
   909						if !ok {
   910							allok = false
   911						}
   912					}
   913				}
   914			}
   915		}
   916	
   917		return allok
   918	}
   919	
   920	// saveCgo saves the information from the #cgo lines in the import "C" comment.
   921	// These lines set CFLAGS, CPPFLAGS, CXXFLAGS and LDFLAGS and pkg-config directives
   922	// that affect the way cgo's C code is built.
   923	//
   924	// TODO(rsc): This duplicates code in cgo.
   925	// Once the dust settles, remove this code from cgo.
   926	func (ctxt *Context) saveCgo(filename string, di *Package, cg *ast.CommentGroup) error {
   927		text := cg.Text()
   928		for _, line := range strings.Split(text, "\n") {
   929			orig := line
   930	
   931			// Line is
   932			//	#cgo [GOOS/GOARCH...] LDFLAGS: stuff
   933			//
   934			line = strings.TrimSpace(line)
   935			if len(line) < 5 || line[:4] != "#cgo" || (line[4] != ' ' && line[4] != '\t') {
   936				continue
   937			}
   938	
   939			// Split at colon.
   940			line = strings.TrimSpace(line[4:])
   941			i := strings.Index(line, ":")
   942			if i < 0 {
   943				return fmt.Errorf("%s: invalid #cgo line: %s", filename, orig)
   944			}
   945			line, argstr := line[:i], line[i+1:]
   946	
   947			// Parse GOOS/GOARCH stuff.
   948			f := strings.Fields(line)
   949			if len(f) < 1 {
   950				return fmt.Errorf("%s: invalid #cgo line: %s", filename, orig)
   951			}
   952	
   953			cond, verb := f[:len(f)-1], f[len(f)-1]
   954			if len(cond) > 0 {
   955				ok := false
   956				for _, c := range cond {
   957					if ctxt.match(c, nil) {
   958						ok = true
   959						break
   960					}
   961				}
   962				if !ok {
   963					continue
   964				}
   965			}
   966	
   967			args, err := splitQuoted(argstr)
   968			if err != nil {
   969				return fmt.Errorf("%s: invalid #cgo line: %s", filename, orig)
   970			}
   971			for _, arg := range args {
   972				if !safeCgoName(arg) {
   973					return fmt.Errorf("%s: malformed #cgo argument: %s", filename, arg)
   974				}
   975			}
   976	
   977			switch verb {
   978			case "CFLAGS":
   979				di.CgoCFLAGS = append(di.CgoCFLAGS, args...)
   980			case "CPPFLAGS":
   981				di.CgoCPPFLAGS = append(di.CgoCPPFLAGS, args...)
   982			case "CXXFLAGS":
   983				di.CgoCXXFLAGS = append(di.CgoCXXFLAGS, args...)
   984			case "LDFLAGS":
   985				di.CgoLDFLAGS = append(di.CgoLDFLAGS, args...)
   986			case "pkg-config":
   987				di.CgoPkgConfig = append(di.CgoPkgConfig, args...)
   988			default:
   989				return fmt.Errorf("%s: invalid #cgo verb: %s", filename, orig)
   990			}
   991		}
   992		return nil
   993	}
   994	
   995	// NOTE: $ is not safe for the shell, but it is allowed here because of linker options like -Wl,$ORIGIN.
   996	// We never pass these arguments to a shell (just to programs we construct argv for), so this should be okay.
   997	// See golang.org/issue/6038.
   998	var safeBytes = []byte("+-.,/0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz:$")
   999	
  1000	func safeCgoName(s string) bool {
  1001		if s == "" {
  1002			return false
  1003		}
  1004		for i := 0; i < len(s); i++ {
  1005			if c := s[i]; c < 0x80 && bytes.IndexByte(safeBytes, c) < 0 {
  1006				return false
  1007			}
  1008		}
  1009		return true
  1010	}
  1011	
  1012	// splitQuoted splits the string s around each instance of one or more consecutive
  1013	// white space characters while taking into account quotes and escaping, and
  1014	// returns an array of substrings of s or an empty list if s contains only white space.
  1015	// Single quotes and double quotes are recognized to prevent splitting within the
  1016	// quoted region, and are removed from the resulting substrings. If a quote in s
  1017	// isn't closed err will be set and r will have the unclosed argument as the
  1018	// last element.  The backslash is used for escaping.
  1019	//
  1020	// For example, the following string:
  1021	//
  1022	//     a b:"c d" 'e''f'  "g\""
  1023	//
  1024	// Would be parsed as:
  1025	//
  1026	//     []string{"a", "b:c d", "ef", `g"`}
  1027	//
  1028	func splitQuoted(s string) (r []string, err error) {
  1029		var args []string
  1030		arg := make([]rune, len(s))
  1031		escaped := false
  1032		quoted := false
  1033		quote := '\x00'
  1034		i := 0
  1035		for _, rune := range s {
  1036			switch {
  1037			case escaped:
  1038				escaped = false
  1039			case rune == '\\':
  1040				escaped = true
  1041				continue
  1042			case quote != '\x00':
  1043				if rune == quote {
  1044					quote = '\x00'
  1045					continue
  1046				}
  1047			case rune == '"' || rune == '\'':
  1048				quoted = true
  1049				quote = rune
  1050				continue
  1051			case unicode.IsSpace(rune):
  1052				if quoted || i > 0 {
  1053					quoted = false
  1054					args = append(args, string(arg[:i]))
  1055					i = 0
  1056				}
  1057				continue
  1058			}
  1059			arg[i] = rune
  1060			i++
  1061		}
  1062		if quoted || i > 0 {
  1063			args = append(args, string(arg[:i]))
  1064		}
  1065		if quote != 0 {
  1066			err = errors.New("unclosed quote")
  1067		} else if escaped {
  1068			err = errors.New("unfinished escaping")
  1069		}
  1070		return args, err
  1071	}
  1072	
  1073	// match returns true if the name is one of:
  1074	//
  1075	//	$GOOS
  1076	//	$GOARCH
  1077	//	cgo (if cgo is enabled)
  1078	//	!cgo (if cgo is disabled)
  1079	//	ctxt.Compiler
  1080	//	!ctxt.Compiler
  1081	//	tag (if tag is listed in ctxt.BuildTags or ctxt.ReleaseTags)
  1082	//	!tag (if tag is not listed in ctxt.BuildTags or ctxt.ReleaseTags)
  1083	//	a comma-separated list of any of these
  1084	//
  1085	func (ctxt *Context) match(name string, allTags map[string]bool) bool {
  1086		if name == "" {
  1087			if allTags != nil {
  1088				allTags[name] = true
  1089			}
  1090			return false
  1091		}
  1092		if i := strings.Index(name, ","); i >= 0 {
  1093			// comma-separated list
  1094			ok1 := ctxt.match(name[:i], allTags)
  1095			ok2 := ctxt.match(name[i+1:], allTags)
  1096			return ok1 && ok2
  1097		}
  1098		if strings.HasPrefix(name, "!!") { // bad syntax, reject always
  1099			return false
  1100		}
  1101		if strings.HasPrefix(name, "!") { // negation
  1102			return len(name) > 1 && !ctxt.match(name[1:], allTags)
  1103		}
  1104	
  1105		if allTags != nil {
  1106			allTags[name] = true
  1107		}
  1108	
  1109		// Tags must be letters, digits, underscores or dots.
  1110		// Unlike in Go identifiers, all digits are fine (e.g., "386").
  1111		for _, c := range name {
  1112			if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_' && c != '.' {
  1113				return false
  1114			}
  1115		}
  1116	
  1117		// special tags
  1118		if ctxt.CgoEnabled && name == "cgo" {
  1119			return true
  1120		}
  1121		if name == ctxt.GOOS || name == ctxt.GOARCH || name == ctxt.Compiler {
  1122			return true
  1123		}
  1124	
  1125		// other tags
  1126		for _, tag := range ctxt.BuildTags {
  1127			if tag == name {
  1128				return true
  1129			}
  1130		}
  1131		for _, tag := range ctxt.ReleaseTags {
  1132			if tag == name {
  1133				return true
  1134			}
  1135		}
  1136	
  1137		return false
  1138	}
  1139	
  1140	// goodOSArchFile returns false if the name contains a $GOOS or $GOARCH
  1141	// suffix which does not match the current system.
  1142	// The recognized name formats are:
  1143	//
  1144	//     name_$(GOOS).*
  1145	//     name_$(GOARCH).*
  1146	//     name_$(GOOS)_$(GOARCH).*
  1147	//     name_$(GOOS)_test.*
  1148	//     name_$(GOARCH)_test.*
  1149	//     name_$(GOOS)_$(GOARCH)_test.*
  1150	//
  1151	func (ctxt *Context) goodOSArchFile(name string, allTags map[string]bool) bool {
  1152		if dot := strings.Index(name, "."); dot != -1 {
  1153			name = name[:dot]
  1154		}
  1155		l := strings.Split(name, "_")
  1156		if n := len(l); n > 0 && l[n-1] == "test" {
  1157			l = l[:n-1]
  1158		}
  1159		n := len(l)
  1160		if n >= 2 && knownOS[l[n-2]] && knownArch[l[n-1]] {
  1161			if allTags != nil {
  1162				allTags[l[n-2]] = true
  1163				allTags[l[n-1]] = true
  1164			}
  1165			return l[n-2] == ctxt.GOOS && l[n-1] == ctxt.GOARCH
  1166		}
  1167		if n >= 1 && knownOS[l[n-1]] {
  1168			if allTags != nil {
  1169				allTags[l[n-1]] = true
  1170			}
  1171			return l[n-1] == ctxt.GOOS
  1172		}
  1173		if n >= 1 && knownArch[l[n-1]] {
  1174			if allTags != nil {
  1175				allTags[l[n-1]] = true
  1176			}
  1177			return l[n-1] == ctxt.GOARCH
  1178		}
  1179		return true
  1180	}
  1181	
  1182	var knownOS = make(map[string]bool)
  1183	var knownArch = make(map[string]bool)
  1184	
  1185	func init() {
  1186		for _, v := range strings.Fields(goosList) {
  1187			knownOS[v] = true
  1188		}
  1189		for _, v := range strings.Fields(goarchList) {
  1190			knownArch[v] = true
  1191		}
  1192	}
  1193	
  1194	// ToolDir is the directory containing build tools.
  1195	var ToolDir = filepath.Join(runtime.GOROOT(), "pkg/tool/"+runtime.GOOS+"_"+runtime.GOARCH)
  1196	
  1197	// IsLocalImport reports whether the import path is
  1198	// a local import path, like ".", "..", "./foo", or "../foo".
  1199	func IsLocalImport(path string) bool {
  1200		return path == "." || path == ".." ||
  1201			strings.HasPrefix(path, "./") || strings.HasPrefix(path, "../")
  1202	}
  1203	
  1204	// ArchChar returns the architecture character for the given goarch.
  1205	// For example, ArchChar("amd64") returns "6".
  1206	func ArchChar(goarch string) (string, error) {
  1207		switch goarch {
  1208		case "386":
  1209			return "8", nil
  1210		case "amd64":
  1211			return "6", nil
  1212		case "arm":
  1213			return "5", nil
  1214		}
  1215		return "", errors.New("unsupported GOARCH " + goarch)
  1216	}

View as plain text