Skip to content

Commit 05b4639

Browse files
3405691582grynspan
andauthored
Work around lack of embed in clang on OpenBSD and Amazon Linux 2. (#1320)
Work around lack of embed in OpenBSD's clang. ### Motivation: 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. Resolves rdar://160591315. ### 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. --------- Co-authored-by: Jonathan Grynspan <[email protected]> Co-authored-by: Jonathan Grynspan <[email protected]>
1 parent 082d6d5 commit 05b4639

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

Sources/_TestingInternals/Versions.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,44 @@
1313
#include <array>
1414
#include <algorithm>
1515
#include <iterator>
16+
#include <mutex>
1617

1718
const char *swt_getTestingLibraryVersion(void) {
1819
#if defined(SWT_TESTING_LIBRARY_VERSION)
1920
// The current environment explicitly specifies a version string to return.
21+
// All CMake builds should take this path (see CompilerSettings.cmake.)
2022
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[] = {
2527
#pragma clang diagnostic push
2628
#pragma clang diagnostic ignored "-Wc23-extensions"
27-
#embed "../../VERSION.txt"
29+
#embed "../../VERSION.txt" suffix(, '\0')
2830
#pragma clang diagnostic pop
29-
};
31+
};
3032

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, [] {
3436
auto i = std::find_if(std::begin(version), std::end(version), [] (char c) {
3537
return c == '\r' || c == '\n';
3638
});
37-
std::copy(std::begin(version), i, result.begin());
38-
return result;
39-
}();
39+
std::fill(i, std::end(version), '\0');
40+
});
4041

41-
return version.data();
42+
return version;
4243
#else
4344
#warning SWT_TESTING_LIBRARY_VERSION not defined and VERSION.txt not found: testing library version is unavailable
4445
return nullptr;
4546
#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
4654
}
4755

4856
void swt_getTestingLibraryCommit(const char *_Nullable *_Nonnull outHash, bool *outModified) {

cmake/modules/shared/CompilerSettings.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
4242
add_compile_definitions("SWT_NO_DYNAMIC_LINKING")
4343
add_compile_definitions("SWT_NO_PIPES")
4444
endif()
45+
46+
file(STRINGS "../VERSION.txt" SWT_TESTING_LIBRARY_VERSION LIMIT_COUNT 1)
47+
if(SWT_TESTING_LIBRARY_VERSION)
48+
message(STATUS "Swift Testing version: ${SWT_TESTING_LIBRARY_VERSION}")
49+
add_compile_definitions("$<$<COMPILE_LANGUAGE:CXX>:SWT_TESTING_LIBRARY_VERSION=\"${SWT_TESTING_LIBRARY_VERSION}\">")
50+
endif()

0 commit comments

Comments
 (0)