From c5fe4f0c180207e76fd385634c4efaa3618a7b57 Mon Sep 17 00:00:00 2001 From: Michael Gottesman Date: Tue, 22 Jun 2021 13:23:44 -0700 Subject: [PATCH 1/2] [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. --- cmake/modules/AddSwift.cmake | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index fe7186aec4d1b..e1c1fc47df860 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -600,13 +600,22 @@ function(add_swift_host_library name) # find all of the necessary swift libraries on Darwin. if (NOT ASHL_PURE_SWIFT) if (CMAKE_Swift_COMPILER) - # Add in the SDK directory for the host platform and add an rpath. - target_link_directories(${name} PRIVATE - ${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}/usr/lib/swift) # Add in the toolchain directory so we can grab compatibility libraries get_filename_component(TOOLCHAIN_BIN_DIR ${CMAKE_Swift_COMPILER} DIRECTORY) get_filename_component(TOOLCHAIN_LIB_DIR "${TOOLCHAIN_BIN_DIR}/../lib/swift/macosx" ABSOLUTE) target_link_directories(${name} PUBLIC ${TOOLCHAIN_LIB_DIR}) + + # Add in the SDK directory for the host platform. + # + # NOTE: We do this /after/ target_link_directorying TOOLCHAIN_LIB_DIR to + # ensure that we first find libraries from the toolchain, rather than + # from the SDK. The reason why this is important is that when we perform + # a stage2 build, this path is into the stage1 build. This is not a pure + # SDK and also contains compatibility libraries. We need to make sure + # that the compiler sees the actual toolchain's compatibility libraries + # first before the just built compability libraries or build errors occur. + target_link_directories(${name} PRIVATE + ${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}/usr/lib/swift) endif() endif() @@ -808,14 +817,23 @@ function(add_swift_host_tool executable) # host side tools but link with clang, add the appropriate -L paths so we # find all of the necessary swift libraries on Darwin. if (CMAKE_Swift_COMPILER) - # Add in the SDK directory for the host platform and add an rpath. - target_link_directories(${executable} PRIVATE - ${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}/usr/lib/swift) # Add in the toolchain directory so we can grab compatibility libraries get_filename_component(TOOLCHAIN_BIN_DIR ${CMAKE_Swift_COMPILER} DIRECTORY) get_filename_component(TOOLCHAIN_LIB_DIR "${TOOLCHAIN_BIN_DIR}/../lib/swift/macosx" ABSOLUTE) target_link_directories(${executable} PUBLIC ${TOOLCHAIN_LIB_DIR}) + # Add in the SDK directory for the host platform and add an rpath. + # + # NOTE: We do this /after/ target_link_directorying TOOLCHAIN_LIB_DIR to + # ensure that we first find libraries from the toolchain, rather than from + # the SDK. The reason why this is important is that when we perform a + # stage2 build, this path is into the stage1 build. This is not a pure SDK + # and also contains compatibility libraries. We need to make sure that the + # compiler sees the actual toolchain's compatibility libraries first + # before the just built compability libraries or build errors occur. + target_link_directories(${executable} PRIVATE + ${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}/usr/lib/swift) + if (ASHT_HAS_LIBSWIFT AND SWIFT_TOOLS_ENABLE_LIBSWIFT) # Workaround to make lldb happy: we have to explicitly add all libswift modules # to the linker command line. From c0d6d8f1907fd8b700b4dbece7f99301bd5ebe53 Mon Sep 17 00:00:00 2001 From: Michael Gottesman Date: Sun, 27 Jun 2021 16:02:38 -0700 Subject: [PATCH 2/2] [cmake] Respect SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER when compiling the stdlib if SWIFT_NATIVE_SWIFT_TOOLS_PATH is not set and CMake_Swift_COMPILER is. Previously, no matter if SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER was set, we would use for swiftc SWIFT_NATIVE_SWIFT_TOOLS_PATH/bin/swiftc. This is correct assuming that the user always passed in that flag. This will no longer always be true since we are attempting to transition the stdlib slowly to use more standard cmake. Instead, in that case if SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER is set and SWIFT_NATIVE_SWIFT_TOOLS_PATH is not set /and/ we have a CMAKE_Swift_COMPILER, we just use CMAKE_Swift_COMPILER. Hopefully with time we get rid of SWIFT_NATIVE_SWIFT_TOOLS_PATH. --- CMakeLists.txt | 12 ++++++++++++ stdlib/cmake/modules/SwiftSource.cmake | 12 +++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3e8598b7e0458..be571722fa7f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -507,6 +507,11 @@ execute_process(COMMAND ${CMAKE_MAKE_PROGRAM} --version message(STATUS "CMake Make Program (${CMAKE_MAKE_PROGRAM}) Version: ${_CMAKE_MAKE_PROGRAM_VERSION}") message(STATUS "C Compiler (${CMAKE_C_COMPILER}) Version: ${CMAKE_C_COMPILER_VERSION}") message(STATUS "C++ Compiler (${CMAKE_CXX_COMPILER}) Version: ${CMAKE_CXX_COMPILER_VERSION}") +if (CMAKE_Swift_COMPILER) + message(STATUS "Swift Compiler (${CMAKE_Swift_COMPILER}) Version: ${CMAKE_Swift_COMPILER_VERSION}") +else() + message(STATUS "Swift Compiler (None).") +endif() if(SWIFT_PATH_TO_CMARK_BUILD) execute_process(COMMAND ${SWIFT_PATH_TO_CMARK_BUILD}/src/cmark --version OUTPUT_VARIABLE _CMARK_VERSION @@ -523,6 +528,13 @@ else() set(SWIFT_PREBUILT_CLANG TRUE) endif() +# Also mark if we have a prebuilt swift before we do anything. +if("${SWIFT_NATIVE_SWIFT_TOOLS_PATH}" STREQUAL "") + set(SWIFT_PREBUILT_SWIFT FALSE) +else() + set(SWIFT_PREBUILT_SWIFT TRUE) +endif() + include(SwiftSharedCMakeConfig) # NOTE: We include this before SwiftComponents as it relies on some LLVM CMake diff --git a/stdlib/cmake/modules/SwiftSource.cmake b/stdlib/cmake/modules/SwiftSource.cmake index 6b5be98cdfd04..18013af0ade5a 100644 --- a/stdlib/cmake/modules/SwiftSource.cmake +++ b/stdlib/cmake/modules/SwiftSource.cmake @@ -630,7 +630,17 @@ function(_compile_swift_files if(CMAKE_HOST_SYSTEM_NAME STREQUAL Windows) set(HOST_EXECUTABLE_SUFFIX .exe) endif() - set(swift_compiler_tool "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swiftc${HOST_EXECUTABLE_SUFFIX}") + if(SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER) + if(SWIFT_PREBUILT_SWIFT) + set(swift_compiler_tool "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swiftc${HOST_EXECUTABLE_SUFFIX}") + elseif(CMAKE_Swift_COMPILER) + set(swift_compiler_tool "${CMAKE_Swift_COMPILER}") + else() + message(ERROR "Must pass in prebuilt tools using SWIFT_NATIVE_SWIFT_TOOLS_PATH or set CMAKE_Swift_COMPILER") + endif() + else() + set(swift_compiler_tool "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swiftc${HOST_EXECUTABLE_SUFFIX}") + endif() set(swift_compiler_tool_dep) if(SWIFT_INCLUDE_TOOLS)