Skip to content

Commit 01d2dd7

Browse files
committed
Update doc to 5.9
1 parent 7ed85fa commit 01d2dd7

File tree

1 file changed

+65
-60
lines changed

1 file changed

+65
-60
lines changed

README.md

Lines changed: 65 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
# Swift cross-compilation SDK for Android
22

3-
All patches used to build this SDK are open source and listed below. I
4-
maintain [a daily CI on github Actions](https://github.com/buttaface/swift-android-sdk/actions?query=event%3Aschedule)
5-
that [cross-compiles the SDK from the three main source branches of the Swift
6-
toolchain for AArch64, armv7, and x86_64, builds several Swift packages against
7-
those SDKs, and then runs their tests in the Android x86_64
8-
emulator](https://github.com/buttaface/swift-android-sdk/blob/main/.github/workflows/sdks.yml).
9-
10-
To build with the Swift 5.8 SDK, first download [the latest Android LTS NDK
11-
25c](https://developer.android.com/ndk/downloads) and [Swift 5.8.1
3+
The patch used to build this SDK is open source and listed below. I
4+
maintain [a daily CI on github Actions](https://github.com/finagolfin/swift-android-sdk/actions?query=event%3Aschedule)
5+
that [cross-compiles the SDK from the release and development source branches of
6+
the Swift toolchain for AArch64, armv7, and x86_64, builds several Swift
7+
packages against those SDKs, and then runs their tests in the Android x86_64
8+
emulator](https://github.com/finagolfin/swift-android-sdk/blob/main/.github/workflows/sdks.yml).
9+
10+
# Cross-compiling and testing Swift packages with the Android SDK
11+
12+
To build with the Swift 5.9 SDK, first download [the last Android LTS NDK
13+
25c](https://github.com/android/ndk/wiki/Unsupported-Downloads) and [Swift 5.9
1214
compiler](https://swift.org/download/#releases) (make sure to install the Swift
1315
compiler's dependencies listed there). Unpack these archives and the SDK.
1416

15-
I will write up a Swift script to do this SDK configuration one day, but you
16-
will need to do it manually for now (I'll show aarch64 below, the same will
17-
need to be done separately for the armv7 or x86_64 json files).
18-
19-
Change the symbolic link at `swift-5.8-android-24-sdk/usr/lib/swift/clang`
17+
Change the symbolic link at `swift-5.9-android-24-sdk/usr/lib/swift/clang`
2018
to point to the clang headers that come with your swift compiler, eg
2119

2220
```
23-
ln -sf /home/yourname/swift-5.8.1-RELEASE-ubuntu22.04/usr/lib/clang/13.0.0
24-
swift-5.8-android-24-sdk/usr/lib/swift/clang
21+
ln -sf /home/yourname/swift-5.9-RELEASE-ubuntu22.04/usr/lib/clang/13.0.0
22+
swift-5.9-android-24-sdk/usr/lib/swift/clang
2523
```
2624

2725
Next, modify the cross-compilation JSON file `android-aarch64.json` in this repo
@@ -30,11 +28,11 @@ similarly:
3028
1. All paths to the NDK should change from `/home/butta/android-ndk-r25c`
3129
to the path to your NDK, `/home/yourname/android-ndk-r25c`.
3230

33-
2. The path to the compiler should change from `/home/butta/swift-5.8.1-RELEASE-ubuntu22.04`
34-
to the path to your Swift compiler, `/home/yourname/swift-5.8.1-RELEASE-centos7`.
31+
2. The path to the compiler should change from `/home/butta/swift-5.9-RELEASE-ubuntu22.04`
32+
to the path to your Swift compiler, `/home/yourname/swift-5.9-RELEASE-centos7`.
3533

36-
3. The path to the Android SDK should change from `/home/butta/swift-5.8-android-24-sdk`
37-
to the path where you unpacked the Android SDK, `/home/yourname/swift-5.8-android-24-sdk`.
34+
3. The paths to the Android SDK should change from `/home/butta/swift-5.9-android-24-sdk`
35+
to the path where you unpacked the Android SDK, `/home/yourname/swift-5.9-android-24-sdk`.
3836

3937
Now you're ready to cross-compile a Swift package with the cross-compilation
4038
configuration JSON file, `android-aarch64.json`, and run its tests on Android.
@@ -44,9 +42,9 @@ git clone --depth 1 https://github.com/apple/swift-argument-parser.git
4442
4543
cd swift-argument-parser/
4644
47-
/home/yourname/swift-5.8.1-RELEASE-ubuntu22.04/usr/bin/swift build --build-tests
48-
--enable-test-discovery --destination ~/swift-android-sdk/android-aarch64.json
49-
-Xlinker -rpath -Xlinker \$ORIGIN/swift-5.8-android-24-sdk/usr/lib/aarch64-linux-android
45+
/home/yourname/swift-5.9-RELEASE-ubuntu22.04/usr/bin/swift build --build-tests
46+
--destination ~/swift-android-sdk/android-aarch64.json
47+
-Xlinker -rpath -Xlinker \$ORIGIN/swift-5.9-android-24-sdk/usr/lib/aarch64-linux-android
5048
```
5149
This will cross-compile the package for Android aarch64 and produce a test
5250
runner executable with the `.xctest` extension, in this case at
@@ -60,13 +58,13 @@ one depends on the example executables `generate-manual`, `math`, `repeat`, and
6058
data in the repo: I've had success moving this data with the test runner, after
6159
modifying the test source so it has the path to this test data in the Android
6260
test environment. See the example of [swift-crypto on the
63-
CI](https://github.com/buttaface/swift-android-sdk/blob/5.8/.github/workflows/sdks.yml#L292).
61+
CI](https://github.com/finagolfin/swift-android-sdk/blob/5.9/.github/workflows/sdks.yml#L293).
6462

6563
You can copy these executables and the SDK to [an emulator or a USB
66-
debugging-enabled device with adb](https://github.com/apple/swift/blob/release/5.8/docs/Android.md#3-deploying-the-build-products-to-the-device),
64+
debugging-enabled device with adb](https://github.com/apple/swift/blob/release/5.9/docs/Android.md#3-deploying-the-build-products-to-the-device),
6765
or put them on an Android device with [a terminal emulator app like Termux](https://termux.dev/en/).
6866
I test aarch64 with Termux so I'll show how to run the test runner there, but
69-
the process is similar with adb, [as can be seen on the CI](https://github.com/buttaface/swift-android-sdk/blob/5.8/.github/workflows/sdks.yml#L326).
67+
the process is similar with adb, [as can be seen on the CI](https://github.com/finagolfin/swift-android-sdk/blob/5.9/.github/workflows/sdks.yml#L328).
7068

7169
Copy the test executables to the same directory as the SDK:
7270
```
@@ -79,10 +77,10 @@ uname -m # check if you're running on the right architecture, should say `aarch6
7977
cd # move to the Termux app's home directory
8078
pkg install openssh
8179
82-
scp [email protected]:{swift-5.8-android-24-sdk.tar.xz,
80+
scp [email protected]:{swift-5.9-android-24-sdk.tar.xz,
8381
swift-argument-parserPackageTests.xctest,generate-manual,math,repeat,roll} .
8482
85-
tar xf swift-5.8-android-24-sdk.tar.xz
83+
tar xf swift-5.9-android-24-sdk.tar.xz
8684
8785
./swift-argument-parserPackageTests.xctest
8886
```
@@ -95,11 +93,15 @@ Revert that with `export LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermu
9593
when you're done running armv7 tests and want to go back to the normal aarch64
9694
mode.
9795

96+
Now that the LTS NDK 26 is out and Swift 5.9 supports [the new experimental SDK
97+
bundle format](https://github.com/apple/swift-evolution/blob/main/proposals/0387-cross-compilation-destinations.md),
98+
I plan to distribute an Android SDK bundle soon.
99+
98100
# Porting Swift packages to Android
99101

100102
The most commonly needed change is to simply import Glibc for Android too (while
101103
Bionic is the name of the C library on Android, currently Swift uses the name
102-
`Glibc` as a placeholder for all non-Darwin, non-Windows C libraries), so add
104+
`Glibc` as a placeholder for most non-Darwin, non-Windows C libraries), so add
103105
or change these two lines for Android:
104106
```
105107
#if canImport(Glibc)
@@ -115,10 +117,13 @@ refer to any FILE pointers like this](https://github.com/apple/swift-tools-suppo
115117
#if os(Android)
116118
typealias FILEPointer = OpaquePointer
117119
```
120+
121+
# Building an Android app with Swift
122+
118123
Some people have reported an issue with using the libraries from this SDK in
119124
their Android app, that the Android toolchain strips `libdispatch.so` and
120125
complains that it has an `empty/missing DT_HASH/DT_GNU_HASH`. You can [work
121-
around this issue by adding the following to your `build.gradle`](https://github.com/buttaface/swift-android-sdk/issues/67#issuecomment-1227460068):
126+
around this issue by adding the following to your `build.gradle`](https://github.com/finagolfin/swift-android-sdk/issues/67#issuecomment-1227460068):
122127
```
123128
packagingOptions {
124129
doNotStrip "*/arm64-v8a/libdispatch.so"
@@ -127,16 +132,22 @@ packagingOptions {
127132
}
128133
```
129134

130-
# Building the Android SDKs
135+
Note that the FoundationNetworking and FoundationXML libraries won't actually
136+
run on Android with this SDK, as their dependencies libcurl and libxml2 have other
137+
library dependencies that aren't included. If you want to use either of these
138+
separate Foundation libraries, you will have to track down those other libcurl/xml2
139+
dependencies and include them yourself.
131140

132-
Download the Swift 5.8.1 compiler and Android NDK 25c as above. Check out this
141+
# Building the Android SDKs from source
142+
143+
Download the Swift 5.9 compiler and Android NDK 25c as above. Check out this
133144
repo and run
134-
`SWIFT_TAG=swift-5.8.1-RELEASE ANDROID_ARCH=aarch64 swift get-packages-and-swift-source.swift`
145+
`SWIFT_TAG=swift-5.9-RELEASE ANDROID_ARCH=aarch64 swift get-packages-and-swift-source.swift`
135146
to get some prebuilt Android libraries and the Swift source to build the SDK. If
136-
you pass in a different tag like `swift-DEVELOPMENT-SNAPSHOT-2023-09-17-a`
147+
you pass in a different tag like `swift-DEVELOPMENT-SNAPSHOT-2023-09-22-a`
137148
for the latest Swift trunk snapshot and pass in the path to the corresponding
138-
official prebuilt Swift toolchain to `build-script` below, you can build a Swift
139-
trunk SDK too, as seen on the CI.
149+
prebuilt Swift toolchain to `build-script` below, you can build a Swift trunk
150+
SDK too, as seen on the CI.
140151

141152
Next, apply a patch to the Swift source, `swift-android.patch` from
142153
this repo, which adds a dependency for the Foundation core library in this
@@ -145,14 +156,14 @@ Android SDK:
145156
git apply swift-android.patch
146157
```
147158

148-
After making sure [needed build tools like python 3, CMake, and ninja](https://github.com/apple/swift/blob/release/5.8/docs/HowToGuides/GettingStarted.md#linux)
159+
After making sure [needed build tools like python 3, CMake, and ninja](https://github.com/apple/swift/blob/release/5.9/docs/HowToGuides/GettingStarted.md#linux)
149160
are installed, run the following `build-script` command with your local paths
150161
substituted instead:
151162
```
152163
./swift/utils/build-script -RA --skip-build-cmark --build-llvm=0 --android
153164
--android-ndk /home/butta/android-ndk-r25c/ --android-arch aarch64 --android-api-level 24
154-
--build-swift-tools=0 --native-swift-tools-path=/home/butta/swift-5.8.1-RELEASE-ubuntu22.04/usr/bin/
155-
--native-clang-tools-path=/home/butta/swift-5.8.1-RELEASE-ubuntu22.04/usr/bin/
165+
--build-swift-tools=0 --native-swift-tools-path=/home/butta/swift-5.9-RELEASE-ubuntu22.04/usr/bin/
166+
--native-clang-tools-path=/home/butta/swift-5.9-RELEASE-ubuntu22.04/usr/bin/
156167
--host-cc=/usr/bin/clang-13 --host-cxx=/usr/bin/clang++-13
157168
--cross-compile-hosts=android-aarch64 --cross-compile-deps-path=/home/butta/swift-release-android-aarch64-24-sdk
158169
--skip-local-build --xctest --swift-install-components='clang-resource-dir-symlink;license;stdlib;sdk-overlay'
@@ -178,12 +189,12 @@ Here is a description of what the above Swift script is doing:
178189

179190
This prebuilt SDK was compiled against Android API 24, because the Swift
180191
stdlib and corelibs require some libraries like libicu, that are pulled from the
181-
prebuilt library packages used by the Termux app which are built against Android
192+
prebuilt library packages used by the Termux app, which are built against Android
182193
API 24. Specifically, it downloads the libicu, libicu-static, libandroid-spawn,
183194
libcurl, and libxml2 packages from the [Termux package
184195
repository](https://packages.termux.dev/apt/termux-main/pool/main/).
185196

186-
Each one is unpacked with `ar x libicu_72.1-1_aarch64.deb; tar xf data.tar.xz` and
197+
Each one is unpacked with `ar x libicu_73.2_aarch64.deb; tar xf data.tar.xz` and
187198
the resulting files moved to a newly-created Swift release SDK directory:
188199
```
189200
mkdir swift-release-android-aarch64-24-sdk
@@ -198,35 +209,29 @@ rm swift-release-android-aarch64-24-sdk/usr/bin/*-config
198209
cd swift-release-android-aarch64-24-sdk/usr/lib
199210
200211
rm libicu{io,test,tu}*
201-
patchelf --set-rpath \$ORIGIN libandroid-spawn.so libcurl.so libicu*so.72.1 libxml2.so
212+
patchelf --set-rpath \$ORIGIN libandroid-spawn.so libcurl.so libicu*so.73.2 libxml2.so
202213
203214
# repeat the following for libicui18n.so and libicudata.so, as needed
204-
rm libicuuc.so libicuuc.so.72
205-
readelf -d libicuuc.so.72.1
206-
mv libicuuc.so.72.1 libicuuc.so
215+
rm libicuuc.so libicuuc.so.73
216+
readelf -d libicuuc.so.73.2
217+
mv libicuuc.so.73.2 libicuuc.so
207218
patchelf --set-soname libicuuc.so libicuuc.so
208-
patchelf --replace-needed libicudata.so.72 libicudata.so libicuuc.so
219+
patchelf --replace-needed libicudata.so.73 libicudata.so libicuuc.so
209220
```
210221
The libcurl and libxml2 packages are [only needed for the FoundationNetworking
211-
and FoundationXML libraries respectively](https://github.com/apple/swift-corelibs-foundation/blob/release/5.8/Docs/ReleaseNotes_Swift5.md),
222+
and FoundationXML libraries respectively](https://github.com/apple/swift-corelibs-foundation/blob/release/5.9/Docs/ReleaseNotes_Swift5.md),
212223
so you don't have to deploy them on the Android device if you don't use those
213-
extra Foundation libraries.
214-
215-
I simply include all four libraries since there's currently no way to disable
216-
building them in the CMake configuration, but they won't actually run on
217-
Android with this SDK, as libcurl and libxml2 have other library dependencies
218-
that aren't included. If you want to use either of these separate Foundation
219-
libraries, you will have to track down those other libcurl/xml2 dependencies and
220-
include them yourself.
224+
extra Foundation libraries. I simply include all four libraries since there's
225+
currently no way to disable building them in the CMake configuration.
221226

222227
The libicu dependency can be [cross-compiled for Android from scratch using
223228
these instructions](https://github.com/apple/swift/blob/release/5.5/docs/Android.md#1-downloading-or-building-the-swift-android-stdlib-dependencies)
224229
instead, so this Swift SDK for Android could be built without using
225230
any prebuilt Termux packages, if you're willing to put in the effort to
226231
cross-compile them yourself, for example, against a different Android API.
227232

228-
Finally, it gets [the 5.8.1 source](https://github.com/apple/swift/releases/tag/swift-5.8.1-RELEASE)
229-
tarballs for five Swift repos and renames them to `llvm-project/`, `swift/`,
230-
`swift-corelibs-libdispatch`, `swift-corelibs-foundation`, and
231-
`swift-corelibs-xctest`, as required by the Swift `build-script`, and creates
232-
an empty directory, `mkdir cmark`.
233+
Finally, it gets [the 5.9 source](https://github.com/apple/swift/releases/tag/swift-5.9-RELEASE)
234+
tarballs for seven Swift repos and renames them to `llvm-project/`, `swift/`,
235+
`swift-syntax`, `swift-experimental-string-processing`, `swift-corelibs-libdispatch`,
236+
`swift-corelibs-foundation`, and `swift-corelibs-xctest`, as required by the Swift
237+
`build-script`, and creates an empty directory, `mkdir cmark`.

0 commit comments

Comments
 (0)