Let BuildToolPlugin replace default build rules

It would be great if a BuildToolPlugin could specify that it replaces the default build rules for the files it handles.

Specifically, I want to put Metal kernels for Core Image into a package. They need to be compiled with certain compiler and linker flags. I figured I can now use a BuildToolPlugin to create a custom build step for those kernels. I found this example that does something similar. However, as also noted by them, the big annoyance here is that SPM will still process the .metal files with its default build rule, creating a default.metallib for the package.

As far as I know, there is no way of telling SPM to not also compile the .metal files that my plugin already handled. I thought a possible workaround would be to give the CI kernel files a different file extension like .ci, but the Metal compiler doesn't like that.

Is there a way to prevent SPM from running the default build rules for .metal files?

2 Likes

There's no way to do that today, but would you mind filing a GitHub issue asking for this?

1 Like

Sure, here it is: Let BuildToolPlugin replace default build rules · Issue #6124 · apple/swift-package-manager · GitHub

Thanks for looking into this!

1 Like

Hi,

Does the '-x metal' Metal compiler's flag can be useful here ?

Here is a simple naive Swift plugin to compile and link CIFilter metal files (with a .cimetal extension) into a specific metallib file (here shaders.metallib). Xcode and SPM do not seem to recompile these files.

Caveats: Xcode (14) syntax highlight for .cimetal files doesn't work even if you change the type of the file.