github.com/jd-ly/tools@v0.5.7/internal/imports/mkstdlib.go (about) 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. 6 package main 7 8 import ( 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 ) 23 24 func mustOpen(name string) io.Reader { 25 f, err := os.Open(name) 26 if err != nil { 27 log.Fatal(err) 28 } 29 return f 30 } 31 32 func api(base string) string { 33 return filepath.Join(runtime.GOROOT(), "api", base) 34 } 35 36 var sym = regexp.MustCompile(`^pkg (\S+).*?, (?:var|func|type|const) ([A-Z]\w*)`) 37 38 var unsafeSyms = map[string]bool{"Alignof": true, "ArbitraryType": true, "Offsetof": true, "Pointer": true, "Sizeof": true} 39 40 func main() { 41 var buf bytes.Buffer 42 outf := func(format string, args ...interface{}) { 43 fmt.Fprintf(&buf, format, args...) 44 } 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") 48 f := io.MultiReader( 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")), 65 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. 68 syscallJSAPI(), 69 ) 70 sc := bufio.NewScanner(f) 71 72 pkgs := map[string]map[string]bool{ 73 "unsafe": unsafeSyms, 74 } 75 paths := []string{"unsafe"} 76 77 for sc.Scan() { 78 l := sc.Text() 79 if m := sym.FindStringSubmatch(l); m != nil { 80 path, sym := m[1], m[2] 81 82 if _, ok := pkgs[path]; !ok { 83 pkgs[path] = map[string]bool{} 84 paths = append(paths, path) 85 } 86 pkgs[path][sym] = true 87 } 88 } 89 if err := sc.Err(); err != nil { 90 log.Fatal(err) 91 } 92 sort.Strings(paths) 93 for _, path := range paths { 94 outf("\t%q: []string{\n", path) 95 pkg := pkgs[path] 96 var syms []string 97 for sym := range pkg { 98 syms = append(syms, sym) 99 } 100 sort.Strings(syms) 101 for _, sym := range syms { 102 outf("\t\t%q,\n", sym) 103 } 104 outf("},\n") 105 } 106 outf("}\n") 107 fmtbuf, err := format.Source(buf.Bytes()) 108 if err != nil { 109 log.Fatal(err) 110 } 111 err = ioutil.WriteFile("zstdlib.go", fmtbuf, 0666) 112 if err != nil { 113 log.Fatal(err) 114 } 115 } 116 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 { 120 var exeSuffix string 121 if runtime.GOOS == "windows" { 122 exeSuffix = ".exe" 123 } 124 cmd := exec.Command("go"+exeSuffix, "run", "cmd/api", "-contexts", "js-wasm", "syscall/js") 125 out, err := cmd.Output() 126 if err != nil { 127 log.Fatalln(err) 128 } 129 return bytes.NewReader(out) 130 }