Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Build Metal Library from Shell
#!/bin/bash
# This is a gist from
# mtldoc.com
#
# If you want to know what the hell is happening here check
# https://mtldoc.com/metal/2022/06/18/build-swift-executable-with-metal-library.html
# Create intermediate representation from Metal files
METAL_FILES=`ls /path/to/metal/files/*.metal`
for path in $METAL_FILES
do
FILE_NAME=${path%%.*}
xcrun -sdk macosx metal -c $path -o ${FILE_NAME}.air
done
# Archive AIR files to single MetalAR file
AIR_FILES=`ls /path/to/air/files/*.air`
for path in $AIR_FILES
do
xcrun metal-ar r default.metalar $path
done
# Build Metal Library from MetalAR archive
xcrun -sdk macosx metallib default.metalar -o default.metallib
# Clean up
rm default.metalar
for path in $AIR_FILES
do
rm $path
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment