How to run a Build-Phase script when building a standalone Swift Package in Xcode?

(Disclaimer: This information relates to Xcode 11. I haven’t tried any of this in the Xcode 12 betas to know if any of it is out of date.)

The Xcode project will be completely ignored by any package clients.

SwiftPM has no equivalent of custom schemes. There is --configuration debug and --configuration release and that is all. (Well, you can add stuff to .swiftpm for some development time tweaks, but clients will not inherit it and you cannot inject build steps.)

External scripts are not supported. The feature you would need to do this “right” (known usually as “extensible build tools” been much talked about but not implemented so far.

What I do for static analysis tools providing inline warnings is to script swift package generate-xcodeproj and the insertion of a build phase. Then I quickly run that when I want to find a bunch of warnings quickly. But I don’t keep the Xcode project around all the time.

If you have CI, just add it as a separate step outside of swift test.

Since SwiftLint is itself a package, an alternative workaround would be to add it as a dependency of your test target. Then you can use #file or other shenanigans to locate the produced executable and launch it with Process inside the tests. (But to be honest I don’t think static analysis belongs inside unit tests in the first place, although that is just my opinion.)

3 Likes