From 3bbfd7acefedf509520ee0e65e8afac0b7bdee24 Mon Sep 17 00:00:00 2001 From: stevapple Date: Tue, 2 Aug 2022 03:46:41 +0800 Subject: [PATCH 1/3] Add destination configuration --- docs/settings.md | 17 +++++++++++++--- package.json | 47 ++++++++++++++++++++++++++++++++++++++++---- src/configuration.ts | 37 +++++++++++++++++++++++++++++++++- 3 files changed, 93 insertions(+), 8 deletions(-) diff --git a/docs/settings.md b/docs/settings.md index 8c3394fb4..8a45f5acf 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -58,11 +58,22 @@ This is a list of environment variables to set when running swift (build, resolv - **Runtime Path** -Where to find Swift runtime libraries. This is mainly of use when these libraries cannot be discovered via the RPATH. On Windows the runtime path is added to the `Path` environment variable. This is of less use on macOS and Linux but will be added to `DYLD_LIBRARY_PATH` and `LD_LIBRARY_PATH` environment variables respectively on each platform. This is of use when supporting non-standard SDK layouts on Windows +Where to find Swift runtime libraries. This is mainly of use when these libraries cannot be discovered via the RPATH. On Windows the runtime path is added to the `Path` environment variable. This is of less use on macOS and Linux but will be added to `DYLD_LIBRARY_PATH` and `LD_LIBRARY_PATH` environment variables respectively on each platform. -- **SDK** +- **Destination** -The path of the target SDK to compile against. The default SDK is determined by the environment on macOS and Windows. This is of use when supporting non-standard SDK layouts on Windows and using custom SDKs. This adds the `--sdk` command line parameter to the relevant `swift` calls. +The build destination for SwiftPM projects. If the value is a string, the plugin will load the `destination.json` file at the given path. If the value is an object, the plugin will map the value as follows and generate the `destination.json` automatically. + +| Configuration key | Description | `destination.json` key | Default value | +|---|---|---|---| +| `target`| The target triple of build destination. | `target` | `null` | +| `sdk` | The path of the SDK to build against. | `sdk` | `null` | +| `binDir` | The path of the folder containing tool binaries. | `toolchain-bin-dir` | The `usr/bin` subdirectory of the Swift toolchain. | +| `extraSwiftCFlags` | Additional arguments to pass to the Swift compiler. | `extra-swiftc-flags` | `[]` | +| `extraCCFlags` | Additional arguments to pass to the C compiler. | `extra-cc-flags` | `[]` | +| `extraCPPFlags` | Additional arguments to pass to the C++ compiler. | `extra-cpp-flags` | `[]` | + +Hot reload is available for either way of specifying build destination. - **Diagnostics** diff --git a/package.json b/package.json index e523706ec..ac6d482c0 100644 --- a/package.json +++ b/package.json @@ -216,10 +216,49 @@ "description": "The path of the folder containing the Swift runtime libraries. Only effective when they can't be discovered by RPath.", "order": 2 }, - "swift.SDK": { - "type": "string", + "swift.destination": { + "type": [ + "object", + "string" + ], + "properties": { + "target": { + "type": "string", + "default": "", + "description": "The target triple of build destination." + }, + "sdk": { + "type": "string", + "default": "", + "description": "The path of the SDK to build against." + }, + "binDir": { + "type": "string", + "default": "", + "description": "The path of the folder containing tool binaries. The default is to look in current toolchain." + }, + "extraSwiftCFlags": { + "type": "array", + "items": "string", + "default": [], + "description": "Additional arguments to pass to the Swift compiler." + }, + "extraCCFlags": { + "type": "array", + "items": "string", + "default": [], + "description": "Additional arguments to pass to the C compiler." + }, + "extraCPPFlags": { + "type": "array", + "items": "string", + "default": [], + "description": "Additional arguments to pass to the C++ compiler." + } + }, + "additionalProperties": false, "default": "", - "description": "The path of the SDK to compile against (`--sdk` parameter). The default SDK is determined by the environment on macOS and Windows.", + "markdownDescription": "Destination configuration object or path to the `destination.json` file.", "order": 3 }, "swift.diagnostics": { @@ -433,4 +472,4 @@ "plist": "^3.0.5", "vscode-languageclient": "^8.0.0" } -} +} \ No newline at end of file diff --git a/src/configuration.ts b/src/configuration.ts index 050ca135b..a7b9bccd1 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -24,6 +24,22 @@ export interface LSPConfiguration { readonly inlayHintsEnabled: boolean; } +/** build destination configuration */ +export interface DestinationConfiguration { + /** Target triple */ + target: string; + /** Path to destination SDK */ + sdk: string; + /** Path to tools directory */ + binDir: string; + /** Extra arguments to pass to Swift compiler */ + extraSwiftCFlags: string[]; + /** Extra arguments to pass to C compiler */ + extraCCFlags: string[]; + /** Extra arguments to pass to C++ compiler */ + extraCPPFlags: string[]; +} + /** * Type-safe wrapper around configuration settings. */ @@ -49,6 +65,24 @@ const configuration = { }; }, + /** build destination configuration */ + get destination(): DestinationConfiguration | string { + const configuration = vscode.workspace + .getConfiguration("swift") + .get("destination", ""); + if (typeof configuration !== "string") { + return { + target: configuration.target ?? "", + sdk: configuration.sdk ?? "", + binDir: configuration.binDir ?? "", + extraSwiftCFlags: configuration.extraSwiftCFlags ?? [], + extraCCFlags: configuration.extraCCFlags ?? [], + extraCPPFlags: configuration.extraCPPFlags ?? [], + }; + } + return configuration; + }, + /** Files and directories to exclude from the Package Dependencies view. */ get excludePathsFromPackageDependencies(): string[] { return vscode.workspace @@ -71,7 +105,8 @@ const configuration = { }, /** Path to custom swift sdk */ get sdk(): string { - return vscode.workspace.getConfiguration("swift").get("SDK", ""); + // FIXME(stevapple): remove the entry + return typeof this.destination === "object" ? this.destination.sdk : ""; }, /** swift build arguments */ get buildArguments(): string[] { From f1414a39fd50ba1d45dd61730be3c8ab74cd67ec Mon Sep 17 00:00:00 2001 From: stevapple Date: Wed, 10 Aug 2022 04:17:10 +0800 Subject: [PATCH 2/3] First workable checkpoint --- src/SwiftPluginTaskProvider.ts | 6 +- src/SwiftTaskProvider.ts | 9 ++- src/WorkspaceContext.ts | 4 +- src/configuration.ts | 27 ++------ src/extension.ts | 2 +- src/toolchain/destination.ts | 121 +++++++++++++++++++++++++++++++++ src/toolchain/toolchain.ts | 63 +++++++++++------ src/utilities/utilities.ts | 20 ++---- 8 files changed, 188 insertions(+), 64 deletions(-) create mode 100644 src/toolchain/destination.ts diff --git a/src/SwiftPluginTaskProvider.ts b/src/SwiftPluginTaskProvider.ts index 37cd5b52a..5f33922e1 100644 --- a/src/SwiftPluginTaskProvider.ts +++ b/src/SwiftPluginTaskProvider.ts @@ -17,7 +17,7 @@ import * as path from "path"; import { WorkspaceContext } from "./WorkspaceContext"; import { PackagePlugin } from "./SwiftPackage"; import configuration from "./configuration"; -import { getSwiftExecutable, swiftRuntimeEnv, withSwiftSDKFlags } from "./utilities/utilities"; +import { getSwiftExecutable, swiftRuntimeEnv, withSwiftFlags } from "./utilities/utilities"; // Interface class for defining task configuration interface TaskConfig { @@ -80,7 +80,7 @@ export class SwiftPluginTaskProvider implements vscode.TaskProvider { task.definition.command, ...task.definition.args, ]; - swiftArgs = withSwiftSDKFlags(swiftArgs); + swiftArgs = withSwiftFlags(swiftArgs, undefined); const newTask = new vscode.Task( task.definition, @@ -108,7 +108,7 @@ export class SwiftPluginTaskProvider implements vscode.TaskProvider { createSwiftPluginTask(plugin: PackagePlugin, args: string[], config: TaskConfig): vscode.Task { const swift = getSwiftExecutable(); let swiftArgs = ["package", plugin.command, ...args]; - swiftArgs = withSwiftSDKFlags(swiftArgs); + swiftArgs = withSwiftFlags(swiftArgs, undefined); // Add relative path current working directory const relativeCwd = path.relative(config.scope.uri.fsPath, config.cwd?.fsPath); diff --git a/src/SwiftTaskProvider.ts b/src/SwiftTaskProvider.ts index 4f90d35c2..d1b01cc8a 100644 --- a/src/SwiftTaskProvider.ts +++ b/src/SwiftTaskProvider.ts @@ -18,8 +18,9 @@ import { WorkspaceContext } from "./WorkspaceContext"; import { FolderContext } from "./FolderContext"; import { Product } from "./SwiftPackage"; import configuration from "./configuration"; -import { getSwiftExecutable, swiftRuntimeEnv, withSwiftSDKFlags } from "./utilities/utilities"; +import { getSwiftExecutable, swiftRuntimeEnv, withSwiftFlags } from "./utilities/utilities"; import { Version } from "./utilities/version"; +import { Destination } from "./toolchain/destination"; /** * References: @@ -40,6 +41,7 @@ interface TaskConfig { problemMatcher?: string | string[]; presentationOptions?: vscode.TaskPresentationOptions; prefix?: string; + destination?: Destination; } /** flag for enabling test discovery */ @@ -92,6 +94,7 @@ export function createBuildAllTask(folderContext: FolderContext): vscode.Task { reveal: vscode.TaskRevealKind.Silent, }, problemMatcher: configuration.problemMatchCompileErrors ? "$swiftc" : undefined, + destination: folderContext.workspaceContext.toolchain.destination, } ); } @@ -160,6 +163,7 @@ function createBuildTasks(product: Product, folderContext: FolderContext): vscod reveal: vscode.TaskRevealKind.Silent, }, problemMatcher: configuration.problemMatchCompileErrors ? "$swiftc" : undefined, + destination: folderContext.workspaceContext.toolchain.destination, } ), createSwiftTask( @@ -173,6 +177,7 @@ function createBuildTasks(product: Product, folderContext: FolderContext): vscod reveal: vscode.TaskRevealKind.Silent, }, problemMatcher: configuration.problemMatchCompileErrors ? "$swiftc" : undefined, + destination: folderContext.workspaceContext.toolchain.destination, } ), ]; @@ -183,7 +188,7 @@ function createBuildTasks(product: Product, folderContext: FolderContext): vscod */ export function createSwiftTask(args: string[], name: string, config: TaskConfig): vscode.Task { const swift = getSwiftExecutable(); - args = withSwiftSDKFlags(args); + args = withSwiftFlags(args, config.destination); // Add relative path current working directory const cwd = config.cwd.fsPath; diff --git a/src/WorkspaceContext.ts b/src/WorkspaceContext.ts index 11d67f9a9..e2bc1ec49 100644 --- a/src/WorkspaceContext.ts +++ b/src/WorkspaceContext.ts @@ -157,9 +157,9 @@ export class WorkspaceContext implements vscode.Disposable { } /** Get swift version and create WorkspaceContext */ - static async create(): Promise { + static async create(context: vscode.ExtensionContext): Promise { const tempFolder = await TemporaryFolder.create(); - const toolchain = await SwiftToolchain.create(); + const toolchain = await SwiftToolchain.create(context.storageUri!); return new WorkspaceContext(tempFolder, toolchain); } diff --git a/src/configuration.ts b/src/configuration.ts index a7b9bccd1..cd7219a88 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -27,17 +27,17 @@ export interface LSPConfiguration { /** build destination configuration */ export interface DestinationConfiguration { /** Target triple */ - target: string; + target: string | undefined; /** Path to destination SDK */ - sdk: string; + sdk: string | undefined; /** Path to tools directory */ - binDir: string; + binDir: string | undefined; /** Extra arguments to pass to Swift compiler */ - extraSwiftCFlags: string[]; + extraSwiftCFlags: string[] | undefined; /** Extra arguments to pass to C compiler */ - extraCCFlags: string[]; + extraCCFlags: string[] | undefined; /** Extra arguments to pass to C++ compiler */ - extraCPPFlags: string[]; + extraCPPFlags: string[] | undefined; } /** @@ -70,16 +70,6 @@ const configuration = { const configuration = vscode.workspace .getConfiguration("swift") .get("destination", ""); - if (typeof configuration !== "string") { - return { - target: configuration.target ?? "", - sdk: configuration.sdk ?? "", - binDir: configuration.binDir ?? "", - extraSwiftCFlags: configuration.extraSwiftCFlags ?? [], - extraCCFlags: configuration.extraCCFlags ?? [], - extraCPPFlags: configuration.extraCPPFlags ?? [], - }; - } return configuration; }, @@ -103,11 +93,6 @@ const configuration = { get runtimePath(): string { return vscode.workspace.getConfiguration("swift").get("runtimePath", ""); }, - /** Path to custom swift sdk */ - get sdk(): string { - // FIXME(stevapple): remove the entry - return typeof this.destination === "object" ? this.destination.sdk : ""; - }, /** swift build arguments */ get buildArguments(): string[] { return vscode.workspace.getConfiguration("swift").get("buildArguments", []); diff --git a/src/extension.ts b/src/extension.ts index abdb51366..02a88f19d 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -39,7 +39,7 @@ export async function activate(context: vscode.ExtensionContext): Promise { try { console.debug("Activating Swift for Visual Studio Code..."); - const workspaceContext = await WorkspaceContext.create(); + const workspaceContext = await WorkspaceContext.create(context); context.subscriptions.push(workspaceContext); diff --git a/src/toolchain/destination.ts b/src/toolchain/destination.ts new file mode 100644 index 000000000..5db6c308a --- /dev/null +++ b/src/toolchain/destination.ts @@ -0,0 +1,121 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the VSCode Swift open source project +// +// Copyright (c) 2022 the VSCode Swift project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of VSCode Swift project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import { readFileSync } from "fs"; +import path = require("path"); +import { TextEncoder } from "util"; +import * as vscode from "vscode"; +import { DestinationConfiguration } from "../configuration"; +import { pathExists } from "../utilities/utilities"; + +/** build destination configuration */ +export class Destination { + constructor( + /** Path to the configuration file */ + public path: string, + /** Is generated by the Swift plugin */ + public generated: boolean, + /** Path to tools directory */ + public toolchainBinDir: string, + /** Target triple */ + public triple?: string, + /** Path to destination SDK */ + public sdk?: string, + /** Extra arguments to pass to Swift compiler */ + public extraSwiftCFlags: string[] = [], + /** Extra arguments to pass to C compiler */ + public extraCCFlags: string[] = [], + /** Extra arguments to pass to C++ compiler */ + public extraCPPFlags: string[] = [] + ) {} + + static async create( + toolchainPath: string, + configuration: DestinationConfiguration | string, + storage: vscode.Uri + ): Promise { + if (typeof configuration === "string") { + return destinationFromFile(configuration); + } + const destinationFile = vscode.Uri.joinPath(storage, "destination.json"); + const destination = new Destination( + destinationFile.path, + true, + configuration.binDir ?? path.join(toolchainPath, "usr", "bin"), + configuration.target, + configuration.sdk, + configuration.extraSwiftCFlags, + configuration.extraCCFlags, + configuration.extraCPPFlags + ); + await vscode.workspace.fs.writeFile( + destinationFile, + new TextEncoder().encode(JSON.stringify(destination)) + ); + return destination; + } + + toJSON(): object { + return { + version: 1, + triple: this.triple, + sdk: this.sdk, + "toolchain-bin-dir": this.toolchainBinDir, + "extra-swiftc-flags": this.extraSwiftCFlags, + "extra-cc-flags": this.extraCCFlags, + "extra-cpp-flags": this.extraCPPFlags, + }; + } + + get extraSourcekitLSPFlags(): string[] { + const swiftFlags = this.extraSwiftCFlags; + const cFlags = this.extraCCFlags; + const cxxFlags = this.extraCPPFlags; + if (this.triple) { + swiftFlags.push("-target", this.triple); + } + if (this.sdk) { + swiftFlags.push("-sdk", this.sdk); + } + return swiftFlags + .flatMap(flag => ["-Xswiftc", flag]) + .concat(cFlags.flatMap(flag => ["-Xcc", flag])) + .concat(cxxFlags.flatMap(flag => ["-Xcxx", flag])); + } + + get extraSwiftPMFlags(): string[] { + return this.path ? ["--destination", this.path] : []; + } +} + +function destinationFromFile(path: string): Destination | undefined { + if (path === "" || !pathExists(path)) { + return undefined; + } + const buffer = readFileSync(path); + const destination = JSON.parse(buffer.toString("utf8")); + if (destination.version !== 1) { + return undefined; + } + return new Destination( + path, + false, + destination["toolchain-bin-dir"], + destination["triple"], + destination["sdk"], + destination["extra-swiftc-flags"], + destination["extra-cc-flags"], + destination["extra-cpp-flags"] + ); +} diff --git a/src/toolchain/toolchain.ts b/src/toolchain/toolchain.ts index 2181549e6..bdd34797b 100644 --- a/src/toolchain/toolchain.ts +++ b/src/toolchain/toolchain.ts @@ -20,9 +20,10 @@ import configuration from "../configuration"; import { SwiftOutputChannel } from "../ui/SwiftOutputChannel"; import { execFile, execSwift, pathExists } from "../utilities/utilities"; import { Version } from "../utilities/version"; +import { Destination } from "./destination"; /** - * Contents of **Info.plist** on Windows. + * Contents of **Info.plist** in Windows.platform */ interface InfoPlist { DefaultProperties: { @@ -54,24 +55,26 @@ export class SwiftToolchain { public swiftVersion: Version, public runtimePath?: string, private defaultTarget?: string, - private defaultSDK?: string, - private customSDK?: string, + public destination?: Destination, + private hostSDK?: string, + private targetSDK?: string, public xcTestPath?: string ) {} - static async create(): Promise { + static async create(storage: vscode.Uri): Promise { 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 destination = await this.getDestination(toolchainPath, storage); + const hostSDK = await this.getHostSDK(); + const targetSDK = this.getTargetSDK(destination); const xcTestPath = await this.getXCTestPath( targetInfo, swiftVersion, runtimePath, - customSDK ?? defaultSDK + targetSDK ?? hostSDK ); return new SwiftToolchain( swiftFolderPath, @@ -80,8 +83,9 @@ export class SwiftToolchain { swiftVersion, runtimePath, targetInfo.target?.triple, - defaultSDK, - customSDK, + destination, + hostSDK, + targetSDK, xcTestPath ); } @@ -95,11 +99,14 @@ export class SwiftToolchain { if (this.defaultTarget) { channel.logDiagnostic(`Default Target: ${this.defaultTarget}`); } - if (this.defaultSDK) { - channel.logDiagnostic(`Default SDK: ${this.defaultSDK}`); + if (this.destination) { + channel.logDiagnostic(`Destination File Path: ${this.destination.path}`); } - if (this.customSDK) { - channel.logDiagnostic(`Custom SDK: ${this.customSDK}`); + if (this.hostSDK) { + channel.logDiagnostic(`Host SDK: ${this.hostSDK}`); + } + if (this.targetSDK) { + channel.logDiagnostic(`Target SDK: ${this.targetSDK}`); } if (this.xcTestPath) { channel.logDiagnostic(`XCTest Path: ${this.xcTestPath}`); @@ -187,29 +194,43 @@ export class SwiftToolchain { } /** - * @returns path to default SDK + * @returns swift build destination + */ + private static async getDestination( + toolchainPath: string, + storage: vscode.Uri + ): Promise { + return await Destination.create(toolchainPath, configuration.destination, storage); + } + + /** + * @returns path to host SDK */ - private static async getDefaultSDK(): Promise { + private static async getHostSDK(): Promise { + const env = { + ...process.env, + ...configuration.swiftEnvironmentVariables, + }; switch (process.platform) { case "darwin": { - if (process.env.SDKROOT) { - return process.env.SDKROOT; + if (env.SDKROOT) { + return env.SDKROOT; } const { stdout } = await execFile("xcrun", ["--sdk", "macosx", "--show-sdk-path"]); return path.join(stdout.trimEnd()); } case "win32": { - return process.env.SDKROOT; + return env.SDKROOT; } } return undefined; } /** - * @returns path to custom SDK + * @returns path to explicit target SDK */ - private static getCustomSDK(): string | undefined { - return configuration.sdk !== "" ? configuration.sdk : undefined; + private static getTargetSDK(destination?: Destination): string | undefined { + return destination?.sdk; } /** diff --git a/src/utilities/utilities.ts b/src/utilities/utilities.ts index 756def2e7..b98cebe62 100644 --- a/src/utilities/utilities.ts +++ b/src/utilities/utilities.ts @@ -19,6 +19,7 @@ import * as path from "path"; import * as Stream from "stream"; import configuration from "../configuration"; import { FolderContext } from "../FolderContext"; +import { Destination } from "../toolchain/destination"; export interface ExecError { error: Error; @@ -155,7 +156,7 @@ export async function execSwift( folderContext?: FolderContext ): Promise<{ stdout: string; stderr: string }> { const swift = getSwiftExecutable(); - args = withSwiftSDKFlags(args); + args = withSwiftFlags(args, folderContext?.workspaceContext.toolchain.destination); if (Object.keys(configuration.swiftEnvironmentVariables).length > 0) { // when adding environment vars we either combine with vars passed // into the function or the process environment vars @@ -172,7 +173,8 @@ export async function execSwift( * * @param args original commandline arguments */ -export function withSwiftSDKFlags(args: string[]): string[] { +export function withSwiftFlags(args: string[], destination: Destination | undefined): string[] { + const swiftpmDestinationFlags = destination?.extraSwiftPMFlags ?? []; switch (args[0]) { case "package": { const subcommand = args.splice(0, 2).concat(buildPathFlags()); @@ -183,7 +185,7 @@ export function withSwiftSDKFlags(args: string[]): string[] { // These two tools require building the package, so SDK // flags are needed. Destination control flags are // required to be placed before subcommand options. - return [...subcommand, ...swiftpmSDKFlags(), ...args]; + return [...subcommand, ...swiftpmDestinationFlags, ...args]; } default: // Other swift-package subcommands operate on the host, @@ -195,7 +197,7 @@ export function withSwiftSDKFlags(args: string[]): string[] { case "run": case "test": { const subcommand = args.splice(0, 1).concat(buildPathFlags()); - return [...subcommand, ...swiftpmSDKFlags(), ...args]; + return [...subcommand, ...swiftpmDestinationFlags, ...args]; } default: // We're not going to call the Swift compiler directly for cross-compiling @@ -204,16 +206,6 @@ export function withSwiftSDKFlags(args: string[]): string[] { } } -/** - * Get SDK flags for SwiftPM - */ -export function swiftpmSDKFlags(): string[] { - if (configuration.sdk !== "") { - return ["--sdk", configuration.sdk]; - } - return []; -} - /** * Get build path flags to be passed to swift package manager and sourcekit-lsp server */ From 8d0a38fb1e5df04014342d175c964df6548f5b12 Mon Sep 17 00:00:00 2001 From: stevapple Date: Wed, 10 Aug 2022 04:39:55 +0800 Subject: [PATCH 3/3] Remove the last piece of traditional support --- src/debugger/launch.ts | 2 +- src/sourcekit-lsp/LanguageClientManager.ts | 10 ++++++---- src/toolchain/toolchain.ts | 2 +- src/utilities/utilities.ts | 13 ------------- 4 files changed, 8 insertions(+), 19 deletions(-) diff --git a/src/debugger/launch.ts b/src/debugger/launch.ts index 9e688a686..2f6014382 100644 --- a/src/debugger/launch.ts +++ b/src/debugger/launch.ts @@ -197,7 +197,7 @@ export function createTestConfiguration( if (xcTestPath !== runtimePath) { testEnv.Path = `${xcTestPath};${testEnv.Path ?? process.env.Path}`; } - const sdkroot = configuration.sdk === "" ? process.env.SDKROOT : configuration.sdk; + const sdkroot = ctx.workspaceContext.toolchain.targetSDK; if (sdkroot === undefined) { return null; } diff --git a/src/sourcekit-lsp/LanguageClientManager.ts b/src/sourcekit-lsp/LanguageClientManager.ts index ab730d59c..f3db6635b 100644 --- a/src/sourcekit-lsp/LanguageClientManager.ts +++ b/src/sourcekit-lsp/LanguageClientManager.ts @@ -20,7 +20,6 @@ import { filterArguments, getSwiftExecutable, isPathInsidePath, - swiftDriverSDKFlags, buildPathFlags, swiftRuntimeEnv, } from "../utilities/utilities"; @@ -29,6 +28,7 @@ import { FolderEvent, WorkspaceContext } from "../WorkspaceContext"; import { activateLegacyInlayHints } from "./inlayHints"; import { FolderContext } from "../FolderContext"; import { LanguageClient } from "vscode-languageclient/node"; +import { Destination } from "../toolchain/destination"; /** Manages the creation and destruction of Language clients as we move between * workspace folders @@ -80,11 +80,13 @@ export class LanguageClientManager { // used by single server support to keep a record of the project folders // that are not at the root of their workspace private subFolderWorkspaces: vscode.Uri[]; + private destination?: Destination; constructor(public workspaceContext: WorkspaceContext) { this.singleServerSupport = workspaceContext.swiftVersion >= new Version(5, 7, 0); this.subscriptions = []; this.subFolderWorkspaces = []; + this.destination = workspaceContext.toolchain.destination; if (this.singleServerSupport) { // add/remove folders from server const observeFoldersDisposable = workspaceContext.observeFolders( @@ -343,8 +345,8 @@ export class LanguageClientManager { const serverPathConfig = lspConfig.serverPath; const serverPath = serverPathConfig.length > 0 ? serverPathConfig : getSwiftExecutable("sourcekit-lsp"); - const sdkArguments = [ - ...swiftDriverSDKFlags(true), + const extraArguments = [ + ...(this.destination?.extraSourcekitLSPFlags ?? []), ...filterArguments( configuration.buildArguments.concat(buildPathFlags()), LanguageClientManager.buildArgumentFilter @@ -353,7 +355,7 @@ export class LanguageClientManager { const sourcekit: langclient.Executable = { command: serverPath, - args: lspConfig.serverArguments.concat(sdkArguments), + args: lspConfig.serverArguments.concat(extraArguments), options: { env: { ...process.env, diff --git a/src/toolchain/toolchain.ts b/src/toolchain/toolchain.ts index bdd34797b..f46677893 100644 --- a/src/toolchain/toolchain.ts +++ b/src/toolchain/toolchain.ts @@ -57,7 +57,7 @@ export class SwiftToolchain { private defaultTarget?: string, public destination?: Destination, private hostSDK?: string, - private targetSDK?: string, + public targetSDK?: string, public xcTestPath?: string ) {} diff --git a/src/utilities/utilities.ts b/src/utilities/utilities.ts index b98cebe62..61779bd38 100644 --- a/src/utilities/utilities.ts +++ b/src/utilities/utilities.ts @@ -230,19 +230,6 @@ export function buildDirectoryFromWorkspacePath(workspacePath: string, absolute } } -/** - * Get SDK flags for swiftc - * - * @param indirect whether to pass the flags by -Xswiftc - */ -export function swiftDriverSDKFlags(indirect = false): string[] { - if (configuration.sdk === "") { - return []; - } - const args = ["-sdk", configuration.sdk]; - return indirect ? args.flatMap(arg => ["-Xswiftc", arg]) : args; -} - /** * Get the file name of executable *