mkstdlib.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // +build ignore
  2. // mkstdlib generates the zstdlib.go file, containing the Go standard
  3. // library API symbols. It's baked into the binary to avoid scanning
  4. // GOPATH in the common case.
  5. package main
  6. import (
  7. "bufio"
  8. "bytes"
  9. "fmt"
  10. "go/format"
  11. "io"
  12. "io/ioutil"
  13. "log"
  14. "os"
  15. "path"
  16. "path/filepath"
  17. "regexp"
  18. "sort"
  19. "strings"
  20. )
  21. func mustOpen(name string) io.Reader {
  22. f, err := os.Open(name)
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. return f
  27. }
  28. func api(base string) string {
  29. return filepath.Join(os.Getenv("GOROOT"), "api", base)
  30. }
  31. var sym = regexp.MustCompile(`^pkg (\S+).*?, (?:var|func|type|const) ([A-Z]\w*)`)
  32. func main() {
  33. var buf bytes.Buffer
  34. outf := func(format string, args ...interface{}) {
  35. fmt.Fprintf(&buf, format, args...)
  36. }
  37. outf("// Code generated by mkstdlib.go. DO NOT EDIT.\n\n")
  38. outf("package imports\n")
  39. outf("var stdlib = map[string]string{\n")
  40. f := io.MultiReader(
  41. mustOpen(api("go1.txt")),
  42. mustOpen(api("go1.1.txt")),
  43. mustOpen(api("go1.2.txt")),
  44. mustOpen(api("go1.3.txt")),
  45. mustOpen(api("go1.4.txt")),
  46. mustOpen(api("go1.5.txt")),
  47. mustOpen(api("go1.6.txt")),
  48. mustOpen(api("go1.7.txt")),
  49. mustOpen(api("go1.8.txt")),
  50. mustOpen(api("go1.9.txt")),
  51. )
  52. sc := bufio.NewScanner(f)
  53. fullImport := map[string]string{} // "zip.NewReader" => "archive/zip"
  54. ambiguous := map[string]bool{}
  55. var keys []string
  56. for sc.Scan() {
  57. l := sc.Text()
  58. has := func(v string) bool { return strings.Contains(l, v) }
  59. if has("struct, ") || has("interface, ") || has(", method (") {
  60. continue
  61. }
  62. if m := sym.FindStringSubmatch(l); m != nil {
  63. full := m[1]
  64. key := path.Base(full) + "." + m[2]
  65. if exist, ok := fullImport[key]; ok {
  66. if exist != full {
  67. ambiguous[key] = true
  68. }
  69. } else {
  70. fullImport[key] = full
  71. keys = append(keys, key)
  72. }
  73. }
  74. }
  75. if err := sc.Err(); err != nil {
  76. log.Fatal(err)
  77. }
  78. sort.Strings(keys)
  79. for _, key := range keys {
  80. if ambiguous[key] {
  81. outf("\t// %q is ambiguous\n", key)
  82. } else {
  83. outf("\t%q: %q,\n", key, fullImport[key])
  84. }
  85. }
  86. outf("\n")
  87. for _, sym := range [...]string{"Alignof", "ArbitraryType", "Offsetof", "Pointer", "Sizeof"} {
  88. outf("\t%q: %q,\n", "unsafe."+sym, "unsafe")
  89. }
  90. outf("}\n")
  91. fmtbuf, err := format.Source(buf.Bytes())
  92. if err != nil {
  93. log.Fatal(err)
  94. }
  95. err = ioutil.WriteFile("zstdlib.go", fmtbuf, 0666)
  96. if err != nil {
  97. log.Fatal(err)
  98. }
  99. }