@@ -467,6 +467,17 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
467467
468468 set (sdk_dir "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _ARCH_${SWIFT_HOST_VARIANT_ARCH} _PATH}/usr/lib/swift" )
469469
470+ # HostCompatibilityLibs is defined as an interface library that
471+ # does not generate any concrete build target
472+ # (https://cmake.org/cmake/help/latest/command/add_library.html#interface-libraries)
473+ # In order to specify a dependency to it using `add_dependencies`
474+ # we need to manually "expand" its underlying targets
475+ get_property (compatibility_libs
476+ TARGET HostCompatibilityLibs
477+ PROPERTY INTERFACE_LINK_LIBRARIES)
478+ set (compatibility_libs_path
479+ "${SWIFTLIB_DIR} /${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _LIB_SUBDIR}/${SWIFT_HOST_VARIANT_ARCH} " )
480+
470481 # If we found a swift compiler and are going to use swift code in swift
471482 # host side tools but link with clang, add the appropriate -L paths so we
472483 # find all of the necessary swift libraries on Darwin.
@@ -505,8 +516,11 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
505516 target_link_directories (${target} PRIVATE "${sdk_dir} " )
506517
507518 # A backup in case the toolchain doesn't have one of the compatibility libraries.
508- target_link_directories (${target} PRIVATE
509- "${SWIFTLIB_DIR} /${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _LIB_SUBDIR}" )
519+ # We are using on purpose `add_dependencies` instead of `target_link_libraries`,
520+ # since we want to ensure the linker is pulling the matching archives
521+ # only if needed
522+ target_link_directories (${target} PRIVATE "${compatibility_libs_path} " )
523+ add_dependencies (${target} ${compatibility_libs} )
510524
511525 # Include the abi stable system stdlib in our rpath.
512526 set (swift_runtime_rpath "/usr/lib/swift" )
@@ -518,8 +532,11 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
518532 target_link_directories (${target} PRIVATE ${bs_lib_dir} )
519533
520534 # Required to pick up the built libswiftCompatibility<n>.a libraries
521- target_link_directories (${target} PRIVATE
522- "${SWIFTLIB_DIR} /${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK} _LIB_SUBDIR}" )
535+ # We are using on purpose `add_dependencies` instead of `target_link_libraries`,
536+ # since we want to ensure the linker is pulling the matching archives
537+ # only if needed
538+ target_link_directories (${target} PRIVATE "${compatibility_libs_path} " )
539+ add_dependencies (${target} ${compatibility_libs} )
523540
524541 # At runtime link against the built swift libraries from the current
525542 # bootstrapping stage.
0 commit comments