3 // mkstdlib generates the zstdlib.go file, containing the Go standard
4 // library API symbols. It's baked into the binary to avoid scanning
5 // GOPATH in the common case.
25 func mustOpen(name string) io.Reader {
26 f, err := os.Open(name)
33 func api(base string) string {
34 return filepath.Join(runtime.GOROOT(), "api", base)
37 var sym = regexp.MustCompile(`^pkg (\S+).*?, (?:var|func|type|const) ([A-Z]\w*)`)
39 var unsafeSyms = map[string]bool{"Alignof": true, "ArbitraryType": true, "Offsetof": true, "Pointer": true, "Sizeof": true}
43 outf := func(format string, args ...interface{}) {
44 fmt.Fprintf(&buf, format, args...)
46 outf("// Code generated by mkstdlib.go. DO NOT EDIT.\n\n")
47 outf("package imports\n")
48 outf("var stdlib = map[string]map[string]bool{\n")
50 mustOpen(api("go1.txt")),
51 mustOpen(api("go1.1.txt")),
52 mustOpen(api("go1.2.txt")),
53 mustOpen(api("go1.3.txt")),
54 mustOpen(api("go1.4.txt")),
55 mustOpen(api("go1.5.txt")),
56 mustOpen(api("go1.6.txt")),
57 mustOpen(api("go1.7.txt")),
58 mustOpen(api("go1.8.txt")),
59 mustOpen(api("go1.9.txt")),
60 mustOpen(api("go1.10.txt")),
61 mustOpen(api("go1.11.txt")),
62 mustOpen(api("go1.12.txt")),
64 // The API of the syscall/js package needs to be computed explicitly,
65 // because it's not included in the GOROOT/api/go1.*.txt files at this time.
68 sc := bufio.NewScanner(f)
70 pkgs := map[string]map[string]bool{
73 paths := []string{"unsafe"}
77 has := func(v string) bool { return strings.Contains(l, v) }
78 if has("struct, ") || has("interface, ") || has(", method (") {
81 if m := sym.FindStringSubmatch(l); m != nil {
82 path, sym := m[1], m[2]
84 if _, ok := pkgs[path]; !ok {
85 pkgs[path] = map[string]bool{}
86 paths = append(paths, path)
88 pkgs[path][sym] = true
91 if err := sc.Err(); err != nil {
95 for _, path := range paths {
96 outf("\t%q: map[string]bool{\n", path)
99 for sym := range pkg {
100 syms = append(syms, sym)
103 for _, sym := range syms {
104 outf("\t\t%q: true,\n", sym)
109 fmtbuf, err := format.Source(buf.Bytes())
113 err = ioutil.WriteFile("zstdlib.go", fmtbuf, 0666)
119 // syscallJSAPI returns the API of the syscall/js package.
120 // It's computed from the contents of $(go env GOROOT)/src/syscall/js.
121 func syscallJSAPI() io.Reader {
123 if runtime.GOOS == "windows" {
126 cmd := exec.Command("go"+exeSuffix, "run", "cmd/api", "-contexts", "js-wasm", "syscall/js")
127 out, err := cmd.Output()
131 return bytes.NewReader(out)