Skip to content

Commit 18f2018

Browse files
authored
Command plugins should respect --build-system selection (#8779)
Command plugins should use the user specified build system when building any dependencies, and if the command plugin requests a build of its own. Supporting the full range of command plugin functionality with the Swift Build backend will take a little more work, but this patch gets the fundamentals wired up
1 parent 0bf9624 commit 18f2018

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

Sources/Commands/PackageCommands/PluginCommand.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ struct PluginCommand: AsyncSwiftCommand {
210210
plugin: matchingPlugins[0],
211211
package: packageGraph.rootPackages[packageGraph.rootPackages.startIndex],
212212
packageGraph: packageGraph,
213+
buildSystem: pluginArguments.globalOptions.build.buildSystem,
213214
options: pluginOptions,
214215
arguments: unparsedArguments,
215216
swiftCommandState: swiftCommandState
@@ -220,6 +221,7 @@ struct PluginCommand: AsyncSwiftCommand {
220221
plugin: ResolvedModule,
221222
package: ResolvedPackage,
222223
packageGraph: ModulesGraph,
224+
buildSystem buildSystemKind: BuildSystemProvider.Kind,
223225
options: PluginOptions,
224226
arguments: [String],
225227
swiftCommandState: SwiftCommandState
@@ -328,7 +330,7 @@ struct PluginCommand: AsyncSwiftCommand {
328330
let buildParameters = try swiftCommandState.toolsBuildParameters
329331
// Build or bring up-to-date any executable host-side tools on which this plugin depends. Add them and any binary dependencies to the tool-names-to-path map.
330332
let buildSystem = try await swiftCommandState.createBuildSystem(
331-
explicitBuildSystem: .native,
333+
explicitBuildSystem: buildSystemKind,
332334
traitConfiguration: .init(),
333335
cacheBuildManifest: false,
334336
productsBuildParameters: swiftCommandState.productsBuildParameters,
@@ -353,7 +355,7 @@ struct PluginCommand: AsyncSwiftCommand {
353355
}
354356

355357
// Set up a delegate to handle callbacks from the command plugin.
356-
let pluginDelegate = PluginDelegate(swiftCommandState: swiftCommandState, plugin: pluginTarget)
358+
let pluginDelegate = PluginDelegate(swiftCommandState: swiftCommandState, buildSystem: buildSystemKind, plugin: pluginTarget)
357359
let delegateQueue = DispatchQueue(label: "plugin-invocation")
358360

359361
// Run the command plugin.

Sources/Commands/Utilities/PluginDelegate.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ import struct Basics.AsyncProcessResult
2525

2626
final class PluginDelegate: PluginInvocationDelegate {
2727
let swiftCommandState: SwiftCommandState
28+
let buildSystem: BuildSystemProvider.Kind
2829
let plugin: PluginModule
2930
var lineBufferedOutput: Data
3031

31-
init(swiftCommandState: SwiftCommandState, plugin: PluginModule) {
32+
init(swiftCommandState: SwiftCommandState, buildSystem: BuildSystemProvider.Kind, plugin: PluginModule) {
3233
self.swiftCommandState = swiftCommandState
34+
self.buildSystem = buildSystem
3335
self.plugin = plugin
3436
self.lineBufferedOutput = Data()
3537
}
@@ -167,7 +169,7 @@ final class PluginDelegate: PluginInvocationDelegate {
167169
}
168170

169171
let buildSystem = try await swiftCommandState.createBuildSystem(
170-
explicitBuildSystem: .native,
172+
explicitBuildSystem: buildSystem,
171173
explicitProduct: explicitProduct,
172174
traitConfiguration: .init(),
173175
cacheBuildManifest: false,
@@ -398,7 +400,7 @@ final class PluginDelegate: PluginInvocationDelegate {
398400

399401
// Create a build system for building the target., skipping the the cache because we need the build plan.
400402
let buildSystem = try await swiftCommandState.createBuildSystem(
401-
explicitBuildSystem: .native,
403+
explicitBuildSystem: buildSystem,
402404
traitConfiguration: TraitConfiguration(enableAllTraits: true),
403405
cacheBuildManifest: false
404406
)

Tests/CommandsTests/PackageCommandTests.swift

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3466,7 +3466,14 @@ class PackageCommandTestCase: CommandsBuildProviderTestCase {
34663466
XCTAssertMatch(stdout, .contains("-DEXTRA_SWIFT_FLAG"))
34673467
XCTAssertMatch(stdout, .contains("Build of product 'MyExecutable' complete!"))
34683468
XCTAssertMatch(stdout, .contains("succeeded: true"))
3469-
XCTAssertMatch(stdout, .and(.contains("artifact-path:"), .contains(RelativePath("debug/MyExecutable").pathString)))
3469+
switch buildSystemProvider {
3470+
case .native:
3471+
XCTAssertMatch(stdout, .and(.contains("artifact-path:"), .contains(RelativePath("debug/MyExecutable").pathString)))
3472+
case .swiftbuild:
3473+
XCTAssertMatch(stdout, .and(.contains("artifact-path:"), .contains(RelativePath("MyExecutable").pathString)))
3474+
case .xcode:
3475+
XCTFail("unimplemented assertion for --build-system xcode")
3476+
}
34703477
XCTAssertMatch(stdout, .and(.contains("artifact-kind:"), .contains("executable")))
34713478
}
34723479

@@ -3478,7 +3485,14 @@ class PackageCommandTestCase: CommandsBuildProviderTestCase {
34783485
XCTAssertNoMatch(stdout, .contains("-module-name MyExecutable"))
34793486
XCTAssertMatch(stdout, .contains("Build of product 'MyExecutable' complete!"))
34803487
XCTAssertMatch(stdout, .contains("succeeded: true"))
3481-
XCTAssertMatch(stdout, .and(.contains("artifact-path:"), .contains(RelativePath("release/MyExecutable").pathString)))
3488+
switch buildSystemProvider {
3489+
case .native:
3490+
XCTAssertMatch(stdout, .and(.contains("artifact-path:"), .contains(RelativePath("release/MyExecutable").pathString)))
3491+
case .swiftbuild:
3492+
XCTAssertMatch(stdout, .and(.contains("artifact-path:"), .contains(RelativePath("MyExecutable").pathString)))
3493+
case .xcode:
3494+
XCTFail("unimplemented assertion for --build-system xcode")
3495+
}
34823496
XCTAssertMatch(stdout, .and(.contains("artifact-kind:"), .contains("executable")))
34833497
}
34843498

@@ -3490,7 +3504,14 @@ class PackageCommandTestCase: CommandsBuildProviderTestCase {
34903504
XCTAssertNoMatch(stdout, .contains("-module-name MyLibrary"))
34913505
XCTAssertMatch(stdout, .contains("Build of product 'MyStaticLibrary' complete!"))
34923506
XCTAssertMatch(stdout, .contains("succeeded: true"))
3493-
XCTAssertMatch(stdout, .and(.contains("artifact-path:"), .contains(RelativePath("release/libMyStaticLibrary").pathString)))
3507+
switch buildSystemProvider {
3508+
case .native:
3509+
XCTAssertMatch(stdout, .and(.contains("artifact-path:"), .contains(RelativePath("release/libMyStaticLibrary").pathString)))
3510+
case .swiftbuild:
3511+
XCTAssertMatch(stdout, .and(.contains("artifact-path:"), .contains(RelativePath("MyStaticLibrary").pathString)))
3512+
case .xcode:
3513+
XCTFail("unimplemented assertion for --build-system xcode")
3514+
}
34943515
XCTAssertMatch(stdout, .and(.contains("artifact-kind:"), .contains("staticLibrary")))
34953516
}
34963517

@@ -3502,11 +3523,18 @@ class PackageCommandTestCase: CommandsBuildProviderTestCase {
35023523
XCTAssertNoMatch(stdout, .contains("-module-name MyLibrary"))
35033524
XCTAssertMatch(stdout, .contains("Build of product 'MyDynamicLibrary' complete!"))
35043525
XCTAssertMatch(stdout, .contains("succeeded: true"))
3505-
#if os(Windows)
3506-
XCTAssertMatch(stdout, .and(.contains("artifact-path:"), .contains("release\\MyDynamicLibrary")))
3507-
#else
3508-
XCTAssertMatch(stdout, .and(.contains("artifact-path:"), .contains("release/libMyDynamicLibrary")))
3509-
#endif
3526+
switch buildSystemProvider {
3527+
case .native:
3528+
#if os(Windows)
3529+
XCTAssertMatch(stdout, .and(.contains("artifact-path:"), .contains(RelativePath("release/MyDynamicLibrary.dll").pathString)))
3530+
#else
3531+
XCTAssertMatch(stdout, .and(.contains("artifact-path:"), .contains(RelativePath("release/libMyDynamicLibrary").pathString)))
3532+
#endif
3533+
case .swiftbuild:
3534+
XCTAssertMatch(stdout, .and(.contains("artifact-path:"), .contains(RelativePath("MyDynamicLibrary").pathString)))
3535+
case .xcode:
3536+
XCTFail("unimplemented assertion for --build-system xcode")
3537+
}
35103538
XCTAssertMatch(stdout, .and(.contains("artifact-kind:"), .contains("dynamicLibrary")))
35113539
}
35123540
}
@@ -4069,10 +4097,7 @@ class PackageCommandSwiftBuildTests: PackageCommandTestCase {
40694097
override func testNoParameters() async throws {
40704098
try await super.testNoParameters()
40714099
}
4072-
4073-
override func testCommandPluginBuildingCallbacks() async throws {
4074-
throw XCTSkip("SWBINTTODO: Test fails because plugin is not producing expected output to stdout.")
4075-
}
4100+
40764101
override func testCommandPluginBuildTestability() async throws {
40774102
throw XCTSkip("SWBINTTODO: Test fails as plugins are not currenty supported")
40784103
}

0 commit comments

Comments
 (0)