How to Build Optimized Version of Swift Package Using the Swift Package Manager 'Swift Build' Command

How to build optimized version of Swift Package using the Swift Package Manager `swift build` command

You can build in a release configuration with: swift build --configuration release.

You can also see the tool usage via:

$ swift build --help
OVERVIEW: Build sources into binary products

USAGE: swift build [mode] [options]

MODES:
-c, --configuration <value> Build with configuration (debug|release) [default: debug]
--clean [<mode>] Delete artifacts (build|dist) [default: build]

OPTIONS:
-C, --chdir <path> Change working directory before any other operation
--build-path <path> Specify build/cache directory [default: ./.build]
--color <mode> Specify color mode (auto|always|never) [default: auto]
-v, --verbose Increase verbosity of informational output
-Xcc <flag> Pass flag through to all C compiler invocations
-Xlinker <flag> Pass flag through to all linker invocations
-Xswiftc <flag> Pass flag through to all Swift compiler invocations

NOTE: Use `swift package` to perform other functions on packages

which lists this option.

See the package manager reference for more information.

Swift on Linux: how to specify compiler optimizations

It is possible to provide the -O, -Onone, and -Ounchecked optimization flags to the Swift compiler, swiftc. However, it appears that there is currently no way to specify additional flags to swift build. See, for example, the following link, even though it is not directly related:
https://bugs.swift.org/browse/SR-397. The same bug report suggests that the team is actively working on adding this missing functionality.

One way that I found to work around the problem is to run swift build -v, find the first command that references -Onone, copy it and all the commands that follow it to a shell script, edit the script to use the desired optimization level instead of -Onone, and run the script. This should re-compile the Swift sources using the desired optimization level and rebuild the executable.

In my testing I found that a simple example involving sorting an array runs a couple of orders of magnitude faster if built using -O or -Ounchecked instead of -Onone.

How to create and build a swift package

UIKit is a framework in iOS and won't be accessible.

#if canImport(UIKit)

// Code specific to platforms where UIKit is available

#endif

Related:

  • Creating a Swift Package with Xcode

Test SPM Package using optimization flag

The magic terminal command is: swift test -Xswiftc -O

Specify minimum macOS version for Swift Package Manager with Swift 5

let package = Package(
name: "NAME",
platforms: [
.macOS(.v10_11)
],
products: [
.library(name: "NAME", targets: ["NAME"]),
],
targets: [
.target(name: "NAME"),
]
)

One way to do this is with Deployment Settings in SPM.

How to improve build times with Firebase in Xcode

If you're optimizing for build time, I suggest that you integrate Firebase as a binary framework rather than an SPM dependency.

I used to have similar build time issues with Firebase on relatively small apps. While incremental build times were tolerable, cold builds on CI were painfully long and that was the turning point for me. After integrating Firebase as a binary .xcframework my cold build times on CI dropped from 30-45 minutes down to 5 minutes.

  1. Go to Firebase Documentation.
  2. In the section that explains how to integrate it without a dependency manager you'll find a link to a .zip archive containing all Firebase frameworks.
  3. Download the archive. The documentation claims that it's about 200 Mb in size however the one I recently download was almost 370 Mb.
  4. Follow the instructions in Readme.md that you'll find inside the archive.

It's a bit painful to set it up for the first time and is a bit annoying to update Firebase manually from time to time, but for me the drop in build times was totally worth it.

Debugging Swift Package Manager dependances

It looks like you can use editable packages. Not sure how debugging this works though.

After running `swift build` dependencies are downloaded by class is not found

This is probably because your class is not public.

In MyXMLHelper.swift, it should be declared like this:

public class MyXMLHelper {

or, if you want to be able to subclass it (Xcode 8 beta 6):

open class MyXMLHelper {


Related Topics



Leave a reply



Submit