Skip to content

Commit f9c5565

Browse files
authored
[5.9] Update Swift SDK nomenclature (#6679)
`buildTimeTriple` and `runTimeTriple` renamed to `hostTriple` and `targetTriple`, destinations renamed to Swift SDKs where possible without a broader disruption. Full renaming of the `Destination` type to `SwiftSDK` would come in a separate change to reduce the diff for this one.
1 parent f45e0be commit f9c5565

14 files changed

+383
-138
lines changed

Sources/CoreCommands/SwiftTool.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ public final class SwiftTool {
737737
{
738738
destination = matchingDestination
739739
} else {
740-
return .failure(DestinationError.noDestinationsDecoded(customDestination))
740+
return .failure(SwiftSDKError.noSwiftSDKDecoded(customDestination))
741741
}
742742
} else if let triple = options.build.customCompileTriple,
743743
let targetDestination = Destination.defaultDestination(for: triple, host: hostDestination)

Sources/PackageModel/Destination.swift

Lines changed: 145 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import TSCBasic
1616

1717
import struct TSCUtility.Version
1818

19-
/// Errors related to cross-compilation destinations.
20-
public enum DestinationError: Swift.Error {
19+
/// Errors related to Swift SDKs.
20+
public enum SwiftSDKError: Swift.Error {
2121
/// A passed argument is neither a valid file system path nor a URL.
2222
case invalidPathOrURL(String)
2323

@@ -30,67 +30,67 @@ public enum DestinationError: Swift.Error {
3030
/// Name of the destination bundle is not valid.
3131
case invalidBundleName(String)
3232

33-
/// No valid destinations were decoded from a destination file.
34-
case noDestinationsDecoded(AbsolutePath)
33+
/// No valid Swift SDKs were decoded from a metadata file.
34+
case noSwiftSDKDecoded(AbsolutePath)
3535

3636
/// Path used for storing destination configuration data is not a directory.
3737
case pathIsNotDirectory(AbsolutePath)
3838

39-
/// A destination couldn't be serialized with the latest serialization schema, potentially because it
39+
/// Swift SDK metadata couldn't be serialized with the latest serialization schema, potentially because it
4040
/// was deserialized from an earlier incompatible schema version or initialized manually with properties
4141
/// required for initialization missing.
42-
case unserializableDestination
42+
case unserializableMetadata
4343

44-
/// No configuration values are available for this destination and run-time triple.
45-
case destinationNotFound(artifactID: String, builtTimeTriple: Triple, runTimeTriple: Triple)
44+
/// No configuration values are available for this Swift SDK and target triple.
45+
case swiftSDKNotFound(artifactID: String, hostTriple: Triple, targetTriple: Triple)
4646

47-
/// A destination bundle with this name is already installed, can't install a new bundle with the same name.
48-
case destinationBundleAlreadyInstalled(bundleName: String)
47+
/// A Swift SDK bundle with this name is already installed, can't install a new bundle with the same name.
48+
case swiftSDKBundleAlreadyInstalled(bundleName: String)
4949

50-
/// A destination with this artifact ID is already installed. Can't install a new bundle with this artifact,
50+
/// A Swift SDK with this artifact ID is already installed. Can't install a new bundle with this artifact,
5151
/// installed artifact IDs are expected to be unique.
52-
case destinationArtifactAlreadyInstalled(installedBundleName: String, newBundleName: String, artifactID: String)
52+
case swiftSDKArtifactAlreadyInstalled(installedBundleName: String, newBundleName: String, artifactID: String)
5353

5454
#if os(macOS)
5555
/// Quarantine attribute should be removed by the `xattr` command from an installed bundle.
5656
case quarantineAttributePresent(bundlePath: AbsolutePath)
5757
#endif
5858
}
5959

60-
extension DestinationError: CustomStringConvertible {
60+
extension SwiftSDKError: CustomStringConvertible {
6161
public var description: String {
6262
switch self {
6363
case .invalidPathOrURL(let argument):
6464
return "`\(argument)` is neither a valid filesystem path nor a URL."
6565
case .invalidSchemaVersion:
66-
return "unsupported destination file schema version"
66+
return "unsupported Swift SDK file schema version"
6767
case .invalidInstallation(let problem):
6868
return problem
6969
case .invalidBundleName(let name):
7070
return """
7171
invalid bundle name `\(name)`, unpacked Swift SDK bundles are expected to have `.artifactbundle` extension
7272
"""
73-
case .noDestinationsDecoded(let path):
74-
return "no valid Swift SDKs were decoded from a destination file at path `\(path)`"
73+
case .noSwiftSDKDecoded(let path):
74+
return "no valid Swift SDKs were decoded from a metadata file at path `\(path)`"
7575
case .pathIsNotDirectory(let path):
7676
return "path expected to be a directory is not a directory or doesn't exist: `\(path)`"
77-
case .unserializableDestination:
77+
case .unserializableMetadata:
7878
return """
7979
Swift SDK configuration couldn't be serialized with the latest serialization schema, potentially because \
8080
it was deserialized from an earlier incompatible schema version or initialized manually with missing \
8181
properties required for initialization
8282
"""
83-
case .destinationNotFound(let artifactID, let buildTimeTriple, let runTimeTriple):
83+
case .swiftSDKNotFound(let artifactID, let hostTriple, let targetTriple):
8484
return """
85-
Swift SDK with ID `\(artifactID)`, build-time triple \(buildTimeTriple), and run-time triple \
86-
\(runTimeTriple) is not currently installed.
85+
Swift SDK with ID `\(artifactID)`, host triple \(hostTriple), and target triple \(targetTriple) is not \
86+
currently installed.
8787
"""
88-
case .destinationBundleAlreadyInstalled(let bundleName):
88+
case .swiftSDKBundleAlreadyInstalled(let bundleName):
8989
return """
9090
Swift SDK bundle with name `\(bundleName)` is already installed. Can't install a new bundle \
9191
with the same name.
9292
"""
93-
case .destinationArtifactAlreadyInstalled(let installedBundleName, let newBundleName, let artifactID):
93+
case .swiftSDKArtifactAlreadyInstalled(let installedBundleName, let newBundleName, let artifactID):
9494
return """
9595
A Swift SDK with artifact ID `\(artifactID)` is already included in an installed bundle with name \
9696
`\(installedBundleName)`. Can't install a new bundle `\(newBundleName)` with this artifact, artifact IDs \
@@ -282,6 +282,52 @@ public struct Destination: Equatable {
282282
}
283283
}
284284

285+
/// Initialize paths configuration from values deserialized using v4 schema.
286+
/// - Parameters:
287+
/// - properties: properties of a Swift SDK for the given triple.
288+
/// - swiftSDKDirectory: directory used for converting relative paths in `properties` to absolute paths.
289+
fileprivate init(_ properties: SwiftSDKMetadataV4.TripleProperties, swiftSDKDirectory: AbsolutePath? = nil) throws {
290+
if let swiftSDKDirectory {
291+
self.init(
292+
sdkRootPath: try AbsolutePath(validating: properties.sdkRootPath, relativeTo: swiftSDKDirectory),
293+
swiftResourcesPath: try properties.swiftResourcesPath.map {
294+
try AbsolutePath(validating: $0, relativeTo: swiftSDKDirectory)
295+
},
296+
swiftStaticResourcesPath: try properties.swiftStaticResourcesPath.map {
297+
try AbsolutePath(validating: $0, relativeTo: swiftSDKDirectory)
298+
},
299+
includeSearchPaths: try properties.includeSearchPaths?.map {
300+
try AbsolutePath(validating: $0, relativeTo: swiftSDKDirectory)
301+
},
302+
librarySearchPaths: try properties.librarySearchPaths?.map {
303+
try AbsolutePath(validating: $0, relativeTo: swiftSDKDirectory)
304+
},
305+
toolsetPaths: try properties.toolsetPaths?.map {
306+
try AbsolutePath(validating: $0, relativeTo: swiftSDKDirectory)
307+
}
308+
)
309+
} else {
310+
self.init(
311+
sdkRootPath: try AbsolutePath(validating: properties.sdkRootPath),
312+
swiftResourcesPath: try properties.swiftResourcesPath.map {
313+
try AbsolutePath(validating: $0)
314+
},
315+
swiftStaticResourcesPath: try properties.swiftStaticResourcesPath.map {
316+
try AbsolutePath(validating: $0)
317+
},
318+
includeSearchPaths: try properties.includeSearchPaths?.map {
319+
try AbsolutePath(validating: $0)
320+
},
321+
librarySearchPaths: try properties.librarySearchPaths?.map {
322+
try AbsolutePath(validating: $0)
323+
},
324+
toolsetPaths: try properties.toolsetPaths?.map {
325+
try AbsolutePath(validating: $0)
326+
}
327+
)
328+
}
329+
}
330+
285331
public mutating func merge(with newConfiguration: Self) {
286332
if let sdkRootPath = newConfiguration.sdkRootPath {
287333
self.sdkRootPath = sdkRootPath
@@ -408,7 +454,7 @@ public struct Destination: Equatable {
408454
environment: environment
409455
).spm_chomp()
410456
guard !sdkPathStr.isEmpty else {
411-
throw DestinationError.invalidInstallation("default SDK not found")
457+
throw SwiftSDKError.invalidInstallation("default SDK not found")
412458
}
413459
sdkPath = try AbsolutePath(validating: sdkPathStr)
414460
}
@@ -569,12 +615,56 @@ extension Destination {
569615
destinationDirectory: destinationDirectory
570616
)
571617
}
618+
619+
case Version(4, 0, 0):
620+
let swiftSDKs = try decoder.decode(path: path, fileSystem: fileSystem, as: SwiftSDKMetadataV4.self)
621+
let swiftSDKDirectory = path.parentDirectory
622+
623+
return try swiftSDKs.targetTriples.map { triple, properties in
624+
let triple = try Triple(triple)
625+
626+
let pathStrings = properties.toolsetPaths ?? []
627+
let toolset = try pathStrings.reduce(into: Toolset(knownTools: [:], rootPaths: [])) {
628+
try $0.merge(
629+
with: Toolset(
630+
from: .init(validating: $1, relativeTo: swiftSDKDirectory),
631+
at: fileSystem,
632+
observabilityScope
633+
)
634+
)
635+
}
636+
637+
return try Destination(
638+
targetTriple: triple,
639+
properties: properties,
640+
toolset: toolset,
641+
swiftSDKDirectory: swiftSDKDirectory
642+
)
643+
}
572644
default:
573-
throw DestinationError.invalidSchemaVersion
645+
throw SwiftSDKError.invalidSchemaVersion
574646
}
575647
}
576648

577-
649+
/// Initialize new Swift SDK from values deserialized using v4 schema.
650+
/// - Parameters:
651+
/// - targetTriple: triple of the machine running code built with this Swift SDK.
652+
/// - properties: properties of the Swift SDK for the given triple.
653+
/// - toolset: combined toolset used by this Swift SDK.
654+
/// - swiftSDKDirectory: directory used for converting relative paths in `properties` to absolute paths.
655+
init(
656+
targetTriple: Triple,
657+
properties: SwiftSDKMetadataV4.TripleProperties,
658+
toolset: Toolset = .init(),
659+
swiftSDKDirectory: AbsolutePath? = nil
660+
) throws {
661+
self.init(
662+
targetTriple: targetTriple,
663+
toolset: toolset,
664+
pathsConfiguration: try .init(properties, swiftSDKDirectory: swiftSDKDirectory)
665+
)
666+
}
667+
578668
/// Initialize new destination from values deserialized using v3 schema.
579669
/// - Parameters:
580670
/// - runTimeTriple: triple of the machine running code built with this destination.
@@ -641,7 +731,7 @@ extension Destination {
641731
)
642732
)
643733
default:
644-
throw DestinationError.invalidSchemaVersion
734+
throw SwiftSDKError.invalidSchemaVersion
645735
}
646736
}
647737

@@ -651,12 +741,12 @@ extension Destination {
651741
/// from different schema versions or constructed manually without providing valid values for such properties.
652742
var serialized: (Triple, SerializedDestinationV3.TripleProperties) {
653743
get throws {
654-
guard let runTimeTriple = self.targetTriple, let sdkRootDir = self.pathsConfiguration.sdkRootPath else {
655-
throw DestinationError.unserializableDestination
744+
guard let targetTriple = self.targetTriple, let sdkRootDir = self.pathsConfiguration.sdkRootPath else {
745+
throw SwiftSDKError.unserializableMetadata
656746
}
657747

658748
return (
659-
runTimeTriple,
749+
targetTriple,
660750
.init(
661751
sdkRootPath: sdkRootDir.pathString,
662752
swiftResourcesPath: self.pathsConfiguration.swiftResourcesPath?.pathString,
@@ -746,3 +836,29 @@ struct SerializedDestinationV3: Decodable {
746836
/// Mapping of triple strings to corresponding properties of such run-time triple.
747837
let runTimeTriples: [String: TripleProperties]
748838
}
839+
840+
/// Represents v4 schema of `swift-sdk.json` (previously `destination.json`) files used for cross-compilation.
841+
struct SwiftSDKMetadataV4: Decodable {
842+
struct TripleProperties: Codable {
843+
/// Path relative to `swift-sdk.json` containing SDK root.
844+
var sdkRootPath: String
845+
846+
/// Path relative to `swift-sdk.json` containing Swift resources for dynamic linking.
847+
var swiftResourcesPath: String?
848+
849+
/// Path relative to `swift-sdk.json` containing Swift resources for static linking.
850+
var swiftStaticResourcesPath: String?
851+
852+
/// Array of paths relative to `swift-sdk.json` containing headers.
853+
var includeSearchPaths: [String]?
854+
855+
/// Array of paths relative to `swift-sdk.json` containing libraries.
856+
var librarySearchPaths: [String]?
857+
858+
/// Array of paths relative to `swift-sdk.json` containing toolset files.
859+
var toolsetPaths: [String]?
860+
}
861+
862+
/// Mapping of triple strings to corresponding properties of such target triple.
863+
let targetTriples: [String: TripleProperties]
864+
}

Sources/PackageModel/SwiftSDKBundle.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public struct SwiftSDKBundle {
177177
{
178178
bundlePath = originalBundlePath
179179
} else {
180-
throw DestinationError.invalidPathOrURL(bundlePathOrURL)
180+
throw SwiftSDKError.invalidPathOrURL(bundlePathOrURL)
181181
}
182182

183183
try installIfValid(
@@ -211,16 +211,16 @@ public struct SwiftSDKBundle {
211211
let regex = try RegEx(pattern: "(.+\\.artifactbundle).*")
212212

213213
guard let bundleName = bundlePath.components.last else {
214-
throw DestinationError.invalidPathOrURL(bundlePath.pathString)
214+
throw SwiftSDKError.invalidPathOrURL(bundlePath.pathString)
215215
}
216216

217217
guard let unpackedBundleName = regex.matchGroups(in: bundleName).first?.first else {
218-
throw DestinationError.invalidBundleName(bundleName)
218+
throw SwiftSDKError.invalidBundleName(bundleName)
219219
}
220220

221221
let installedBundlePath = destinationsDirectory.appending(component: unpackedBundleName)
222222
guard !fileSystem.exists(installedBundlePath) else {
223-
throw DestinationError.destinationBundleAlreadyInstalled(bundleName: unpackedBundleName)
223+
throw SwiftSDKError.swiftSDKBundleAlreadyInstalled(bundleName: unpackedBundleName)
224224
}
225225

226226
// If there's no archive extension on the bundle name, assuming it's not archived and returning the same path.
@@ -252,7 +252,7 @@ public struct SwiftSDKBundle {
252252
#if os(macOS)
253253
// Check the quarantine attribute on bundles downloaded manually in the browser.
254254
guard !fileSystem.hasAttribute(.quarantine, bundlePath) else {
255-
throw DestinationError.quarantineAttributePresent(bundlePath: bundlePath)
255+
throw SwiftSDKError.quarantineAttributePresent(bundlePath: bundlePath)
256256
}
257257
#endif
258258

@@ -268,7 +268,7 @@ public struct SwiftSDKBundle {
268268
fileSystem.isDirectory(unpackedBundlePath),
269269
let bundleName = unpackedBundlePath.components.last
270270
else {
271-
throw DestinationError.pathIsNotDirectory(bundlePath)
271+
throw SwiftSDKError.pathIsNotDirectory(bundlePath)
272272
}
273273

274274
let installedBundlePath = destinationsDirectory.appending(component: bundleName)
@@ -289,7 +289,7 @@ public struct SwiftSDKBundle {
289289
for installedBundle in installedBundles {
290290
for artifactID in installedBundle.artifacts.keys {
291291
guard !newArtifactIDs.contains(artifactID) else {
292-
throw DestinationError.destinationArtifactAlreadyInstalled(
292+
throw SwiftSDKError.swiftSDKArtifactAlreadyInstalled(
293293
installedBundleName: installedBundle.name,
294294
newBundleName: validatedBundle.name,
295295
artifactID: artifactID
@@ -302,12 +302,12 @@ public struct SwiftSDKBundle {
302302
}
303303

304304
/// Parses metadata of an `.artifactbundle` and validates it as a bundle containing
305-
/// cross-compilation destinations.
305+
/// cross-compilation Swift SDKs.
306306
/// - Parameters:
307307
/// - bundlePath: path to the bundle root directory.
308308
/// - fileSystem: filesystem containing the bundle.
309309
/// - observabilityScope: observability scope to log validation warnings.
310-
/// - Returns: Validated `DestinationsBundle` containing validated `Destination` values for
310+
/// - Returns: Validated `SwiftSDKBundle` containing validated `Destination` values for
311311
/// each artifact and its variants.
312312
private static func parseAndValidate(
313313
bundlePath: AbsolutePath,
@@ -319,7 +319,7 @@ public struct SwiftSDKBundle {
319319
rootPath: bundlePath
320320
)
321321

322-
return try parsedManifest.validateDestinationBundle(
322+
return try parsedManifest.validateSwiftSDKBundle(
323323
bundlePath: bundlePath,
324324
fileSystem: fileSystem,
325325
observabilityScope: observabilityScope
@@ -328,7 +328,7 @@ public struct SwiftSDKBundle {
328328
}
329329

330330
extension ArtifactsArchiveMetadata {
331-
fileprivate func validateDestinationBundle(
331+
fileprivate func validateSwiftSDKBundle(
332332
bundlePath: AbsolutePath,
333333
fileSystem: FileSystem,
334334
observabilityScope: ObservabilityScope

0 commit comments

Comments
 (0)