|
13 | 13 | #include <array>
|
14 | 14 | #include <algorithm>
|
15 | 15 | #include <iterator>
|
| 16 | +#include <mutex> |
16 | 17 |
|
17 | 18 | const char *swt_getTestingLibraryVersion(void) {
|
18 | 19 | #if defined(SWT_TESTING_LIBRARY_VERSION)
|
19 | 20 | // The current environment explicitly specifies a version string to return.
|
| 21 | + // All CMake builds should take this path (see CompilerSettings.cmake.) |
20 | 22 | return SWT_TESTING_LIBRARY_VERSION;
|
21 |
| -#elif __has_embed("../../VERSION.txt") |
22 |
| - static constinit auto version = [] () constexpr { |
23 |
| - // Read the version from version.txt at the root of the package's repo. |
24 |
| - char version[] = { |
| 23 | +#elif __clang_major__ >= 17 && defined(__has_embed) |
| 24 | +#if __has_embed("../../VERSION.txt") |
| 25 | + // Read the version from version.txt at the root of the package's repo. |
| 26 | + static char version[] = { |
25 | 27 | #pragma clang diagnostic push
|
26 | 28 | #pragma clang diagnostic ignored "-Wc23-extensions"
|
27 |
| -#embed "../../VERSION.txt" |
| 29 | +#embed "../../VERSION.txt" suffix(, '\0') |
28 | 30 | #pragma clang diagnostic pop
|
29 |
| - }; |
| 31 | + }; |
30 | 32 |
|
31 |
| - // Copy the first line from the C string into a C array so that we can |
32 |
| - // return it from this closure. |
33 |
| - std::array<char, std::size(version) + 1> result {}; |
| 33 | + // Zero out the newline character and anything after it. |
| 34 | + static std::once_flag once; |
| 35 | + std::call_once(once, [] { |
34 | 36 | auto i = std::find_if(std::begin(version), std::end(version), [] (char c) {
|
35 | 37 | return c == '\r' || c == '\n';
|
36 | 38 | });
|
37 |
| - std::copy(std::begin(version), i, result.begin()); |
38 |
| - return result; |
39 |
| - }(); |
| 39 | + std::fill(i, std::end(version), '\0'); |
| 40 | + }); |
40 | 41 |
|
41 |
| - return version.data(); |
| 42 | + return version; |
42 | 43 | #else
|
43 | 44 | #warning SWT_TESTING_LIBRARY_VERSION not defined and VERSION.txt not found: testing library version is unavailable
|
44 | 45 | return nullptr;
|
45 | 46 | #endif
|
| 47 | +#elif defined(__OpenBSD__) |
| 48 | + // OpenBSD's version of clang doesn't support __has_embed or #embed. |
| 49 | + return nullptr; |
| 50 | +#else |
| 51 | +#warning SWT_TESTING_LIBRARY_VERSION not defined and could not read from VERSION.txt at compile time: testing library version is unavailable |
| 52 | + return nullptr; |
| 53 | +#endif |
46 | 54 | }
|
47 | 55 |
|
48 | 56 | void swt_getTestingLibraryCommit(const char *_Nullable *_Nonnull outHash, bool *outModified) {
|
|
0 commit comments