Skip to content

CMake: ensure Swift host tools depend on HostCompatibilityLibs target #73065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,17 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)

set(sdk_dir "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}/usr/lib/swift")

# HostCompatibilityLibs is defined as an interface library that
# does not generate any concrete build target
# (https://cmake.org/cmake/help/latest/command/add_library.html#interface-libraries)
# In order to specify a dependency to it using `add_dependencies`
# we need to manually "expand" its underlying targets
get_property(compatibility_libs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did add_dependencies(${target} HostCompatibilityLibs) not work?

Copy link
Contributor Author

@edymtt edymtt Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case no, because HostCompatibilityLibs is defined as an interface library, so its dependencies would only be expanded if I used target_link_libraries -- this is the same approach I perused in #60728

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment about this in 4d14497 (as this is an aspect of interface libraries that is easy to forget)

TARGET HostCompatibilityLibs
PROPERTY INTERFACE_LINK_LIBRARIES)
set(compatibility_libs_path
"${SWIFTLIB_DIR}/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}/${SWIFT_HOST_VARIANT_ARCH}")

# If we found a swift compiler and are going to use swift code in swift
# host side tools but link with clang, add the appropriate -L paths so we
# find all of the necessary swift libraries on Darwin.
Expand Down Expand Up @@ -505,8 +516,11 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
target_link_directories(${target} PRIVATE "${sdk_dir}")

# A backup in case the toolchain doesn't have one of the compatibility libraries.
target_link_directories(${target} PRIVATE
"${SWIFTLIB_DIR}/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
# We are using on purpose `add_dependencies` instead of `target_link_libraries`,
# since we want to ensure the linker is pulling the matching archives
# only if needed
target_link_directories(${target} PRIVATE "${compatibility_libs_path}")
add_dependencies(${target} ${compatibility_libs})

# Include the abi stable system stdlib in our rpath.
set(swift_runtime_rpath "/usr/lib/swift")
Expand All @@ -518,8 +532,11 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
target_link_directories(${target} PRIVATE ${bs_lib_dir})

# Required to pick up the built libswiftCompatibility<n>.a libraries
target_link_directories(${target} PRIVATE
"${SWIFTLIB_DIR}/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
# We are using on purpose `add_dependencies` instead of `target_link_libraries`,
# since we want to ensure the linker is pulling the matching archives
# only if needed
target_link_directories(${target} PRIVATE "${compatibility_libs_path}")
add_dependencies(${target} ${compatibility_libs})

# At runtime link against the built swift libraries from the current
# bootstrapping stage.
Expand Down