-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsystems.lua
More file actions
270 lines (239 loc) · 5.78 KB
/
systems.lua
File metadata and controls
270 lines (239 loc) · 5.78 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
-- Copyright 2025 The zb Authors
-- SPDX-License-Identifier: MIT
---@param s string
---@param prefix string
---@return boolean
local function hasPrefix(s, prefix)
return s:sub(1, #prefix) == prefix
end
---@param s string
---@return boolean
local function isUnknown(s)
return s == "" or s == "unknown"
end
---@param arch string
---@return boolean
local function isX8632(arch)
return arch == "i386" or
arch == "i486" or
arch == "i586" or
arch == "i686" or
arch == "i786" or
arch == "i886" or
arch == "i986"
end
---@param arch string
---@return boolean
local function isX8664(arch)
return arch == "x86_64" or
arch == "amd64" or
arch == "x86_64h"
end
---@param arch string
---@return boolean
local function isARM32(arch)
return arch == "arm"
end
---@param arch string
---@return boolean
local function isARM64(arch)
return arch == "aarch64" or arch == "arm64"
end
---@param arch string
---@return boolean
local function isRISCV32(arch)
return arch == "riscv32"
end
---@param arch string
---@return boolean
local function isRISCV64(arch)
return arch == "riscv64"
end
---@param os string
---@return boolean
local function isMacOS(os)
return hasPrefix(os, "darwin") or hasPrefix(os, "macos")
end
---@param os string
---@return boolean
local function isiOS(os)
return hasPrefix(os, "ios")
end
---@param os string
---@return boolean
local function isDarwin(os)
return isMacOS(os) or isiOS(os)
end
---@param os string
---@return boolean
local function isWindows(os)
return hasPrefix(os, "windows") or hasPrefix(os, "win32")
end
---@param os string
---@return boolean
local function isLinux(os)
return hasPrefix(os, "linux")
end
---@param os string
---@return boolean
local function isKnownOS(os)
return isLinux(os) or isWindows(os) or isDarwin(os)
end
---@param os string
---@return string
local function defaultVendor(os)
if isDarwin(os) then
return "apple"
elseif isWindows(os) then
return "pc"
else
return "unknown"
end
end
---@param os string
---@return string
local function defaultEnvironment(os)
if isWindows(os) then
return "msvc"
else
return "unknown"
end
end
---@param x any
---@return boolean
local function isEmptyOrNil(x)
return x == nil or x == ""
end
---@param s string
---@return boolean
local function isCygwin(s)
return hasPrefix(s, "cygwin")
end
---@param s string
---@return boolean
local function isMinGW32(s)
return hasPrefix(s, "mingw")
end
---@class system
---@field arch string
---@field vendor string
---@field os string
---@field env string
---@field is32Bit boolean
---@field is64Bit boolean
---@field isX86 boolean
---@field isARM boolean
---@field isRISCV boolean
---@field isMacOS boolean
---@field isiOS boolean
---@field isDarwin boolean
---@field isLinux boolean
---@field isWindows boolean
local systemMetatable = {
__name = "system";
}
---Supported systems for the standard library.
stdlibSystems = {
"x86_64-unknown-linux",
"aarch64-apple-macos",
}
---Parses a system string into a system,
---or returns nil if the string is not a valid system value.
---@param s string
---@return system|nil
function parse(s)
local parts = {}
for part in s:gmatch("[^-]*") do
if #parts >= 4 then
return nil
end
parts[#parts + 1] = part
end
local hasTrailing = false
for i = #parts, 1, -1 do
if isCygwin(parts[i]) or isMinGW32(parts[i]) then
if hasTrailing then return nil end
hasTrailing = true
elseif isEmptyOrNil(parts[i]) then
hasTrailing = true
end
end
if #parts == 1 then
return nil
elseif #parts == 2 then
parts[2], parts[3] = "", parts[2]
elseif #parts == 3 and (isKnownOS(parts[2]) or parts[2] == "none") then
parts[2], parts[3], parts[4] = "", parts[2], parts[3]
end
-- Expand Cygwin/MinGW first, since that can trigger other implications.
if isCygwin(parts[3]) then
parts[3] = "windows"
parts[4] = "cygnus"
elseif isMinGW32(parts[3]) then
parts[3] = "windows"
parts[4] = "gnu"
end
-- Now fill in the system based on the parts.
local arch = "unknown"
local vendor = "unknown"
local os = "unknown"
local env = "unknown"
if not isEmptyOrNil(parts[1]) then
arch = parts[1]
end
if not isEmptyOrNil(parts[3]) then
os = parts[3]
end
if not isEmptyOrNil(parts[2]) then
vendor = parts[2]
else
vendor = defaultVendor(os)
end
if not isEmptyOrNil(parts[4]) then
env = parts[4]
else
env = defaultEnvironment(os)
end
return setmetatable({
arch = arch;
os = os;
vendor = vendor;
env = env;
is32Bit = isX8632(arch) or isARM32(arch) or isRISCV32(arch);
is64Bit = isX8664(arch) or isARM64(arch) or isRISCV64(arch);
isX86 = isX8632(arch) or isX8664(arch);
isARM = isARM32(arch) or isARM64(arch);
isRISCV = isRISCV32(arch) or isRISCV64(arch);
isMacOS = isMacOS(os);
isiOS = isiOS(os);
isDarwin = isDarwin(os);
isLinux = isLinux(os);
isWindows = isWindows(os);
}, systemMetatable)
end
local function toUnknown(x)
if x == nil or x == "" then
return "unknown"
end
return x
end
---Converts a system-like table into a system string value.
---@param sys {
---arch: string,
---vendor: string,
---os: string,
---env: string,
---}
---@return string
function tostring(sys)
if isLinux(sys.os) and isUnknown(sys.vendor) and isUnknown(sys.env) then
return toUnknown(sys.arch).."-"..toUnknown(sys.os)
elseif isWindows(sys.os) and hasPrefix(sys.env, "cygnus") then
return toUnknown(sys.arch).."-"..toUnknown(sys.vendor).."-cygwin"
elseif toUnknown(sys.env) == defaultEnvironment(sys.os) then
return toUnknown(sys.arch).."-"..toUnknown(sys.vendor).."-"..toUnknown(sys.os)
else
return toUnknown(sys.arch).."-"..toUnknown(sys.vendor).."-"..toUnknown(sys.os).."-"..toUnknown(sys.env)
end
end
systemMetatable.__tostring = tostring