| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
preferredPorts = []port{ |
| 32 |
|
| 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 |
|
| 43 |
|
| 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 |
|
| 88 |
|
| 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 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 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 |
|
| 130 |
|
| 131 |
{"windows", "arm"}, |
| 132 |
{"windows", "arm64"}, |
| 133 |
} |
| 134 |
) |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
func (p port) matches(path string, content []byte) bool { |
| 143 |
ctxt := build.Default |
| 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 |
| 149 |
} |
| 150 |
dir, name := filepath.Split(path) |
| 151 |
|
| 152 |
|
| 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 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
func trimContentForPortMatch(content []byte) []byte { |
| 176 |
buildComment := buildComment(content) |
| 177 |
|
| 178 |
|
| 179 |
return []byte(buildComment + "\n\npackage p") |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 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 |
|
| 196 |
return c.Text |
| 197 |
} |
| 198 |
if constraint.IsPlusBuild(c.Text) { |
| 199 |
|
| 200 |
lines = append(lines, c.Text) |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
return strings.Join(lines, "\n") |
| 205 |
} |