Repository: apple/swift-nio
File: Sources/NIOPosix/System.swift (Swift)

Code before:
//  value before we were able to read it.
//  It's important that all static methods are declared with `@inline(never)` so it's not possible any ARC traffic happens while we need to read errno.

import NIOCore

#if canImport(Darwin)
@_exported import Darwin.C
import CNIODarwin
internal typealias MMsgHdr = CNIODarwin_mmsghdr
#elseif os(Linux) || os(FreeBSD) || os(Android)
#if canImport(Glibc)
@_exported import Glibc
#elseif canImport(Musl)
@_exported import Musl
#endif
import CNIOLinux
internal typealias MMsgHdr = CNIOLinux_mmsghdr
internal typealias in6_pktinfo = CNIOLinux_in6_pktinfo
#elseif os(Windows)
@_exported import ucrt

import CNIOWindows

internal typealias MMsgHdr = CNIOWindows_mmsghdr
#else
let badOS = { fatalError("unsupported OS") }()
#endif

#if os(Android)
let INADDR_ANY = UInt32(0) // #define INADDR_ANY ((unsigned long int) 0x00000000)
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)
internal typealias in_port_t = UInt16
extension ipv6_mreq { // http://lkml.iu.edu/hypermail/linux/kernel/0106.1/0080.html
    init (ipv6mr_multiaddr: in6_addr, ipv6mr_interface: UInt32) {
        self.init(ipv6mr_multiaddr: ipv6mr_multiaddr,
                  ipv6mr_ifindex: Int32(bitPattern: ipv6mr_interface))
    }
}
#if arch(arm)
let S_IFSOCK = UInt32(SwiftGlibc.S_IFSOCK)
let S_IFMT = UInt32(SwiftGlibc.S_IFMT)
let S_IFREG = UInt32(SwiftGlibc.S_IFREG)
let S_IFDIR = UInt32(SwiftGlibc.S_IFDIR)
let S_IFLNK = UInt32(SwiftGlibc.S_IFLNK)
let S_IFBLK = UInt32(SwiftGlibc.S_IFBLK)
#endif
#endif

// Declare aliases to share more code and not need to repeat #if #else blocks

Review comment (suggestion):
I have no idea why @weissi used this formulation instead of erroring, as I added now.

Code after:
//  value before we were able to read it.
//  It's important that all static methods are declared with `@inline(never)` so it's not possible any ARC traffic happens while we need to read errno.

import NIOCore

#if canImport(Darwin)
@_exported import Darwin.C
import CNIODarwin
internal typealias MMsgHdr = CNIODarwin_mmsghdr
#elseif os(Linux) || os(FreeBSD) || os(Android)
#if canImport(Glibc)
@_exported import Glibc
#elseif canImport(Musl)
@_exported import Musl
#endif
import CNIOLinux
internal typealias MMsgHdr = CNIOLinux_mmsghdr
internal typealias in6_pktinfo = CNIOLinux_in6_pktinfo
#elseif os(Windows)
@_exported import ucrt

import CNIOWindows

internal typealias MMsgHdr = CNIOWindows_mmsghdr
#else
#error("The POSIX system module was unable to identify your C library.")
#endif

#if os(Android)
let INADDR_ANY = UInt32(0) // #define INADDR_ANY ((unsigned long int) 0x00000000)
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)
internal typealias in_port_t = UInt16
extension ipv6_mreq { // http://lkml.iu.edu/hypermail/linux/kernel/0106.1/0080.html
    init (ipv6mr_multiaddr: in6_addr, ipv6mr_interface: UInt32) {
        self.init(ipv6mr_multiaddr: ipv6mr_multiaddr,
                  ipv6mr_ifindex: Int32(bitPattern: ipv6mr_interface))
    }
}
#if arch(arm)
let S_IFSOCK = UInt32(SwiftGlibc.S_IFSOCK)
let S_IFMT = UInt32(SwiftGlibc.S_IFMT)
let S_IFREG = UInt32(SwiftGlibc.S_IFREG)
let S_IFDIR = UInt32(SwiftGlibc.S_IFDIR)
let S_IFLNK = UInt32(SwiftGlibc.S_IFLNK)
let S_IFBLK = UInt32(SwiftGlibc.S_IFBLK)
#endif
#endif

// Declare aliases to share more code and not need to repeat #if #else blocks