Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 316
Michael Avatar asked Feb 16 '20 03:02

Michael


People also ask

How do I create a Swift package?

To create a new Swift package, open Xcode and select File > New > Swift Package. Choose a name and select a file location. Select “Create Git repository on my Mac” to put your package under version control. On completion, the Swift package opens in Xcode and looks similar to a standard Xcode project.

What is Xcode build command?

xcodebuild is a command-line tool that allows you to perform build, query, analyze, test, and archive operations on your Xcode projects and workspaces from the command line. It operates on one or more targets contained in your project, or a scheme contained in your project or workspace.

How do I add a Swift package to an existing project?

To add a new Swift package dependency: Select your project in the Project navigator, then select your app target and navigate to its General pane. Click the + button in the "Frameworks, Libraries, and Embedded Content" section, select the local package's library product, and add it as a dependency.

Does Swift package manager support iOS?

The Swift Package Manager can be used to add external libraries in Xcode projects. This makes the tool a good alternative to Cocoapods and Carthage for managing the dependencies of an iOS project.


2 Answers

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.

like image 179
Michael Avatar answered Sep 25 '22 03:09

Michael


Starting with Xcode 11, xcodebuild supports SwiftPM packages out of the box.

An example invocation would look like this:

xcodebuild -scheme Foo \
  -destination 'platform=iOS Simulator,OS=13.5,name=iPhone 11 Pro'

where Foo would be the name of the library product you're trying to build. You can get the full list of available schemes for you SwiftPM package with xcodebuild -list. You can get the list of available destinations for a given scheme with this invocation:

xcodebuild -showdestinations -scheme Foo
like image 42
Max Desiatov Avatar answered Sep 25 '22 03:09

Max Desiatov