3 // Copyright 2013 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 // Command mkindex creates the file "pkgindex.go" containing an index of the Go
8 // standard library. The file is intended to be built as part of the imports
9 // package, so that the package may be used in environments where a GOROOT is
10 // not available (such as App Engine).
30 pkgIndex = make(map[string][]pkg)
31 exports = make(map[string]map[string]bool)
39 // Populate pkgIndex global from GOROOT.
40 for _, path := range ctx.SrcDirs() {
41 f, err := os.Open(path)
46 children, err := f.Readdir(-1)
52 for _, child := range children {
54 loadPkg(path, child.Name())
58 // Populate exports global.
59 for _, ps := range pkgIndex {
60 for _, p := range ps {
61 e := loadExports(p.dir)
68 // Construct source file.
70 fmt.Fprint(&buf, pkgIndexHead)
71 fmt.Fprintf(&buf, "var pkgIndexMaster = %#v\n", pkgIndex)
72 fmt.Fprintf(&buf, "var exportsMaster = %#v\n", exports)
75 // Replace main.pkg type name with pkg.
76 src = bytes.Replace(src, []byte("main.pkg"), []byte("pkg"), -1)
77 // Replace actual GOROOT with "/go".
78 src = bytes.Replace(src, []byte(ctx.GOROOT), []byte("/go"), -1)
79 // Add some line wrapping.
80 src = bytes.Replace(src, []byte("}, "), []byte("},\n"), -1)
81 src = bytes.Replace(src, []byte("true, "), []byte("true,\n"), -1)
84 src, err = format.Source(src)
89 // Write out source file.
90 err = ioutil.WriteFile("pkgindex.go", src, 0o644)
96 const pkgIndexHead = `package imports
99 pkgIndexOnce.Do(func() {
100 pkgIndex.m = pkgIndexMaster
102 loadExports = func(dir string) map[string]bool {
103 return exportsMaster[dir]
109 importpath string // full pkg import path, e.g. "net/http"
110 dir string // absolute file path to pkg directory e.g. "/usr/lib/go/src/fmt"
113 var fset = token.NewFileSet()
115 func loadPkg(root, importpath string) {
116 shortName := path.Base(importpath)
117 if shortName == "testdata" {
121 dir := filepath.Join(root, importpath)
122 pkgIndex[shortName] = append(pkgIndex[shortName], pkg{
123 importpath: importpath,
127 pkgDir, err := os.Open(dir)
131 children, err := pkgDir.Readdir(-1)
136 for _, child := range children {
141 if c := name[0]; c == '.' || ('0' <= c && c <= '9') {
145 loadPkg(root, filepath.Join(importpath, name))
150 func loadExports(dir string) map[string]bool {
151 exports := make(map[string]bool)
152 buildPkg, err := build.ImportDir(dir, 0)
154 if strings.Contains(err.Error(), "no buildable Go source files in") {
157 log.Printf("could not import %q: %v", dir, err)
160 for _, file := range buildPkg.GoFiles {
161 f, err := parser.ParseFile(fset, filepath.Join(dir, file), nil, 0)
163 log.Printf("could not parse %q: %v", file, err)
166 for name := range f.Scope.Objects {
167 if ast.IsExported(name) {