Skip to content

Commit 3aa51bd

Browse files
committed
Paper over some minor platform API differences.
These may be better handled by some other mechanism, but for now, make these changes to get a clean compile: * getnameinfo on BSD takes a `size_t` for the host name, not a `socklen_t`. * PTHREAD_MUTEX_RECURSIVE is part of an enum on OpenBSD, so requires `.rawValue`. * Like on Android, SOCK_STREAM and SOCK_DGRAM are not enums. INADDR_LOOPBACK is a complex macro, so requires defining the flattened the value (this is of course brittle, but is the best option for now).
1 parent 56e5046 commit 3aa51bd

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

Diff for: Sources/Foundation/Host.swift

+12-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,12 @@ open class Host: NSObject {
183183
let family = ifa_addr.pointee.sa_family
184184
if family == sa_family_t(AF_INET) || family == sa_family_t(AF_INET6) {
185185
let sa_len: socklen_t = socklen_t((family == sa_family_t(AF_INET6)) ? MemoryLayout<sockaddr_in6>.size : MemoryLayout<sockaddr_in>.size)
186-
if getnameinfo(ifa_addr, sa_len, address, socklen_t(NI_MAXHOST), nil, 0, NI_NUMERICHOST) == 0 {
186+
#if os(OpenBSD)
187+
let hostlen = size_t(NI_MAXHOST)
188+
#else
189+
let hostlen = socklen_t(NI_MAXHOST)
190+
#endif
191+
if getnameinfo(ifa_addr, sa_len, address, hostlen, nil, 0, NI_NUMERICHOST) == 0 {
187192
_addresses.append(String(cString: address))
188193
}
189194
}
@@ -303,7 +308,12 @@ open class Host: NSObject {
303308
}
304309
let sa_len: socklen_t = socklen_t((family == AF_INET6) ? MemoryLayout<sockaddr_in6>.size : MemoryLayout<sockaddr_in>.size)
305310
let lookupInfo = { (content: inout [String], flags: Int32) in
306-
if getnameinfo(info.ai_addr, sa_len, host, socklen_t(NI_MAXHOST), nil, 0, flags) == 0 {
311+
#if os(OpenBSD)
312+
let hostlen = size_t(NI_MAXHOST)
313+
#else
314+
let hostlen = socklen_t(NI_MAXHOST)
315+
#endif
316+
if getnameinfo(info.ai_addr, sa_len, host, hostlen, nil, 0, flags) == 0 {
307317
content.append(String(cString: host))
308318
}
309319
}

Diff for: Sources/Foundation/NSLock.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,12 @@ open class NSRecursiveLock: NSObject, NSLocking {
249249
#endif
250250
withUnsafeMutablePointer(to: &attrib) { attrs in
251251
pthread_mutexattr_init(attrs)
252-
pthread_mutexattr_settype(attrs, Int32(PTHREAD_MUTEX_RECURSIVE))
252+
#if os(OpenBSD)
253+
let type = Int32(PTHREAD_MUTEX_RECURSIVE.rawValue)
254+
#else
255+
let type = Int32(PTHREAD_MUTEX_RECURSIVE)
256+
#endif
257+
pthread_mutexattr_settype(attrs, type)
253258
pthread_mutex_init(mutex, attrs)
254259
}
255260
#if os(macOS) || os(iOS)

Diff for: Sources/Foundation/Port.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,22 @@ public protocol PortDelegate: AnyObject {
8383
func handle(_ message: PortMessage)
8484
}
8585

86-
#if canImport(Glibc) && !os(Android)
86+
#if canImport(Glibc) && !os(Android) && !os(OpenBSD)
8787
import Glibc
8888
fileprivate let SOCK_STREAM = Int32(Glibc.SOCK_STREAM.rawValue)
8989
fileprivate let SOCK_DGRAM = Int32(Glibc.SOCK_DGRAM.rawValue)
9090
fileprivate let IPPROTO_TCP = Int32(Glibc.IPPROTO_TCP)
9191
#endif
9292

93-
#if canImport(Glibc) && os(Android)
93+
#if canImport(Glibc) && os(Android) || os(OpenBSD)
9494
import Glibc
9595
fileprivate let SOCK_STREAM = Int32(Glibc.SOCK_STREAM)
9696
fileprivate let SOCK_DGRAM = Int32(Glibc.SOCK_DGRAM)
9797
fileprivate let IPPROTO_TCP = Int32(Glibc.IPPROTO_TCP)
9898
fileprivate let INADDR_ANY: in_addr_t = 0
99+
#if os(OpenBSD)
100+
fileprivate let INADDR_LOOPBACK = 0x7f000001
101+
#endif
99102
#endif
100103

101104

Diff for: Sources/Foundation/Thread.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,13 @@ open class Thread : NSObject {
135135
var ti = end_at - start_at
136136
let end_ut = start_ut + ti
137137
while (0.0 < ti) {
138-
var __ts__ = timespec(tv_sec: Int.max, tv_nsec: 0)
138+
var __ts__ = timespec(tv_sec: time_t.max, tv_nsec: 0)
139139
if ti < Double(Int.max) {
140140
var integ = 0.0
141141
let frac: Double = withUnsafeMutablePointer(to: &integ) { integp in
142142
return modf(ti, integp)
143143
}
144-
__ts__.tv_sec = Int(integ)
144+
__ts__.tv_sec = time_t(integ)
145145
__ts__.tv_nsec = Int(frac * 1000000000.0)
146146
}
147147
let _ = withUnsafePointer(to: &__ts__) { ts in
@@ -170,13 +170,13 @@ open class Thread : NSObject {
170170
let start_ut = CFGetSystemUptime()
171171
let end_ut = start_ut + ti
172172
while 0.0 < ti {
173-
var __ts__ = timespec(tv_sec: Int.max, tv_nsec: 0)
173+
var __ts__ = timespec(tv_sec: time_t.max, tv_nsec: 0)
174174
if ti < Double(Int.max) {
175175
var integ = 0.0
176176
let frac: Double = withUnsafeMutablePointer(to: &integ) { integp in
177177
return modf(ti, integp)
178178
}
179-
__ts__.tv_sec = Int(integ)
179+
__ts__.tv_sec = time_t(integ)
180180
__ts__.tv_nsec = Int(frac * 1000000000.0)
181181
}
182182
let _ = withUnsafePointer(to: &__ts__) { ts in

Diff for: Tests/Foundation/HTTPServer.swift

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ import Dispatch
2727
typealias SOCKET = Int32
2828
#endif
2929

30+
#if os(OpenBSD)
31+
let INADDR_LOOPBACK = 0x7f000001
32+
#endif
33+
3034
private let serverDebug = (ProcessInfo.processInfo.environment["SCLF_HTTP_SERVER_DEBUG"] == "YES")
3135

3236
private func debugLog(_ msg: String) {

0 commit comments

Comments
 (0)