Skip to content

Commit 424aade

Browse files
committed
Final cleanup
1 parent dbc539a commit 424aade

7 files changed

+74
-98
lines changed

Sources/SwiftBuildSupport/PIF.swift

+9-38
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,8 @@ import enum SwiftBuild.ProjectModel
2929
/// between builds which use different schemes or configurations), and can be
3030
/// incrementally updated by clients when something changes.
3131
public enum PIF {
32-
/// This is used as part of the signature for the high-level PIF objects, to ensure that changes to the PIF schema
33-
/// are represented by the objects which do not use a content-based signature scheme
34-
/// (workspaces and projects, currently).
35-
static let schemaVersion = 11
36-
3732
/// The type used for identifying PIF objects.
38-
public typealias GUID = String
33+
public typealias GUID = ProjectModel.GUID
3934

4035
/// The top-level PIF object.
4136
public struct TopLevelObject: Encodable {
@@ -117,6 +112,7 @@ public enum PIF {
117112
}
118113
}
119114

115+
/// The high-level PIF *workspace* object.
120116
public final class Workspace: HighLevelObject {
121117
override class var type: String { "workspace" }
122118

@@ -127,7 +123,7 @@ public enum PIF {
127123
var signature: String?
128124

129125
public init(guid: GUID, name: String, path: AbsolutePath, projects: [ProjectModel.Project]) {
130-
precondition(!guid.isEmpty)
126+
precondition(!guid.value.isEmpty)
131127
precondition(!name.isEmpty)
132128
precondition(Set(projects.map(\.id)).count == projects.count)
133129

@@ -174,12 +170,12 @@ public enum PIF {
174170
}
175171
}
176172

173+
/// A high-level PIF *project* object.
177174
public final class Project: HighLevelObject {
178175
override class var type: String { "project" }
179176

180-
public fileprivate(set) var underlying: ProjectModel.Project
177+
public var underlying: ProjectModel.Project
181178
var signature: String?
182-
183179
var id: ProjectModel.GUID { underlying.id }
184180

185181
public init(wrapping underlying: ProjectModel.Project) {
@@ -216,11 +212,11 @@ public enum PIF {
216212
}
217213
}
218214

215+
/// A high-level PIF *target* object.
219216
private final class Target: HighLevelObject {
220217
override class var type: String { "target" }
221218

222-
public fileprivate(set) var underlying: ProjectModel.BaseTarget
223-
219+
public var underlying: ProjectModel.BaseTarget
224220
var id: ProjectModel.GUID { underlying.id }
225221

226222
public init(wrapping underlying: ProjectModel.BaseTarget) {
@@ -257,27 +253,13 @@ public enum PIF {
257253
}
258254
}
259255

260-
extension CodingUserInfoKey {
261-
public static let encodingPIFSignature: CodingUserInfoKey = CodingUserInfoKey(rawValue: "encodingPIFSignature")!
256+
// MARK: - PIF Signature Support
262257

258+
extension CodingUserInfoKey {
263259
/// Perform the encoding for SwiftBuild consumption.
264260
public static let encodeForSwiftBuild: CodingUserInfoKey = CodingUserInfoKey(rawValue: "encodeForXCBuild")!
265261
}
266262

267-
// MARK: - PIF Signature Support
268-
269-
protocol PIFSignableObject {
270-
var signature: String? { get set }
271-
var name: String { get }
272-
}
273-
274-
extension PIF.Workspace: PIFSignableObject {}
275-
extension SwiftBuild.ProjectModel.Project: PIFSignableObject {
276-
var signature: String? { get { "" } set { } }
277-
278-
}
279-
extension SwiftBuild.ProjectModel.TargetCommon: PIFSignableObject {}
280-
281263
extension PIF {
282264
/// Add signature to workspace and its high-level subobjects.
283265
static func sign(workspace: PIF.Workspace) throws {
@@ -299,15 +281,4 @@ extension PIF {
299281
}
300282
workspace.signature = try signature(of: workspace)
301283
}
302-
303-
static func ____sign(workspace: PIF.Workspace) throws {
304-
for project in workspace.projects {
305-
for targetIndex in project.underlying.targets.indices {
306-
let targetSignature = project.underlying.targets[targetIndex].id.value
307-
project.underlying.targets[targetIndex].common.signature = targetSignature
308-
}
309-
project.signature = project.id.value
310-
}
311-
workspace.signature = workspace.guid
312-
}
313284
}

Sources/SwiftBuildSupport/PIFBuilder.swift

+12-20
Original file line numberDiff line numberDiff line change
@@ -107,47 +107,32 @@ public final class PIFBuilder {
107107

108108
let topLevelObject = try self.construct()
109109

110-
// Sign the PIF objects before encoding it for SwiftBuild.
110+
// Sign the PIF objects before encoding it for Swift Build.
111111
try PIF.sign(workspace: topLevelObject.workspace)
112112

113113
let pifData = try encoder.encode(topLevelObject)
114114
let pifString = String(decoding: pifData, as: UTF8.self)
115115

116-
let pifFilePath = "/Users/pmattos/HighLevelTools/SPM_new_pif_builder/SW_PIF.json"
117-
// print(">>> Writing to:", pifFilePath)
118-
try pifString.write(toFile: pifFilePath, atomically: true, encoding: .utf8)
119-
120116
return pifString
121117
}
122118

123119
/// Constructs a `PIF.TopLevelObject` representing the package graph.
124120
public func construct() throws -> PIF.TopLevelObject {
121+
#if canImport(SwiftBuild)
125122
try memoize(to: &self.pif) {
126123
let rootPackage = self.graph.rootPackages.first!
127124

128125
let sortedPackages = self.graph.packages
129126
.sorted { $0.manifest.displayName < $1.manifest.displayName } // TODO: use identity instead?
130127

131-
/*
132-
var projects: [PIFProjectBuilder] = try sortedPackages.map { package in
133-
try _PackagePIFProjectBuilder(
134-
package: package,
135-
parameters: self.parameters,
136-
fileSystem: self.fileSystem,
137-
observabilityScope: self.observabilityScope
138-
)
139-
}
140-
projects.append(AggregatePIFProjectBuilder(projects: projects))
141-
*/
142-
143128
let packagesAndProjects: [(ResolvedPackage, ProjectModel.Project)] = try sortedPackages.map { package in
144129
let packagePIFBuilderDelegate = PackagePIFBuilderDelegate(
145130
package: package
146131
)
147132
let packagePIFBuilder = PackagePIFBuilder(
148133
modulesGraph: self.graph,
149134
resolvedPackage: package,
150-
packageManifest: package.manifest,
135+
packageManifest: package.manifest,
151136
delegate: packagePIFBuilderDelegate,
152137
buildToolPluginResultsByTargetName: [:],
153138
createDylibForDynamicProducts: self.parameters.shouldCreateDylibForDynamicProducts,
@@ -176,6 +161,9 @@ public final class PIFBuilder {
176161

177162
return PIF.TopLevelObject(workspace: workspace)
178163
}
164+
#else
165+
fatalError("Swift Build support is not linked in.")
166+
#endif
179167
}
180168

181169
// Convenience method for generating PIF.
@@ -197,6 +185,8 @@ public final class PIFBuilder {
197185
}
198186
}
199187

188+
#if canImport(SwiftBuild)
189+
200190
fileprivate final class PackagePIFBuilderDelegate: PackagePIFBuilder.BuildDelegate {
201191
let package: ResolvedPackage
202192

@@ -289,6 +279,8 @@ fileprivate final class PackagePIFBuilderDelegate: PackagePIFBuilder.BuildDelega
289279
}
290280
}
291281

282+
#endif
283+
292284
fileprivate func buildAggregateProject(
293285
packagesAndProjects: [(package: ResolvedPackage, project: ProjectModel.Project)],
294286
observabilityScope: ObservabilityScope
@@ -379,13 +371,13 @@ fileprivate func buildAggregateProject(
379371
.debug,
380372
indent: 1,
381373
"Created target '\(allIncludingTests.id)' with name '\(allIncludingTests.name)' " +
382-
"and \(allIncludingTests.common.dependencies.count) dependencies"
374+
"and \(allIncludingTests.common.dependencies.count) (unlinked) dependencies"
383375
)
384376
observabilityScope.logPIF(
385377
.debug,
386378
indent: 1,
387379
"Created target '\(allExcludingTests.id)' with name '\(allExcludingTests.name)' " +
388-
"and \(allExcludingTests.common.dependencies.count) dependencies"
380+
"and \(allExcludingTests.common.dependencies.count) (unlinked) dependencies"
389381
)
390382
}
391383

Sources/SwiftBuildSupport/PackagePIFBuilder+Helpers.swift

+12-4
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,33 @@ extension GUID {
8787
}
8888

8989
extension PackageModel.Module {
90-
func pifTargetGUID(suffix: TargetSuffix? = nil) -> GUID {
90+
var pifTargetGUID: GUID { pifTargetGUID(suffix: nil) }
91+
92+
func pifTargetGUID(suffix: TargetSuffix?) -> GUID {
9193
PackagePIFBuilder.targetGUID(forModuleName: self.name, suffix: suffix)
9294
}
9395
}
9496

9597
extension PackageGraph.ResolvedModule {
96-
func pifTargetGUID(suffix: TargetSuffix? = nil) -> GUID {
98+
var pifTargetGUID: GUID { pifTargetGUID(suffix: nil) }
99+
100+
func pifTargetGUID(suffix: TargetSuffix?) -> GUID {
97101
self.underlying.pifTargetGUID(suffix: suffix)
98102
}
99103
}
100104

101105
extension PackageModel.Product {
102-
func pifTargetGUID(suffix: TargetSuffix? = nil) -> GUID {
106+
var pifTargetGUID: GUID { pifTargetGUID(suffix: nil) }
107+
108+
func pifTargetGUID(suffix: TargetSuffix?) -> GUID {
103109
PackagePIFBuilder.targetGUID(forProductName: self.name, suffix: suffix)
104110
}
105111
}
106112

107113
extension PackageGraph.ResolvedProduct {
108-
func pifTargetGUID(suffix: TargetSuffix? = nil) -> GUID {
114+
var pifTargetGUID: GUID { pifTargetGUID(suffix: nil) }
115+
116+
func pifTargetGUID(suffix: TargetSuffix?) -> GUID {
109117
self.underlying.pifTargetGUID(suffix: suffix)
110118
}
111119

Sources/SwiftBuildSupport/PackagePIFProjectBuilder+Modules.swift

+14-14
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extension PackagePIFProjectBuilder {
3939
// Create an executable PIF target in order to get specialization.
4040
let pluginTargetKeyPath = try self.project.addTarget { _ in
4141
ProjectModel.Target(
42-
id: pluginModule.pifTargetGUID(),
42+
id: pluginModule.pifTargetGUID,
4343
productType: .executable,
4444
name: pluginModule.name,
4545
productName: pluginModule.name
@@ -77,20 +77,20 @@ extension PackagePIFProjectBuilder {
7777

7878
if let productDependency {
7979
self.project[keyPath: pluginTargetKeyPath].common.addDependency(
80-
on: productDependency.pifTargetGUID(),
80+
on: productDependency.pifTargetGUID,
8181
platformFilters: dependencyPlatformFilters
8282
)
83-
log(.debug, indent: 1, "Added dependency on product '\(productDependency.pifTargetGUID())'")
83+
log(.debug, indent: 1, "Added dependency on product '\(productDependency.pifTargetGUID)'")
8484
} else {
8585
log(
8686
.debug,
8787
indent: 1,
88-
"Could not find a build plugin product to depend on for target '\(moduleDependency.pifTargetGUID())'"
88+
"Could not find a build plugin product to depend on for target '\(moduleDependency.pifTargetGUID)'"
8989
)
9090
}
9191

9292
case .library, .systemModule, .test, .binary, .plugin, .macro:
93-
let dependencyGUID = moduleDependency.pifTargetGUID()
93+
let dependencyGUID = moduleDependency.pifTargetGUID
9494
self.project[keyPath: pluginTargetKeyPath].common.addDependency(
9595
on: dependencyGUID,
9696
platformFilters: dependencyPlatformFilters
@@ -108,7 +108,7 @@ extension PackagePIFProjectBuilder {
108108
product: productDependency.underlying,
109109
buildSettings: &buildSettings
110110
) {
111-
let dependencyGUID = productDependency.pifTargetGUID()
111+
let dependencyGUID = productDependency.pifTargetGUID
112112
let dependencyPlatformFilters = packageConditions
113113
.toPlatformFilter(toolsVersion: self.package.manifest.toolsVersion)
114114

@@ -629,16 +629,16 @@ extension PackagePIFProjectBuilder {
629629
.productRepresentingDependencyOfBuildPlugin(in: moduleMainProducts)
630630
{
631631
self.project[keyPath: sourceModuleTargetKeyPath].common.addDependency(
632-
on: product.pifTargetGUID(),
632+
on: product.pifTargetGUID,
633633
platformFilters: dependencyPlatformFilters,
634634
linkProduct: false
635635
)
636-
log(.debug, indent: 1, "Added dependency on product '\(product.pifTargetGUID())'")
636+
log(.debug, indent: 1, "Added dependency on product '\(product.pifTargetGUID)'")
637637
} else {
638638
log(
639639
.debug,
640640
indent: 1,
641-
"Could not find a build plugin product to depend on for target '\(moduleDependency.pifTargetGUID())'"
641+
"Could not find a build plugin product to depend on for target '\(moduleDependency.pifTargetGUID)'"
642642
)
643643
}
644644

@@ -670,7 +670,7 @@ extension PackagePIFProjectBuilder {
670670
log(.debug, indent: 1, "Added use of binary library '\(moduleDependency.path)'")
671671

672672
case .plugin:
673-
let dependencyGUID = moduleDependency.pifTargetGUID()
673+
let dependencyGUID = moduleDependency.pifTargetGUID
674674
self.project[keyPath: sourceModuleTargetKeyPath].common.addDependency(
675675
on: dependencyGUID,
676676
platformFilters: dependencyPlatformFilters,
@@ -680,14 +680,14 @@ extension PackagePIFProjectBuilder {
680680

681681
case .library, .test, .macro, .systemModule:
682682
self.project[keyPath: sourceModuleTargetKeyPath].common.addDependency(
683-
on: moduleDependency.pifTargetGUID(),
683+
on: moduleDependency.pifTargetGUID,
684684
platformFilters: dependencyPlatformFilters,
685685
linkProduct: shouldLinkProduct
686686
)
687687
log(
688688
.debug,
689689
indent: 1,
690-
"Added \(shouldLinkProduct ? "linked " : "")dependency on target '\(moduleDependency.pifTargetGUID())'"
690+
"Added \(shouldLinkProduct ? "linked " : "")dependency on target '\(moduleDependency.pifTargetGUID)'"
691691
)
692692
}
693693

@@ -706,7 +706,7 @@ extension PackagePIFProjectBuilder {
706706
let shouldLinkProduct = shouldLinkProduct && productDependency.isLinkable
707707

708708
self.project[keyPath: sourceModuleTargetKeyPath].common.addDependency(
709-
on: productDependency.pifTargetGUID(),
709+
on: productDependency.pifTargetGUID,
710710
platformFilters: dependencyPlatformFilters,
711711
linkProduct: shouldLinkProduct
712712
)
@@ -814,7 +814,7 @@ extension PackagePIFProjectBuilder {
814814
// Create an aggregate PIF target (which doesn't have an actual product).
815815
let systemLibraryTargetKeyPath = try self.project.addAggregateTarget { _ in
816816
ProjectModel.AggregateTarget(
817-
id: resolvedSystemLibrary.pifTargetGUID(),
817+
id: resolvedSystemLibrary.pifTargetGUID,
818818
name: resolvedSystemLibrary.name
819819
)
820820
}

0 commit comments

Comments
 (0)