833 lines · 20.3 KB · Go
RawDownload
// Copyright 2011 The Go Authors.  All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file. // Binary api computes the exported API of a set of Go packages.package main import (	"bufio"	"bytes"	"flag"	"fmt"	"go/ast"	"go/build"	"go/parser"	"go/token"	"go/types"	"io"	"io/ioutil"	"log"	"os"	"os/exec"	"path/filepath"	"regexp"	"runtime"	"sort"	"strings") // Flagsvar (	checkFile  = flag.String("c", "", "optional comma-separated filename(s) to check API against")	allowNew   = flag.Bool("allow_new", true, "allow API additions")	exceptFile = flag.String("except", "", "optional filename of packages that are allowed to change without triggering a failure in the tool")	nextFile   = flag.String("next", "", "optional filename of tentative upcoming API features for the next release. This file can be lazily maintained. It only affects the delta warnings from the -c file printed on success.")	verbose    = flag.Bool("v", false, "verbose debugging")	forceCtx   = flag.String("contexts", "", "optional comma-separated list of <goos>-<goarch>[-cgo] to override default contexts.")) // contexts are the default contexts which are scanned, unless// overridden by the -contexts flag.var contexts = []*build.Context{	{GOOS: "linux", GOARCH: "386", CgoEnabled: true},	{GOOS: "linux", GOARCH: "386"},	{GOOS: "linux", GOARCH: "amd64", CgoEnabled: true},	{GOOS: "linux", GOARCH: "amd64"},	{GOOS: "linux", GOARCH: "arm", CgoEnabled: true},	{GOOS: "linux", GOARCH: "arm"},	{GOOS: "darwin", GOARCH: "386", CgoEnabled: true},	{GOOS: "darwin", GOARCH: "386"},	{GOOS: "darwin", GOARCH: "amd64", CgoEnabled: true},	{GOOS: "darwin", GOARCH: "amd64"},	{GOOS: "windows", GOARCH: "amd64"},	{GOOS: "windows", GOARCH: "386"},	{GOOS: "freebsd", GOARCH: "386", CgoEnabled: true},	{GOOS: "freebsd", GOARCH: "386"},	{GOOS: "freebsd", GOARCH: "amd64", CgoEnabled: true},	{GOOS: "freebsd", GOARCH: "amd64"},	{GOOS: "freebsd", GOARCH: "arm", CgoEnabled: true},	{GOOS: "freebsd", GOARCH: "arm"},	{GOOS: "netbsd", GOARCH: "386", CgoEnabled: true},	{GOOS: "netbsd", GOARCH: "386"},	{GOOS: "netbsd", GOARCH: "amd64", CgoEnabled: true},	{GOOS: "netbsd", GOARCH: "amd64"},	{GOOS: "netbsd", GOARCH: "arm", CgoEnabled: true},	{GOOS: "netbsd", GOARCH: "arm"},	{GOOS: "openbsd", GOARCH: "386", CgoEnabled: true},	{GOOS: "openbsd", GOARCH: "386"},	{GOOS: "openbsd", GOARCH: "amd64", CgoEnabled: true},	{GOOS: "openbsd", GOARCH: "amd64"},} func contextName(c *build.Context) string {	s := c.GOOS + "-" + c.GOARCH	if c.CgoEnabled {		return s + "-cgo"	}	return s} func parseContext(c string) *build.Context {	parts := strings.Split(c, "-")	if len(parts) < 2 {		log.Fatalf("bad context: %q", c)	}	bc := &build.Context{		GOOS:   parts[0],		GOARCH: parts[1],	}	if len(parts) == 3 {		if parts[2] == "cgo" {			bc.CgoEnabled = true		} else {			log.Fatalf("bad context: %q", c)		}	}	return bc} func setContexts() {	contexts = []*build.Context{}	for _, c := range strings.Split(*forceCtx, ",") {		contexts = append(contexts, parseContext(c))	}} var internalPkg = regexp.MustCompile(`(^|/)internal($|/)`) func main() {	flag.Parse() 	if !strings.Contains(runtime.Version(), "weekly") && !strings.Contains(runtime.Version(), "devel") {		if *nextFile != "" {			fmt.Printf("Go version is %q, ignoring -next %s\n", runtime.Version(), *nextFile)			*nextFile = ""		}	} 	if *forceCtx != "" {		setContexts()	}	for _, c := range contexts {		c.Compiler = build.Default.Compiler	} 	var pkgNames []string	if flag.NArg() > 0 {		pkgNames = flag.Args()	} else {		stds, err := exec.Command("go", "list", "std").Output()		if err != nil {			log.Fatal(err)		}		for _, pkg := range strings.Fields(string(stds)) {			if !internalPkg.MatchString(pkg) {				pkgNames = append(pkgNames, pkg)			}		}	} 	var featureCtx = make(map[string]map[string]bool) // feature -> context name -> true	for _, context := range contexts {		w := NewWalker(context, filepath.Join(build.Default.GOROOT, "src")) 		for _, name := range pkgNames {			// - Package "unsafe" contains special signatures requiring			//   extra care when printing them - ignore since it is not			//   going to change w/o a language change.			// - We don't care about the API of commands.			if name != "unsafe" && !strings.HasPrefix(name, "cmd/") {				if name == "runtime/cgo" && !context.CgoEnabled {					// w.Import(name) will return nil					continue				}				pkg, _ := w.Import(name)				w.export(pkg)			}		} 		ctxName := contextName(context)		for _, f := range w.Features() {			if featureCtx[f] == nil {				featureCtx[f] = make(map[string]bool)			}			featureCtx[f][ctxName] = true		}	} 	var features []string	for f, cmap := range featureCtx {		if len(cmap) == len(contexts) {			features = append(features, f)			continue		}		comma := strings.Index(f, ",")		for cname := range cmap {			f2 := fmt.Sprintf("%s (%s)%s", f[:comma], cname, f[comma:])			features = append(features, f2)		}	} 	fail := false	defer func() {		if fail {			os.Exit(1)		}	}() 	bw := bufio.NewWriter(os.Stdout)	defer bw.Flush() 	if *checkFile == "" {		sort.Strings(features)		for _, f := range features {			fmt.Fprintln(bw, f)		}		return	} 	var required []string	for _, file := range strings.Split(*checkFile, ",") {		required = append(required, fileFeatures(file)...)	}	optional := fileFeatures(*nextFile)	exception := fileFeatures(*exceptFile)	fail = !compareAPI(bw, features, required, optional, exception,		*allowNew && strings.Contains(runtime.Version(), "devel"))} // export emits the exported package features.func (w *Walker) export(pkg *types.Package) {	if *verbose {		log.Println(pkg)	}	pop := w.pushScope("pkg " + pkg.Path())	w.current = pkg	scope := pkg.Scope()	for _, name := range scope.Names() {		if ast.IsExported(name) {			w.emitObj(scope.Lookup(name))		}	}	pop()} func set(items []string) map[string]bool {	s := make(map[string]bool)	for _, v := range items {		s[v] = true	}	return s} var spaceParensRx = regexp.MustCompile(` \(\S+?\)`) func featureWithoutContext(f string) string {	if !strings.Contains(f, "(") {		return f	}	return spaceParensRx.ReplaceAllString(f, "")} func compareAPI(w io.Writer, features, required, optional, exception []string, allowAdd bool) (ok bool) {	ok = true 	optionalSet := set(optional)	exceptionSet := set(exception)	featureSet := set(features) 	sort.Strings(features)	sort.Strings(required) 	take := func(sl *[]string) string {		s := (*sl)[0]		*sl = (*sl)[1:]		return s	} 	for len(required) > 0 || len(features) > 0 {		switch {		case len(features) == 0 || (len(required) > 0 && required[0] < features[0]):			feature := take(&required)			if exceptionSet[feature] {				// An "unfortunate" case: the feature was once				// included in the API (e.g. go1.txt), but was				// subsequently removed. These are already				// acknowledged by being in the file				// "api/except.txt". No need to print them out				// here.			} else if featureSet[featureWithoutContext(feature)] {				// okay.			} else {				fmt.Fprintf(w, "-%s\n", feature)				ok = false // broke compatibility			}		case len(required) == 0 || (len(features) > 0 && required[0] > features[0]):			newFeature := take(&features)			if optionalSet[newFeature] {				// Known added feature to the upcoming release.				// Delete it from the map so we can detect any upcoming features				// which were never seen.  (so we can clean up the nextFile)				delete(optionalSet, newFeature)			} else {				fmt.Fprintf(w, "+%s\n", newFeature)				if !allowAdd {					ok = false // we're in lock-down mode for next release				}			}		default:			take(&required)			take(&features)		}	} 	// In next file, but not in API.	var missing []string	for feature := range optionalSet {		missing = append(missing, feature)	}	sort.Strings(missing)	for _, feature := range missing {		fmt.Fprintf(w, "±%s\n", feature)	}	return} func fileFeatures(filename string) []string {	if filename == "" {		return nil	}	bs, err := ioutil.ReadFile(filename)	if err != nil {		log.Fatalf("Error reading file %s: %v", filename, err)	}	lines := strings.Split(string(bs), "\n")	var nonblank []string	for _, line := range lines {		line = strings.TrimSpace(line)		if line != "" && !strings.HasPrefix(line, "#") {			nonblank = append(nonblank, line)		}	}	return nonblank} var fset = token.NewFileSet() type Walker struct {	context  *build.Context	root     string	scope    []string	current  *types.Package	features map[string]bool           // set	imported map[string]*types.Package // packages already imported} func NewWalker(context *build.Context, root string) *Walker {	return &Walker{		context:  context,		root:     root,		features: map[string]bool{},		imported: map[string]*types.Package{"unsafe": types.Unsafe},	}} func (w *Walker) Features() (fs []string) {	for f := range w.features {		fs = append(fs, f)	}	sort.Strings(fs)	return} var parsedFileCache = make(map[string]*ast.File) func (w *Walker) parseFile(dir, file string) (*ast.File, error) {	filename := filepath.Join(dir, file)	if f := parsedFileCache[filename]; f != nil {		return f, nil	} 	f, err := parser.ParseFile(fset, filename, nil, 0)	if err != nil {		return nil, err	}	parsedFileCache[filename] = f 	return f, nil} func contains(list []string, s string) bool {	for _, t := range list {		if t == s {			return true		}	}	return false} // The package cache doesn't operate correctly in rare (so far artificial)// circumstances (issue 8425). Disable before debugging non-obvious errors// from the type-checker.const usePkgCache = true var (	pkgCache = map[string]*types.Package{} // map tagKey to package	pkgTags  = map[string][]string{}       // map import dir to list of relevant tags) // tagKey returns the tag-based key to use in the pkgCache.// It is a comma-separated string; the first part is dir, the rest tags.// The satisfied tags are derived from context but only those that// matter (the ones listed in the tags argument) are used.// The tags list, which came from go/build's Package.AllTags,// is known to be sorted.func tagKey(dir string, context *build.Context, tags []string) string {	ctags := map[string]bool{		context.GOOS:   true,		context.GOARCH: true,	}	if context.CgoEnabled {		ctags["cgo"] = true	}	for _, tag := range context.BuildTags {		ctags[tag] = true	}	// TODO: ReleaseTags (need to load default)	key := dir	for _, tag := range tags {		if ctags[tag] {			key += "," + tag		}	}	return key} // Importing is a sentinel taking the place in Walker.imported// for a package that is in the process of being imported.var importing types.Package func (w *Walker) Import(name string) (*types.Package, error) {	pkg := w.imported[name]	if pkg != nil {		if pkg == &importing {			log.Fatalf("cycle importing package %q", name)		}		return pkg, nil	}	w.imported[name] = &importing 	root := w.root	if strings.HasPrefix(name, "golang.org/x/") {		root = filepath.Join(root, "vendor")	} 	// Determine package files.	dir := filepath.Join(root, filepath.FromSlash(name))	if fi, err := os.Stat(dir); err != nil || !fi.IsDir() {		log.Fatalf("no source in tree for import %q: %v", name, err)	} 	context := w.context	if context == nil {		context = &build.Default	} 	// Look in cache.	// If we've already done an import with the same set	// of relevant tags, reuse the result.	var key string	if usePkgCache {		if tags, ok := pkgTags[dir]; ok {			key = tagKey(dir, context, tags)			if pkg := pkgCache[key]; pkg != nil {				w.imported[name] = pkg				return pkg, nil			}		}	} 	info, err := context.ImportDir(dir, 0)	if err != nil {		if _, nogo := err.(*build.NoGoError); nogo {			return nil, nil		}		log.Fatalf("pkg %q, dir %q: ScanDir: %v", name, dir, err)	} 	// Save tags list first time we see a directory.	if usePkgCache {		if _, ok := pkgTags[dir]; !ok {			pkgTags[dir] = info.AllTags			key = tagKey(dir, context, info.AllTags)		}	} 	filenames := append(append([]string{}, info.GoFiles...), info.CgoFiles...) 	// Parse package files.	var files []*ast.File	for _, file := range filenames {		f, err := w.parseFile(dir, file)		if err != nil {			log.Fatalf("error parsing package %s: %s", name, err)		}		files = append(files, f)	} 	// Type-check package files.	conf := types.Config{		IgnoreFuncBodies: true,		FakeImportC:      true,		Importer:         w,	}	pkg, err = conf.Check(name, fset, files, nil)	if err != nil {		ctxt := "<no context>"		if w.context != nil {			ctxt = fmt.Sprintf("%s-%s", w.context.GOOS, w.context.GOARCH)		}

Lines 1–500 of 833.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t