Skip to content

Commit

Permalink
Update the doc for the latest patch release and NDK
Browse files Browse the repository at this point in the history
  • Loading branch information
finagolfin committed Oct 29, 2024
1 parent af78fed commit 1f03b5d
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ emulator](https://github.com/finagolfin/swift-android-sdk/blob/main/.github/work
## Cross-compiling and testing Swift packages with the Android SDK bundle

To build with the Swift 6 SDK bundle, first download [the official open-source
Swift 6.0.1 toolchain for linux or macOS](https://swift.org/download/#releases)
Swift 6.0.2 toolchain for linux or macOS](https://swift.org/download/#releases)
(make sure to install the Swift dependencies linked there). Install the OSS
toolchain on macOS as detailed in [the instructions for using the static linux
Musl SDK bundle at swift.org](https://www.swift.org/documentation/articles/static-linux-getting-started.html).
Expand All @@ -19,27 +19,18 @@ On linux, simply download the toolchain, unpack it, and add it to your `PATH`.
Next, install the Android SDK bundle by having the Swift toolchain directly
download it:
```
swift sdk install https://github.com/finagolfin/swift-android-sdk/releases/download/6.0.1/swift-6.0.1-RELEASE-android-24-0.1.artifactbundle.tar.gz --checksum 28d019e91902681e04bf62b9535888441aa7c0cc96902940964013f29020d100
swift sdk install https://github.com/finagolfin/swift-android-sdk/releases/download/6.0.2/swift-6.0.2-RELEASE-android-24-0.1.artifactbundle.tar.gz --checksum d75615eac3e614131133c7cc2076b0b8fb4327d89dce802c25cd53e75e1881f4
```
or alternately, download the SDK bundle with your favorite downloader and install
it separately:
```
> wget https://github.com/finagolfin/swift-android-sdk/releases/download/6.0.1/swift-6.0.1-RELEASE-android-24-0.1.artifactbundle.tar.gz
> sha256sum swift-6.0.1-RELEASE-android-24-0.1.artifactbundle.tar.gz
28d019e91902681e04bf62b9535888441aa7c0cc96902940964013f29020d100 swift-6.0.1-RELEASE-android-24-0.1.artifactbundle.tar.gz
> swift sdk install swift-6.0.1-RELEASE-android-24-0.1.artifactbundle.tar.gz
> wget https://github.com/finagolfin/swift-android-sdk/releases/download/6.0.2/swift-6.0.2-RELEASE-android-24-0.1.artifactbundle.tar.gz
> sha256sum swift-6.0.2-RELEASE-android-24-0.1.artifactbundle.tar.gz
d75615eac3e614131133c7cc2076b0b8fb4327d89dce802c25cd53e75e1881f4 swift-6.0.2-RELEASE-android-24-0.1.artifactbundle.tar.gz
> swift sdk install swift-6.0.2-RELEASE-android-24-0.1.artifactbundle.tar.gz
```
You can check if it was properly installed by running `swift sdk list`.

You will have to modify SwiftPM 6.0.1 on linux and macOS for a recent regression
when cross-compiling a package's tests for Android:
```
perl -pi -e 's%canImport\(Bionic%canImport\(Android%' swift-6.0.1-RELEASE-ubuntu22.04/usr/bin/swift-package
perl -pi -e 's%import Bionic%import Android%' swift-6.0.1-RELEASE-ubuntu22.04/usr/bin/swift-package
perl -pi -e 's%TSCBasic, would be%TSCBasic, would %' swift-6.0.1-RELEASE-ubuntu22.04/usr/bin/swift-package
```
This is fixed in the upcoming 6.0.2 patch release.

Now you're ready to cross-compile a Swift package and run its tests on Android.
I'll demonstrate with the swift-argument-parser package:
```
Expand All @@ -59,19 +50,19 @@ one depends on the example executables `color`, `generate-manual`, `math`,
point at test data in the repo: I've had success moving this data with the test
runner, after modifying the test source so it has the path to this test data in
the Android test environment. See the example of [swift-crypto on the
CI](https://github.com/finagolfin/swift-android-sdk/blob/6.0.1/.github/workflows/sdks.yml#L492).
CI](https://github.com/finagolfin/swift-android-sdk/blob/6.0.2/.github/workflows/sdks.yml#L505).

You can copy these executables and the Swift runtime libraries to [an emulator
or a USB debugging-enabled device with adb](https://github.com/swiftlang/swift/blob/release/6.0/docs/Android.md#3-deploying-the-build-products-to-the-device),
or put them on an Android device with [a terminal emulator app like Termux](https://termux.dev/en/).
I test aarch64 with Termux so I'll show how to run the test runner there, but
the process is similar with adb, [as can be seen on the CI](https://github.com/finagolfin/swift-android-sdk/blob/6.0.1/.github/workflows/sdks.yml#L440).
the process is similar with adb, [as can be seen on the CI](https://github.com/finagolfin/swift-android-sdk/blob/6.0.2/.github/workflows/sdks.yml#L453).

Copy the test executables to the same directory as the Swift 6 runtime libraries,
removing a few Android stub libraries that aren't needed:
```
cp .build/aarch64-unknown-linux-android24/debug/{swift-argument-parserPackageTests.xctest,color,generate-manual,math,repeat,roll} ..
cp ~/.swiftpm/swift-sdks/swift-6.0.1-RELEASE-android-24-0.1.artifactbundle/swift-6.0.1-release-android-24-sdk/android-27b-sysroot/usr/lib/aarch64-linux-android/24/lib*.so ..
cp ~/.swiftpm/swift-sdks/swift-6.0.2-RELEASE-android-24-0.1.artifactbundle/swift-6.0.2-release-android-24-sdk/android-27c-sysroot/usr/lib/aarch64-linux-android/24/lib*.so ..
rm ../lib{c,dl,log,m,z}.so
```
You can copy the test executables and Swift 6 runtime libraries to Termux using
Expand All @@ -81,7 +72,8 @@ uname -m # check if you're running on the right architecture, should say `aarch6
cd # move to the Termux app's home directory
pkg install openssh
scp yourname@192.168.1.1:{lib*.so,swift-argument-parserPackageTests.xctest,color,generate-manual,math,repeat,roll} .
scp yourname@192.168.1.1:"lib*.so" .
scp yourname@192.168.1.1:{swift-argument-parserPackageTests.xctest,color,generate-manual,math,repeat,roll} .
./swift-argument-parserPackageTests.xctest
```
Expand Down Expand Up @@ -131,9 +123,9 @@ packagingOptions {

## Building an Android SDK from source

Download the Swift 6.0.1 compiler as above and Android NDK 27b (only building
Download the Swift 6.0.2 compiler as above and Android NDK 27c (only building
the Android SDKs on linux works for now). Check out this repo and run
`SWIFT_TAG=swift-6.0.1-RELEASE ANDROID_ARCH=aarch64 swift get-packages-and-swift-source.swift`
`SWIFT_TAG=swift-6.0.2-RELEASE ANDROID_ARCH=aarch64 swift get-packages-and-swift-source.swift`
to get some prebuilt Android libraries and the Swift source to build an AArch64
SDK. If you pass in a different tag like `swift-DEVELOPMENT-SNAPSHOT-2024-10-08-a`
for the latest Swift trunk snapshot and pass in the path to the corresponding
Expand All @@ -154,9 +146,9 @@ are installed, run the following `build-script` command with your local paths
substituted instead:
```
./swift/utils/build-script -RA --skip-build-cmark --build-llvm=0 --android
--android-ndk /home/finagolfin/android-ndk-r27b/ --android-arch aarch64 --android-api-level 24
--build-swift-tools=0 --native-swift-tools-path=/home/finagolfin/swift-6.0.1-RELEASE-ubuntu22.04/usr/bin/
--native-clang-tools-path=/home/finagolfin/swift-6.0.1-RELEASE-ubuntu22.04/usr/bin/
--android-ndk /home/finagolfin/android-ndk-r27c/ --android-arch aarch64 --android-api-level 24
--build-swift-tools=0 --native-swift-tools-path=/home/finagolfin/swift-6.0.2-RELEASE-ubuntu22.04/usr/bin/
--native-clang-tools-path=/home/finagolfin/swift-6.0.2-RELEASE-ubuntu22.04/usr/bin/
--host-cc=/usr/bin/clang-13 --host-cxx=/usr/bin/clang++-13
--cross-compile-hosts=android-aarch64 --cross-compile-deps-path=/home/finagolfin/swift-release-android-aarch64-24-sdk
--skip-local-build --xctest --swift-install-components='clang-resource-dir-symlink;license;stdlib;sdk-overlay'
Expand All @@ -173,7 +165,7 @@ into these commands to build SDKs for those architectures instead.
Finally, copy `libc++_shared.so` from the NDK and modify the cross-compiled
Swift corelibs to include `$ORIGIN` and other relative directories in their rpaths:
```
cp /home/yourname/android-ndk-r27b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/libc++_shared.so swift-release-android-aarch64-24-sdk/usr/lib
cp /home/yourname/android-ndk-r27c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/libc++_shared.so swift-release-android-aarch64-24-sdk/usr/lib
patchelf --set-rpath \$ORIGIN/../..:\$ORIGIN swift-release-android-aarch64-24-sdk/usr/lib/swift/android/lib*.so
```

Expand Down Expand Up @@ -219,7 +211,7 @@ packages, by compiling against a more recent Android API that doesn't need the
`libandroid-spawn` backport, and by cross-compiling libcurl/libxml2 and their
dependencies yourself or not using FoundationNetworking and FoundationXML.

Finally, it gets [the 6.0.1 source](https://github.com/swiftlang/swift/releases/tag/swift-6.0.1-RELEASE)
Finally, it gets [the 6.0.2 source](https://github.com/swiftlang/swift/releases/tag/swift-6.0.2-RELEASE)
tarballs for ten Swift repos and renames them to `llvm-project/`, `swift/`,
`swift-syntax`, `swift-experimental-string-processing`, `swift-corelibs-libdispatch`,
`swift-corelibs-foundation`, `swift-collections`, `swift-foundation`,
Expand Down

0 comments on commit 1f03b5d

Please sign in to comment.