@@ -460,6 +460,17 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
460
460
461
461
set(sdk_dir " ${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}/usr/lib/swift ")
462
462
463
+ # HostCompatibilityLibs is defined as an interface library that
464
+ # does not generate any concrete build target
465
+ # (https://cmake.org/cmake/help/latest/command/add_library.html#interface-libraries)
466
+ # In order to specify a dependency to it using `add_dependencies`
467
+ # we need to manually " expand " its underlying targets
468
+ get_property(compatibility_libs
469
+ TARGET HostCompatibilityLibs
470
+ PROPERTY INTERFACE_LINK_LIBRARIES)
471
+ set(compatibility_libs_path
472
+ " ${SWIFTLIB_DIR} /${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}/${SWIFT_HOST_VARIANT_ARCH} ")
473
+
463
474
# If we found a swift compiler and are going to use swift code in swift
464
475
# host side tools but link with clang, add the appropriate -L paths so we
465
476
# find all of the necessary swift libraries on Darwin.
@@ -498,8 +509,11 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
498
509
target_link_directories(${target} PRIVATE " ${sdk_dir} ")
499
510
500
511
# A backup in case the toolchain doesn't have one of the compatibility libraries.
501
- target_link_directories(${target} PRIVATE
502
- " ${SWIFTLIB_DIR} /${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR} ")
512
+ # We are using on purpose `add_dependencies` instead of `target_link_libraries`,
513
+ # since we want to ensure the linker is pulling the matching archives
514
+ # only if needed
515
+ target_link_directories(${target} PRIVATE " ${compatibility_libs_path} ")
516
+ add_dependencies(${target} ${compatibility_libs} )
503
517
504
518
# Include the abi stable system stdlib in our rpath.
505
519
set(swift_runtime_rpath " /usr/lib/swift ")
@@ -511,8 +525,11 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
511
525
target_link_directories(${target} PRIVATE ${bs_lib_dir} )
512
526
513
527
# Required to pick up the built libswiftCompatibility<n>.a libraries
514
- target_link_directories(${target} PRIVATE
515
- " ${SWIFTLIB_DIR} /${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR} ")
528
+ # We are using on purpose `add_dependencies` instead of `target_link_libraries`,
529
+ # since we want to ensure the linker is pulling the matching archives
530
+ # only if needed
531
+ target_link_directories(${target} PRIVATE " ${compatibility_libs_path} ")
532
+ add_dependencies(${target} ${compatibility_libs} )
516
533
517
534
# At runtime link against the built swift libraries from the current
518
535
# bootstrapping stage.
0 commit comments