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		"freebsd/386":   true,
   264		"freebsd/amd64": true,
   265		"freebsd/arm":   true,
   266		"linux/386":     true,
   267		"linux/amd64":   true,
   268		"linux/arm":     true,
   269		"netbsd/386":    true,
   270		"netbsd/amd64":  true,
   271		"netbsd/arm":    true,
   272		"openbsd/386":   true,
   273		"openbsd/amd64": true,
   274		"windows/386":   true,
   275		"windows/amd64": true,
   276	}
   277	
   278	func defaultContext() Context {
   279		var c Context
   280	
   281		c.GOARCH = envOr("GOARCH", runtime.GOARCH)
   282		c.GOOS = envOr("GOOS", runtime.GOOS)
   283		c.GOROOT = runtime.GOROOT()
   284		c.GOPATH = envOr("GOPATH", "")
   285		c.Compiler = runtime.Compiler
   286	
   287		// Each major Go release in the Go 1.x series should add a tag here.
   288		// Old tags should not be removed. That is, the go1.x tag is present
   289		// in all releases >= Go 1.x. Code that requires Go 1.x or later should
   290		// say "+build go1.x", and code that should only be built before Go 1.x
   291		// (perhaps it is the stub to use in that case) should say "+build !go1.x".
   292		//
   293		// When we reach Go 1.3 the line will read
   294		//	c.ReleaseTags = []string{"go1.1", "go1.2", "go1.3"}
   295		// and so on.
   296		c.ReleaseTags = []string{"go1.1"}
   297	
   298		switch os.Getenv("CGO_ENABLED") {
   299		case "1":
   300			c.CgoEnabled = true
   301		case "0":
   302			c.CgoEnabled = false
   303		default:
   304			// golang.org/issue/5141
   305			// cgo should be disabled for cross compilation builds
   306			if runtime.GOARCH == c.GOARCH && runtime.GOOS == c.GOOS {
   307				c.CgoEnabled = cgoEnabled[c.GOOS+"/"+c.GOARCH]
   308				break
   309			}
   310			c.CgoEnabled = false
   311		}
   312	
   313		return c
   314	}
   315	
   316	func envOr(name, def string) string {
   317		s := os.Getenv(name)
   318		if s == "" {
   319			return def
   320		}
   321		return s
   322	}
   323	
   324	// An ImportMode controls the behavior of the Import method.
   325	type ImportMode uint
   326	
   327	const (
   328		// If FindOnly is set, Import stops after locating the directory
   329		// that should contain the sources for a package.  It does not
   330		// read any files in the directory.
   331		FindOnly ImportMode = 1 << iota
   332	
   333		// If AllowBinary is set, Import can be satisfied by a compiled
   334		// package object without corresponding sources.
   335		AllowBinary
   336	)
   337	
   338	// A Package describes the Go package found in a directory.
   339	type Package struct {
   340		Dir        string // directory containing package sources
   341		Name       string // package name
   342		Doc        string // documentation synopsis
   343		ImportPath string // import path of package ("" if unknown)
   344		Root       string // root of Go tree where this package lives
   345		SrcRoot    string // package source root directory ("" if unknown)
   346		PkgRoot    string // package install root directory ("" if unknown)
   347		BinDir     string // command install directory ("" if unknown)
   348		Goroot     bool   // package found in Go root
   349		PkgObj     string // installed .a file
   350	
   351		// Source files
   352		GoFiles        []string // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles)
   353		CgoFiles       []string // .go source files that import "C"
   354		IgnoredGoFiles []string // .go source files ignored for this build
   355		CFiles         []string // .c source files
   356		HFiles         []string // .h source files
   357		SFiles         []string // .s source files
   358		SysoFiles      []string // .syso system object files to add to archive
   359		SwigFiles      []string // .swig files
   360		SwigCXXFiles   []string // .swigcxx files
   361	
   362		// Cgo directives
   363		CgoPkgConfig []string // Cgo pkg-config directives
   364		CgoCFLAGS    []string // Cgo CFLAGS directives
   365		CgoLDFLAGS   []string // Cgo LDFLAGS directives
   366	
   367		// Dependency information
   368		Imports   []string                    // imports from GoFiles, CgoFiles
   369		ImportPos map[string][]token.Position // line information for Imports
   370	
   371		// Test information
   372		TestGoFiles    []string                    // _test.go files in package
   373		TestImports    []string                    // imports from TestGoFiles
   374		TestImportPos  map[string][]token.Position // line information for TestImports
   375		XTestGoFiles   []string                    // _test.go files outside package
   376		XTestImports   []string                    // imports from XTestGoFiles
   377		XTestImportPos map[string][]token.Position // line information for XTestImports
   378	}
   379	
   380	// IsCommand reports whether the package is considered a
   381	// command to be installed (not just a library).
   382	// Packages named "main" are treated as commands.
   383	func (p *Package) IsCommand() bool {
   384		return p.Name == "main"
   385	}
   386	
   387	// ImportDir is like Import but processes the Go package found in
   388	// the named directory.
   389	func (ctxt *Context) ImportDir(dir string, mode ImportMode) (*Package, error) {
   390		return ctxt.Import(".", dir, mode)
   391	}
   392	
   393	// NoGoError is the error used by Import to describe a directory
   394	// containing no Go source files.
   395	type NoGoError struct {
   396		Dir string
   397	}
   398	
   399	func (e *NoGoError) Error() string {
   400		return "no Go source files in " + e.Dir
   401	}
   402	
   403	// Import returns details about the Go package named by the import path,
   404	// interpreting local import paths relative to the srcDir directory.
   405	// If the path is a local import path naming a package that can be imported
   406	// using a standard import path, the returned package will set p.ImportPath
   407	// to that path.
   408	//
   409	// In the directory containing the package, .go, .c, .h, and .s files are
   410	// considered part of the package except for:
   411	//
   412	//	- .go files in package documentation
   413	//	- files starting with _ or . (likely editor temporary files)
   414	//	- files with build constraints not satisfied by the context
   415	//
   416	// If an error occurs, Import returns a non-nil error and a non-nil
   417	// *Package containing partial information.
   418	//
   419	func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Package, error) {
   420		p := &Package{
   421			ImportPath: path,
   422		}
   423		if path == "" {
   424			return p, fmt.Errorf("import %q: invalid import path", path)
   425		}
   426	
   427		var pkga string
   428		var pkgerr error
   429		switch ctxt.Compiler {
   430		case "gccgo":
   431			dir, elem := pathpkg.Split(p.ImportPath)
   432			pkga = "pkg/gccgo/" + dir + "lib" + elem + ".a"
   433		case "gc":
   434			suffix := ""
   435			if ctxt.InstallSuffix != "" {
   436				suffix = "_" + ctxt.InstallSuffix
   437			}
   438			pkga = "pkg/" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix + "/" + p.ImportPath + ".a"
   439		default:
   440			// Save error for end of function.
   441			pkgerr = fmt.Errorf("import %q: unknown compiler %q", path, ctxt.Compiler)
   442		}
   443	
   444		binaryOnly := false
   445		if IsLocalImport(path) {
   446			pkga = "" // local imports have no installed path
   447			if srcDir == "" {
   448				return p, fmt.Errorf("import %q: import relative to unknown directory", path)
   449			}
   450			if !ctxt.isAbsPath(path) {
   451				p.Dir = ctxt.joinPath(srcDir, path)
   452			}
   453			// Determine canonical import path, if any.
   454			if ctxt.GOROOT != "" {
   455				root := ctxt.joinPath(ctxt.GOROOT, "src", "pkg")
   456				if sub, ok := ctxt.hasSubdir(root, p.Dir); ok {
   457					p.Goroot = true
   458					p.ImportPath = sub
   459					p.Root = ctxt.GOROOT
   460					goto Found
   461				}
   462			}
   463			all := ctxt.gopath()
   464			for i, root := range all {
   465				rootsrc := ctxt.joinPath(root, "src")
   466				if sub, ok := ctxt.hasSubdir(rootsrc, p.Dir); ok {
   467					// We found a potential import path for dir,
   468					// but check that using it wouldn't find something
   469					// else first.
   470					if ctxt.GOROOT != "" {
   471						if dir := ctxt.joinPath(ctxt.GOROOT, "src", "pkg", sub); ctxt.isDir(dir) {
   472							goto Found
   473						}
   474					}
   475					for _, earlyRoot := range all[:i] {
   476						if dir := ctxt.joinPath(earlyRoot, "src", sub); ctxt.isDir(dir) {
   477							goto Found
   478						}
   479					}
   480	
   481					// sub would not name some other directory instead of this one.
   482					// Record it.
   483					p.ImportPath = sub
   484					p.Root = root
   485					goto Found
   486				}
   487			}
   488			// It's okay that we didn't find a root containing dir.
   489			// Keep going with the information we have.
   490		} else {
   491			if strings.HasPrefix(path, "/") {
   492				return p, fmt.Errorf("import %q: cannot import absolute path", path)
   493			}
   494	
   495			// tried records the location of unsuccessful package lookups
   496			var tried struct {
   497				goroot string
   498				gopath []string
   499			}
   500	
   501			// Determine directory from import path.
   502			if ctxt.GOROOT != "" {
   503				dir := ctxt.joinPath(ctxt.GOROOT, "src", "pkg", path)
   504				isDir := ctxt.isDir(dir)
   505				binaryOnly = !isDir && mode&AllowBinary != 0 && pkga != "" && ctxt.isFile(ctxt.joinPath(ctxt.GOROOT, pkga))
   506				if isDir || binaryOnly {
   507					p.Dir = dir
   508					p.Goroot = true
   509					p.Root = ctxt.GOROOT
   510					goto Found
   511				}
   512				tried.goroot = dir
   513			}
   514			for _, root := range ctxt.gopath() {
   515				dir := ctxt.joinPath(root, "src", path)
   516				isDir := ctxt.isDir(dir)
   517				binaryOnly = !isDir && mode&AllowBinary != 0 && pkga != "" && ctxt.isFile(ctxt.joinPath(root, pkga))
   518				if isDir || binaryOnly {
   519					p.Dir = dir
   520					p.Root = root
   521					goto Found
   522				}
   523				tried.gopath = append(tried.gopath, dir)
   524			}
   525	
   526			// package was not found
   527			var paths []string
   528			if tried.goroot != "" {
   529				paths = append(paths, fmt.Sprintf("\t%s (from $GOROOT)", tried.goroot))
   530			} else {
   531				paths = append(paths, "\t($GOROOT not set)")
   532			}
   533			var i int
   534			var format = "\t%s (from $GOPATH)"
   535			for ; i < len(tried.gopath); i++ {
   536				if i > 0 {
   537					format = "\t%s"
   538				}
   539				paths = append(paths, fmt.Sprintf(format, tried.gopath[i]))
   540			}
   541			if i == 0 {
   542				paths = append(paths, "\t($GOPATH not set)")
   543			}
   544			return p, fmt.Errorf("cannot find package %q in any of:\n%s", path, strings.Join(paths, "\n"))
   545		}
   546	
   547	Found:
   548		if p.Root != "" {
   549			if p.Goroot {
   550				p.SrcRoot = ctxt.joinPath(p.Root, "src", "pkg")
   551			} else {
   552				p.SrcRoot = ctxt.joinPath(p.Root, "src")
   553			}
   554			p.PkgRoot = ctxt.joinPath(p.Root, "pkg")
   555			p.BinDir = ctxt.joinPath(p.Root, "bin")
   556			if pkga != "" {
   557				p.PkgObj = ctxt.joinPath(p.Root, pkga)
   558			}
   559		}
   560	
   561		if mode&FindOnly != 0 {
   562			return p, pkgerr
   563		}
   564		if binaryOnly && (mode&AllowBinary) != 0 {
   565			return p, pkgerr
   566		}
   567	
   568		dirs, err := ctxt.readDir(p.Dir)
   569		if err != nil {
   570			return p, err
   571		}
   572	
   573		var Sfiles []string // files with ".S" (capital S)
   574		var firstFile string
   575		imported := make(map[string][]token.Position)
   576		testImported := make(map[string][]token.Position)
   577		xTestImported := make(map[string][]token.Position)
   578		fset := token.NewFileSet()
   579		for _, d := range dirs {
   580			if d.IsDir() {
   581				continue
   582			}
   583			name := d.Name()
   584			if strings.HasPrefix(name, "_") ||
   585				strings.HasPrefix(name, ".") {
   586				continue
   587			}
   588	
   589			i := strings.LastIndex(name, ".")
   590			if i < 0 {
   591				i = len(name)
   592			}
   593			ext := name[i:]
   594	
   595			if !ctxt.UseAllFiles && !ctxt.goodOSArchFile(name) {
   596				if ext == ".go" {
   597					p.IgnoredGoFiles = append(p.IgnoredGoFiles, name)
   598				}
   599				continue
   600			}
   601	
   602			switch ext {
   603			case ".go", ".c", ".s", ".h", ".S", ".swig", ".swigcxx":
   604				// tentatively okay - read to make sure
   605			case ".syso":
   606				// binary objects to add to package archive
   607				// Likely of the form foo_windows.syso, but
   608				// the name was vetted above with goodOSArchFile.
   609				p.SysoFiles = append(p.SysoFiles, name)
   610				continue
   611			default:
   612				// skip
   613				continue
   614			}
   615	
   616			filename := ctxt.joinPath(p.Dir, name)
   617			f, err := ctxt.openFile(filename)
   618			if err != nil {
   619				return p, err
   620			}
   621	
   622			var data []byte
   623			if strings.HasSuffix(filename, ".go") {
   624				data, err = readImports(f, false)
   625			} else {
   626				data, err = readComments(f)
   627			}
   628			f.Close()
   629			if err != nil {
   630				return p, fmt.Errorf("read %s: %v", filename, err)
   631			}
   632	
   633			// Look for +build comments to accept or reject the file.
   634			if !ctxt.UseAllFiles && !ctxt.shouldBuild(data) {
   635				if ext == ".go" {
   636					p.IgnoredGoFiles = append(p.IgnoredGoFiles, name)
   637				}
   638				continue
   639			}
   640	
   641			// Going to save the file.  For non-Go files, can stop here.
   642			switch ext {
   643			case ".c":
   644				p.CFiles = append(p.CFiles, name)
   645				continue
   646			case ".h":
   647				p.HFiles = append(p.HFiles, name)
   648				continue
   649			case ".s":
   650				p.SFiles = append(p.SFiles, name)
   651				continue
   652			case ".S":
   653				Sfiles = append(Sfiles, name)
   654				continue
   655			case ".swig":
   656				p.SwigFiles = append(p.SwigFiles, name)
   657				continue
   658			case ".swigcxx":
   659				p.SwigCXXFiles = append(p.SwigCXXFiles, name)
   660				continue
   661			}
   662	
   663			pf, err := parser.ParseFile(fset, filename, data, parser.ImportsOnly|parser.ParseComments)
   664			if err != nil {
   665				return p, err
   666			}
   667	
   668			pkg := pf.Name.Name
   669			if pkg == "documentation" {
   670				p.IgnoredGoFiles = append(p.IgnoredGoFiles, name)
   671				continue
   672			}
   673	
   674			isTest := strings.HasSuffix(name, "_test.go")
   675			isXTest := false
   676			if isTest && strings.HasSuffix(pkg, "_test") {
   677				isXTest = true
   678				pkg = pkg[:len(pkg)-len("_test")]
   679			}
   680	
   681			if p.Name == "" {
   682				p.Name = pkg
   683				firstFile = name
   684			} else if pkg != p.Name {
   685				return p, fmt.Errorf("found packages %s (%s) and %s (%s) in %s", p.Name, firstFile, pkg, name, p.Dir)
   686			}
   687			if pf.Doc != nil && p.Doc == "" {
   688				p.Doc = doc.Synopsis(pf.Doc.Text())
   689			}
   690	
   691			// Record imports and information about cgo.
   692			isCgo := false
   693			for _, decl := range pf.Decls {
   694				d, ok := decl.(*ast.GenDecl)
   695				if !ok {
   696					continue
   697				}
   698				for _, dspec := range d.Specs {
   699					spec, ok := dspec.(*ast.ImportSpec)
   700					if !ok {
   701						continue
   702					}
   703					quoted := spec.Path.Value
   704					path, err := strconv.Unquote(quoted)
   705					if err != nil {
   706						log.Panicf("%s: parser returned invalid quoted string: <%s>", filename, quoted)
   707					}
   708					if isXTest {
   709						xTestImported[path] = append(xTestImported[path], fset.Position(spec.Pos()))
   710					} else if isTest {
   711						testImported[path] = append(testImported[path], fset.Position(spec.Pos()))
   712					} else {
   713						imported[path] = append(imported[path], fset.Position(spec.Pos()))
   714					}
   715					if path == "C" {
   716						if isTest {
   717							return p, fmt.Errorf("use of cgo in test %s not supported", filename)
   718						}
   719						cg := spec.Doc
   720						if cg == nil && len(d.Specs) == 1 {
   721							cg = d.Doc
   722						}
   723						if cg != nil {
   724							if err := ctxt.saveCgo(filename, p, cg); err != nil {
   725								return p, err
   726							}
   727						}
   728						isCgo = true
   729					}
   730				}
   731			}
   732			if isCgo {
   733				if ctxt.CgoEnabled {
   734					p.CgoFiles = append(p.CgoFiles, name)
   735				}
   736			} else if isXTest {
   737				p.XTestGoFiles = append(p.XTestGoFiles, name)
   738			} else if isTest {
   739				p.TestGoFiles = append(p.TestGoFiles, name)
   740			} else {
   741				p.GoFiles = append(p.GoFiles, name)
   742			}
   743		}
   744		if p.Name == "" {
   745			return p, &NoGoError{p.Dir}
   746		}
   747	
   748		p.Imports, p.ImportPos = cleanImports(imported)
   749		p.TestImports, p.TestImportPos = cleanImports(testImported)
   750		p.XTestImports, p.XTestImportPos = cleanImports(xTestImported)
   751	
   752		// add the .S files only if we are using cgo
   753		// (which means gcc will compile them).
   754		// The standard assemblers expect .s files.
   755		if len(p.CgoFiles) > 0 {
   756			p.SFiles = append(p.SFiles, Sfiles...)
   757			sort.Strings(p.SFiles)
   758		}
   759	
   760		return p, pkgerr
   761	}
   762	
   763	func cleanImports(m map[string][]token.Position) ([]string, map[string][]token.Position) {
   764		all := make([]string, 0, len(m))
   765		for path := range m {
   766			all = append(all, path)
   767		}
   768		sort.Strings(all)
   769		return all, m
   770	}
   771	
   772	// Import is shorthand for Default.Import.
   773	func Import(path, srcDir string, mode ImportMode) (*Package, error) {
   774		return Default.Import(path, srcDir, mode)
   775	}
   776	
   777	// ImportDir is shorthand for Default.ImportDir.
   778	func ImportDir(dir string, mode ImportMode) (*Package, error) {
   779		return Default.ImportDir(dir, mode)
   780	}
   781	
   782	var slashslash = []byte("//")
   783	
   784	// shouldBuild reports whether it is okay to use this file,
   785	// The rule is that in the file's leading run of // comments
   786	// and blank lines, which must be followed by a blank line
   787	// (to avoid including a Go package clause doc comment),
   788	// lines beginning with '// +build' are taken as build directives.
   789	//
   790	// The file is accepted only if each such line lists something
   791	// matching the file.  For example:
   792	//
   793	//	// +build windows linux
   794	//
   795	// marks the file as applicable only on Windows and Linux.
   796	//
   797	func (ctxt *Context) shouldBuild(content []byte) bool {
   798		// Pass 1. Identify leading run of // comments and blank lines,
   799		// which must be followed by a blank line.
   800		end := 0
   801		p := content
   802		for len(p) > 0 {
   803			line := p
   804			if i := bytes.IndexByte(line, '\n'); i >= 0 {
   805				line, p = line[:i], p[i+1:]
   806			} else {
   807				p = p[len(p):]
   808			}
   809			line = bytes.TrimSpace(line)
   810			if len(line) == 0 { // Blank line
   811				end = len(content) - len(p)
   812				continue
   813			}
   814			if !bytes.HasPrefix(line, slashslash) { // Not comment line
   815				break
   816			}
   817		}
   818		content = content[:end]
   819	
   820		// Pass 2.  Process each line in the run.
   821		p = content
   822		for len(p) > 0 {
   823			line := p
   824			if i := bytes.IndexByte(line, '\n'); i >= 0 {
   825				line, p = line[:i], p[i+1:]
   826			} else {
   827				p = p[len(p):]
   828			}
   829			line = bytes.TrimSpace(line)
   830			if bytes.HasPrefix(line, slashslash) {
   831				line = bytes.TrimSpace(line[len(slashslash):])
   832				if len(line) > 0 && line[0] == '+' {
   833					// Looks like a comment +line.
   834					f := strings.Fields(string(line))
   835					if f[0] == "+build" {
   836						ok := false
   837						for _, tok := range f[1:] {
   838							if ctxt.match(tok) {
   839								ok = true
   840								break
   841							}
   842						}
   843						if !ok {
   844							return false // this one doesn't match
   845						}
   846					}
   847				}
   848			}
   849		}
   850		return true // everything matches
   851	}
   852	
   853	// saveCgo saves the information from the #cgo lines in the import "C" comment.
   854	// These lines set CFLAGS and LDFLAGS and pkg-config directives that affect
   855	// the way cgo's C code is built.
   856	//
   857	// TODO(rsc): This duplicates code in cgo.
   858	// Once the dust settles, remove this code from cgo.
   859	func (ctxt *Context) saveCgo(filename string, di *Package, cg *ast.CommentGroup) error {
   860		text := cg.Text()
   861		for _, line := range strings.Split(text, "\n") {
   862			orig := line
   863	
   864			// Line is
   865			//	#cgo [GOOS/GOARCH...] LDFLAGS: stuff
   866			//
   867			line = strings.TrimSpace(line)
   868			if len(line) < 5 || line[:4] != "#cgo" || (line[4] != ' ' && line[4] != '\t') {
   869				continue
   870			}
   871	
   872			// Split at colon.
   873			line = strings.TrimSpace(line[4:])
   874			i := strings.Index(line, ":")
   875			if i < 0 {
   876				return fmt.Errorf("%s: invalid #cgo line: %s", filename, orig)
   877			}
   878			line, argstr := line[:i], line[i+1:]
   879	
   880			// Parse GOOS/GOARCH stuff.
   881			f := strings.Fields(line)
   882			if len(f) < 1 {
   883				return fmt.Errorf("%s: invalid #cgo line: %s", filename, orig)
   884			}
   885	
   886			cond, verb := f[:len(f)-1], f[len(f)-1]
   887			if len(cond) > 0 {
   888				ok := false
   889				for _, c := range cond {
   890					if ctxt.match(c) {
   891						ok = true
   892						break
   893					}
   894				}
   895				if !ok {
   896					continue
   897				}
   898			}
   899	
   900			args, err := splitQuoted(argstr)
   901			if err != nil {
   902				return fmt.Errorf("%s: invalid #cgo line: %s", filename, orig)
   903			}
   904			for _, arg := range args {
   905				if !safeName(arg) {
   906					return fmt.Errorf("%s: malformed #cgo argument: %s", filename, arg)
   907				}
   908			}
   909	
   910			switch verb {
   911			case "CFLAGS":
   912				di.CgoCFLAGS = append(di.CgoCFLAGS, args...)
   913			case "LDFLAGS":
   914				di.CgoLDFLAGS = append(di.CgoLDFLAGS, args...)
   915			case "pkg-config":
   916				di.CgoPkgConfig = append(di.CgoPkgConfig, args...)
   917			default:
   918				return fmt.Errorf("%s: invalid #cgo verb: %s", filename, orig)
   919			}
   920		}
   921		return nil
   922	}
   923	
   924	var safeBytes = []byte("+-.,/0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz:")
   925	
   926	func safeName(s string) bool {
   927		if s == "" {
   928			return false
   929		}
   930		for i := 0; i < len(s); i++ {
   931			if c := s[i]; c < 0x80 && bytes.IndexByte(safeBytes, c) < 0 {
   932				return false
   933			}
   934		}
   935		return true
   936	}
   937	
   938	// splitQuoted splits the string s around each instance of one or more consecutive
   939	// white space characters while taking into account quotes and escaping, and
   940	// returns an array of substrings of s or an empty list if s contains only white space.
   941	// Single quotes and double quotes are recognized to prevent splitting within the
   942	// quoted region, and are removed from the resulting substrings. If a quote in s
   943	// isn't closed err will be set and r will have the unclosed argument as the
   944	// last element.  The backslash is used for escaping.
   945	//
   946	// For example, the following string:
   947	//
   948	//     a b:"c d" 'e''f'  "g\""
   949	//
   950	// Would be parsed as:
   951	//
   952	//     []string{"a", "b:c d", "ef", `g"`}
   953	//
   954	func splitQuoted(s string) (r []string, err error) {
   955		var args []string
   956		arg := make([]rune, len(s))
   957		escaped := false
   958		quoted := false
   959		quote := '\x00'
   960		i := 0
   961		for _, rune := range s {
   962			switch {
   963			case escaped:
   964				escaped = false
   965			case rune == '\\':
   966				escaped = true
   967				continue
   968			case quote != '\x00':
   969				if rune == quote {
   970					quote = '\x00'
   971					continue
   972				}
   973			case rune == '"' || rune == '\'':
   974				quoted = true
   975				quote = rune
   976				continue
   977			case unicode.IsSpace(rune):
   978				if quoted || i > 0 {
   979					quoted = false
   980					args = append(args, string(arg[:i]))
   981					i = 0
   982				}
   983				continue
   984			}
   985			arg[i] = rune
   986			i++
   987		}
   988		if quoted || i > 0 {
   989			args = append(args, string(arg[:i]))
   990		}
   991		if quote != 0 {
   992			err = errors.New("unclosed quote")
   993		} else if escaped {
   994			err = errors.New("unfinished escaping")
   995		}
   996		return args, err
   997	}
   998	
   999	// match returns true if the name is one of:
  1000	//
  1001	//	$GOOS
  1002	//	$GOARCH
  1003	//	cgo (if cgo is enabled)
  1004	//	!cgo (if cgo is disabled)
  1005	//	ctxt.Compiler
  1006	//	!ctxt.Compiler
  1007	//	tag (if tag is listed in ctxt.BuildTags or ctxt.ReleaseTags)
  1008	//	!tag (if tag is not listed in ctxt.BuildTags or ctxt.ReleaseTags)
  1009	//	a comma-separated list of any of these
  1010	//
  1011	func (ctxt *Context) match(name string) bool {
  1012		if name == "" {
  1013			return false
  1014		}
  1015		if i := strings.Index(name, ","); i >= 0 {
  1016			// comma-separated list
  1017			return ctxt.match(name[:i]) && ctxt.match(name[i+1:])
  1018		}
  1019		if strings.HasPrefix(name, "!!") { // bad syntax, reject always
  1020			return false
  1021		}
  1022		if strings.HasPrefix(name, "!") { // negation
  1023			return len(name) > 1 && !ctxt.match(name[1:])
  1024		}
  1025	
  1026		// Tags must be letters, digits, underscores or dots.
  1027		// Unlike in Go identifiers, all digits are fine (e.g., "386").
  1028		for _, c := range name {
  1029			if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_' && c != '.' {
  1030				return false
  1031			}
  1032		}
  1033	
  1034		// special tags
  1035		if ctxt.CgoEnabled && name == "cgo" {
  1036			return true
  1037		}
  1038		if name == ctxt.GOOS || name == ctxt.GOARCH || name == ctxt.Compiler {
  1039			return true
  1040		}
  1041	
  1042		// other tags
  1043		for _, tag := range ctxt.BuildTags {
  1044			if tag == name {
  1045				return true
  1046			}
  1047		}
  1048		for _, tag := range ctxt.ReleaseTags {
  1049			if tag == name {
  1050				return true
  1051			}
  1052		}
  1053	
  1054		return false
  1055	}
  1056	
  1057	// goodOSArchFile returns false if the name contains a $GOOS or $GOARCH
  1058	// suffix which does not match the current system.
  1059	// The recognized name formats are:
  1060	//
  1061	//     name_$(GOOS).*
  1062	//     name_$(GOARCH).*
  1063	//     name_$(GOOS)_$(GOARCH).*
  1064	//     name_$(GOOS)_test.*
  1065	//     name_$(GOARCH)_test.*
  1066	//     name_$(GOOS)_$(GOARCH)_test.*
  1067	//
  1068	func (ctxt *Context) goodOSArchFile(name string) bool {
  1069		if dot := strings.Index(name, "."); dot != -1 {
  1070			name = name[:dot]
  1071		}
  1072		l := strings.Split(name, "_")
  1073		if n := len(l); n > 0 && l[n-1] == "test" {
  1074			l = l[:n-1]
  1075		}
  1076		n := len(l)
  1077		if n >= 2 && knownOS[l[n-2]] && knownArch[l[n-1]] {
  1078			return l[n-2] == ctxt.GOOS && l[n-1] == ctxt.GOARCH
  1079		}
  1080		if n >= 1 && knownOS[l[n-1]] {
  1081			return l[n-1] == ctxt.GOOS
  1082		}
  1083		if n >= 1 && knownArch[l[n-1]] {
  1084			return l[n-1] == ctxt.GOARCH
  1085		}
  1086		return true
  1087	}
  1088	
  1089	var knownOS = make(map[string]bool)
  1090	var knownArch = make(map[string]bool)
  1091	
  1092	func init() {
  1093		for _, v := range strings.Fields(goosList) {
  1094			knownOS[v] = true
  1095		}
  1096		for _, v := range strings.Fields(goarchList) {
  1097			knownArch[v] = true
  1098		}
  1099	}
  1100	
  1101	// ToolDir is the directory containing build tools.
  1102	var ToolDir = filepath.Join(runtime.GOROOT(), "pkg/tool/"+runtime.GOOS+"_"+runtime.GOARCH)
  1103	
  1104	// IsLocalImport reports whether the import path is
  1105	// a local import path, like ".", "..", "./foo", or "../foo".
  1106	func IsLocalImport(path string) bool {
  1107		return path == "." || path == ".." ||
  1108			strings.HasPrefix(path, "./") || strings.HasPrefix(path, "../")
  1109	}
  1110	
  1111	// ArchChar returns the architecture character for the given goarch.
  1112	// For example, ArchChar("amd64") returns "6".
  1113	func ArchChar(goarch string) (string, error) {
  1114		switch goarch {
  1115		case "386":
  1116			return "8", nil
  1117		case "amd64":
  1118			return "6", nil
  1119		case "arm":
  1120			return "5", nil
  1121		}
  1122		return "", errors.New("unsupported GOARCH " + goarch)
  1123	}

View as plain text