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")),
65 // The API of the syscall/js package needs to be computed explicitly,
66 // because it's not included in the GOROOT/api/go1.*.txt files at this time.
69 sc := bufio.NewScanner(f)
71 pkgs := map[string]map[string]bool{
74 paths := []string{"unsafe"}
78 if m := sym.FindStringSubmatch(l); m != nil {
79 path, sym := m[1], m[2]
81 if _, ok := pkgs[path]; !ok {
82 pkgs[path] = map[string]bool{}
83 paths = append(paths, path)
85 pkgs[path][sym] = true
88 if err := sc.Err(); err != nil {
92 for _, path := range paths {
93 outf("\t%q: []string{\n", path)
96 for sym := range pkg {
97 syms = append(syms, sym)
100 for _, sym := range syms {
101 outf("\t\t%q,\n", sym)
106 fmtbuf, err := format.Source(buf.Bytes())
110 err = ioutil.WriteFile("zstdlib.go", fmtbuf, 0o666)
116 // syscallJSAPI returns the API of the syscall/js package.
117 // It's computed from the contents of $(go env GOROOT)/src/syscall/js.
118 func syscallJSAPI() io.Reader {
120 if runtime.GOOS == "windows" {
123 cmd := exec.Command("go"+exeSuffix, "run", "cmd/api", "-contexts", "js-wasm", "syscall/js")
124 out, err := cmd.Output()
128 return bytes.NewReader(out)