/
platform.go
212 lines (183 loc) · 3.88 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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2023 The Prime Citizens
package toolchain
import (
"fmt"
"runtime"
"strings"
)
// Platform is a os/arch pair
type Platform string
func (p Platform) Pair() (os, arch string) {
os, arch, ok := strings.Cut(string(p), "/")
if ok {
return
}
return "noos", string(p)
}
func (p Platform) OS() string {
ret, _ := p.Pair()
return ret
}
func (p Platform) Arch() string {
_, ret := p.Pair()
return ret
}
func (p Platform) GOOS(envGOOS string) string {
switch os := p.OS(); os {
case "":
if len(envGOOS) != 0 { // from env
return envGOOS
}
return EnvOr("GOHOSTOS", runtime.GOOS) // fallback to host GOOS
case "noos", "efi":
switch p.Arch() {
case "wasm":
return "js"
default:
// use linux for noos target as it supports all available cpu archs
// with most compiler features available
return "linux"
}
case "js", "nodejs", "web", "webext":
return "js"
case "wasip1", "wasi", "wasix":
return "wasip1"
case "darwin", "ios",
"linux", "android",
"freebsd", "openbsd", "netbsd", "solaris", "aix", "dragonfly", "illumos",
"plan9",
"windows":
return os
case "watchos", "tvos":
return "darwin"
default:
panic(fmt.Errorf("invalid platform option: %q", os))
}
}
func (p Platform) GOARCH(envGOARCH string) string {
switch arch := p.Arch(); arch {
case "":
if len(envGOARCH) != 0 {
return envGOARCH
}
return EnvOr("GOHOSTARCH", runtime.GOARCH) // fallback to host GOARCH
case "x86", "x86sse2", "x86sf":
return "386"
case "arm", "armv5", "armv6", "armv7":
return "arm"
case "arm64":
return "arm64"
case "amd64", "amd64v1", "amd64v2", "amd64v3", "amd64v4":
return "amd64"
case "mips", "mipssf", "mipshf":
return "mips"
case "mipsle", "mipslesf", "mipslehf":
return "mipsle"
case "mips64", "mips64sf", "mips64hf":
return "mips64"
case "mips64le", "mips64lesf", "mips64lehf":
return "mips64le"
case "ppc64", "ppc64v8", "ppc64v9", "ppc64v10":
return "ppc64"
case "ppc64le", "ppc64lev8", "ppc64lev9", "ppc64lev10":
return "ppc64le"
case "wasm":
return "wasm"
default:
if strings.HasPrefix(arch, "wasm:") {
return "wasm"
}
panic(fmt.Errorf("unknown arch value: %q", arch))
}
}
func (p Platform) GOARM() string {
switch arch := p.Arch(); arch {
case "":
return GOARM
case "arm":
return "7"
case "armv5", "armv6", "armv7":
return strings.TrimPrefix(arch, "armv")
default:
panic("bad usage")
}
}
func (p Platform) GO386() string {
switch p.Arch() {
case "":
return GO386
case "x86", "x86sse2":
return "sse2"
case "x86sf":
return "softfloat"
default:
panic("bad usage")
}
}
func (p Platform) GOAMD64() string {
switch arch := p.Arch(); arch {
case "":
return GOAMD64
case "amd64", "amd64v1", "amd64v2", "amd64v3", "amd64v4":
suffix := strings.TrimPrefix(arch, "amd64")
if len(suffix) == 0 {
suffix = "v1"
}
return suffix
default:
panic("bad usage")
}
}
func (p Platform) GOMIPS64() string {
switch p.Arch() {
case "":
return GOMIPS64
case "mips64", "mips64hf", "mips64le", "mips64lehf":
return "hardfloat"
case "mips64sf", "mips64lesf":
return "softfloat"
default:
panic("bad usage")
}
}
func (p Platform) GOMIPS() string {
switch p.Arch() {
case "":
return GOMIPS
case "mips", "mipshf", "mipsle", "mipslehf":
return "hardfloat"
case "mipssf", "mipslesf":
return "softfloat"
default:
panic("bad usage")
}
}
func (p Platform) GOPPC64() string {
switch p.Arch() {
case "":
return GOPPC64
case "ppc64", "ppc64v8", "ppc64le", "ppc64lev8":
return "power8"
case "ppc64v9", "ppc64lev9":
return "power9"
case "ppc64v10", "ppc64lev10":
return "power10"
default:
panic("bad usage")
}
}
func (p Platform) GOWASM() string {
switch arch := p.Arch(); arch {
case "":
return GOWASM
case "wasm":
return ""
default:
opts, ok := strings.CutPrefix(arch, "wasm:")
if ok {
return opts
}
panic("bad usage")
}
}