github.com/jhump/golang-x-tools@v0.0.0-20220218190644-4958d6d39439/internal/imports/mkstdlib.go (about)

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