# Let BuildToolPlugin replace default build rules

**URL:** <https://forums.swift.org/t/let-buildtoolplugin-replace-default-build-rules/62968>\
**Category:** Package Manager\
**Tags:** packagemanager, buildtoolplugin, metal\
**Created:** [February 6, 2023, 8:15am UTC](https://forums.swift.org/t/let-buildtoolplugin-replace-default-build-rules/62968 "2023-02-06T08:15:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fuzzyhead](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/fuzzyhead/32/28653_2.png) [@fuzzyhead](https://forums.swift.org/u/fuzzyhead)\
**Post date:** [February 6, 2023, 8:15am UTC](https://forums.swift.org/t/let-buildtoolplugin-replace-default-build-rules/62968/1 "2023-02-06T08:15:35Z")

</div>

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](https://github.com/schwa/MetalCompilerPlugin) 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?

---

<div class="post-metadata">

**Author:** ![NeoNacho](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/neonacho/32/1368_2.png) [@NeoNacho](https://forums.swift.org/u/NeoNacho)\
**Post date:** [February 6, 2023, 6:55pm UTC](https://forums.swift.org/t/let-buildtoolplugin-replace-default-build-rules/62968/2 "2023-02-06T18:55:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fuzzyhead](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/fuzzyhead/32/28653_2.png) [@fuzzyhead](https://forums.swift.org/u/fuzzyhead)\
**Post date:** [February 6, 2023, 7:20pm UTC](https://forums.swift.org/t/let-buildtoolplugin-replace-default-build-rules/62968/3 "2023-02-06T19:20:58Z")

</div>

Sure, here it is: [Let BuildToolPlugin replace default build rules · Issue #6124 · apple/swift-package-manager · GitHub](https://github.com/apple/swift-package-manager/issues/6124)

Thanks for looking into this!

---

<div class="post-metadata">

**Author:** ![franckbrun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/franckbrun/32/29166_2.png) [@franckbrun](https://forums.swift.org/u/franckbrun)\
**Post date:** [October 14, 2023, 1:13pm UTC](https://forums.swift.org/t/let-buildtoolplugin-replace-default-build-rules/62968/4 "2023-10-14T13:13:28Z")

</div>

Hi,

> [@fuzzyhead](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![franckbrun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/franckbrun/32/29166_2.png) [@franckbrun](https://forums.swift.org/u/franckbrun)\
**Post date:** [October 20, 2023, 8:35am UTC](https://forums.swift.org/t/let-buildtoolplugin-replace-default-build-rules/62968/5 "2023-10-20T08:35:21Z")

</div>

> **[Compile CIFilter in Swift Package](https://devjourney.moxadventu.com/macos/2023/10/19/cimetal-plugin.html)**
>
> Create a Swift Package plugin to compile CIFilter metal file (which requires the -fcimetal flag).

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.

---

<div class="post-metadata">

**Author:** ![JuniperPhoton](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/juniperphoton/32/26944_2.png) [@JuniperPhoton](https://forums.swift.org/u/JuniperPhoton)\
**Post date:** [May 25, 2025, 8:59pm UTC](https://forums.swift.org/t/let-buildtoolplugin-replace-default-build-rules/62968/6 "2025-05-25T20:59:30Z")

</div>

After some investigation, I found that you can just exclude the shaders in the target declaration:

```swift
targets: [
    .target(
        name: "...",
        exclude: [
            "Shaders/"
        ],
        plugins: [
            .plugin(name: "...", package: "...")
        ]
    )
]

```

Then, the default build tool will just ignored the .metal files inside the Shaders directory in this case. Of course, those files should be built in the SPM Plugin itself.
