Skip to content

Commit 55f0a83

Browse files
authored
Make sdkRootPath property of swift-sdk.json targetTriples object optional (#8687)
Makes the "sdkRootPath" property of `swift-sdk.json` optional. ### Motivation: The Android SDK bundle (swiftlang/swift#80788) does not include the Android NDK's sysroot in the bundle itself, but instead relies on it being installed locally. The install location will vary, and the user will be able to configure it with a command (modulo #8584) like: ``` swift sdk configure --sdk-root-path ~/Library/Android/sdk/ndk/27.0.12077973/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/ swift-6.2-RELEASE-android-0.1 aarch64-unknown-linux-android28 ``` However, since the `SwiftSDKMetadataV4.sdkRootPath` property is declared as non-optional, *some* value must be included in the `swift-sdk.json` file. @MaxDesiatov at swiftlang/swift#80788 (comment) mentions: > As for making it optional, I don't quite remember the exact issue that caused it to become non-optional. After all, making it optional is technically not a breaking change, so potentially could be considered if necessary. ### Modifications: Change `SwiftSDKMetadataV4.sdkRootPath` from `String` to `String?` ### Result: The swift-sdk.json can now contain destinations that do not specify a `sdkRootPath` property.
1 parent beecce0 commit 55f0a83

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

Sources/PackageModel/SwiftSDKs/SwiftSDK.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,9 @@ public struct SwiftSDK: Equatable {
340340
swiftSDKDirectory: Basics.AbsolutePath? = nil
341341
) throws where Path == Basics.AbsolutePath {
342342
self.init(
343-
sdkRootPath: try AbsolutePath(validating: properties.sdkRootPath, relativeTo: swiftSDKDirectory),
343+
sdkRootPath: try properties.sdkRootPath.map {
344+
try AbsolutePath(validating: $0, relativeTo: swiftSDKDirectory)
345+
},
344346
swiftResourcesPath: try properties.swiftResourcesPath.map {
345347
try AbsolutePath(validating: $0, relativeTo: swiftSDKDirectory)
346348
},
@@ -1175,7 +1177,7 @@ struct SerializedDestinationV3: Decodable {
11751177
struct SwiftSDKMetadataV4: Decodable {
11761178
struct TripleProperties: Codable {
11771179
/// Path relative to `swift-sdk.json` containing SDK root.
1178-
var sdkRootPath: String
1180+
var sdkRootPath: String?
11791181

11801182
/// Path relative to `swift-sdk.json` containing Swift resources for dynamic linking.
11811183
var swiftResourcesPath: String?

Tests/PackageModelTests/SwiftSDKTests.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ private let hostTriple = try! Triple("arm64-apple-darwin22.1.0")
2525
private let olderHostTriple = try! Triple("arm64-apple-darwin20.1.0")
2626
private let linuxGNUTargetTriple = try! Triple("x86_64-unknown-linux-gnu")
2727
private let linuxMuslTargetTriple = try! Triple("x86_64-unknown-linux-musl")
28+
private let androidTargetTriple = try! Triple("aarch64-unknown-linux-android28")
2829
private let wasiTargetTriple = try! Triple("wasm32-unknown-wasi")
2930
private let extraFlags = BuildFlags(
3031
cCompilerFlags: ["-fintegrated-as"],
@@ -186,6 +187,20 @@ private let toolsetRootSwiftSDKv4 = (
186187
"""# as SerializedJSON
187188
)
188189

190+
private let androidWithoutSDKRootPathSwiftSDKv4 = (
191+
path: bundleRootPath.appending(component: "androidWithoutSDKRootPathSwiftSDKv4.json"),
192+
json: #"""
193+
{
194+
"targetTriples": {
195+
"\#(androidTargetTriple.tripleString)": {
196+
"toolsetPaths": ["/tools/otherToolsNoRoot.json"]
197+
}
198+
},
199+
"schemaVersion": "4.0"
200+
}
201+
"""# as SerializedJSON
202+
)
203+
189204
private let missingToolsetSwiftSDKv4 = (
190205
path: bundleRootPath.appending(component: "missingToolsetSwiftSDKv4.json"),
191206
json: #"""
@@ -351,6 +366,23 @@ private let parsedToolsetRootDestination = SwiftSDK(
351366
)
352367
)
353368

369+
private let parsedToolsetNoSDKRootPathDestination = SwiftSDK(
370+
targetTriple: androidTargetTriple,
371+
toolset: .init(
372+
knownTools: [
373+
.librarian: .init(path: try! AbsolutePath(validating: "\(usrBinTools[.librarian]!)")),
374+
.linker: .init(path: try! AbsolutePath(validating: "\(usrBinTools[.linker]!)")),
375+
.debugger: .init(path: try! AbsolutePath(validating: "\(usrBinTools[.debugger]!)")),
376+
],
377+
rootPaths: []
378+
),
379+
pathsConfiguration: .init(
380+
sdkRootPath: nil,
381+
toolsetPaths: ["/tools/otherToolsNoRoot.json"]
382+
.map { try! AbsolutePath(validating: $0) }
383+
)
384+
)
385+
354386
private let testFiles: [(path: AbsolutePath, json: SerializedJSON)] = [
355387
destinationV1,
356388
destinationV2,
@@ -365,6 +397,7 @@ private let testFiles: [(path: AbsolutePath, json: SerializedJSON)] = [
365397
invalidVersionSwiftSDKv4,
366398
invalidToolsetSwiftSDKv4,
367399
wasiWithoutToolsetsSwiftSDKv4,
400+
androidWithoutSDKRootPathSwiftSDKv4,
368401
otherToolsNoRoot,
369402
someToolsWithRoot,
370403
invalidToolset,
@@ -488,6 +521,15 @@ final class SwiftSDKTests: XCTestCase {
488521

489522
XCTAssertEqual(toolsetRootSwiftSDKv4Decoded, [parsedToolsetRootDestination])
490523

524+
let androidWithoutSDKRootPathSwiftSDKv4Decoded = try SwiftSDK.decode(
525+
fromFile: androidWithoutSDKRootPathSwiftSDKv4.path,
526+
hostToolchainBinDir: toolchainBinAbsolutePath,
527+
fileSystem: fs,
528+
observabilityScope: observability
529+
)
530+
531+
XCTAssertEqual(androidWithoutSDKRootPathSwiftSDKv4Decoded, [parsedToolsetNoSDKRootPathDestination])
532+
491533
XCTAssertThrowsError(try SwiftSDK.decode(
492534
fromFile: missingToolsetSwiftSDKv4.path,
493535
hostToolchainBinDir: toolchainBinAbsolutePath,

0 commit comments

Comments
 (0)