-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli.ts
More file actions
422 lines (385 loc) · 12.3 KB
/
Copy pathcli.ts
File metadata and controls
422 lines (385 loc) · 12.3 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
export type ToolchainSource = "managed" | "system";
export type ToolchainStatus = "installed" | "available" | "planned";
export interface ToolchainItem {
family: string;
version: string;
spec: string;
source: ToolchainSource;
effective: boolean;
}
export interface TargetItem {
target: string;
note: string;
toolchainSpec: string | undefined;
status: ToolchainStatus;
effective: boolean;
}
export interface ToolchainInventory {
installed: ToolchainItem[];
available: ToolchainItem[];
targets: TargetItem[];
effective: ToolchainItem | undefined;
effectiveTarget: string | undefined;
globalDefaultSpec: string | undefined;
projectOverridesGlobal: boolean;
recognized: boolean;
rawOutput: string;
}
type Section = "managed" | "system" | "available" | "targets" | undefined;
const ANSI_ESCAPE = /\u001b\[[0-?]*[ -/]*[@-~]/g;
const NAME_PATTERN = "[A-Za-z][A-Za-z0-9+_.-]*";
const FAMILY_PATTERN = `(?:${NAME_PATTERN}:)?${NAME_PATTERN}`;
const VERSION_PATTERN = "[0-9][A-Za-z0-9+_.:-]*";
const SPEC_VERSION_PATTERN = `(?:${VERSION_PATTERN}|system)`;
const INPUT_VERSION_PATTERN = "[A-Za-z0-9][A-Za-z0-9+_.:-]*";
const AUXILIARY_WORDS = new Set([
"available",
"default",
"global",
"host",
"installed",
"no",
"none",
"run",
"system",
"target",
"targets",
"toolchain",
"toolchains",
]);
export function mcppCommandArguments(...args: string[]): string[] {
return [...args];
}
function unqualifiedFamily(value: string): string {
const colon = value.indexOf(":");
return (colon === -1 ? value : value.slice(colon + 1)).toLowerCase();
}
export function normalizeToolchainSpec(input: string): string | undefined {
let value = input.trim();
if (value.length >= 2) {
const first = value[0];
const last = value[value.length - 1];
if ((first === "'" && last === "'") || (first === '"' && last === '"')) {
value = value.slice(1, -1).trim();
}
}
if (/^msvc$/i.test(value)) {
return "msvc";
}
const atMatch = value.match(
new RegExp(`^(${FAMILY_PATTERN})@(${INPUT_VERSION_PATTERN})$`),
);
if (atMatch !== null) {
const family = atMatch[1].toLowerCase();
const version = atMatch[2];
return `${family}@${version}`;
}
const spacedMatch = value.match(
new RegExp(`^(${FAMILY_PATTERN})\\s+(${INPUT_VERSION_PATTERN})$`),
);
if (spacedMatch !== null) {
const family = spacedMatch[1].toLowerCase();
const version = spacedMatch[2];
return `${family}@${version}`;
}
const bareMatch = value.match(new RegExp(`^(${FAMILY_PATTERN})$`));
if (bareMatch !== null) {
return bareMatch[1].toLowerCase();
}
return undefined;
}
export function isMsvcToolchainSpec(input: string): boolean {
const normalized = normalizeToolchainSpec(input);
const compiler = normalized?.split("@", 1)[0];
return compiler !== undefined && unqualifiedFamily(compiler) === "msvc";
}
export type ToolchainInstallKind = "managed-host" | "managed-target" | "system-detect";
/**
* 返回兼容拼写可能隐含的 target 提示;undefined 表示没有对应提示。
* 结果只用于 UI 分流,输入和 triple 的合法性最终由 mcpp CLI 判断。
*/
export type ToolchainSpecTargetHint = "musl" | "windows-gnu" | "target";
export function toolchainSpecTargetHint(input: string): ToolchainSpecTargetHint | undefined {
const normalized = normalizeToolchainSpec(input)?.toLowerCase();
if (normalized === undefined) {
return undefined;
}
const qualifiedCompiler = normalized.split("@", 1)[0];
const compiler = unqualifiedFamily(qualifiedCompiler);
if (
compiler === "musl-gcc"
|| (compiler === "gcc" && normalized.endsWith("-musl"))
) {
return "musl";
}
if (compiler.includes("mingw")) {
return "windows-gnu";
}
// mcpp 还接受带 triple 的旧编译器写法,例如
// `aarch64-linux-musl-gcc@16`。这里只识别其外形,triple 是否有效由 mcpp 判断。
if (compiler.endsWith("-gcc")) {
return compiler.includes("-musl-") ? "musl" : "target";
}
return undefined;
}
export function toolchainInstallKind(input: string): ToolchainInstallKind {
if (isMsvcToolchainSpec(input)) {
return "system-detect";
}
return toolchainSpecTargetHint(input) === undefined ? "managed-host" : "managed-target";
}
export function hostDefaultToolchains(inventory: ToolchainInventory): ToolchainItem[] {
const hostSpecs = new Set(
inventory.targets
.filter((target) => target.status === "installed" && /\bhost\b/i.test(target.note))
.map((target) => target.toolchainSpec)
.filter((spec): spec is string => spec !== undefined),
);
const windowsHost = inventory.targets.some((target) =>
target.status !== "planned"
&& target.target.endsWith("-windows-msvc")
&& /\bhost\b/i.test(target.note),
);
const windowsGccSpecs = new Set(
inventory.targets
.filter((target) => target.status === "installed" && target.target.endsWith("-windows-gnu"))
.map((target) => target.toolchainSpec)
.filter((spec): spec is string => spec !== undefined),
);
if (inventory.targets.length === 0) {
return inventory.installed;
}
return inventory.installed.filter((toolchain) =>
toolchain.source === "system"
|| hostSpecs.has(toolchain.spec)
// mcpp 在 Windows 上将省略 target 的 GCC 映射到 MinGW payload。
|| (windowsHost && toolchain.family === "gcc" && windowsGccSpecs.has(toolchain.spec)));
}
function sectionForHeader(line: string): Section | "header" | false {
if (/^\s*Toolchains\s*:\s*$/i.test(line)) {
return "managed";
}
if (/^\s*System(?:\s+toolchains?)?\s*:\s*$/i.test(line)) {
return "system";
}
if (/^\s*Targets\s*:\s*$/i.test(line)) {
return "targets";
}
if (/^\s*Available\s+toolchains?\b.*:\s*$/i.test(line)) {
return "available";
}
if (/^\s*[A-Za-z][A-Za-z0-9 _-]*\s*:\s*$/i.test(line)) {
return "header";
}
return false;
}
function parseToolchainRow(
line: string,
source: ToolchainSource,
): Array<{ family: string; version: string; effective: boolean; spec: string }> | undefined {
let value = line.trim();
let effective = false;
if (value.startsWith("*")) {
effective = true;
value = value.slice(1).trimStart();
}
if (value.startsWith("-")) {
value = value.slice(1).trimStart();
}
if (value.startsWith("(") || value.length === 0) {
return undefined;
}
const tokens = value.split(/\s+/);
const firstToken = tokens[0];
if (firstToken === undefined || AUXILIARY_WORDS.has(firstToken.toLowerCase())) {
return undefined;
}
let family: string;
let versions: string[];
if (firstToken.includes("@")) {
const spec = normalizeToolchainSpec(firstToken);
if (spec === undefined) {
return undefined;
}
const atIndex = firstToken.indexOf("@");
family = firstToken.slice(0, atIndex).toLowerCase();
versions = [firstToken.slice(atIndex + 1)];
} else {
const secondToken = tokens[1];
if (secondToken === undefined || !new RegExp(`^${VERSION_PATTERN}$`).test(secondToken)) {
return undefined;
}
family = firstToken.toLowerCase();
versions = [secondToken];
for (let index = 2; index + 1 < tokens.length; index += 2) {
if (tokens[index] !== "/" || !new RegExp(`^${VERSION_PATTERN}$`).test(tokens[index + 1])) {
break;
}
versions.push(tokens[index + 1]);
}
}
return versions.flatMap((version) => {
const spec = source === "system" && family === "msvc"
? "msvc"
: normalizeToolchainSpec(`${family} ${version}`);
return spec === undefined ? [] : [{ family, version, spec, effective }];
});
}
function parseTargetRow(line: string): TargetItem | undefined {
let value = line.trim();
let effective = false;
if (value.startsWith("*")) {
effective = true;
value = value.slice(1).trimStart();
}
if (value.startsWith("-")) {
value = value.slice(1).trimStart();
}
const tokens = value.split(/\s+/);
if (tokens.length < 3 || tokens[0]?.toUpperCase() === "TARGET") {
return undefined;
}
const statusToken = tokens.at(-1)?.toLowerCase();
if (statusToken !== "installed" && statusToken !== "available" && statusToken !== "planned") {
return undefined;
}
const target = tokens[0];
const columns = tokens.slice(1, -1);
if (target === undefined || columns.length === 0) {
return undefined;
}
const lastColumn = columns.at(-1);
let noteColumns: string[];
let toolchainSpec: string | undefined;
if (lastColumn === "\u2014" || lastColumn === "-") {
noteColumns = columns.slice(0, -1);
} else if (columns.length >= 2) {
const family = columns.at(-2);
const version = columns.at(-1);
if (family === undefined || version === undefined) {
return undefined;
}
toolchainSpec = normalizeToolchainSpec(family + " " + version);
if (toolchainSpec === undefined) {
return undefined;
}
noteColumns = columns.slice(0, -2);
} else {
return undefined;
}
return {
target,
note: noteColumns.join(" "),
toolchainSpec,
status: statusToken,
effective,
};
}
interface ParsedGlobalDefault {
value: string | undefined;
}
function parseGlobalDefault(line: string): ParsedGlobalDefault | undefined {
const match = line.match(/\bglobal\s+default\s+is\s+['"]([^'"]+)['"]/i);
if (match !== null) {
return { value: normalizeToolchainSpec(match[1]) };
}
const none = line.match(/\bglobal\s+default\s+is\s+['"]?<none>['"]?/i);
if (none !== null) {
return { value: undefined };
}
const unquoted = line.match(
new RegExp(`\\bglobal\\s+default\\s+is\\s+(${FAMILY_PATTERN}(?:@${SPEC_VERSION_PATTERN}|\\s+${VERSION_PATTERN}|))\\b`, "i"),
);
return unquoted === null ? undefined : { value: normalizeToolchainSpec(unquoted[1]) };
}
export function parseToolchainList(output: string): ToolchainInventory {
const installed: ToolchainItem[] = [];
const available: ToolchainItem[] = [];
const targets: TargetItem[] = [];
let effective: ToolchainItem | undefined;
let effectiveTarget: string | undefined;
let section: Section;
let recognized = false;
let projectMarker = false;
let explicitGlobalDefault: string | undefined;
let explicitGlobalDefaultSeen = false;
for (const rawLine of output.split(/\r?\n/)) {
const line = rawLine.replace(ANSI_ESCAPE, "");
const lowerLine = line.toLowerCase();
if (lowerLine.includes("no toolchains installed")) {
recognized = true;
}
if (/effective\s+toolchain\s+from\s+project\s+mcpp\.toml\s+\[toolchain\]/i.test(line)) {
projectMarker = true;
}
const parsedGlobalDefault = parseGlobalDefault(line);
if (parsedGlobalDefault !== undefined) {
explicitGlobalDefaultSeen = true;
explicitGlobalDefault = parsedGlobalDefault.value;
}
const header = sectionForHeader(line);
if (header !== false) {
if (header === "header") {
section = undefined;
} else {
recognized = true;
section = header;
}
continue;
}
if (section === undefined) {
continue;
}
if (section === "targets") {
const target = parseTargetRow(line);
if (target === undefined) {
continue;
}
targets.push(target);
if (target.effective && effectiveTarget === undefined) {
effectiveTarget = target.target;
}
continue;
}
const parsedRows = parseToolchainRow(line, section === "system" ? "system" : "managed");
if (parsedRows === undefined) {
continue;
}
for (const parsed of parsedRows) {
const item: ToolchainItem = {
family: parsed.family,
version: parsed.version,
spec: parsed.spec,
source: section === "system" ? "system" : "managed",
effective: parsed.effective,
};
if (section === "available") {
available.push(item);
} else {
installed.push(item);
if (item.effective && effective === undefined) {
effective = item;
}
}
}
}
const globalDefaultSpec = recognized
? explicitGlobalDefaultSeen ? explicitGlobalDefault : effective?.spec
: undefined;
const projectOverridesGlobal = recognized
&& (projectMarker
|| (explicitGlobalDefaultSeen
&& effective !== undefined
&& explicitGlobalDefault !== effective.spec));
return {
installed,
available,
targets,
effective,
effectiveTarget,
globalDefaultSpec,
projectOverridesGlobal,
recognized,
rawOutput: output,
};
}