Skip to content

Commit 14c3d61

Browse files
authored
Prebuild plugin support for swiftbuild build system (#9209)
Add prebuild command support for build tool plugins with the swiftbuild build system.Include a test case that verifies that referring to a tool through context fails in both major build systems. Test that tools made available through binary artifact dependencies with an artifact bundle that works with both build systems.
1 parent d3c1175 commit 14c3d61

File tree

11 files changed

+304
-115
lines changed

11 files changed

+304
-115
lines changed

Fixtures/Miscellaneous/Plugins/MyBinaryToolPlugin/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.6
1+
// swift-tools-version: 6.1
22
import PackageDescription
33

44
let package = Package(

Fixtures/Miscellaneous/Plugins/MyBinaryToolPlugin/Plugins/MySourceGenBuildToolPlugin/plugin.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ struct MyPlugin: BuildToolPlugin {
77
print("Hello from the Build Tool Plugin!")
88
guard let target = target as? SourceModuleTarget else { return [] }
99
let inputFiles = target.sourceFiles.filter({ $0.path.extension == "dat" })
10+
let workDir = context.pluginWorkDirectoryURL
11+
1012
return try inputFiles.map {
1113
let inputFile = $0
1214
let inputPath = inputFile.path
@@ -29,6 +31,16 @@ struct MyPlugin: BuildToolPlugin {
2931
outputPath
3032
]
3133
)
32-
}
34+
} + [
35+
.prebuildCommand(
36+
displayName:
37+
"Generating files in \(workDir.path)",
38+
executable:
39+
try context.tool(named: "mytool").url,
40+
arguments:
41+
["--verbose", "\(target.directoryURL.appendingPathComponent("bar.in").path)", "\(workDir.appendingPathComponent("bar.swift").path)"],
42+
outputFilesDirectory: workDir
43+
)
44+
]
3345
}
3446
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let bar = "I am Bar!"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
print("Generated string Foo: '\(foo)'")
2+
print("Generated string Bar: '\(bar)'")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// swift-tools-version: 6.3
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "PrebuildDependsExecutableTarget",
7+
platforms: [ .macOS(.v13) ],
8+
dependencies: [
9+
],
10+
targets: [
11+
.executableTarget(name: "MyExecutable"),
12+
13+
.plugin(
14+
name: "MyPlugin",
15+
capability: .buildTool(),
16+
dependencies: [
17+
"MyExecutable"
18+
]
19+
),
20+
21+
.executableTarget(
22+
name: "MyClient",
23+
plugins: [
24+
"MyPlugin",
25+
]
26+
),
27+
],
28+
swiftLanguageModes: [.v5]
29+
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import PackagePlugin
2+
import Foundation
3+
4+
@main
5+
struct MyPlugin: BuildToolPlugin {
6+
func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
7+
let outputFilePath = context.pluginWorkDirectoryURL.appendingPathComponent("MyGeneratedFile.swift")
8+
9+
// We are attempting to assemble a prebuild command that relies on an executable that hasn't
10+
// been built yet. This should result in an error in prebuild.
11+
let myExecutable = try context.tool(named: "MyExecutable")
12+
13+
return [
14+
.prebuildCommand(
15+
displayName: "Prebuild that runs MyExecutable",
16+
executable: myExecutable.url,
17+
arguments: ["--output-file-path", outputFilePath.path],
18+
outputFilesDirectory: context.pluginWorkDirectoryURL
19+
)
20+
]
21+
}
22+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@main
2+
struct Client {
3+
static func main() throws {
4+
print(MyGeneratedStruct.message)
5+
}
6+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Foundation
2+
3+
@main
4+
struct Tool {
5+
static func main() async throws {
6+
print("warning: Whoops! Coming from the executable", to: &StdErr.shared)
7+
8+
let path = CommandLine.arguments[2]
9+
print("Writing a file to \(path)")
10+
11+
try #"""
12+
public struct MyGeneratedStruct {
13+
public static var message: String = "You got struct'd"
14+
}
15+
"""#.write(to: URL(fileURLWithPath: path), atomically: true, encoding: .utf8)
16+
}
17+
}
18+
19+
struct StdErr: TextOutputStream {
20+
static var shared: Self = .init()
21+
mutating func write(_ string: String) {
22+
string.withCString { ptr in
23+
_ = fputs(ptr, stderr)
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)