Had been a while since I looked into generating Xcode projects from a Swift package. On my latest use of the generate-xcodeproj subcommand, I was greeted by a nice warning surprise…
swift package generate-xcodeproj
Xcode can handle Swift packages directly. Similarly, xcodebuild can handle them too. This isn't new. It's likely been available since Xcode 11. I just totally missed it.
Note: I've yet to dig into Xcode 13 beta, as Swift packages may already support the build/test features I was after in xcodebuild (like build/test on Catalyst).
In any case, on to xcodebuild… but let's first create a brand new Swift package.
mkdir FooBar && cd FooBar
swift package init --type library
We can use xcodebuild to list the available schemes.
xcodebuild -list
Similarly, we can list destinations supported for the schemes.
xcodebuild -showdestinations -scheme FooBar
Let's build for macOS, though let's first import UIKit into FooBar.swift. This ensures we get an expected failure when building for macOS.
import UIKit
struct FooBar {
var text = "Hello, World!"
}
Now let's attempt to build it…
xcodebuild build -quiet -scheme FooBar -destination 'platform=macOS'
The failure expected as UIKit isn't available on your typical macOS builds.
We do, however, have Catalyst available, so we can use its variant to build for macOS with UIKit support, and.. voilà!
xcodebuild build -quiet -scheme FooBar -destination 'platform=macOS,variant=Mac Catalyst' && echo \\o/