Whenever I build a project that includes a metal shader to an x86_64 target (iOS simulator), I get a dependency analysis warning:
warning: no rule to process file '[File Path]/Shaders.metal' of type sourcecode.metal for architecture x86_64
I know this isn't a huge issue but I like to keep my projects free from warnings when I build, so that when a real issue does arise, I actually notice the yellow warning triangle.
Any quick way to get Xcode to ignore metal files for simulator targets?
You can resolve this by precompiling your .metal file into a Metal library during the build step, and removing the .metal source code from your app target.
Select your .metal file in the project navigator, and uncheck the target that is giving you the warning.
Create a bash script called CompileMetalLib.sh in your project, alongside your .metal file, with contents like this:
xcrun -sdk iphoneos metal -c MyShader.metal -o MyShader.air
xcrun -sdk iphoneos metallib MyShader.air -o MyShader.metallib
rm MyShader.air
Make sure to give it executable permissions by running chmod +x CompileMetalLib.sh
.
MyShader.air
is the intermediate compile step, and MyShader.metallib
is the fully compiled metal library. Read all about compiling a Metal file here
If you're compiling for OS X, change iphoneos
to macosx
.
Now you'll want to trigger this script in your Build Phases.
Add a New Run Script Phase to your target. The contents should look like this:
cd ${SRCROOT}/path/to/folder/containing/yourshader
./CompileMetalLib.sh
It's important to drag this step so that it happens before the Copy Bundle Resources step.
You'll now load the compiled Metal library from your app bundle.
Here's some pseudo-Swift:
let metalLibPath = Bundle.main.path(forResource: "MyShader", ofType: "metallib")
let myLibrary = try metalDevice.makeLibrary(filepath: metalLibPath)
You are manually compiling your .metal file with an external script, and copying the compiled library into your bundle resources. Your code loads this library. Now that you don't have any .metal files in your target, the simulator will no longer throw a warning about not being able to compile for x86_64.
As of Xcode 11, Simulator supports Metal when running on macOS Catalina. Metal files are supported during the build, including when running on macOS Mojave or when building with an older deployment target. Metal won't be functional in those scenarios but you no longer need to exclude files from the build. (Of course when running iOS 13 / tvOS 13 simulators on 10.15 Metal actually works).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With