diff --git a/Sources/_TestingInternals/Versions.cpp b/Sources/_TestingInternals/Versions.cpp index bd8f2314a..8ca708b26 100644 --- a/Sources/_TestingInternals/Versions.cpp +++ b/Sources/_TestingInternals/Versions.cpp @@ -13,36 +13,44 @@ #include #include #include +#include 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 __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[] = { +#elif __clang_major__ >= 17 && defined(__has_embed) +#if __has_embed("../../VERSION.txt") + // 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" +#embed "../../VERSION.txt" suffix(, '\0') #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; #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; +#endif } void swt_getTestingLibraryCommit(const char *_Nullable *_Nonnull outHash, bool *outModified) { diff --git a/cmake/modules/shared/CompilerSettings.cmake b/cmake/modules/shared/CompilerSettings.cmake index ae9ee8fce..df3bb826a 100644 --- a/cmake/modules/shared/CompilerSettings.cmake +++ b/cmake/modules/shared/CompilerSettings.cmake @@ -42,3 +42,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "WASI") add_compile_definitions("SWT_NO_DYNAMIC_LINKING") add_compile_definitions("SWT_NO_PIPES") endif() + +file(STRINGS "../VERSION.txt" SWT_TESTING_LIBRARY_VERSION LIMIT_COUNT 1) +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()