Skip to content

Commit fddda82

Browse files
committed
Fix FreeBSD build
Various fixes to enable building on FreeBSD. Fixed an issue that warning messages from libthr can be emitted to stderr when thread specific data is used. This is due to CoreFoundation setting the TSD to `CF_TSD_BAD_PTR` even after `PTHERAD_DESTRUCTOR_ITERATIONS` iterators of dtor calls. This causes libthr to emit a warning to stderr about left-over non-null TSD after max destructor iterations.
1 parent 8255a77 commit fddda82

File tree

11 files changed

+37
-20
lines changed

11 files changed

+37
-20
lines changed

Sources/CoreFoundation/CFPlatform.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ const char *_CFProcessPath(void) {
281281

282282
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_BSD
283283
CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void) {
284-
#if defined(__OpenBSD__)
284+
#if defined(__OpenBSD__) || defined(__FreeBSD__)
285285
return pthread_equal(pthread_self(), _CFMainPThread) != 0;
286286
#else
287287
return pthread_main_np() == 1;
@@ -928,9 +928,13 @@ static void __CFTSDFinalize(void *arg) {
928928
#if _POSIX_THREADS && !TARGET_OS_WASI
929929
if (table->destructorCount == PTHREAD_DESTRUCTOR_ITERATIONS - 1) { // On PTHREAD_DESTRUCTOR_ITERATIONS-1 call, destroy our data
930930
free(table);
931-
931+
932+
// FreeBSD libthr emits debug message to stderr if there are leftover nonnull TSD after PTHREAD_DESTRUCTOR_ITERATIONS
933+
// On this platform, the destructor will never be called again, therefore it is unneccessary to set the TSD to CF_TSD_BAD_PTR
934+
#if !defined(FreeBSD)
932935
// Now if the destructor is called again we will take the shortcut at the beginning of this function.
933936
__CFTSDSetSpecific(CF_TSD_BAD_PTR);
937+
#endif
934938
return;
935939
}
936940
#else

Sources/CoreFoundation/include/CoreFoundation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474

7575
#include "ForSwiftFoundationOnly.h"
7676

77-
#if TARGET_OS_OSX || TARGET_OS_IPHONE || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_WASI
77+
#if TARGET_OS_OSX || TARGET_OS_IPHONE || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_WASI || TARGET_OS_BSD
7878
# if !TARGET_OS_WASI
7979
#include "CFMessagePort.h"
8080
#include "CFPlugIn.h"

Sources/CoreFoundation/internalInclude/CFBundle_Internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ CF_PRIVATE const CFStringRef _kCFBundleUseAppleLocalizationsKey;
431431
// The buffer must be PATH_MAX long or more.
432432
static bool _CFGetPathFromFileDescriptor(int fd, char *path);
433433

434-
#if TARGET_OS_MAC || (TARGET_OS_BSD && !defined(__OpenBSD__))
434+
#if TARGET_OS_MAC || (TARGET_OS_BSD && !defined(__OpenBSD__) && !defined(__FreeBSD__))
435435

436436
static bool _CFGetPathFromFileDescriptor(int fd, char *path) {
437437
return fcntl(fd, F_GETPATH, path) != -1;

Sources/Foundation/FileManager+POSIX.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ extension FileManager {
8989
}
9090
urls = mountPoints(statBuf, Int(fsCount))
9191
}
92-
#elseif os(OpenBSD)
92+
#elseif os(OpenBSD) || os(FreeBSD)
9393
func mountPoints(_ statBufs: UnsafePointer<statfs>, _ fsCount: Int) -> [URL] {
9494
var urls: [URL] = []
9595

Sources/Foundation/FileManager.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,15 @@ extension FileManager {
316316
flags |= flagsToSet
317317
flags &= ~flagsToUnset
318318

319-
guard chflags(fsRep, flags) == 0 else {
319+
#if os(FreeBSD)
320+
guard chflags(path, UInt(flags)) == 0 else {
320321
throw _NSErrorWithErrno(errno, reading: false, path: path)
321322
}
323+
#else
324+
guard chflags(path, flags) == 0 else {
325+
throw _NSErrorWithErrno(errno, reading: false, path: path)
326+
}
327+
#endif
322328
#endif
323329
}
324330

Sources/Foundation/Host.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ open class Host: NSObject {
194194
let family = ifa_addr.pointee.sa_family
195195
if family == sa_family_t(AF_INET) || family == sa_family_t(AF_INET6) {
196196
let sa_len: socklen_t = socklen_t((family == sa_family_t(AF_INET6)) ? MemoryLayout<sockaddr_in6>.size : MemoryLayout<sockaddr_in>.size)
197-
#if os(OpenBSD)
197+
#if os(OpenBSD) || os(FreeBSD)
198198
let hostlen = size_t(NI_MAXHOST)
199199
#else
200200
let hostlen = socklen_t(NI_MAXHOST)
@@ -294,7 +294,7 @@ open class Host: NSObject {
294294
}
295295
var hints = addrinfo()
296296
hints.ai_family = PF_UNSPEC
297-
#if os(macOS) || os(iOS) || os(Android) || os(OpenBSD) || canImport(Musl)
297+
#if os(macOS) || os(iOS) || os(Android) || os(OpenBSD) || canImport(Musl) || os(FreeBSD)
298298
hints.ai_socktype = SOCK_STREAM
299299
#else
300300
hints.ai_socktype = Int32(SOCK_STREAM.rawValue)
@@ -324,7 +324,7 @@ open class Host: NSObject {
324324
}
325325
let sa_len: socklen_t = socklen_t((family == AF_INET6) ? MemoryLayout<sockaddr_in6>.size : MemoryLayout<sockaddr_in>.size)
326326
let lookupInfo = { (content: inout [String], flags: Int32) in
327-
#if os(OpenBSD)
327+
#if os(OpenBSD) || os(FreeBSD)
328328
let hostlen = size_t(NI_MAXHOST)
329329
#else
330330
let hostlen = socklen_t(NI_MAXHOST)

Sources/Foundation/NSLock.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extension NSLocking {
3939
private typealias _MutexPointer = UnsafeMutablePointer<SRWLOCK>
4040
private typealias _RecursiveMutexPointer = UnsafeMutablePointer<CRITICAL_SECTION>
4141
private typealias _ConditionVariablePointer = UnsafeMutablePointer<CONDITION_VARIABLE>
42-
#elseif CYGWIN || os(OpenBSD)
42+
#elseif CYGWIN || os(OpenBSD) || os(FreeBSD)
4343
private typealias _MutexPointer = UnsafeMutablePointer<pthread_mutex_t?>
4444
private typealias _RecursiveMutexPointer = UnsafeMutablePointer<pthread_mutex_t?>
4545
private typealias _ConditionVariablePointer = UnsafeMutablePointer<pthread_cond_t?>
@@ -287,14 +287,14 @@ open class NSRecursiveLock: NSObject, NSLocking, @unchecked Sendable {
287287
InitializeConditionVariable(timeoutCond)
288288
InitializeSRWLock(timeoutMutex)
289289
#else
290-
#if CYGWIN || os(OpenBSD)
290+
#if CYGWIN || os(OpenBSD) || os(FreeBSD)
291291
var attrib : pthread_mutexattr_t? = nil
292292
#else
293293
var attrib = pthread_mutexattr_t()
294294
#endif
295295
withUnsafeMutablePointer(to: &attrib) { attrs in
296296
pthread_mutexattr_init(attrs)
297-
#if os(OpenBSD)
297+
#if os(OpenBSD) || os(FreeBSD)
298298
let type = Int32(PTHREAD_MUTEX_RECURSIVE.rawValue)
299299
#else
300300
let type = Int32(PTHREAD_MUTEX_RECURSIVE)

Sources/Foundation/NSPlatform.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#if os(macOS) || os(iOS)
1111
fileprivate let _NSPageSize = Int(vm_page_size)
12-
#elseif os(Linux) || os(Android) || os(OpenBSD)
12+
#elseif os(Linux) || os(Android) || os(OpenBSD) || os(FreeBSD)
1313
fileprivate let _NSPageSize = Int(getpagesize())
1414
#elseif os(Windows)
1515
import WinSDK

Sources/Foundation/Port.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fileprivate let FOUNDATION_SOCK_STREAM = SOCK_STREAM
107107
fileprivate let FOUNDATION_IPPROTO_TCP = IPPROTO_TCP
108108
#endif
109109

110-
#if canImport(Glibc) && !os(Android) && !os(OpenBSD)
110+
#if canImport(Glibc) && !os(Android) && !os(OpenBSD) && !os(FreeBSD)
111111
import Glibc
112112
fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM.rawValue)
113113
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
@@ -119,7 +119,7 @@ fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM)
119119
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
120120
#endif
121121

122-
#if canImport(Glibc) && os(Android) || os(OpenBSD)
122+
#if canImport(Glibc) && os(Android) || os(OpenBSD) || os(FreeBSD)
123123
import Glibc
124124
fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM)
125125
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)

Sources/Foundation/Process.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ open class Process: NSObject, @unchecked Sendable {
779779
}
780780

781781
var taskSocketPair : [Int32] = [0, 0]
782-
#if os(macOS) || os(iOS) || os(Android) || os(OpenBSD) || canImport(Musl)
782+
#if os(macOS) || os(iOS) || os(Android) || os(OpenBSD) || os(FreeBSD) || canImport(Musl)
783783
socketpair(AF_UNIX, SOCK_STREAM, 0, &taskSocketPair)
784784
#else
785785
socketpair(AF_UNIX, Int32(SOCK_STREAM.rawValue), 0, &taskSocketPair)
@@ -936,7 +936,7 @@ open class Process: NSObject, @unchecked Sendable {
936936
useFallbackChdir = false
937937
}
938938

939-
#if canImport(Darwin) || os(Android) || os(OpenBSD)
939+
#if canImport(Darwin) || os(Android) || os(OpenBSD) || os(FreeBSD)
940940
var spawnAttrs: posix_spawnattr_t? = nil
941941
#else
942942
var spawnAttrs: posix_spawnattr_t = posix_spawnattr_t()

Sources/Foundation/Thread.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ open class Thread : NSObject {
224224
get { _attrStorage.value }
225225
set { _attrStorage.value = newValue }
226226
}
227-
#elseif CYGWIN || os(OpenBSD)
227+
#elseif CYGWIN || os(OpenBSD) || os(FreeBSD)
228228
internal var _attr : pthread_attr_t? = nil
229229
#else
230230
internal var _attr = pthread_attr_t()
@@ -264,7 +264,7 @@ open class Thread : NSObject {
264264
_status = .finished
265265
return
266266
}
267-
#if CYGWIN || os(OpenBSD)
267+
#if CYGWIN || os(OpenBSD) || os(FreeBSD)
268268
if let attr = self._attr {
269269
_thread = self.withRetainedReference {
270270
return _CFThreadCreate(attr, NSThreadStart, $0)
@@ -388,6 +388,8 @@ open class Thread : NSObject {
388388
#elseif os(Windows)
389389
let count = RtlCaptureStackBackTrace(0, DWORD(maxSupportedStackDepth),
390390
addrs, nil)
391+
#elseif os(FreeBSD)
392+
let count = backtrace(addrs, maxSupportedStackDepth)
391393
#else
392394
let count = backtrace(addrs, Int32(maxSupportedStackDepth))
393395
#endif
@@ -449,7 +451,12 @@ open class Thread : NSObject {
449451
#else
450452
return backtraceAddresses({ (addrs, count) in
451453
var symbols: [String] = []
452-
if let bs = backtrace_symbols(addrs, Int32(count)) {
454+
#if os(FreeBSD)
455+
let bs = backtrace_symbols(addrs, count)
456+
#else
457+
let bs = backtrace_symbols(addrs, Int32(count))
458+
#endif
459+
if let bs {
453460
symbols = UnsafeBufferPointer(start: bs, count: count).map {
454461
guard let symbol = $0 else {
455462
return "<null>"

0 commit comments

Comments
 (0)