aboutsummaryrefslogtreecommitdiff
path: root/go/tools/internal/stdlib_tags/stdlib_tags.go
blob: 8bd507af3af1642cf7227ae4602431ee57084730 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main

import (
	"bufio"
	"fmt"
	"go/build/constraint"
	"log"
	"os"
	"path/filepath"
	"regexp"
	"sort"
	"strings"
)

var goVersionRegex = regexp.MustCompile(`^go1.(\d+)$`)

// Used to update the list of tags affecting the standard library kept in
// transitions.bzl.
func main() {
	if len(os.Args) < 2 {
		log.Fatal("usage: stdlib_tags <go SDK src directory>...")
	}

	filteredTags, err := extractBuildTags(os.Args[1:]...)
	if err != nil {
		log.Fatal(err.Error())
	}

	fmt.Printf("_TAG_AFFECTS_STDLIB = {\n")
	for _, tag := range filteredTags {
		fmt.Printf("    %q: None,\n", tag)
	}
	fmt.Printf("}\n")
}

func extractBuildTags(sdkPaths ...string) ([]string, error) {
	tags := make(map[string]struct{})
	for _, dir := range sdkPaths {
		err := filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
			if d.IsDir() {
				if d.Name() == "testdata" {
					return filepath.SkipDir
				}
				return nil
			}
			if filepath.Ext(path) != ".go" {
				return nil
			}
			if strings.HasSuffix(filepath.Base(path), "_test.go") {
				return nil
			}
			return walkFile(path, tags)
		})
		if err != nil {
			return nil, fmt.Errorf("%s: %w", dir, err)
		}
	}

	filteredTags := make([]string, 0, len(tags))
	for tag := range tags {
		if !shouldExclude(tag) {
			filteredTags = append(filteredTags, tag)
		}
	}
	sort.Strings(filteredTags)

	return filteredTags, nil
}

func shouldExclude(tag string) bool {
	// Set via CGO_ENABLED
	return tag == "cgo" ||
		// Set via GOARCH and GOOS
		knownOS[tag] || knownArch[tag] || tag == "unix" ||
		// Set via GOEXPERIMENT and GOAMD64
		strings.HasPrefix(tag, "goexperiment.") || strings.HasPrefix(tag, "amd64.") ||
		// Set implicitly
		goVersionRegex.MatchString(tag)
}

func walkFile(path string, tags map[string]struct{}) error {
	file, err := os.Open(path)
	if err != nil {
		return err
	}

	scanner := bufio.NewScanner(file)
	// The Go SDK contains some very long lines in vendored files (minified JS).
	scanner.Buffer(make([]byte, 0, 128*1024), 1024*1024)
	for scanner.Scan() {
		line := scanner.Text()
		if !isConstraint(line) {
			continue
		}
		c, err := constraint.Parse(line)
		if err != nil {
			continue
		}
		walkConstraint(c, tags)
	}

	if err = scanner.Err(); err != nil {
		return fmt.Errorf("%s: %w", path, err)
	}
	return nil
}

func walkConstraint(c constraint.Expr, tags map[string]struct{}) {
	switch c.(type) {
	case *constraint.AndExpr:
		walkConstraint(c.(*constraint.AndExpr).X, tags)
		walkConstraint(c.(*constraint.AndExpr).Y, tags)
	case *constraint.OrExpr:
		walkConstraint(c.(*constraint.OrExpr).X, tags)
		walkConstraint(c.(*constraint.OrExpr).Y, tags)
	case *constraint.NotExpr:
		walkConstraint(c.(*constraint.NotExpr).X, tags)
	case *constraint.TagExpr:
		tags[c.(*constraint.TagExpr).Tag] = struct{}{}
	}
}

func isConstraint(line string) bool {
	return constraint.IsPlusBuild(line) || constraint.IsGoBuild(line)
}

// Taken from
// https://github.com/golang/go/blob/3d5391ed87d813110e10b954c62bf7ed578b591f/src/go/build/syslist.go
var knownOS = map[string]bool{
	"aix":       true,
	"android":   true,
	"darwin":    true,
	"dragonfly": true,
	"freebsd":   true,
	"hurd":      true,
	"illumos":   true,
	"ios":       true,
	"js":        true,
	"linux":     true,
	"nacl":      true,
	"netbsd":    true,
	"openbsd":   true,
	"plan9":     true,
	"solaris":   true,
	"windows":   true,
	"zos":       true,
}

var knownArch = map[string]bool{
	"386":         true,
	"amd64":       true,
	"amd64p32":    true,
	"arm":         true,
	"armbe":       true,
	"arm64":       true,
	"arm64be":     true,
	"loong64":     true,
	"mips":        true,
	"mipsle":      true,
	"mips64":      true,
	"mips64le":    true,
	"mips64p32":   true,
	"mips64p32le": true,
	"ppc":         true,
	"ppc64":       true,
	"ppc64le":     true,
	"riscv":       true,
	"riscv64":     true,
	"s390":        true,
	"s390x":       true,
	"sparc":       true,
	"sparc64":     true,
	"wasm":        true,
}