Skip to content

Commit

Permalink
Use the new Android overlay in the tests and update some Bionic decla…
Browse files Browse the repository at this point in the history
…rations (#3009)

### Motivation:

Get this repo building again for Android with NDK 27

### Modifications:

- Update some networking declarations for newly added nullability
annotations
- Import the new Android overlay instead in some tests
- Add two force-unwraps on all platforms, that are needed for Android

### Result:

This repo and its tests build for Android again

I've been [using these patches on my Android
CI](https://github.com/finagolfin/swift-android-sdk/blob/main/swift-nio-ndk27.patch)
and natively on Android for a couple months now. I didn't bother keeping
this patch building for Android with Swift 5 anymore, as my Android CI
no longer tests Swift 5.

I built this pull and ran the tests on linux x86_64 to make sure there
was no regression.
  • Loading branch information
finagolfin authored Dec 1, 2024
1 parent 8a1523f commit 33b8a48
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 21 deletions.
11 changes: 1 addition & 10 deletions Sources/NIOFileSystem/Internal/System Calls/Syscalls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -452,21 +452,12 @@ internal func libc_confstr(
#endif

/// fts(3)
#if os(Android)
internal func libc_fts_open(
_ path: [UnsafeMutablePointer<CInterop.PlatformChar>],
_ options: CInt
) -> UnsafeMutablePointer<CInterop.FTS> {
fts_open(path, options, nil)!
}
#else
internal func libc_fts_open(
_ path: [UnsafeMutablePointer<CInterop.PlatformChar>?],
_ options: CInt
) -> UnsafeMutablePointer<CInterop.FTS> {
fts_open(path, options, nil)
fts_open(path, options, nil)!
}
#endif

/// fts(3)
internal func libc_fts_read(
Expand Down
18 changes: 14 additions & 4 deletions Sources/NIOPosix/System.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,20 @@ private let sysWritev = sysWritev_wrapper
#elseif !os(Windows)
private let sysWritev: @convention(c) (Int32, UnsafePointer<iovec>?, CInt) -> CLong = writev
#endif
#if !os(Windows)
#if canImport(Android)
private let sysRecvMsg: @convention(c) (CInt, UnsafeMutablePointer<msghdr>, CInt) -> ssize_t = recvmsg
private let sysSendMsg: @convention(c) (CInt, UnsafePointer<msghdr>, CInt) -> ssize_t = sendmsg
#elseif !os(Windows)
private let sysRecvMsg: @convention(c) (CInt, UnsafeMutablePointer<msghdr>?, CInt) -> ssize_t = recvmsg
private let sysSendMsg: @convention(c) (CInt, UnsafePointer<msghdr>?, CInt) -> ssize_t = sendmsg
#endif
private let sysDup: @convention(c) (CInt) -> CInt = dup
#if !os(Windows)
#if canImport(Android)
private let sysGetpeername:
@convention(c) (CInt, UnsafeMutablePointer<sockaddr>, UnsafeMutablePointer<socklen_t>) -> CInt = getpeername
private let sysGetsockname:
@convention(c) (CInt, UnsafeMutablePointer<sockaddr>, UnsafeMutablePointer<socklen_t>) -> CInt = getsockname
#elseif !os(Windows)
private let sysGetpeername:
@convention(c) (CInt, UnsafeMutablePointer<sockaddr>?, UnsafeMutablePointer<socklen_t>?) -> CInt = getpeername
private let sysGetsockname:
Expand All @@ -141,7 +149,9 @@ private let sysIfNameToIndex: @convention(c) (UnsafePointer<CChar>) -> CUnsigned
#else
private let sysIfNameToIndex: @convention(c) (UnsafePointer<CChar>?) -> CUnsignedInt = if_nametoindex
#endif
#if !os(Windows)
#if canImport(Android)
private let sysSocketpair: @convention(c) (CInt, CInt, CInt, UnsafeMutablePointer<CInt>) -> CInt = socketpair
#elseif !os(Windows)
private let sysSocketpair: @convention(c) (CInt, CInt, CInt, UnsafeMutablePointer<CInt>?) -> CInt = socketpair
#endif

Expand Down Expand Up @@ -1000,7 +1010,7 @@ internal enum Posix {
socketVector: UnsafeMutablePointer<CInt>?
) throws {
_ = try syscall(blocking: false) {
sysSocketpair(domain.rawValue, type.rawValue, protocolSubtype.rawValue, socketVector)
sysSocketpair(domain.rawValue, type.rawValue, protocolSubtype.rawValue, socketVector!)
}
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import XCTest
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Bionic)
import Bionic
#elseif canImport(Android)
import Android
#else
#error("The Concurrency helpers test module was unable to identify your C library.")
#endif
Expand Down
4 changes: 4 additions & 0 deletions Tests/NIOCoreTests/XCTest+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
import NIOCore
import XCTest

#if canImport(Android)
import Android
#endif

func assert(
_ condition: @autoclosure () -> Bool,
within time: TimeAmount,
Expand Down
4 changes: 4 additions & 0 deletions Tests/NIOEmbeddedTests/TestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import NIOConcurrencyHelpers
import NIOCore
import XCTest

#if canImport(Android)
import Android
#endif

// FIXME: Duplicated with NIO
func assert(
_ condition: @autoclosure () -> Bool,
Expand Down
10 changes: 5 additions & 5 deletions Tests/NIOFileSystemTests/FileInfoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import _NIOFileSystem
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Bionic)
import Bionic
#elseif canImport(Android)
import Android
#endif

final class FileInfoTests: XCTestCase {
Expand All @@ -44,7 +44,7 @@ final class FileInfoTests: XCTestCase {
status.st_birthtimespec = timespec(tv_sec: 3, tv_nsec: 0)
status.st_flags = 11
status.st_gen = 12
#elseif canImport(Glibc) || canImport(Bionic)
#elseif canImport(Glibc) || canImport(Android)
status.st_atim = timespec(tv_sec: 0, tv_nsec: 0)
status.st_mtim = timespec(tv_sec: 1, tv_nsec: 0)
status.st_ctim = timespec(tv_sec: 2, tv_nsec: 0)
Expand Down Expand Up @@ -98,7 +98,7 @@ final class FileInfoTests: XCTestCase {
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_birthtimespec.tv_sec += 1 }
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_flags += 1 }
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_gen += 1 }
#elseif canImport(Glibc) || canImport(Bionic)
#elseif canImport(Glibc) || canImport(Android)
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_atim.tv_sec += 1 }
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_mtim.tv_sec += 1 }
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_ctim.tv_sec += 1 }
Expand Down Expand Up @@ -151,7 +151,7 @@ final class FileInfoTests: XCTestCase {
}
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_flags += 1 }
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_gen += 1 }
#elseif canImport(Glibc) || canImport(Bionic)
#elseif canImport(Glibc) || canImport(Android)
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_atim.tv_sec += 1 }
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_mtim.tv_sec += 1 }
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_ctim.tv_sec += 1 }
Expand Down
2 changes: 2 additions & 0 deletions Tests/NIOFileSystemTests/FileTypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import XCTest
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Android)
import Android
#endif

final class FileTypeTests: XCTestCase {
Expand Down

0 comments on commit 33b8a48

Please sign in to comment.