How to combine .metal and .swift in the same package

Viewed 86

I created a MacOS command line app that defines and successfully calls a Metal kernel. I'm now trying to move this app's .metal and .swift logic into its own package so that it can used in other projects. I expected I would be able to create a Swift package, add my .metal and .swift logic and build/test it with no issues but this has not been the case.

In Xcode I created a new package (File->New->Package). After the package was created I tried to add a Metal file (right-click on sources -> New File). I opened the macOS tab in the window and tried to find a .metal template file but there was no result. I then just tried copy/pasting my .metal file into the sources and that worked but when I went to build I got an error

CompileSwiftSources normal x86_64 com.apple.xcode.tools.swift.compiler  (in target '...' from project '...')
...
swiftc -incremental -module-name ...
...
Command CompileSwiftSources failed with a nonzero exit code

How can I add and build .metal files as part of my Xcode Swift package? I can't seem to find what I'm missing. I've found examples of packages on GitHub that have a combination of Metal and Swift in them and I managed to get this one (Metal in sources/other) to compile on my computer in Xcode.

1 Answers

Have you updated your Swift package file?

.metallib file (Bundle.module) is only available once you have specified your resources in the package manifest.

Package.swift:

.target(
      name: "TargetName",
      dependencies: [],
      resources: [.process("ThePathToYour/Shader.metal")]
 )
Related