Skip to content

Commit e5cb71d

Browse files
committed
Fix missing weak linking flags
1 parent 4cb3362 commit e5cb71d

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

swift/toolchains/xcode_swift_toolchain.bzl

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,6 @@ _DEVELOPER_DIR_SYMLINKS = [
117117
"/var/db/xcode_select_link",
118118
]
119119

120-
def _swift_developer_lib_dir(platform_framework_dir):
121-
"""Returns the directory containing extra Swift developer libraries.
122-
123-
Args:
124-
platform_framework_dir: The developer platform framework directory for
125-
the current platform.
126-
127-
Returns:
128-
The directory containing extra Swift-specific development libraries and
129-
swiftmodules.
130-
"""
131-
return paths.join(
132-
paths.dirname(paths.dirname(platform_framework_dir)),
133-
"usr",
134-
"lib",
135-
)
136-
137120
def _platform_developer_framework_dir(developer_dir, target_triple):
138121
"""Returns the Developer framework directory for the platform.
139122
@@ -294,11 +277,16 @@ def _swift_linkopts_cc_info(
294277
# Add the linker path to the directory containing the dylib with Swift
295278
# extensions for the XCTest module.
296279
if platform_developer_framework_dir:
297-
linkopts.extend([
298-
"-L{}".format(
299-
_swift_developer_lib_dir(platform_developer_framework_dir),
280+
swift_lib_dir_path = swift_developer_lib_dir([
281+
struct(
282+
developer_path_label = "platform",
283+
path = platform_developer_framework_dir,
300284
),
301285
])
286+
if swift_lib_dir_path:
287+
linkopts.extend([
288+
"-L{}".format(swift_lib_dir_path),
289+
])
302290

303291
return CcInfo(
304292
linking_context = cc_common.create_linking_context(
@@ -311,23 +299,34 @@ def _swift_linkopts_cc_info(
311299
),
312300
)
313301

314-
def _test_linking_context(target_triple, toolchain_label):
302+
def _test_linking_context(target_triple, toolchain_label, xcode_config):
315303
"""Returns a `CcLinkingContext` containing linker flags for test binaries.
316304
317305
Args:
318306
target_triple: The target triple `struct`.
319307
toolchain_label: The label of the Swift toolchain that will act as the
320308
owner of the linker input propagating the flags.
309+
xcode_config: The Xcode configuration.
321310
322311
Returns:
323312
A `CcLinkingContext` that will provide linker flags to `swift_test`
324313
binaries.
325314
"""
326315

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+
327327
# We use these as the rpaths for linking tests so that the required
328328
# libraries are found if Xcode is installed in a different location on the
329329
# machine that runs the tests than the machine used to link them.
330-
linkopts = []
331330
for developer_dir in _DEVELOPER_DIR_SYMLINKS:
332331
platform_developer_framework_dir = _platform_developer_framework_dir(
333332
developer_dir,
@@ -735,6 +734,7 @@ def _xcode_swift_toolchain_impl(ctx):
735734
test_linking_context = _test_linking_context(
736735
target_triple = target_triple,
737736
toolchain_label = ctx.label,
737+
xcode_config = xcode_config,
738738
)
739739

740740
# `--define=SWIFT_USE_TOOLCHAIN_ROOT=<path>` is a rapid development feature

0 commit comments

Comments
 (0)