Skip to content

Commit 31a5e4d

Browse files
committed
Workaround broken glibc modulemap
We have been running into modularization issues on newer versions of various Linux distros, resulting in the compiler saying that the Glibc module doesn't have a SOCK_STREAM or SOCK_DGRAM. From some poking around, the definition is now coming from the CoreFoundation module as far as Swift is concerned. This is ultimately because our modulemap for Glibc is bad, but also means that I can't bring up Swift 6 on all of the Linux distros that 5.10 has support for. This workaround removes the explicit module name from `SOCK_STREAM` and renames it to `FOUNDATION_SOCK_STREAM` to avoid an ambiguous name, and completely removes `SOCK_DGRAM`. Both SOCK_STREAM and SOCK_DGRAM are fileprivates, so changing them will have no visible external effect. It is true that if another header somewhere defines `SOCK_STREAM`, we may pick it up instead of the definition from the glibc module, but that will also cause some nasty surprises to anyone using that header in C, so it is unlikely. Fixes: rdar://128079849
1 parent a4995d0 commit 31a5e4d

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

Sources/Foundation/Port.swift

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,14 @@ open class SocketPort: Port {}
9292

9393
#if canImport(Glibc) && !os(Android) && !os(OpenBSD)
9494
import Glibc
95-
fileprivate let SOCK_STREAM = Int32(Glibc.SOCK_STREAM.rawValue)
96-
fileprivate let SOCK_DGRAM = Int32(Glibc.SOCK_DGRAM.rawValue)
97-
fileprivate let IPPROTO_TCP = Int32(Glibc.IPPROTO_TCP)
95+
fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM.rawValue)
96+
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
9897
#endif
9998

10099
#if canImport(Glibc) && os(Android) || os(OpenBSD)
101100
import Glibc
102-
fileprivate let SOCK_STREAM = Int32(Glibc.SOCK_STREAM)
103-
fileprivate let SOCK_DGRAM = Int32(Glibc.SOCK_DGRAM)
104-
fileprivate let IPPROTO_TCP = Int32(Glibc.IPPROTO_TCP)
101+
fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM)
102+
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
105103
fileprivate let INADDR_ANY: in_addr_t = 0
106104
#if os(OpenBSD)
107105
fileprivate let INADDR_LOOPBACK = 0x7f000001
@@ -123,7 +121,7 @@ import WinSDK
123121
fileprivate typealias sa_family_t = ADDRESS_FAMILY
124122
fileprivate typealias in_port_t = USHORT
125123
fileprivate typealias in_addr_t = UInt32
126-
fileprivate let IPPROTO_TCP = Int32(WinSDK.IPPROTO_TCP.rawValue)
124+
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(WinSDK.IPPROTO_TCP.rawValue)
127125
#endif
128126

129127
// MARK: Darwin representation of socket addresses
@@ -484,7 +482,7 @@ open class SocketPort : Port {
484482

485483
let data = withUnsafeBytes(of: address) { Data($0) }
486484

487-
self.init(protocolFamily: PF_INET, socketType: SOCK_STREAM, protocol: IPPROTO_TCP, address: data)
485+
self.init(protocolFamily: PF_INET, socketType: FOUNDATION_SOCK_STREAM, protocol: FOUNDATION_IPPROTO_TCP, address: data)
488486
}
489487

490488
private final func createNonuniquedCore(from socket: CFSocket, protocolFamily family: Int32, socketType type: Int32, protocol: Int32) {
@@ -500,7 +498,7 @@ open class SocketPort : Port {
500498
var context = CFSocketContext()
501499
context.info = Unmanaged.passUnretained(self).toOpaque()
502500
var s: CFSocket
503-
if type == SOCK_STREAM {
501+
if type == FOUNDATION_SOCK_STREAM {
504502
s = CFSocketCreate(nil, family, type, `protocol`, CFOptionFlags(kCFSocketAcceptCallBack), __NSFireSocketAccept, &context)
505503
} else {
506504
s = CFSocketCreate(nil, family, type, `protocol`, CFOptionFlags(kCFSocketDataCallBack), __NSFireSocketDatagram, &context)
@@ -519,7 +517,7 @@ open class SocketPort : Port {
519517
var context = CFSocketContext()
520518
context.info = Unmanaged.passUnretained(self).toOpaque()
521519
var s: CFSocket
522-
if type == SOCK_STREAM {
520+
if type == FOUNDATION_SOCK_STREAM {
523521
s = CFSocketCreateWithNative(nil, CFSocketNativeHandle(sock), CFOptionFlags(kCFSocketAcceptCallBack), __NSFireSocketAccept, &context)
524522
} else {
525523
s = CFSocketCreateWithNative(nil, CFSocketNativeHandle(sock), CFOptionFlags(kCFSocketDataCallBack), __NSFireSocketDatagram, &context)
@@ -543,7 +541,7 @@ open class SocketPort : Port {
543541
sinAddr.sin_addr = inAddr
544542

545543
let data = withUnsafeBytes(of: sinAddr) { Data($0) }
546-
self.init(remoteWithProtocolFamily: PF_INET, socketType: SOCK_STREAM, protocol: IPPROTO_TCP, address: data)
544+
self.init(remoteWithProtocolFamily: PF_INET, socketType: FOUNDATION_SOCK_STREAM, protocol: FOUNDATION_IPPROTO_TCP, address: data)
547545
return
548546
}
549547
}
@@ -556,7 +554,7 @@ open class SocketPort : Port {
556554
sinAddr.sin6_addr = in6Addr
557555

558556
let data = withUnsafeBytes(of: sinAddr) { Data($0) }
559-
self.init(remoteWithProtocolFamily: PF_INET, socketType: SOCK_STREAM, protocol: IPPROTO_TCP, address: data)
557+
self.init(remoteWithProtocolFamily: PF_INET, socketType: FOUNDATION_SOCK_STREAM, protocol: FOUNDATION_IPPROTO_TCP, address: data)
560558
return
561559
}
562560
}
@@ -573,7 +571,7 @@ open class SocketPort : Port {
573571
withUnsafeBytes(of: in_addr_t(INADDR_LOOPBACK).bigEndian) { buffer.copyMemory(from: $0) }
574572
}
575573
let data = withUnsafeBytes(of: sinAddr) { Data($0) }
576-
self.init(remoteWithProtocolFamily: PF_INET, socketType: SOCK_STREAM, protocol: IPPROTO_TCP, address: data)
574+
self.init(remoteWithProtocolFamily: PF_INET, socketType: FOUNDATION_SOCK_STREAM, protocol: FOUNDATION_IPPROTO_TCP, address: data)
577575
}
578576

579577
private static let remoteSocketCoresLock = NSLock()
@@ -1049,7 +1047,7 @@ open class SocketPort : Port {
10491047
if let connector = core.connectors[signature], CFSocketIsValid(connector) {
10501048
return connector
10511049
} else {
1052-
if signature.socketType == SOCK_STREAM {
1050+
if signature.socketType == FOUNDATION_SOCK_STREAM {
10531051
if let connector = CFSocketCreate(nil, socketKind.protocolFamily, socketKind.socketType, socketKind.protocol, CFOptionFlags(kCFSocketDataCallBack), __NSFireSocketData, &context), CFSocketIsValid(connector) {
10541052
var timeout = time - Date.timeIntervalSinceReferenceDate
10551053
if timeout < 0 || timeout >= SocketPort.maximumTimeout {

0 commit comments

Comments
 (0)