Use command-line tools to run the Metal compiler toolchain.
Framework
- Metal
Overview
Manually compiling Metal Shading Language source code and building a Metal library without using Xcode involves the following process:
Use the
metaltool to compile each.metalfile into a single.airfile, which stores an intermediate representation of Metal Shading Language source code.Optionally, use the
metal-artool to archive several.airfiles together into a single.metalarfile. (Themetal-artool is similar to the UNIXartool.)Use the
metallibtool to build.airor.metalarfiles into a single.metallibfile, which stores the Metal library.

Run Metal's Command-Line Tools
The following code shows the minimum number of commands that you need to compile and build a single .metal file into a single .metallib file. You can run these commands in the Terminal app and use the -help command to display the available options for each Metal tool. This example uses the macosx SDK, but you can use the iphoneos or appletvos SDK instead.
xcrun -sdk macosx metal -c MyLibrary.metal -o MyLibrary.air
xcrun -sdk macosx metallib MyLibrary.air -o MyLibrary.metallib
Retrieve and Access a Built Library
After you've built a library with Metal's command-line tools, add the resulting .metallib file to your Xcode project. Then, at runtime, call the make method to retrieve and access your library as a MTLLibrary object.
guard let libraryFile = Bundle.main.path(forResource: "MyLibrary", ofType: "metallib") else { return }
do {
let myLibrary = try device.makeLibrary(filepath: libraryFile)
} catch let error {
print("Library error: \(error.localizedDescription)")
}