Skip to content

Combine the test discovery implementations for WASI and static Mach-O. #761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ extension Array where Element == PackageDescription.CXXSetting {
static var packageSettings: Self {
var result = Self()

result += [
.define("SWT_NO_EXIT_TESTS", .when(platforms: [.iOS, .watchOS, .tvOS, .visionOS, .wasi, .android])),
.define("SWT_NO_SNAPSHOT_TYPES", .when(platforms: [.linux, .windows, .wasi])),
.define("SWT_NO_DYNAMIC_LINKING", .when(platforms: [.wasi])),
.define("SWT_NO_PIPES", .when(platforms: [.wasi])),
]

// Capture the testing library's version as a C++ string constant.
if let git = Context.gitInformation {
let testingLibraryVersion = if let tag = git.currentTag {
Expand Down
76 changes: 29 additions & 47 deletions Sources/_TestingInternals/Discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ struct SWTTypeMetadataRecord {
}
};

#if defined(__APPLE__)
#if !defined(SWT_NO_DYNAMIC_LINKING)
#if defined(__APPLE__)
#pragma mark - Apple implementation

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

#else
#pragma mark - Apple implementation (statically linked)

// This environment does not have a dynamic linker/loader. Therefore, there is
// only one image (this one) with Swift code in it.
// SEE: https://github.com/swiftlang/swift/tree/main/stdlib/public/runtime/ImageInspectionStatic.cpp

extern "C" const char sectionBegin __asm("section$start$__TEXT$__swift5_types");
extern "C" const char sectionEnd __asm("section$end$__TEXT$__swift5_types");

template <typename SectionEnumerator>
static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
SWTSectionBounds<SWTTypeMetadataRecord> sb = {
nullptr,
&sectionBegin,
static_cast<size_t>(std::distance(&sectionBegin, &sectionEnd))
};
bool stop = false;
body(sb, &stop);
}
#endif

#elif defined(_WIN32)
#pragma mark - Windows implementation

Expand Down Expand Up @@ -434,29 +412,6 @@ static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
}
}

#elif defined(__wasi__)
#pragma mark - WASI implementation (statically linked)

extern "C" const char __start_swift5_type_metadata;
extern "C" const char __stop_swift5_type_metadata;

template <typename SectionEnumerator>
static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
// WASI only has a single image (so far) and it is statically linked, so all
// Swift metadata ends up in the same section bounded by the named symbols
// above. So we can just yield the section betwixt them.
const auto& sectionBegin = __start_swift5_type_metadata;
const auto& sectionEnd = __stop_swift5_type_metadata;

SWTSectionBounds<SWTTypeMetadataRecord> sb = {
nullptr,
&sectionBegin,
static_cast<size_t>(std::distance(&sectionBegin, &sectionEnd))
};
bool stop = false;
body(sb, &stop);
}

#elif defined(__linux__) || defined(__FreeBSD__) || defined(__ANDROID__)
#pragma mark - ELF implementation

Expand Down Expand Up @@ -517,11 +472,38 @@ static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
}, const_cast<SectionEnumerator *>(&body));
}
#else
#warning Platform-specific implementation missing: Runtime test discovery unavailable
#warning Platform-specific implementation missing: Runtime test discovery unavailable (dynamic)
template <typename SectionEnumerator>
static void enumerateTypeMetadataSections(const SectionEnumerator& body) {}
#endif

#else
#pragma mark - Statically-linked implementation

#if defined(__APPLE__)
extern "C" const char sectionBegin __asm__("section$start$__TEXT$__swift5_types");
extern "C" const char sectionEnd __asm__("section$end$__TEXT$__swift5_types");
#elif defined(__wasi__)
extern "C" const char sectionBegin __asm__("__start_swift5_type_metadata");
extern "C" const char sectionEnd __asm__("__stop_swift5_type_metadata");
#else
#warning Platform-specific implementation missing: Runtime test discovery unavailable (static)
static const char sectionBegin = 0;
static const char& sectionEnd = sectionBegin;
#endif

template <typename SectionEnumerator>
static void enumerateTypeMetadataSections(const SectionEnumerator& body) {
SWTSectionBounds<SWTTypeMetadataRecord> sb = {
nullptr,
&sectionBegin,
static_cast<size_t>(std::distance(&sectionBegin, &sectionEnd))
};
bool stop = false;
body(sb, &stop);
}
#endif

#pragma mark -

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