forked from mitchellh/gox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
platform.go
287 lines (252 loc) · 9.11 KB
/
platform.go
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
package main
import (
"fmt"
"log"
"strings"
version "github.com/hashicorp/go-version"
)
// Platform is a combination of OS/arch that can be built against.
type Platform struct {
OS string
Arch string
// Default, if true, will be included as a default build target
// if no OS/arch is specified. We try to only set as a default popular
// targets or targets that are generally useful. For example, Android
// is not a default because it is quite rare that you're cross-compiling
// something to Android AND something like Linux.
Default bool
ARM string
}
func PlatformFromString(os, arch string) Platform {
if strings.HasPrefix(arch, "armv") && len(arch) >= 5 {
return Platform{
OS: os,
Arch: "arm",
ARM: arch[4:],
}
}
return Platform{
OS: os,
Arch: arch,
}
}
func (p *Platform) String() string {
return fmt.Sprintf("%s/%s", p.OS, p.GetArch())
}
func (p *Platform) GetArch() string {
return fmt.Sprintf("%s%s", p.Arch, p.GetARMVersion())
}
func (p *Platform) GetARMVersion() string {
if len(p.ARM) > 0 {
return "v" + p.ARM
}
return ""
}
// addDrop appends all of the "add" entries and drops the "drop" entries, ignoring
// the "Default" parameter.
func addDrop(base []Platform, add []Platform, drop []Platform) []Platform {
newPlatforms := make([]Platform, len(base)+len(add))
copy(newPlatforms, base)
copy(newPlatforms[len(base):], add)
// slow, but we only do this during initialization at most once per version
for _, platform := range drop {
found := -1
for i := range newPlatforms {
if newPlatforms[i].Arch == platform.Arch && newPlatforms[i].OS == platform.OS {
found = i
break
}
}
if found < 0 {
panic(fmt.Sprintf("Expected to remove %+v but not found in list %+v", platform, newPlatforms))
}
if found == len(newPlatforms)-1 {
newPlatforms = newPlatforms[:found]
} else if found == 0 {
newPlatforms = newPlatforms[found:]
} else {
newPlatforms = append(newPlatforms[:found], newPlatforms[found+1:]...)
}
}
return newPlatforms
}
var (
Platforms_1_0 = []Platform{
{OS: "darwin", Arch: "386", Default: true},
{OS: "darwin", Arch: "amd64", Default: true},
{OS: "linux", Arch: "386", Default: true},
{OS: "linux", Arch: "amd64", Default: true},
{OS: "linux", Arch: "arm", Default: true, ARM: "5"},
{OS: "linux", Arch: "arm", Default: true, ARM: "6"},
{OS: "linux", Arch: "arm", Default: true, ARM: "7"},
{OS: "freebsd", Arch: "386", Default: true},
{OS: "freebsd", Arch: "amd64", Default: true},
{OS: "openbsd", Arch: "386", Default: true},
{OS: "openbsd", Arch: "amd64", Default: true},
{OS: "windows", Arch: "386", Default: true},
{OS: "windows", Arch: "amd64", Default: true},
}
Platforms_1_1 = addDrop(Platforms_1_0, []Platform{
{OS: "freebsd", Arch: "arm", Default: true, ARM: "5"},
{OS: "freebsd", Arch: "arm", Default: true, ARM: "6"},
{OS: "freebsd", Arch: "arm", Default: true, ARM: "7"},
{OS: "netbsd", Arch: "386", Default: true},
{OS: "netbsd", Arch: "amd64", Default: true},
{OS: "netbsd", Arch: "arm", Default: true, ARM: "5"},
{OS: "netbsd", Arch: "arm", Default: true, ARM: "6"},
{OS: "netbsd", Arch: "arm", Default: true, ARM: "7"},
{OS: "plan9", Arch: "386", Default: false},
}, nil)
Platforms_1_3 = addDrop(Platforms_1_1, []Platform{
{OS: "dragonfly", Arch: "386", Default: false},
{OS: "dragonfly", Arch: "amd64", Default: false},
{OS: "nacl", Arch: "amd64", Default: false},
{OS: "nacl", Arch: "amd64p32", Default: false},
{OS: "nacl", Arch: "arm", Default: false, ARM: "5"},
{OS: "nacl", Arch: "arm", Default: false, ARM: "6"},
{OS: "nacl", Arch: "arm", Default: false, ARM: "7"},
{OS: "solaris", Arch: "amd64", Default: false},
}, nil)
Platforms_1_4 = addDrop(Platforms_1_3, []Platform{
{OS: "android", Arch: "arm", Default: false, ARM: "5"},
{OS: "android", Arch: "arm", Default: false, ARM: "6"},
{OS: "android", Arch: "arm", Default: false, ARM: "7"},
{OS: "plan9", Arch: "amd64", Default: false},
}, nil)
Platforms_1_5 = addDrop(Platforms_1_4, []Platform{
{OS: "darwin", Arch: "arm", Default: false, ARM: "5"},
{OS: "darwin", Arch: "arm", Default: false, ARM: "6"},
{OS: "darwin", Arch: "arm", Default: false, ARM: "7"},
{OS: "darwin", Arch: "arm64", Default: false},
{OS: "linux", Arch: "arm64", Default: false},
{OS: "linux", Arch: "ppc64", Default: false},
{OS: "linux", Arch: "ppc64le", Default: false},
}, nil)
Platforms_1_6 = addDrop(Platforms_1_5, []Platform{
{OS: "android", Arch: "386", Default: false},
{OS: "android", Arch: "amd64", Default: false},
{OS: "linux", Arch: "mips64", Default: false},
{OS: "linux", Arch: "mips64le", Default: false},
{OS: "nacl", Arch: "386", Default: false},
{OS: "openbsd", Arch: "arm", Default: true, ARM: "5"},
{OS: "openbsd", Arch: "arm", Default: true, ARM: "6"},
{OS: "openbsd", Arch: "arm", Default: true, ARM: "7"},
}, nil)
Platforms_1_7 = addDrop(Platforms_1_5, []Platform{
// While not fully supported s390x is generally useful
{OS: "linux", Arch: "s390x", Default: true},
{OS: "plan9", Arch: "arm", Default: false, ARM: "5"},
{OS: "plan9", Arch: "arm", Default: false, ARM: "6"},
{OS: "plan9", Arch: "arm", Default: false, ARM: "7"},
// Add the 1.6 Platforms, but reflect full support for mips64 and mips64le
{OS: "android", Arch: "386", Default: false},
{OS: "android", Arch: "amd64", Default: false},
{OS: "linux", Arch: "mips64", Default: true},
{OS: "linux", Arch: "mips64le", Default: true},
{OS: "nacl", Arch: "386", Default: false},
{OS: "openbsd", Arch: "arm", Default: true, ARM: "5"},
{OS: "openbsd", Arch: "arm", Default: true, ARM: "6"},
{OS: "openbsd", Arch: "arm", Default: true, ARM: "7"},
}, nil)
Platforms_1_8 = addDrop(Platforms_1_7, []Platform{
{OS: "linux", Arch: "mips", Default: true},
{OS: "linux", Arch: "mipsle", Default: true},
}, nil)
// no new platforms in 1.9
Platforms_1_9 = Platforms_1_8
// unannounced, but dropped support for android/amd64
Platforms_1_10 = addDrop(Platforms_1_9, nil, []Platform{{OS: "android", Arch: "amd64", Default: false}})
Platforms_1_11 = addDrop(Platforms_1_10, []Platform{
{OS: "js", Arch: "wasm", Default: true},
}, nil)
Platforms_1_12 = addDrop(Platforms_1_11, []Platform{
{OS: "aix", Arch: "ppc64", Default: false},
{OS: "windows", Arch: "arm", Default: true, ARM: "5"},
{OS: "windows", Arch: "arm", Default: true, ARM: "6"},
{OS: "windows", Arch: "arm", Default: true, ARM: "7"},
}, nil)
Platforms_1_13 = addDrop(Platforms_1_12, []Platform{
{OS: "illumos", Arch: "amd64", Default: false},
{OS: "netbsd", Arch: "arm64", Default: true},
{OS: "openbsd", Arch: "arm64", Default: true},
}, nil)
Platforms_1_14 = addDrop(Platforms_1_13, []Platform{
{OS: "freebsd", Arch: "arm64", Default: true},
{OS: "linux", Arch: "arm64", Default: true},
{OS: "linux", Arch: "riscv64", Default: true},
}, []Platform{
// drop nacl
{OS: "nacl", Arch: "386", Default: false},
{OS: "nacl", Arch: "amd64", Default: false},
{OS: "nacl", Arch: "arm", Default: false, ARM: "5"},
{OS: "nacl", Arch: "arm", Default: false, ARM: "6"},
{OS: "nacl", Arch: "arm", Default: false, ARM: "7"},
})
Platforms_1_15 = addDrop(Platforms_1_14, []Platform{
{OS: "android", Arch: "arm64", Default: false},
}, []Platform{
// drop i386 macos
{OS: "darwin", Arch: "386", Default: false},
})
Platforms_1_16 = addDrop(Platforms_1_15, []Platform{
{OS: "android", Arch: "amd64", Default: false},
{OS: "darwin", Arch: "arm64", Default: true},
{OS: "openbsd", Arch: "mips64", Default: false},
}, nil)
Platforms_1_17 = addDrop(Platforms_1_16, []Platform{
{OS: "windows", Arch: "arm64", Default: true},
}, nil)
// no new platforms in 1.18
Platforms_1_18 = Platforms_1_17
PlatformsLatest = Platforms_1_18
)
// SupportedPlatforms returns the full list of supported platforms for
// the version of Go that is
func SupportedPlatforms(v string) []Platform {
// Use latest if we get an unexpected version string
if !strings.HasPrefix(v, "go") {
return PlatformsLatest
}
// go-version only cares about version numbers
v = v[2:]
current, err := version.NewVersion(v)
if err != nil {
log.Printf("Unable to parse current go version: %s\n%s", v, err.Error())
// Default to latest
return PlatformsLatest
}
var platforms = []struct {
constraint string
plat []Platform
}{
{"<= 1.0", Platforms_1_0},
{">= 1.1, < 1.3", Platforms_1_1},
{">= 1.3, < 1.4", Platforms_1_3},
{">= 1.4, < 1.5", Platforms_1_4},
{">= 1.5, < 1.6", Platforms_1_5},
{">= 1.6, < 1.7", Platforms_1_6},
{">= 1.7, < 1.8", Platforms_1_7},
{">= 1.8, < 1.9", Platforms_1_8},
{">= 1.9, < 1.10", Platforms_1_9},
{">= 1.10, < 1.11", Platforms_1_10},
{">= 1.11, < 1.12", Platforms_1_11},
{">= 1.12, < 1.13", Platforms_1_12},
{">= 1.13, < 1.14", Platforms_1_13},
{">= 1.14, < 1.15", Platforms_1_14},
{">= 1.15, < 1.16", Platforms_1_15},
{">= 1.16, < 1.17", Platforms_1_16},
{">= 1.17, < 1.18", Platforms_1_17},
{">= 1.18, < 1.19", Platforms_1_18},
}
for _, p := range platforms {
constraints, err := version.NewConstraint(p.constraint)
if err != nil {
panic(err)
}
if constraints.Check(current) {
return p.plat
}
}
// Assume latest
return PlatformsLatest
}