Debian Codegraph @

internal/imports/mkindex.go

raw symbols SCIP
1 //go:build ignore
2
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.
6
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).
11 package imports
12
13 import (
14 "bytes"
15 "fmt"
16 "go/ast"
17 "go/build"
18 "go/format"
19 "go/parser"
20 "go/token"
21 "log"
22 "os"
23 "path"
24 "path/filepath"
25 "strings"
26 )
27
28 var (
29 pkgIndex = make(map[string][]pkg)
30 exports = make(map[string]map[string]bool)
31 )
32
33 func main() {
34 // Don't use GOPATH.
35 ctx := build.Default
36 ctx.GOPATH = ""
37
38 // Populate pkgIndex global from GOROOT.
39 for _, path := range ctx.SrcDirs() {
40 f, err := os.Open(path)
41 if err != nil {
42 log.Print(err)
43 continue
44 }
45 children, err := f.Readdir(-1)
46 f.Close()
47 if err != nil {
48 log.Print(err)
49 continue
50 }
51 for _, child := range children {
52 if child.IsDir() {
53 loadPkg(path, child.Name())
54 }
55 }
56 }
57 // Populate exports global.
58 for _, ps := range pkgIndex {
59 for _, p := range ps {
60 e := loadExports(p.dir)
61 if e != nil {
62 exports[p.dir] = e
63 }
64 }
65 }
66
67 // Construct source file.
68 var buf bytes.Buffer
69 fmt.Fprint(&buf, pkgIndexHead)
70 fmt.Fprintf(&buf, "var pkgIndexMaster = %#v\n", pkgIndex)
71 fmt.Fprintf(&buf, "var exportsMaster = %#v\n", exports)
72 src := buf.Bytes()
73
74 // Replace main.pkg type name with pkg.
75 src = bytes.Replace(src, []byte("main.pkg"), []byte("pkg"), -1)
76 // Replace actual GOROOT with "/go".
77 src = bytes.Replace(src, []byte(ctx.GOROOT), []byte("/go"), -1)
78 // Add some line wrapping.
79 src = bytes.Replace(src, []byte("}, "), []byte("},\n"), -1)
80 src = bytes.Replace(src, []byte("true, "), []byte("true,\n"), -1)
81
82 var err error
83 src, err = format.Source(src)
84 if err != nil {
85 log.Fatal(err)
86 }
87
88 // Write out source file.
89 err = os.WriteFile("pkgindex.go", src, 0644)
90 if err != nil {
91 log.Fatal(err)
92 }
93 }
94
95 const pkgIndexHead = `package imports
96
97 func init() {
98 pkgIndexOnce.Do(func() {
99 pkgIndex.m = pkgIndexMaster
100 })
101 loadExports = func(dir string) map[string]bool {
102 return exportsMaster[dir]
103 }
104 }
105 `
106
107 type pkg struct {
108 importpath string // full pkg import path, e.g. "net/http"
109 dir string // absolute file path to pkg directory e.g. "/usr/lib/go/src/fmt"
110 }
111
112 var fset = token.NewFileSet()
113
114 func loadPkg(root, importpath string) {
115 shortName := path.Base(importpath)
116 if shortName == "testdata" {
117 return
118 }
119
120 dir := filepath.Join(root, importpath)
121 pkgIndex[shortName] = append(pkgIndex[shortName], pkg{
122 importpath: importpath,
123 dir: dir,
124 })
125
126 pkgDir, err := os.Open(dir)
127 if err != nil {
128 return
129 }
130 children, err := pkgDir.Readdir(-1)
131 pkgDir.Close()
132 if err != nil {
133 return
134 }
135 for _, child := range children {
136 name := child.Name()
137 if name == "" {
138 continue
139 }
140 if c := name[0]; c == '.' || ('0' <= c && c <= '9') {
141 continue
142 }
143 if child.IsDir() {
144 loadPkg(root, filepath.Join(importpath, name))
145 }
146 }
147 }
148
149 func loadExports(dir string) map[string]bool {
150 exports := make(map[string]bool)
151 buildPkg, err := build.ImportDir(dir, 0)
152 if err != nil {
153 if strings.Contains(err.Error(), "no buildable Go source files in") {
154 return nil
155 }
156 log.Printf("could not import %q: %v", dir, err)
157 return nil
158 }
159 for _, file := range buildPkg.GoFiles {
160 // Legacy ast.Object resolution is needed here.
161 f, err := parser.ParseFile(fset, filepath.Join(dir, file), nil, 0)
162 if err != nil {
163 log.Printf("could not parse %q: %v", file, err)
164 continue
165 }
166 for name := range f.Scope.Objects {
167 if ast.IsExported(name) {
168 exports[name] = true
169 }
170 }
171 }
172 return exports
173 }