Debian Codegraph @

gopls/internal/cache/port.go

raw symbols SCIP
1 // Copyright 2023 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 package cache
6
7 import (
8 "bytes"
9 "go/build"
10 "go/build/constraint"
11 "go/parser"
12 "go/token"
13 "io"
14 "path/filepath"
15 "strings"
16
17 "golang.org/x/tools/gopls/internal/util/bug"
18 )
19
20 type port struct{ GOOS, GOARCH string }
21
22 var (
23 // preferredPorts holds GOOS/GOARCH combinations for which we dynamically
24 // create new Views, by setting GOOS=... and GOARCH=... on top of
25 // user-provided configuration when we detect that the default build
26 // configuration does not match an open file. Ports are matched in the order
27 // defined below, so that when multiple ports match a file we use the port
28 // occurring at a lower index in the slice. For that reason, we sort first
29 // class ports ahead of secondary ports, and (among first class ports) 64-bit
30 // ports ahead of the less common 32-bit ports.
31 preferredPorts = []port{
32 // First class ports, from https://go.dev/wiki/PortingPolicy.
33 {"darwin", "amd64"},
34 {"darwin", "arm64"},
35 {"linux", "amd64"},
36 {"linux", "arm64"},
37 {"windows", "amd64"},
38 {"linux", "arm"},
39 {"linux", "386"},
40 {"windows", "386"},
41
42 // Secondary ports, from GOROOT/src/internal/platform/zosarch.go.
43 // (First class ports are commented out.)
44 {"aix", "ppc64"},
45 {"dragonfly", "amd64"},
46 {"freebsd", "386"},
47 {"freebsd", "amd64"},
48 {"freebsd", "arm"},
49 {"freebsd", "arm64"},
50 {"illumos", "amd64"},
51 {"linux", "ppc64"},
52 {"linux", "ppc64le"},
53 {"linux", "mips"},
54 {"linux", "mipsle"},
55 {"linux", "mips64"},
56 {"linux", "mips64le"},
57 {"linux", "riscv64"},
58 {"linux", "s390x"},
59 {"android", "386"},
60 {"android", "amd64"},
61 {"android", "arm"},
62 {"android", "arm64"},
63 {"ios", "arm64"},
64 {"ios", "amd64"},
65 {"js", "wasm"},
66 {"netbsd", "386"},
67 {"netbsd", "amd64"},
68 {"netbsd", "arm"},
69 {"netbsd", "arm64"},
70 {"openbsd", "386"},
71 {"openbsd", "amd64"},
72 {"openbsd", "arm"},
73 {"openbsd", "arm64"},
74 {"openbsd", "mips64"},
75 {"plan9", "386"},
76 {"plan9", "amd64"},
77 {"plan9", "arm"},
78 {"solaris", "amd64"},
79 {"windows", "arm"},
80 {"windows", "arm64"},
81
82 {"aix", "ppc64"},
83 {"android", "386"},
84 {"android", "amd64"},
85 {"android", "arm"},
86 {"android", "arm64"},
87 // {"darwin", "amd64"},
88 // {"darwin", "arm64"},
89 {"dragonfly", "amd64"},
90 {"freebsd", "386"},
91 {"freebsd", "amd64"},
92 {"freebsd", "arm"},
93 {"freebsd", "arm64"},
94 {"freebsd", "riscv64"},
95 {"illumos", "amd64"},
96 {"ios", "amd64"},
97 {"ios", "arm64"},
98 {"js", "wasm"},
99 // {"linux", "386"},
100 // {"linux", "amd64"},
101 // {"linux", "arm"},
102 // {"linux", "arm64"},
103 {"linux", "loong64"},
104 {"linux", "mips"},
105 {"linux", "mips64"},
106 {"linux", "mips64le"},
107 {"linux", "mipsle"},
108 {"linux", "ppc64"},
109 {"linux", "ppc64le"},
110 {"linux", "riscv64"},
111 {"linux", "s390x"},
112 {"linux", "sparc64"},
113 {"netbsd", "386"},
114 {"netbsd", "amd64"},
115 {"netbsd", "arm"},
116 {"netbsd", "arm64"},
117 {"openbsd", "386"},
118 {"openbsd", "amd64"},
119 {"openbsd", "arm"},
120 {"openbsd", "arm64"},
121 {"openbsd", "mips64"},
122 {"openbsd", "ppc64"},
123 {"openbsd", "riscv64"},
124 {"plan9", "386"},
125 {"plan9", "amd64"},
126 {"plan9", "arm"},
127 {"solaris", "amd64"},
128 {"wasip1", "wasm"},
129 // {"windows", "386"},
130 // {"windows", "amd64"},
131 {"windows", "arm"},
132 {"windows", "arm64"},
133 }
134 )
135
136 // matches reports whether the port matches a file with the given absolute path
137 // and content.
138 //
139 // Note that this function accepts content rather than e.g. a file.Handle,
140 // because we trim content before matching for performance reasons, and
141 // therefore need to do this outside of matches when considering multiple ports.
142 func (p port) matches(path string, content []byte) bool {
143 ctxt := build.Default // make a copy
144 ctxt.UseAllFiles = false
145 path = filepath.Clean(path)
146 if !filepath.IsAbs(path) {
147 bug.Reportf("non-abs file path %q", path)
148 return false // fail closed
149 }
150 dir, name := filepath.Split(path)
151
152 // The only virtualized operation called by MatchFile is OpenFile.
153 ctxt.OpenFile = func(p string) (io.ReadCloser, error) {
154 if p != path {
155 return nil, bug.Errorf("unexpected file %q", p)
156 }
157 return io.NopCloser(bytes.NewReader(content)), nil
158 }
159
160 ctxt.GOOS = p.GOOS
161 ctxt.GOARCH = p.GOARCH
162 ok, err := ctxt.MatchFile(dir, name)
163 return err == nil && ok
164 }
165
166 // trimContentForPortMatch trims the given Go file content to a minimal file
167 // containing the same build constraints, if any.
168 //
169 // This is an unfortunate but necessary optimization, as matching build
170 // constraints using go/build has significant overhead, and involves parsing
171 // more than just the build constraint.
172 //
173 // TestMatchingPortsConsistency enforces consistency by comparing results
174 // without trimming content.
175 func trimContentForPortMatch(content []byte) []byte {
176 buildComment := buildComment(content)
177 // The package name does not matter, but +build lines
178 // require a blank line before the package declaration.
179 return []byte(buildComment + "\n\npackage p")
180 }
181
182 // buildComment returns the first matching //go:build comment in the given
183 // content, or "" if none exists.
184 func buildComment(content []byte) string {
185 var lines []string
186
187 f, err := parser.ParseFile(token.NewFileSet(), "", content, parser.PackageClauseOnly|parser.ParseComments)
188 if err != nil {
189 return ""
190 }
191
192 for _, cg := range f.Comments {
193 for _, c := range cg.List {
194 if constraint.IsGoBuild(c.Text) {
195 // A file must have only one //go:build line.
196 return c.Text
197 }
198 if constraint.IsPlusBuild(c.Text) {
199 // A file may have several // +build lines.
200 lines = append(lines, c.Text)
201 }
202 }
203 }
204 return strings.Join(lines, "\n")
205 }