From edde58370053160d38732fca2fc740e5c7557076 Mon Sep 17 00:00:00 2001 From: 3405691582 Date: Tue, 16 Sep 2025 19:24:51 -0400 Subject: [PATCH 01/10] Work around lack of embed in OpenBSD's clang. For various reasons, the Swift toolchain for OpenBSD relies on using the platform's native clang, which is 16. clang 19 is the most recent version that will not emit an error with the new __has_embed features in C23. Since swift-testing is experimentally supported by OpenBSD and thus to make swift-testing build again on the platform, work around the issue with a platform-specific command-line specified macro override in swiftpm and in cmake. Furthermore, we can use cmake trickery to subsitute the version file contents instead of using embed. This may not be possible to do with swiftpm, but I don't know for sure. --- Package.swift | 3 +++ cmake/modules/shared/CompilerSettings.cmake | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/Package.swift b/Package.swift index e387f1453..444c304b0 100644 --- a/Package.swift +++ b/Package.swift @@ -466,6 +466,9 @@ extension Array where Element == PackageDescription.CXXSetting { .define("SWT_NO_LEGACY_TEST_DISCOVERY", .whenEmbedded()), .define("SWT_NO_LIBDISPATCH", .whenEmbedded()), + + // OpenBSD's version of clang doesn't support __has_embed. + .define("SWT_TESTING_LIBRARY_VERSION", to: "0", .when(platforms: [.openbsd])) ] // Capture the testing library's commit info as C++ constants. diff --git a/cmake/modules/shared/CompilerSettings.cmake b/cmake/modules/shared/CompilerSettings.cmake index ae9ee8fce..99b7a0206 100644 --- a/cmake/modules/shared/CompilerSettings.cmake +++ b/cmake/modules/shared/CompilerSettings.cmake @@ -42,3 +42,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL "WASI") add_compile_definitions("SWT_NO_DYNAMIC_LINKING") add_compile_definitions("SWT_NO_PIPES") endif() +if(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD") + file(STRINGS "../VERSION.txt" SWT_TESTING_LIBRARY_VERSION) + add_compile_definitions("$<$:SWT_TESTING_LIBRARY_VERSION=\"${SWT_TESTING_LIBRARY_VERSION}\">") +endif() From 5f0572b5983e6909e5f65eb5c187d3156eea2e39 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Wed, 17 Sep 2025 09:21:23 -0400 Subject: [PATCH 02/10] Update CompilerSettings.cmake Always define SWT_TESTING_LIBRARY_VERSION in CMake --- cmake/modules/shared/CompilerSettings.cmake | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cmake/modules/shared/CompilerSettings.cmake b/cmake/modules/shared/CompilerSettings.cmake index 99b7a0206..b1257d1df 100644 --- a/cmake/modules/shared/CompilerSettings.cmake +++ b/cmake/modules/shared/CompilerSettings.cmake @@ -42,7 +42,8 @@ if(CMAKE_SYSTEM_NAME STREQUAL "WASI") add_compile_definitions("SWT_NO_DYNAMIC_LINKING") add_compile_definitions("SWT_NO_PIPES") endif() -if(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD") - file(STRINGS "../VERSION.txt" SWT_TESTING_LIBRARY_VERSION) - add_compile_definitions("$<$:SWT_TESTING_LIBRARY_VERSION=\"${SWT_TESTING_LIBRARY_VERSION}\">") -endif() + +# Avoid using C23's #embed when building with CMake as OpenBSD and Amazon Linux +# 2 are both using older clang versions that don't support it. +file(STRINGS "../VERSION.txt" SWT_TESTING_LIBRARY_VERSION LIMIT_COUNT 1) +add_compile_definitions("$<$:SWT_TESTING_LIBRARY_VERSION=\"${SWT_TESTING_LIBRARY_VERSION}\">") From ddcaa64c0d6b3c72e3b97fb99fcbc74c1f75f97f Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Wed, 17 Sep 2025 09:26:27 -0400 Subject: [PATCH 03/10] Update Versions.cpp Check the major clang version before attempting to use `__has_embed` or `__clang_major__`. --- Sources/_TestingInternals/Versions.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sources/_TestingInternals/Versions.cpp b/Sources/_TestingInternals/Versions.cpp index bd8f2314a..565cab608 100644 --- a/Sources/_TestingInternals/Versions.cpp +++ b/Sources/_TestingInternals/Versions.cpp @@ -18,7 +18,8 @@ const char *swt_getTestingLibraryVersion(void) { #if defined(SWT_TESTING_LIBRARY_VERSION) // The current environment explicitly specifies a version string to return. return SWT_TESTING_LIBRARY_VERSION; -#elif __has_embed("../../VERSION.txt") +#elif __clang_major__ >= 19 +#if __has_embed("../../VERSION.txt") static constinit auto version = [] () constexpr { // Read the version from version.txt at the root of the package's repo. char version[] = { @@ -43,6 +44,10 @@ const char *swt_getTestingLibraryVersion(void) { #warning SWT_TESTING_LIBRARY_VERSION not defined and VERSION.txt not found: testing library version is unavailable return nullptr; #endif +#else +#warning SWT_TESTING_LIBRARY_VERSION not defined and could not read from VERSION.txt at compile time: testing library version is unavailable + return nullptr; +#endif } void swt_getTestingLibraryCommit(const char *_Nullable *_Nonnull outHash, bool *outModified) { From 3deaa5bd609f2397a218132056e4b44a3b971746 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Wed, 17 Sep 2025 09:48:31 -0400 Subject: [PATCH 04/10] Update Versions.cpp clang in CI jobs is 17.x but can handle `__has_embed`. --- Sources/_TestingInternals/Versions.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/_TestingInternals/Versions.cpp b/Sources/_TestingInternals/Versions.cpp index 565cab608..5adb567fe 100644 --- a/Sources/_TestingInternals/Versions.cpp +++ b/Sources/_TestingInternals/Versions.cpp @@ -18,7 +18,7 @@ const char *swt_getTestingLibraryVersion(void) { #if defined(SWT_TESTING_LIBRARY_VERSION) // The current environment explicitly specifies a version string to return. return SWT_TESTING_LIBRARY_VERSION; -#elif __clang_major__ >= 19 +#elif __clang_major__ >= 17 && defined(__has_embed) #if __has_embed("../../VERSION.txt") static constinit auto version = [] () constexpr { // Read the version from version.txt at the root of the package's repo. From 442d5c80d12b910ab59d0ff0fe14a0a35929c7a6 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Wed, 17 Sep 2025 11:36:03 -0400 Subject: [PATCH 05/10] Avoid modifying the string in a constinit expression (in case that's the cause of the failure rather than #embed itself) --- Sources/_TestingInternals/Versions.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Sources/_TestingInternals/Versions.cpp b/Sources/_TestingInternals/Versions.cpp index 5adb567fe..74b71dd77 100644 --- a/Sources/_TestingInternals/Versions.cpp +++ b/Sources/_TestingInternals/Versions.cpp @@ -13,6 +13,7 @@ #include #include #include +#include const char *swt_getTestingLibraryVersion(void) { #if defined(SWT_TESTING_LIBRARY_VERSION) @@ -20,26 +21,24 @@ const char *swt_getTestingLibraryVersion(void) { return SWT_TESTING_LIBRARY_VERSION; #elif __clang_major__ >= 17 && defined(__has_embed) #if __has_embed("../../VERSION.txt") - static constinit auto version = [] () constexpr { - // Read the version from version.txt at the root of the package's repo. - char version[] = { + // Read the version from version.txt at the root of the package's repo. + static char version[] = { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wc23-extensions" #embed "../../VERSION.txt" #pragma clang diagnostic pop - }; + }; - // Copy the first line from the C string into a C array so that we can - // return it from this closure. - std::array result {}; + // Zero out the newline character and anything after it. + static std::once_flag once; + std::call_once(once, [] { auto i = std::find_if(std::begin(version), std::end(version), [] (char c) { return c == '\r' || c == '\n'; }); - std::copy(std::begin(version), i, result.begin()); - return result; - }(); + std::fill(i, std::end(version), '\0'); + }); - return version.data(); + return version; #else #warning SWT_TESTING_LIBRARY_VERSION not defined and VERSION.txt not found: testing library version is unavailable return nullptr; From 7ca953aa89b1aabfa2adf5fc3bfa68c0142b7b80 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Wed, 17 Sep 2025 12:06:23 -0400 Subject: [PATCH 06/10] Improve failure signal in exit tests so I can maybe see why they're failing on AL2 --- Sources/Testing/ExitTests/ExitTest.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Testing/ExitTests/ExitTest.swift b/Sources/Testing/ExitTests/ExitTest.swift index 32e930e0d..b9ce39496 100644 --- a/Sources/Testing/ExitTests/ExitTest.swift +++ b/Sources/Testing/ExitTests/ExitTest.swift @@ -525,11 +525,11 @@ func callExitTest( } // Plumb the exit test's result through the general expectation machinery. + let expression = __Expression(String(describingForTest: expectedExitCondition)) return __checkValue( expectedExitCondition.isApproximatelyEqual(to: result.exitStatus), expression: expression, expressionWithCapturedRuntimeValues: expression.capturingRuntimeValues(result.exitStatus), - mismatchedExitConditionDescription: String(describingForTest: expectedExitCondition), comments: comments(), isRequired: isRequired, sourceLocation: sourceLocation From 97dc2802eec50642af8c6eed69163b9ad6a643de Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Wed, 17 Sep 2025 12:22:50 -0400 Subject: [PATCH 07/10] Revert "Improve failure signal in exit tests so I can maybe see why they're failing on AL2" This reverts commit 7ca953aa89b1aabfa2adf5fc3bfa68c0142b7b80. --- Sources/Testing/ExitTests/ExitTest.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Testing/ExitTests/ExitTest.swift b/Sources/Testing/ExitTests/ExitTest.swift index b9ce39496..32e930e0d 100644 --- a/Sources/Testing/ExitTests/ExitTest.swift +++ b/Sources/Testing/ExitTests/ExitTest.swift @@ -525,11 +525,11 @@ func callExitTest( } // Plumb the exit test's result through the general expectation machinery. - let expression = __Expression(String(describingForTest: expectedExitCondition)) return __checkValue( expectedExitCondition.isApproximatelyEqual(to: result.exitStatus), expression: expression, expressionWithCapturedRuntimeValues: expression.capturingRuntimeValues(result.exitStatus), + mismatchedExitConditionDescription: String(describingForTest: expectedExitCondition), comments: comments(), isRequired: isRequired, sourceLocation: sourceLocation From 72c4c37852c141487dd06bffe9508bb978571ed8 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Wed, 17 Sep 2025 13:23:53 -0400 Subject: [PATCH 08/10] Special-case OpenBSD in Versions.cpp instead of in Package.swift in case it gets a clang update in the future --- Package.swift | 3 --- Sources/_TestingInternals/Versions.cpp | 4 ++++ cmake/modules/shared/CompilerSettings.cmake | 2 -- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Package.swift b/Package.swift index 444c304b0..e387f1453 100644 --- a/Package.swift +++ b/Package.swift @@ -466,9 +466,6 @@ extension Array where Element == PackageDescription.CXXSetting { .define("SWT_NO_LEGACY_TEST_DISCOVERY", .whenEmbedded()), .define("SWT_NO_LIBDISPATCH", .whenEmbedded()), - - // OpenBSD's version of clang doesn't support __has_embed. - .define("SWT_TESTING_LIBRARY_VERSION", to: "0", .when(platforms: [.openbsd])) ] // Capture the testing library's commit info as C++ constants. diff --git a/Sources/_TestingInternals/Versions.cpp b/Sources/_TestingInternals/Versions.cpp index 74b71dd77..364c59a62 100644 --- a/Sources/_TestingInternals/Versions.cpp +++ b/Sources/_TestingInternals/Versions.cpp @@ -18,6 +18,7 @@ const char *swt_getTestingLibraryVersion(void) { #if defined(SWT_TESTING_LIBRARY_VERSION) // The current environment explicitly specifies a version string to return. + // All CMake builds should take this path (see CompilerSettings.cmake.) return SWT_TESTING_LIBRARY_VERSION; #elif __clang_major__ >= 17 && defined(__has_embed) #if __has_embed("../../VERSION.txt") @@ -43,6 +44,9 @@ const char *swt_getTestingLibraryVersion(void) { #warning SWT_TESTING_LIBRARY_VERSION not defined and VERSION.txt not found: testing library version is unavailable return nullptr; #endif +#elif defined(__OpenBSD__) + // OpenBSD's version of clang doesn't support __has_embed or #embed. + return nullptr; #else #warning SWT_TESTING_LIBRARY_VERSION not defined and could not read from VERSION.txt at compile time: testing library version is unavailable return nullptr; diff --git a/cmake/modules/shared/CompilerSettings.cmake b/cmake/modules/shared/CompilerSettings.cmake index b1257d1df..c92d1fdad 100644 --- a/cmake/modules/shared/CompilerSettings.cmake +++ b/cmake/modules/shared/CompilerSettings.cmake @@ -43,7 +43,5 @@ if(CMAKE_SYSTEM_NAME STREQUAL "WASI") add_compile_definitions("SWT_NO_PIPES") endif() -# Avoid using C23's #embed when building with CMake as OpenBSD and Amazon Linux -# 2 are both using older clang versions that don't support it. file(STRINGS "../VERSION.txt" SWT_TESTING_LIBRARY_VERSION LIMIT_COUNT 1) add_compile_definitions("$<$:SWT_TESTING_LIBRARY_VERSION=\"${SWT_TESTING_LIBRARY_VERSION}\">") From 62a35ea51e01215791eaffce29b6fcc816ce4724 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Wed, 17 Sep 2025 13:26:05 -0400 Subject: [PATCH 09/10] Log detected version when building with CMake --- cmake/modules/shared/CompilerSettings.cmake | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cmake/modules/shared/CompilerSettings.cmake b/cmake/modules/shared/CompilerSettings.cmake index c92d1fdad..df3bb826a 100644 --- a/cmake/modules/shared/CompilerSettings.cmake +++ b/cmake/modules/shared/CompilerSettings.cmake @@ -44,4 +44,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL "WASI") endif() file(STRINGS "../VERSION.txt" SWT_TESTING_LIBRARY_VERSION LIMIT_COUNT 1) -add_compile_definitions("$<$:SWT_TESTING_LIBRARY_VERSION=\"${SWT_TESTING_LIBRARY_VERSION}\">") +if(SWT_TESTING_LIBRARY_VERSION) + message(STATUS "Swift Testing version: ${SWT_TESTING_LIBRARY_VERSION}") + add_compile_definitions("$<$:SWT_TESTING_LIBRARY_VERSION=\"${SWT_TESTING_LIBRARY_VERSION}\">") +endif() From 87fb3fb5399647615758ace34844febdf898931c Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Wed, 17 Sep 2025 13:30:16 -0400 Subject: [PATCH 10/10] Ensure there's a trailing NUL when we use #embed --- Sources/_TestingInternals/Versions.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/_TestingInternals/Versions.cpp b/Sources/_TestingInternals/Versions.cpp index 364c59a62..8ca708b26 100644 --- a/Sources/_TestingInternals/Versions.cpp +++ b/Sources/_TestingInternals/Versions.cpp @@ -26,7 +26,7 @@ const char *swt_getTestingLibraryVersion(void) { static char version[] = { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wc23-extensions" -#embed "../../VERSION.txt" +#embed "../../VERSION.txt" suffix(, '\0') #pragma clang diagnostic pop };