Skip to content

Commit c5fe4f0

Browse files
committed
[cmake] Add the SDK directory after the toolchain directory when compiling host swift code.
This ensures that we pick up libraries from the toolchain before picking up libraries from the SDK we are using. Normally it would not matter the order that we add these -L files since the contents of the SDK and toolchain are disjoint. Once we begin performing stage2 swift builds though, we no longer have this property since we pass in the stage1's installed libraries as the SDK directory and we have not split the two sets of libraries yet. The end result is that if we have the -L in the previous order, we will pick up just built compatibility libraries before we pick up the actual compatibility libraries from the actual toolchain we are using to compile. This results in compilation breakage.
1 parent 558a0cc commit c5fe4f0

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -600,13 +600,22 @@ function(add_swift_host_library name)
600600
# find all of the necessary swift libraries on Darwin.
601601
if (NOT ASHL_PURE_SWIFT)
602602
if (CMAKE_Swift_COMPILER)
603-
# Add in the SDK directory for the host platform and add an rpath.
604-
target_link_directories(${name} PRIVATE
605-
${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}/usr/lib/swift)
606603
# Add in the toolchain directory so we can grab compatibility libraries
607604
get_filename_component(TOOLCHAIN_BIN_DIR ${CMAKE_Swift_COMPILER} DIRECTORY)
608605
get_filename_component(TOOLCHAIN_LIB_DIR "${TOOLCHAIN_BIN_DIR}/../lib/swift/macosx" ABSOLUTE)
609606
target_link_directories(${name} PUBLIC ${TOOLCHAIN_LIB_DIR})
607+
608+
# Add in the SDK directory for the host platform.
609+
#
610+
# NOTE: We do this /after/ target_link_directorying TOOLCHAIN_LIB_DIR to
611+
# ensure that we first find libraries from the toolchain, rather than
612+
# from the SDK. The reason why this is important is that when we perform
613+
# a stage2 build, this path is into the stage1 build. This is not a pure
614+
# SDK and also contains compatibility libraries. We need to make sure
615+
# that the compiler sees the actual toolchain's compatibility libraries
616+
# first before the just built compability libraries or build errors occur.
617+
target_link_directories(${name} PRIVATE
618+
${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}/usr/lib/swift)
610619
endif()
611620
endif()
612621
@@ -808,14 +817,23 @@ function(add_swift_host_tool executable)
808817
# host side tools but link with clang, add the appropriate -L paths so we
809818
# find all of the necessary swift libraries on Darwin.
810819
if (CMAKE_Swift_COMPILER)
811-
# Add in the SDK directory for the host platform and add an rpath.
812-
target_link_directories(${executable} PRIVATE
813-
${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}/usr/lib/swift)
814820
# Add in the toolchain directory so we can grab compatibility libraries
815821
get_filename_component(TOOLCHAIN_BIN_DIR ${CMAKE_Swift_COMPILER} DIRECTORY)
816822
get_filename_component(TOOLCHAIN_LIB_DIR "${TOOLCHAIN_BIN_DIR}/../lib/swift/macosx" ABSOLUTE)
817823
target_link_directories(${executable} PUBLIC ${TOOLCHAIN_LIB_DIR})
818824
825+
# Add in the SDK directory for the host platform and add an rpath.
826+
#
827+
# NOTE: We do this /after/ target_link_directorying TOOLCHAIN_LIB_DIR to
828+
# ensure that we first find libraries from the toolchain, rather than from
829+
# the SDK. The reason why this is important is that when we perform a
830+
# stage2 build, this path is into the stage1 build. This is not a pure SDK
831+
# and also contains compatibility libraries. We need to make sure that the
832+
# compiler sees the actual toolchain's compatibility libraries first
833+
# before the just built compability libraries or build errors occur.
834+
target_link_directories(${executable} PRIVATE
835+
${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}/usr/lib/swift)
836+
819837
if (ASHT_HAS_LIBSWIFT AND SWIFT_TOOLS_ENABLE_LIBSWIFT)
820838
# Workaround to make lldb happy: we have to explicitly add all libswift modules
821839
# to the linker command line.

0 commit comments

Comments
 (0)