With support for SE-0387 in the works, some of the most important pieces are finally in place to make it super easy to cross-compile Darwin apps on Linux. To show this off, I've put together a proof of concept SDK for compiling iOS and macOS apps on Ubuntu:
Once you build and install the SDK, using it is as simple as
swift build --experimental-swift-sdk ios16
Scope
Keep in mind that this doesn't aim to augment SwiftPM (specifically swift-build)'s feature set, so the result of swift build
will be an executable/.dylib/.a MachO file. If you build an .executableTarget
with an @main
, this is all you need to get a SwiftUI Mac app going!
The story is a bit trickier on iOS: you need to bundle the executable into a .app, codesign it, and then deploy it. All of this is possible too (you can sign with sdk/toolset/bin/ldid and deploy with ideviceinstaller), but automating it is out of scope for this SDK. I do plan to simplify this in the future, but that's another project for another day.
Finally, there's no support for running iOS/macOS unit tests on Linux since simulating/emulating a Darwin environment is a whole different ballgame. The non-solution solution is to make your core logic platform-agnostic and run unit tests natively on Linux for now.
Caveats
Support for Swift SDKs in SwiftPM is highly experimental atm, so the SDK has a few workarounds for this. macOS works relatively well already, but iOS support is a bit buggier, using unspeakable hacks to get SwiftPM to understand what iOS even is; for example iOS xcframeworks don't work. There are open SwiftPM PRs (some mine, some from others) around these issues so some of them should be resolved soon.
In general, Swift SDK support is incomplete atm so don't count on everything working smoothly until the "experimental" label is dropped. For example, SourceKit LSP doesn't seem to support SDKs yet so if you want to get that working you need yet more hacks (it's possible but shall be left as an exercise for the reader). That it's possible to build a SwiftUI app on Linux despite the WIP nature of this feature is pretty amazing.
Future Directions
As more of the Swift SDK spec is implemented, it should help simplify installation and usage. I'd eventually like to build a tool for one-click building, packaging, signing, and deployment — if you're interested in using/contributing to this, let me know!
Shameless plug: I'm doing a talk on cross-platform Swift at Swift TO next month, where I'll talk about this and more. Check it out if you're in the area!