@@ -299,23 +299,34 @@ def _swift_linkopts_cc_info(
299299 ),
300300 )
301301
302- def _test_linking_context (target_triple , toolchain_label ):
302+ def _test_linking_context (target_triple , toolchain_label , xcode_config ):
303303 """Returns a `CcLinkingContext` containing linker flags for test binaries.
304304
305305 Args:
306306 target_triple: The target triple `struct`.
307307 toolchain_label: The label of the Swift toolchain that will act as the
308308 owner of the linker input propagating the flags.
309+ xcode_config: The Xcode configuration.
309310
310311 Returns:
311312 A `CcLinkingContext` that will provide linker flags to `swift_test`
312313 binaries.
313314 """
314315
316+ # Weakly link to XCTest. It's possible that machine that links the test
317+ # binary will have Xcode installed at a different path than the machine that
318+ # runs the binary. To handle this, the binary `dlopen`s XCTest at startup
319+ # using the path Bazel passes in the test action's environment.
320+ linkopts = [
321+ "-Wl,-weak_framework,XCTest" ,
322+ "-Wl,-weak-lXCTestSwiftSupport" ,
323+ ]
324+ if _is_xcode_at_least_version (xcode_config , "16.0" ):
325+ linkopts .append ("-Wl,-weak_framework,Testing" )
326+
315327 # We use these as the rpaths for linking tests so that the required
316328 # libraries are found if Xcode is installed in a different location on the
317329 # machine that runs the tests than the machine used to link them.
318- linkopts = []
319330 for developer_dir in _DEVELOPER_DIR_SYMLINKS :
320331 platform_developer_framework_dir = _platform_developer_framework_dir (
321332 developer_dir ,
@@ -723,6 +734,7 @@ def _xcode_swift_toolchain_impl(ctx):
723734 test_linking_context = _test_linking_context (
724735 target_triple = target_triple ,
725736 toolchain_label = ctx .label ,
737+ xcode_config = xcode_config ,
726738 )
727739
728740 # `--define=SWIFT_USE_TOOLCHAIN_ROOT=<path>` is a rapid development feature
0 commit comments