Skip to content

Commit 821663c

Browse files
authored
Use SwiftPM's SDK computation logic (#1643)
Using `SwiftSDK.deriveTargetSwiftSDK`, which is the same method that SwiftPM's own CLI tools use to determine the SDK from passed-in info (target `--triple`, `--swift-sdk`, and host sdk). This allows us to better uphold the contract in the [Configuration File](https://github.com/swiftlang/sourcekit-lsp/blob/d11c101ce210ad23f917cf06fb13a7d9e243872b/Documentation/Configuration%20File.md#structure) docs, namely that the `swiftSDK` param is "Equivalent to SwiftPM's `--swift-sdk` option" and similarly for `triple`. As concrete examples of where (AFAICT) the current implementation diverges: - Passing a `--triple` of `wasm32-unknown-wasi` to `swift-build` will use the toolchain-integrated Wasm SDK if one exists. Passing the same value to sourcekit-lsp does not do this. - Perhaps more relevant: after landing swiftlang/swift-package-manager#6828, this change will make it so that building for iOS is as simple as setting `"triple": "arm64-apple-ios"` in the config! Currently, it's necessary to set C/Swift flags and hardcode the sysroot. Should close #1587. This PR depends on: - swiftlang/swift-package-manager#7925
1 parent d11c101 commit 821663c

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,25 +226,23 @@ package actor SwiftPMBuildSystem {
226226
let hostSDK = try SwiftSDK.hostSwiftSDK(AbsolutePath(destinationToolchainBinDir))
227227
let hostSwiftPMToolchain = try UserToolchain(swiftSDK: hostSDK)
228228

229-
var destinationSDK: SwiftSDK
230-
if let swiftSDK = options.swiftPM.swiftSDK {
231-
let bundleStore = try SwiftSDKBundleStore(
229+
let destinationSDK = try SwiftSDK.deriveTargetSwiftSDK(
230+
hostSwiftSDK: hostSDK,
231+
hostTriple: hostSwiftPMToolchain.targetTriple,
232+
customCompileTriple: options.swiftPM.triple.map { try Triple($0) },
233+
swiftSDKSelector: options.swiftPM.swiftSDK,
234+
store: SwiftSDKBundleStore(
232235
swiftSDKsDirectory: fileSystem.getSharedSwiftSDKsDirectory(
233236
explicitDirectory: options.swiftPM.swiftSDKsDirectory.map { try AbsolutePath(validating: $0) }
234237
),
235238
fileSystem: fileSystem,
236239
observabilityScope: observabilitySystem.topScope,
237240
outputHandler: { _ in }
238-
)
239-
destinationSDK = try bundleStore.selectBundle(matching: swiftSDK, hostTriple: hostSwiftPMToolchain.targetTriple)
240-
} else {
241-
destinationSDK = hostSDK
242-
}
241+
),
242+
observabilityScope: observabilitySystem.topScope,
243+
fileSystem: fileSystem
244+
)
243245

244-
if let triple = options.swiftPM.triple {
245-
destinationSDK = hostSDK
246-
destinationSDK.targetTriple = try Triple(triple)
247-
}
248246
let destinationSwiftPMToolchain = try UserToolchain(swiftSDK: destinationSDK)
249247

250248
var location = try Workspace.Location(

Tests/BuildSystemIntegrationTests/SwiftPMBuildSystemTests.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,46 @@ final class SwiftPMBuildSystemTests: XCTestCase {
288288
}
289289
}
290290

291+
func testDefaultSDKs() async throws {
292+
let fs = localFileSystem
293+
try await withTestScratchDir { tempDir in
294+
try fs.createFiles(
295+
root: tempDir,
296+
files: [
297+
"pkg/Sources/lib/a.swift": "",
298+
"pkg/Package.swift": """
299+
// swift-tools-version:6.0
300+
import PackageDescription
301+
let package = Package(
302+
name: "a",
303+
targets: [.target(name: "lib")]
304+
)
305+
""",
306+
]
307+
)
308+
let tr = ToolchainRegistry.forTesting
309+
310+
let options = SourceKitLSPOptions.SwiftPMOptions(
311+
swiftSDKsDirectory: "/tmp/non_existent_sdks_dir",
312+
triple: "wasm32-unknown-wasi"
313+
)
314+
315+
let swiftpmBuildSystem = try await SwiftPMBuildSystem(
316+
workspacePath: tempDir.appending(component: "pkg"),
317+
toolchainRegistry: tr,
318+
fileSystem: fs,
319+
options: SourceKitLSPOptions(swiftPM: options),
320+
testHooks: SwiftPMTestHooks()
321+
)
322+
let path = await swiftpmBuildSystem.destinationBuildParameters.toolchain.sdkRootPath
323+
XCTAssertEqual(
324+
path?.components.suffix(3),
325+
["usr", "share", "wasi-sysroot"],
326+
"SwiftPMBuildSystem should share default SDK derivation logic with libSwiftPM"
327+
)
328+
}
329+
}
330+
291331
func testManifestArgs() async throws {
292332
let fs = localFileSystem
293333
try await withTestScratchDir { tempDir in

0 commit comments

Comments
 (0)