Skip to content

Commit 4dce989

Browse files
authored
Leverage package graph, add include paths. (#8750)
Take advantage of the fact we're in swiftpm and calculate things out of the package graph instead of hard coding. In particular, we compute the list of targets for the requested list of products, as well as the list of C modules and their include paths. Swift build is better at ensuring the include path is added to all targets that need it. Unfortunately when mixing libraries that use swift-syntax and macros, we end up adding both the C modules from the checkouts and the prebuilts causing duplicate definition of the C modules. By adding the above include paths and leveraging that we always checkout the source anyway, switch to using the checked out include paths, thus avoiding the duplicate. Also some prep for adding a config.json file so the list of prebuilts can be changed without a PR to SwiftPM. Removes the magic we were using to add the prebuilt library to the package manifest and just add them at the end.
1 parent c5d77e5 commit 4dce989

File tree

8 files changed

+288
-147
lines changed

8 files changed

+288
-147
lines changed

Sources/PackageLoading/PackageBuilder.swift

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,17 +290,33 @@ public struct PrebuiltLibrary {
290290
/// The path to the extracted prebuilt artifacts
291291
public let path: AbsolutePath
292292

293+
/// The path to the checked out source
294+
public let checkoutPath: AbsolutePath?
295+
293296
/// The products in the library
294297
public let products: [String]
295298

299+
/// The include path relative to the checkouts dir
300+
public let includePath: [RelativePath]?
301+
296302
/// The C modules that need their includes directory added to the include path
297303
public let cModules: [String]
298304

299-
public init(identity: PackageIdentity, libraryName: String, path: AbsolutePath, products: [String], cModules: [String]) {
305+
public init(
306+
identity: PackageIdentity,
307+
libraryName: String,
308+
path: AbsolutePath,
309+
checkoutPath: AbsolutePath?,
310+
products: [String],
311+
includePath: [RelativePath]? = nil,
312+
cModules: [String] = []
313+
) {
300314
self.identity = identity
301315
self.libraryName = libraryName
302316
self.path = path
317+
self.checkoutPath = checkoutPath
303318
self.products = products
319+
self.includePath = includePath
304320
self.cModules = cModules
305321
}
306322
}
@@ -1318,8 +1334,14 @@ public final class PackageBuilder {
13181334
table.add(ldFlagsAssignment, for: .OTHER_LDFLAGS)
13191335

13201336
var includeDirs: [AbsolutePath] = [prebuilt.path.appending(component: "Modules")]
1321-
for cModule in prebuilt.cModules {
1322-
includeDirs.append(prebuilt.path.appending(components: "include", cModule))
1337+
if let checkoutPath = prebuilt.checkoutPath, let includePath = prebuilt.includePath {
1338+
for includeDir in includePath {
1339+
includeDirs.append(checkoutPath.appending(includeDir))
1340+
}
1341+
} else {
1342+
for cModule in prebuilt.cModules {
1343+
includeDirs.append(prebuilt.path.appending(components: "include", cModule))
1344+
}
13231345
}
13241346
var includeAssignment = BuildSettings.Assignment()
13251347
includeAssignment.values = includeDirs.map({ "-I\($0.pathString)" })

Sources/Workspace/ManagedPrebuilt.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,15 @@ extension Workspace {
3131
/// The path to the extracted prebuilt artifacts
3232
public let path: AbsolutePath
3333

34+
/// The path to the checked out source
35+
public let checkoutPath: AbsolutePath?
36+
3437
/// The products in the library
3538
public let products: [String]
3639

40+
/// The include path for the C modules
41+
public let includePath: [RelativePath]?
42+
3743
/// The C modules that need their includes directory added to the include path
3844
public let cModules: [String]
3945
}

Sources/Workspace/Workspace+Prebuilts.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ extension Workspace {
5353
public struct Library: Identifiable, Codable {
5454
public let name: String
5555
public var products: [String]
56-
public var cModules: [String]
57-
public var artifacts: [Artifact]
56+
public var cModules: [String]?
57+
public var includePath: [RelativePath]?
58+
public var artifacts: [Artifact]?
5859

5960
public var id: String { name }
6061

@@ -73,12 +74,14 @@ extension Workspace {
7374
public init(
7475
name: String,
7576
products: [String] = [],
76-
cModules: [String] = [],
77-
artifacts: [Artifact] = []
77+
cModules: [String]? = nil,
78+
includePath: [RelativePath]? = nil,
79+
artifacts: [Artifact]? = nil
7880
) {
7981
self.name = name
8082
self.products = products
8183
self.cModules = cModules
84+
self.includePath = includePath
8285
self.artifacts = artifacts
8386
}
8487
}
@@ -600,7 +603,7 @@ extension Workspace {
600603
let hostPlatform = prebuiltsManager.hostPlatform
601604

602605
for library in prebuiltManifest.libraries {
603-
for artifact in library.artifacts where artifact.platform == hostPlatform {
606+
for artifact in library.artifacts ?? [] where artifact.platform == hostPlatform {
604607
if let path = try await prebuiltsManager
605608
.downloadPrebuilt(
606609
package: prebuilt,
@@ -611,13 +614,17 @@ extension Workspace {
611614
)
612615
{
613616
// Add to workspace state
617+
let checkoutPath = self.location.repositoriesCheckoutsDirectory
618+
.appending(component: prebuilt.identity.description)
614619
let managedPrebuilt = ManagedPrebuilt(
615620
identity: prebuilt.identity,
616621
version: packageVersion,
617622
libraryName: library.name,
618623
path: path,
624+
checkoutPath: checkoutPath,
619625
products: library.products,
620-
cModules: library.cModules
626+
includePath: library.includePath,
627+
cModules: library.cModules ?? []
621628
)
622629
addedPrebuilts.add(managedPrebuilt)
623630
await self.state.prebuilts.add(managedPrebuilt)

Sources/Workspace/Workspace+State.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,15 +464,19 @@ extension WorkspaceStateStorage {
464464
let version: TSCUtility.Version
465465
let libraryName: String
466466
let path: Basics.AbsolutePath
467+
let checkoutPath: Basics.AbsolutePath?
467468
let products: [String]
469+
let includePath: [Basics.RelativePath]?
468470
let cModules: [String]
469471

470472
init(_ managedPrebuilt: Workspace.ManagedPrebuilt) {
471473
self.identity = managedPrebuilt.identity
472474
self.version = managedPrebuilt.version
473475
self.libraryName = managedPrebuilt.libraryName
474476
self.path = managedPrebuilt.path
477+
self.checkoutPath = managedPrebuilt.checkoutPath
475478
self.products = managedPrebuilt.products
479+
self.includePath = managedPrebuilt.includePath
476480
self.cModules = managedPrebuilt.cModules
477481
}
478482
}
@@ -546,7 +550,9 @@ extension Workspace.ManagedPrebuilt {
546550
version: prebuilt.version,
547551
libraryName: prebuilt.libraryName,
548552
path: prebuilt.path,
553+
checkoutPath: prebuilt.checkoutPath,
549554
products: prebuilt.products,
555+
includePath: prebuilt.includePath,
550556
cModules: prebuilt.cModules
551557
)
552558
}

Sources/Workspace/Workspace.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,14 @@ extension Workspace {
969969
}
970970

971971
let prebuilts: [PackageIdentity: [String: PrebuiltLibrary]] = await self.state.prebuilts.reduce(into: .init()) {
972-
let prebuilt = PrebuiltLibrary(identity: $1.identity, libraryName: $1.libraryName, path: $1.path, products: $1.products, cModules: $1.cModules)
972+
let prebuilt = PrebuiltLibrary(
973+
identity: $1.identity,
974+
libraryName: $1.libraryName,
975+
path: $1.path,
976+
checkoutPath: $1.checkoutPath,
977+
products: $1.products,
978+
includePath: $1.includePath,
979+
cModules: $1.cModules)
973980
for product in $1.products {
974981
$0[$1.identity, default: [:]][product] = prebuilt
975982
}

0 commit comments

Comments
 (0)