1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Foundation
typealias WireInput = HostToPluginMessage.InputContext
/// Deserializer for constructing a plugin input from the wire representation
/// received from SwiftPM, which consists of a set of flat lists of entities,
/// referenced by array index in all cross-references. The deserialized data
/// structure forms a directed acyclic graph. This information is provided to
/// the plugin in the `PluginContext` struct.
internal struct PluginContextDeserializer {
let wireInput: WireInput
var urlsById: [WireInput.URL.Id: URL] = [:]
var packagesById: [WireInput.Package.Id: Package] = [:]
var productsById: [WireInput.Product.Id: Product] = [:]
var targetsById: [WireInput.Target.Id: Target] = [:]
/// Initializes the deserializer with the given wire input.
init(_ input: WireInput) {
self.wireInput = input
}
/// Returns the `URL` that corresponds to the given ID (a small integer),
/// or throws an error if the ID is invalid. The URL is deserialized on-
/// demand if it hasn't already been deserialized.
mutating func url(for id: WireInput.URL.Id) throws -> URL {
if let path = urlsById[id] { return path }
guard id < wireInput.paths.count else {
throw PluginDeserializationError.malformedInputJSON("invalid URL id (\(id))")
}
// Compose a path based on an optional base path and a subpath.
let wirePath = wireInput.paths[id]
let basePath = try wireInput.paths[id].baseURLId.map{ try self.url(for: $0) } ?? URL(fileURLWithPath: "/")
let path = basePath.appendingPathComponent(wirePath.subpath)
// Store it for the next look up.
urlsById[id] = path
return path
}
/// Returns the `Target` that corresponds to the given ID (a small integer),
/// or throws an error if the ID is invalid. The module is deserialized on-
/// demand if it hasn't already been deserialized.
mutating func target(for id: WireInput.Target.Id, pluginGeneratedSources: [URL] = [], pluginGeneratedResources: [URL] = []) throws -> Target {
if let target = targetsById[id],
target.sourceModule?.pluginGeneratedSources.count == pluginGeneratedSources.count,
target.sourceModule?.pluginGeneratedResources.count == pluginGeneratedResources.count {
return target
}
guard id < wireInput.targets.count else {
throw PluginDeserializationError.malformedInputJSON("invalid target id (\(id))")
}
let wireTarget = wireInput.targets[id]
let dependencies: [TargetDependency] = try wireTarget.dependencies.map {
switch $0 {
case .target(let targetId):
let target = try self.target(for: targetId)
return .target(target)
case .product(let productId):
let product = try self.product(for: productId)
return .product(product)
}
}
let directory = try self.url(for: wireTarget.directoryId)
let target: Target
switch wireTarget.info {
case let .swiftSourceModuleInfo(moduleName, kind, sourceFiles, compilationConditions, linkedLibraries, linkedFrameworks):
let sourceFiles = FileList(try sourceFiles.map {
let path = try self.url(for: $0.basePathId).appendingPathComponent($0.name)
let type: FileType
switch $0.type {
case .source:
type = .source
case .header:
type = .header
case .resource:
type = .resource
case .unknown:
type = .unknown
}
return File(path: Path(url: path), url: path, type: type)
})
target = SwiftSourceModuleTarget(
id: String(id),
name: wireTarget.name,
kind: .init(kind),
directory: Path(url: directory),
directoryURL: directory,
dependencies: dependencies,
moduleName: moduleName,
sourceFiles: sourceFiles,
compilationConditions: compilationConditions,
linkedLibraries: linkedLibraries,
linkedFrameworks: linkedFrameworks,
pluginGeneratedSources: pluginGeneratedSources,
pluginGeneratedResources: pluginGeneratedResources
)
case let .clangSourceModuleInfo(moduleName, kind, sourceFiles, preprocessorDefinitions, headerSearchPaths, publicHeadersDirId, linkedLibraries, linkedFrameworks):
let publicHeadersDir = try publicHeadersDirId.map { try self.url(for: $0) }
let sourceFiles = FileList(try sourceFiles.map {
let path = try self.url(for: $0.basePathId).appendingPathComponent($0.name)
let type: FileType
switch $0.type {
case .source:
type = .source
case .header:
type = .header
case .resource:
type = .resource
case .unknown:
type = .unknown
}
return File(path: Path(url: path), url: path, type: type)
})
target = ClangSourceModuleTarget(
id: String(id),
name: wireTarget.name,
kind: .init(kind),
directory: Path(url: directory),
directoryURL: directory,
dependencies: dependencies,
moduleName: moduleName,
sourceFiles: sourceFiles,
preprocessorDefinitions: preprocessorDefinitions,
headerSearchPaths: headerSearchPaths,
publicHeadersDirectory: publicHeadersDir.map { .init(url: $0) },
publicHeadersDirectoryURL: publicHeadersDir,
linkedLibraries: linkedLibraries,
linkedFrameworks: linkedFrameworks,
pluginGeneratedSources: pluginGeneratedSources,
pluginGeneratedResources: pluginGeneratedResources
)
case let .binaryArtifactInfo(kind, origin, artifactId):
let artifact = try self.url(for: artifactId)
let artifactKind: BinaryArtifactTarget.Kind
switch kind {
case .artifactsArchive:
artifactKind = .artifactsArchive
case .xcframework:
artifactKind = .xcframework
}
let artifactOrigin: BinaryArtifactTarget.Origin
switch origin {
case .local:
artifactOrigin = .local
case .remote(let url):
artifactOrigin = .remote(url: url)
}
target = BinaryArtifactTarget(
id: String(id),
name: wireTarget.name,
directory: Path(url: directory),
directoryURL: directory,
dependencies: dependencies,
kind: artifactKind,
origin: artifactOrigin,
artifact: Path(url: artifact),
artifactURL: artifact)
case let .systemLibraryInfo(pkgConfig, compilerFlags, linkerFlags):
target = SystemLibraryTarget(
id: String(id),
name: wireTarget.name,
directory: Path(url: directory),
directoryURL: directory,
dependencies: dependencies,
pkgConfig: pkgConfig,
compilerFlags: compilerFlags,
linkerFlags: linkerFlags)
}
targetsById[id] = target
return target
}
/// Returns the `Product` that corresponds to the given ID (a small integer),
/// or throws an error if the ID is invalid. The product is deserialized on-
/// demand if it hasn't already been deserialized.
mutating func product(for id: WireInput.Product.Id) throws -> Product {
if let product = productsById[id] { return product }
guard id < wireInput.products.count else {
throw PluginDeserializationError.malformedInputJSON("invalid product id (\(id))")
}
let wireProduct = wireInput.products[id]
let targets: [Target] = try wireProduct.targetIds.map{ try self.target(for: $0) }
let product: Product
switch wireProduct.info {
case .executable(let mainTargetId):
let mainTarget = try self.target(for: mainTargetId)
product = ExecutableProduct(
id: String(id),
name: wireProduct.name,
targets: targets,
mainTarget: mainTarget)
case .library(let type):
let libraryKind: LibraryProduct.Kind
switch type {
case .static:
libraryKind = .static
case .dynamic:
libraryKind = .dynamic
case .automatic:
libraryKind = .automatic
}
product = LibraryProduct(
id: String(id),
name: wireProduct.name,
targets: targets,
kind: libraryKind)
}
productsById[id] = product
return product
}
/// Returns the `Package` that corresponds to the given ID (a small integer),
/// or throws an error if the ID is invalid. The package is deserialized on-
/// demand if it hasn't already been deserialized.
mutating func package(for id: WireInput.Product.Id) throws -> Package {
if let package = packagesById[id] { return package }
guard id < wireInput.packages.count else {
throw PluginDeserializationError.malformedInputJSON("invalid package id (\(id))") }
let wirePackage = wireInput.packages[id]
let directory = try self.url(for: wirePackage.directoryId)
let toolsVersion = ToolsVersion(
major: wirePackage.toolsVersion.major,
minor: wirePackage.toolsVersion.minor,
patch: wirePackage.toolsVersion.patch)
let dependencies: [PackageDependency] = try wirePackage.dependencies.map {
.init(package: try self.package(for: $0.packageId))
}
let products = try wirePackage.productIds.map { try self.product(for: $0) }
let targets = try wirePackage.targetIds.map { try self.target(for: $0) }
let origin : PackageOrigin = switch wirePackage.origin {
case .root:
.root
case .local(let pathId):
try .local(path: url(for: pathId).path)
case .repository(let url, let displayVersion, let scmRevision):
.repository(url: url, displayVersion: displayVersion, scmRevision: scmRevision)
case .registry(let identity, let displayVersion):
.registry(identity: identity, displayVersion: displayVersion)
}
let package = Package(
id: wirePackage.identity,
displayName: wirePackage.displayName,
directory: Path(url: directory),
directoryURL: directory,
origin: origin,
toolsVersion: toolsVersion,
dependencies: dependencies,
products: products,
targets: targets)
packagesById[id] = package
return package
}
}
fileprivate extension ModuleKind {
init(_ kind: WireInput.Target.TargetInfo.SourceModuleKind) {
switch kind {
case .generic:
self = .generic
case .executable:
self = .executable
case .snippet:
self = .snippet
case .test:
self = .test
case .macro:
self = .macro
}
}
}
|