Skip to content

Commit

Permalink
Use the new Android overlay and Bionic module from Swift 6
Browse files Browse the repository at this point in the history
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
  • Loading branch information
finagolfin committed Jul 22, 2024
1 parent 2841257 commit 17d1def
Show file tree
Hide file tree
Showing 33 changed files with 164 additions and 43 deletions.
2 changes: 2 additions & 0 deletions Sources/NIOConcurrencyHelpers/NIOLock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import WinSDK
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Bionic)
import Bionic
#else
#error("The concurrency NIOLock module was unable to identify your C library.")
#endif
Expand Down
2 changes: 2 additions & 0 deletions Sources/NIOConcurrencyHelpers/atomics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ private func sys_sched_yield() {
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Bionic)
import Bionic
#else
#error("The concurrency atomics module was unable to identify your C library.")
#endif
Expand Down
2 changes: 2 additions & 0 deletions Sources/NIOConcurrencyHelpers/lock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import WinSDK
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Bionic)
import Bionic
#else
#error("The concurrency lock module was unable to identify your C library.")
#endif
Expand Down
8 changes: 8 additions & 0 deletions Sources/NIOCore/BSDSocketAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ internal typealias socklen_t = ucrt.size_t
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Android)
import Android
#endif
import CNIOLinux

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

#if os(Android)
#if compiler(>=6.0)
let IFF_BROADCAST: CUnsignedInt = numericCast(Android.IFF_BROADCAST.rawValue)
let IFF_POINTOPOINT: CUnsignedInt = numericCast(Android.IFF_POINTOPOINT.rawValue)
let IFF_MULTICAST: CUnsignedInt = numericCast(Android.IFF_MULTICAST.rawValue)
#else
let IFF_BROADCAST: CUnsignedInt = numericCast(SwiftGlibc.IFF_BROADCAST.rawValue)
let IFF_POINTOPOINT: CUnsignedInt = numericCast(SwiftGlibc.IFF_POINTOPOINT.rawValue)
let IFF_MULTICAST: CUnsignedInt = numericCast(SwiftGlibc.IFF_MULTICAST.rawValue)
#endif
#if arch(arm)
let SO_RCVTIMEO = SO_RCVTIMEO_OLD
let SO_TIMESTAMP = SO_TIMESTAMP_OLD
Expand Down
2 changes: 2 additions & 0 deletions Sources/NIOCore/ByteBuffer-core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import Darwin
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Bionic)
import Bionic
#else
#error("The Byte Buffer module was unable to identify your C library.")
#endif
Expand Down
2 changes: 2 additions & 0 deletions Sources/NIOCore/FileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import Darwin
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Android)
import Android
#else
#error("The File Handle module was unable to identify your C library.")
#endif
Expand Down
2 changes: 2 additions & 0 deletions Sources/NIOCore/FileRegion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import Darwin
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Bionic)
import Bionic
#else
#error("The File Region module was unable to identify your C library.")
#endif
Expand Down
2 changes: 2 additions & 0 deletions Sources/NIOCore/GlobalSingletons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import WinSDK
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Bionic)
import Bionic
#else
#error("Unsupported C library")
#endif
Expand Down
2 changes: 2 additions & 0 deletions Sources/NIOCore/IO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ internal func MAKELANGID(_ p: WORD, _ s: WORD) -> DWORD {
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Bionic)
import Bionic
#elseif canImport(Darwin)
import Darwin
#else
Expand Down
2 changes: 2 additions & 0 deletions Sources/NIOCore/Interfaces.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Bionic)
import Bionic
#endif
import CNIOLinux
#elseif canImport(Darwin)
Expand Down
2 changes: 2 additions & 0 deletions Sources/NIOCore/SocketAddresses.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import Darwin
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Android)
import Android
#endif
import CNIOLinux
#else
Expand Down
2 changes: 2 additions & 0 deletions Sources/NIOCore/SocketOptionProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import Darwin
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Bionic)
import Bionic
#endif
import CNIOLinux
#elseif os(Windows)
Expand Down
2 changes: 2 additions & 0 deletions Sources/NIOCore/SystemCallHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import Glibc
import Musl
#elseif os(Windows)
import CNIOWindows
#elseif canImport(Android)
import Android
#else
#error("The system call helpers module was unable to identify your C library.")
#endif
Expand Down
2 changes: 2 additions & 0 deletions Sources/NIOCore/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import CNIOLinux
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Android)
import Android
#endif
#elseif os(Windows)
import let WinSDK.RelationProcessorCore
Expand Down
11 changes: 7 additions & 4 deletions Sources/NIOFileSystem/FileInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import CNIOLinux
#elseif canImport(Musl)
import Musl
import CNIOLinux
#elseif canImport(Android)
import Android
import CNIOLinux
#endif

/// Information about a file system object.
Expand Down Expand Up @@ -85,7 +88,7 @@ public struct FileInfo: Hashable, Sendable {
self.lastAccessTime = Timespec(platformSpecificStatus.st_atimespec)
self.lastDataModificationTime = Timespec(platformSpecificStatus.st_mtimespec)
self.lastStatusChangeTime = Timespec(platformSpecificStatus.st_ctimespec)
#elseif canImport(Glibc) || canImport(Musl)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
self.lastAccessTime = Timespec(platformSpecificStatus.st_atim)
self.lastDataModificationTime = Timespec(platformSpecificStatus.st_mtim)
self.lastStatusChangeTime = Timespec(platformSpecificStatus.st_ctim)
Expand Down Expand Up @@ -150,7 +153,7 @@ extension FileInfo {
#if canImport(Darwin)
private static let utimeOmit = Int(UTIME_OMIT)
private static let utimeNow = Int(UTIME_NOW)
#elseif canImport(Glibc) || canImport(Musl)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
private static let utimeOmit = Int(CNIOLinux_UTIME_OMIT)
private static let utimeNow = Int(CNIOLinux_UTIME_NOW)
#endif
Expand Down Expand Up @@ -218,7 +221,7 @@ private struct Stat: Hashable {
hasher.combine(FileInfo.Timespec(stat.st_birthtimespec))
hasher.combine(stat.st_flags)
hasher.combine(stat.st_gen)
#elseif canImport(Glibc) || canImport(Musl)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
hasher.combine(FileInfo.Timespec(stat.st_atim))
hasher.combine(FileInfo.Timespec(stat.st_mtim))
hasher.combine(FileInfo.Timespec(stat.st_ctim))
Expand Down Expand Up @@ -259,7 +262,7 @@ private struct Stat: Hashable {
== FileInfo.Timespec(rStat.st_birthtimespec)
isEqual = isEqual && lStat.st_flags == rStat.st_flags
isEqual = isEqual && lStat.st_gen == rStat.st_gen
#elseif canImport(Glibc) || canImport(Musl)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
isEqual = isEqual && FileInfo.Timespec(lStat.st_atim) == FileInfo.Timespec(rStat.st_atim)
isEqual = isEqual && FileInfo.Timespec(lStat.st_mtim) == FileInfo.Timespec(rStat.st_mtim)
isEqual = isEqual && FileInfo.Timespec(lStat.st_ctim) == FileInfo.Timespec(rStat.st_ctim)
Expand Down
4 changes: 3 additions & 1 deletion Sources/NIOFileSystem/FileSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import Darwin
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Bionic)
import Bionic
#endif

/// A file system which interacts with the local system. The file system uses a thread pool to
Expand Down Expand Up @@ -1086,7 +1088,7 @@ extension FileSystem {
location: .here()
)
}
#elseif canImport(Glibc) || canImport(Musl)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
var offset = 0

while offset < sourceInfo.size {
Expand Down
2 changes: 2 additions & 0 deletions Sources/NIOFileSystem/FileType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import Darwin
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Android)
import Android
#endif

/// The type of a file system object.
Expand Down
14 changes: 12 additions & 2 deletions Sources/NIOFileSystem/Internal/System Calls/CInterop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import CNIOLinux
#elseif canImport(Musl)
import Musl
import CNIOLinux
#elseif canImport(Android)
import Android
import CNIOLinux
#endif

/// Aliases for platform-dependent types used for system calls.
Expand All @@ -34,6 +37,8 @@ extension CInterop {
public typealias Stat = Glibc.stat
#elseif canImport(Musl)
public typealias Stat = Musl.stat
#elseif canImport(Android)
public typealias Stat = Android.stat
#endif

#if canImport(Darwin)
Expand All @@ -45,11 +50,14 @@ extension CInterop {
#elseif canImport(Musl)
@_spi(Testing)
public static let maxPathLength = Musl.PATH_MAX
#elseif canImport(Android)
@_spi(Testing)
public static let maxPathLength = Android.PATH_MAX
#endif

#if canImport(Darwin)
typealias DirPointer = UnsafeMutablePointer<Darwin.DIR>
#elseif canImport(Glibc) || canImport(Musl)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
typealias DirPointer = OpaquePointer
#endif

Expand All @@ -59,12 +67,14 @@ extension CInterop {
typealias DirEnt = Glibc.dirent
#elseif canImport(Musl)
typealias DirEnt = Musl.dirent
#elseif canImport(Android)
typealias DirEnt = Android.dirent
#endif

#if canImport(Darwin)
typealias FTS = CNIODarwin.FTS
typealias FTSEnt = CNIODarwin.FTSENT
#elseif canImport(Glibc) || canImport(Musl)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
typealias FTS = CNIOLinux.FTS
typealias FTSEnt = CNIOLinux.FTSENT
#endif
Expand Down
8 changes: 8 additions & 0 deletions Sources/NIOFileSystem/Internal/System Calls/Errno.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import Darwin
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Android)
import Android
#endif

extension Errno {
Expand All @@ -33,6 +35,8 @@ extension Errno {
return Errno(rawValue: Glibc.errno)
#elseif canImport(Musl)
return Errno(rawValue: Musl.errno)
#elseif canImport(Android)
return Errno(rawValue: Android.errno)
#endif
}
set {
Expand All @@ -42,6 +46,8 @@ extension Errno {
Glibc.errno = newValue.rawValue
#elseif canImport(Musl)
Musl.errno = newValue.rawValue
#elseif canImport(Android)
Android.errno = newValue.rawValue
#endif
}
}
Expand All @@ -53,6 +59,8 @@ extension Errno {
Glibc.errno = 0
#elseif canImport(Musl)
Musl.errno = 0
#elseif canImport(Android)
Android.errno = 0
#endif
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import CNIOLinux
#elseif canImport(Musl)
import Musl
import CNIOLinux
#elseif canImport(Bionic)
import Bionic
import CNIOLinux
#endif

extension FileDescriptor {
Expand Down Expand Up @@ -309,7 +312,7 @@ extension FileDescriptor {
}
}

#if canImport(Glibc) || canImport(Musl)
#if canImport(Glibc) || canImport(Musl) || canImport(Bionic)
extension FileDescriptor.OpenOptions {
static var temporaryFile: Self {
Self(rawValue: CNIOLinux_O_TMPFILE)
Expand Down
8 changes: 8 additions & 0 deletions Sources/NIOFileSystem/Internal/System Calls/Mocking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import CNIOLinux
#elseif canImport(Musl)
import Musl
import CNIOLinux
#elseif canImport(Android)
import Android
import CNIOLinux
#endif

// Syscall mocking support.
Expand Down Expand Up @@ -284,6 +287,11 @@ internal var system_errno: CInt {
get { Musl.errno }
set { Musl.errno = newValue }
}
#elseif canImport(Android)
internal var system_errno: CInt {
get { Android.errno }
set { Android.errno = newValue }
}
#endif

// MARK: C stdlib decls
Expand Down
9 changes: 6 additions & 3 deletions Sources/NIOFileSystem/Internal/System Calls/Syscall.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import CNIOLinux
#elseif canImport(Musl)
import Musl
import CNIOLinux
#elseif canImport(Bionic)
import Bionic
import CNIOLinux
#endif

@_spi(Testing)
Expand Down Expand Up @@ -106,7 +109,7 @@ public enum Syscall {
}
#endif

#if canImport(Glibc) || canImport(Musl)
#if canImport(Glibc) || canImport(Musl) || canImport(Bionic)
@_spi(Testing)
public static func rename(
from old: FilePath,
Expand Down Expand Up @@ -148,7 +151,7 @@ public enum Syscall {
}
#endif

#if canImport(Glibc) || canImport(Musl)
#if canImport(Glibc) || canImport(Musl) || canImport(Bionic)
@_spi(Testing)
public struct LinkAtFlags: OptionSet {
@_spi(Testing)
Expand Down Expand Up @@ -253,7 +256,7 @@ public enum Syscall {
}
}

#if canImport(Glibc) || canImport(Musl)
#if canImport(Glibc) || canImport(Musl) || canImport(Bionic)
@_spi(Testing)
public static func sendfile(
to output: FileDescriptor,
Expand Down
Loading

0 comments on commit 17d1def

Please sign in to comment.