2
2
// Use of this source code is governed by a BSD-style
3
3
// license that can be found in the LICENSE file.
4
4
5
+ //go:generate go test . -run=^TestGenerated$ -fix
6
+
5
7
package platform
6
8
9
+ // An OSArch is a pair of GOOS and GOARCH values indicating a platform.
10
+ type OSArch struct {
11
+ GOOS , GOARCH string
12
+ }
13
+
14
+ func (p OSArch ) String () string {
15
+ return p .GOOS + "/" + p .GOARCH
16
+ }
17
+
7
18
// RaceDetectorSupported reports whether goos/goarch supports the race
8
19
// detector. There is a copy of this function in cmd/dist/test.go.
9
20
// Race detector only supports 48-bit VMA on arm64. But it will always
@@ -24,11 +35,10 @@ func RaceDetectorSupported(goos, goarch string) bool {
24
35
25
36
// MSanSupported reports whether goos/goarch supports the memory
26
37
// sanitizer option.
27
- // There is a copy of this function in misc/cgo/testsanitizers/cc_test.go.
28
38
func MSanSupported (goos , goarch string ) bool {
29
39
switch goos {
30
40
case "linux" :
31
- return goarch == "amd64" || goarch == "arm64"
41
+ return goarch == "amd64" || goarch == "arm64" || goarch == "loong64"
32
42
case "freebsd" :
33
43
return goarch == "amd64"
34
44
default :
@@ -38,11 +48,10 @@ func MSanSupported(goos, goarch string) bool {
38
48
39
49
// ASanSupported reports whether goos/goarch supports the address
40
50
// sanitizer option.
41
- // There is a copy of this function in misc/cgo/testsanitizers/cc_test.go.
42
51
func ASanSupported (goos , goarch string ) bool {
43
52
switch goos {
44
53
case "linux" :
45
- return goarch == "arm64" || goarch == "amd64" || goarch == "riscv64" || goarch == "ppc64le"
54
+ return goarch == "arm64" || goarch == "amd64" || goarch == "loong64" || goarch == " riscv64" || goarch == "ppc64le"
46
55
default :
47
56
return false
48
57
}
@@ -71,20 +80,12 @@ func FuzzInstrumented(goos, goarch string) bool {
71
80
}
72
81
}
73
82
74
- // MustLinkExternal reports whether goos/goarch requires external linking.
75
- func MustLinkExternal (goos , goarch string ) bool {
76
- return MustLinkExternalGo121 (goos , goarch , false )
77
- }
78
-
79
- // MustLinkExternalGo121 reports whether goos/goarch requires external linking,
80
- // with or without cgo dependencies. [This version back-ported from
81
- // Go 1.21 as part of a test].
82
- func MustLinkExternalGo121 (goos , goarch string , withCgo bool ) bool {
83
+ // MustLinkExternal reports whether goos/goarch requires external linking
84
+ // with or without cgo dependencies.
85
+ func MustLinkExternal (goos , goarch string , withCgo bool ) bool {
83
86
if withCgo {
84
87
switch goarch {
85
- case "loong64" ,
86
- "mips" , "mipsle" , "mips64" , "mips64le" ,
87
- "riscv64" :
88
+ case "loong64" , "mips" , "mipsle" , "mips64" , "mips64le" :
88
89
// Internally linking cgo is incomplete on some architectures.
89
90
// https://go.dev/issue/14449
90
91
return true
@@ -96,7 +97,9 @@ func MustLinkExternalGo121(goos, goarch string, withCgo bool) bool {
96
97
case "ppc64" :
97
98
// Big Endian PPC64 cgo internal linking is not implemented for aix or linux.
98
99
// https://go.dev/issue/8912
99
- return true
100
+ if goos == "aix" || goos == "linux" {
101
+ return true
102
+ }
100
103
}
101
104
102
105
switch goos {
@@ -125,25 +128,48 @@ func MustLinkExternalGo121(goos, goarch string, withCgo bool) bool {
125
128
126
129
// BuildModeSupported reports whether goos/goarch supports the given build mode
127
130
// using the given compiler.
131
+ // There is a copy of this function in cmd/dist/test.go.
128
132
func BuildModeSupported (compiler , buildmode , goos , goarch string ) bool {
129
133
if compiler == "gccgo" {
130
134
return true
131
135
}
132
136
133
- platform := goos + "/" + goarch
137
+ if _ , ok := distInfo [OSArch {goos , goarch }]; ! ok {
138
+ return false // platform unrecognized
139
+ }
134
140
141
+ platform := goos + "/" + goarch
135
142
switch buildmode {
136
143
case "archive" :
137
144
return true
138
145
139
146
case "c-archive" :
140
- // TODO(bcmills): This seems dubious.
141
- // Do we really support c-archive mode on js/wasm‽
142
- return platform != "linux/ppc64"
147
+ switch goos {
148
+ case "aix" , "darwin" , "ios" , "windows" :
149
+ return true
150
+ case "linux" :
151
+ switch goarch {
152
+ case "386" , "amd64" , "arm" , "armbe" , "arm64" , "arm64be" , "loong64" , "ppc64le" , "riscv64" , "s390x" :
153
+ // linux/ppc64 not supported because it does
154
+ // not support external linking mode yet.
155
+ return true
156
+ default :
157
+ // Other targets do not support -shared,
158
+ // per ParseFlags in
159
+ // cmd/compile/internal/base/flag.go.
160
+ // For c-archive the Go tool passes -shared,
161
+ // so that the result is suitable for inclusion
162
+ // in a PIE or shared library.
163
+ return false
164
+ }
165
+ case "freebsd" :
166
+ return goarch == "amd64"
167
+ }
168
+ return false
143
169
144
170
case "c-shared" :
145
171
switch platform {
146
- case "linux/amd64" , "linux/arm" , "linux/arm64" , "linux/386" , "linux/ppc64le" , "linux/riscv64" , "linux/s390x" ,
172
+ case "linux/amd64" , "linux/arm" , "linux/arm64" , "linux/loong64" , "linux/ 386" , "linux/ppc64le" , "linux/riscv64" , "linux/s390x" ,
147
173
"android/amd64" , "android/arm" , "android/arm64" , "android/386" ,
148
174
"freebsd/amd64" ,
149
175
"darwin/amd64" , "darwin/arm64" ,
@@ -160,7 +186,7 @@ func BuildModeSupported(compiler, buildmode, goos, goarch string) bool {
160
186
161
187
case "pie" :
162
188
switch platform {
163
- case "linux/386" , "linux/amd64" , "linux/arm" , "linux/arm64" , "linux/ppc64le" , "linux/riscv64" , "linux/s390x" ,
189
+ case "linux/386" , "linux/amd64" , "linux/arm" , "linux/arm64" , "linux/loong64" , "linux/ ppc64le" , "linux/riscv64" , "linux/s390x" ,
164
190
"android/amd64" , "android/arm" , "android/arm64" , "android/386" ,
165
191
"freebsd/amd64" ,
166
192
"darwin/amd64" , "darwin/arm64" ,
@@ -180,8 +206,8 @@ func BuildModeSupported(compiler, buildmode, goos, goarch string) bool {
180
206
181
207
case "plugin" :
182
208
switch platform {
183
- case "linux/amd64" , "linux/arm" , "linux/arm64" , "linux/386" , "linux/s390x" , "linux/ppc64le" ,
184
- "android/amd64" , "android/arm" , "android/arm64" , "android/ 386" ,
209
+ case "linux/amd64" , "linux/arm" , "linux/arm64" , "linux/386" , "linux/loong64" , "linux/ s390x" , "linux/ppc64le" ,
210
+ "android/amd64" , "android/386" ,
185
211
"darwin/amd64" , "darwin/arm64" ,
186
212
"freebsd/amd64" :
187
213
return true
@@ -195,11 +221,66 @@ func BuildModeSupported(compiler, buildmode, goos, goarch string) bool {
195
221
196
222
func InternalLinkPIESupported (goos , goarch string ) bool {
197
223
switch goos + "/" + goarch {
198
- case "darwin/amd64" , "darwin/arm64" ,
224
+ case "android/arm64" ,
225
+ "darwin/amd64" , "darwin/arm64" ,
199
226
"linux/amd64" , "linux/arm64" , "linux/ppc64le" ,
200
- "android/arm64" ,
201
- "windows-amd64" , "windows-386" , "windows-arm" :
227
+ "windows/386" , "windows/amd64" , "windows/arm" , "windows/arm64" :
228
+ return true
229
+ }
230
+ return false
231
+ }
232
+
233
+ // DefaultPIE reports whether goos/goarch produces a PIE binary when using the
234
+ // "default" buildmode. On Windows this is affected by -race,
235
+ // so force the caller to pass that in to centralize that choice.
236
+ func DefaultPIE (goos , goarch string , isRace bool ) bool {
237
+ switch goos {
238
+ case "android" , "ios" :
239
+ return true
240
+ case "windows" :
241
+ if isRace {
242
+ // PIE is not supported with -race on windows;
243
+ // see https://go.dev/cl/416174.
244
+ return false
245
+ }
246
+ return true
247
+ case "darwin" :
202
248
return true
203
249
}
204
250
return false
205
251
}
252
+
253
+ // ExecutableHasDWARF reports whether the linked executable includes DWARF
254
+ // symbols on goos/goarch.
255
+ func ExecutableHasDWARF (goos , goarch string ) bool {
256
+ switch goos {
257
+ case "plan9" , "ios" :
258
+ return false
259
+ }
260
+ return true
261
+ }
262
+
263
+ // osArchInfo describes information about an OSArch extracted from cmd/dist and
264
+ // stored in the generated distInfo map.
265
+ type osArchInfo struct {
266
+ CgoSupported bool
267
+ FirstClass bool
268
+ Broken bool
269
+ }
270
+
271
+ // CgoSupported reports whether goos/goarch supports cgo.
272
+ func CgoSupported (goos , goarch string ) bool {
273
+ return distInfo [OSArch {goos , goarch }].CgoSupported
274
+ }
275
+
276
+ // FirstClass reports whether goos/goarch is considered a “first class” port.
277
+ // (See https://go.dev/wiki/PortingPolicy#first-class-ports.)
278
+ func FirstClass (goos , goarch string ) bool {
279
+ return distInfo [OSArch {goos , goarch }].FirstClass
280
+ }
281
+
282
+ // Broken reportsr whether goos/goarch is considered a broken port.
283
+ // (See https://go.dev/wiki/PortingPolicy#broken-ports.)
284
+ func Broken (goos , goarch string ) bool {
285
+ return distInfo [OSArch {goos , goarch }].Broken
286
+ }
0 commit comments