Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FreeBSD] Fix FreeBSD build #5146

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Sources/CoreFoundation/CFPlatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ const char *_CFProcessPath(void) {

#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_BSD
CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void) {
#if defined(__OpenBSD__)
#if defined(__OpenBSD__) || defined(__FreeBSD__)
return pthread_equal(pthread_self(), _CFMainPThread) != 0;
#else
return pthread_main_np() == 1;
Expand Down Expand Up @@ -928,9 +928,13 @@ static void __CFTSDFinalize(void *arg) {
#if _POSIX_THREADS && !TARGET_OS_WASI
if (table->destructorCount == PTHREAD_DESTRUCTOR_ITERATIONS - 1) { // On PTHREAD_DESTRUCTOR_ITERATIONS-1 call, destroy our data
free(table);


// FreeBSD libthr emits debug message to stderr if there are leftover nonnull TSD after PTHREAD_DESTRUCTOR_ITERATIONS
// On this platform, the destructor will never be called again, therefore it is unneccessary to set the TSD to CF_TSD_BAD_PTR
#if !defined(__FreeBSD__)
// Now if the destructor is called again we will take the shortcut at the beginning of this function.
__CFTSDSetSpecific(CF_TSD_BAD_PTR);
#endif
return;
}
#else
Expand Down
2 changes: 1 addition & 1 deletion Sources/CoreFoundation/include/CoreFoundation.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@

#include "ForSwiftFoundationOnly.h"

#if TARGET_OS_OSX || TARGET_OS_IPHONE || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_WASI
#if TARGET_OS_OSX || TARGET_OS_IPHONE || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_WASI || TARGET_OS_BSD
# if !TARGET_OS_WASI
#include "CFMessagePort.h"
#include "CFPlugIn.h"
Expand Down
2 changes: 1 addition & 1 deletion Sources/CoreFoundation/internalInclude/CFBundle_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ CF_PRIVATE const CFStringRef _kCFBundleUseAppleLocalizationsKey;
// The buffer must be PATH_MAX long or more.
static bool _CFGetPathFromFileDescriptor(int fd, char *path);

#if TARGET_OS_MAC || (TARGET_OS_BSD && !defined(__OpenBSD__))
#if TARGET_OS_MAC || (TARGET_OS_BSD && !defined(__OpenBSD__) && !defined(__FreeBSD__))

static bool _CFGetPathFromFileDescriptor(int fd, char *path) {
return fcntl(fd, F_GETPATH, path) != -1;
Expand Down
2 changes: 1 addition & 1 deletion Sources/Foundation/FileManager+POSIX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ extension FileManager {
}
urls = mountPoints(statBuf, Int(fsCount))
}
#elseif os(OpenBSD)
#elseif os(OpenBSD) || os(FreeBSD)
func mountPoints(_ statBufs: UnsafePointer<statfs>, _ fsCount: Int) -> [URL] {
var urls: [URL] = []

Expand Down
8 changes: 7 additions & 1 deletion Sources/Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,15 @@ extension FileManager {
flags |= flagsToSet
flags &= ~flagsToUnset

guard chflags(fsRep, flags) == 0 else {
#if os(FreeBSD)
guard chflags(path, UInt(flags)) == 0 else {
throw _NSErrorWithErrno(errno, reading: false, path: path)
}
#else
guard chflags(path, flags) == 0 else {
throw _NSErrorWithErrno(errno, reading: false, path: path)
}
#endif
#endif
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/Foundation/Host.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ open class Host: NSObject {
let family = ifa_addr.pointee.sa_family
if family == sa_family_t(AF_INET) || family == sa_family_t(AF_INET6) {
let sa_len: socklen_t = socklen_t((family == sa_family_t(AF_INET6)) ? MemoryLayout<sockaddr_in6>.size : MemoryLayout<sockaddr_in>.size)
#if os(OpenBSD)
#if os(OpenBSD) || os(FreeBSD)
let hostlen = size_t(NI_MAXHOST)
#else
let hostlen = socklen_t(NI_MAXHOST)
Expand Down Expand Up @@ -295,7 +295,7 @@ open class Host: NSObject {
}
var hints = addrinfo()
hints.ai_family = PF_UNSPEC
#if os(macOS) || os(iOS) || os(Android) || os(OpenBSD) || canImport(Musl)
#if os(macOS) || os(iOS) || os(Android) || os(OpenBSD) || canImport(Musl) || os(FreeBSD)
hints.ai_socktype = SOCK_STREAM
#else
hints.ai_socktype = Int32(SOCK_STREAM.rawValue)
Expand Down Expand Up @@ -325,7 +325,7 @@ open class Host: NSObject {
}
let sa_len: socklen_t = socklen_t((family == AF_INET6) ? MemoryLayout<sockaddr_in6>.size : MemoryLayout<sockaddr_in>.size)
let lookupInfo = { (content: inout [String], flags: Int32) in
#if os(OpenBSD)
#if os(OpenBSD) || os(FreeBSD)
let hostlen = size_t(NI_MAXHOST)
#else
let hostlen = socklen_t(NI_MAXHOST)
Expand Down
6 changes: 3 additions & 3 deletions Sources/Foundation/NSLock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extension NSLocking {
private typealias _MutexPointer = UnsafeMutablePointer<SRWLOCK>
private typealias _RecursiveMutexPointer = UnsafeMutablePointer<CRITICAL_SECTION>
private typealias _ConditionVariablePointer = UnsafeMutablePointer<CONDITION_VARIABLE>
#elseif CYGWIN || os(OpenBSD)
#elseif CYGWIN || os(OpenBSD) || os(FreeBSD)
private typealias _MutexPointer = UnsafeMutablePointer<pthread_mutex_t?>
private typealias _RecursiveMutexPointer = UnsafeMutablePointer<pthread_mutex_t?>
private typealias _ConditionVariablePointer = UnsafeMutablePointer<pthread_cond_t?>
Expand Down Expand Up @@ -289,14 +289,14 @@ open class NSRecursiveLock: NSObject, NSLocking, @unchecked Sendable {
InitializeConditionVariable(timeoutCond)
InitializeSRWLock(timeoutMutex)
#else
#if CYGWIN || os(OpenBSD)
#if CYGWIN || os(OpenBSD) || os(FreeBSD)
var attrib : pthread_mutexattr_t? = nil
#else
var attrib = pthread_mutexattr_t()
#endif
withUnsafeMutablePointer(to: &attrib) { attrs in
pthread_mutexattr_init(attrs)
#if os(OpenBSD)
#if os(OpenBSD) || os(FreeBSD)
let type = Int32(PTHREAD_MUTEX_RECURSIVE.rawValue)
#else
let type = Int32(PTHREAD_MUTEX_RECURSIVE)
Expand Down
3 changes: 2 additions & 1 deletion Sources/Foundation/NSPlatform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

#if os(macOS) || os(iOS)
fileprivate let _NSPageSize = Int(vm_page_size)
#elseif os(Linux) || os(Android) || os(OpenBSD)
#elseif os(Linux) || os(Android) || os(OpenBSD) || os(FreeBSD)
#if canImport(Android)
import Android
#endif

fileprivate let _NSPageSize = Int(getpagesize())
#elseif os(Windows)
import WinSDK
Expand Down
4 changes: 2 additions & 2 deletions Sources/Foundation/Port.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fileprivate let FOUNDATION_SOCK_STREAM = SOCK_STREAM
fileprivate let FOUNDATION_IPPROTO_TCP = IPPROTO_TCP
#endif

#if canImport(Glibc) && !os(OpenBSD)
#if canImport(Glibc) && !os(OpenBSD) && !os(FreeBSD)
import Glibc
fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM.rawValue)
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
Expand All @@ -119,7 +119,7 @@ fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM)
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
#endif

#if canImport(Glibc) && os(OpenBSD)
#if canImport(Glibc) && (os(OpenBSD) || os(FreeBSD))
import Glibc
fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM)
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Foundation/Process.swift
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ open class Process: NSObject, @unchecked Sendable {
}

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

#if canImport(Darwin) || os(Android) || os(OpenBSD)
#if canImport(Darwin) || os(Android) || os(OpenBSD) || os(FreeBSD)
var spawnAttrs: posix_spawnattr_t? = nil
#else
var spawnAttrs: posix_spawnattr_t = posix_spawnattr_t()
Expand Down
13 changes: 10 additions & 3 deletions Sources/Foundation/Thread.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ open class Thread : NSObject {
get { _attrStorage.value }
set { _attrStorage.value = newValue }
}
#elseif CYGWIN || os(OpenBSD)
#elseif CYGWIN || os(OpenBSD) || os(FreeBSD)
internal var _attr : pthread_attr_t? = nil
#else
internal var _attr = pthread_attr_t()
Expand Down Expand Up @@ -266,7 +266,7 @@ open class Thread : NSObject {
_status = .finished
return
}
#if CYGWIN || os(OpenBSD)
#if CYGWIN || os(OpenBSD) || os(FreeBSD)
if let attr = self._attr {
_thread = self.withRetainedReference {
return _CFThreadCreate(attr, NSThreadStart, $0)
Expand Down Expand Up @@ -390,6 +390,8 @@ open class Thread : NSObject {
#elseif os(Windows)
let count = RtlCaptureStackBackTrace(0, DWORD(maxSupportedStackDepth),
addrs, nil)
#elseif os(FreeBSD)
let count = backtrace(addrs, maxSupportedStackDepth)
#else
let count = backtrace(addrs, Int32(maxSupportedStackDepth))
#endif
Expand Down Expand Up @@ -451,7 +453,12 @@ open class Thread : NSObject {
#else
return backtraceAddresses({ (addrs, count) in
var symbols: [String] = []
if let bs = backtrace_symbols(addrs, Int32(count)) {
#if os(FreeBSD)
let bs = backtrace_symbols(addrs, count)
#else
let bs = backtrace_symbols(addrs, Int32(count))
#endif
if let bs {
symbols = UnsafeBufferPointer(start: bs, count: count).map {
guard let symbol = $0 else {
return "<null>"
Expand Down