Skip to content

Commit 591dc82

Browse files
authored
Fix more things on FreeBSD (#867)
This PR contains a few more fixes for FreeBSD: Use threadsafe `strerror_r` on FreeBSD Define "SWT_NO_SNAPSHOT_TYPES" when host is FreeBSD Work around issue swiftlang/swift#62985 ### Result: Passed all the tests on FreeBSD ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 014d874 commit 591dc82

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

Package.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ let package = Package(
4545
],
4646
exclude: ["CMakeLists.txt"],
4747
cxxSettings: .packageSettings,
48-
swiftSettings: .packageSettings
48+
swiftSettings: .packageSettings,
49+
linkerSettings: [
50+
.linkedLibrary("execinfo", .when(platforms: [.custom("freebsd")]))
51+
]
4952
),
5053
.testTarget(
5154
name: "TestingTests",
@@ -114,7 +117,7 @@ let package = Package(
114117
)
115118

116119
// BUG: swift-package-manager-#6367
117-
#if !os(Windows)
120+
#if !os(Windows) && !os(FreeBSD)
118121
package.targets.append(contentsOf: [
119122
.testTarget(
120123
name: "TestingMacrosTests",
@@ -143,7 +146,7 @@ extension Array where Element == PackageDescription.SwiftSetting {
143146

144147
.define("SWT_NO_EXIT_TESTS", .when(platforms: [.iOS, .watchOS, .tvOS, .visionOS, .wasi, .android])),
145148
.define("SWT_NO_PROCESS_SPAWNING", .when(platforms: [.iOS, .watchOS, .tvOS, .visionOS, .wasi, .android])),
146-
.define("SWT_NO_SNAPSHOT_TYPES", .when(platforms: [.linux, .windows, .wasi])),
149+
.define("SWT_NO_SNAPSHOT_TYPES", .when(platforms: [.linux, .custom("freebsd"), .windows, .wasi])),
147150
.define("SWT_NO_DYNAMIC_LINKING", .when(platforms: [.wasi])),
148151
.define("SWT_NO_PIPES", .when(platforms: [.wasi])),
149152
]
@@ -178,7 +181,7 @@ extension Array where Element == PackageDescription.CXXSetting {
178181
result += [
179182
.define("SWT_NO_EXIT_TESTS", .when(platforms: [.iOS, .watchOS, .tvOS, .visionOS, .wasi, .android])),
180183
.define("SWT_NO_PROCESS_SPAWNING", .when(platforms: [.iOS, .watchOS, .tvOS, .visionOS, .wasi, .android])),
181-
.define("SWT_NO_SNAPSHOT_TYPES", .when(platforms: [.linux, .windows, .wasi])),
184+
.define("SWT_NO_SNAPSHOT_TYPES", .when(platforms: [.linux, .custom("freebsd"), .windows, .wasi])),
182185
.define("SWT_NO_DYNAMIC_LINKING", .when(platforms: [.wasi])),
183186
.define("SWT_NO_PIPES", .when(platforms: [.wasi])),
184187
]

Sources/Testing/SourceAttribution/Backtrace.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ extension Backtrace {
181181
/// crash. To avoid said crash, we'll keep a strong reference to the
182182
/// object (abandoning memory until the process exits.)
183183
/// ([swift-#62985](https://github.com/swiftlang/swift/issues/62985))
184-
#if os(Windows)
184+
#if os(Windows) || os(FreeBSD)
185185
nonisolated(unsafe) var errorObject: AnyObject?
186186
#else
187187
nonisolated(unsafe) weak var errorObject: AnyObject?

Sources/Testing/Support/CError.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ func strerror(_ errorCode: CInt) -> String {
4747
_ = strerror_s(buffer.baseAddress!, buffer.count, errorCode)
4848
return strnlen(buffer.baseAddress!, buffer.count)
4949
}
50+
#elseif os(FreeBSD)
51+
// FreeBSD's implementation of strerror() is not thread-safe.
52+
String(unsafeUninitializedCapacity: 1024) { buffer in
53+
_ = strerror_r(errorCode, buffer.baseAddress!, buffer.count)
54+
return strnlen(buffer.baseAddress!, buffer.count)
55+
}
5056
#else
5157
String(cString: _TestingInternals.strerror(errorCode))
5258
#endif

0 commit comments

Comments
 (0)