Skip to content

Add support for FreeBSD and OpenBSD#387

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

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

Conversation

@kkebo
Copy link
Copy Markdown
Contributor

@kkebo kkebo commented Nov 6, 2025

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
Copy link
Copy Markdown
Contributor Author

kkebo commented Dec 13, 2025

Yes, sure.

@kkebo
Copy link
Copy Markdown
Contributor Author

kkebo commented Dec 13, 2025

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

@nreilly
Copy link
Copy Markdown

nreilly commented Jan 9, 2026

I've been using this successfully via

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

Copy link
Copy Markdown
Member

@ktoso ktoso left a comment

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
Copy link
Copy Markdown
Member

ktoso commented Jan 15, 2026

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
Copy link
Copy Markdown
Contributor Author

kkebo commented Jan 15, 2026

@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
Copy link
Copy Markdown
Contributor Author

kkebo commented Jan 19, 2026

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 <lukasa@apple.com>
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