Skip to content

Instantly share code, notes, and snippets.

@petertretyakov
Last active June 18, 2022 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petertretyakov/3fe54fc12d7f585f27610841616e8b5f to your computer and use it in GitHub Desktop.
Save petertretyakov/3fe54fc12d7f585f27610841616e8b5f to your computer and use it in GitHub Desktop.
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