Skip to content

Commit a0ff972

Browse files
Generate SDKSettings.json file for Swift SDKs for Swift 5.9-6.0 (#185)
I know that the warnings about the `SDKSettings.json` file were fixed in Swift 6.1 and later and that issue #110 was closed. However, we are still generating Swift SDKs for 5.9, 5.10, and 6.0 which all give the warnings. So, my suggestion is to generate this `SDKSettings.json` file for versions older than 6.1, similar to how the the Linux Static Swift SDKs do it. Here is the file that comes in the `swift-6.0.3-RELEASE_static-linux-0.0.1` Swift SDK for example: ```json { "DisplayName": "Swift SDK for Statically Linked Linux (x86_64)", "Version": "0.0.1", "VersionMap": {}, "CanonicalName": "x86_64-swift-linux-musl" } ``` Taking inspiration from this, I am generating a similar file for Swift SDKs created by the generator: ```json { "CanonicalName" : "x86_64-swift-linux-gnu", "DisplayName" : "Swift SDK for Ubuntu Jammy (x86_64)", "Version" : "0.0.1", "VersionMap" : { } } ``` The ordering is not the same due to how `JSONEncoder()` sorts things, and the formatting is a little different too due to `.prettyPrinted`, but it works just the same. And, the "warning: Could not read SDKSettings.json for SDK at: <path>" warning is successfully suppressed. This really is a minor change that is more "quality-of-life" then adding any functionality. But, not having those warnings printed when using the generated Swift SDKs are used is a nice to have.
1 parent 7145887 commit a0ff972

File tree

4 files changed

+122
-4
lines changed

4 files changed

+122
-4
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
extension String {
2+
func hasAnyPrefix(from array: [String]) -> Bool {
3+
for item in array {
4+
if self.hasPrefix(item) {
5+
return true
6+
}
7+
}
8+
return false
9+
}
10+
}

Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Metadata.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,36 @@ extension SwiftSDKGenerator {
112112
)
113113
)
114114
}
115+
116+
struct SDKSettings: Codable {
117+
var DisplayName: String
118+
var Version: String
119+
var VersionMap: [String: String] = [:]
120+
var CanonicalName: String
121+
}
122+
123+
/// Generates an `SDKSettings.json` file that looks like this:
124+
///
125+
/// ```json
126+
/// {
127+
/// "CanonicalName" : "<arch>-swift-linux-[gnu|gnueabihf]",
128+
/// "DisplayName" : "Swift SDK for <distribution> (<arch>)",
129+
/// "Version" : "0.0.1",
130+
/// "VersionMap" : {
131+
///
132+
/// }
133+
/// }
134+
/// ```
135+
func generateSDKSettingsFile(sdkDirPath: FilePath, distribution: LinuxDistribution) throws {
136+
logger.info("Generating SDKSettings.json file to silence cross-compilation warnings...")
137+
138+
let sdkSettings = SDKSettings(
139+
DisplayName: "Swift SDK for \(distribution) (\(targetTriple.archName))",
140+
Version: bundleVersion,
141+
CanonicalName: targetTriple.triple.replacingOccurrences(of: "unknown", with: "swift")
142+
)
143+
144+
let sdkSettingsFilePath = sdkDirPath.appending("SDKSettings.json")
145+
try writeFile(at: sdkSettingsFilePath, encoder.encode(sdkSettings))
146+
}
115147
}

Sources/SwiftSDKGenerator/SwiftSDKRecipes/LinuxRecipe.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,7 @@ public struct LinuxRecipe: SwiftSDKRecipe {
195195
if self.hostSwiftSource == .preinstalled {
196196
// Swift 5.9 and 5.10 require `supportedTriples` to be set in info.json.
197197
// FIXME: This can be removed once the SDK generator does not support 5.9/5.10 any more.
198-
if self.versionsConfiguration.swiftVersion.hasPrefix("5.9")
199-
|| self.versionsConfiguration.swiftVersion.hasPrefix("5.10") {
198+
if self.versionsConfiguration.swiftVersion.hasAnyPrefix(from: ["5.9", "5.10"]) {
200199
return [
201200
Triple("x86_64-unknown-linux-gnu"),
202201
Triple("aarch64-unknown-linux-gnu"),
@@ -292,13 +291,18 @@ public struct LinuxRecipe: SwiftSDKRecipe {
292291

293292
try await generator.fixAbsoluteSymlinks(sdkDirPath: sdkDirPath)
294293

294+
// Swift 6.1 and later do not throw warnings about the SDKSettings.json file missing,
295+
// so they don't need this file.
296+
if self.versionsConfiguration.swiftVersion.hasAnyPrefix(from: ["5.9", "5.10", "6.0"]) {
297+
try await generator.generateSDKSettingsFile(sdkDirPath: sdkDirPath, distribution: linuxDistribution)
298+
}
299+
295300
if self.hostSwiftSource != .preinstalled {
296301
if self.mainHostTriple.os != .linux && !self.versionsConfiguration.swiftVersion.hasPrefix("6.0") {
297302
try await generator.prepareLLDLinker(engine, llvmArtifact: downloadableArtifacts.hostLLVM)
298303
}
299304

300-
if self.versionsConfiguration.swiftVersion.hasPrefix("5.9") ||
301-
self.versionsConfiguration.swiftVersion.hasPrefix("5.10") {
305+
if self.versionsConfiguration.swiftVersion.hasAnyPrefix(from: ["5.9", "5.10"]) {
302306
try await generator.symlinkClangHeaders()
303307
}
304308

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2022-2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Logging
14+
import SystemPackage
15+
import XCTest
16+
17+
@testable import SwiftSDKGenerator
18+
19+
final class SwiftSDKGeneratorMetadataTests: XCTestCase {
20+
let logger = Logger(label: "SwiftSDKGeneratorMetadataTests")
21+
22+
func testGenerateSDKSettingsFile() async throws {
23+
let testCases = [
24+
(
25+
bundleVersion: "0.0.1",
26+
targetTriple: Triple("x86_64-unknown-linux-gnu"),
27+
expectedCanonicalName: "x86_64-swift-linux-gnu"
28+
),
29+
(
30+
bundleVersion: "0.0.2",
31+
targetTriple: Triple("aarch64-unknown-linux-gnu"),
32+
expectedCanonicalName: "aarch64-swift-linux-gnu"
33+
),
34+
(
35+
bundleVersion: "0.0.3",
36+
targetTriple: Triple("armv7-unknown-linux-gnueabihf"),
37+
expectedCanonicalName: "armv7-swift-linux-gnueabihf"
38+
)
39+
]
40+
41+
for testCase in testCases {
42+
let sdk = try await SwiftSDKGenerator(
43+
bundleVersion: testCase.bundleVersion,
44+
targetTriple: testCase.targetTriple,
45+
artifactID: "6.0.3-RELEASE_ubuntu_jammy_\(testCase.targetTriple.archName)",
46+
isIncremental: false,
47+
isVerbose: false,
48+
logger: logger
49+
)
50+
let linuxDistribution = try LinuxDistribution(name: .ubuntu, version: "22.04")
51+
52+
let sdkDirPath = FilePath(".")
53+
try await sdk.generateSDKSettingsFile(sdkDirPath: sdkDirPath, distribution: linuxDistribution)
54+
55+
// Make sure the file exists
56+
let sdkSettingsFile = sdkDirPath.appending("SDKSettings.json")
57+
let fileExists = await sdk.doesFileExist(at: sdkSettingsFile)
58+
XCTAssertTrue(fileExists)
59+
60+
// Read back file, make sure it contains the expected data
61+
let data = String(data: try await sdk.readFile(at: sdkSettingsFile), encoding: .utf8)
62+
XCTAssertNotNil(data)
63+
XCTAssertTrue(data!.contains(testCase.bundleVersion))
64+
XCTAssertTrue(data!.contains("(\(testCase.targetTriple.archName))"))
65+
XCTAssertTrue(data!.contains(linuxDistribution.description))
66+
XCTAssertTrue(data!.contains(testCase.expectedCanonicalName))
67+
68+
// Cleanup
69+
try await sdk.removeFile(at: sdkSettingsFile)
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)