-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
383 lines (353 loc) · 9.89 KB
/
parse.go
File metadata and controls
383 lines (353 loc) · 9.89 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package platforms
import (
"fmt"
"regexp"
"strings"
)
// Split limits for strings.SplitN in decompose functions.
const (
splitTwo = 2
splitThree = 3
splitFour = 4
)
var (
// manylinux_2_17_x86_64, musllinux_1_1_aarch64
reManylinux = regexp.MustCompile(`^(many|musl)linux_(\d+)_(\d+)_(\w+)$`)
// macosx_11_0_arm64
reMacOSX = regexp.MustCompile(`^macosx_(\d+)_(\d+)_(\w+)$`)
// win_amd64, win32, win_arm64
reWinPython = regexp.MustCompile(`^win(?:_(\w+)|32)$`)
// linux_x86_64, linux_aarch64, linux_armv7l
reLinuxPython = regexp.MustCompile(`^linux_(\w+)$`)
)
// Parse converts an ecosystem-specific platform string into a canonical Platform.
func Parse(eco Ecosystem, s string) (Platform, error) {
if !validEcosystem(eco) {
return Platform{}, &ErrUnknownEcosystem{Ecosystem: string(eco)}
}
idx, err := loadData()
if err != nil {
return Platform{}, fmt.Errorf("loading platform data: %w", err)
}
// Check pre-computed platforms.json index first.
if ecoIdx, ok := idx.platByString[string(eco)]; ok {
if i, ok := ecoIdx[strings.ToLower(s)]; ok {
p := idx.platFile.Platforms[i]
plat := Platform{
Arch: p.Arch,
OS: p.OS,
Vendor: p.Vendor,
ABI: p.ABI,
}
// Extract version info from Python strings.
if eco == Python {
parsePythonVersions(s, &plat)
}
return plat, nil
}
}
// Decompose using ecosystem-specific rules.
plat, ok := decompose(idx, eco, s)
if ok {
return plat, nil
}
return Platform{}, &ErrUnknownPlatform{Ecosystem: eco, Input: s}
}
func decompose(idx *indices, eco Ecosystem, s string) (Platform, bool) {
switch eco {
case Go:
return decomposeGo(idx, s)
case Node:
return decomposeNode(idx, s)
case Rust:
return decomposeRustLLVM(idx, eco, s)
case LLVM:
return decomposeRustLLVM(idx, eco, s)
case RubyGems:
return decomposeRubyGems(idx, s)
case Python:
return decomposePython(idx, s)
case Debian:
return decomposeDebian(idx, s)
case NuGet:
return decomposeNuGet(idx, s)
case Vcpkg:
return decomposeVcpkg(idx, s)
case Conan:
return decomposeConan(idx, s)
case Homebrew:
return decomposeHomebrew(idx, s)
case Swift:
return decomposeRustLLVM(idx, Swift, s)
case Kotlin:
return decomposeKotlin(idx, s)
case Maven:
return decomposeMaven(idx, s)
}
return Platform{}, false
}
// go: os/arch
func decomposeGo(idx *indices, s string) (Platform, bool) {
parts := strings.SplitN(s, "/", splitTwo)
if len(parts) != splitTwo {
return Platform{}, false
}
osName := resolveOS(idx, Go, parts[0])
arch := resolveArch(idx, Go, parts[1])
if osName == "" || arch == "" {
return Platform{}, false
}
return Platform{Arch: arch, OS: osName}, true
}
// node: os-arch
func decomposeNode(idx *indices, s string) (Platform, bool) {
parts := strings.SplitN(s, "-", splitTwo)
if len(parts) != splitTwo {
return Platform{}, false
}
osName := resolveOS(idx, Node, parts[0])
arch := resolveArch(idx, Node, parts[1])
if osName == "" || arch == "" {
return Platform{}, false
}
return Platform{Arch: arch, OS: osName}, true
}
// rust/llvm: arch-vendor-os[-abi]
func decomposeRustLLVM(idx *indices, eco Ecosystem, s string) (Platform, bool) {
parts := strings.SplitN(s, "-", splitFour)
if len(parts) < splitThree {
return Platform{}, false
}
arch := resolveArch(idx, eco, parts[0])
if arch == "" {
return Platform{}, false
}
vendor := parts[1]
osName := resolveOS(idx, eco, parts[2])
if osName == "" {
return Platform{}, false
}
p := Platform{Arch: arch, OS: osName, Vendor: vendor}
if len(parts) == splitFour {
p.ABI = normalizeABI(parts[3])
}
return p, true
}
// rubygems: arch-os[-abi] or cpu-os[-version]
func decomposeRubyGems(idx *indices, s string) (Platform, bool) {
parts := strings.SplitN(s, "-", splitThree)
if len(parts) < splitTwo {
return Platform{}, false
}
arch := resolveArch(idx, RubyGems, parts[0])
if arch == "" {
return Platform{}, false
}
osName := resolveOS(idx, RubyGems, parts[1])
if osName == "" {
return Platform{}, false
}
p := Platform{Arch: arch, OS: osName}
if len(parts) == splitThree {
p.ABI = normalizeABI(parts[2])
}
return p, true
}
// python: manylinux_M_m_arch, musllinux_M_m_arch, macosx_M_m_arch, win_arch, win32, linux_arch
func decomposePython(idx *indices, s string) (Platform, bool) {
if m := reManylinux.FindStringSubmatch(s); m != nil {
arch := resolveArch(idx, Python, m[4])
if arch == "" {
return Platform{}, false
}
abi := abiGNU
if m[1] == abiMusl {
abi = abiMusl
}
return Platform{
Arch: arch,
OS: osLinux,
ABI: abi,
LibCVersion: m[2] + "." + m[3],
}, true
}
if m := reMacOSX.FindStringSubmatch(s); m != nil {
arch := resolveArch(idx, Python, m[3])
if arch == "" {
return Platform{}, false
}
return Platform{
Arch: arch,
OS: osDarwin,
Vendor: "apple",
OSVersion: m[1] + "." + m[2],
}, true
}
if s == "win32" {
return Platform{Arch: "i686", OS: osWindows, Vendor: "pc"}, true
}
if m := reWinPython.FindStringSubmatch(s); m != nil && m[1] != "" {
arch := resolveArch(idx, Python, m[1])
if arch == "" {
return Platform{}, false
}
return Platform{Arch: arch, OS: osWindows, Vendor: "pc"}, true
}
if m := reLinuxPython.FindStringSubmatch(s); m != nil {
arch := resolveArch(idx, Python, m[1])
if arch == "" {
return Platform{}, false
}
return Platform{Arch: arch, OS: osLinux}, true
}
return Platform{}, false
}
// debian: arch-os-abi
func decomposeDebian(idx *indices, s string) (Platform, bool) {
parts := strings.SplitN(s, "-", splitThree)
if len(parts) != splitThree {
return Platform{}, false
}
arch := resolveArch(idx, Debian, parts[0])
if arch == "" {
return Platform{}, false
}
osName := resolveOS(idx, Debian, parts[1])
if osName == "" {
return Platform{}, false
}
return Platform{Arch: arch, OS: osName, ABI: normalizeABI(parts[2])}, true
}
func normalizeABI(s string) string {
s = strings.ToLower(s)
switch {
case s == abiGNU || s == abiGNUEABI:
return abiGNU
case s == abiGNUEABIHF:
return abiEABIHF
case s == abiMusl:
return abiMusl
case s == abiMSVC:
return abiMSVC
case strings.HasPrefix(s, "mingw"):
return "mingw"
case s == abiEABI:
return abiEABI
case s == abiEABIHF:
return abiEABIHF
}
return s
}
func parsePythonVersions(s string, p *Platform) {
if m := reManylinux.FindStringSubmatch(s); m != nil {
p.LibCVersion = m[2] + "." + m[3]
} else if m := reMacOSX.FindStringSubmatch(s); m != nil {
p.OSVersion = m[1] + "." + m[2]
}
}
// nuget: os-arch or os-musl-arch (e.g., linux-x64, linux-musl-x64, win-arm64, osx-x64)
func decomposeNuGet(idx *indices, s string) (Platform, bool) {
parts := strings.SplitN(s, "-", splitThree)
if len(parts) == splitThree && strings.ToLower(parts[1]) == abiMusl {
osName := resolveOS(idx, NuGet, parts[0])
arch := resolveArch(idx, NuGet, parts[2])
if osName == "" || arch == "" {
return Platform{}, false
}
return Platform{Arch: arch, OS: osName, ABI: abiMusl}, true
}
if len(parts) < splitTwo {
return Platform{}, false
}
osName := resolveOS(idx, NuGet, parts[0])
arch := resolveArch(idx, NuGet, parts[1])
if osName == "" || arch == "" {
return Platform{}, false
}
return Platform{Arch: arch, OS: osName}, true
}
// vcpkg: arch-os (e.g., x64-linux, arm64-osx, x64-windows)
func decomposeVcpkg(idx *indices, s string) (Platform, bool) {
parts := strings.SplitN(s, "-", splitTwo)
if len(parts) != splitTwo {
return Platform{}, false
}
arch := resolveArch(idx, Vcpkg, parts[0])
osName := resolveOS(idx, Vcpkg, parts[1])
if arch == "" || osName == "" {
return Platform{}, false
}
return Platform{Arch: arch, OS: osName}, true
}
// conan: structured settings, but we handle "os-arch" style strings
// Conan uses settings like os=Linux, arch=armv8 but we parse "os/arch" pairs
func decomposeConan(idx *indices, s string) (Platform, bool) {
// Try os/arch with slash separator
parts := strings.SplitN(s, "/", splitTwo)
if len(parts) == splitTwo {
osName := resolveOS(idx, Conan, parts[0])
arch := resolveArch(idx, Conan, parts[1])
if osName != "" && arch != "" {
return Platform{Arch: arch, OS: osName}, true
}
}
// Try os-arch with dash separator
parts = strings.SplitN(s, "-", splitTwo)
if len(parts) == splitTwo {
osName := resolveOS(idx, Conan, parts[0])
arch := resolveArch(idx, Conan, parts[1])
if osName != "" && arch != "" {
return Platform{Arch: arch, OS: osName}, true
}
}
return Platform{}, false
}
// homebrew: arch_codename (e.g., arm64_sonoma, arm64_ventura)
// We can only extract the arch since the codename maps to a macOS version, not a canonical OS
func decomposeHomebrew(idx *indices, s string) (Platform, bool) {
parts := strings.SplitN(s, "_", splitTwo)
if len(parts) != splitTwo {
return Platform{}, false
}
arch := resolveArch(idx, Homebrew, parts[0])
if arch == "" {
return Platform{}, false
}
// Homebrew is macOS-only
return Platform{Arch: arch, OS: osDarwin, Vendor: "apple"}, true
}
// kotlin: camelCase DSL names (e.g., linuxX64, macosArm64, mingwX64, iosArm64, androidNativeArm64)
var reKotlin = regexp.MustCompile(`^([a-z]+)([A-Z]\w+)$`)
func decomposeKotlin(idx *indices, s string) (Platform, bool) {
m := reKotlin.FindStringSubmatch(s)
if m == nil {
return Platform{}, false
}
osName := resolveOS(idx, Kotlin, m[1])
arch := resolveArch(idx, Kotlin, m[2])
if osName == "" || arch == "" {
return Platform{}, false
}
return Platform{Arch: arch, OS: osName}, true
}
// maven: os-arch (e.g., linux-x86_64, osx-aarch_64, windows-x86_64)
func decomposeMaven(idx *indices, s string) (Platform, bool) {
parts := strings.SplitN(s, "-", splitTwo)
if len(parts) != splitTwo {
return Platform{}, false
}
osName := resolveOS(idx, Maven, parts[0])
arch := resolveArch(idx, Maven, parts[1])
if osName == "" || arch == "" {
return Platform{}, false
}
return Platform{Arch: arch, OS: osName}, true
}
func validEcosystem(eco Ecosystem) bool {
for _, e := range allEcosystems {
if e == eco {
return true
}
}
return false
}