Skip to content

Commit cbff0d0

Browse files
authored
Support Swift SDKs (#1191)
1 parent 5d8f620 commit cbff0d0

File tree

7 files changed

+70
-5
lines changed

7 files changed

+70
-5
lines changed

Diff for: package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -674,9 +674,15 @@
674674
"swift.SDK": {
675675
"type": "string",
676676
"default": "",
677-
"markdownDescription": "The path of the SDK to compile against (`--sdk` parameter). This is of use when supporting non-standard SDK layouts on Windows and using custom SDKs. The default SDK is determined by the environment on macOS and Windows.",
677+
"markdownDescription": "The path of the SDK to compile against (`--sdk` parameter). This is of use when supporting non-standard SDK layouts on Windows and using custom SDKs. The default SDK is determined by the environment on macOS and Windows.\n\nFor SwiftPM projects, prefer using `swift.swiftSDK` with a triple (such as `arm64-apple-ios`) or Swift SDK name instead.",
678678
"order": 3
679679
},
680+
"swift.swiftSDK": {
681+
"type": "string",
682+
"default": "",
683+
"markdownDescription": "The [Swift SDK](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0387-cross-compilation-destinations.md) to compile against (`--swift-sdk` parameter).",
684+
"order": 4
685+
},
680686
"swift.diagnostics": {
681687
"type": "boolean",
682688
"default": false,

Diff for: src/configuration.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,20 @@ const configuration = {
221221
get runtimePath(): string {
222222
return vscode.workspace.getConfiguration("swift").get<string>("runtimePath", "");
223223
},
224-
/** Path to custom swift sdk */
224+
/** Path to custom --sdk */
225225
get sdk(): string {
226226
return vscode.workspace.getConfiguration("swift").get<string>("SDK", "");
227227
},
228228
set sdk(value: string | undefined) {
229229
vscode.workspace.getConfiguration("swift").update("SDK", value);
230230
},
231+
/** Path to custom --swift-sdk */
232+
get swiftSDK(): string {
233+
return vscode.workspace.getConfiguration("swift").get<string>("swiftSDK", "");
234+
},
235+
set swiftSDK(value: string | undefined) {
236+
vscode.workspace.getConfiguration("swift").update("swiftSDK", value);
237+
},
231238
/** swift build arguments */
232239
get buildArguments(): string[] {
233240
return vscode.workspace.getConfiguration("swift").get<string[]>("buildArguments", []);

Diff for: src/extension.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ export async function activate(context: vscode.ExtensionContext): Promise<Api> {
9999
);
100100
}
101101
// on sdk config change, restart sourcekit-lsp
102-
if (event.affectsConfiguration("swift.SDK")) {
102+
if (
103+
event.affectsConfiguration("swift.SDK") ||
104+
event.affectsConfiguration("swift.swiftSDK")
105+
) {
103106
// FIXME: There is a bug stopping us from restarting SourceKit-LSP directly.
104107
// As long as it's fixed we won't need to reload on newer versions.
105108
showReloadExtensionNotification(

Diff for: src/sourcekit-lsp/LanguageClientManager.ts

+8
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,14 @@ export class LanguageClientManager implements vscode.Disposable {
651651
backgroundPreparationMode: "enabled",
652652
};
653653
}
654+
655+
if (configuration.swiftSDK !== "") {
656+
options = {
657+
...options,
658+
swiftPM: { swiftSDK: configuration.swiftSDK },
659+
};
660+
}
661+
654662
return options;
655663
}
656664

Diff for: src/toolchain/BuildFlags.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,14 @@ export class BuildFlags {
7373
* Get SDK flags for SwiftPM
7474
*/
7575
swiftpmSDKFlags(): string[] {
76+
const flags: string[] = [];
7677
if (configuration.sdk !== "") {
77-
return ["--sdk", configuration.sdk, ...this.swiftDriverTargetFlags(true)];
78+
flags.push("--sdk", configuration.sdk, ...this.swiftDriverTargetFlags(true));
7879
}
79-
return [];
80+
if (configuration.swiftSDK !== "") {
81+
flags.push("--swift-sdk", configuration.swiftSDK);
82+
}
83+
return flags;
8084
}
8185

8286
/**

Diff for: test/unit-tests/sourcekit-lsp/LanguageClientManager.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,25 @@ suite("LanguageClientManager Suite", () => {
187187
expect(languageClientMock.start).to.have.been.calledOnce;
188188
});
189189

190+
test("launches SourceKit-LSP on startup with swiftSDK", async () => {
191+
mockedConfig.swiftSDK = "arm64-apple-ios";
192+
193+
const sut = new LanguageClientManager(instance(mockedWorkspace), languageClientFactoryMock);
194+
await waitForReturnedPromises(languageClientMock.start);
195+
196+
expect(sut.state).to.equal(State.Running);
197+
expect(languageClientFactoryMock.createLanguageClient).to.have.been.calledOnceWith(
198+
/* id */ match.string,
199+
/* name */ match.string,
200+
/* serverOptions */ match.has("command", "/path/to/toolchain/bin/sourcekit-lsp"),
201+
/* clientOptions */ match.hasNested(
202+
"initializationOptions.swiftPM.swiftSDK",
203+
"arm64-apple-ios"
204+
)
205+
);
206+
expect(languageClientMock.start).to.have.been.calledOnce;
207+
});
208+
190209
test("chooses the correct backgroundIndexing value is auto, swift version if 6.0.0", async () => {
191210
mockedWorkspace.swiftVersion = new Version(6, 0, 0);
192211
mockedConfig.backgroundIndexing = "auto";

Diff for: test/unit-tests/toolchain/BuildFlags.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,11 @@ suite("BuildFlags Test Suite", () => {
7878

7979
suite("swiftpmSDKFlags", () => {
8080
const sdkConfig = mockGlobalValue(configuration, "sdk");
81+
const swiftSDKConfig = mockGlobalValue(configuration, "swiftSDK");
8182

8283
test("no configuration provided", async () => {
8384
sdkConfig.setValue("");
85+
swiftSDKConfig.setValue("");
8486
expect(buildFlags.swiftpmSDKFlags()).to.be.an("array").that.is.empty;
8587
});
8688

@@ -92,6 +94,22 @@ suite("BuildFlags Test Suite", () => {
9294
]);
9395
});
9496

97+
test("configuration provided for swiftSDK", () => {
98+
swiftSDKConfig.setValue("arm64-apple-ios");
99+
expect(buildFlags.swiftpmSDKFlags()).to.deep.equal(["--swift-sdk", "arm64-apple-ios"]);
100+
});
101+
102+
test("configuration provided for swiftSDK and sdk", () => {
103+
sdkConfig.setValue("/some/other/full/test/path");
104+
swiftSDKConfig.setValue("arm64-apple-ios");
105+
expect(buildFlags.swiftpmSDKFlags()).to.deep.equal([
106+
"--sdk",
107+
"/some/other/full/test/path",
108+
"--swift-sdk",
109+
"arm64-apple-ios",
110+
]);
111+
});
112+
95113
test("include target", () => {
96114
sdkConfig.setValue("/some/other/full/test/path/WatchOS.sdk");
97115
expect(buildFlags.swiftpmSDKFlags()).to.deep.equal([

0 commit comments

Comments
 (0)