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.
24 func mustOpen(name string) io.Reader {
25 f, err := os.Open(name)
32 func api(base string) string {
33 return filepath.Join(runtime.GOROOT(), "api", base)
36 var sym = regexp.MustCompile(`^pkg (\S+).*?, (?:var|func|type|const) ([A-Z]\w*)`)
38 var unsafeSyms = map[string]bool{"Alignof": true, "ArbitraryType": true, "Offsetof": true, "Pointer": true, "Sizeof": true}
42 outf := func(format string, args ...interface{}) {
43 fmt.Fprintf(&buf, format, args...)
45 outf("// Code generated by mkstdlib.go. DO NOT EDIT.\n\n")
46 outf("package imports\n")
47 outf("var stdlib = map[string][]string{\n")
49 mustOpen(api("go1.txt")),
50 mustOpen(api("go1.1.txt")),
51 mustOpen(api("go1.2.txt")),
52 mustOpen(api("go1.3.txt")),
53 mustOpen(api("go1.4.txt")),
54 mustOpen(api("go1.5.txt")),
55 mustOpen(api("go1.6.txt")),
56 mustOpen(api("go1.7.txt")),
57 mustOpen(api("go1.8.txt")),
58 mustOpen(api("go1.9.txt")),
59 mustOpen(api("go1.10.txt")),
60 mustOpen(api("go1.11.txt")),
61 mustOpen(api("go1.12.txt")),
62 mustOpen(api("go1.13.txt")),
63 mustOpen(api("go1.14.txt")),
64 mustOpen(api("go1.15.txt")),
66 // The API of the syscall/js package needs to be computed explicitly,
67 // because it's not included in the GOROOT/api/go1.*.txt files at this time.
70 sc := bufio.NewScanner(f)
72 pkgs := map[string]map[string]bool{
75 paths := []string{"unsafe"}
79 if m := sym.FindStringSubmatch(l); m != nil {
80 path, sym := m[1], m[2]
82 if _, ok := pkgs[path]; !ok {
83 pkgs[path] = map[string]bool{}
84 paths = append(paths, path)
86 pkgs[path][sym] = true
89 if err := sc.Err(); err != nil {
93 for _, path := range paths {
94 outf("\t%q: []string{\n", path)
97 for sym := range pkg {
98 syms = append(syms, sym)
101 for _, sym := range syms {
102 outf("\t\t%q,\n", sym)
107 fmtbuf, err := format.Source(buf.Bytes())
111 err = ioutil.WriteFile("zstdlib.go", fmtbuf, 0o666)
117 // syscallJSAPI returns the API of the syscall/js package.
118 // It's computed from the contents of $(go env GOROOT)/src/syscall/js.
119 func syscallJSAPI() io.Reader {
121 if runtime.GOOS == "windows" {
124 cmd := exec.Command("go"+exeSuffix, "run", "cmd/api", "-contexts", "js-wasm", "syscall/js")
125 out, err := cmd.Output()
129 return bytes.NewReader(out)