Swift for Android: Call for the Community

Took me awhile, but there's now a prebuilt Swift 5.2.4 toolchain available to natively build Swift packages on Android AArch64 phones and tablets. Simply install the Termux app for Android and run pkg install swift and you have a working Swift compiler, along with Foundation and the other corelibs plus the package manager (no lldb REPL or sourcekit-lsp yet):

> pkg install swift
> swift --version
Swift version 5.2.4 (swift-5.2.4-RELEASE)
Target: aarch64-unknown-linux-android

> git clone https://github.com/apple/swift-argument-parser.git
> cd swift-argument-parser/
> time swift build -j 9
[53/53] Linking math

real    0m12.384s
> time swift test -j 9 --enable-test-discovery --parallel
[59/59] Linking swift-argument-parserPackageTests.xctest
[298/298] Testing ArgumentParserEndToEndTests.RepeatingEndToEndTests/testParsing_repeatingArgu…

real    0m16.606s

The time command shows how long it takes to build and run the tests of a pure Swift package on mid-range Android hardware.

This native toolchain port builds on the Android stdlib port that started getting merged into mainline 4-5 years ago by Zhuowei Zhang and Brian Gesiak and continued by Amr Aboelela, along with others, before being maintained and kept up to date with an Android stdlib CI by @compnerd and @drodriguez in recent years.

My Swift 5.2 patches are easily accessible, and about a quarter have already been merged into mainline. The remaining mostly patch the build scripts and some of the CMake config, as the corelibs already have good support for cross-compilation (Termux packages are cross-compiled from linux x86_64 using the Android NDK). Some description of the yet unmerged patches follows:

  1. The stdlib CMake config supports cross-compiling the stdlib with a prebuilt Swift compiler, but some pulls last year broke parts of it for Android, so it doesn't work without one build-script flag or doesn't apply other linker flags that it used to. Also, it has never worked if you try to only cross-compile the stdlib and not build the host stdlib. I've submitted a pull to fix these problems, but it has sat in limbo without proper review for three months now.
  2. The current build-script supports cross-compiling the Swift compiler and stdlib, but not the corelibs and package manager, so I added support for cross-compiling those also.
  3. The build-script always builds the toolchain natively for the host first (with the single aforementioned exception of a flag to build the stdlib with a prebuilt Swift compiler instead) and then builds everything else with it: I disabled that and use a prebuilt official release of the Swift 5.2 compiler to cross-compile the corelibs and package manager too.
  4. Several small tweaks like removing unnecessary rpaths from the package manager and configuring Swift to use the Termux clang, instead of its own.

I'm not sure how much interest there is in merging these remaining patches that would be generally useful. If anyone has any problems with this Swift package on Android 7 or 8, let me know, as I do all my testing on Android 9.

5 Likes