1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // mkstdlib generates the zstdlib.go file, containing the Go standard
8 // library API symbols. It's baked into the binary to avoid scanning
9 // GOPATH in the common case.
17 exec "golang.org/x/sys/execabs"
28 func mustOpen(name string) io.Reader {
29 f, err := os.Open(name)
36 func api(base string) string {
37 return filepath.Join(runtime.GOROOT(), "api", base)
40 var sym = regexp.MustCompile(`^pkg (\S+).*?, (?:var|func|type|const) ([A-Z]\w*)`)
42 var unsafeSyms = map[string]bool{"Alignof": true, "ArbitraryType": true, "Offsetof": true, "Pointer": true, "Sizeof": true}
46 outf := func(format string, args ...interface{}) {
47 fmt.Fprintf(&buf, format, args...)
49 outf("// Code generated by mkstdlib.go. DO NOT EDIT.\n\n")
50 outf("package imports\n")
51 outf("var stdlib = map[string][]string{\n")
53 mustOpen(api("go1.txt")),
54 mustOpen(api("go1.1.txt")),
55 mustOpen(api("go1.2.txt")),
56 mustOpen(api("go1.3.txt")),
57 mustOpen(api("go1.4.txt")),
58 mustOpen(api("go1.5.txt")),
59 mustOpen(api("go1.6.txt")),
60 mustOpen(api("go1.7.txt")),
61 mustOpen(api("go1.8.txt")),
62 mustOpen(api("go1.9.txt")),
63 mustOpen(api("go1.10.txt")),
64 mustOpen(api("go1.11.txt")),
65 mustOpen(api("go1.12.txt")),
66 mustOpen(api("go1.13.txt")),
67 mustOpen(api("go1.14.txt")),
68 mustOpen(api("go1.15.txt")),
70 // The API of the syscall/js package needs to be computed explicitly,
71 // because it's not included in the GOROOT/api/go1.*.txt files at this time.
74 sc := bufio.NewScanner(f)
76 pkgs := map[string]map[string]bool{
79 paths := []string{"unsafe"}
83 if m := sym.FindStringSubmatch(l); m != nil {
84 path, sym := m[1], m[2]
86 if _, ok := pkgs[path]; !ok {
87 pkgs[path] = map[string]bool{}
88 paths = append(paths, path)
90 pkgs[path][sym] = true
93 if err := sc.Err(); err != nil {
97 for _, path := range paths {
98 outf("\t%q: []string{\n", path)
101 for sym := range pkg {
102 syms = append(syms, sym)
105 for _, sym := range syms {
106 outf("\t\t%q,\n", sym)
111 fmtbuf, err := format.Source(buf.Bytes())
115 err = ioutil.WriteFile("zstdlib.go", fmtbuf, 0666)
121 // syscallJSAPI returns the API of the syscall/js package.
122 // It's computed from the contents of $(go env GOROOT)/src/syscall/js.
123 func syscallJSAPI() io.Reader {
125 if runtime.GOOS == "windows" {
128 cmd := exec.Command("go"+exeSuffix, "run", "cmd/api", "-contexts", "js-wasm", "syscall/js")
129 out, err := cmd.Output()
133 return bytes.NewReader(out)