Skip to content

Commit fd7995e

Browse files
committed
Package LibraryMetadata together with its toolchain location
Location is required down the line for build planning to inject proper header and library includes into the build. The location is currently assumed to be in `share/pm/<productName>`
1 parent 3eb6910 commit fd7995e

File tree

13 files changed

+89
-60
lines changed

13 files changed

+89
-60
lines changed

Sources/Build/BuildOperation.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ package final class BuildOperation: PackageStructureDelegate, SPMBuildCore.Build
269269

270270
// TODO: Currently this function will only match frameworks.
271271
func detectUnexpressedDependencies(
272-
availableLibraries: [LibraryMetadata],
272+
availableLibraries: [ProvidedLibrary],
273273
targetDependencyMap: [String: [String]]?
274274
) {
275275
// Ensure we only emit these once, regardless of how many builds are being done.
@@ -279,8 +279,8 @@ package final class BuildOperation: PackageStructureDelegate, SPMBuildCore.Build
279279
Self.didEmitUnexpressedDependencies = true
280280

281281
let availableFrameworks = Dictionary<String, PackageIdentity>(uniqueKeysWithValues: availableLibraries.compactMap {
282-
if let identity = Set($0.identities.map(\.identity)).spm_only {
283-
return ("\($0.productName!).framework", identity)
282+
if let identity = Set($0.metadata.identities.map(\.identity)).spm_only {
283+
return ("\($0.metadata.productName).framework", identity)
284284
} else {
285285
return nil
286286
}

Sources/PackageGraph/Resolution/PubGrub/ContainerProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ final class ContainerProvider {
107107
}
108108

109109
/// Starts prefetching the given containers.
110-
func prefetch(containers identifiers: [PackageReference], availableLibraries: [LibraryMetadata]) {
110+
func prefetch(containers identifiers: [PackageReference], availableLibraries: [ProvidedLibrary]) {
111111
let filteredIdentifiers = identifiers.filter {
112112
$0.matchingPrebuiltLibrary(in: availableLibraries) == nil
113113
}

Sources/PackageGraph/Resolution/PubGrub/PubGrubDependencyResolver.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public struct PubGrubDependencyResolver {
105105
private let pins: PinsStore.Pins
106106

107107
/// The packages that are available in a prebuilt form in SDK or a toolchain
108-
private let availableLibraries: [LibraryMetadata]
108+
private let availableLibraries: [ProvidedLibrary]
109109

110110
/// The container provider used to load package containers.
111111
private let provider: ContainerProvider
@@ -125,7 +125,7 @@ public struct PubGrubDependencyResolver {
125125
public init(
126126
provider: PackageContainerProvider,
127127
pins: PinsStore.Pins = [:],
128-
availableLibraries: [LibraryMetadata] = [],
128+
availableLibraries: [ProvidedLibrary] = [],
129129
skipDependenciesUpdates: Bool = false,
130130
prefetchBasedOnResolvedFile: Bool = false,
131131
observabilityScope: ObservabilityScope,
@@ -895,14 +895,14 @@ extension PackageRequirement {
895895
}
896896

897897
extension PackageReference {
898-
public func matchingPrebuiltLibrary(in availableLibraries: [LibraryMetadata]) -> LibraryMetadata? {
898+
public func matchingPrebuiltLibrary(in availableLibraries: [ProvidedLibrary]) -> ProvidedLibrary? {
899899
switch self.kind {
900900
case .fileSystem, .localSourceControl, .root, .providedLibrary:
901901
return nil // can never match a prebuilt library
902902
case .registry(let identity):
903903
if let registryIdentity = identity.registry {
904904
return availableLibraries.first(
905-
where: { $0.identities.contains(
905+
where: { $0.metadata.identities.contains(
906906
where: { $0 == .packageIdentity(
907907
scope: registryIdentity.scope.description,
908908
name: registryIdentity.name.description
@@ -916,7 +916,7 @@ extension PackageReference {
916916
}
917917
case .remoteSourceControl(let url):
918918
return availableLibraries.first(where: {
919-
$0.identities.contains(where: { $0 == .sourceControl(url: url) })
919+
$0.metadata.identities.contains(where: { $0 == .sourceControl(url: url) })
920920
})
921921
}
922922
}

Sources/PackageModel/InstalledLibrariesSupport/LibraryMetadata.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313
import Basics
1414
import Foundation
1515

16+
public struct ProvidedLibrary {
17+
public let location: AbsolutePath
18+
public let metadata: LibraryMetadata
19+
20+
public var version: String {
21+
metadata.version
22+
}
23+
}
24+
1625
public struct LibraryMetadata: Decodable {
1726
public enum Identity: Equatable, Decodable {
1827
case packageIdentity(scope: String, name: String)
@@ -24,7 +33,7 @@ public struct LibraryMetadata: Decodable {
2433
/// The version that was built (e.g., 509.0.2)
2534
public let version: String
2635
/// The product name, if it differs from the module name (e.g., SwiftParser).
27-
public let productName: String?
36+
public let productName: String
2837

2938
let schemaVersion: Int
3039
}

Sources/PackageModel/Toolchain.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public protocol Toolchain {
4444
var installedSwiftPMConfiguration: InstalledSwiftPMConfiguration { get }
4545

4646
/// Metadata for libraries provided by the used toolchain.
47-
var providedLibraries: [LibraryMetadata] { get }
47+
var providedLibraries: [ProvidedLibrary] { get }
4848

4949
/// The root path to the Swift SDK used by this toolchain.
5050
var sdkRootPath: AbsolutePath? { get }

Sources/PackageModel/UserToolchain.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public final class UserToolchain: Toolchain {
8888

8989
public let installedSwiftPMConfiguration: InstalledSwiftPMConfiguration
9090

91-
public let providedLibraries: [LibraryMetadata]
91+
public let providedLibraries: [ProvidedLibrary]
9292

9393
/// Returns the runtime library for the given sanitizer.
9494
public func runtimeLibrary(for sanitizer: Sanitizer) throws -> AbsolutePath {
@@ -487,7 +487,7 @@ public final class UserToolchain: Toolchain {
487487
searchStrategy: SearchStrategy = .default,
488488
customLibrariesLocation: ToolchainConfiguration.SwiftPMLibrariesLocation? = nil,
489489
customInstalledSwiftPMConfiguration: InstalledSwiftPMConfiguration? = nil,
490-
customProvidedLibraries: [LibraryMetadata]? = nil
490+
customProvidedLibraries: [ProvidedLibrary]? = nil
491491
) throws {
492492
self.swiftSDK = swiftSDK
493493
self.environment = environment
@@ -551,7 +551,15 @@ public final class UserToolchain: Toolchain {
551551
self.providedLibraries = try Self.loadJSONResource(
552552
config: path,
553553
type: [LibraryMetadata].self,
554-
default: [])
554+
default: []
555+
).map {
556+
.init(
557+
location: path.parentDirectory.appending(component: $0.productName),
558+
metadata: $0
559+
)
560+
}.filter {
561+
localFileSystem.isDirectory($0.location)
562+
}
555563
}
556564

557565
// Use the triple from Swift SDK or compute the host triple using swiftc.

Sources/SPMTestSupport/MockBuildTestHelper.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ package struct MockToolchain: PackageModel.Toolchain {
3939
package let swiftPluginServerPath: AbsolutePath? = nil
4040
package let extraFlags = PackageModel.BuildFlags()
4141
package let installedSwiftPMConfiguration = InstalledSwiftPMConfiguration.default
42-
package let providedLibraries = [LibraryMetadata]()
42+
package let providedLibraries = [ProvidedLibrary]()
4343

4444
package func getClangCompiler() throws -> AbsolutePath {
4545
"/fake/path/to/clang"

Sources/Workspace/Workspace+Dependencies.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import class PackageGraph.PinsStore
3737
import struct PackageGraph.PubGrubDependencyResolver
3838
import struct PackageGraph.Term
3939
import class PackageLoading.ManifestLoader
40-
import struct PackageModel.LibraryMetadata
40+
import struct PackageModel.ProvidedLibrary
4141
import enum PackageModel.PackageDependency
4242
import struct PackageModel.PackageIdentity
4343
import struct PackageModel.PackageReference
@@ -57,7 +57,7 @@ extension Workspace {
5757
root: PackageGraphRootInput,
5858
packages: [String] = [],
5959
dryRun: Bool = false,
60-
availableLibraries: [LibraryMetadata],
60+
availableLibraries: [ProvidedLibrary],
6161
observabilityScope: ObservabilityScope
6262
) throws -> [(PackageReference, Workspace.PackageStateChange)]? {
6363
let start = DispatchTime.now()
@@ -200,7 +200,7 @@ extension Workspace {
200200
func _resolve(
201201
root: PackageGraphRootInput,
202202
explicitProduct: String?,
203-
availableLibraries: [LibraryMetadata],
203+
availableLibraries: [ProvidedLibrary],
204204
resolvedFileStrategy: ResolvedFileStrategy,
205205
observabilityScope: ObservabilityScope
206206
) throws -> DependencyManifests {
@@ -304,7 +304,7 @@ extension Workspace {
304304
func _resolveBasedOnResolvedVersionsFile(
305305
root: PackageGraphRootInput,
306306
explicitProduct: String?,
307-
availableLibraries: [LibraryMetadata],
307+
availableLibraries: [ProvidedLibrary],
308308
observabilityScope: ObservabilityScope
309309
) throws -> DependencyManifests {
310310
let (manifests, precomputationResult) = try self.tryResolveBasedOnResolvedVersionsFile(
@@ -343,7 +343,7 @@ extension Workspace {
343343
fileprivate func tryResolveBasedOnResolvedVersionsFile(
344344
root: PackageGraphRootInput,
345345
explicitProduct: String?,
346-
availableLibraries: [LibraryMetadata],
346+
availableLibraries: [ProvidedLibrary],
347347
observabilityScope: ObservabilityScope
348348
) throws -> (DependencyManifests, ResolutionPrecomputationResult) {
349349
// Ensure the cache path exists.
@@ -495,7 +495,7 @@ extension Workspace {
495495
func resolveAndUpdateResolvedFile(
496496
root: PackageGraphRootInput,
497497
explicitProduct: String? = nil,
498-
availableLibraries: [LibraryMetadata],
498+
availableLibraries: [ProvidedLibrary],
499499
forceResolution: Bool,
500500
constraints: [PackageContainerConstraint],
501501
observabilityScope: ObservabilityScope
@@ -849,7 +849,7 @@ extension Workspace {
849849
dependencyManifests: DependencyManifests,
850850
pinsStore: PinsStore,
851851
constraints: [PackageContainerConstraint],
852-
availableLibraries: [LibraryMetadata],
852+
availableLibraries: [ProvidedLibrary],
853853
observabilityScope: ObservabilityScope
854854
) throws -> ResolutionPrecomputationResult {
855855
let computedConstraints =
@@ -1144,7 +1144,7 @@ extension Workspace {
11441144
/// Creates resolver for the workspace.
11451145
fileprivate func createResolver(
11461146
pins: PinsStore.Pins,
1147-
availableLibraries: [LibraryMetadata],
1147+
availableLibraries: [ProvidedLibrary],
11481148
observabilityScope: ObservabilityScope
11491149
) throws -> PubGrubDependencyResolver {
11501150
var delegate: DependencyResolverDelegate

Sources/Workspace/Workspace+Editing.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import class Basics.ObservabilityScope
1515
import struct Basics.RelativePath
1616
import func Basics.temp_await
1717
import struct PackageGraph.PackageGraphRootInput
18-
import struct PackageModel.LibraryMetadata
18+
import struct PackageModel.ProvidedLibrary
1919
import struct SourceControl.Revision
2020
import class TSCBasic.InMemoryFileSystem
2121

@@ -173,7 +173,7 @@ extension Workspace {
173173
dependency: ManagedDependency,
174174
forceRemove: Bool,
175175
root: PackageGraphRootInput? = nil,
176-
availableLibraries: [LibraryMetadata],
176+
availableLibraries: [ProvidedLibrary],
177177
observabilityScope: ObservabilityScope
178178
) throws {
179179
// Compute if we need to force remove.

Sources/Workspace/Workspace+Manifests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import struct PackageGraph.PackageGraphRoot
2828
import class PackageLoading.ManifestLoader
2929
import struct PackageLoading.ManifestValidator
3030
import struct PackageLoading.ToolsVersionParser
31-
import struct PackageModel.LibraryMetadata
31+
import struct PackageModel.ProvidedLibrary
3232
import class PackageModel.Manifest
3333
import struct PackageModel.PackageIdentity
3434
import struct PackageModel.PackageReference
@@ -61,7 +61,7 @@ extension Workspace {
6161

6262
private let workspace: Workspace
6363

64-
private let availableLibraries: [LibraryMetadata]
64+
private let availableLibraries: [ProvidedLibrary]
6565

6666
private let observabilityScope: ObservabilityScope
6767

@@ -81,7 +81,7 @@ extension Workspace {
8181
fileSystem: FileSystem
8282
)],
8383
workspace: Workspace,
84-
availableLibraries: [LibraryMetadata],
84+
availableLibraries: [ProvidedLibrary],
8585
observabilityScope: ObservabilityScope
8686
) {
8787
self.root = root
@@ -173,7 +173,7 @@ extension Workspace {
173173
fileSystem: FileSystem
174174
)],
175175
workspace: Workspace,
176-
availableLibraries: [LibraryMetadata],
176+
availableLibraries: [ProvidedLibrary],
177177
observabilityScope: ObservabilityScope
178178
) throws
179179
-> (
@@ -205,7 +205,7 @@ extension Workspace {
205205
})
206206

207207
let identitiesAvailableInSDK = availableLibraries.flatMap {
208-
$0.identities.map {
208+
$0.metadata.identities.map {
209209
$0.ref
210210
}.filter {
211211
// We "trust the process" here, if an identity from the SDK is available, filter it.
@@ -438,7 +438,7 @@ extension Workspace {
438438
public func loadDependencyManifests(
439439
root: PackageGraphRoot,
440440
automaticallyAddManagedDependencies: Bool = false,
441-
availableLibraries: [LibraryMetadata],
441+
availableLibraries: [ProvidedLibrary],
442442
observabilityScope: ObservabilityScope
443443
) throws -> DependencyManifests {
444444
let prepopulateManagedDependencies: ([PackageReference]) throws -> Void = { refs in
@@ -821,7 +821,7 @@ extension Workspace {
821821
/// If some edited dependency is removed from the file system, mark it as unedited and
822822
/// fallback on the original checkout.
823823
private func fixManagedDependencies(
824-
availableLibraries: [LibraryMetadata],
824+
availableLibraries: [ProvidedLibrary],
825825
observabilityScope: ObservabilityScope
826826
) {
827827
// Reset managed dependencies if the state file was removed during the lifetime of the Workspace object.

Sources/Workspace/Workspace.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ public class Workspace {
572572
)
573573
}
574574

575-
fileprivate var providedLibraries: [LibraryMetadata] {
575+
fileprivate var providedLibraries: [ProvidedLibrary] {
576576
// Note: Eventually, we should get these from the individual SDKs, but the first step is providing the metadata centrally in the toolchain.
577577
return self.hostToolchain.providedLibraries
578578
}
@@ -626,7 +626,7 @@ extension Workspace {
626626
packageName: String,
627627
forceRemove: Bool,
628628
root: PackageGraphRootInput,
629-
availableLibraries: [LibraryMetadata],
629+
availableLibraries: [ProvidedLibrary],
630630
observabilityScope: ObservabilityScope
631631
) throws {
632632
guard let dependency = self.state.dependencies[.plain(packageName)] else {
@@ -879,7 +879,7 @@ extension Workspace {
879879
forceResolvedVersions: Bool = false,
880880
customXCTestMinimumDeploymentTargets: [PackageModel.Platform: PlatformVersion]? = .none,
881881
testEntryPointPath: AbsolutePath? = nil,
882-
availableLibraries: [LibraryMetadata],
882+
availableLibraries: [ProvidedLibrary],
883883
expectedSigningEntities: [PackageIdentity: RegistryReleaseMetadata.SigningEntity] = [:],
884884
observabilityScope: ObservabilityScope
885885
) throws -> ModulesGraph {

Tests/BuildTests/BuildOperationTests.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@ final class BuildOperationTests: XCTestCase {
5252
buildOp.detectUnexpressedDependencies(
5353
availableLibraries: [
5454
.init(
55-
identities: [
56-
.sourceControl(url: .init("https://example.com/org/foo"))
57-
],
58-
version: "1.0.0",
59-
productName: "Best",
60-
schemaVersion: 1
55+
location: "/foo",
56+
metadata: .init(
57+
identities: [
58+
.sourceControl(url: .init("https://example.com/org/foo"))
59+
],
60+
version: "1.0.0",
61+
productName: "Best",
62+
schemaVersion: 1
63+
)
6164
)
6265
],
6366
targetDependencyMap: ["Lunch": []]

0 commit comments

Comments
 (0)