From b635325ecc0687b88a4aa482e0a929235b3369df Mon Sep 17 00:00:00 2001 From: Jake Petroules Date: Fri, 24 Oct 2025 10:36:27 -0700 Subject: [PATCH] Fix Android builds with NDK r28 and later The nullability annotations of many POSIX functions changed in r28. Also switch the CI to test with both the latest LTS version (r27d) and the latest stable version (r29). --- .github/workflows/pull_request.yml | 1 + Sources/Subprocess/Thread.swift | 15 +-------------- Sources/_SubprocessCShims/include/process_shims.h | 8 ++++++++ Sources/_SubprocessCShims/process_shims.c | 9 +++++++++ 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index de92c9e6..0b32032b 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -39,6 +39,7 @@ jobs: macos_build_command: 'xcrun swift-format lint -s -r --configuration ./.swift-format . && xcrun swift test && xcrun swift test -c release && xcrun swift test --disable-default-traits' enable_linux_static_sdk_build: true enable_android_sdk_build: true + android_ndk_version: '["r27d", "r29"]' linux_static_sdk_versions: '["6.1", "nightly-6.2"]' linux_static_sdk_build_command: | for triple in aarch64-swift-linux-musl x86_64-swift-linux-musl ; do diff --git a/Sources/Subprocess/Thread.swift b/Sources/Subprocess/Thread.swift index 534959ac..5e51b345 100644 --- a/Sources/Subprocess/Thread.swift +++ b/Sources/Subprocess/Thread.swift @@ -307,29 +307,16 @@ internal func pthread_create( self.body = body } } - #if canImport(Darwin) - func proc(_ context: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? { - (Unmanaged.fromOpaque(context).takeRetainedValue() as! Context).body() - return context - } - #elseif canImport(Glibc) || canImport(Musl) func proc(_ context: UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? { (Unmanaged.fromOpaque(context!).takeRetainedValue() as! Context).body() return context } - #elseif canImport(Bionic) - func proc(_ context: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer { - (Unmanaged.fromOpaque(context).takeRetainedValue() as! Context).body() - return context - } - #endif - #if canImport(Glibc) || canImport(Bionic) var thread = pthread_t() #else var thread: pthread_t? #endif - let rc = pthread_create( + let rc = _subprocess_pthread_create( &thread, nil, proc, diff --git a/Sources/_SubprocessCShims/include/process_shims.h b/Sources/_SubprocessCShims/include/process_shims.h index cd4b3423..7e392fcc 100644 --- a/Sources/_SubprocessCShims/include/process_shims.h +++ b/Sources/_SubprocessCShims/include/process_shims.h @@ -15,6 +15,7 @@ #include "target_conditionals.h" #if !TARGET_OS_WINDOWS +#include #include #if _POSIX_SPAWN @@ -39,6 +40,13 @@ extern "C" { #endif +int _subprocess_pthread_create( + pthread_t * _Nonnull ptr, + pthread_attr_t const * _Nullable attr, + void * _Nullable (* _Nonnull start)(void * _Nullable), + void * _Nullable context +); + #if __has_include() vm_size_t _subprocess_vm_size(void); #endif diff --git a/Sources/_SubprocessCShims/process_shims.c b/Sources/_SubprocessCShims/process_shims.c index 9fc00e53..9d4837de 100644 --- a/Sources/_SubprocessCShims/process_shims.c +++ b/Sources/_SubprocessCShims/process_shims.c @@ -79,6 +79,15 @@ int _was_process_suspended(int status) { return WIFSTOPPED(status); } +int _subprocess_pthread_create( + pthread_t * _Nonnull ptr, + pthread_attr_t const * _Nullable attr, + void * _Nullable (* _Nonnull start)(void * _Nullable), + void * _Nullable context +) { + return pthread_create(ptr, attr, start, context); +} + #endif #if __has_include()