| 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. |
| 4 | |
| 5 | //go:build ignore |
| 6 | // +build ignore |
| 7 | |
| 8 | // mkstdlib generates the zstdlib.go file, containing the Go standard |
| 9 | // library API symbols. It's baked into the binary to avoid scanning |
| 10 | // GOPATH in the common case. |
| 11 | package main |
| 12 | |
| 13 | import ( |
| 14 | "bufio" |
| 15 | "bytes" |
| 16 | "fmt" |
| 17 | "go/format" |
| 18 | "io" |
| 19 | "io/ioutil" |
| 20 | "log" |
| 21 | "os" |
| 22 | "path/filepath" |
| 23 | "regexp" |
| 24 | "runtime" |
| 25 | "sort" |
| 26 | "strings" |
| 27 | |
| 28 | exec "golang.org/x/sys/execabs" |
| 29 | ) |
| 30 | |
| 31 | func mustOpen(name string) io.Reader { |
| 32 | f, err := os.Open(name) |
| 33 | if err != nil { |
| 34 | log.Fatal(err) |
| 35 | } |
| 36 | return f |
| 37 | } |
| 38 | |
| 39 | func api(base string) string { |
| 40 | return filepath.Join(runtime.GOROOT(), "api", base) |
| 41 | } |
| 42 | |
| 43 | var sym = regexp.MustCompile(`^pkg (\S+).*?, (?:var|func|type|const) ([A-Z]\w*)`) |
| 44 | |
| 45 | var unsafeSyms = map[string]bool{"Alignof": true, "ArbitraryType": true, "Offsetof": true, "Pointer": true, "Sizeof": true} |
| 46 | |
| 47 | func main() { |
| 48 | var buf bytes.Buffer |
| 49 | outf := func(format string, args ...interface{}) { |
| 50 | fmt.Fprintf(&buf, format, args...) |
| 51 | } |
| 52 | outf(`// Copyright 2022 The Go Authors. All rights reserved. |
| 53 | // Use of this source code is governed by a BSD-style |
| 54 | // license that can be found in the LICENSE file. |
| 55 | |
| 56 | `) |
| 57 | outf("// Code generated by mkstdlib.go. DO NOT EDIT.\n\n") |
| 58 | outf("package imports\n") |
| 59 | outf("var stdlib = map[string][]string{\n") |
| 60 | f := readAPI() |
| 61 | sc := bufio.NewScanner(f) |
| 62 | |
| 63 | pkgs := map[string]map[string]bool{ |
| 64 | "unsafe": unsafeSyms, |
| 65 | } |
| 66 | paths := []string{"unsafe"} |
| 67 | |
| 68 | for sc.Scan() { |
| 69 | l := sc.Text() |
| 70 | if m := sym.FindStringSubmatch(l); m != nil { |
| 71 | path, sym := m[1], m[2] |
| 72 | |
| 73 | if _, ok := pkgs[path]; !ok { |
| 74 | pkgs[path] = map[string]bool{} |
| 75 | paths = append(paths, path) |
| 76 | } |
| 77 | pkgs[path][sym] = true |
| 78 | } |
| 79 | } |
| 80 | if err := sc.Err(); err != nil { |
| 81 | log.Fatal(err) |
| 82 | } |
| 83 | sort.Strings(paths) |
| 84 | for _, path := range paths { |
| 85 | outf("\t%q: {\n", path) |
| 86 | pkg := pkgs[path] |
| 87 | var syms []string |
| 88 | for sym := range pkg { |
| 89 | syms = append(syms, sym) |
| 90 | } |
| 91 | sort.Strings(syms) |
| 92 | for _, sym := range syms { |
| 93 | outf("\t\t%q,\n", sym) |
| 94 | } |
| 95 | outf("},\n") |
| 96 | } |
| 97 | outf("}\n") |
| 98 | fmtbuf, err := format.Source(buf.Bytes()) |
| 99 | if err != nil { |
| 100 | log.Fatal(err) |
| 101 | } |
| 102 | err = ioutil.WriteFile("zstdlib.go", fmtbuf, 0666) |
| 103 | if err != nil { |
| 104 | log.Fatal(err) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // readAPI opens an io.Reader that reads all stdlib API content. |
| 109 | func readAPI() io.Reader { |
| 110 | entries, err := os.ReadDir(filepath.Join(runtime.GOROOT(), "api")) |
| 111 | if err != nil { |
| 112 | log.Fatal(err) |
| 113 | } |
| 114 | var readers []io.Reader |
| 115 | for _, entry := range entries { |
| 116 | name := entry.Name() |
| 117 | if strings.HasPrefix(name, "go") && strings.HasSuffix(name, ".txt") { |
| 118 | readers = append(readers, mustOpen(api(name))) |
| 119 | } |
| 120 | } |
| 121 | // The API of the syscall/js package needs to be computed explicitly, |
| 122 | // because it's not included in the GOROOT/api/go1.*.txt files at this time. |
| 123 | readers = append(readers, syscallJSAPI()) |
| 124 | return io.MultiReader(readers...) |
| 125 | } |
| 126 | |
| 127 | // syscallJSAPI returns the API of the syscall/js package. |
| 128 | // It's computed from the contents of $(go env GOROOT)/src/syscall/js. |
| 129 | func syscallJSAPI() io.Reader { |
| 130 | var exeSuffix string |
| 131 | if runtime.GOOS == "windows" { |
| 132 | exeSuffix = ".exe" |
| 133 | } |
| 134 | cmd := exec.Command("go"+exeSuffix, "run", "cmd/api", "-contexts", "js-wasm", "syscall/js") |
| 135 | out, err := cmd.Output() |
| 136 | if err != nil { |
| 137 | log.Fatalln(err) |
| 138 | } |
| 139 | return bytes.NewReader(out) |
| 140 | } |
| 141 |
Members