Delivering binary code by SPM with fastlane

Brenno de Moura 🏳️‍🌈
3 min readSep 11, 2021

--

Distributing Swift code via binary is a great way to make code proprietary. With the popularization of SPM, it is possible to find several proprietary repositories without Apple’s manager support.

Creating a binary module is highly recommended for public libraries with protected code, for companies and developers who want to preserve their intellectual property. Also, this is not always the main goal, as binary libraries are not recompiled, speeding up the project’s build time.

The SPM does not natively support this type of implementation, it only allows you to create a repository to distribute xcframework files stored in the repository itself or on a server. Despite this limitation, it is possible to use binary code with SPM and in projects in general.

Fastlane

Fastlane is a command-line platform that automates development processes. It contains a series of functions that allow you to compile, distribute applications, send messages in Slack, and several other functions that make the integration and distribution process very simple.

Initially, as a study of this article, other techniques were used via xcodebuild that were not successful. In fact, there are several articles in the medium about compiling an SPM for binary that didn’t generate the expected result.

Fastlane contains the create_xcframework function that works correctly, generating the binary for Apple’s various platforms and fully functional. The only problem that was found is that the code needs to use Swift’s @inlinable, @usableFromInline, and @frozen notations.

This was because the functions were not fully visible to the application, even marked as public. Maybe it’s a compilation bug or some configuration that was missing. If you have any problems in this regard, these notations are worth knowing.

The script

Implementing the script for fastlane requires the use of two functions and the correct configuration of Package.swift. After that, the result is obtained with fastlane generating the xcframework correctly.

For lack of SPM support in this regard, it is necessary to turn the module into an xcode project. For this, it is necessary to add spm(command: "generate-xcodeproj") in the fastlane script.

After generating the project, you need to call the function to compile the xcframework. Figure 1 details the use of fastlane functions to build binary from a module in SPM.

Figure 1 — Fastfile file for xcframework compilation.

The module name in this example is CoreKit and when generating the project via the SPM command, the Apple manager configures the scheme with the module name plus the suffix -Package. In destinations, all platforms supported by the module must be specified.

Final considerations

This script made the distribution process via xcframework very dynamic and, even with some customizations of the fastlane script, it was possible to address other interesting aspects of distribution. Although it was necessary to add Swift’s compilation notations to make the code properly available, this ensured the integrity of the binary.

This solution isn’t the best, but it helps make code private with ease. Also, the goal was long despite the limitations of SPM with xcframework.

Thanks for reading this far!

If you would like to contribute so that I can continue producing more technical content, please feel free to buy me a coffee ☕️ through the Buy me a Coffee platform.

Your support is essential to maintain my work and contribute to the development community.

--

--

Brenno de Moura 🏳️‍🌈

Software engineer with a passion for technology and a focus on declarative programming, experience in challenging projects and multidisciplinary teams