From ea1161f7326df34c44d6d207807475daead990de Mon Sep 17 00:00:00 2001 From: Zxilly Date: Thu, 30 May 2024 14:50:34 +0800 Subject: [PATCH] fix: scrape all goos/goarch pair for stdlib Signed-off-by: Zxilly --- internal/generate.go | 97 ++++++++++++++++++++++++++++++++++-- pkg/section/standard_list.go | 3 +- pkg/section/standard_test.go | 1 + 3 files changed, 95 insertions(+), 6 deletions(-) diff --git a/internal/generate.go b/internal/generate.go index f106be2..618cead 100644 --- a/internal/generate.go +++ b/internal/generate.go @@ -2,10 +2,13 @@ package main import ( "bytes" + "fmt" "go/format" "os" "runtime" + "slices" "strings" + "sync" "text/template" "golang.org/x/tools/go/packages" @@ -35,25 +38,109 @@ func main() { } } +// update from https://go.dev/doc/install/source#environment +var list = `aix ppc64 +android 386 +android amd64 +android arm +android arm64 +darwin amd64 +darwin arm64 +dragonfly amd64 +freebsd 386 +freebsd amd64 +freebsd arm +illumos amd64 +ios arm64 +js wasm +linux 386 +linux amd64 +linux arm +linux arm64 +linux loong64 +linux mips +linux mipsle +linux mips64 +linux mips64le +linux ppc64 +linux ppc64le +linux riscv64 +linux s390x +netbsd 386 +netbsd amd64 +netbsd arm +openbsd 386 +openbsd amd64 +openbsd arm +openbsd arm64 +plan9 386 +plan9 amd64 +plan9 arm +solaris amd64 +wasip1 wasm +windows 386 +windows amd64 +windows arm +windows arm64` + func generate() error { - all, err := packages.Load(nil, "std") - if err != nil { - return err + var all []*packages.Package + + writeLock := sync.Mutex{} + wg := sync.WaitGroup{} + + pairs := strings.Split(list, "\n") + for _, pair := range pairs { + wg.Add(1) + go func(pair string) { + defer wg.Done() + + parts := strings.Split(pair, "\t") + if len(parts) != 2 { + return + } + goos := parts[0] + goarch := parts[1] + + pkgs, err := packages.Load(&packages.Config{ + Mode: packages.NeedName, + Env: append(os.Environ(), "GOOS="+goos, "GOARCH="+goarch, "GOEXPERIMENT=arenas,boringcrypto"), + }, "std") + if err != nil { + panic(err) + } + fmt.Println("loaded", goos, goarch, len(pkgs)) + + writeLock.Lock() + defer writeLock.Unlock() + + all = append(all, pkgs...) + }(pair) } - var pkgs []string + wg.Wait() + + var pkgSet = make(map[string]struct{}) // go list std | grep -v vendor | grep -v internal for _, pkg := range all { if !strings.Contains(pkg.PkgPath, "internal") && !strings.Contains(pkg.PkgPath, "vendor") { - pkgs = append(pkgs, pkg.PkgPath) + pkgSet[pkg.PkgPath] = struct{}{} } } + var pkgs []string + for pkg := range pkgSet { + pkgs = append(pkgs, pkg) + } + + slices.Sort(pkgs) + file, err := os.Create(outputFile) if err != nil { return err } + defer file.Close() models := map[string]interface{}{ "Packages": pkgs, diff --git a/pkg/section/standard_list.go b/pkg/section/standard_list.go index a2cd0a6..322bf13 100644 --- a/pkg/section/standard_list.go +++ b/pkg/section/standard_list.go @@ -1,6 +1,6 @@ package section -// Code generated based on go1.22.0 X:boringcrypto,arenas. DO NOT EDIT. +// Code generated based on go1.22.3. DO NOT EDIT. var standardPackages = map[string]struct{}{ "archive/tar": {}, @@ -154,6 +154,7 @@ var standardPackages = map[string]struct{}{ "sync": {}, "sync/atomic": {}, "syscall": {}, + "syscall/js": {}, "testing": {}, "testing/fstest": {}, "testing/iotest": {}, diff --git a/pkg/section/standard_test.go b/pkg/section/standard_test.go index 2209402..6f9a718 100644 --- a/pkg/section/standard_test.go +++ b/pkg/section/standard_test.go @@ -15,6 +15,7 @@ func TestStandardPackageSpecificity(t *testing.T) { {"crypto/ae", Standard{}, specificity.MisMatch{}}, {"crypto/aes", Standard{}, specificity.StandardMatch{}}, {"crypto/aes2", Standard{}, specificity.MisMatch{}}, + {"syscall/js", Standard{}, specificity.StandardMatch{}}, } testSpecificity(t, testCases) }