Skip to content

Commit c811d17

Browse files
authored
Combine the test discovery implementations for WASI and static Mach-O. (#761)
This PR reduces code duplication between the two supported platforms that do not use dynamic linking during test discovery. ### 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.
1 parent d9f8d2f commit c811d17

File tree

2 files changed

+36
-47
lines changed

2 files changed

+36
-47
lines changed

Diff for: Package.swift

+7
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@ extension Array where Element == PackageDescription.CXXSetting {
162162
static var packageSettings: Self {
163163
var result = Self()
164164

165+
result += [
166+
.define("SWT_NO_EXIT_TESTS", .when(platforms: [.iOS, .watchOS, .tvOS, .visionOS, .wasi, .android])),
167+
.define("SWT_NO_SNAPSHOT_TYPES", .when(platforms: [.linux, .windows, .wasi])),
168+
.define("SWT_NO_DYNAMIC_LINKING", .when(platforms: [.wasi])),
169+
.define("SWT_NO_PIPES", .when(platforms: [.wasi])),
170+
]
171+
165172
// Capture the testing library's version as a C++ string constant.
166173
if let git = Context.gitInformation {
167174
let testingLibraryVersion = if let tag = git.currentTag {

Diff for: Sources/_TestingInternals/Discovery.cpp

+29-47
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ struct SWTTypeMetadataRecord {
239239
}
240240
};
241241

242-
#if defined(__APPLE__)
243242
#if !defined(SWT_NO_DYNAMIC_LINKING)
243+
#if defined(__APPLE__)
244244
#pragma mark - Apple implementation
245245

246246
/// Get a copy of the currently-loaded type metadata sections list.
@@ -318,28 +318,6 @@ static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
318318
}
319319
}
320320

321-
#else
322-
#pragma mark - Apple implementation (statically linked)
323-
324-
// This environment does not have a dynamic linker/loader. Therefore, there is
325-
// only one image (this one) with Swift code in it.
326-
// SEE: https://github.com/swiftlang/swift/tree/main/stdlib/public/runtime/ImageInspectionStatic.cpp
327-
328-
extern "C" const char sectionBegin __asm("section$start$__TEXT$__swift5_types");
329-
extern "C" const char sectionEnd __asm("section$end$__TEXT$__swift5_types");
330-
331-
template <typename SectionEnumerator>
332-
static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
333-
SWTSectionBounds<SWTTypeMetadataRecord> sb = {
334-
nullptr,
335-
&sectionBegin,
336-
static_cast<size_t>(std::distance(&sectionBegin, &sectionEnd))
337-
};
338-
bool stop = false;
339-
body(sb, &stop);
340-
}
341-
#endif
342-
343321
#elif defined(_WIN32)
344322
#pragma mark - Windows implementation
345323

@@ -434,29 +412,6 @@ static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
434412
}
435413
}
436414

437-
#elif defined(__wasi__)
438-
#pragma mark - WASI implementation (statically linked)
439-
440-
extern "C" const char __start_swift5_type_metadata;
441-
extern "C" const char __stop_swift5_type_metadata;
442-
443-
template <typename SectionEnumerator>
444-
static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
445-
// WASI only has a single image (so far) and it is statically linked, so all
446-
// Swift metadata ends up in the same section bounded by the named symbols
447-
// above. So we can just yield the section betwixt them.
448-
const auto& sectionBegin = __start_swift5_type_metadata;
449-
const auto& sectionEnd = __stop_swift5_type_metadata;
450-
451-
SWTSectionBounds<SWTTypeMetadataRecord> sb = {
452-
nullptr,
453-
&sectionBegin,
454-
static_cast<size_t>(std::distance(&sectionBegin, &sectionEnd))
455-
};
456-
bool stop = false;
457-
body(sb, &stop);
458-
}
459-
460415
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__ANDROID__)
461416
#pragma mark - ELF implementation
462417

@@ -517,11 +472,38 @@ static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
517472
}, const_cast<SectionEnumerator *>(&body));
518473
}
519474
#else
520-
#warning Platform-specific implementation missing: Runtime test discovery unavailable
475+
#warning Platform-specific implementation missing: Runtime test discovery unavailable (dynamic)
521476
template <typename SectionEnumerator>
522477
static void enumerateTypeMetadataSections(const SectionEnumerator& body) {}
523478
#endif
524479

480+
#else
481+
#pragma mark - Statically-linked implementation
482+
483+
#if defined(__APPLE__)
484+
extern "C" const char sectionBegin __asm__("section$start$__TEXT$__swift5_types");
485+
extern "C" const char sectionEnd __asm__("section$end$__TEXT$__swift5_types");
486+
#elif defined(__wasi__)
487+
extern "C" const char sectionBegin __asm__("__start_swift5_type_metadata");
488+
extern "C" const char sectionEnd __asm__("__stop_swift5_type_metadata");
489+
#else
490+
#warning Platform-specific implementation missing: Runtime test discovery unavailable (static)
491+
static const char sectionBegin = 0;
492+
static const char& sectionEnd = sectionBegin;
493+
#endif
494+
495+
template <typename SectionEnumerator>
496+
static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
497+
SWTSectionBounds<SWTTypeMetadataRecord> sb = {
498+
nullptr,
499+
&sectionBegin,
500+
static_cast<size_t>(std::distance(&sectionBegin, &sectionEnd))
501+
};
502+
bool stop = false;
503+
body(sb, &stop);
504+
}
505+
#endif
506+
525507
#pragma mark -
526508

527509
void swt_enumerateTypesWithNamesContaining(const char *nameSubstring, void *context, SWTTypeEnumerator body) {

0 commit comments

Comments
 (0)