mirror of
https://github.com/apple/swift.git
synced 2026-11-28 08:15:08 +01:00
The Swift runtime libraries on Android already link against `libc++_shared.so`, but CMake defaults to `c++_static`. So when build-script cross-compiles the corelibs, `lib_FoundationICU.so` links in its own copy of libc++ and libc++abi and exports about 1,600 of their symbols. This sets `CMAKE_ANDROID_STL_TYPE=c++_shared` for Android, next to the other Android CMake options, as discussed in swiftlang/swift-foundation-icu#103. Results with the Swift SDK for Android 6.4.1, built with `swift-docker`'s `build.sh` for aarch64 with only this change, compared to the released SDK: - `lib_FoundationICU.so`: `std::__ndk1` exports 1636 → 23 (ICU's own functions that take `std::string_view`), C++ ABI exports (`__cxa_*`, `__gxx_*`) 30 → 0, 734 KB smaller after strip, now needs `libc++_shared.so`. - `libdispatch.so`: now needs `libc++_shared.so` and takes `__emutls_get_address` from it instead of linking its own copy. - No other library changed. - Both changed libraries link with `-Wl,-z,defs`, so they aren't underlinked. As a control, the same ICU objects linked with `-nostdlib++ -Wl,-z,defs` fail with `undefined symbol: __cxa_pure_virtual`. - In swiftlang/swift-foundation-icu#103, finagolfin asked that this not break the static build. It doesn't: all 33 static libraries in `swift_static` are byte-for-byte the same as in a build without this change, and the Foundation test app linked with `--static-swift-stdlib` gives the same output as with the released SDK. - Apps already ship `libc++_shared.so`, because `libswiftCore.so` needs it. To check an SDK, in `swift-android/swift-resources/usr/lib/swift-aarch64/android`: ```sh llvm-nm -D --defined-only lib_FoundationICU.so | grep -cE ' (__cxa_|__gxx_)' # 30 before, 0 after llvm-readelf -d lib_FoundationICU.so | grep NEEDED # libc++_shared.so after ``` Tested on an API 36 arm64 emulator with an app that uses Foundation (dates, numbers, durations, relative dates, encodings, transforms, regex) and an app that runs C++ exceptions and Swift concurrency: same output as with the released SDK. The new check in `validation-test/BuildSystem/android_cross_compile.test` fails without this change. I ran the dry-run and matched the check lines by hand, but didn't run lit. Not tested: armv7 and x86_64, and building the toolchain itself for an Android host (LLVM also gets this option there). Together with #93374, which removes the extra copy of libc++abi from `libswiftCore.so` and `libswift_Concurrency.so`, no library in the SDK exports C++ ABI symbols anymore. Resolves the lib_FoundationICU.so part of https://github.com/swiftlang/swift/issues/93320.