forked from swiftlang/vscode-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolchain.ts
849 lines (795 loc) · 30.1 KB
/
toolchain.ts
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2021-2023 the VS Code Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import * as fs from "fs/promises";
import * as path from "path";
import * as os from "os";
import * as plist from "plist";
import * as vscode from "vscode";
import configuration from "../configuration";
import { SwiftOutputChannel } from "../ui/SwiftOutputChannel";
import { execFile, execSwift } from "../utilities/utilities";
import { expandFilePathTilda, pathExists } from "../utilities/filesystem";
import { Version } from "../utilities/version";
import { BuildFlags } from "./BuildFlags";
import { Sanitizer } from "./Sanitizer";
/**
* Contents of **Info.plist** on Windows.
*/
interface InfoPlist {
DefaultProperties: {
XCTEST_VERSION: string | undefined;
SWIFT_TESTING_VERSION: string | undefined;
};
}
/**
* Project template information retrieved from `swift package init --help`
*/
export interface SwiftProjectTemplate {
id: string;
name: string;
description: string;
}
/**
* Stripped layout of `swift -print-target-info` output.
*/
interface SwiftTargetInfo {
compilerVersion: string;
target?: {
triple: string;
unversionedTriple: string;
[name: string]: string | string[];
};
paths: {
runtimeLibraryPaths: string[];
[name: string]: string | string[];
};
[name: string]: string | object | undefined;
}
/**
* A Swift compilation target that can be compiled to
* from macOS. These are similar to XCode's target list.
*/
export enum DarwinCompatibleTarget {
iOS = "iOS",
tvOS = "tvOS",
watchOS = "watchOS",
visionOS = "xrOS",
}
export function getDarwinSDKName(target: DarwinCompatibleTarget): string {
switch (target) {
case DarwinCompatibleTarget.iOS:
return "iphoneos";
case DarwinCompatibleTarget.tvOS:
return "appletvos";
case DarwinCompatibleTarget.watchOS:
return "watchos";
case DarwinCompatibleTarget.visionOS:
return "xros";
}
}
export function getDarwinTargetTriple(target: DarwinCompatibleTarget): string | undefined {
switch (target) {
case DarwinCompatibleTarget.iOS:
return "arm64-apple-ios";
case DarwinCompatibleTarget.tvOS:
return "arm64-apple-tvos";
case DarwinCompatibleTarget.watchOS:
return "arm64-apple-watchos";
case DarwinCompatibleTarget.visionOS:
return "arm64-apple-xros";
}
}
export class SwiftToolchain {
public swiftVersionString: string;
constructor(
public swiftFolderPath: string, // folder swift executable in $PATH was found in
public toolchainPath: string, // toolchain folder. One folder up from swift bin folder. This is to support toolchains without usr folder
private targetInfo: SwiftTargetInfo,
public swiftVersion: Version, // Swift version as semVar variable
public runtimePath?: string, // runtime library included in output from `swift -print-target-info`
public defaultSDK?: string,
public customSDK?: string,
public xcTestPath?: string,
public swiftTestingPath?: string,
public swiftPMTestingHelperPath?: string
) {
this.swiftVersionString = targetInfo.compilerVersion;
}
static async create(): Promise<SwiftToolchain> {
const swiftFolderPath = await this.getSwiftFolderPath();
const toolchainPath = await this.getToolchainPath(swiftFolderPath);
const targetInfo = await this.getSwiftTargetInfo();
const swiftVersion = await this.getSwiftVersion(targetInfo);
const runtimePath = await this.getRuntimePath(targetInfo);
const defaultSDK = await this.getDefaultSDK();
const customSDK = this.getCustomSDK();
const xcTestPath = await this.getXCTestPath(
targetInfo,
swiftFolderPath,
swiftVersion,
runtimePath,
customSDK ?? defaultSDK
);
const swiftTestingPath = await this.getSwiftTestingPath(
targetInfo,
swiftVersion,
runtimePath,
customSDK ?? defaultSDK
);
const swiftPMTestingHelperPath = await this.getSwiftPMTestingHelperPath(toolchainPath);
return new SwiftToolchain(
swiftFolderPath,
toolchainPath,
targetInfo,
swiftVersion,
runtimePath,
defaultSDK,
customSDK,
xcTestPath,
swiftTestingPath,
swiftPMTestingHelperPath
);
}
public get unversionedTriple(): string | undefined {
return this.targetInfo.target?.unversionedTriple;
}
/** build flags */
public get buildFlags(): BuildFlags {
return new BuildFlags(this);
}
/** build flags */
public sanitizer(name: string): Sanitizer | undefined {
return Sanitizer.create(name, this);
}
/**
* Returns true if the console output of `swift test --parallel` prints results
* to stdout with newlines or not.
*/
public get hasMultiLineParallelTestOutput(): boolean {
return (
this.swiftVersion.isLessThanOrEqual(new Version(5, 6, 0)) ||
this.swiftVersion.isGreaterThanOrEqual(new Version(6, 0, 0))
);
}
/**
* Get active developer dir for Xcode
*/
public static async getXcodeDeveloperDir(env?: { [key: string]: string }): Promise<string> {
const { stdout } = await execFile("xcode-select", ["-p"], {
env: env,
});
return stdout.trimEnd();
}
/**
* @param target Target to obtain the SDK path for
* @returns path to the SDK for the target
*/
public static async getSDKForTarget(
target: DarwinCompatibleTarget
): Promise<string | undefined> {
return await this.getSDKPath(getDarwinSDKName(target));
}
/**
* @param sdk sdk name
* @returns path to the SDK
*/
static async getSDKPath(sdk: string): Promise<string | undefined> {
// Include custom variables so that non-standard XCode installs can be better supported.
const { stdout } = await execFile("xcrun", ["--sdk", sdk, "--show-sdk-path"], {
env: { ...process.env, ...configuration.swiftEnvironmentVariables },
});
return path.join(stdout.trimEnd());
}
/**
* Get list of Xcode versions intalled on mac
* @returns Folders for each Xcode install
*/
public static async getXcodeInstalls(): Promise<string[]> {
if (process.platform !== "darwin") {
return [];
}
const { stdout: xcodes } = await execFile("mdfind", [
`kMDItemCFBundleIdentifier == 'com.apple.dt.Xcode'`,
]);
return xcodes.trimEnd().split("\n");
}
/**
* Reads the swiftly configuration file to find a list of installed toolchains.
*
* @returns an array of toolchain paths
*/
public static async getSwiftlyToolchainInstalls(): Promise<string[]> {
// Swiftly is only available on Linux right now
if (process.platform !== "linux") {
return [];
}
try {
const swiftlyHomeDir: string | undefined = process.env["SWIFTLY_HOME_DIR"];
if (!swiftlyHomeDir) {
return [];
}
const swiftlyConfigRaw = await fs.readFile(
path.join(swiftlyHomeDir, "config.json"),
"utf-8"
);
const swiftlyConfig: unknown = JSON.parse(swiftlyConfigRaw);
if (!(swiftlyConfig instanceof Object) || !("installedToolchains" in swiftlyConfig)) {
return [];
}
const installedToolchains = swiftlyConfig.installedToolchains;
if (!Array.isArray(installedToolchains)) {
return [];
}
return installedToolchains
.filter((toolchain): toolchain is string => typeof toolchain === "string")
.map(toolchain => path.join(swiftlyHomeDir, "toolchains", toolchain));
} catch (error) {
throw new Error("Failed to retrieve Swiftly installations from disk.");
}
}
/**
* Checks common directories for available swift toolchain installations.
*
* @returns an array of toolchain paths
*/
public static async getToolchainInstalls(): Promise<string[]> {
if (process.platform !== "darwin") {
return [];
}
return Promise.all([
this.findToolchainsIn("/Library/Developer/Toolchains/"),
this.findToolchainsIn(path.join(os.homedir(), "Library/Developer/Toolchains/")),
]).then(results => results.flatMap(a => a));
}
/**
* Searches the given directory for any swift toolchain installations.
*
* @param directory the directory path to search in
* @returns an array of toolchain paths
*/
public static async findToolchainsIn(directory: string): Promise<string[]> {
try {
const toolchains = await Promise.all(
(await fs.readdir(directory, { withFileTypes: true }))
.filter(dirent => dirent.name.startsWith("swift-"))
.map(async dirent => {
const toolchainPath = path.join(dirent.path, dirent.name);
const toolchainSwiftPath = path.join(toolchainPath, "usr", "bin", "swift");
if (!(await pathExists(toolchainSwiftPath))) {
return null;
}
return toolchainPath;
})
);
return toolchains.filter(
(toolchain): toolchain is string => typeof toolchain === "string"
);
} catch {
// Assume that there are no installations here
return [];
}
}
/**
* Get a list of new project templates from swift package manager
* @returns a {@link SwiftProjectTemplate} for each discovered project type
*/
public async getProjectTemplates(): Promise<SwiftProjectTemplate[]> {
// Only swift versions >=5.8.0 are supported
if (this.swiftVersion.isLessThan(new Version(5, 8, 0))) {
return [];
}
// Parse the output from `swift package init --help`
const { stdout } = await execSwift(["package", "init", "--help"], "default");
const lines = stdout.split(/\r?\n/g);
// Determine where the `--type` option is documented
let position = lines.findIndex(line => line.trim().startsWith("--type"));
if (position === -1) {
throw new Error("Unable to parse output from `swift package init --help`");
}
// Loop through the possible project types in the output
position += 1;
const result: SwiftProjectTemplate[] = [];
const typeRegex = /^\s*([a-zA-z-]+)\s+-\s+(.+)$/;
for (; position < lines.length; position++) {
const line = lines[position];
// Stop if we hit a new command line option
if (line.trim().startsWith("--")) {
break;
}
// Check if this is the start of a new project type
const match = line.match(typeRegex);
if (match) {
const nameSegments = match[1].split("-");
result.push({
id: match[1],
name: nameSegments
.map(seg => seg[0].toLocaleUpperCase() + seg.slice(1))
.join(" "),
description: match[2],
});
} else {
// Continuation of the previous project type
result[result.length - 1].description += " " + line.trim();
}
}
return result;
}
/**
* Return fullpath for toolchain executable
*/
public getToolchainExecutable(executable: string): string {
// should we add `.exe` at the end of the executable name
const executableSuffix = process.platform === "win32" ? ".exe" : "";
return path.join(this.toolchainPath, "bin", executable + executableSuffix);
}
private static getXcodeDirectory(toolchainPath: string): string | undefined {
let xcodeDirectory = toolchainPath;
while (path.extname(xcodeDirectory) !== ".app") {
xcodeDirectory = path.dirname(xcodeDirectory);
if (path.parse(xcodeDirectory).base === "") {
return undefined;
}
}
return xcodeDirectory;
}
/**
* Returns the path to the LLDB executable inside the selected toolchain.
* If the user is on macOS and has no OSS toolchain selected, also search
* inside Xcode.
* @returns The path to the `lldb` executable
* @throws Throws an error if the executable cannot be found
*/
public async getLLDB(): Promise<string> {
return this.findToolchainOrXcodeExecutable("lldb");
}
/**
* Returns the path to the LLDB debug adapter executable inside the selected
* toolchain. If the user is on macOS and has no OSS toolchain selected, also
* search inside Xcode.
* @returns The path to the `lldb-dap` executable
* @throws Throws an error if the executable cannot be found
*/
public async getLLDBDebugAdapter(): Promise<string> {
return this.findToolchainOrXcodeExecutable("lldb-dap");
}
/**
* Search for the supplied executable in the toolchain.
* If the user is on macOS and has no OSS toolchain selected, also
* search inside Xcode.
*/
private async findToolchainOrXcodeExecutable(executable: string): Promise<string> {
const toolchainExecutablePath = path.join(
this.swiftFolderPath,
process.platform === "win32" ? `${executable}.exe` : executable
);
if (await pathExists(toolchainExecutablePath)) {
return toolchainExecutablePath;
}
if (process.platform !== "darwin") {
throw new Error(`Failed to find ${executable} in swift toolchain`);
}
return this.findXcodeExecutable(executable);
}
private async findXcodeExecutable(executable: string): Promise<string> {
const xcodeDirectory = SwiftToolchain.getXcodeDirectory(this.toolchainPath);
if (!xcodeDirectory) {
throw new Error(`Failed to find ${executable} in Swift toolchain`);
}
const { stdout } = await execFile("xcrun", ["-find", executable], {
env: { ...process.env, DEVELOPER_DIR: xcodeDirectory },
});
return stdout.trimEnd();
}
private basePlatformDeveloperPath(): string | undefined {
const sdk = this.customSDK ?? this.defaultSDK;
if (!sdk) {
return undefined;
}
return path.resolve(sdk, "../../");
}
/**
* Library path for swift-testing executables
*/
public swiftTestingLibraryPath(): string | undefined {
const base = this.basePlatformDeveloperPath();
if (!base) {
return undefined;
}
return path.join(base, "usr/lib");
}
/**
* Framework path for swift-testing executables
*/
public swiftTestingFrameworkPath(): string | undefined {
const base = this.basePlatformDeveloperPath();
if (!base) {
return undefined;
}
return path.join(base, "Library/Frameworks");
}
get diagnostics(): string {
let str = "";
str += this.swiftVersionString;
str += `\nSwift Path: ${this.swiftFolderPath}`;
str += `\nToolchain Path: ${this.toolchainPath}`;
if (this.runtimePath) {
str += `\nRuntime Library Path: ${this.runtimePath}`;
}
if (this.targetInfo.target?.triple) {
str += `\nDefault Target: ${this.targetInfo.target?.triple}`;
}
if (this.defaultSDK) {
str += `\nDefault SDK: ${this.defaultSDK}`;
}
if (this.customSDK) {
str += `\nCustom SDK: ${this.customSDK}`;
}
if (this.xcTestPath) {
str += `\nXCTest Path: ${this.xcTestPath}`;
}
return str;
}
logDiagnostics(channel: SwiftOutputChannel) {
channel.logDiagnostic(this.diagnostics);
}
private static async getSwiftFolderPath(): Promise<string> {
try {
let swift: string;
if (configuration.path !== "") {
const windowsExeSuffix = process.platform === "win32" ? ".exe" : "";
swift = path.join(configuration.path, `swift${windowsExeSuffix}`);
} else {
switch (process.platform) {
case "darwin": {
const { stdout } = await execFile("which", ["swift"]);
swift = stdout.trimEnd();
break;
}
case "win32": {
const { stdout } = await execFile("where", ["swift"]);
const paths = stdout.trimEnd().split("\r\n");
if (paths.length > 1) {
vscode.window.showWarningMessage(
`Found multiple swift executables in in %PATH%. Using excutable found at ${paths[0]}`
);
}
swift = paths[0];
break;
}
default: {
// use `type swift` to find `swift`. Run inside /bin/sh to ensure
// we get consistent output as different shells output a different
// format. Tried running with `-p` but that is not available in /bin/sh
const { stdout } = await execFile("/bin/sh", [
"-c",
"LC_MESSAGES=C type swift",
]);
const swiftMatch = /^swift is (.*)$/.exec(stdout.trimEnd());
if (swiftMatch) {
swift = swiftMatch[1];
} else {
throw Error("Failed to find swift executable");
}
break;
}
}
}
// swift may be a symbolic link
const realSwift = await fs.realpath(swift);
const swiftPath = expandFilePathTilda(path.dirname(realSwift));
return await this.getSwiftEnvPath(swiftPath);
} catch {
throw Error("Failed to find swift executable");
}
}
/**
* swiftenv is a popular way to install swift on Linux. It uses shim shell scripts
* for all of the swift executables. This is problematic when we are trying to find
* the lldb version. Also swiftenv can also change the swift version beneath which
* could cause problems. This function will return the actual path to the swift
* executable instead of the shim version
* @param swiftPath Path to swift folder
* @returns Path to swift folder installed by swiftenv
*/
private static async getSwiftEnvPath(swiftPath: string): Promise<string> {
if (process.platform === "linux" && swiftPath.endsWith(".swiftenv/shims")) {
try {
const swiftenvPath = path.dirname(swiftPath);
const swiftenv = path.join(swiftenvPath, "libexec", "swiftenv");
const { stdout } = await execFile(swiftenv, ["which", "swift"]);
const swift = stdout.trimEnd();
return path.dirname(swift);
} catch {
return swiftPath;
}
} else {
return swiftPath;
}
}
/**
* @returns path to Toolchain folder
*/
private static async getToolchainPath(swiftPath: string): Promise<string> {
try {
switch (process.platform) {
case "darwin": {
if (configuration.path !== "") {
return path.dirname(configuration.path);
}
const { stdout } = await execFile("xcrun", ["--find", "swift"], {
env: configuration.swiftEnvironmentVariables,
});
const swift = stdout.trimEnd();
return path.dirname(path.dirname(swift));
}
default: {
return path.dirname(swiftPath);
}
}
} catch {
throw Error("Failed to find swift toolchain");
}
}
/**
* @param targetInfo swift target info
* @returns path to Swift runtime
*/
private static async getRuntimePath(targetInfo: SwiftTargetInfo): Promise<string | undefined> {
if (configuration.runtimePath !== "") {
return configuration.runtimePath;
} else if (process.platform === "win32") {
const { stdout } = await execFile("where", ["swiftCore.dll"]);
const swiftCore = stdout.trimEnd();
return swiftCore.length > 0 ? path.dirname(swiftCore) : undefined;
} else {
return targetInfo.paths.runtimeLibraryPaths.length > 0
? targetInfo.paths.runtimeLibraryPaths.join(":")
: undefined;
}
}
/**
* @returns path to default SDK
*/
private static async getDefaultSDK(): Promise<string | undefined> {
switch (process.platform) {
case "darwin": {
if (process.env.SDKROOT) {
return process.env.SDKROOT;
}
return this.getSDKPath("macosx");
}
case "win32": {
return process.env.SDKROOT;
}
}
return undefined;
}
/**
* @returns path to custom SDK
*/
private static getCustomSDK(): string | undefined {
return configuration.sdk !== "" ? configuration.sdk : undefined;
}
/**
* @returns path to the swiftpm-testing-helper binary, if it exists.
*/
private static async getSwiftPMTestingHelperPath(
toolchainPath: string
): Promise<string | undefined> {
if (process.platform === "darwin") {
const toolchainSwiftPMHelperPath = path.join(
toolchainPath,
"libexec",
"swift",
"pm",
"swiftpm-testing-helper"
);
// Verify that the helper exists. Older toolchains wont have it and thats ok,
// it just means that XCTests and swift-testing tests exist in their own binaries
// and can each be run separately. If this path exists we know the tests exist in
// a unified binary and we need to use this utility to run the swift-testing tests
// on macOS. XCTests are still run with the xctest utility on macOS. The test binaries
// can be invoked directly on Linux/Windows.
if (await this.fileExists(toolchainSwiftPMHelperPath)) {
return toolchainSwiftPMHelperPath;
}
}
return undefined;
}
/**
* @param targetInfo swift target info
* @param swiftVersion parsed swift version
* @param runtimePath path to Swift runtime
* @param sdkroot path to swift SDK
* @returns path to folder where xctest can be found
*/
private static async getSwiftTestingPath(
targetInfo: SwiftTargetInfo,
swiftVersion: Version,
runtimePath: string | undefined,
sdkroot: string | undefined
): Promise<string | undefined> {
if (process.platform !== "win32") {
return undefined;
}
return this.getWindowsPlatformDLLPath(
"Testing",
targetInfo,
swiftVersion,
runtimePath,
sdkroot
);
}
/**
* @param targetInfo swift target info
* @param swiftVersion parsed swift version
* @param runtimePath path to Swift runtime
* @param sdkroot path to swift SDK
* @returns path to folder where xctest can be found
*/
private static async getXCTestPath(
targetInfo: SwiftTargetInfo,
swiftFolderPath: string,
swiftVersion: Version,
runtimePath: string | undefined,
sdkroot: string | undefined
): Promise<string | undefined> {
switch (process.platform) {
case "darwin": {
const xcodeDirectory = this.getXcodeDirectory(swiftFolderPath);
const swiftEnvironmentVariables = configuration.swiftEnvironmentVariables;
if (xcodeDirectory && !("DEVELOPER_DIR" in swiftEnvironmentVariables)) {
swiftEnvironmentVariables["DEVELOPER_DIR"] = xcodeDirectory;
}
const developerDir = await this.getXcodeDeveloperDir(swiftEnvironmentVariables);
return path.join(developerDir, "usr", "bin");
}
case "win32": {
return await this.getWindowsPlatformDLLPath(
"XCTest",
targetInfo,
swiftVersion,
runtimePath,
sdkroot
);
}
}
return undefined;
}
private static async getWindowsPlatformDLLPath(
type: "XCTest" | "Testing",
targetInfo: SwiftTargetInfo,
swiftVersion: Version,
runtimePath: string | undefined,
sdkroot: string | undefined
): Promise<string | undefined> {
// look up runtime library directory for XCTest/Testing alternatively
const fallbackPath =
runtimePath !== undefined && (await pathExists(path.join(runtimePath, `${type}.dll`)))
? runtimePath
: undefined;
if (!sdkroot) {
return fallbackPath;
}
const platformPath = path.dirname(path.dirname(path.dirname(sdkroot)));
const platformManifest = path.join(platformPath, "Info.plist");
if ((await pathExists(platformManifest)) !== true) {
if (fallbackPath) {
return fallbackPath;
}
vscode.window.showWarningMessage(
`${type} not found due to non-standardized library layout. Tests explorer won't work as expected.`
);
return undefined;
}
const data = await fs.readFile(platformManifest, "utf8");
let infoPlist;
try {
infoPlist = plist.parse(data) as unknown as InfoPlist;
} catch (error) {
vscode.window.showWarningMessage(`Unable to parse ${platformManifest}: ${error}`);
return undefined;
}
const plistKey = type === "XCTest" ? "XCTEST_VERSION" : "SWIFT_TESTING_VERSION";
const version = infoPlist.DefaultProperties[plistKey];
if (!version) {
new SwiftOutputChannel("swift", true).appendLine(
`Warning: ${platformManifest} is missing the ${plistKey} key.`
);
return undefined;
}
if (swiftVersion.isGreaterThanOrEqual(new Version(5, 7, 0))) {
let bindir: string;
const arch = targetInfo.target?.triple.split("-", 1)[0];
switch (arch) {
case "x86_64":
bindir = "bin64";
break;
case "i686":
bindir = "bin32";
break;
case "aarch64":
bindir = "bin64a";
break;
default:
throw Error(`unsupported architecture ${arch}`);
}
return path.join(
platformPath,
"Developer",
"Library",
`${type}-${version}`,
"usr",
bindir
);
} else {
return path.join(
platformPath,
"Developer",
"Library",
`${type}-${version}`,
"usr",
"bin"
);
}
}
/** @returns swift target info */
private static async getSwiftTargetInfo(): Promise<SwiftTargetInfo> {
try {
try {
const { stdout } = await execSwift(["-print-target-info"], "default");
const targetInfo = JSON.parse(stdout.trimEnd()) as SwiftTargetInfo;
if (targetInfo.compilerVersion) {
return targetInfo;
}
} catch {
// hit error while running `swift -print-target-info`. We are possibly running
// a version of swift 5.3 or older
}
const { stdout } = await execSwift(["--version"], "default");
return {
compilerVersion: stdout.split("\n", 1)[0],
paths: { runtimeLibraryPaths: [""] },
};
} catch {
throw Error(
"Failed to get swift version from either '-print-target-info' or '--version'."
);
}
}
/**
* @param targetInfo swift target info
* @returns swift version object
*/
private static getSwiftVersion(targetInfo: SwiftTargetInfo): Version {
const match = targetInfo.compilerVersion.match(/Swift version ([\S]+)/);
let version: Version | undefined;
if (match) {
version = Version.fromString(match[1]);
}
return version ?? new Version(0, 0, 0);
}
/**
* Check if a file exists.
* @returns true if the file exists at the supplied path
*/
private static async fileExists(path: string): Promise<boolean> {
try {
await fs.access(path, fs.constants.F_OK);
return true;
} catch {
return false;
}
}
}