Add support for FreeBSD and OpenBSD (#387)

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 <f.busch@apple.com>
This commit is contained in:
Kenta KuboandFranz Busch authored and GitHub committed 2026-01-19 16:37:43 +00:00
1 parent 2778fd4e5a
commit 8f634e4e68
3 files changed
+25 -6

No files matched your search

+12 -5
View File
@@ -58,7 +58,7 @@ package final class Lock {
#if os(Windows)
fileprivate let mutex: UnsafeMutablePointer<SRWLOCK> =
UnsafeMutablePointer.allocate(capacity: 1)
#elseif os(OpenBSD)
#elseif os(FreeBSD) || os(OpenBSD)
fileprivate let mutex: UnsafeMutablePointer<pthread_mutex_t?> =
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<SRWLOCK> =
UnsafeMutablePointer.allocate(capacity: 1)
fileprivate var shared: Bool = true
#elseif os(FreeBSD) || os(OpenBSD)
fileprivate let rwlock: UnsafeMutablePointer<pthread_rwlock_t?> =
UnsafeMutablePointer.allocate(capacity: 1)
#else
fileprivate let rwlock: UnsafeMutablePointer<pthread_rwlock_t> =
UnsafeMutablePointer.allocate(capacity: 1)
+9 -1
View File
@@ -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<FILE>
@@ -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)
+4
View File
@@ -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