Skip to content

command plugins: Build command plugin dependencies for the host, not the target #7280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,21 @@ let package = Package(
description: "Build a target for testing"
))
),
.plugin(
name: "plugin-dependencies-stub",
capability: .command(intent: .custom(
verb: "build-plugin-dependency",
description: "Build a plugin dependency for testing"
)),
dependencies: [
.target(name: "plugintool")
]
),
.executableTarget(
name: "placeholder"
),
.executableTarget(
name: "plugintool"
),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import PackagePlugin

@main
struct test: CommandPlugin {
// This plugin exists to test that the executable it requires is built correctly when cross-compiling
func performCommand(context: PluginContext, arguments: [String]) async throws {
print("Hello from dependencies-stub")
let _ = try packageManager.build(
.product("placeholder"),
parameters: .init(configuration: .debug, logging: .concise)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello from plugintool")
6 changes: 5 additions & 1 deletion Sources/Commands/PackageTools/PluginCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,11 @@ struct PluginCommand: SwiftCommand {
// 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.
let buildSystem = try swiftTool.createBuildSystem(
explicitBuildSystem: .native,
cacheBuildManifest: false
cacheBuildManifest: false,
// Force all dependencies to be built for the host, to work around the fact that BuildOperation.plan
// knows to compile build tool plugin dependencies for the host but does not do the same for command
// plugins.
productsBuildParameters: buildParameters
)
let accessibleTools = try plugin.processAccessibleTools(
packageGraph: packageGraph,
Expand Down
39 changes: 39 additions & 0 deletions Tests/BuildTests/PluginsBuildPlanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

import Basics
import SPMTestSupport
@testable import SPMBuildCore
import XCTest
import PackageModel

final class PluginsBuildPlanTests: XCTestCase {
func testBuildToolsDatabasePath() throws {
Expand All @@ -22,4 +24,41 @@ final class PluginsBuildPlanTests: XCTestCase {
XCTAssertTrue(localFileSystem.exists(fixturePath.appending(RelativePath(".build/plugins/tools/build.db"))))
}
}

func testCommandPluginDependenciesWhenCrossCompiling() throws {
// Command Plugin dependencies must be built for the host.
// This test is only supported on macOS because that is the only
// platform on which we can currently be sure of having a viable
// cross-compilation environment (arm64->x86_64 or vice versa).
// On Linux it is typically only possible to build for the host
// environment unless cross-compilation SDKs are being used.
#if !os(macOS)
try XCTSkipIf(true, "test is only supported on macOS")
#endif

let hostToolchain = try UserToolchain(swiftSDK: .hostSwiftSDK())
let hostTriple = try! hostToolchain.targetTriple.withoutVersion().tripleString

let x86Triple = "x86_64-apple-macosx"
let armTriple = "arm64-apple-macosx"
let targetTriple = hostToolchain.targetTriple.arch == .aarch64 ? x86Triple : armTriple

// By default, plugin dependencies are built for the host platform
try fixture(name: "Miscellaneous/Plugins/CommandPluginTestStub") { fixturePath in
let (stdout, stderr) = try executeSwiftPackage(fixturePath, extraArgs: ["-v", "build-plugin-dependency"])
XCTAssertMatch(stdout, .contains("Hello from dependencies-stub"))
XCTAssertMatch(stderr, .contains("Build of product 'plugintool' complete!"))
XCTAssertTrue(localFileSystem.exists(fixturePath.appending(RelativePath(".build/\(hostTriple)/debug/plugintool"))))
XCTAssertTrue(localFileSystem.exists(fixturePath.appending(RelativePath(".build/\(hostTriple)/debug/placeholder"))))
}

// When cross compiling the final product, plugin dependencies should still be built for the host
try fixture(name: "Miscellaneous/Plugins/CommandPluginTestStub") { fixturePath in
let (stdout, stderr) = try executeSwiftPackage(fixturePath, extraArgs: ["--triple", targetTriple, "-v", "build-plugin-dependency"])
XCTAssertMatch(stdout, .contains("Hello from dependencies-stub"))
XCTAssertMatch(stderr, .contains("Build of product 'plugintool' complete!"))
XCTAssertTrue(localFileSystem.exists(fixturePath.appending(RelativePath(".build/\(hostTriple)/debug/plugintool"))))
XCTAssertTrue(localFileSystem.exists(fixturePath.appending(RelativePath(".build/\(targetTriple)/debug/placeholder"))))
}
}
}