Skip to content

Add support for FreeBSD and OpenBSD#387

Merged
FranzBusch merged 3 commits into
apple:mainfrom
kkebo:freebsd
Jan 19, 2026
Merged

Add support for FreeBSD and OpenBSD#387
FranzBusch merged 3 commits into
apple:mainfrom
kkebo:freebsd

Conversation

@kkebo

@kkebo kkebo commented Nov 6, 2025

Copy link
Copy Markdown
Contributor

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 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.

$ 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:

$ 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.

@3405691582

Copy link
Copy Markdown

OpenBSD post 7.8 also needs the following changes, but otherwise swift-log compiles with this patch. Do you mind integrating this and rebasing?

diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift
index a93b9a1..b3d897a 100644
--- a/Sources/Logging/Logging.swift
+++ b/Sources/Logging/Logging.swift
@@ -1312,7 +1312,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>
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

@kkebo

kkebo commented Dec 13, 2025

Copy link
Copy Markdown
Contributor Author

Yes, sure.

@kkebo

kkebo commented Dec 13, 2025

Copy link
Copy Markdown
Contributor Author

@3405691582 I've added those changes. Thank you for your suggestions!

@nreilly

nreilly commented Jan 9, 2026

Copy link
Copy Markdown

I've been using this successfully via

var dependencies: [Package.Dependency] = [
    .package(url: "https://github.com/apple/swift-log", branch: "pull/387/head"),
]

@ktoso ktoso left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we don't have CI for this platform seems folks used this successfully... we can always keep updating if it happened to be slightly off

@ktoso

ktoso commented Jan 15, 2026

Copy link
Copy Markdown
Member

Sorry this is taking a while to integrate -- would you mind rebasing? I don't have a freebsd at hand to verify I'll get the changes right

@kkebo

kkebo commented Jan 15, 2026

Copy link
Copy Markdown
Contributor Author

@ktoso I've resolved the conflict with a92ccc6. Also, I found that a92ccc6 was broken on OpenBSD because it was using uninitialized attr. I fixed that too.

let err = pthread_mutex_init(self.mutex, &attr)

Finally, I verified that my changes were appropriate using swift test.

$ freebsd-version
15.0-RELEASE-p1
$ uname -a
FreeBSD Coconut-rhinoceros-beetle 15.0-RELEASE-p1 FreeBSD 15.0-RELEASE-p1 releng/15.0-n280999-7bceec30b351 GENERIC amd64
$ swift --version
Swift version 6.3-dev (LLVM b58b2a34d509492, Swift cf535d8b998d09b)
Target: x86_64-unknown-freebsd14.3
Build config: +assertions
$ swift test
[1/1] Planning build                                                                                                                                                                   
Building for debugging...                                                                                                                                                              
[41/41] Linking swift-logPackageTests.xctest                                                                                                                                           
Build complete! (6.41s)                                                                                                                                                                
Test Suite 'All tests' started at 2026-01-15 22:13:08.972                                                                                                                              
Test Suite 'debug.xctest' started at 2026-01-15 22:13:09.007                                                                                                                           
Test Suite 'debug.xctest' passed at 2026-01-15 22:13:09.007                                                                                                                            
         Executed 0 tests, with 0 failures (0 unexpected) in 0.0 (0.0) seconds                                                                                                         
Test Suite 'All tests' passed at 2026-01-15 22:13:09.007                                                                                                                               
         Executed 0 tests, with 0 failures (0 unexpected) in 0.0 (0.0) seconds                                                                                                         
◇ Test run started.                                                                                                                                                                    
↳ Testing Library Version: 6.3-dev (98430f2d6cb8d81)                                                                                                                                   
↳ Target Platform: x86_64-unknown-freebsd                                                                                                                                              
◇ Suite GlobalLoggerTest started.                                                                                                                                                      
◇ Suite MDCTest started.                                                                                                                                                               
◇ Suite SendableTest started.                                                                                                                                                          
◇ Suite MetadataProviderTest started.                                                                                                                                                  
◇ Suite LocalLoggerTest started.                                                                                                                                                       
◇ Suite CompatibilityTest started.                                                                                                                                                     
◇ Suite InMemoryLogHandlerTests started.                                                                                                                                               
◇ Suite LoggingTest started.
...
✔ Suite GlobalLoggerTest passed after 0.307 seconds.
✔ Test run with 57 tests in 8 suites passed after 0.307 seconds.

@kkebo kkebo changed the title Add support for FreeBSD Add support for FreeBSD and OpenBSD Jan 15, 2026
@FranzBusch
FranzBusch enabled auto-merge (squash) January 19, 2026 16:04
@kkebo

kkebo commented Jan 19, 2026

Copy link
Copy Markdown
Contributor Author

The PR label / Semantic version label check job requires a semver/* label, but I don't have the permission to apply it. Could someone apply it? This PR doesn't change any public APIs, so I guess that the semver/patch label is enough.

@FranzBusch FranzBusch added the 🔨 semver/patch No public API change. label Jan 19, 2026
@FranzBusch
FranzBusch merged commit 8f634e4 into apple:main Jan 19, 2026
68 of 69 checks passed
@kkebo
kkebo deleted the freebsd branch January 19, 2026 16:43
Lukasa added a commit to apple/swift-nio that referenced this pull request Mar 18, 2026
This PR adds support for FreeBSD to the `CNIOSHA1` target and the
`NIOConcurrencyHelpers` target.

### Motivation:

This PR is partial because the `NIOCore` target still can't be built,
and the existing PR #3398 includes almost the same changes too.

However, #3398 is a little bit old. Besides, the source code in
`NIOConcurrencyHelpers` is used in other repositories now, for example:

- apple/swift-log#398
- apple/swift-log#387 was merged before
swift-nio is fixed.

So I think it's important to fix `NIOConcurrencyHelpers` first, even if
the entire swift-nio is yet to be fixed.

### Modifications:

- Make `swift build --target NIOConcurrencyHelpers` work on FreeBSD
- Make `swift build --target CNIOSHA1` work on FreeBSD

### Result:

The following commands finished successfully.

```shell
swift build --target NIOConcurrencyHelpers
swift build --target CNIOSHA1
```

Environment:

- FreeBSD 15.0-RELEASE-p1 x86_64
- [Swift on FreeBSD Preview
toolchain](https://forums.swift.org/t/swift-on-freebsd-preview/83064)
  ```
  Swift version 6.3-dev (LLVM b58b2a34d509492, Swift cf535d8b998d09b)
  Target: x86_64-unknown-freebsd14.3
  Build config: +assertions
  ```

---------

Co-authored-by: Cory Benfield <[email protected]>
max-potapov added a commit to max-potapov/swift-prometheus that referenced this pull request May 22, 2026
### Motivation:

NIOLock's pthread fallback branch types LockPrimitive as
pthread_mutex_t and initializes pthread_mutexattr_t with a default
initializer; both compile on Linux but fail on FreeBSD/OpenBSD where
those types are opaque-pointer typedefs imported as
Optional<OpaquePointer>. Additionally, PTHREAD_MUTEX_ERRORCHECK is
imported as a pthread_mutextype enum case on FreeBSD whose .rawValue
must be used to construct the CInt that pthread_mutexattr_settype
expects.

### Modifications:

Mirror the pattern landed in apple/swift-log#387:
- alias LockPrimitive to pthread_mutex_t? on FreeBSD/OpenBSD
- initialize pthread_mutexattr_t via the bitPattern overload
- on FreeBSD/OpenBSD, access PTHREAD_MUTEX_ERRORCHECK via .rawValue
  before passing to pthread_mutexattr_settype

### Result:

swift-prometheus compiles on FreeBSD with the official Swift FreeBSD
preview toolchain.
FranzBusch pushed a commit to swift-server/swift-service-lifecycle that referenced this pull request Jul 14, 2026
### Motivation

`ConcurrencyHelpers/Lock` has a pthread fallback branch (taken on every
non-Windows / non-Darwin platform) that types `LockPrimitive` as
`pthread_mutex_t` and initializes `pthread_mutexattr_t` with a default
initializer. Both compile on Linux but fail on FreeBSD / OpenBSD, where
those types are opaque-pointer typedefs Swift imports as
`Optional<OpaquePointer>` — the no-argument initializers don't exist.

This blocks swift-service-lifecycle (and everything that depends on
it — Hummingbird, downstream HTTP servers) from compiling on FreeBSD
with the official Swift FreeBSD preview toolchain.

### Modifications

Mirror the pattern landed in [apple/swift-log#387][log-pr]; same change
is being submitted for the sibling NIOLock copies (e.g.
[apple/swift-http-types#123][sht-pr]):

- On `os(FreeBSD) || os(OpenBSD)` alias `LockPrimitive` to
  `pthread_mutex_t?`.
- In `LockOperations.create(_:)`, wrap `pthread_mutexattr_t()` with
  `#if os(FreeBSD) || os(OpenBSD)` to use the `bitPattern:` overload.

[log-pr]: apple/swift-log#387
[sht-pr]: apple/swift-http-types#123

Touches one file (`Sources/ConcurrencyHelpers/Lock.swift`), +7 lines,
all behind FreeBSD/OpenBSD conditionals. No behaviour change on
existing platforms.

### Result

`swift build` and `swift test` both clean on FreeBSD 15.0 with the
official Swift FreeBSD preview toolchain:

```
$ uname -a
FreeBSD swiftbuild 15.0-RELEASE FreeBSD 15.0-RELEASE-p9 releng/15.0-n281048-6d536196f1bd GENERIC amd64

$ swift --version
Swift version 6.3-dev (LLVM 972b62858355d07, Swift 3f8c798cfe25bbb)
Target: x86_64-unknown-freebsd14.3
Build config: +assertions

$ swift build
Building for debugging...
... 276 modules ...
Build complete! (42.62s)

$ swift test
... ClosureServiceTests passed ...
✔ Test run passed.
```

Without this change `ServiceLifecycle` fails to compile on the same
toolchain.

### Checks

- [x] Builds on Linux/Darwin (no change there, all changes guarded
      behind `#if os(FreeBSD) || os(OpenBSD)`).
- [x] Builds + passes existing tests on FreeBSD 15.0 with Swift
      6.3-dev preview toolchain.
- [x] Same pattern as [apple/swift-log#387][log-pr] /
      [apple/swift-http-types#123][sht-pr].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔨 semver/patch No public API change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BSD support

5 participants