From 821663c6fd9f17ef1ae68612e0eaa957c3ec1543 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Thu, 29 Aug 2024 12:15:26 -0400 Subject: [PATCH] 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 https://github.com/swiftlang/swift-package-manager/pull/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 https://github.com/swiftlang/sourcekit-lsp/issues/1587. This PR depends on: - https://github.com/swiftlang/swift-package-manager/pull/7925 --- .../SwiftPMBuildSystem.swift | 22 +++++----- .../SwiftPMBuildSystemTests.swift | 40 +++++++++++++++++++ 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift b/Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift index 8d68de1e..a68941ff 100644 --- a/Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift +++ b/Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift @@ -226,25 +226,23 @@ package actor SwiftPMBuildSystem { let hostSDK = try SwiftSDK.hostSwiftSDK(AbsolutePath(destinationToolchainBinDir)) let hostSwiftPMToolchain = try UserToolchain(swiftSDK: hostSDK) - var destinationSDK: SwiftSDK - if let swiftSDK = options.swiftPM.swiftSDK { - let bundleStore = try SwiftSDKBundleStore( + let destinationSDK = try SwiftSDK.deriveTargetSwiftSDK( + hostSwiftSDK: hostSDK, + hostTriple: hostSwiftPMToolchain.targetTriple, + customCompileTriple: options.swiftPM.triple.map { try Triple($0) }, + swiftSDKSelector: options.swiftPM.swiftSDK, + store: SwiftSDKBundleStore( swiftSDKsDirectory: fileSystem.getSharedSwiftSDKsDirectory( explicitDirectory: options.swiftPM.swiftSDKsDirectory.map { try AbsolutePath(validating: $0) } ), fileSystem: fileSystem, observabilityScope: observabilitySystem.topScope, outputHandler: { _ in } - ) - destinationSDK = try bundleStore.selectBundle(matching: swiftSDK, hostTriple: hostSwiftPMToolchain.targetTriple) - } else { - destinationSDK = hostSDK - } + ), + observabilityScope: observabilitySystem.topScope, + fileSystem: fileSystem + ) - if let triple = options.swiftPM.triple { - destinationSDK = hostSDK - destinationSDK.targetTriple = try Triple(triple) - } let destinationSwiftPMToolchain = try UserToolchain(swiftSDK: destinationSDK) var location = try Workspace.Location( diff --git a/Tests/BuildSystemIntegrationTests/SwiftPMBuildSystemTests.swift b/Tests/BuildSystemIntegrationTests/SwiftPMBuildSystemTests.swift index 437f230f..190c7a0e 100644 --- a/Tests/BuildSystemIntegrationTests/SwiftPMBuildSystemTests.swift +++ b/Tests/BuildSystemIntegrationTests/SwiftPMBuildSystemTests.swift @@ -288,6 +288,46 @@ final class SwiftPMBuildSystemTests: XCTestCase { } } + func testDefaultSDKs() async throws { + let fs = localFileSystem + try await withTestScratchDir { tempDir in + try fs.createFiles( + root: tempDir, + files: [ + "pkg/Sources/lib/a.swift": "", + "pkg/Package.swift": """ + // swift-tools-version:6.0 + import PackageDescription + let package = Package( + name: "a", + targets: [.target(name: "lib")] + ) + """, + ] + ) + let tr = ToolchainRegistry.forTesting + + let options = SourceKitLSPOptions.SwiftPMOptions( + swiftSDKsDirectory: "/tmp/non_existent_sdks_dir", + triple: "wasm32-unknown-wasi" + ) + + let swiftpmBuildSystem = try await SwiftPMBuildSystem( + workspacePath: tempDir.appending(component: "pkg"), + toolchainRegistry: tr, + fileSystem: fs, + options: SourceKitLSPOptions(swiftPM: options), + testHooks: SwiftPMTestHooks() + ) + let path = await swiftpmBuildSystem.destinationBuildParameters.toolchain.sdkRootPath + XCTAssertEqual( + path?.components.suffix(3), + ["usr", "share", "wasi-sysroot"], + "SwiftPMBuildSystem should share default SDK derivation logic with libSwiftPM" + ) + } + } + func testManifestArgs() async throws { let fs = localFileSystem try await withTestScratchDir { tempDir in