Skip to content

Commit 238130a

Browse files
authored
Merge pull request #5146 from michael-yuji/freebsd-wip
[FreeBSD] Fix FreeBSD build
2 parents 4c3228c + f415af0 commit 238130a

11 files changed

+38
-20
lines changed

Sources/CoreFoundation/CFPlatform.c

+6-2
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

+1-1
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

+1-1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ extension FileManager {
9393
}
9494
urls = mountPoints(statBuf, Int(fsCount))
9595
}
96-
#elseif os(OpenBSD)
96+
#elseif os(OpenBSD) || os(FreeBSD)
9797
func mountPoints(_ statBufs: UnsafePointer<statfs>, _ fsCount: Int) -> [URL] {
9898
var urls: [URL] = []
9999

Sources/Foundation/FileManager.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,15 @@ extension FileManager {
318318
flags |= flagsToSet
319319
flags &= ~flagsToUnset
320320

321-
guard chflags(fsRep, flags) == 0 else {
321+
#if os(FreeBSD)
322+
guard chflags(path, UInt(flags)) == 0 else {
322323
throw _NSErrorWithErrno(errno, reading: false, path: path)
323324
}
325+
#else
326+
guard chflags(path, flags) == 0 else {
327+
throw _NSErrorWithErrno(errno, reading: false, path: path)
328+
}
329+
#endif
324330
#endif
325331
}
326332

Sources/Foundation/Host.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ open class Host: NSObject {
195195
let family = ifa_addr.pointee.sa_family
196196
if family == sa_family_t(AF_INET) || family == sa_family_t(AF_INET6) {
197197
let sa_len: socklen_t = socklen_t((family == sa_family_t(AF_INET6)) ? MemoryLayout<sockaddr_in6>.size : MemoryLayout<sockaddr_in>.size)
198-
#if os(OpenBSD)
198+
#if os(OpenBSD) || os(FreeBSD)
199199
let hostlen = size_t(NI_MAXHOST)
200200
#else
201201
let hostlen = socklen_t(NI_MAXHOST)
@@ -295,7 +295,7 @@ open class Host: NSObject {
295295
}
296296
var hints = addrinfo()
297297
hints.ai_family = PF_UNSPEC
298-
#if os(macOS) || os(iOS) || os(Android) || os(OpenBSD) || canImport(Musl)
298+
#if os(macOS) || os(iOS) || os(Android) || os(OpenBSD) || canImport(Musl) || os(FreeBSD)
299299
hints.ai_socktype = SOCK_STREAM
300300
#else
301301
hints.ai_socktype = Int32(SOCK_STREAM.rawValue)
@@ -325,7 +325,7 @@ open class Host: NSObject {
325325
}
326326
let sa_len: socklen_t = socklen_t((family == AF_INET6) ? MemoryLayout<sockaddr_in6>.size : MemoryLayout<sockaddr_in>.size)
327327
let lookupInfo = { (content: inout [String], flags: Int32) in
328-
#if os(OpenBSD)
328+
#if os(OpenBSD) || os(FreeBSD)
329329
let hostlen = size_t(NI_MAXHOST)
330330
#else
331331
let hostlen = socklen_t(NI_MAXHOST)

Sources/Foundation/NSLock.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extension NSLocking {
4141
private typealias _MutexPointer = UnsafeMutablePointer<SRWLOCK>
4242
private typealias _RecursiveMutexPointer = UnsafeMutablePointer<CRITICAL_SECTION>
4343
private typealias _ConditionVariablePointer = UnsafeMutablePointer<CONDITION_VARIABLE>
44-
#elseif CYGWIN || os(OpenBSD)
44+
#elseif CYGWIN || os(OpenBSD) || os(FreeBSD)
4545
private typealias _MutexPointer = UnsafeMutablePointer<pthread_mutex_t?>
4646
private typealias _RecursiveMutexPointer = UnsafeMutablePointer<pthread_mutex_t?>
4747
private typealias _ConditionVariablePointer = UnsafeMutablePointer<pthread_cond_t?>
@@ -289,14 +289,14 @@ open class NSRecursiveLock: NSObject, NSLocking, @unchecked Sendable {
289289
InitializeConditionVariable(timeoutCond)
290290
InitializeSRWLock(timeoutMutex)
291291
#else
292-
#if CYGWIN || os(OpenBSD)
292+
#if CYGWIN || os(OpenBSD) || os(FreeBSD)
293293
var attrib : pthread_mutexattr_t? = nil
294294
#else
295295
var attrib = pthread_mutexattr_t()
296296
#endif
297297
withUnsafeMutablePointer(to: &attrib) { attrs in
298298
pthread_mutexattr_init(attrs)
299-
#if os(OpenBSD)
299+
#if os(OpenBSD) || os(FreeBSD)
300300
let type = Int32(PTHREAD_MUTEX_RECURSIVE.rawValue)
301301
#else
302302
let type = Int32(PTHREAD_MUTEX_RECURSIVE)

Sources/Foundation/NSPlatform.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
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
#if canImport(Android)
1414
import Android
1515
#endif
16+
1617
fileprivate let _NSPageSize = Int(getpagesize())
1718
#elseif os(Windows)
1819
import WinSDK

Sources/Foundation/Port.swift

+2-2
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(OpenBSD)
110+
#if canImport(Glibc) && !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(OpenBSD)
122+
#if canImport(Glibc) && (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

+2-2
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ open class Process: NSObject, @unchecked Sendable {
781781
}
782782

783783
var taskSocketPair : [Int32] = [0, 0]
784-
#if os(macOS) || os(iOS) || os(Android) || os(OpenBSD) || canImport(Musl)
784+
#if os(macOS) || os(iOS) || os(Android) || os(OpenBSD) || os(FreeBSD) || canImport(Musl)
785785
socketpair(AF_UNIX, SOCK_STREAM, 0, &taskSocketPair)
786786
#else
787787
socketpair(AF_UNIX, Int32(SOCK_STREAM.rawValue), 0, &taskSocketPair)
@@ -938,7 +938,7 @@ open class Process: NSObject, @unchecked Sendable {
938938
useFallbackChdir = false
939939
}
940940

941-
#if canImport(Darwin) || os(Android) || os(OpenBSD)
941+
#if canImport(Darwin) || os(Android) || os(OpenBSD) || os(FreeBSD)
942942
var spawnAttrs: posix_spawnattr_t? = nil
943943
#else
944944
var spawnAttrs: posix_spawnattr_t = posix_spawnattr_t()

Sources/Foundation/Thread.swift

+10-3
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ open class Thread : NSObject {
226226
get { _attrStorage.value }
227227
set { _attrStorage.value = newValue }
228228
}
229-
#elseif CYGWIN || os(OpenBSD)
229+
#elseif CYGWIN || os(OpenBSD) || os(FreeBSD)
230230
internal var _attr : pthread_attr_t? = nil
231231
#else
232232
internal var _attr = pthread_attr_t()
@@ -266,7 +266,7 @@ open class Thread : NSObject {
266266
_status = .finished
267267
return
268268
}
269-
#if CYGWIN || os(OpenBSD)
269+
#if CYGWIN || os(OpenBSD) || os(FreeBSD)
270270
if let attr = self._attr {
271271
_thread = self.withRetainedReference {
272272
return _CFThreadCreate(attr, NSThreadStart, $0)
@@ -390,6 +390,8 @@ open class Thread : NSObject {
390390
#elseif os(Windows)
391391
let count = RtlCaptureStackBackTrace(0, DWORD(maxSupportedStackDepth),
392392
addrs, nil)
393+
#elseif os(FreeBSD)
394+
let count = backtrace(addrs, maxSupportedStackDepth)
393395
#else
394396
let count = backtrace(addrs, Int32(maxSupportedStackDepth))
395397
#endif
@@ -451,7 +453,12 @@ open class Thread : NSObject {
451453
#else
452454
return backtraceAddresses({ (addrs, count) in
453455
var symbols: [String] = []
454-
if let bs = backtrace_symbols(addrs, Int32(count)) {
456+
#if os(FreeBSD)
457+
let bs = backtrace_symbols(addrs, count)
458+
#else
459+
let bs = backtrace_symbols(addrs, Int32(count))
460+
#endif
461+
if let bs {
455462
symbols = UnsafeBufferPointer(start: bs, count: count).map {
456463
guard let symbol = $0 else {
457464
return "<null>"

0 commit comments

Comments
 (0)