Skip to content

Commit 69a3a70

Browse files
authored
Enable contrived test plugin test case (#8756)
This test case did not work until swiftlang/swift-build#541 was merged. Enable more plugin dependency testing. Allow more than one custom task per target in the PIF Builder.
1 parent 893282e commit 69a3a70

File tree

4 files changed

+69
-29
lines changed

4 files changed

+69
-29
lines changed

Sources/SwiftBuildSupport/PIFBuilder.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public final class PIFBuilder {
257257
var packagesAndProjects: [(ResolvedPackage, ProjectModel.Project)] = []
258258

259259
for package in sortedPackages {
260-
var buildToolPluginResultsByTargetName: [String: PackagePIFBuilder.BuildToolPluginInvocationResult] = [:]
260+
var buildToolPluginResultsByTargetName: [String: [PackagePIFBuilder.BuildToolPluginInvocationResult]] = [:]
261261

262262
for module in package.modules {
263263
// Apply each build tool plugin used by the target in order,
@@ -388,7 +388,11 @@ public final class PIFBuilder {
388388

389389
// Add a BuildToolPluginInvocationResult to the mapping.
390390
buildToolPluginResults.append(result2)
391-
buildToolPluginResultsByTargetName[module.name] = result2
391+
if var existingResults = buildToolPluginResultsByTargetName[module.name] {
392+
existingResults.append(result2)
393+
} else {
394+
buildToolPluginResultsByTargetName[module.name] = [result2]
395+
}
392396
}
393397
}
394398

Sources/SwiftBuildSupport/PackagePIFBuilder.swift

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public final class PackagePIFBuilder {
159159
}
160160

161161
/// Records the results of applying build tool plugins to modules in the package.
162-
let buildToolPluginResultsByTargetName: [String: PackagePIFBuilder.BuildToolPluginInvocationResult]
162+
let buildToolPluginResultsByTargetName: [String: [PackagePIFBuilder.BuildToolPluginInvocationResult]]
163163

164164
/// Whether to create dynamic libraries for dynamic products.
165165
///
@@ -192,7 +192,7 @@ public final class PackagePIFBuilder {
192192
resolvedPackage: ResolvedPackage,
193193
packageManifest: PackageModel.Manifest,
194194
delegate: PackagePIFBuilder.BuildDelegate,
195-
buildToolPluginResultsByTargetName: [String: BuildToolPluginInvocationResult],
195+
buildToolPluginResultsByTargetName: [String: [BuildToolPluginInvocationResult]],
196196
createDylibForDynamicProducts: Bool = false,
197197
packageDisplayVersion: String?,
198198
fileSystem: FileSystem,
@@ -209,6 +209,28 @@ public final class PackagePIFBuilder {
209209
self.observabilityScope = observabilityScope
210210
}
211211

212+
public init(
213+
modulesGraph: ModulesGraph,
214+
resolvedPackage: ResolvedPackage,
215+
packageManifest: PackageModel.Manifest,
216+
delegate: PackagePIFBuilder.BuildDelegate,
217+
buildToolPluginResultsByTargetName: [String: BuildToolPluginInvocationResult],
218+
createDylibForDynamicProducts: Bool = false,
219+
packageDisplayVersion: String?,
220+
fileSystem: FileSystem,
221+
observabilityScope: ObservabilityScope
222+
) {
223+
self.package = resolvedPackage
224+
self.packageManifest = packageManifest
225+
self.modulesGraph = modulesGraph
226+
self.delegate = delegate
227+
self.buildToolPluginResultsByTargetName = buildToolPluginResultsByTargetName.mapValues { [$0] }
228+
self.createDylibForDynamicProducts = createDylibForDynamicProducts
229+
self.packageDisplayVersion = packageDisplayVersion
230+
self.fileSystem = fileSystem
231+
self.observabilityScope = observabilityScope
232+
}
233+
212234
/// Build an empty PIF project.
213235
public func buildEmptyPIF() {
214236
self._pifProject = PackagePIFBuilder.buildEmptyPIF(package: self.package.underlying)

Sources/SwiftBuildSupport/PackagePIFProjectBuilder.swift

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -380,28 +380,36 @@ struct PackagePIFProjectBuilder {
380380
targetKeyPath: WritableKeyPath<ProjectModel.Project, ProjectModel.Target>,
381381
addBuildToolPluginCommands: Bool
382382
) -> (sourceFilePaths: [AbsolutePath], resourceFilePaths: [String]) {
383-
guard let pluginResult = pifBuilder.buildToolPluginResultsByTargetName[module.name] else {
383+
guard let pluginResults = pifBuilder.buildToolPluginResultsByTargetName[module.name] else {
384384
// We found no results for the target.
385385
return (sourceFilePaths: [], resourceFilePaths: [])
386386
}
387387

388-
// Process the results of applying any build tool plugins on the target.
389-
// If we've been asked to add build tool commands for the result, we do so now.
390-
if addBuildToolPluginCommands {
391-
for command in pluginResult.buildCommands {
392-
self.addBuildToolCommand(command, to: targetKeyPath)
388+
var sourceFilePaths: [AbsolutePath] = []
389+
var resourceFilePaths: [AbsolutePath] = []
390+
391+
for pluginResult in pluginResults {
392+
// Process the results of applying any build tool plugins on the target.
393+
// If we've been asked to add build tool commands for the result, we do so now.
394+
if addBuildToolPluginCommands {
395+
for command in pluginResult.buildCommands {
396+
self.addBuildToolCommand(command, to: targetKeyPath)
397+
}
393398
}
394-
}
395399

396-
// Process all the paths of derived output paths using the same rules as for source.
397-
let result = self.process(
398-
pluginGeneratedFilePaths: pluginResult.allDerivedOutputPaths,
399-
forModule: module,
400-
toolsVersion: self.package.manifest.toolsVersion
401-
)
400+
// Process all the paths of derived output paths using the same rules as for source.
401+
let result = self.process(
402+
pluginGeneratedFilePaths: pluginResult.allDerivedOutputPaths,
403+
forModule: module,
404+
toolsVersion: self.package.manifest.toolsVersion
405+
)
406+
407+
sourceFilePaths.append(contentsOf: result.sourceFilePaths)
408+
resourceFilePaths.append(contentsOf: result.resourceFilePaths.map(\.path))
409+
}
402410
return (
403-
sourceFilePaths: result.sourceFilePaths,
404-
resourceFilePaths: result.resourceFilePaths.map(\.path.pathString)
411+
sourceFilePaths: sourceFilePaths,
412+
resourceFilePaths: resourceFilePaths.map(\.pathString)
405413
)
406414
}
407415

@@ -414,17 +422,19 @@ struct PackagePIFProjectBuilder {
414422
sourceFilePaths: [AbsolutePath],
415423
resourceFilePaths: [String]
416424
) {
417-
guard let pluginResult = pifBuilder.buildToolPluginResultsByTargetName[module.name] else {
425+
guard let pluginResults = pifBuilder.buildToolPluginResultsByTargetName[module.name] else {
418426
return
419427
}
420428

421-
for command in pluginResult.buildCommands {
422-
let producesResources = Set(command.outputPaths).intersection(resourceFilePaths).hasContent
429+
for pluginResult in pluginResults {
430+
for command in pluginResult.buildCommands {
431+
let producesResources = Set(command.outputPaths).intersection(resourceFilePaths).hasContent
423432

424-
if producesResources {
425-
self.addBuildToolCommand(command, to: resourceBundleTargetKeyPath)
426-
} else {
427-
self.addBuildToolCommand(command, to: sourceModuleTargetKeyPath)
433+
if producesResources {
434+
self.addBuildToolCommand(command, to: resourceBundleTargetKeyPath)
435+
} else {
436+
self.addBuildToolCommand(command, to: sourceModuleTargetKeyPath)
437+
}
428438
}
429439
}
430440
}

Tests/FunctionalTests/PluginTests.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,12 @@ final class PluginTests: XCTestCase {
204204
XCTAssert(stdout.contains("Build of product 'MyLocalTool' complete!"), "stdout:\n\(stdout)")
205205
}
206206

207-
/*
208-
FIXME: Determine the cause of the compile error.
209207
#if os(macOS) // See https://github.com/swiftlang/swift-package-manager/issues/8416 for errors running build tools on Linux
210208
try await fixture(name: "Miscellaneous/Plugins") { fixturePath in
211209
let (stdout, _) = try await executeSwiftBuild(fixturePath.appending("ContrivedTestPlugin"), configuration: .Debug, extraArgs: ["--build-system", "swiftbuild", "--product", "MyLocalTool", "--disable-sandbox"])
212210
XCTAssert(stdout.contains("Build complete!"), "stdout:\n\(stdout)")
213211
}
214212
#endif
215-
*/
216213
}
217214

218215
func testPluginScriptSandbox() async throws {
@@ -1335,5 +1332,12 @@ FIXME: Determine the cause of the compile error.
13351332
let (stdout, _) = try await executeSwiftBuild(fixturePath)
13361333
XCTAssert(stdout.contains("Build complete!"), "stdout:\n\(stdout)")
13371334
}
1335+
1336+
#if os(macOS) // See https://github.com/swiftlang/swift-package-manager/issues/8416 for errors running build tools on Linux
1337+
try await fixture(name: "Miscellaneous/Plugins/DependentPlugins") { fixturePath in
1338+
let (stdout, _) = try await executeSwiftBuild(fixturePath, extraArgs: ["--build-system", "swiftbuild"])
1339+
XCTAssert(stdout.contains("Build complete!"), "stdout:\n\(stdout)")
1340+
}
1341+
#endif
13381342
}
13391343
}

0 commit comments

Comments
 (0)