mirror of
https://github.com/apple/sourcekit-lsp.git
synced 2026-03-02 18:23:24 +01:00
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](d11c101ce2/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
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user