Skip to content

Commit f0fd5a6

Browse files
committed
WIP
1 parent a1f589f commit f0fd5a6

File tree

8 files changed

+75
-50
lines changed

8 files changed

+75
-50
lines changed

package.json

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,17 @@
184184
}
185185
}
186186
},
187+
{
188+
"title": "Cross compilation (Experimental)",
189+
"properties": {
190+
"destination.SDK": {
191+
"type": "string",
192+
"default": "",
193+
"description": "The path of the SDK to compile against. The default SDK is determined by the environment on macOS and Windows.",
194+
"order": 1
195+
}
196+
}
197+
},
187198
{
188199
"title": "Advanced",
189200
"properties": {
@@ -193,12 +204,6 @@
193204
"description": "The path of the folder containing the Swift runtime libraries. Only effective when they can't be discovered by RPath.",
194205
"order": 1
195206
},
196-
"swift.SDK": {
197-
"type": "string",
198-
"default": "",
199-
"description": "The path of the SDK to compile against. The default SDK is determined by the environment on macOS and Windows.",
200-
"order": 2
201-
},
202207
"swift.diagnostics": {
203208
"type": "boolean",
204209
"default": false,

src/SwiftTaskProvider.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ import { WorkspaceContext } from "./WorkspaceContext";
1717
import { FolderContext } from "./FolderContext";
1818
import { Product } from "./SwiftPackage";
1919
import configuration from "./configuration";
20-
import { getSwiftExecutable, swiftRuntimeEnv, withSwiftSDKFlags } from "./utilities/utilities";
20+
import {
21+
getSwiftExecutable,
22+
swiftRuntimeEnv,
23+
withSwiftDestinationFlags,
24+
} from "./utilities/utilities";
2125
import { Version } from "./utilities/version";
2226

2327
/**
@@ -180,7 +184,7 @@ function createBuildTasks(product: Product, folderContext: FolderContext): vscod
180184
*/
181185
export function createSwiftTask(args: string[], name: string, config?: TaskConfig): vscode.Task {
182186
const swift = getSwiftExecutable();
183-
args = withSwiftSDKFlags(args);
187+
args = withSwiftDestinationFlags(args);
184188
const task = new vscode.Task(
185189
{ type: "swift", command: swift, args: args, cwd: config?.cwd?.fsPath },
186190
config?.scope ?? vscode.TaskScope.Workspace,

src/WorkspaceContext.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,6 @@ export class WorkspaceContext implements vscode.Disposable {
6666
}
6767
});
6868
}
69-
// on sdk config change, restart sourcekit-lsp
70-
if (event.affectsConfiguration("swift.SDK")) {
71-
// FIXME: There is a bug stopping us from restarting SourceKit-LSP directly.
72-
// As long as it's fixed we won't need to reload on newer versions.
73-
vscode.window
74-
.showInformationMessage(
75-
"Changing the Swift SDK path requires the project be reloaded.",
76-
"Ok"
77-
)
78-
.then(selected => {
79-
if (selected === "Ok") {
80-
vscode.commands.executeCommand("workbench.action.reloadWindow");
81-
}
82-
});
83-
}
8469
// on runtime path config change, regenerate launch.json
8570
if (event.affectsConfiguration("swift.runtimePath")) {
8671
if (!configuration.autoGenerateLaunchConfigurations) {
@@ -100,6 +85,21 @@ export class WorkspaceContext implements vscode.Disposable {
10085
}
10186
});
10287
}
88+
// on destination config change, restart sourcekit-lsp
89+
if (event.affectsConfiguration("destination.SDK")) {
90+
// FIXME: There is a bug stopping us from restarting SourceKit-LSP directly.
91+
// As long as it's fixed we won't need to reload on newer versions.
92+
vscode.window
93+
.showInformationMessage(
94+
"Changing the destination SDK path requires the project be reloaded.",
95+
"Ok"
96+
)
97+
.then(selected => {
98+
if (selected === "Ok") {
99+
vscode.commands.executeCommand("workbench.action.reloadWindow");
100+
}
101+
});
102+
}
103103
});
104104
const backgroundCompilationOnDidSave = BackgroundCompilation.start(this);
105105
this.subscriptions = [

src/configuration.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ export interface LSPConfiguration {
2424
readonly inlayHintsEnabled: boolean;
2525
}
2626

27+
/** destination configuration */
28+
export interface DestinationConfiguration {
29+
/** Path to destination sdk */
30+
readonly sdk: string;
31+
}
32+
2733
/**
2834
* Type-safe wrapper around configuration settings.
2935
*/
@@ -48,6 +54,14 @@ const configuration = {
4854
},
4955
};
5056
},
57+
/** destination configuration */
58+
get destination(): DestinationConfiguration {
59+
return {
60+
get sdk(): string {
61+
return vscode.workspace.getConfiguration("destination").get<string>("SDK", "");
62+
},
63+
};
64+
},
5165

5266
/** Files and directories to exclude from the Package Dependencies view. */
5367
get excludePathsFromPackageDependencies(): string[] {
@@ -69,10 +83,6 @@ const configuration = {
6983
get runtimePath(): string {
7084
return vscode.workspace.getConfiguration("swift").get<string>("runtimePath", "");
7185
},
72-
/** Path to custom swift sdk */
73-
get sdk(): string {
74-
return vscode.workspace.getConfiguration("swift").get<string>("SDK", "");
75-
},
7686
/** swift build arguments */
7787
get buildArguments(): string[] {
7888
return vscode.workspace.getConfiguration("swift").get<string[]>("buildArguments", []);

src/debugger/launch.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,10 @@ export function createTestConfiguration(
183183
if (xcTestPath !== runtimePath) {
184184
testEnv.Path = `${xcTestPath};${testEnv.Path ?? process.env.Path}`;
185185
}
186-
const sdkroot = configuration.sdk === "" ? process.env.SDKROOT : configuration.sdk;
186+
const sdkroot =
187+
configuration.destination.sdk === ""
188+
? process.env.SDKROOT
189+
: configuration.destination.sdk;
187190
if (sdkroot === undefined) {
188191
return null;
189192
}

src/sourcekit-lsp/LanguageClientManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import configuration from "../configuration";
1818
import {
1919
getSwiftExecutable,
2020
isPathInsidePath,
21-
swiftDriverSDKFlags,
21+
swiftDriverDestinationFlags,
2222
swiftRuntimeEnv,
2323
} from "../utilities/utilities";
2424
import { Version } from "../utilities/version";
@@ -243,7 +243,7 @@ export class LanguageClientManager {
243243
const serverPathConfig = lspConfig.serverPath;
244244
const serverPath =
245245
serverPathConfig.length > 0 ? serverPathConfig : getSwiftExecutable("sourcekit-lsp");
246-
const sdkArguments = swiftDriverSDKFlags(true);
246+
const sdkArguments = swiftDriverDestinationFlags(true);
247247
const sourcekit: langclient.Executable = {
248248
command: serverPath,
249249
args: lspConfig.serverArguments.concat(sdkArguments),

src/toolchain/toolchain.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ export class SwiftToolchain {
178178
* @returns path to custom SDK
179179
*/
180180
private static getCustomSDK(): string | undefined {
181-
return configuration.sdk !== "" ? configuration.sdk : undefined;
181+
const destination = configuration.destination;
182+
return destination.sdk !== "" ? destination.sdk : undefined;
182183
}
183184

184185
/**

src/utilities/utilities.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -137,67 +137,69 @@ export async function execFileStreamOutput(
137137
*
138138
* @param args array of arguments to pass to swift executable
139139
* @param options execution options
140-
* @param setSDKFlags whether to set SDK flags
140+
* @param setDestinationFlags whether to set destination flags
141141
*/
142142
export async function execSwift(
143143
args: string[],
144144
options: cp.ExecFileOptions = {},
145-
setSDKFlags = false,
145+
setDestinationFlags = false,
146146
folderContext?: FolderContext
147147
): Promise<{ stdout: string; stderr: string }> {
148148
const swift = getSwiftExecutable();
149-
if (setSDKFlags) {
150-
args = withSwiftSDKFlags(args);
149+
if (setDestinationFlags) {
150+
args = withSwiftDestinationFlags(args);
151151
}
152152
return await execFile(swift, args, options, folderContext);
153153
}
154154

155155
/**
156-
* Get modified swift arguments with SDK flags.
156+
* Get modified swift arguments with config-based destination flags.
157157
*
158158
* @param args original commandline arguments
159159
*/
160-
export function withSwiftSDKFlags(args: string[]): string[] {
160+
export function withSwiftDestinationFlags(args: string[]): string[] {
161161
switch (args.length > 0 ? args[0] : null) {
162162
case "package": {
163-
// swift-package requires SDK flags to be placed before subcommand options
163+
// swift-package requires destination flags to be placed before arguments
164164
// eg. ["package", "describe", "--type", "json"] should be turned into
165165
// ["package", "describe", "--sdk", "/path/to/sdk", "--type", "json"]
166166
if (args.length <= 2) {
167-
return args.concat(swiftpmSDKFlags());
167+
return args.concat(swiftpmDestinationFlags());
168168
}
169169
const subcommand = args.splice(0, 2);
170-
return [...subcommand, ...swiftpmSDKFlags(), ...args];
170+
return [...subcommand, ...swiftpmDestinationFlags(), ...args];
171171
}
172172
case "build":
173173
case "run":
174174
case "test":
175-
return args.concat(swiftpmSDKFlags());
175+
return args.concat(swiftpmDestinationFlags());
176176
default:
177-
return args.concat(swiftDriverSDKFlags());
177+
return args.concat(swiftDriverDestinationFlags());
178178
}
179179
}
180180

181181
/**
182-
* Get SDK flags for SwiftPM
182+
* Get destination flags for SwiftPM
183183
*/
184-
export function swiftpmSDKFlags(): string[] {
185-
if (configuration.sdk !== "") {
186-
return ["--sdk", configuration.sdk];
184+
export function swiftpmDestinationFlags(): string[] {
185+
const destination = configuration.destination;
186+
if (destination.sdk !== "") {
187+
return ["--sdk", destination.sdk];
187188
}
188189
return [];
189190
}
190191

191192
/**
192-
* Get SDK flags for swiftc
193+
* Get destination flags for swiftc
193194
*
194195
* @param indirect whether to pass the flags by -Xswiftc
195196
*/
196-
export function swiftDriverSDKFlags(indirect = false): string[] {
197-
if (configuration.sdk === "") {
197+
export function swiftDriverDestinationFlags(indirect = false): string[] {
198+
const destination = configuration.destination;
199+
if (destination.sdk === "") {
198200
return [];
199201
}
200-
const args = ["-sdk", configuration.sdk];
202+
const args = ["-sdk", destination.sdk];
201203
return indirect ? args.flatMap(arg => ["-Xswiftc", arg]) : args;
202204
}
203205

0 commit comments

Comments
 (0)