The introduction of macros to Swift has given developers the ability to create more expressive libraries that can be distributed as a Swift package. Macros can greatly improve the developer ergonomics making Swift packages an even more appealing environment in which to work.
One issue developers run into when adding dependencies on these packages is that build times all of a sudden get significantly longer. This is especially true when building in release mode and on CI machines with limited CPU and memory. There has been much discussion here: Compilation extremely slow since macros adoption, where people have reported extended build times of 5 minutes or more.
The key contributing factor to this build performance is the need to build the swift-syntax library that is the foundation of macro implementations. It’s a fairly large amount of Swift code required to create and present the Swift syntax tree to the macro.
As discussed at length here Distribute a prebuilt swift-syntax binary · Issue #2421 · swiftlang/swift-syntax · GitHub, one possible solution would be to provide a prebuilt library for swift-syntax that could be downloaded by Swift Package Manager and integrated into the build for macros. The good news, if you follow along with the SwiftPM PRs, is that’s what I’ve been working on for the last few months. With one final fix in Swift 6.1.1, it’s now ready for the community to try out.
Supporting prebuilt binaries for Swift can be challenging but SwiftPM takes care of that for you. It will download the prebuilt binary for the resolved version of swift-syntax, for the current running host platform and for the current version of the compiler and integrate them into the build. If prebuilts aren’t available for that combination, SwiftPM will build from source as it does today. Note that currently we have prebuilts for swift-syntax versions 600.0.1 and 601.0.1.
I encourage you to give it a try. I have added a flag to swift build
and swift test
to enable the feature:
swift build --enable-experimental-prebuilts
You should see an extra message when SwiftPM is downloading the zip file containing the prebuilts, and of course, your build should be faster, especially if you use -c release
. You may also see your macros run faster since the swift-syntax prebuilts are built in release mode to help improve performance.
It is also available in Xcode 16.4 by enabling a user default.
defaults write com.apple.dt.Xcode IDEPackageEnablePrebuilts YES
Set it to NO
to disable it or delete that key.
defaults delete com.apple.dt.Xcode IDEPackageEnablePrebuilts
Please report any problems you have to the SwiftPM Github Issues, GitHub · Where software is built. And, of course, I'd also love to hear if this is helping you. It has been fun to test it and I hope you find the same.