From ed3d9cd4ff750d8ccbdaa9e9465bec7aaed0154f Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Thu, 2 Oct 2025 12:01:25 -0400 Subject: [PATCH] Normalize the order in which we specify our Linux/FreeBSD/OpenBSD checks. 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. --- .../Event.HumanReadableOutputRecorder.swift | 2 +- Sources/Testing/ExitTests/SpawnProcess.swift | 2 +- Sources/Testing/ExitTests/WaitFor.swift | 4 ---- Sources/Testing/Support/Locked+Platform.swift | 5 +++-- Sources/Testing/Support/Versions.swift | 2 +- Tests/TestingTests/ABIEntryPointTests.swift | 13 +++++++------ 6 files changed, 13 insertions(+), 15 deletions(-) diff --git a/Sources/Testing/Events/Recorder/Event.HumanReadableOutputRecorder.swift b/Sources/Testing/Events/Recorder/Event.HumanReadableOutputRecorder.swift index 32d1ce770..9f0ac21b1 100644 --- a/Sources/Testing/Events/Recorder/Event.HumanReadableOutputRecorder.swift +++ b/Sources/Testing/Events/Recorder/Event.HumanReadableOutputRecorder.swift @@ -361,7 +361,7 @@ extension Event.HumanReadableOutputRecorder { comments.append("Swift Standard Library Version: \(swiftStandardLibraryVersion)") } comments.append("Swift Compiler Version: \(swiftCompilerVersion)") -#if canImport(Glibc) && !os(FreeBSD) && !os(OpenBSD) +#if os(Linux) && canImport(Glibc) comments.append("GNU C Library Version: \(glibcVersion)") #endif } diff --git a/Sources/Testing/ExitTests/SpawnProcess.swift b/Sources/Testing/ExitTests/SpawnProcess.swift index d1f600e2d..d82d7b663 100644 --- a/Sources/Testing/ExitTests/SpawnProcess.swift +++ b/Sources/Testing/ExitTests/SpawnProcess.swift @@ -137,7 +137,7 @@ func spawnExecutable( // standardized in POSIX.1-2024 (see https://pubs.opengroup.org/onlinepubs/9799919799/functions/posix_spawn_file_actions_adddup2.html // and https://www.austingroupbugs.net/view.php?id=411). _ = posix_spawn_file_actions_adddup2(fileActions, fd, fd) -#if canImport(Glibc) && !os(FreeBSD) && !os(OpenBSD) +#if os(Linux) && canImport(Glibc) if _slowPath(glibcVersion < VersionNumber(2, 29)) { // This system is using an older version of glibc that does not // implement FD_CLOEXEC clearing in posix_spawn_file_actions_adddup2(), diff --git a/Sources/Testing/ExitTests/WaitFor.swift b/Sources/Testing/ExitTests/WaitFor.swift index 4569dddc9..8c6ad52f3 100644 --- a/Sources/Testing/ExitTests/WaitFor.swift +++ b/Sources/Testing/ExitTests/WaitFor.swift @@ -85,11 +85,7 @@ private let _childProcessContinuations = LockedWith.allocate(capacity: 1) -#else let result = UnsafeMutablePointer.allocate(capacity: 1) -#endif _ = pthread_cond_init(result, nil) return result }() diff --git a/Sources/Testing/Support/Locked+Platform.swift b/Sources/Testing/Support/Locked+Platform.swift index 9e6f9db26..a2ba82ac2 100644 --- a/Sources/Testing/Support/Locked+Platform.swift +++ b/Sources/Testing/Support/Locked+Platform.swift @@ -39,9 +39,10 @@ extension os_unfair_lock_s: Lockable { #if os(FreeBSD) || os(OpenBSD) typealias pthread_mutex_t = _TestingInternals.pthread_mutex_t? +typealias pthread_cond_t = _TestingInternals.pthread_cond_t? #endif -#if SWT_TARGET_OS_APPLE || os(Linux) || os(Android) || (os(WASI) && _runtime(_multithreaded)) || os(FreeBSD) || os(OpenBSD) +#if SWT_TARGET_OS_APPLE || os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android) || (os(WASI) && _runtime(_multithreaded)) extension pthread_mutex_t: Lockable { static func initializeLock(at lock: UnsafeMutablePointer) { _ = pthread_mutex_init(lock, nil) @@ -83,7 +84,7 @@ extension SRWLOCK: Lockable { #if SWT_TARGET_OS_APPLE && !SWT_NO_OS_UNFAIR_LOCK typealias DefaultLock = os_unfair_lock -#elseif SWT_TARGET_OS_APPLE || os(Linux) || os(Android) || (os(WASI) && _runtime(_multithreaded)) || os(FreeBSD) || os(OpenBSD) +#elseif SWT_TARGET_OS_APPLE || os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android) || (os(WASI) && _runtime(_multithreaded)) typealias DefaultLock = pthread_mutex_t #elseif os(Windows) typealias DefaultLock = SRWLOCK diff --git a/Sources/Testing/Support/Versions.swift b/Sources/Testing/Support/Versions.swift index 76c5aeaf6..b671b302b 100644 --- a/Sources/Testing/Support/Versions.swift +++ b/Sources/Testing/Support/Versions.swift @@ -198,7 +198,7 @@ var swiftCompilerVersion: VersionNumber { ) } -#if canImport(Glibc) && !os(FreeBSD) && !os(OpenBSD) +#if os(Linux) && canImport(Glibc) /// The (runtime, not compile-time) version of glibc in use on this system. /// /// This value is not part of the public interface of the testing library. diff --git a/Tests/TestingTests/ABIEntryPointTests.swift b/Tests/TestingTests/ABIEntryPointTests.swift index 76af1b83e..15b9cc879 100644 --- a/Tests/TestingTests/ABIEntryPointTests.swift +++ b/Tests/TestingTests/ABIEntryPointTests.swift @@ -67,12 +67,12 @@ struct ABIEntryPointTests { passing arguments: __CommandLineArguments_v0, recordHandler: @escaping @Sendable (_ recordJSON: UnsafeRawBufferPointer) -> Void = { _ in } ) async throws -> Bool { -#if !os(Linux) && !os(FreeBSD) && !os(Android) && !SWT_NO_DYNAMIC_LINKING +#if !(os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android)) && !SWT_NO_DYNAMIC_LINKING // Get the ABI entry point by dynamically looking it up at runtime. // - // NOTE: The standard Linux linker does not allow exporting symbols from - // executables, so dlsym() does not let us find this function on that - // platform when built as an executable rather than a dynamic library. + // NOTE: The standard linkers on these platforms do not export symbols from + // executables, so dlsym() does not let us find this function on these + // platforms when built as an executable rather than a dynamic library. let abiv0_getEntryPoint = try withTestingLibraryImageAddress { testingLibrary in try #require( symbol(in: testingLibrary, named: "swt_abiv0_getEntryPoint").map { @@ -187,8 +187,9 @@ private func withTestingLibraryImageAddress(_ body: (ImageAddress?) throws -> defer { dlclose(testingLibraryAddress) } -#elseif os(Linux) || os(FreeBSD) || os(Android) - // When using glibc, dladdr() is only available if __USE_GNU is specified. +#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android) + // We can't dynamically look up a function linked into the test executable on + // ELF-based platforms. #elseif os(Windows) let flags = DWORD(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS) try addressInTestingLibrary.withMemoryRebound(to: wchar_t.self, capacity: MemoryLayout.stride / MemoryLayout.stride) { addressInTestingLibrary in