Skip to content

Commit 4feff3c

Browse files
authored
Use the new Android overlay and Bionic module from Swift 6 (#2784)
Motivation: Get this repo building again for Android with the new overlay Modifications: - Import the new module or overlay wherever `Glibc` is used - Keep this repo building with Swift 5 by duplicating some declarations Result: All the same tests keep passing on my Android CI, finagolfin/swift-android-sdk#158
1 parent 2841257 commit 4feff3c

33 files changed

+164
-43
lines changed

Sources/NIOConcurrencyHelpers/NIOLock.swift

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import WinSDK
2121
import Glibc
2222
#elseif canImport(Musl)
2323
import Musl
24+
#elseif canImport(Bionic)
25+
import Bionic
2426
#else
2527
#error("The concurrency NIOLock module was unable to identify your C library.")
2628
#endif

Sources/NIOConcurrencyHelpers/atomics.swift

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ private func sys_sched_yield() {
3030
import Glibc
3131
#elseif canImport(Musl)
3232
import Musl
33+
#elseif canImport(Bionic)
34+
import Bionic
3335
#else
3436
#error("The concurrency atomics module was unable to identify your C library.")
3537
#endif

Sources/NIOConcurrencyHelpers/lock.swift

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import WinSDK
2121
import Glibc
2222
#elseif canImport(Musl)
2323
import Musl
24+
#elseif canImport(Bionic)
25+
import Bionic
2426
#else
2527
#error("The concurrency lock module was unable to identify your C library.")
2628
#endif

Sources/NIOCore/BSDSocketAPI.swift

+8
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ internal typealias socklen_t = ucrt.size_t
6565
import Glibc
6666
#elseif canImport(Musl)
6767
import Musl
68+
#elseif canImport(Android)
69+
import Android
6870
#endif
6971
import CNIOLinux
7072

@@ -90,9 +92,15 @@ private let sysInet_pton: @convention(c) (CInt, UnsafePointer<CChar>?, UnsafeMut
9092
#endif
9193

9294
#if os(Android)
95+
#if compiler(>=6.0)
96+
let IFF_BROADCAST: CUnsignedInt = numericCast(Android.IFF_BROADCAST.rawValue)
97+
let IFF_POINTOPOINT: CUnsignedInt = numericCast(Android.IFF_POINTOPOINT.rawValue)
98+
let IFF_MULTICAST: CUnsignedInt = numericCast(Android.IFF_MULTICAST.rawValue)
99+
#else
93100
let IFF_BROADCAST: CUnsignedInt = numericCast(SwiftGlibc.IFF_BROADCAST.rawValue)
94101
let IFF_POINTOPOINT: CUnsignedInt = numericCast(SwiftGlibc.IFF_POINTOPOINT.rawValue)
95102
let IFF_MULTICAST: CUnsignedInt = numericCast(SwiftGlibc.IFF_MULTICAST.rawValue)
103+
#endif
96104
#if arch(arm)
97105
let SO_RCVTIMEO = SO_RCVTIMEO_OLD
98106
let SO_TIMESTAMP = SO_TIMESTAMP_OLD

Sources/NIOCore/ByteBuffer-core.swift

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import Darwin
2020
import Glibc
2121
#elseif canImport(Musl)
2222
import Musl
23+
#elseif canImport(Bionic)
24+
import Bionic
2325
#else
2426
#error("The Byte Buffer module was unable to identify your C library.")
2527
#endif

Sources/NIOCore/FileHandle.swift

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import Darwin
1919
import Glibc
2020
#elseif canImport(Musl)
2121
import Musl
22+
#elseif canImport(Android)
23+
import Android
2224
#else
2325
#error("The File Handle module was unable to identify your C library.")
2426
#endif

Sources/NIOCore/FileRegion.swift

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import Darwin
1919
import Glibc
2020
#elseif canImport(Musl)
2121
import Musl
22+
#elseif canImport(Bionic)
23+
import Bionic
2224
#else
2325
#error("The File Region module was unable to identify your C library.")
2426
#endif

Sources/NIOCore/GlobalSingletons.swift

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import WinSDK
2323
import Glibc
2424
#elseif canImport(Musl)
2525
import Musl
26+
#elseif canImport(Bionic)
27+
import Bionic
2628
#else
2729
#error("Unsupported C library")
2830
#endif

Sources/NIOCore/IO.swift

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ internal func MAKELANGID(_ p: WORD, _ s: WORD) -> DWORD {
3232
import Glibc
3333
#elseif canImport(Musl)
3434
import Musl
35+
#elseif canImport(Bionic)
36+
import Bionic
3537
#elseif canImport(Darwin)
3638
import Darwin
3739
#else

Sources/NIOCore/Interfaces.swift

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import Glibc
1717
#elseif canImport(Musl)
1818
import Musl
19+
#elseif canImport(Bionic)
20+
import Bionic
1921
#endif
2022
import CNIOLinux
2123
#elseif canImport(Darwin)

Sources/NIOCore/SocketAddresses.swift

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ import Darwin
4848
import Glibc
4949
#elseif canImport(Musl)
5050
import Musl
51+
#elseif canImport(Android)
52+
import Android
5153
#endif
5254
import CNIOLinux
5355
#else

Sources/NIOCore/SocketOptionProvider.swift

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import Darwin
1818
import Glibc
1919
#elseif canImport(Musl)
2020
import Musl
21+
#elseif canImport(Bionic)
22+
import Bionic
2123
#endif
2224
import CNIOLinux
2325
#elseif os(Windows)

Sources/NIOCore/SystemCallHelpers.swift

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import Glibc
2727
import Musl
2828
#elseif os(Windows)
2929
import CNIOWindows
30+
#elseif canImport(Android)
31+
import Android
3032
#else
3133
#error("The system call helpers module was unable to identify your C library.")
3234
#endif

Sources/NIOCore/Utilities.swift

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import CNIOLinux
1717
import Glibc
1818
#elseif canImport(Musl)
1919
import Musl
20+
#elseif canImport(Android)
21+
import Android
2022
#endif
2123
#elseif os(Windows)
2224
import let WinSDK.RelationProcessorCore

Sources/NIOFileSystem/FileInfo.swift

+7-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import CNIOLinux
2323
#elseif canImport(Musl)
2424
import Musl
2525
import CNIOLinux
26+
#elseif canImport(Android)
27+
import Android
28+
import CNIOLinux
2629
#endif
2730

2831
/// Information about a file system object.
@@ -85,7 +88,7 @@ public struct FileInfo: Hashable, Sendable {
8588
self.lastAccessTime = Timespec(platformSpecificStatus.st_atimespec)
8689
self.lastDataModificationTime = Timespec(platformSpecificStatus.st_mtimespec)
8790
self.lastStatusChangeTime = Timespec(platformSpecificStatus.st_ctimespec)
88-
#elseif canImport(Glibc) || canImport(Musl)
91+
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
8992
self.lastAccessTime = Timespec(platformSpecificStatus.st_atim)
9093
self.lastDataModificationTime = Timespec(platformSpecificStatus.st_mtim)
9194
self.lastStatusChangeTime = Timespec(platformSpecificStatus.st_ctim)
@@ -150,7 +153,7 @@ extension FileInfo {
150153
#if canImport(Darwin)
151154
private static let utimeOmit = Int(UTIME_OMIT)
152155
private static let utimeNow = Int(UTIME_NOW)
153-
#elseif canImport(Glibc) || canImport(Musl)
156+
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
154157
private static let utimeOmit = Int(CNIOLinux_UTIME_OMIT)
155158
private static let utimeNow = Int(CNIOLinux_UTIME_NOW)
156159
#endif
@@ -218,7 +221,7 @@ private struct Stat: Hashable {
218221
hasher.combine(FileInfo.Timespec(stat.st_birthtimespec))
219222
hasher.combine(stat.st_flags)
220223
hasher.combine(stat.st_gen)
221-
#elseif canImport(Glibc) || canImport(Musl)
224+
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
222225
hasher.combine(FileInfo.Timespec(stat.st_atim))
223226
hasher.combine(FileInfo.Timespec(stat.st_mtim))
224227
hasher.combine(FileInfo.Timespec(stat.st_ctim))
@@ -259,7 +262,7 @@ private struct Stat: Hashable {
259262
== FileInfo.Timespec(rStat.st_birthtimespec)
260263
isEqual = isEqual && lStat.st_flags == rStat.st_flags
261264
isEqual = isEqual && lStat.st_gen == rStat.st_gen
262-
#elseif canImport(Glibc) || canImport(Musl)
265+
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
263266
isEqual = isEqual && FileInfo.Timespec(lStat.st_atim) == FileInfo.Timespec(rStat.st_atim)
264267
isEqual = isEqual && FileInfo.Timespec(lStat.st_mtim) == FileInfo.Timespec(rStat.st_mtim)
265268
isEqual = isEqual && FileInfo.Timespec(lStat.st_ctim) == FileInfo.Timespec(rStat.st_ctim)

Sources/NIOFileSystem/FileSystem.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import Darwin
2525
import Glibc
2626
#elseif canImport(Musl)
2727
import Musl
28+
#elseif canImport(Bionic)
29+
import Bionic
2830
#endif
2931

3032
/// A file system which interacts with the local system. The file system uses a thread pool to
@@ -1086,7 +1088,7 @@ extension FileSystem {
10861088
location: .here()
10871089
)
10881090
}
1089-
#elseif canImport(Glibc) || canImport(Musl)
1091+
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
10901092
var offset = 0
10911093

10921094
while offset < sourceInfo.size {

Sources/NIOFileSystem/FileType.swift

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import Darwin
2121
import Glibc
2222
#elseif canImport(Musl)
2323
import Musl
24+
#elseif canImport(Android)
25+
import Android
2426
#endif
2527

2628
/// The type of a file system object.

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import CNIOLinux
2424
#elseif canImport(Musl)
2525
import Musl
2626
import CNIOLinux
27+
#elseif canImport(Android)
28+
import Android
29+
import CNIOLinux
2730
#endif
2831

2932
/// Aliases for platform-dependent types used for system calls.
@@ -34,6 +37,8 @@ extension CInterop {
3437
public typealias Stat = Glibc.stat
3538
#elseif canImport(Musl)
3639
public typealias Stat = Musl.stat
40+
#elseif canImport(Android)
41+
public typealias Stat = Android.stat
3742
#endif
3843

3944
#if canImport(Darwin)
@@ -45,11 +50,14 @@ extension CInterop {
4550
#elseif canImport(Musl)
4651
@_spi(Testing)
4752
public static let maxPathLength = Musl.PATH_MAX
53+
#elseif canImport(Android)
54+
@_spi(Testing)
55+
public static let maxPathLength = Android.PATH_MAX
4856
#endif
4957

5058
#if canImport(Darwin)
5159
typealias DirPointer = UnsafeMutablePointer<Darwin.DIR>
52-
#elseif canImport(Glibc) || canImport(Musl)
60+
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
5361
typealias DirPointer = OpaquePointer
5462
#endif
5563

@@ -59,12 +67,14 @@ extension CInterop {
5967
typealias DirEnt = Glibc.dirent
6068
#elseif canImport(Musl)
6169
typealias DirEnt = Musl.dirent
70+
#elseif canImport(Android)
71+
typealias DirEnt = Android.dirent
6272
#endif
6373

6474
#if canImport(Darwin)
6575
typealias FTS = CNIODarwin.FTS
6676
typealias FTSEnt = CNIODarwin.FTSENT
67-
#elseif canImport(Glibc) || canImport(Musl)
77+
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
6878
typealias FTS = CNIOLinux.FTS
6979
typealias FTSEnt = CNIOLinux.FTSENT
7080
#endif

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

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import Darwin
2121
import Glibc
2222
#elseif canImport(Musl)
2323
import Musl
24+
#elseif canImport(Android)
25+
import Android
2426
#endif
2527

2628
extension Errno {
@@ -33,6 +35,8 @@ extension Errno {
3335
return Errno(rawValue: Glibc.errno)
3436
#elseif canImport(Musl)
3537
return Errno(rawValue: Musl.errno)
38+
#elseif canImport(Android)
39+
return Errno(rawValue: Android.errno)
3640
#endif
3741
}
3842
set {
@@ -42,6 +46,8 @@ extension Errno {
4246
Glibc.errno = newValue.rawValue
4347
#elseif canImport(Musl)
4448
Musl.errno = newValue.rawValue
49+
#elseif canImport(Android)
50+
Android.errno = newValue.rawValue
4551
#endif
4652
}
4753
}
@@ -53,6 +59,8 @@ extension Errno {
5359
Glibc.errno = 0
5460
#elseif canImport(Musl)
5561
Musl.errno = 0
62+
#elseif canImport(Android)
63+
Android.errno = 0
5664
#endif
5765
}
5866
}

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import CNIOLinux
2424
#elseif canImport(Musl)
2525
import Musl
2626
import CNIOLinux
27+
#elseif canImport(Bionic)
28+
import Bionic
29+
import CNIOLinux
2730
#endif
2831

2932
extension FileDescriptor {
@@ -309,7 +312,7 @@ extension FileDescriptor {
309312
}
310313
}
311314

312-
#if canImport(Glibc) || canImport(Musl)
315+
#if canImport(Glibc) || canImport(Musl) || canImport(Bionic)
313316
extension FileDescriptor.OpenOptions {
314317
static var temporaryFile: Self {
315318
Self(rawValue: CNIOLinux_O_TMPFILE)

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

+8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import CNIOLinux
2828
#elseif canImport(Musl)
2929
import Musl
3030
import CNIOLinux
31+
#elseif canImport(Android)
32+
import Android
33+
import CNIOLinux
3134
#endif
3235

3336
// Syscall mocking support.
@@ -284,6 +287,11 @@ internal var system_errno: CInt {
284287
get { Musl.errno }
285288
set { Musl.errno = newValue }
286289
}
290+
#elseif canImport(Android)
291+
internal var system_errno: CInt {
292+
get { Android.errno }
293+
set { Android.errno = newValue }
294+
}
287295
#endif
288296

289297
// MARK: C stdlib decls

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import CNIOLinux
2424
#elseif canImport(Musl)
2525
import Musl
2626
import CNIOLinux
27+
#elseif canImport(Bionic)
28+
import Bionic
29+
import CNIOLinux
2730
#endif
2831

2932
@_spi(Testing)
@@ -106,7 +109,7 @@ public enum Syscall {
106109
}
107110
#endif
108111

109-
#if canImport(Glibc) || canImport(Musl)
112+
#if canImport(Glibc) || canImport(Musl) || canImport(Bionic)
110113
@_spi(Testing)
111114
public static func rename(
112115
from old: FilePath,
@@ -148,7 +151,7 @@ public enum Syscall {
148151
}
149152
#endif
150153

151-
#if canImport(Glibc) || canImport(Musl)
154+
#if canImport(Glibc) || canImport(Musl) || canImport(Bionic)
152155
@_spi(Testing)
153156
public struct LinkAtFlags: OptionSet {
154157
@_spi(Testing)
@@ -253,7 +256,7 @@ public enum Syscall {
253256
}
254257
}
255258

256-
#if canImport(Glibc) || canImport(Musl)
259+
#if canImport(Glibc) || canImport(Musl) || canImport(Bionic)
257260
@_spi(Testing)
258261
public static func sendfile(
259262
to output: FileDescriptor,

0 commit comments

Comments
 (0)