Description
@compnerd and some Windows devs have been trying out Android cross-compilation from Windows, and in response to some issues they've been seeing, I suggested symlinking the Swift files into the C sysroot in the Android NDK. I then tried it myself on linux, using my Android SDK for Swift for convenience, and found that it works with the legacy C++ Driver, but not this one:
> wget https://github.com/finagolfin/swift-android-sdk/releases/download/5.9.2/swift-5.9.2-android-24-sdk.tar.xz
> tar xf swift-5.9.2-android-24-sdk.tar.xz
> ln -s ~/swift-5.9.2-android-24-sdk/usr/lib/swift android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/swift
> ./swift-5.9.2-RELEASE-ubi9/usr/bin/swiftc -tools-directory ~/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/bin/ -target aarch64-unknown-linux-android24 -sdk android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot swift/test/Interpreter/hello_toplevel.swift -v -L swift-5.9.2-android-24-sdk/usr/lib/aarch64-linux-android/ -disallow-use-new-driver
<unknown>:0: warning: legacy driver is now deprecated; consider avoiding specifying '-disallow-use-new-driver'
Swift version 5.9.2 (swift-5.9.2-RELEASE)
Target: aarch64-unknown-linux-android24
/home/fina/swift-5.9.2-RELEASE-ubi9/usr/bin/swift-frontend -frontend -c -primary-file swift/test/Interpreter/hello_toplevel.swift -target aarch64-unknown-linux-android24 -Xllvm -aarch64-use-tbi -disable-objc-interop -sdk android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot -color-diagnostics -tools-directory /home/fina/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/bin/ -plugin-path /home/fina/swift-5.9.2-RELEASE-ubi9/usr/lib/swift/host/plugins -plugin-path /home/fina/swift-5.9.2-RELEASE-ubi9/usr/local/lib/swift/host/plugins -module-name hello_toplevel -o /tmp/hello_toplevel-b8d1e8.o
/home/fina/swift-5.9.2-RELEASE-ubi9/usr/bin/swift-autolink-extract /tmp/hello_toplevel-b8d1e8.o -o /tmp/hello_toplevel-9c4d55.autolink
/home/fina/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/bin/clang -fuse-ld=lld -Xlinker -z -Xlinker nostart-stop-gc -B /home/fina/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/bin/ -pie android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/swift/android/aarch64/swiftrt.o /tmp/hello_toplevel-b8d1e8.o --sysroot android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot @/tmp/hello_toplevel-9c4d55.autolink -L android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/swift/android -lswiftCore --target=aarch64-unknown-linux-android24 -v -L swift-5.9.2-android-24-sdk/usr/lib/aarch64-linux-android/ -o hello_toplevel
Android (11349228, +pgo, +bolt, +lto, -mlgo, based on r487747e) clang version 17.0.2 (https://android.googlesource.com/toolchain/llvm-project d9f89f4d16663d5012e5c09495f3b30ece3d2362)
Target: aarch64-unknown-linux-android24
Thread model: posix
InstalledDir: /home/fina/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/bin
"/home/fina/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/bin/ld.lld" --sysroot=android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot -pie -EL --fix-cortex-a53-843419 -z now -z relro -z max-page-size=4096 --hash-style=gnu --eh-frame-hdr -m aarch64linux -dynamic-linker /system/bin/linker64 -o hello_toplevel android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/24/crtbegin_dynamic.o -Landroid-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/swift/android -Lswift-5.9.2-android-24-sdk/usr/lib/aarch64-linux-android/ -L/home/fina/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/lib/clang/17/lib/linux/aarch64 -Landroid-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/24 -Landroid-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android -Landroid-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib -z nostart-stop-gc android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/swift/android/aarch64/swiftrt.o /tmp/hello_toplevel-b8d1e8.o -lswiftSwiftOnoneSupport -lswiftCore -lswift_Concurrency -lswift_StringProcessing -lswift_RegexParser -lswiftCore /home/fina/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/lib/clang/17/lib/linux/libclang_rt.builtins-aarch64-android.a -l:libunwind.a -ldl -lc /home/fina/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/lib/clang/17/lib/linux/libclang_rt.builtins-aarch64-android.a -l:libunwind.a -ldl android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/24/crtend_android.o
> file hello_toplevel
hello_toplevel: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, not stripped
> ./swift-5.9.2-RELEASE-ubi9/usr/bin/swiftc -tools-directory ~/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/bin/ -target aarch64-unknown-linux-android24 -sdk android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot swift/test/Interpreter/hello_toplevel.swift -v -L swift-5.9.2-android-24-sdk/usr/lib/aarch64-linux-android/
warning: Could not read SDKSettings.json for SDK at: /home/fina/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot
Swift version 5.9.2 (swift-5.9.2-RELEASE)
Target: aarch64-unknown-linux-android24
/home/fina/swift-5.9.2-RELEASE-ubi9/usr/bin/swift-frontend -frontend -c -primary-file swift/test/Interpreter/hello_toplevel.swift -target aarch64-unknown-linux-android24 -Xllvm -aarch64-use-tbi -disable-objc-interop -sdk /home/fina/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot -color-diagnostics -new-driver-path /home/fina/swift-5.9.2-RELEASE-ubi9/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /home/fina/swift-5.9.2-RELEASE-ubi9/usr/lib/swift -module-name hello_toplevel -plugin-path /home/fina/swift-5.9.2-RELEASE-ubi9/usr/lib/swift/host/plugins -plugin-path /home/fina/swift-5.9.2-RELEASE-ubi9/usr/local/lib/swift/host/plugins -o /tmp/TemporaryDirectory.A4x0Xq/hello_toplevel-1.o
/home/fina/swift-5.9.2-RELEASE-ubi9/usr/bin/swift-autolink-extract /tmp/TemporaryDirectory.A4x0Xq/hello_toplevel-1.o -o /tmp/TemporaryDirectory.A4x0Xq/hello_toplevel-2.autolink
/home/fina/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/bin/clang -fuse-ld=lld -Xlinker -z -Xlinker nostart-stop-gc -B /home/fina/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/bin -pie /home/fina/swift-5.9.2-RELEASE-ubi9/usr/lib/swift/android/aarch64/swiftrt.o /tmp/TemporaryDirectory.A4x0Xq/hello_toplevel-1.o @/tmp/TemporaryDirectory.A4x0Xq/hello_toplevel-2.autolink --sysroot /home/fina/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot -L /home/fina/swift-5.9.2-RELEASE-ubi9/usr/lib/swift/android -lswiftCore --target=aarch64-unknown-linux-android24 -v -L swift-5.9.2-android-24-sdk/usr/lib/aarch64-linux-android -o hello_toplevel
error: link command failed with exit code 1 (use -v to see invocation)
Android (11349228, +pgo, +bolt, +lto, -mlgo, based on r487747e) clang version 17.0.2 (https://android.googlesource.com/toolchain/llvm-project d9f89f4d16663d5012e5c09495f3b30ece3d2362)
Target: aarch64-unknown-linux-android24
Thread model: posix
InstalledDir: /home/fina/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/bin
clang-17: error: no such file or directory: '/home/fina/swift-5.9.2-RELEASE-ubi9/usr/lib/swift/android/aarch64/swiftrt.o'
error: fatalError
Note how the old C++ driver correctly passes -Landroid-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/swift/android
to the linker, while this swift-driver incorrectly passes a non-existent -L /home/fina/swift-5.9.2-RELEASE-ubi9/usr/lib/swift/android
to the linker and strangely also -resource-dir /home/fina/swift-5.9.2-RELEASE-ubi9/usr/lib/swift
to the frontend.
At the very least, we should resolve this discrepancy between the two drivers, which was probably not noticed because nobody is distributing full Unix cross-compilation SDKs, ie a single platform SDK containing both a C/C++ sysroot and Swift libraries, not bundled with the toolchain.