Recently I've moved some graphics and UI code of my app into Swift Package for organization purpose, and am hitting various issues. For now, I'm trying to make the shader code in the package debuggable in my app, as SPM by default doesn't include debug information in the auto-generated metallib.
Through research I'm trying to use a custom BuildToolPlugin
, but the plugin doesn't run when I trigger build in XCode for my app project.
My package.swift
looks like this:
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "Slate",
platforms: [.iOS(.v18)],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(name: "Slate", targets: ["Slate"]),
// This was for debugging - exposing to make it visible and let my app add it in Build Phases.
// The plugin runs, but against the app, not "Slate", of course.
.plugin(name: "MetalDebugSymbolGen", targets: ["MetalDebugSymbolGen"]),
],
dependencies: [.package(url: "https://github.com/apple/swift-algorithms", from: "1.2.0")],
targets: [
.target(name: "cslate"),
.target(
name: "Slate",
dependencies: [
.target(name: "cslate"), .product(name: "Algorithms", package: "swift-algorithms"),
],
plugins: [
.plugin(name: "MetalDebugSymbolGen")
]
), .testTarget(name: "SlateTests", dependencies: ["Slate"]),
.plugin(name: "MetalDebugSymbolGen", capability: .buildTool()),
]
)
And my app project adds a package dependency to the local package Slate
.
These are some build logs that seem relevant:
Note that the compile and link are not from my plugin but the build tool's default behavior.
Why is the plugin not running when the Slate
package is being built in XCode as a dependency? Is this expected? What is a correct approach to what I'm trying to do?
I also tried to use GitHub - schwa/MetalCompilerPlugin: Swift Package Manager plug-in to compile Metal files that can be debugged in Xcode Metal Debugger. but it doesn't run either.
Essentially, I'm trying to do something described in the following (source):
To introduce a custom Metal compilation step to the build process, create a Swift Package Build Tool Plugin that invokes the
metal
command-line tool with custom arguments, precompiles a.metallib
, and stores it in the target’s resources directory by specifying it as an output file of the build command. Then, apply the plugin to the target that contains your.metal
files.
Thanks for your help!