diff --git a/CMakeLists.txt b/CMakeLists.txt index 342f60855e32a..f8e678b9c297b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -799,6 +799,7 @@ include(SwiftConfigureSDK) include(SwiftComponents) include(SwiftList) include(AddPureSwift) +include(SetRPATH) # Configure swift include, install, build components. swift_configure_components() @@ -957,6 +958,12 @@ if(SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR) else() set(SWIFT_SWIFT_PARSER TRUE) include(${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS}) + + if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD" AND NOT BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS") + # Only "HOSTTOOLS" is supported in Linux when Swift parser integration is enabled. + message(WARNING "Force setting BOOTSTRAPPING=HOSTTOOLS because Swift parser integration is enabled") + set(BOOTSTRAPPING_MODE "HOSTTOOLS") + endif() endif() endif() diff --git a/SwiftCompilerSources/CMakeLists.txt b/SwiftCompilerSources/CMakeLists.txt index 89a78c6486078..51b575aec4b1c 100644 --- a/SwiftCompilerSources/CMakeLists.txt +++ b/SwiftCompilerSources/CMakeLists.txt @@ -231,7 +231,7 @@ else() # # step 1: generate a dummy source file, which just includes all headers # defined in include/swift/module.modulemap - file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp" + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp.tmp" " #include \"Basic/BridgedSwiftObject.h\" #include \"Basic/BasicBridging.h\" @@ -247,6 +247,12 @@ else() #include \"Parse/RegexParserBridging.h\" ") + add_custom_command( + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp" + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp.tmp" + "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp" + ) # step 2: build a library containing that source file. This library depends on all the included header files. # The swift modules can now depend on that target. diff --git a/cmake/modules/AddPureSwift.cmake b/cmake/modules/AddPureSwift.cmake index 80a2b32ca8a0c..17f5792339941 100644 --- a/cmake/modules/AddPureSwift.cmake +++ b/cmake/modules/AddPureSwift.cmake @@ -65,6 +65,25 @@ function(_add_host_swift_compile_options name) _add_host_variant_swift_sanitizer_flags(${name}) endfunction() +function(_set_pure_swift_link_flags name relpath_to_lib_dir) + if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD") + # Don't add builder's stdlib RPATH automatically. + target_compile_options(${name} PRIVATE + $<$:-no-toolchain-stdlib-rpath> + ) + + set_property(TARGET ${name} + APPEND PROPERTY INSTALL_RPATH + # At runtime, use swiftCore in the current just-built toolchain. + # NOTE: This relies on the ABI being the same as the builder. + "$ORIGIN/${relpath_to_lib_dir}/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}" + ) + # NOTE: At this point we don't have any pure swift executables/shared + # libraries required for building runtime/stdlib. So we don't need to add + # RPATH to the builder's runtime. + endif() +endfunction() + # Add a new "pure" Swift host library. # # "Pure" Swift host libraries can only contain Swift code, and will be built @@ -266,10 +285,17 @@ function(add_pure_swift_host_tool name) # Create the library. add_executable(${name} ${APSHT_SOURCES}) _add_host_swift_compile_options(${name}) - - set_property(TARGET ${name} - APPEND PROPERTY INSTALL_RPATH - "@executable_path/../lib/swift/host") + _set_pure_swift_link_flags(${name} "../lib") + + if(${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_DARWIN_PLATFORMS) + set_property(TARGET ${name} + APPEND PROPERTY INSTALL_RPATH + "@executable_path/../lib/swift/host") + elseif(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD") + 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/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index 5b8365e930ce8..76702543d25dd 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -545,11 +545,11 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping) target_link_libraries(${target} PRIVATE "swiftCore") target_link_directories(${target} PRIVATE ${host_lib_dir}) - if(ASRLF_BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS") - set(swift_runtime_rpath "${host_lib_dir}") - else() - set(swift_runtime_rpath "$ORIGIN/${relpath_to_lib_dir}/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}") - endif() + + # At runtime, use swiftCore in the current toolchain. + # For building stdlib, LD_LIBRARY_PATH will be set to builder's stdlib + # FIXME: This assumes the ABI hasn't changed since the builder. + set(swift_runtime_rpath "$ORIGIN/${relpath_to_lib_dir}/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}") elseif(ASRLF_BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING") # At build time link against the built swift libraries from the @@ -573,10 +573,7 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping) endif() 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") - - # 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 +587,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() @@ -822,8 +826,43 @@ macro(add_swift_lib_subdirectory name) add_llvm_subdirectory(SWIFT LIB ${name}) endmacro() +# Add a new Swift host executable. +# +# Usage: +# add_swift_host_tool(name +# [HAS_SWIFT_MODULES | DOES_NOT_USE_SWIFT] +# [THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY] +# +# [BOOTSTRAPPING 0|1] +# [SWIFT_COMPONENT component] +# [LLVM_LINK_COMPONENTS comp1 ...] +# source1 [source2 source3 ...]) +# +# name +# Name of the executable (e.g., swift-frontend). +# +# HAS_SWIFT_MODULES +# Whether to link with SwiftCompilerSources library +# +# DOES_NOT_USE_SWIFT +# Do not link with swift runtime +# +# THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY +# Opt-out of LLVM IR optimizations when linking ThinLTO with ld64 +# +# BOOTSTRAPPING +# Bootstrapping stage. +# +# SWIFT_COMPONENT +# Installation component where this tool belongs to. +# +# LLVM_LINK_COMPONENTS +# LLVM components this library depends on. +# +# source1 ... +# Sources to add into this executable. function(add_swift_host_tool executable) - set(options HAS_SWIFT_MODULES THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY) + set(options HAS_SWIFT_MODULES DOES_NOT_USE_SWIFT THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY) set(single_parameter_options SWIFT_COMPONENT BOOTSTRAPPING) set(multiple_parameter_options LLVM_LINK_COMPONENTS) @@ -879,12 +918,12 @@ function(add_swift_host_tool executable) endif() # Once the new Swift parser is linked in, every host tool has Swift modules. - if (SWIFT_SWIFT_PARSER) + if (SWIFT_SWIFT_PARSER AND NOT ASHT_DOES_NOT_USE_SWIFT) set(ASHT_HAS_SWIFT_MODULES ON) endif() if (ASHT_HAS_SWIFT_MODULES) - _add_swift_runtime_link_flags(${executable} "../lib" "${ASHT_BOOTSTRAPPING}") + _add_swift_runtime_link_flags(${executable} "../lib" "${ASHT_BOOTSTRAPPING}") endif() llvm_update_compile_flags(${executable}) @@ -926,10 +965,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) @@ -950,7 +996,8 @@ function(add_swift_host_tool executable) swift_install_in_component(TARGETS ${executable} RUNTIME DESTINATION bin - COMPONENT ${ASHT_SWIFT_COMPONENT}) + COMPONENT ${ASHT_SWIFT_COMPONENT} + ) swift_is_installing_component(${ASHT_SWIFT_COMPONENT} is_installing) endif() diff --git a/cmake/modules/AddSwiftUnittests.cmake b/cmake/modules/AddSwiftUnittests.cmake index a4573981c225c..45acb9bf10698 100644 --- a/cmake/modules/AddSwiftUnittests.cmake +++ b/cmake/modules/AddSwiftUnittests.cmake @@ -5,10 +5,32 @@ add_custom_target(SwiftUnitTests) set_target_properties(SwiftUnitTests PROPERTIES FOLDER "Tests") +# Add a new Swift unit test executable. +# +# Usage: +# add_swift_unittest(name +# [IS_TARGET_TEST] +# source1 [source2 source3 ...]) +# +# name +# Name of the test (e.g., SwiftASTTest). +# +# IS_TARGET_TEST +# Indicates this is a test for target libraries. Not host library. +# This avoids linking with toolchains stdlib. +# +# source1 ... +# Sources to add into this executable. function(add_swift_unittest test_dirname) + cmake_parse_arguments(ASU + "IS_TARGET_TEST" + "" + "" + ${ARGN}) + # *NOTE* Even though "add_unittest" does not have llvm in its name, it is a # function defined by AddLLVM.cmake. - add_unittest(SwiftUnitTests ${test_dirname} ${ARGN}) + add_unittest(SwiftUnitTests ${test_dirname} ${ASU_UNPARSED_ARGUMENTS}) set_target_properties(${test_dirname} PROPERTIES LINKER_LANGUAGE CXX) @@ -89,7 +111,8 @@ function(add_swift_unittest test_dirname) endif() endif() - if (SWIFT_SWIFT_PARSER) + if (SWIFT_SWIFT_PARSER AND NOT ASU_IS_TARGET_TEST) + # Link to stdlib the compiler uses. _add_swift_runtime_link_flags(${test_dirname} "../../lib" "") set_property(TARGET ${test_dirname} PROPERTY BUILD_WITH_INSTALL_RPATH OFF) endif() diff --git a/cmake/modules/SetRPATH.cmake b/cmake/modules/SetRPATH.cmake new file mode 100644 index 0000000000000..76b8b4644e59a --- /dev/null +++ b/cmake/modules/SetRPATH.cmake @@ -0,0 +1,24 @@ +set(SWIFT_SET_RPATH_SCRIPT_FILE "${CMAKE_CURRENT_LIST_FILE}") + +function(swift_get_set_rpath_script_file out_var) + set(${out_var} "${SWIFT_SET_RPATH_SCRIPT_FILE}" PARENT_SCOPE) +endfunction() + +# Actual RPATH_CHANGE operation to the file. +function(_swift_set_rpath_impl file new_rpath) + # FIXME: Handle non-ELF files. We can't use RPATH_SET because it's only available CMake 3.21.0 + execute_process( + COMMAND readelf -Wd "${file}" + COMMAND grep -Po "R(UN)?PATH.*\\[\\K[^\\]]*" + OUTPUT_VARIABLE current_rpath + ) + string(STRIP "${current_rpath}" current_rpath) + + # NOTE: RPATH_CHANGE is not documented, and works only for ELF and XCOFF. + file(RPATH_CHANGE FILE "${file}" OLD_RPATH "${current_rpath}" NEW_RPATH "${new_rpath}") +endfunction() + +# For 'cmake -P '. +if (SWIFT_SET_RPATH_FILE AND SWIFT_SET_RPATH_NEW_RPATH) + _swift_set_rpath_impl("${SWIFT_SET_RPATH_FILE}" "${SWIFT_SET_RPATH_NEW_RPATH}") +endif() diff --git a/cmake/modules/SwiftImplicitImport.cmake b/cmake/modules/SwiftImplicitImport.cmake index 50c5e5415ff81..3c79357f01883 100644 --- a/cmake/modules/SwiftImplicitImport.cmake +++ b/cmake/modules/SwiftImplicitImport.cmake @@ -6,7 +6,7 @@ function(swift_supports_implicit_module module_name out_var) "${CMAKE_Swift_COMPILER}" -Xfrontend -disable-implicit-${module_name}-module-import -Xfrontend -parse-stdlib - -c - -o /dev/null + -parse - INPUT_FILE "${CMAKE_BINARY_DIR}/tmp/empty-check-${module_name}.swift" OUTPUT_QUIET ERROR_QUIET diff --git a/cmake/modules/SwiftUtils.cmake b/cmake/modules/SwiftUtils.cmake index 9d6b2ce73af94..7e682f7f6471e 100644 --- a/cmake/modules/SwiftUtils.cmake +++ b/cmake/modules/SwiftUtils.cmake @@ -107,6 +107,16 @@ function(get_bootstrapping_swift_lib_dir bs_lib_dir bootstrapping) elseif("${bootstrapping}" STREQUAL "") get_bootstrapping_path(bs_lib_dir ${lib_dir} "1") endif() + elseif(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS") + if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD") + # Compiler's INSTALL_RPATH is set to libs in the build directory + # For building stdlib, use stdlib in the builder's resource directory + # because the runtime may not be built yet. + # FIXME: This assumes the ABI hasn't changed since the builder. + get_filename_component(swift_bin_dir ${CMAKE_Swift_COMPILER} DIRECTORY) + get_filename_component(swift_dir ${swift_bin_dir} DIRECTORY) + set(bs_lib_dir "${swift_dir}/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}") + endif() endif() set(bs_lib_dir ${bs_lib_dir} PARENT_SCOPE) endfunction() diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 55e8d7dc73ae2..846d165345b56 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -36,7 +36,7 @@ if (SWIFT_SWIFT_PARSER) list(TRANSFORM SWIFT_SYNTAX_MODULES PREPEND "SwiftSyntax::" OUTPUT_VARIABLE SWIFT_SYNTAX_TARGETS) - set(SWIFT_SYNTAX_LIBRARIES_SOURCE_DIR + set(SWIFT_SYNTAX_LIBRARIES_BUILD_DIR "${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/lib/swift/host") set(SWIFT_HOST_LIBRARIES_DEST_DIR "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/swift/host") @@ -56,10 +56,22 @@ if (SWIFT_SWIFT_PARSER) # Copy over all of the shared libraries from earlyswiftsyntax so they can # be found via RPATH. foreach (sharedlib ${SWIFT_SYNTAX_SHARED_LIBRARIES}) + set(add_origin_rpath) + if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD") + # At runtime, use swiftCore in the current toolchain. + swift_get_set_rpath_script_file(setrpath_command) + set(add_origin_rpath COMMAND ${CMAKE_COMMAND} + "-DSWIFT_SET_RPATH_FILE=${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}" + "-DSWIFT_SET_RPATH_NEW_RPATH='$$ORIGIN:$$ORIGIN/../${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}'" + -P "${setrpath_command}" + ) + endif() + add_custom_command( OUTPUT "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}" - DEPENDS "${SWIFT_SYNTAX_LIBRARIES_SOURCE_DIR}/${sharedlib}" - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SWIFT_SYNTAX_LIBRARIES_SOURCE_DIR}/${sharedlib} ${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib} + DEPENDS "${SWIFT_SYNTAX_LIBRARIES_BUILD_DIR}/${sharedlib}" + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SWIFT_SYNTAX_LIBRARIES_BUILD_DIR}/${sharedlib} ${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib} + ${add_origin_rpath} ) add_custom_target(copy_swiftSyntaxLibrary_${sharedlib} @@ -67,6 +79,12 @@ if (SWIFT_SWIFT_PARSER) COMMENT "Copying ${sharedlib}" ) + swift_install_in_component( + PROGRAMS "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}" + DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/host" + COMPONENT compiler + ) + add_dependencies(swiftSyntaxLibraries copy_swiftSyntaxLibrary_${sharedlib}) endforeach() @@ -79,7 +97,7 @@ if (SWIFT_SWIFT_PARSER) foreach(module_dir ${SWIFT_SYNTAX_MODULE_DIRS}) # Find all of the source module files. file(GLOB module_files - "${SWIFT_SYNTAX_LIBRARIES_SOURCE_DIR}/${module_dir}/*.swiftinterface") + "${SWIFT_SYNTAX_LIBRARIES_BUILD_DIR}/${module_dir}/*.swiftinterface") # Determine the destination module files. set(dest_module_files) @@ -101,6 +119,12 @@ if (SWIFT_SWIFT_PARSER) COMMENT "Copying ${module_dir}" ) + swift_install_in_component( + FILES ${dest_module_files} + DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/host/${module_dir}" + COMPONENT compiler + ) + add_dependencies(swiftSyntaxLibraries copy_swiftSyntaxModule_${module_dir}) endforeach() diff --git a/lib/Macros/CMakeLists.txt b/lib/Macros/CMakeLists.txt index ed98b3d9f5789..3598a4e917557 100644 --- a/lib/Macros/CMakeLists.txt +++ b/lib/Macros/CMakeLists.txt @@ -37,6 +37,19 @@ function(add_swift_macro_library name) return() endif() + # Add rpath to 'lib/{platform}' + file(RELATIVE_PATH relpath_to_lib + "${SWIFT_HOST_PLUGINS_DEST_DIR}" + "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}" + ) + _set_pure_swift_link_flags(${name} "${relpath_to_lib}") + + # Add rpath to 'lib/host' + if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD") + set_property(TARGET ${name} + APPEND PROPERTY INSTALL_RPATH "$ORIGIN/..") + endif() + # Install into the plugin directory. set_target_properties(${name} PROPERTIES @@ -44,12 +57,14 @@ function(add_swift_macro_library name) LIBRARY_OUTPUT_DIRECTORY "${SWIFT_HOST_PLUGINS_DEST_DIR}" ) + set(destination_dir "lib${LLVM_LIBDIR_SUFFIX}/swift/host/plugins") + swift_install_in_component(TARGETS ${name} LIBRARY - DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/host/plugins" + DESTINATION "${destination_dir}" COMPONENT compiler ARCHIVE - DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/host/plugins" + DESTINATION "${destination_dir}" COMPONENT compiler) # Export this macro plugin target. diff --git a/lib/SILOptimizer/Utils/BasicBlockOptUtils.cpp b/lib/SILOptimizer/Utils/BasicBlockOptUtils.cpp index 5cf2c81fb6d6b..1db6a8879cb1d 100644 --- a/lib/SILOptimizer/Utils/BasicBlockOptUtils.cpp +++ b/lib/SILOptimizer/Utils/BasicBlockOptUtils.cpp @@ -367,3 +367,12 @@ StaticInitCloner::clone(SingleValueInstruction *initVal) { } return cast(getMappedValue(initVal)); } + +// START: Workaround for C++ interop +#include +template std::pair std::make_pair<>(SILInstruction *&&, SILInstruction *&&); +template std::pair std::make_pair<>(SILValue &&, SILInstruction *&&); +template std::pair std::make_pair<>(SILType &&, SILType &&); +template llvm::DomTreeNodeBase *&std::get<0>(std::tuple *, std::default_delete>> &); +template std::default_delete> &std::get<1>(std::tuple *, std::default_delete>> &); +// END: Workaround for C++ interop diff --git a/stdlib/cmake/modules/SwiftSource.cmake b/stdlib/cmake/modules/SwiftSource.cmake index 3ea29705af520..e91471aeffff7 100644 --- a/stdlib/cmake/modules/SwiftSource.cmake +++ b/stdlib/cmake/modules/SwiftSource.cmake @@ -799,20 +799,24 @@ function(_compile_swift_files set(swift_compiler_tool "${SWIFT_SOURCE_DIR}/utils/check-incremental" "${swift_compiler_tool}") endif() - if(SWIFTFILE_IS_STDLIB) + set(custom_env "PYTHONIOENCODING=UTF8") + if(SWIFTFILE_IS_STDLIB OR + # Linux "hosttools" build require builder's runtime before building the runtime. + (BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS" AND SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD") + ) get_bootstrapping_swift_lib_dir(bs_lib_dir "${SWIFTFILE_BOOTSTRAPPING}") if(bs_lib_dir) # When building the stdlib with bootstrapping, the compiler needs # to pick up the stdlib from the previous bootstrapping stage, because the # stdlib in the current stage is not built yet. - if(${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_APPLE_PLATFORMS) - set(set_environment_args "${CMAKE_COMMAND}" "-E" "env" "DYLD_LIBRARY_PATH=${bs_lib_dir}") + if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_APPLE_PLATFORMS) + list(APPEND custom_env "DYLD_LIBRARY_PATH=${bs_lib_dir}") elseif(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD") - set(set_environment_args "${CMAKE_COMMAND}" "-E" "env" "LD_LIBRARY_PATH=${bs_lib_dir}") + list(APPEND custom_env "LD_LIBRARY_PATH=${bs_lib_dir}") endif() endif() - endif() + set(set_environment_args "${CMAKE_COMMAND}" "-E" "env" "${custom_env}") if (SWIFT_REPORT_STATISTICS) list(GET dirs_to_create 0 first_obj_dir) 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 d74963fc49c0c..76d61f614b7dd 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -526,27 +526,31 @@ lit_config.note("Using code completion cache: " + completion_cache_path) config.swift_host_lib_dir = make_path(config.swift_lib_dir, 'swift', 'host') -if kIsWindows: - config.swift_driver = ( - "%r %s %s %s -libc %s" % (config.swift, mcp_opt, - config.swift_test_options, - config.swift_driver_test_options, - config.swift_stdlib_msvc_runtime)) - config.swiftc_driver = ( - "%r -toolchain-stdlib-rpath %s %s %s" % (config.swiftc, mcp_opt, - config.swift_test_options, - config.swift_driver_test_options)) -else: +if platform.system() == 'Darwin': config.swift_driver = ( "env SDKROOT=%s %r %s %s %s" % (shell_quote(config.host_sdkroot), config.swift, mcp_opt, config.swift_test_options, config.swift_driver_test_options)) config.swiftc_driver = ( "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)) - -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) -) + 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)) +else: + config.swift_driver = ( + "%r %s %s %s" + % (config.swift, mcp_opt, config.swift_test_options, config.swift_driver_test_options)) + if kIsWindows: + config.swift_driver += " -libc " + config.swift_stdlib_msvc_runtime + config.swiftc_driver = ( + "%r -toolchain-stdlib-rpath %s %s %s" + % (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() + toolchain_lib_dir = make_path(config.swift_lib_dir, 'swift', host_os) + config.host_build_swift = ( + "%s -target %s -I %s -L %s -Xlinker -rpath -Xlinker %s" + % (config.swiftc_driver, config.host_triple, config.swift_host_lib_dir, config.swift_host_lib_dir, toolchain_lib_dir)) config.substitutions.append( ('%llvm_obj_root', config.llvm_obj_root) ) config.substitutions.append( ('%swift-lib-dir', config.swift_lib_dir) ) diff --git a/tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake b/tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake index 5c480dcd4a0ba..09f7c437c2f49 100644 --- a/tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake +++ b/tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake @@ -116,6 +116,11 @@ function(add_sourcekit_swift_runtime_link_flags target path HAS_SWIFT_MODULES) endif() # HAS_SWIFT_MODULES AND ASKD_BOOTSTRAPPING_MODE + if(SWIFT_SWIFT_PARSER) + # Add rpath to the host Swift libraries. + file(RELATIVE_PATH relative_hostlib_path "${path}" "${SWIFTLIB_DIR}/host") + list(APPEND RPATH_LIST "@loader_path/${relative_hostlib_path}") + endif() elseif(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD" AND HAS_SWIFT_MODULES AND ASKD_BOOTSTRAPPING_MODE) set(swiftrt "swiftImageRegistrationObject${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_OBJECT_FORMAT}-${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}-${SWIFT_HOST_VARIANT_ARCH}") if(${ASKD_BOOTSTRAPPING_MODE} MATCHES "HOSTTOOLS|CROSSCOMPILE") @@ -129,12 +134,11 @@ function(add_sourcekit_swift_runtime_link_flags target path HAS_SWIFT_MODULES) target_link_libraries(${target} PRIVATE "swiftCore") target_link_directories(${target} PRIVATE ${host_lib_dir}) - if(ASKD_BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS") - list(APPEND RPATH_LIST "${host_lib_dir}") - else() - file(RELATIVE_PATH relative_rtlib_path "${path}" "${SWIFTLIB_DIR}/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}") - list(APPEND RPATH_LIST "$ORIGIN/${relative_rtlib_path}") - endif() + + file(RELATIVE_PATH relative_rtlib_path "${path}" "${SWIFTLIB_DIR}/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}") + list(APPEND RPATH_LIST "$ORIGIN/${relative_rtlib_path}") + # NOTE: SourceKit components are NOT executed before stdlib is built. + # So there's no need to add RUNPATH to builder's runtime libraries. elseif(ASKD_BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING") get_bootstrapping_swift_lib_dir(bs_lib_dir "") @@ -152,31 +156,33 @@ function(add_sourcekit_swift_runtime_link_flags target path HAS_SWIFT_MODULES) else() message(FATAL_ERROR "Unknown ASKD_BOOTSTRAPPING_MODE '${ASKD_BOOTSTRAPPING_MODE}'") endif() + + if(SWIFT_SWIFT_PARSER) + # Add rpath to the host Swift libraries. + file(RELATIVE_PATH relative_hostlib_path "${path}" "${SWIFTLIB_DIR}/host") + list(APPEND RPATH_LIST "$ORIGIN/${relative_hostlib_path}") + 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") - # 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.*") - if (NOT "${bootstrapping}" STREQUAL "1") - if(${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_DARWIN_PLATFORMS) - target_link_directories(${target} PRIVATE "${sdk_dir}") - - # Include the abi stable system stdlib in our rpath. - set(swift_runtime_rpath "/usr/lib/swift") - - # Add in the toolchain directory so we can grab compatibility libraries - 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}) - endif() + if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS) + target_link_directories(${target} PRIVATE "${sdk_dir}") + + # Include the abi stable system stdlib in our rpath. + set(swift_runtime_rpath "/usr/lib/swift") + + # Add in the toolchain directory so we can grab compatibility libraries + 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}) endif() endif() endif() + + set(RPATH_LIST ${RPATH_LIST} PARENT_SCOPE) endfunction() # Add a new SourceKit library. @@ -287,6 +293,7 @@ macro(add_sourcekit_library name) RUNTIME DESTINATION "bin" COMPONENT "${SOURCEKITLIB_INSTALL_IN_COMPONENT}") + swift_install_in_component(FILES ${SOURCEKITLIB_HEADERS} DESTINATION "include/SourceKit" COMPONENT "${SOURCEKITLIB_INSTALL_IN_COMPONENT}") @@ -337,16 +344,6 @@ macro(add_sourcekit_executable name) add_sourcekit_default_compiler_flags("${name}") set_target_properties(${name} PROPERTIES LINKER_LANGUAGE CXX) - - if(SWIFT_SWIFT_PARSER) - set(SKEXEC_HAS_SWIFT_MODULES TRUE) - else() - set(SKEXEC_HAS_SWIFT_MODULES FALSE) - endif() - - set(RPATH_LIST) - add_sourcekit_swift_runtime_link_flags(${name} ${SOURCEKIT_LIBRARY_OUTPUT_INTDIR} ${SKEXEC_HAS_SWIFT_MODULES}) - endmacro() # Add a new SourceKit framework. diff --git a/tools/libSwiftScan/CMakeLists.txt b/tools/libSwiftScan/CMakeLists.txt index 5854d77a77e5e..78a6169b224d3 100644 --- a/tools/libSwiftScan/CMakeLists.txt +++ b/tools/libSwiftScan/CMakeLists.txt @@ -30,14 +30,36 @@ set_target_properties(libSwiftScan PROPERTIES OUTPUT_NAME ${SWIFT_SCAN_LIB_NAME}) -if(SWIFT_SWIFT_PARSER) - # Ensure that we can find the host shared libraries. - set_property( - TARGET libSwiftScan - APPEND PROPERTY INSTALL_RPATH "@loader_path/swift/host") +if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD" AND BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS") + # Runtime INSTALL_RPATH are set by 'add_swift_host_library', but that expects + # libSwiftScan be installed in 'lib'. But since it's actually installed in 'lib/swift/host', + # we need to have correct runtime path to 'lib/swift/{platform}'. + # FIXME: BUILD_RPATH and INSTALL_PATH should be different + # FIXME: add_swift_host_library should accept 'DESTINATION' and handle installation + # FIXME: Build this library into 'lib/swift/host/' instead of 'lib/' set_property( TARGET libSwiftScan - APPEND PROPERTY INSTALL_RPATH "@loader_path/../host") + APPEND PROPERTY INSTALL_RPATH "$ORIGIN/../${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}" + ) +endif() + +if(SWIFT_SWIFT_PARSER) + if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS) + # Ensure that we can find the host shared libraries. + set_property( + TARGET libSwiftScan + APPEND PROPERTY INSTALL_RPATH "@loader_path/swift/host") + set_property( + TARGET libSwiftScan + APPEND PROPERTY INSTALL_RPATH "@loader_path/../host") + elseif(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD") + set_property( + TARGET libSwiftScan + APPEND PROPERTY INSTALL_RPATH "$ORIGIN/swift/host") + set_property( + TARGET libSwiftScan + APPEND PROPERTY INSTALL_RPATH "$ORIGIN/../host") + endif() endif() add_llvm_symbol_exports(libSwiftScan ${LLVM_EXPORTED_SYMBOL_FILE}) diff --git a/tools/swift-compatibility-symbols/CMakeLists.txt b/tools/swift-compatibility-symbols/CMakeLists.txt index e28de9182cf4f..fff7ad8112af9 100644 --- a/tools/swift-compatibility-symbols/CMakeLists.txt +++ b/tools/swift-compatibility-symbols/CMakeLists.txt @@ -2,6 +2,7 @@ add_swift_host_tool(swift-compatibility-symbols swift-compatibility-symbols.cpp LLVM_LINK_COMPONENTS support SWIFT_COMPONENT tools + DOES_NOT_USE_SWIFT ) set(syms_file "${CMAKE_BINARY_DIR}/share/swift/compatibility-symbols") diff --git a/tools/swift-def-to-strings-converter/CMakeLists.txt b/tools/swift-def-to-strings-converter/CMakeLists.txt index 2bfd9145a5a30..fa81fe6ae60bc 100644 --- a/tools/swift-def-to-strings-converter/CMakeLists.txt +++ b/tools/swift-def-to-strings-converter/CMakeLists.txt @@ -1,6 +1,7 @@ add_swift_host_tool(swift-def-to-strings-converter swift-def-to-strings-converter.cpp SWIFT_COMPONENT tools + DOES_NOT_USE_SWIFT ) target_link_libraries(swift-def-to-strings-converter PRIVATE diff --git a/tools/swift-serialize-diagnostics/CMakeLists.txt b/tools/swift-serialize-diagnostics/CMakeLists.txt index 4a1ab49b59ed3..aee14abd2dd32 100644 --- a/tools/swift-serialize-diagnostics/CMakeLists.txt +++ b/tools/swift-serialize-diagnostics/CMakeLists.txt @@ -1,6 +1,7 @@ add_swift_host_tool(swift-serialize-diagnostics swift-serialize-diagnostics.cpp SWIFT_COMPONENT tools + DOES_NOT_USE_SWIFT ) target_link_libraries(swift-serialize-diagnostics PRIVATE swiftLocalization) diff --git a/unittests/SwiftDemangle/CMakeLists.txt b/unittests/SwiftDemangle/CMakeLists.txt index 9ba1aeb348d2d..d5657e511b811 100644 --- a/unittests/SwiftDemangle/CMakeLists.txt +++ b/unittests/SwiftDemangle/CMakeLists.txt @@ -1,5 +1,5 @@ if(TARGET swiftDemangle) - add_swift_unittest(SwiftDemangleTests + add_swift_unittest(SwiftDemangleTests IS_TARGET_TEST DemangleTest.cpp ) set_target_properties(SwiftDemangleTests diff --git a/unittests/Threading/CMakeLists.txt b/unittests/Threading/CMakeLists.txt index 5e0f27c089dc4..232f7d5b2ec34 100644 --- a/unittests/Threading/CMakeLists.txt +++ b/unittests/Threading/CMakeLists.txt @@ -1,6 +1,6 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND ("${SWIFT_HOST_VARIANT_ARCH}" STREQUAL "${SWIFT_PRIMARY_VARIANT_ARCH}")) - add_swift_unittest(SwiftThreadingTests + add_swift_unittest(SwiftThreadingTests IS_TARGET_TEST Mutex.cpp ConditionVariable.cpp Once.cpp diff --git a/unittests/runtime/CMakeLists.txt b/unittests/runtime/CMakeLists.txt index 79df463c0edb0..4949ac516320b 100644 --- a/unittests/runtime/CMakeLists.txt +++ b/unittests/runtime/CMakeLists.txt @@ -100,7 +100,7 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND Actor.cpp TaskStatus.cpp) - add_swift_unittest(SwiftRuntimeTests + add_swift_unittest(SwiftRuntimeTests IS_TARGET_TEST Array.cpp CompatibilityOverrideRuntime.cpp CompatibilityOverrideConcurrency.cpp diff --git a/unittests/runtime/LongTests/CMakeLists.txt b/unittests/runtime/LongTests/CMakeLists.txt index 592e532f8cf53..a07db267fbcc5 100644 --- a/unittests/runtime/LongTests/CMakeLists.txt +++ b/unittests/runtime/LongTests/CMakeLists.txt @@ -34,7 +34,7 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND list(APPEND PLATFORM_TARGET_LINK_LIBRARIES DbgHelp;Synchronization) endif() - add_swift_unittest(SwiftRuntimeLongTests + add_swift_unittest(SwiftRuntimeLongTests IS_TARGET_TEST LongRefcounting.cpp ../Stdlib.cpp ${PLATFORM_SOURCES} diff --git a/utils/swift_build_support/swift_build_support/products/earlyswiftsyntax.py b/utils/swift_build_support/swift_build_support/products/earlyswiftsyntax.py index 9450eee9c9b95..e9b68b02872fc 100644 --- a/utils/swift_build_support/swift_build_support/products/earlyswiftsyntax.py +++ b/utils/swift_build_support/swift_build_support/products/earlyswiftsyntax.py @@ -36,7 +36,7 @@ def is_before_build_script_impl_product(cls): def should_build(self, host_target): # Temporarily disable for non-darwin since this build never works # outside of that case currently. - if sys.platform != 'darwin': + if sys.platform != 'darwin' and sys.platform != 'linux': return False if self.args.build_early_swiftsyntax: @@ -73,18 +73,10 @@ def test(self, host_target): pass def should_install(self, host_target): - """should_install() -> Bool - - Whether or not this product should be installed with the given - arguments. - """ - return self.should_build(host_target) and self.args.install_swiftsyntax + # The artifacts are copied to build directory of 'swift' and are + # installed as a part of 'swift' product. + return False def install(self, host_target): - """ - Perform the install phase for the product. - - This phase might copy the artifacts from the previous phases into a - destination directory. - """ - self.install_with_cmake(["install"], self.host_install_destdir(host_target)) + # No-op. + pass