mirror of
https://github.com/apple/swift-nio
synced 2026-11-28 05:14:23 +00:00
This change enables swift-nio to be compiled for Android against both NDK 27 and NDK 28 ### Motivation: Android's NDK had been adopting [nullability annotations](https://android.googlesource.com/platform/developers/docs/+/refs/heads/master/api-guidelines/ndk.md#nullability) in its C headers, and they have been changing them from release to release. The churn has mostly died down since the release of the NDK 27 (LTS), but one new change in NDK 28 affects the destructor argument for `pthread_create`, making the existing `ThreadDestructor` typealias invalid and leading to the following compile error when the [Android SDK bundle](https://github.com/skiptools/swift-android-toolchain/releases) is installed and linked to NDK 28: ``` $ swiftly run swift build --swift-sdk aarch64-unknown-linux-android28 +6.2-snapshot-2025-05-15-a Building for debugging... /opt/src/github/swift-everywhere/swift-nio/Sources/NIOPosix/ThreadPosix.swift:82:9: error: cannot convert value of type '@convention(c) (UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer?' to expected argument type '@convention(c) (UnsafeMutableRawPointer) -> UnsafeMutableRawPointer' 80 | &handleLinux, 81 | nil, 82 | destructor, | `- error: cannot convert value of type '@convention(c) (UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer?' to expected argument type '@convention(c) (UnsafeMutableRawPointer) -> UnsafeMutableRawPointer' 83 | args 84 | ) [4/21] Compiling NIOPosix ThreadPosix.swift ``` This change adapts the parameter we pass to that function to accommodate both NDK 27 and NDK 28. ### Modifications: There's currently no way to determine which version of the NDK is in use at compile time (see https://github.com/swiftlang/swift/issues/81402), so this accommodation is implementing by having two functions named `coerceThreadDestructor` that return values that are suitable either NDK 27 or NDK 28 (using `unsafeBitCast`, but the layout of C functions doesn't know anything about nullability, so this is safe), and then let the type inference for `pthread_create` handle the rest. ### Result: swift-nio can be compiled against both NDK 27 and NDK 28 without errors.