Skip to content

Use SwiftPM's SDK computation logic #1643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
40 changes: 40 additions & 0 deletions Tests/BuildSystemIntegrationTests/SwiftPMBuildSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down