From 9d58a1b6a8f22a0d6e714c80180728e638ce9ab6 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Thu, 10 Oct 2024 15:21:49 -0400 Subject: [PATCH 1/2] Combine the test discovery implementations for WASI and static Mach-O. This PR reduces code duplication between the two supported platforms that do not use dynamic linking during test discovery. --- Package.swift | 7 +++ Sources/_TestingInternals/Discovery.cpp | 74 +++++++++---------------- 2 files changed, 34 insertions(+), 47 deletions(-) diff --git a/Package.swift b/Package.swift index 32ea88bc3..d6b3b6f87 100644 --- a/Package.swift +++ b/Package.swift @@ -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 { diff --git a/Sources/_TestingInternals/Discovery.cpp b/Sources/_TestingInternals/Discovery.cpp index dc9e882ae..4a6488cd8 100644 --- a/Sources/_TestingInternals/Discovery.cpp +++ b/Sources/_TestingInternals/Discovery.cpp @@ -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. @@ -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 -static void enumerateTypeMetadataSections(const SectionEnumerator& body) { - SWTSectionBounds sb = { - nullptr, - §ionBegin, - static_cast(std::distance(§ionBegin, §ionEnd)) - }; - bool stop = false; - body(sb, &stop); -} -#endif - #elif defined(_WIN32) #pragma mark - Windows implementation @@ -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 -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 sb = { - nullptr, - §ionBegin, - static_cast(std::distance(§ionBegin, §ionEnd)) - }; - bool stop = false; - body(sb, &stop); -} - #elif defined(__linux__) || defined(__FreeBSD__) || defined(__ANDROID__) #pragma mark - ELF implementation @@ -517,11 +472,36 @@ static void enumerateTypeMetadataSections(const SectionEnumerator& body) { }, const_cast(&body)); } #else -#warning Platform-specific implementation missing: Runtime test discovery unavailable +#warning Platform-specific implementation missing: Runtime test discovery unavailable (dynamic) template 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) +#endif + +template +static void enumerateTypeMetadataSections(const SectionEnumerator& body) { + SWTSectionBounds sb = { + nullptr, + §ionBegin, + static_cast(std::distance(§ionBegin, §ionEnd)) + }; + bool stop = false; + body(sb, &stop); +} +#endif + #pragma mark - void swt_enumerateTypesWithNamesContaining(const char *nameSubstring, void *context, SWTTypeEnumerator body) { From a46b58e2e945090286071f1ce4136c705c8b12dd Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Thu, 10 Oct 2024 15:35:21 -0400 Subject: [PATCH 2/2] Fallback definitions of sectionBegin/sectionEnd for future porting --- Sources/_TestingInternals/Discovery.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/_TestingInternals/Discovery.cpp b/Sources/_TestingInternals/Discovery.cpp index 4a6488cd8..eac49c45d 100644 --- a/Sources/_TestingInternals/Discovery.cpp +++ b/Sources/_TestingInternals/Discovery.cpp @@ -488,6 +488,8 @@ 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