mkstdlib.go

  1// +build ignore
  2
  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.
  6package main
  7
  8import (
  9	"bufio"
 10	"bytes"
 11	"fmt"
 12	"go/format"
 13	"io"
 14	"io/ioutil"
 15	"log"
 16	"os"
 17	"os/exec"
 18	"path/filepath"
 19	"regexp"
 20	"runtime"
 21	"sort"
 22	"strings"
 23)
 24
 25func mustOpen(name string) io.Reader {
 26	f, err := os.Open(name)
 27	if err != nil {
 28		log.Fatal(err)
 29	}
 30	return f
 31}
 32
 33func api(base string) string {
 34	return filepath.Join(runtime.GOROOT(), "api", base)
 35}
 36
 37var sym = regexp.MustCompile(`^pkg (\S+).*?, (?:var|func|type|const) ([A-Z]\w*)`)
 38
 39var unsafeSyms = map[string]bool{"Alignof": true, "ArbitraryType": true, "Offsetof": true, "Pointer": true, "Sizeof": true}
 40
 41func main() {
 42	var buf bytes.Buffer
 43	outf := func(format string, args ...interface{}) {
 44		fmt.Fprintf(&buf, format, args...)
 45	}
 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")
 49	f := io.MultiReader(
 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")),
 63
 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.
 66		syscallJSAPI(),
 67	)
 68	sc := bufio.NewScanner(f)
 69
 70	pkgs := map[string]map[string]bool{
 71		"unsafe": unsafeSyms,
 72	}
 73	paths := []string{"unsafe"}
 74
 75	for sc.Scan() {
 76		l := sc.Text()
 77		has := func(v string) bool { return strings.Contains(l, v) }
 78		if has("struct, ") || has("interface, ") || has(", method (") {
 79			continue
 80		}
 81		if m := sym.FindStringSubmatch(l); m != nil {
 82			path, sym := m[1], m[2]
 83
 84			if _, ok := pkgs[path]; !ok {
 85				pkgs[path] = map[string]bool{}
 86				paths = append(paths, path)
 87			}
 88			pkgs[path][sym] = true
 89		}
 90	}
 91	if err := sc.Err(); err != nil {
 92		log.Fatal(err)
 93	}
 94	sort.Strings(paths)
 95	for _, path := range paths {
 96		outf("\t%q: map[string]bool{\n", path)
 97		pkg := pkgs[path]
 98		var syms []string
 99		for sym := range pkg {
100			syms = append(syms, sym)
101		}
102		sort.Strings(syms)
103		for _, sym := range syms {
104			outf("\t\t%q: true,\n", sym)
105		}
106		outf("},\n")
107	}
108	outf("}\n")
109	fmtbuf, err := format.Source(buf.Bytes())
110	if err != nil {
111		log.Fatal(err)
112	}
113	err = ioutil.WriteFile("zstdlib.go", fmtbuf, 0666)
114	if err != nil {
115		log.Fatal(err)
116	}
117}
118
119// syscallJSAPI returns the API of the syscall/js package.
120// It's computed from the contents of $(go env GOROOT)/src/syscall/js.
121func syscallJSAPI() io.Reader {
122	var exeSuffix string
123	if runtime.GOOS == "windows" {
124		exeSuffix = ".exe"
125	}
126	cmd := exec.Command("go"+exeSuffix, "run", "cmd/api", "-contexts", "js-wasm", "syscall/js")
127	out, err := cmd.Output()
128	if err != nil {
129		log.Fatalln(err)
130	}
131	return bytes.NewReader(out)
132}