Search code examples
swiftxcodeuikitxcodebuildswift-package-manager

How can I build a Swift Package for iOS over command line?


In Xcode, I can select my destination as a "generic iOS device" or any iOS simulator, and my package will build platform-specific code for ios.

Via command line "swift build" only builds my target for macOS.

I want to build the target for iOS for CI purposes. The problem with building for macOS is that UIKit-specific code won't be built.

For example:

#if canImport(UIKit)
    // some invalid code
#endif

The invalid code will not be noticed and will pass the build phase.

Ideally, I could say something like swift build -platform iOS. Is there a way to do something like this?


Solution

  • At time of writing (Feb 16, 2019), a working solution is:

    swift build -v \
        -Xswiftc "-sdk" \
        -Xswiftc "`xcrun --sdk iphonesimulator --show-sdk-path`" \
        -Xswiftc "-target" \
        -Xswiftc "x86_64-apple-ios13.0-simulator"
    

    This command uses -Xswiftc to workaround the issue by overriding the sdk from macOS to iphonesimulator.

    Strictly we add these flags so developers can work around issues, but they also should report a bug so that we can provide a proper solution for their needs.

    Source

    So I'm guessing there will be a more elegant solution in the future.