Skip to content

Commit f276d25

Browse files
authored
Normalize the order in which we specify our Linux/FreeBSD/OpenBSD checks. (#1349)
This PR adjusts some of our `#if os(...)` checks to always specify Linux, FreeBSD, OpenBSD, and Android in a consistent order. This makes it easier to find such code in the repo. I've also found a couple of spots where OpenBSD was missing and have fixed them. ### 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 c996b0a commit f276d25

File tree

6 files changed

+13
-15
lines changed

6 files changed

+13
-15
lines changed

Sources/Testing/Events/Recorder/Event.HumanReadableOutputRecorder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ extension Event.HumanReadableOutputRecorder {
361361
comments.append("Swift Standard Library Version: \(swiftStandardLibraryVersion)")
362362
}
363363
comments.append("Swift Compiler Version: \(swiftCompilerVersion)")
364-
#if canImport(Glibc) && !os(FreeBSD) && !os(OpenBSD)
364+
#if os(Linux) && canImport(Glibc)
365365
comments.append("GNU C Library Version: \(glibcVersion)")
366366
#endif
367367
}

Sources/Testing/ExitTests/SpawnProcess.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func spawnExecutable(
137137
// standardized in POSIX.1-2024 (see https://pubs.opengroup.org/onlinepubs/9799919799/functions/posix_spawn_file_actions_adddup2.html
138138
// and https://www.austingroupbugs.net/view.php?id=411).
139139
_ = posix_spawn_file_actions_adddup2(fileActions, fd, fd)
140-
#if canImport(Glibc) && !os(FreeBSD) && !os(OpenBSD)
140+
#if os(Linux) && canImport(Glibc)
141141
if _slowPath(glibcVersion < VersionNumber(2, 29)) {
142142
// This system is using an older version of glibc that does not
143143
// implement FD_CLOEXEC clearing in posix_spawn_file_actions_adddup2(),

Sources/Testing/ExitTests/WaitFor.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,7 @@ private let _childProcessContinuations = LockedWith<pthread_mutex_t, [pid_t: Che
8585
/// A condition variable used to suspend the waiter thread created by
8686
/// `_createWaitThread()` when there are no child processes to await.
8787
private nonisolated(unsafe) let _waitThreadNoChildrenCondition = {
88-
#if os(FreeBSD) || os(OpenBSD)
89-
let result = UnsafeMutablePointer<pthread_cond_t?>.allocate(capacity: 1)
90-
#else
9188
let result = UnsafeMutablePointer<pthread_cond_t>.allocate(capacity: 1)
92-
#endif
9389
_ = pthread_cond_init(result, nil)
9490
return result
9591
}()

Sources/Testing/Support/Locked+Platform.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ extension os_unfair_lock_s: Lockable {
3939

4040
#if os(FreeBSD) || os(OpenBSD)
4141
typealias pthread_mutex_t = _TestingInternals.pthread_mutex_t?
42+
typealias pthread_cond_t = _TestingInternals.pthread_cond_t?
4243
#endif
4344

44-
#if SWT_TARGET_OS_APPLE || os(Linux) || os(Android) || (os(WASI) && _runtime(_multithreaded)) || os(FreeBSD) || os(OpenBSD)
45+
#if SWT_TARGET_OS_APPLE || os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android) || (os(WASI) && _runtime(_multithreaded))
4546
extension pthread_mutex_t: Lockable {
4647
static func initializeLock(at lock: UnsafeMutablePointer<Self>) {
4748
_ = pthread_mutex_init(lock, nil)
@@ -83,7 +84,7 @@ extension SRWLOCK: Lockable {
8384

8485
#if SWT_TARGET_OS_APPLE && !SWT_NO_OS_UNFAIR_LOCK
8586
typealias DefaultLock = os_unfair_lock
86-
#elseif SWT_TARGET_OS_APPLE || os(Linux) || os(Android) || (os(WASI) && _runtime(_multithreaded)) || os(FreeBSD) || os(OpenBSD)
87+
#elseif SWT_TARGET_OS_APPLE || os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android) || (os(WASI) && _runtime(_multithreaded))
8788
typealias DefaultLock = pthread_mutex_t
8889
#elseif os(Windows)
8990
typealias DefaultLock = SRWLOCK

Sources/Testing/Support/Versions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ var swiftCompilerVersion: VersionNumber {
198198
)
199199
}
200200

201-
#if canImport(Glibc) && !os(FreeBSD) && !os(OpenBSD)
201+
#if os(Linux) && canImport(Glibc)
202202
/// The (runtime, not compile-time) version of glibc in use on this system.
203203
///
204204
/// This value is not part of the public interface of the testing library.

Tests/TestingTests/ABIEntryPointTests.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ struct ABIEntryPointTests {
6767
passing arguments: __CommandLineArguments_v0,
6868
recordHandler: @escaping @Sendable (_ recordJSON: UnsafeRawBufferPointer) -> Void = { _ in }
6969
) async throws -> Bool {
70-
#if !os(Linux) && !os(FreeBSD) && !os(Android) && !SWT_NO_DYNAMIC_LINKING
70+
#if !(os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android)) && !SWT_NO_DYNAMIC_LINKING
7171
// Get the ABI entry point by dynamically looking it up at runtime.
7272
//
73-
// NOTE: The standard Linux linker does not allow exporting symbols from
74-
// executables, so dlsym() does not let us find this function on that
75-
// platform when built as an executable rather than a dynamic library.
73+
// NOTE: The standard linkers on these platforms do not export symbols from
74+
// executables, so dlsym() does not let us find this function on these
75+
// platforms when built as an executable rather than a dynamic library.
7676
let abiv0_getEntryPoint = try withTestingLibraryImageAddress { testingLibrary in
7777
try #require(
7878
symbol(in: testingLibrary, named: "swt_abiv0_getEntryPoint").map {
@@ -187,8 +187,9 @@ private func withTestingLibraryImageAddress<R>(_ body: (ImageAddress?) throws ->
187187
defer {
188188
dlclose(testingLibraryAddress)
189189
}
190-
#elseif os(Linux) || os(FreeBSD) || os(Android)
191-
// When using glibc, dladdr() is only available if __USE_GNU is specified.
190+
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android)
191+
// We can't dynamically look up a function linked into the test executable on
192+
// ELF-based platforms.
192193
#elseif os(Windows)
193194
let flags = DWORD(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS)
194195
try addressInTestingLibrary.withMemoryRebound(to: wchar_t.self, capacity: MemoryLayout<UnsafeRawPointer>.stride / MemoryLayout<wchar_t>.stride) { addressInTestingLibrary in

0 commit comments

Comments
 (0)