From 8f634e4e682819a9aa857fca8c88ef899fb64d75 Mon Sep 17 00:00:00 2001 From: Kenta Kubo <601636+kkebo@users.noreply.github.com> Date: Tue, 20 Jan 2026 01:37:43 +0900 Subject: [PATCH] Add support for FreeBSD and OpenBSD (#387) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I've added support for FreeBSD. I've only tested this PR on FreeBSD, but I believe it should also work on OpenBSD. ### Motivation: closes #372 Also, [Swift on FreeBSD preview toolchain](https://forums.swift.org/t/swift-on-freebsd-preview/83064) is now available. ### Modifications: - changed pthread-related code for *BSD - changed `Glibc.stdout` and `Glibc.stderr` for *BSD because they are non-optional on *BSD ### Result: swift-log and packages depending on it will be usable on FreeBSD. ### Checks: I tested it with `swift build` and `swift test`. All tests have passed. ```console $ swift test ... Test Suite 'All tests' passed at 2025-11-07 04:29:34.003 Executed 0 tests, with 0 failures (0 unexpected) in 0.0 (0.0) seconds ... ✔ Test run with 52 tests in 7 suites passed after 0.309 seconds. ``` Environment: ```console $ uname -a FreeBSD Coconut-rhinoceros-beetle 15.0-BETA4 FreeBSD 15.0-BETA4 releng/15.0-n280841-a7707f2a3bf4 GENERIC amd64 $ swift --version Swift version 6.3-dev (LLVM bff1370bd79c983, Swift 57cf4ce563f700b) Target: x86_64-unknown-freebsd14.3 Build config: +assertions ``` Swift toolchain is from https://forums.swift.org/t/swift-on-freebsd-preview/83064. --------- Co-authored-by: Franz Busch --- Sources/Logging/Locks.swift | 17 ++++++++++++----- Sources/Logging/Logging.swift | 10 +++++++++- Tests/LoggingTests/TestLogger.swift | 4 ++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Sources/Logging/Locks.swift b/Sources/Logging/Locks.swift index 1e90d8c..70bf5f3 100644 --- a/Sources/Logging/Locks.swift +++ b/Sources/Logging/Locks.swift @@ -58,7 +58,7 @@ package final class Lock { #if os(Windows) fileprivate let mutex: UnsafeMutablePointer = UnsafeMutablePointer.allocate(capacity: 1) - #elseif os(OpenBSD) + #elseif os(FreeBSD) || os(OpenBSD) fileprivate let mutex: UnsafeMutablePointer = UnsafeMutablePointer.allocate(capacity: 1) #else @@ -70,15 +70,19 @@ package final class Lock { package init() { #if os(Windows) InitializeSRWLock(self.mutex) - #elseif os(OpenBSD) - var attr = pthread_mutexattr_t(bitPattern: 0) - let err = pthread_mutex_init(self.mutex, &attr) - precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") #elseif (compiler(<6.1) && !os(WASI)) || (compiler(>=6.1) && _runtime(_multithreaded)) + #if os(FreeBSD) || os(OpenBSD) + var attr = pthread_mutexattr_t(bitPattern: 0) + #else var attr = pthread_mutexattr_t() + #endif pthread_mutexattr_init(&attr) debugOnly { + #if os(FreeBSD) || os(OpenBSD) + pthread_mutexattr_settype(&attr, .init(PTHREAD_MUTEX_ERRORCHECK.rawValue)) + #else pthread_mutexattr_settype(&attr, .init(PTHREAD_MUTEX_ERRORCHECK)) + #endif } let err = pthread_mutex_init(self.mutex, &attr) @@ -176,6 +180,9 @@ internal final class ReadWriteLock: @unchecked Sendable { fileprivate let rwlock: UnsafeMutablePointer = UnsafeMutablePointer.allocate(capacity: 1) fileprivate var shared: Bool = true + #elseif os(FreeBSD) || os(OpenBSD) + fileprivate let rwlock: UnsafeMutablePointer = + UnsafeMutablePointer.allocate(capacity: 1) #else fileprivate let rwlock: UnsafeMutablePointer = UnsafeMutablePointer.allocate(capacity: 1) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index 66569b4..bcbecce 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -1388,7 +1388,7 @@ public struct MultiplexLogHandler: LogHandler { } } -#if canImport(WASILibc) || os(Android) +#if canImport(WASILibc) || os(Android) || os(OpenBSD) internal typealias CFilePointer = OpaquePointer #else internal typealias CFilePointer = UnsafeMutablePointer @@ -1444,7 +1444,11 @@ internal struct StdioOutputStream: TextOutputStream, @unchecked Sendable { #elseif os(Windows) let systemStderr = CRT.stderr #elseif canImport(Glibc) + #if os(FreeBSD) || os(OpenBSD) + let systemStderr = Glibc.stderr + #else let systemStderr = Glibc.stderr! + #endif #elseif canImport(Android) let systemStderr = Android.stderr #elseif canImport(Musl) @@ -1464,7 +1468,11 @@ internal struct StdioOutputStream: TextOutputStream, @unchecked Sendable { #elseif os(Windows) let systemStdout = CRT.stdout #elseif canImport(Glibc) + #if os(FreeBSD) || os(OpenBSD) + let systemStdout = Glibc.stdout + #else let systemStdout = Glibc.stdout! + #endif #elseif canImport(Android) let systemStdout = Android.stdout #elseif canImport(Musl) diff --git a/Tests/LoggingTests/TestLogger.swift b/Tests/LoggingTests/TestLogger.swift index 7d6258a..fc77249 100644 --- a/Tests/LoggingTests/TestLogger.swift +++ b/Tests/LoggingTests/TestLogger.swift @@ -375,6 +375,10 @@ public class MDC { return Int(pthread_mach_thread_np(pthread_self())) #elseif os(Windows) return Int(GetCurrentThreadId()) + #elseif os(FreeBSD) + return Int(pthread_getthreadid_np()) + #elseif os(OpenBSD) + return Int(bitPattern: pthread_self()) #else return Int(pthread_self()) #endif