From c9383ff619564c311e68233ac0374b6183ec204b Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Sun, 28 May 2023 17:01:01 -0700 Subject: [PATCH 1/3] [CMake] Add paths for building Swift code against host toolchain When building Swift code into the compiler (e.g, the new Swift parser along with macros support), make sure we always add the appropriate paths to (1) link against the host Swift toolchain, and (2) find the host Swift libraries (such as SwiftSyntax) at runtime. The CMake code for doing this was only running for Darwin builds, so generalize it to also work on Linux. --- cmake/modules/AddSwift.cmake | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index 588a57465a040..248178cd4702f 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -576,7 +576,7 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping) # Make sure we can find the early SwiftSyntax libraries. target_link_directories(${target} PRIVATE "${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/lib/swift/host") - # For the "end step" of bootstrapping configurations on Darwin, need to be + # For the "end step" of bootstrapping configurations, we need to be # able to fall back to the SDK directory for libswiftCore et al. if (BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*") if (NOT "${bootstrapping}" STREQUAL "1") @@ -590,6 +590,13 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping) get_filename_component(TOOLCHAIN_BIN_DIR ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY) get_filename_component(TOOLCHAIN_LIB_DIR "${TOOLCHAIN_BIN_DIR}/../lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}" ABSOLUTE) target_link_directories(${target} PUBLIC ${TOOLCHAIN_LIB_DIR}) + else() + get_filename_component(swift_bin_dir ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY) + get_filename_component(swift_dir ${swift_bin_dir} DIRECTORY) + set(host_lib_dir "${swift_dir}/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}") + target_link_directories(${target} PRIVATE ${host_lib_dir}) + + set(swift_runtime_rpath "${host_lib_dir}") endif() endif() endif() @@ -926,10 +933,17 @@ function(add_swift_host_tool executable) endif() endif() - set_property( - TARGET ${executable} - APPEND PROPERTY INSTALL_RPATH - "@executable_path/../${extra_relative_rpath}lib/swift/host") + if(${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_DARWIN_PLATFORMS) + set_property( + TARGET ${executable} + APPEND PROPERTY INSTALL_RPATH + "@executable_path/../${extra_relative_rpath}lib/swift/host") + else() + set_property( + TARGET ${executable} + APPEND PROPERTY INSTALL_RPATH + "$ORIGIN/../${extra_relative_rpath}lib/swift/host") + endif() endif() if(ASHT_THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY) From a41c36934b058bc09e75256eddacf89252934270 Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Sun, 28 May 2023 22:20:25 -0700 Subject: [PATCH 2/3] [CMake] Fix host library builds and rpaths for testing macros This enables running macro tests on Linux. --- cmake/modules/AddPureSwift.cmake | 12 +++++++++--- test/Macros/macro_swiftdeps.swift | 4 ++-- test/lit.cfg | 11 +++++++++-- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/cmake/modules/AddPureSwift.cmake b/cmake/modules/AddPureSwift.cmake index 325f574bb3e61..b1bce09bdd7e7 100644 --- a/cmake/modules/AddPureSwift.cmake +++ b/cmake/modules/AddPureSwift.cmake @@ -261,9 +261,15 @@ function(add_pure_swift_host_tool name) add_executable(${name} ${APSHT_SOURCES}) _add_host_swift_compile_options(${name}) - set_property(TARGET ${name} - APPEND PROPERTY INSTALL_RPATH - "@executable_path/../lib/swift/host") + if(${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_DARWIN_PLATFORMS) + set_property(TARGET ${name} + APPEND PROPERTY INSTALL_RPATH + "@executable_path/../lib/swift/host") + else() + set_property(TARGET ${name} + APPEND PROPERTY INSTALL_RPATH + "$ORIGIN/../lib/swift/host") + endif() set_property(TARGET ${name} PROPERTY BUILD_WITH_INSTALL_RPATH YES) diff --git a/test/Macros/macro_swiftdeps.swift b/test/Macros/macro_swiftdeps.swift index dd969f3d6e9a9..2b6438e7ffada 100644 --- a/test/Macros/macro_swiftdeps.swift +++ b/test/Macros/macro_swiftdeps.swift @@ -7,7 +7,7 @@ // RUN: split-file %s %t/src -//#-- Prepare the macro dylib plugin. +//#-- Prepare the macro shared library plugin. // RUN: %host-build-swift \ // RUN: -swift-version 5 \ // RUN: -emit-library -o %t/plugin/%target-library-name(MacroDefinition) \ @@ -76,7 +76,7 @@ // RUN: %FileCheck --check-prefix WITHOUT_PLUGIN %s < %t/with_macro_nonprimary.swiftdeps.processed // WITH_PLUGIN: externalDepend interface '' 'BUILD_DIR{{.*}}mock-plugin' false -// WITH_PLUGIN: externalDepend interface '' 'BUILD_DIR{{.*}}libMacroDefinition.dylib' false +// WITH_PLUGIN: externalDepend interface '' 'BUILD_DIR{{.*}}libMacroDefinition.{{(dylib|so|dll)}}' false // WITHOUT_PLUGIN-NOT: MacroDefinition // WITHOUT_PLUGIN-NOT: mock-plugin diff --git a/test/lit.cfg b/test/lit.cfg index b8f7840153595..612cb548bb00f 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -548,9 +548,16 @@ else: "env SDKROOT=%s %r -toolchain-stdlib-rpath -Xlinker -rpath -Xlinker /usr/lib/swift %s %s %s" % (shell_quote(config.host_sdkroot), config.swiftc, mcp_opt, config.swift_test_options, config.swift_driver_test_options)) +# Parse the host triple. +(host_cpu, host_vendor, host_os, host_vers) = re.match('([^-]+)-([^-]+)-([^0-9-]+)(.*)', config.host_triple).groups() + +if platform.system() == 'Darwin': + host_build_extra_rpath="" +else: + host_build_extra_rpath="-Xlinker -rpath -Xlinker %s" % (make_path(config.swift_lib_dir, 'swift', host_os)) + config.host_build_swift = ( - "%s -sdk %s -target %s -I %s -L %s" % (config.swiftc_driver, config.host_sdkroot, config.host_triple, config.swift_host_lib_dir, config.swift_host_lib_dir) -) + "%s -sdk %s -target %s -I %s -L %s %s" % (config.swiftc_driver, config.host_sdkroot, config.host_triple, config.swift_host_lib_dir, config.swift_host_lib_dir, host_build_extra_rpath)) config.substitutions.append( ('%llvm_obj_root', config.llvm_obj_root) ) config.substitutions.append( ('%swift-bin-dir', config.swift_bin_dir) ) From 3ca13588a4a7053cf2973d3868bb69abff2ab0b0 Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Sun, 28 May 2023 23:27:15 -0700 Subject: [PATCH 3/3] [SourceKit] Fix paths when including the new Swift compiler --- .../cmake/modules/AddSwiftSourceKit.cmake | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake b/tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake index 5c480dcd4a0ba..f44b8de53895e 100644 --- a/tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake +++ b/tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake @@ -153,12 +153,17 @@ function(add_sourcekit_swift_runtime_link_flags target path HAS_SWIFT_MODULES) message(FATAL_ERROR "Unknown ASKD_BOOTSTRAPPING_MODE '${ASKD_BOOTSTRAPPING_MODE}'") endif() endif() - set(RPATH_LIST ${RPATH_LIST} PARENT_SCOPE) if(SWIFT_SWIFT_PARSER) # Make sure we can find the early SwiftSyntax libraries. target_link_directories(${target} PRIVATE "${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/lib/swift/host") + # Add rpath to the host Swift libraries. + if (NOT ${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_DARWIN_PLATFORMS) + file(RELATIVE_PATH relative_hostlib_path "${path}" "${SWIFTLIB_DIR}/host") + list(APPEND RPATH_LIST "$ORIGIN/${relative_hostlib_path}") + endif() + # For the "end step" of bootstrapping configurations on Darwin, need to be # able to fall back to the SDK directory for libswiftCore et al. if (BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*") @@ -173,10 +178,19 @@ function(add_sourcekit_swift_runtime_link_flags target path HAS_SWIFT_MODULES) get_filename_component(TOOLCHAIN_BIN_DIR ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY) get_filename_component(TOOLCHAIN_LIB_DIR "${TOOLCHAIN_BIN_DIR}/../lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}" ABSOLUTE) target_link_directories(${target} PUBLIC ${TOOLCHAIN_LIB_DIR}) + else() + get_filename_component(swift_bin_dir ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY) + get_filename_component(swift_dir ${swift_bin_dir} DIRECTORY) + set(host_lib_dir "${swift_dir}/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}") + target_link_directories(${target} PUBLIC ${host_lib_dir}) + + list(APPEND RPATH_LIST "${host_lib_dir}") endif() endif() endif() endif() + + set(RPATH_LIST ${RPATH_LIST} PARENT_SCOPE) endfunction() # Add a new SourceKit library.