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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2025 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
//
//===----------------------------------------------------------------------===//
public import SWBProtocol
public import SWBUtil
public import SWBMacro
public typealias ProvisioningStyle = SWBProtocol.ProvisioningStyle
public enum TargetType: Sendable {
/// Native targets are ones the build system actually knows how to construct.
case standard
/// Aggregate targets generally behave like the most-generic kind of target.
case aggregate
/// External targets behave like aggregate ones, but have some additional custom behaviors.
case external
/// Package product targets have no code themselves, but behave like aggregate targets which do, however, define a product (a virtual aggregate). See the class for more information.
case packageProduct
}
public final class CustomTask: ProjectModelItem, Sendable {
public let commandLine: [MacroStringExpression]
public let environment: [(MacroStringExpression, MacroStringExpression)]
public let workingDirectory: MacroStringExpression?
public let executionDescription: MacroStringExpression
public let inputFilePaths: [MacroStringExpression]
public let outputFilePaths: [MacroStringExpression]
public let enableSandboxing: Bool
public let preparesForIndexing: Bool
init(_ model: SWBProtocol.CustomTask, _ pifLoader: PIFLoader) {
self.commandLine = model.commandLine.map { pifLoader.userNamespace.parseString($0) }
self.environment = model.environment.map { (pifLoader.userNamespace.parseString($0.0), pifLoader.userNamespace.parseString($0.1)) }
self.workingDirectory = pifLoader.userNamespace.parseString(model.workingDirectory)
self.executionDescription = pifLoader.userNamespace.parseString(model.executionDescription)
self.inputFilePaths = model.inputFilePaths.map { pifLoader.userNamespace.parseString($0) }
self.outputFilePaths = model.outputFilePaths.map { pifLoader.userNamespace.parseString($0) }
self.enableSandboxing = model.enableSandboxing
self.preparesForIndexing = model.preparesForIndexing
}
init(fromDictionary pifDict: ProjectModelItemPIF, withPIFLoader pifLoader: PIFLoader) throws {
self.commandLine = try Self.parseValueForKeyAsArrayOfStrings(PIFKey_CustomTask_commandLine, pifDict: pifDict).map { pifLoader.userNamespace.parseString($0) }
var environment: [(MacroStringExpression, MacroStringExpression)] = []
let environmentItems = try Self.parseValueForKeyAsArrayOfPropertyListItems(PIFKey_CustomTask_environment, pifDict: pifDict)
for environmentItemPL in environmentItems {
guard let keyAndValuePLs = environmentItemPL.arrayValue else {
throw PIFParsingError.incorrectTypeInArray(keyName: PIFKey_CustomTask_environment, objectType: Self.self, expectedType: "Array")
}
guard keyAndValuePLs.count == 2 else {
throw StubError.error("Expected a key/value pair when deserializing environment")
}
guard let key = keyAndValuePLs[0].stringValue, let value = keyAndValuePLs[1].stringValue else {
throw PIFParsingError.incorrectTypeInArray(keyName: PIFKey_CustomTask_environment, objectType: Self.self, expectedType: "String")
}
environment.append((pifLoader.userNamespace.parseString(key), pifLoader.userNamespace.parseString(value)))
}
self.environment = environment
self.workingDirectory = try Self.parseOptionalValueForKeyAsString(PIFKey_CustomTask_workingDirectory, pifDict: pifDict).map { pifLoader.userNamespace.parseString($0) }
self.executionDescription = try pifLoader.userNamespace.parseString(Self.parseValueForKeyAsString(PIFKey_CustomTask_executionDescription, pifDict: pifDict))
self.inputFilePaths = try Self.parseValueForKeyAsArrayOfStrings(PIFKey_CustomTask_inputFilePaths, pifDict: pifDict).map { pifLoader.userNamespace.parseString($0) }
self.outputFilePaths = try Self.parseValueForKeyAsArrayOfStrings(PIFKey_CustomTask_outputFilePaths, pifDict: pifDict).map { pifLoader.userNamespace.parseString($0) }
self.enableSandboxing = try Self.parseValueForKeyAsBool(PIFKey_CustomTask_enableSandboxing, pifDict: pifDict)
self.preparesForIndexing = try Self.parseValueForKeyAsBool(PIFKey_CustomTask_preparesForIndexing, pifDict: pifDict)
}
}
// MARK: Target Dependency Info
public final class TargetDependency: ProjectModelItem, Encodable, Sendable
{
/// The GUID that maps back to the target GUID.
public let guid: String
/// The name of the target dependency, for display purposes in diagnostics.
public let name: String?
private enum CodingKeys: String, CodingKey {
case guid
case name
case platformFilters
}
/// The set of the platform filters for the target dependency.
public let platformFilters: Set<PlatformFilter>
public init(guid: String, name: String?, platformFilters: Set<PlatformFilter> = []) {
self.guid = guid
self.name = name
self.platformFilters = platformFilters
}
init(_ model: SWBProtocol.TargetDependency, _ pifLoader: PIFLoader) {
self.guid = model.guid
self.name = model.name
self.platformFilters = Set(model.platformFilters.map{ SWBCore.PlatformFilter($0, pifLoader) })
}
init(fromDictionary pifDict: ProjectModelItemPIF, withPIFLoader pifLoader: PIFLoader) throws {
// FIXME: Falling back to an empty string GUID does not seem correct, but this can happen for dependencies on targets from missing project references. Should we instead allow `guid` to be nil to represent this case?
self.guid = try Self.parseOptionalValueForKeyAsString(PIFKey_guid, pifDict: pifDict) ?? ""
self.name = try Self.parseOptionalValueForKeyAsString(PIFKey_name, pifDict: pifDict)
self.platformFilters = Set(try Self.parseOptionalValueForKeyAsArrayOfProjectModelItems(PIFKey_platformFilters, pifDict: pifDict, pifLoader: pifLoader, construct: {
try PlatformFilter(fromDictionary: $0, withPIFLoader: pifLoader)
}) ?? [])
}
}
// MARK: Target abstract class
/// The Target abstract class defines properties common to all types of targets.
public class Target: ProjectModelItem, PIFObject, Hashable, Encodable, @unchecked Sendable
{
static func referencedObjects(for data: EncodedPIFValue) -> [PIFObjectReference] {
return []
}
/// Parses a ProjectModelItemPIF dictionary as an object of the appropriate subclass of Target.
static func construct(from data: EncodedPIFValue, signature: PIFObject.Signature, loader: PIFLoader ) throws -> Self {
switch data {
case .json(let data):
return try construct(from: data, signature: signature, loader: loader)
case .binary(let data):
let deserializer = MsgPackDeserializer(data)
let model: SWBProtocol.Target = try deserializer.deserialize()
return try create(model, loader, signature: signature)
}
}
public static func create(_ model: SWBProtocol.Target, _ pifLoader: PIFLoader, signature: String) throws -> Self {
// Workaround inability to express protocol completely.
func autocast<T>(_ some: Target) -> T {
return some as! T
}
switch model {
case let model as SWBProtocol.AggregateTarget: return autocast(AggregateTarget(model, pifLoader, signature: signature))
case let model as SWBProtocol.ExternalTarget: return autocast(ExternalTarget(model, pifLoader, signature: signature))
case let model as SWBProtocol.PackageProductTarget: return autocast(PackageProductTarget(model, pifLoader, signature: signature))
case let model as SWBProtocol.StandardTarget: return try autocast(StandardTarget(model, pifLoader, signature: signature))
default:
fatalError("unexpected model: \(model)")
}
}
/// The PIFObject type name
static let pifType = PIFObjectType.target
/// A unique signature for the instance.
public let signature: String
/// A stable identifier for the target.
///
/// This is guaranteed to be unique within a single workspace.
public let guid: String
public let name: String
public var type: TargetType { preconditionFailure( "\(Swift.type(of: self)) should never be asked directly for its type" ) }
public let buildConfigurations: [BuildConfiguration]
public let hasImpartedBuildProperties: Bool
/// An optional reference to a target which can build a dynamically linked variant of the same product.
public let dynamicTargetVariantGuid: String?
/// Some targets (e.g. third-party macros) may require approval by the user.
public let approvedByUser: Bool
/// Custom tasks associated with the target, which may be defined by the user or a higher-level API client.
public let customTasks: [CustomTask]
/// The list of GUIDs and `PlatformFilters` for all dependencies.
///
/// This list may include dependencies which are not part of the project this target is a part of, and can only be resolved via the workspace.
public let dependencies: [TargetDependency]
private enum CodingKeys: String, CodingKey {
case signature
case guid
case name
case buildConfigurations
case dependencies
case dynamicTargetVariantGuid
}
init(_ model: SWBProtocol.Target, _ pifLoader: PIFLoader, signature: String, errors: [String] = [], warnings: [String] = []) {
self.signature = signature
self.guid = model.guid
self.name = model.name
self.buildConfigurations = model.buildConfigurations.map{ BuildConfiguration($0, pifLoader) }
self.customTasks = model.customTasks.map { CustomTask($0, pifLoader) }
self.dependencies = model.dependencies.map{ TargetDependency($0, pifLoader) }
self.dynamicTargetVariantGuid = model.dynamicTargetVariantGuid
self.approvedByUser = model.approvedByUser
self.hasImpartedBuildProperties = buildConfigurations.filter { !$0.impartedBuildProperties.isEmpty }.isEmpty
self.errors = errors
self.warnings = warnings
}
/// Errors about the configuration of the target.
public let errors: [String]
/// Warnings about the configuration of the target.
public let warnings: [String]
init(fromDictionary pifDict: ProjectModelItemPIF, signature: String, withPIFLoader pifLoader: PIFLoader, errors: [String] = [], warnings: [String] = []) throws {
self.signature = signature
// The GUID is required.
guid = try Self.parseValueForKeyAsString(PIFKey_guid, pifDict: pifDict)
// The name is required.
name = try Self.parseValueForKeyAsString(PIFKey_name, pifDict: pifDict)
// The list of build configurations is required.
buildConfigurations = try Self.parseValueForKeyAsArrayOfProjectModelItems(PIFKey_buildConfigurations, pifDict: pifDict, pifLoader: pifLoader, construct: { try BuildConfiguration(fromDictionary: $0, withPIFLoader: pifLoader) })
customTasks = try Self.parseOptionalValueForKeyAsArrayOfProjectModelItems(PIFKey_Target_customTasks, pifDict: pifDict, pifLoader: pifLoader, construct: { try CustomTask(fromDictionary: $0, withPIFLoader: pifLoader) }) ?? []
// The list of dependencies is required.
//
// The target dependencies are resolved lazily (see `TargetDependencyResolver`) since they may cross target (and even project) boundaries.
dependencies = try Self.parseValueForKeyAsArrayOfProjectModelItems(PIFKey_Target_dependencies, pifDict: pifDict, pifLoader: pifLoader, construct: {
return try TargetDependency(fromDictionary: $0, withPIFLoader: pifLoader)
})
dynamicTargetVariantGuid = try Self.parseOptionalValueForKeyAsString(PIFKey_Target_dynamicTargetVariantGuid, pifDict: pifDict)
approvedByUser = try Self.parseValueForKeyAsBool(PIFKey_Target_approvedByUser, pifDict: pifDict, defaultValue: true)
hasImpartedBuildProperties = buildConfigurations.filter { !$0.impartedBuildProperties.isEmpty }.isEmpty
self.errors = errors
self.warnings = warnings
}
/// Parses a ProjectModelItemPIF dictionary as an object of the appropriate subclass of Target.
static func construct(from data: ProjectModelItemPIF, signature: PIFObject.Signature, loader: PIFLoader ) throws -> Self
{
// Workaround inability to express protocol completely.
func autocast<T>(_ some: Target) -> T {
return some as! T
}
// Delegate to protocol-based representation, if in use.
if let data = try parseOptionalValueForKeyAsByteString("data", pifDict: data) {
let deserializer = MsgPackDeserializer(data)
let model: SWBProtocol.Target = try deserializer.deserialize()
return try autocast(Target.create(model, loader, signature: signature))
}
// Get the type of the reference. This is required, so if there isn't one then it will die.
switch try parseValueForKeyAsStringEnum(PIFKey_type, pifDict: data) as PIFTargetTypeValue {
case .standard:
return try autocast(StandardTarget(fromDictionary: data, signature: signature, withPIFLoader: loader))
case .aggregate:
return try autocast(AggregateTarget(fromDictionary: data, signature: signature, withPIFLoader: loader))
case .external:
return try autocast(ExternalTarget(fromDictionary: data, signature: signature, withPIFLoader: loader))
case .packageProduct:
return try autocast(PackageProductTarget(fromDictionary: data, signature: signature, withPIFLoader: loader))
}
}
public var description: String
{
return "\(Swift.type(of: self))<\(guid):\(name)>"
}
/// Get the named configuration.
//
// FIXME: This code is currently duplicated on the project. We should perhaps have an explicit BuildConfigurationList object, like Xcode, to hold these.
func getConfiguration(_ name: String) -> BuildConfiguration? {
for config in buildConfigurations {
if config.name == name {
return config
}
}
return nil
}
/// Get the effective configuration to use for the given name.
public func getEffectiveConfiguration(_ name: String?, defaultConfigurationName: String) -> BuildConfiguration? {
// Return the named config, if present.
if let name {
if let config = getConfiguration(name) {
return config
}
}
// Otherwise, return the configuration matching the default configuration name.
if let config = getConfiguration(defaultConfigurationName) {
return config
}
// Otherwise, return any configuration.
return buildConfigurations.first
}
/// Returns a hash value based on the identity of the object.
public func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self))
}
public static func ==(lhs: Target, rhs: Target) -> Bool {
return lhs === rhs
}
}
// MARK: BuildPhaseTarget class
/// A BuildPhaseTarget is a kind of target which can contain build phases.
public class BuildPhaseTarget: Target, @unchecked Sendable
{
/// List of build phases in the target.
public let buildPhases: [BuildPhase]
/// The canonical sources build phase in the target, if there is one. There should only be one in a target; if there is more than one, then a warning will be emitted and the project might not build as expected.
public let sourcesBuildPhase: SourcesBuildPhase?
/// The canonical frameworks build phase in the target, if there is one. There should only be one in a target; if there is more than one, then a warning will be emitted and the project might not build as expected.
public let frameworksBuildPhase: FrameworksBuildPhase?
/// The canonical headers build phase in the target, if there is one. There should only be one in a target; if there is more than one, then a warning will be emitted and the project might not build as expected.
public let headersBuildPhase: HeadersBuildPhase?
/// The canonical resources build phase in the target, if there is one. There should only be one in a target; if there is more than one, then a warning will be emitted and the project might not build as expected.
public let resourcesBuildPhase: ResourcesBuildPhase?
init(_ model: SWBProtocol.BuildPhaseTarget, _ pifLoader: PIFLoader, signature: String) {
buildPhases = model.buildPhases.map{ BuildPhase.create($0, pifLoader) }
// Populate the convenience build phase properties.
var warnings: [String] = []
var sourcesBuildPhase: SourcesBuildPhase? = nil
var frameworksBuildPhase: FrameworksBuildPhase? = nil
var headersBuildPhase: HeadersBuildPhase? = nil
var resourcesBuildPhase: ResourcesBuildPhase? = nil
for buildPhase in self.buildPhases {
switch buildPhase {
case let sourcesPhase as SourcesBuildPhase:
if sourcesBuildPhase != nil {
warnings.append("target has multiple \(buildPhase.name) build phases, which may cause it to build incorrectly - all but one should be deleted")
}
sourcesBuildPhase = sourcesPhase
case let frameworksPhase as FrameworksBuildPhase:
if frameworksBuildPhase != nil {
warnings.append("target has multiple \(buildPhase.name) build phases, which may cause it to build incorrectly - all but one should be deleted")
}
frameworksBuildPhase = frameworksPhase
case let headersPhase as HeadersBuildPhase:
if headersBuildPhase != nil {
warnings.append("target has multiple \(buildPhase.name) build phases, which may cause it to build incorrectly - all but one should be deleted")
}
headersBuildPhase = headersPhase
case let resourcesPhase as ResourcesBuildPhase:
if resourcesBuildPhase != nil {
warnings.append("target has multiple \(buildPhase.name) build phases, which may cause it to build incorrectly - all but one should be deleted")
}
resourcesBuildPhase = resourcesPhase
default:
break
}
}
self.sourcesBuildPhase = sourcesBuildPhase
self.frameworksBuildPhase = frameworksBuildPhase
self.headersBuildPhase = headersBuildPhase
self.resourcesBuildPhase = resourcesBuildPhase
super.init(model, pifLoader, signature: signature, errors: [], warnings: warnings)
}
@_spi(Testing) public override init(fromDictionary pifDict: ProjectModelItemPIF, signature: String, withPIFLoader pifLoader: PIFLoader, errors: [String] = [], warnings: [String] = []) throws
{
// The list of build phases is required.
buildPhases = try Self.parseValueForKeyAsArrayOfPropertyListItems(PIFKey_Target_buildPhases, pifDict: pifDict).map { plItem in
guard case .plDict(let dictValue) = plItem else {
throw PIFParsingError.incorrectTypeInArray(keyName: PIFKey_Target_buildPhases, objectType: Self.self, expectedType: "Dictionary")
}
// Delegate to protocol-based representation, if in use.
if let data = try Self.parseOptionalValueForKeyAsByteString("data", pifDict: dictValue) {
let deserializer = MsgPackDeserializer(data)
let buildPhase: SWBProtocol.BuildPhase = try deserializer.deserialize()
return BuildPhase.create(buildPhase, pifLoader)
} else {
return try BuildPhase.parsePIFDictAsBuildPhase(dictValue, pifLoader: pifLoader)
}
}
// Populate the convenience build phase properties.
var newWarnings: [String] = []
var sourcesBuildPhase: SourcesBuildPhase? = nil
var frameworksBuildPhase: FrameworksBuildPhase? = nil
var headersBuildPhase: HeadersBuildPhase? = nil
var resourcesBuildPhase: ResourcesBuildPhase? = nil
for buildPhase in self.buildPhases
{
switch buildPhase
{
case let sourcesPhase as SourcesBuildPhase:
if sourcesBuildPhase != nil {
newWarnings.append("target has multiple \(buildPhase.name) build phases, which may cause it to build incorrectly - all but one should be deleted")
}
sourcesBuildPhase = sourcesPhase
case let frameworksPhase as FrameworksBuildPhase:
if frameworksBuildPhase != nil {
newWarnings.append("target has multiple \(buildPhase.name) build phases, which may cause it to build incorrectly - all but one should be deleted")
}
frameworksBuildPhase = frameworksPhase
case let headersPhase as HeadersBuildPhase:
if headersBuildPhase != nil {
newWarnings.append("target has multiple \(buildPhase.name) build phases, which may cause it to build incorrectly - all but one should be deleted")
}
headersBuildPhase = headersPhase
case let resourcesPhase as ResourcesBuildPhase:
if resourcesBuildPhase != nil {
newWarnings.append("target has multiple \(buildPhase.name) build phases, which may cause it to build incorrectly - all but one should be deleted")
}
resourcesBuildPhase = resourcesPhase
default:
break
}
}
self.sourcesBuildPhase = sourcesBuildPhase
self.frameworksBuildPhase = frameworksBuildPhase
self.headersBuildPhase = headersBuildPhase
self.resourcesBuildPhase = resourcesBuildPhase
try super.init(fromDictionary: pifDict, signature: signature, withPIFLoader: pifLoader, errors: errors, warnings: warnings + newWarnings)
}
}
// MARK: StandardTarget class
/// Source data needed to compute provisioning task inputs for a target+configuration pair.
public typealias ProvisioningSourceData = SWBProtocol.ProvisioningSourceData
/// A StandardTarget is the most common type of target: A target which has build phases describing its input files, and which generates a product.
public final class StandardTarget: BuildPhaseTarget, @unchecked Sendable
{
public enum SourceCodeLanguage: CustomStringConvertible, Sendable {
case undefined
case swift
case c
case objectiveC
case cPlusPlus
case objectiveCPlusPlus
case other(String)
/// Return an appropriate source code language value for the given string. If the string optional is nil, or is an unrecognized value, then `undefined` will be returned.
static func from(string: String?) -> SourceCodeLanguage {
guard let string else { return .undefined }
switch string {
case "Xcode.SourceCodeLanguage.Swift":
return .swift
case "Xcode.SourceCodeLanguage.C":
return .c
case "Xcode.SourceCodeLanguage.Objective-C":
return .objectiveC
case "Xcode.SourceCodeLanguage.C-Plus-Plus":
return .cPlusPlus
case "Xcode.SourceCodeLanguage.Objective-C-Plus-Plus":
return .objectiveCPlusPlus
default:
return .other(string)
}
}
public var description: String {
switch self {
case .undefined: return ""
case .swift: return "Xcode.SourceCodeLanguage.Swift"
case .c: return "Xcode.SourceCodeLanguage.C"
case .objectiveC: return "Xcode.SourceCodeLanguage.Objective-C"
case .cPlusPlus: return "Xcode.SourceCodeLanguage.C-Plus-Plus"
case .objectiveCPlusPlus: return "Xcode.SourceCodeLanguage.Objective-C-Plus-Plus"
case .other(let string): return string
}
}
}
public override var type: TargetType { return TargetType.standard }
public let buildRules: [BuildRule]
public let productTypeIdentifier: String
public let productReference: ProductReference
/// Indicates if this target is derived from a Swift package.
public let isPackageTarget: Bool
/// The path to the baselines for performance tests. This will be nil for targets for which it isn't useful.
public let performanceTestsBaselinesPath: Path?
/// The predominant source code language in this target. This language is computed by Xcode and passed to us in the PIF.
public let predominantSourceCodeLanguage: SourceCodeLanguage
/// The class prefix used in this target
public let classPrefix: String
private let _provisioningSourceData: UnsafeDelayedInitializationSendableWrapper<[ProvisioningSourceData]> = .init()
var provisioningSourceData: [ProvisioningSourceData] {
_provisioningSourceData.value
}
init(_ model: SWBProtocol.StandardTarget, _ pifLoader: PIFLoader, signature: String) throws {
buildRules = model.buildRules.map{ BuildRule($0, pifLoader) }
productTypeIdentifier = model.productTypeIdentifier
productReference = try ProductReference(model.productReference, pifLoader)
isPackageTarget = model.isPackageTarget
performanceTestsBaselinesPath = model.performanceTestsBaselinesPath != nil ? Path(model.performanceTestsBaselinesPath!) : nil
predominantSourceCodeLanguage = SourceCodeLanguage.from(string: model.predominantSourceCodeLanguage)
var provisioningSourceData = model.provisioningSourceData
classPrefix = model.classPrefix ?? ""
super.init(model, pifLoader, signature: signature)
// Set our product reference's backpointer to ourself.
productReference.target = self
try StandardTarget.fixupProvisioningSourceData(&provisioningSourceData, name: name, buildConfigurations: buildConfigurations)
_provisioningSourceData.initialize(to: provisioningSourceData)
}
@_spi(Testing) public override init(fromDictionary pifDict: ProjectModelItemPIF, signature: String, withPIFLoader pifLoader: PIFLoader, errors: [String] = [], warnings: [String] = []) throws
{
// The product type identifier is required.
productTypeIdentifier = try Self.parseValueForKeyAsString(PIFKey_Target_productTypeIdentifier, pifDict: pifDict)
// The product reference is required.
//
// NOTE: It is important to understand what is happening here. This will be the *actual* product reference model item, not the one present in the project group tree. Therefore, there exist some references which are completely outside the group tree.
productReference = try Self.parseValueForKeyAsProjectModelItem(PIFKey_Target_productReference, pifDict: pifDict, pifLoader: pifLoader, construct: { try ProductReference(fromDictionary: $0, withPIFLoader: pifLoader) })
// The list of build rules is required.
buildRules = try Self.parseValueForKeyAsArrayOfProjectModelItems(PIFKey_Target_buildRules, pifDict: pifDict, pifLoader: pifLoader, construct: { try BuildRule(fromDictionary: $0, withPIFLoader: pifLoader) })
isPackageTarget = try Self.parseValueForKeyAsBool(PIFKey_Target_isPackageTarget, pifDict: pifDict)
performanceTestsBaselinesPath = try Self.parseOptionalValueForKeyAsString(PIFKey_Target_performanceTestsBaselinesPath, pifDict: pifDict).map { Path($0) }
predominantSourceCodeLanguage = try SourceCodeLanguage.from(string: Self.parseOptionalValueForKeyAsString(PIFKey_Target_predominantSourceCodeLanguage, pifDict: pifDict))
classPrefix = try Self.parseOptionalValueForKeyAsString(PIFKey_Project_classPrefix, pifDict: pifDict) ?? ""
// The list of provisioning source data dictionaries is optional. For any configurations for which we were not given a provisioning source data object we will create a default one.
var provisioningSourceData = [ProvisioningSourceData]()
if let sourceDataDicts = try Self.parseOptionalValueForKeyAsArrayOfPropertyListItems(PIFKey_Target_provisioningSourceData, pifDict: pifDict) {
provisioningSourceData = try sourceDataDicts.map { try PropertyList.decode(ProvisioningSourceData.self, from: $0) }
}
try super.init(fromDictionary: pifDict, signature: signature, withPIFLoader: pifLoader, errors: errors, warnings: warnings)
// Set our product reference's backpointer to ourself.
productReference.target = self
try StandardTarget.fixupProvisioningSourceData(&provisioningSourceData, name: name, buildConfigurations: buildConfigurations)
_provisioningSourceData.initialize(to: provisioningSourceData)
}
private static func fixupProvisioningSourceData(_ provisioningSourceData: inout [ProvisioningSourceData], name: String, buildConfigurations: [BuildConfiguration]) throws {
// Make sure all of our provisioning source data objects match existing configurations, and that there are no duplicate source data structs for a configuration.
var configurationNames = Set<String>(buildConfigurations.map({ $0.name }))
var foundConfigurationNames = Set<String>()
for sourceData in provisioningSourceData {
guard !foundConfigurationNames.contains(sourceData.configurationName) else {
throw PIFParsingError.custom("Target '\(name)' has multiple provisioning source data for configuration '\(sourceData.configurationName)'")
}
guard configurationNames.contains(sourceData.configurationName) else {
throw PIFParsingError.custom("Target '\(name)' has provisioning source data for configuration '\(sourceData.configurationName)' but no configuration of that name")
}
configurationNames.remove(sourceData.configurationName)
foundConfigurationNames.insert(sourceData.configurationName)
}
// If any configurations didn't have provisioning source data objects, then create default ones for those configurations.
for configurationName in configurationNames
{
let sourceData = ProvisioningSourceData(configurationName: configurationName, provisioningStyle: .automatic, bundleIdentifierFromInfoPlist: "")
provisioningSourceData.append(sourceData)
}
}
/// Get the provisioning source data for the given configuration name.
/// - scope: An optional macro evaluation scope to use which can override certain provisioning data with build settings.
/// - returns: The relevant provisioning source data, or `nil` if there is no provisioning data for the given configuration name.
public func provisioningSourceData(for configurationName: String, scope: MacroEvaluationScope? = nil) -> ProvisioningSourceData? {
for sourceData in provisioningSourceData {
if sourceData.configurationName == configurationName {
// Override the provisioning style using $(CODE_SIGN_STYLE), if defined.
// FIXME: If $(CODE_SIGN_STYLE) evaluates to an unexpected value, we should emit an error somewhere.
if let codeSignStyleValue = scope?.evaluate(BuiltinMacros.CODE_SIGN_STYLE), !codeSignStyleValue.isEmpty, let codeSignStyle = ProvisioningStyle.fromString(codeSignStyleValue) {
return ProvisioningSourceData(configurationName: sourceData.configurationName, provisioningStyle: codeSignStyle, bundleIdentifierFromInfoPlist: sourceData.bundleIdentifierFromInfoPlist)
}
return sourceData
}
}
return nil
}
}
// MARK: AggregateTarget class
/// An AggregateTarget is a special kind of target primarily intended to group together other targets it depends on, and which does not have a defined product. However, it may also have build phases, which will be run after all of its dependencies have finished building.
public final class AggregateTarget: BuildPhaseTarget, @unchecked Sendable
{
public override var type: TargetType { return TargetType.aggregate }
}
// MARK: PackageProductTarget class
/// A PackageProductTarget is a custom target used by the Swift package manager to encapsulate the semantics of products.
///
/// This target is currently only expected to have target dependencies and an optional frameworks build phase, which should reference other (static library) targets package product targets.
///
/// The behavior of this target is "as if" the dependencies of the package pass through to downstream things which link the target.
public final class PackageProductTarget: Target, @unchecked Sendable
{
public override var type: TargetType { return TargetType.packageProduct }
/// The frameworks build phase which encodes the link dependencies.
public let frameworksBuildPhase: FrameworksBuildPhase?
/// The synthesized product reference.
public let productReference: ProductReference
init(_ model: SWBProtocol.PackageProductTarget, _ pifLoader: PIFLoader, signature: String) {
self.frameworksBuildPhase = model.frameworksBuildPhase.map{ BuildPhase.create($0, pifLoader) as! FrameworksBuildPhase }
self.productReference = ProductReference(guid: "\(model.guid):ProductReference", name: model.name)
super.init(model, pifLoader, signature: signature)
self.productReference.target = self
}
@_spi(Testing) public override init(fromDictionary pifDict: ProjectModelItemPIF, signature: String, withPIFLoader pifLoader: PIFLoader, errors: [String] = [], warnings: [String] = []) throws {
self.frameworksBuildPhase = try Self.parseOptionalValueForKeyAsPIFDictionary(PIFKey_Target_frameworksBuildPhase, pifDict: pifDict).map { try BuildPhase.parsePIFDictAsBuildPhase($0, pifLoader: pifLoader) as? FrameworksBuildPhase } ?? nil
self.productReference = try ProductReference(guid: "\(Self.parseValueForKeyAsString(PIFKey_guid, pifDict: pifDict)):ProductReference", name: Self.parseValueForKeyAsString(PIFKey_name, pifDict: pifDict))
try super.init(fromDictionary: pifDict, signature: signature, withPIFLoader: pifLoader, errors: errors, warnings: warnings)
}
}
// MARK: ExternalTarget class
/// An ExternalTarget represents the use of an external build system (most commonly, but not limited to, make). It is very different from other kinds of targets in that it has no build phases, and does have a defined product.
public final class ExternalTarget: Target, @unchecked Sendable
{
public override var type: TargetType { return TargetType.external }
public let toolPath: MacroStringExpression
public let arguments: MacroStringListExpression
public let workingDirectory: MacroStringExpression
public let passBuildSettingsInEnvironment: Bool
init(_ model: SWBProtocol.ExternalTarget, _ pifLoader: PIFLoader, signature: String) {
toolPath = pifLoader.userNamespace.parseString(model.toolPath)
arguments = pifLoader.userNamespace.parseStringList(model.arguments)
workingDirectory = pifLoader.userNamespace.parseString(model.workingDirectory)
passBuildSettingsInEnvironment = model.passBuildSettingsInEnvironment
super.init(model, pifLoader, signature: signature)
}
@_spi(Testing) public override init(fromDictionary pifDict: ProjectModelItemPIF, signature: String, withPIFLoader pifLoader: PIFLoader, errors: [String] = [], warnings: [String] = []) throws
{
// The tool path is required.
toolPath = try pifLoader.userNamespace.parseString(Self.parseValueForKeyAsString(PIFKey_ExternalTarget_toolPath, pifDict: pifDict))
// The arguments string is required.
arguments = try pifLoader.userNamespace.parseStringList(Self.parseValueForKeyAsString(PIFKey_ExternalTarget_arguments, pifDict: pifDict))
// The working directory is required.
workingDirectory = try pifLoader.userNamespace.parseString(Self.parseValueForKeyAsString(PIFKey_ExternalTarget_workingDirectory, pifDict: pifDict))
// The flag to indicate whether to pass build settings to the tool in the environment is required.
passBuildSettingsInEnvironment = try Self.parseValueForKeyAsBool(PIFKey_ExternalTarget_passBuildSettingsInEnvironment, pifDict: pifDict)
try super.init(fromDictionary: pifDict, signature: signature, withPIFLoader: pifLoader, errors: errors, warnings: warnings)
}
}
|