Skip to content

Commit 1395add

Browse files
authored
Update Porting.md to describe how to handle platforms that use static linkage. (#763)
This PR adds [a section](https://github.com/swiftlang/swift-testing/blob/jgrynspan/porting-static-linkage/Documentation/Porting.md#runtime-test-discovery-with-static-linkage) to Porting.md explaining what to do when your platform doesn't use a dynamic linker. ### 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 c811d17 commit 1395add

File tree

1 file changed

+55
-3
lines changed

1 file changed

+55
-3
lines changed

Documentation/Porting.md

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,72 @@ to load that information:
140140
+ UseResFile(refNum);
141141
+ Handle handle = Get1NamedResource('swft', "\p__swift5_types");
142142
+ if (handle && *handle) {
143-
+ size_t size = GetHandleSize(handle);
144-
+ body(*handle, size);
143+
+ auto imageAddress = reinterpret_cast<const void *>(static_cast<uintptr_t>(refNum));
144+
+ SWTSectionBounds sb = { imageAddress, *handle, GetHandleSize(handle) };
145+
+ bool stop = false;
146+
+ body(sb, &stop);
147+
+ if (stop) {
148+
+ break;
149+
+ }
145150
+ }
146151
+ } while (noErr == GetNextResourceFile(refNum, &refNum));
147152
+ UseResFile(oldRefNum);
148153
+ }
149154
+}
150155
#else
151-
#warning Platform-specific implementation missing: Runtime test discovery unavailable
156+
#warning Platform-specific implementation missing: Runtime test discovery unavailable (dynamic)
152157
template <typename SectionEnumerator>
153158
static void enumerateTypeMetadataSections(const SectionEnumerator& body) {}
154159
#endif
155160
```
156161

162+
## Runtime test discovery with static linkage
163+
164+
If your platform does not support dynamic linking and loading, you will need to
165+
use static linkage instead. Define the `"SWT_NO_DYNAMIC_LINKING"` compiler
166+
conditional for your platform in both Package.swift and CompilerSettings.cmake,
167+
then define the `sectionBegin` and `sectionEnd` symbols in Discovery.cpp:
168+
169+
```diff
170+
diff --git a/Sources/_TestingInternals/Discovery.cpp b/Sources/_TestingInternals/Discovery.cpp
171+
// ...
172+
+#elif defined(macintosh)
173+
+extern "C" const char sectionBegin __asm__("...");
174+
+extern "C" const char sectionEnd __asm__("...");
175+
#else
176+
#warning Platform-specific implementation missing: Runtime test discovery unavailable (static)
177+
static const char sectionBegin = 0;
178+
static const char& sectionEnd = sectionBegin;
179+
#endif
180+
```
181+
182+
These symbols must have unique addresses corresponding to the first byte of the
183+
test content section and the first byte _after_ the test content section,
184+
respectively. Their linker-level names will be platform-dependent: refer to the
185+
linker documentation for your platform to determine what names to place in the
186+
`__asm__` attribute applied to each.
187+
188+
If you can't use `__asm__` on your platform, you can declare these symbols as
189+
C++ references to linker-defined symbols:
190+
191+
```diff
192+
diff --git a/Sources/_TestingInternals/Discovery.cpp b/Sources/_TestingInternals/Discovery.cpp
193+
// ...
194+
+#elif defined(macintosh)
195+
+extern "C" const char __linker_defined_begin_symbol;
196+
+extern "C" const char __linker_defined_end_symbol;
197+
+static const auto& sectionBegin = __linker_defined_begin_symbol;
198+
+static const auto& sectionEnd = __linker_defined_end_symbol;
199+
#else
200+
#warning Platform-specific implementation missing: Runtime test discovery unavailable (static)
201+
static const char sectionBegin = 0;
202+
static const char& sectionEnd = sectionBegin;
203+
#endif
204+
```
205+
206+
The names of `__linker_defined_begin_symbol` and `__linker_defined_end_symbol`
207+
in this example are, as with the shorter implementation, platform-dependent.
208+
157209
## C++ stub implementations
158210

159211
Some symbols defined in C and C++ headers, especially "complex" macros, cannot

0 commit comments

Comments
 (0)