Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Swift SDKs #1191

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,15 @@
"swift.SDK": {
"type": "string",
"default": "",
"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.",
"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.",
"order": 3
},
"swift.swiftSDK": {
"type": "string",
"default": "",
"markdownDescription": "The [Swift SDK](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0387-cross-compilation-destinations.md) to compile against (`--swift-sdk` parameter).",
"order": 4
},
"swift.diagnostics": {
"type": "boolean",
"default": false,
Expand Down
9 changes: 8 additions & 1 deletion src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,20 @@ const configuration = {
get runtimePath(): string {
return vscode.workspace.getConfiguration("swift").get<string>("runtimePath", "");
},
/** Path to custom swift sdk */
/** Path to custom --sdk */
get sdk(): string {
return vscode.workspace.getConfiguration("swift").get<string>("SDK", "");
},
set sdk(value: string | undefined) {
vscode.workspace.getConfiguration("swift").update("SDK", value);
},
/** Path to custom --swift-sdk */
get swiftSDK(): string {
return vscode.workspace.getConfiguration("swift").get<string>("swiftSDK", "");
},
set swiftSDK(value: string | undefined) {
vscode.workspace.getConfiguration("swift").update("swiftSDK", value);
},
/** swift build arguments */
get buildArguments(): string[] {
return vscode.workspace.getConfiguration("swift").get<string[]>("buildArguments", []);
Expand Down
5 changes: 4 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ export async function activate(context: vscode.ExtensionContext): Promise<Api> {
);
}
// on sdk config change, restart sourcekit-lsp
if (event.affectsConfiguration("swift.SDK")) {
if (
event.affectsConfiguration("swift.SDK") ||
event.affectsConfiguration("swift.swiftSDK")
) {
// FIXME: There is a bug stopping us from restarting SourceKit-LSP directly.
// As long as it's fixed we won't need to reload on newer versions.
showReloadExtensionNotification(
Expand Down
8 changes: 8 additions & 0 deletions src/sourcekit-lsp/LanguageClientManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,14 @@ export class LanguageClientManager implements vscode.Disposable {
backgroundPreparationMode: "enabled",
};
}

if (configuration.swiftSDK !== "") {
options = {
...options,
swiftPM: { swiftSDK: configuration.swiftSDK },
};
}

return options;
}

Expand Down
8 changes: 6 additions & 2 deletions src/toolchain/BuildFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,14 @@ export class BuildFlags {
* Get SDK flags for SwiftPM
*/
swiftpmSDKFlags(): string[] {
const flags: string[] = [];
if (configuration.sdk !== "") {
return ["--sdk", configuration.sdk, ...this.swiftDriverTargetFlags(true)];
flags.push("--sdk", configuration.sdk, ...this.swiftDriverTargetFlags(true));
}
return [];
if (configuration.swiftSDK !== "") {
flags.push("--swift-sdk", configuration.swiftSDK);
}
return flags;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions test/unit-tests/sourcekit-lsp/LanguageClientManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,25 @@ suite("LanguageClientManager Suite", () => {
expect(languageClientMock.start).to.have.been.calledOnce;
});

test("launches SourceKit-LSP on startup with swiftSDK", async () => {
mockedConfig.swiftSDK = "arm64-apple-ios";

const sut = new LanguageClientManager(instance(mockedWorkspace), languageClientFactoryMock);
await waitForReturnedPromises(languageClientMock.start);

expect(sut.state).to.equal(State.Running);
expect(languageClientFactoryMock.createLanguageClient).to.have.been.calledOnceWith(
/* id */ match.string,
/* name */ match.string,
/* serverOptions */ match.has("command", "/path/to/toolchain/bin/sourcekit-lsp"),
/* clientOptions */ match.hasNested(
"initializationOptions.swiftPM.swiftSDK",
"arm64-apple-ios"
)
);
expect(languageClientMock.start).to.have.been.calledOnce;
});

test("chooses the correct backgroundIndexing value is auto, swift version if 6.0.0", async () => {
mockedWorkspace.swiftVersion = new Version(6, 0, 0);
mockedConfig.backgroundIndexing = "auto";
Expand Down
18 changes: 18 additions & 0 deletions test/unit-tests/toolchain/BuildFlags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ suite("BuildFlags Test Suite", () => {

suite("swiftpmSDKFlags", () => {
const sdkConfig = mockGlobalValue(configuration, "sdk");
const swiftSDKConfig = mockGlobalValue(configuration, "swiftSDK");

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

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

test("configuration provided for swiftSDK", () => {
swiftSDKConfig.setValue("arm64-apple-ios");
expect(buildFlags.swiftpmSDKFlags()).to.deep.equal(["--swift-sdk", "arm64-apple-ios"]);
});

test("configuration provided for swiftSDK and sdk", () => {
sdkConfig.setValue("/some/other/full/test/path");
swiftSDKConfig.setValue("arm64-apple-ios");
expect(buildFlags.swiftpmSDKFlags()).to.deep.equal([
"--sdk",
"/some/other/full/test/path",
"--swift-sdk",
"arm64-apple-ios",
]);
});

test("include target", () => {
sdkConfig.setValue("/some/other/full/test/path/WatchOS.sdk");
expect(buildFlags.swiftpmSDKFlags()).to.deep.equal([
Expand Down
Loading