Skip to content

Commit 001c74a

Browse files
MaxDesiatovxedin
andauthored
[6.0] Improvements to prebuilt/provided library handling (#7713)
Cherry-pick of #7496 and #7433, excluding [16b4142](16b4142). **Explanation**: The idea is to enable provided libraries to be a part of a toolchain, which means that instead of completely eliding them from a package graph, they need to be handled post-resolution in a special way and reflected in a build plan for Swift targets (as compiler and linker arguments). Unified handling of `config.json` and `provided-libraries.json` resources and dropped use of `Bundle` type (config should always be a part of SDK or a toolchain). **Scope**: Localized, touches modules graph resolution and build planning only in certain configurations. **Risk**: Low. While the change has a somewhat broad scope, it's NFC for most builds and has been incubated on `main` for 2 months with no known issues. **Testing**: New test cases added and existing ones updated. **Issue**: rdar://125531670 **Reviewer**: @MaxDesiatov and @bnbarham --------- Co-authored-by: Pavel Yaskevich <[email protected]>
1 parent 74a045f commit 001c74a

File tree

49 files changed

+594
-270
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+594
-270
lines changed

Sources/Build/BuildDescription/ProductBuildDescription.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
4646
// Computed during build planning.
4747
var dylibs: [ProductBuildDescription] = []
4848

49+
/// The list of provided libraries that are going to be used by this product.
50+
var providedLibraries: [String: AbsolutePath] = [:]
51+
4952
/// Any additional flags to be added. These flags are expected to be computed during build planning.
5053
var additionalFlags: [String] = []
5154

@@ -157,6 +160,8 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
157160
args += ["-F", self.buildParameters.buildPath.pathString]
158161
}
159162

163+
self.providedLibraries.forEach { args += ["-L", $1.pathString, "-l", $0] }
164+
160165
args += ["-L", self.buildParameters.buildPath.pathString]
161166
args += try ["-o", binaryPath.pathString]
162167
args += ["-module-name", self.product.name.spm_mangledToC99ExtendedIdentifier()]

Sources/Build/BuildManifest/LLBuildManifestBuilder+Swift.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ extension LLBuildManifestBuilder {
429429
if target.underlying is BinaryTarget { return }
430430
// Ignore Plugin Targets.
431431
if target.underlying is PluginTarget { return }
432+
// Ignore Provided Libraries.
433+
if target.underlying is ProvidedLibraryTarget { return }
432434

433435
// Depend on the binary for executable targets.
434436
if target.type == .executable && !prepareForIndexing {

Sources/Build/BuildOperation.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
275275

276276
// TODO: Currently this function will only match frameworks.
277277
func detectUnexpressedDependencies(
278-
availableLibraries: [LibraryMetadata],
278+
availableLibraries: [ProvidedLibrary],
279279
targetDependencyMap: [String: [String]]?
280280
) {
281281
// Ensure we only emit these once, regardless of how many builds are being done.
@@ -285,8 +285,8 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
285285
Self.didEmitUnexpressedDependencies = true
286286

287287
let availableFrameworks = Dictionary<String, PackageIdentity>(uniqueKeysWithValues: availableLibraries.compactMap {
288-
if let identity = Set($0.identities.map(\.identity)).spm_only {
289-
return ("\($0.productName!).framework", identity)
288+
if let identity = Set($0.metadata.identities.map(\.identity)).spm_only {
289+
return ("\($0.metadata.productName).framework", identity)
290290
} else {
291291
return nil
292292
}

Sources/Build/BuildPlan/BuildPlan+Product.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ extension BuildPlan {
116116
}
117117
buildProduct.libraryBinaryPaths = dependencies.libraryBinaryPaths
118118

119+
buildProduct.providedLibraries = dependencies.providedLibraries
120+
119121
buildProduct.availableTools = dependencies.availableTools
120122
}
121123

@@ -128,6 +130,7 @@ extension BuildPlan {
128130
staticTargets: [ResolvedModule],
129131
systemModules: [ResolvedModule],
130132
libraryBinaryPaths: Set<AbsolutePath>,
133+
providedLibraries: [String: AbsolutePath],
131134
availableTools: [String: AbsolutePath]
132135
) {
133136
/* Prior to tools-version 5.9, we used to erroneously recursively traverse executable/plugin dependencies and statically include their
@@ -205,6 +208,7 @@ extension BuildPlan {
205208
var staticTargets = [ResolvedModule]()
206209
var systemModules = [ResolvedModule]()
207210
var libraryBinaryPaths: Set<AbsolutePath> = []
211+
var providedLibraries = [String: AbsolutePath]()
208212
var availableTools = [String: AbsolutePath]()
209213

210214
for dependency in allTargets {
@@ -258,6 +262,8 @@ extension BuildPlan {
258262
}
259263
case .plugin:
260264
continue
265+
case .providedLibrary:
266+
providedLibraries[target.name] = target.underlying.path
261267
}
262268

263269
case .product(let product, _):
@@ -275,7 +281,7 @@ extension BuildPlan {
275281
}
276282
}
277283

278-
return (linkLibraries, staticTargets, systemModules, libraryBinaryPaths, availableTools)
284+
return (linkLibraries, staticTargets, systemModules, libraryBinaryPaths, providedLibraries, availableTools)
279285
}
280286

281287
/// Extracts the artifacts from an artifactsArchive

Sources/Build/BuildPlan/BuildPlan+Swift.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import struct Basics.InternalError
1414
import class PackageModel.BinaryTarget
1515
import class PackageModel.ClangTarget
1616
import class PackageModel.SystemLibraryTarget
17+
import class PackageModel.ProvidedLibraryTarget
1718

1819
extension BuildPlan {
1920
func plan(swiftTarget: SwiftTargetBuildDescription) throws {
@@ -48,6 +49,10 @@ extension BuildPlan {
4849
swiftTarget.libraryBinaryPaths.insert(library.libraryPath)
4950
}
5051
}
52+
case let target as ProvidedLibraryTarget:
53+
swiftTarget.additionalFlags += [
54+
"-I", target.path.pathString
55+
]
5156
default:
5257
break
5358
}

Sources/Build/BuildPlan/BuildPlan.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
419419
toolsVersion: toolsVersion,
420420
fileSystem: fileSystem
421421
))
422-
case is SystemLibraryTarget, is BinaryTarget:
422+
case is SystemLibraryTarget, is BinaryTarget, is ProvidedLibraryTarget:
423423
break
424424
default:
425425
throw InternalError("unhandled \(target.underlying)")

Sources/Commands/PackageCommands/EditCommands.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ extension SwiftPackageCommand {
7272
packageName: packageName,
7373
forceRemove: shouldForceRemove,
7474
root: swiftCommandState.getWorkspaceRoot(),
75-
availableLibraries: swiftCommandState.getHostToolchain().providedLibraries,
7675
observabilityScope: swiftCommandState.observabilityScope
7776
)
7877
}

Sources/Commands/PackageCommands/Update.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ extension SwiftPackageCommand {
7171
case .removed:
7272
report += "\n"
7373
report += "- \(package.identity) \(currentVersion)"
74-
case .unchanged:
74+
case .unchanged, .usesLibrary:
7575
continue
7676
}
7777
}

Sources/Commands/Snippets/Cards/TopCard.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ fileprivate extension Target.Kind {
151151
return "snippets"
152152
case .macro:
153153
return "macros"
154+
case .providedLibrary:
155+
return "provided libraries"
154156
}
155157
}
156158
}

Sources/CoreCommands/SwiftCommandState.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,6 @@ public final class SwiftCommandState {
622622
explicitProduct: explicitProduct,
623623
forceResolvedVersions: options.resolver.forceResolvedVersions,
624624
testEntryPointPath: testEntryPointPath,
625-
availableLibraries: self.getHostToolchain().providedLibraries,
626625
observabilityScope: self.observabilityScope
627626
)
628627

0 commit comments

Comments
 (0)