Skip to content

Commit 33b8a48

Browse files
authored
Use the new Android overlay in the tests and update some Bionic declarations (#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.
1 parent 8a1523f commit 33b8a48

File tree

7 files changed

+32
-21
lines changed

7 files changed

+32
-21
lines changed

Sources/NIOFileSystem/Internal/System Calls/Syscalls.swift

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -452,21 +452,12 @@ internal func libc_confstr(
452452
#endif
453453

454454
/// fts(3)
455-
#if os(Android)
456-
internal func libc_fts_open(
457-
_ path: [UnsafeMutablePointer<CInterop.PlatformChar>],
458-
_ options: CInt
459-
) -> UnsafeMutablePointer<CInterop.FTS> {
460-
fts_open(path, options, nil)!
461-
}
462-
#else
463455
internal func libc_fts_open(
464456
_ path: [UnsafeMutablePointer<CInterop.PlatformChar>?],
465457
_ options: CInt
466458
) -> UnsafeMutablePointer<CInterop.FTS> {
467-
fts_open(path, options, nil)
459+
fts_open(path, options, nil)!
468460
}
469-
#endif
470461

471462
/// fts(3)
472463
internal func libc_fts_read(

Sources/NIOPosix/System.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,20 @@ private let sysWritev = sysWritev_wrapper
124124
#elseif !os(Windows)
125125
private let sysWritev: @convention(c) (Int32, UnsafePointer<iovec>?, CInt) -> CLong = writev
126126
#endif
127-
#if !os(Windows)
127+
#if canImport(Android)
128+
private let sysRecvMsg: @convention(c) (CInt, UnsafeMutablePointer<msghdr>, CInt) -> ssize_t = recvmsg
129+
private let sysSendMsg: @convention(c) (CInt, UnsafePointer<msghdr>, CInt) -> ssize_t = sendmsg
130+
#elseif !os(Windows)
128131
private let sysRecvMsg: @convention(c) (CInt, UnsafeMutablePointer<msghdr>?, CInt) -> ssize_t = recvmsg
129132
private let sysSendMsg: @convention(c) (CInt, UnsafePointer<msghdr>?, CInt) -> ssize_t = sendmsg
130133
#endif
131134
private let sysDup: @convention(c) (CInt) -> CInt = dup
132-
#if !os(Windows)
135+
#if canImport(Android)
136+
private let sysGetpeername:
137+
@convention(c) (CInt, UnsafeMutablePointer<sockaddr>, UnsafeMutablePointer<socklen_t>) -> CInt = getpeername
138+
private let sysGetsockname:
139+
@convention(c) (CInt, UnsafeMutablePointer<sockaddr>, UnsafeMutablePointer<socklen_t>) -> CInt = getsockname
140+
#elseif !os(Windows)
133141
private let sysGetpeername:
134142
@convention(c) (CInt, UnsafeMutablePointer<sockaddr>?, UnsafeMutablePointer<socklen_t>?) -> CInt = getpeername
135143
private let sysGetsockname:
@@ -141,7 +149,9 @@ private let sysIfNameToIndex: @convention(c) (UnsafePointer<CChar>) -> CUnsigned
141149
#else
142150
private let sysIfNameToIndex: @convention(c) (UnsafePointer<CChar>?) -> CUnsignedInt = if_nametoindex
143151
#endif
144-
#if !os(Windows)
152+
#if canImport(Android)
153+
private let sysSocketpair: @convention(c) (CInt, CInt, CInt, UnsafeMutablePointer<CInt>) -> CInt = socketpair
154+
#elseif !os(Windows)
145155
private let sysSocketpair: @convention(c) (CInt, CInt, CInt, UnsafeMutablePointer<CInt>?) -> CInt = socketpair
146156
#endif
147157

@@ -1000,7 +1010,7 @@ internal enum Posix {
10001010
socketVector: UnsafeMutablePointer<CInt>?
10011011
) throws {
10021012
_ = try syscall(blocking: false) {
1003-
sysSocketpair(domain.rawValue, type.rawValue, protocolSubtype.rawValue, socketVector)
1013+
sysSocketpair(domain.rawValue, type.rawValue, protocolSubtype.rawValue, socketVector!)
10041014
}
10051015
}
10061016
#endif

Tests/NIOConcurrencyHelpersTests/NIOConcurrencyHelpersTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import XCTest
2222
import Darwin
2323
#elseif canImport(Glibc)
2424
import Glibc
25-
#elseif canImport(Bionic)
26-
import Bionic
25+
#elseif canImport(Android)
26+
import Android
2727
#else
2828
#error("The Concurrency helpers test module was unable to identify your C library.")
2929
#endif

Tests/NIOCoreTests/XCTest+Extensions.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
import NIOCore
1616
import XCTest
1717

18+
#if canImport(Android)
19+
import Android
20+
#endif
21+
1822
func assert(
1923
_ condition: @autoclosure () -> Bool,
2024
within time: TimeAmount,

Tests/NIOEmbeddedTests/TestUtils.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import NIOConcurrencyHelpers
1717
import NIOCore
1818
import XCTest
1919

20+
#if canImport(Android)
21+
import Android
22+
#endif
23+
2024
// FIXME: Duplicated with NIO
2125
func assert(
2226
_ condition: @autoclosure () -> Bool,

Tests/NIOFileSystemTests/FileInfoTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import _NIOFileSystem
1919
import Darwin
2020
#elseif canImport(Glibc)
2121
import Glibc
22-
#elseif canImport(Bionic)
23-
import Bionic
22+
#elseif canImport(Android)
23+
import Android
2424
#endif
2525

2626
final class FileInfoTests: XCTestCase {
@@ -44,7 +44,7 @@ final class FileInfoTests: XCTestCase {
4444
status.st_birthtimespec = timespec(tv_sec: 3, tv_nsec: 0)
4545
status.st_flags = 11
4646
status.st_gen = 12
47-
#elseif canImport(Glibc) || canImport(Bionic)
47+
#elseif canImport(Glibc) || canImport(Android)
4848
status.st_atim = timespec(tv_sec: 0, tv_nsec: 0)
4949
status.st_mtim = timespec(tv_sec: 1, tv_nsec: 0)
5050
status.st_ctim = timespec(tv_sec: 2, tv_nsec: 0)
@@ -98,7 +98,7 @@ final class FileInfoTests: XCTestCase {
9898
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_birthtimespec.tv_sec += 1 }
9999
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_flags += 1 }
100100
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_gen += 1 }
101-
#elseif canImport(Glibc) || canImport(Bionic)
101+
#elseif canImport(Glibc) || canImport(Android)
102102
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_atim.tv_sec += 1 }
103103
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_mtim.tv_sec += 1 }
104104
assertNotEqualAfterMutation { $0.platformSpecificStatus!.st_ctim.tv_sec += 1 }
@@ -151,7 +151,7 @@ final class FileInfoTests: XCTestCase {
151151
}
152152
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_flags += 1 }
153153
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_gen += 1 }
154-
#elseif canImport(Glibc) || canImport(Bionic)
154+
#elseif canImport(Glibc) || canImport(Android)
155155
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_atim.tv_sec += 1 }
156156
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_mtim.tv_sec += 1 }
157157
assertDifferentHashValueAfterMutation { $0.platformSpecificStatus!.st_ctim.tv_sec += 1 }

Tests/NIOFileSystemTests/FileTypeTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import XCTest
1919
import Darwin
2020
#elseif canImport(Glibc)
2121
import Glibc
22+
#elseif canImport(Android)
23+
import Android
2224
#endif
2325

2426
final class FileTypeTests: XCTestCase {

0 commit comments

Comments
 (0)