Skip to content

Commit bbac657

Browse files
authored
Merge pull request #3611 from swiftwasm/maxd/main-merge
Resolve conflicts with upstream `main`
2 parents e99ad26 + f50ae11 commit bbac657

File tree

89 files changed

+3811
-734
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+3811
-734
lines changed

CMakeLists.txt

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
cmake_minimum_required(VERSION 3.19.6)
22

3+
4+
# set_property(GLOBAL PROPERTY GLOBAL_DEPENDS_DEBUG_MODE 1)
5+
36
# TODO: Fix RPATH usage to be CMP0068 compliant
47
# Disable Policy CMP0068 for CMake 3.9
58
# rdar://37725888
@@ -196,9 +199,13 @@ set(SWIFT_TOOLS_ENABLE_LTO OFF CACHE STRING "Build Swift tools with LTO. One
196199
no effect on the target libraries (the standard library and the runtime).")
197200

198201
# NOTE: We do not currently support building libswift with the Xcode generator.
199-
cmake_dependent_option(SWIFT_TOOLS_ENABLE_LIBSWIFT
200-
"Enable building libswift and linking libswift into the compiler itself." FALSE
201-
"NOT CMAKE_GENERATOR STREQUAL \"Xcode\"" FALSE)
202+
cmake_dependent_option(LIBSWIFT_BUILD_MODE "How to build libswift. Possible values are
203+
OFF: the compiler is built without libswift
204+
HOSTTOOLS: libswift is built with a pre-installed toolchain
205+
BOOTSTRAPPING: libswift is built with a 2-stage bootstrapping process
206+
BOOTSTRAPPING-WITH-HOSTLIBS: libswift is built with a 2-stage bootstrapping process,
207+
but the compiler links against the host system swift libs (macOS only)"
208+
OFF "NOT CMAKE_GENERATOR STREQUAL \"Xcode\"" OFF)
202209

203210
# The following only works with the Ninja generator in CMake >= 3.0.
204211
set(SWIFT_PARALLEL_LINK_JOBS "" CACHE STRING
@@ -594,6 +601,13 @@ set(SWIFT_RUNTIME_OUTPUT_INTDIR "${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin")
594601
set(SWIFT_LIBRARY_OUTPUT_INTDIR "${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib")
595602
if("${SWIFT_NATIVE_SWIFT_TOOLS_PATH}" STREQUAL "")
596603
set(SWIFT_NATIVE_SWIFT_TOOLS_PATH "${SWIFT_RUNTIME_OUTPUT_INTDIR}")
604+
set(SWIFT_EXEC_FOR_LIBSWIFT "${CMAKE_Swift_COMPILER}")
605+
elseif(LIBSWIFT_BUILD_MODE STREQUAL "BOOTSTRAPPING" OR LIBSWIFT_BUILD_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS")
606+
# If cross-compiling we don't have to bootstrap. We can just use the previously
607+
# built native swiftc to build libswift.
608+
message(STATUS "Building libswift with native host tools instead of bootstrapping")
609+
set(LIBSWIFT_BUILD_MODE "HOSTTOOLS")
610+
set(SWIFT_EXEC_FOR_LIBSWIFT "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swiftc")
597611
endif()
598612

599613
# This setting causes all CMakeLists.txt to automatically have
@@ -938,7 +952,7 @@ if(SWIFT_INCLUDE_TOOLS)
938952
message(STATUS " Build type: ${CMAKE_BUILD_TYPE}")
939953
message(STATUS " Assertions: ${LLVM_ENABLE_ASSERTIONS}")
940954
message(STATUS " LTO: ${SWIFT_TOOLS_ENABLE_LTO}")
941-
message(STATUS " libswift: ${SWIFT_TOOLS_ENABLE_LIBSWIFT}")
955+
message(STATUS " libswift: ${LIBSWIFT_BUILD_MODE}")
942956
message(STATUS "")
943957
else()
944958
message(STATUS "Not building host Swift tools")
@@ -1045,6 +1059,9 @@ if(SWIFT_ENABLE_DISPATCH)
10451059
include(Libdispatch)
10461060
endif()
10471061

1062+
add_bootstrapping_target(0)
1063+
add_bootstrapping_target(1)
1064+
10481065
# Add all of the subdirectories, where we actually do work.
10491066

10501067
###############

cmake/modules/AddSwift.cmake

Lines changed: 153 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ function(add_swift_host_library name)
522522
523523
add_library(${name} ${libkind} ${ASHL_SOURCES})
524524
525-
if (ASHL_HAS_LIBSWIFT AND SWIFT_TOOLS_ENABLE_LIBSWIFT)
525+
if (ASHL_HAS_LIBSWIFT AND LIBSWIFT_BUILD_MODE)
526526
# Workaround for a linker crash related to autolinking: rdar://77839981
527527
set_property(TARGET ${name} APPEND_STRING PROPERTY
528528
LINK_FLAGS " -lobjc ")
@@ -724,8 +724,13 @@ endfunction()
724724
# This is a temporary workaround until it's possible to compile libswift with
725725
# cmake's builtin swift support.
726726
function(add_libswift name)
727+
cmake_parse_arguments(ALS
728+
""
729+
"BOOTSTRAPPING;SWIFT_EXEC;DEPENDS"
730+
""
731+
${ARGN})
732+
727733
set(libswift_compile_options
728-
"-target" "x86_64-apple-macosx10.15" # TODO: remove this once #38675 lands.
729734
"-Xfrontend" "-validate-tbd-against-ir=none"
730735
"-Xfrontend" "-enable-cxx-interop")
731736
@@ -735,10 +740,11 @@ function(add_libswift name)
735740
list(APPEND libswift_compile_options "-O" "-cross-module-optimization")
736741
endif()
737742
738-
set(build_dir ${CMAKE_CURRENT_BINARY_DIR})
743+
get_bootstrapping_path(build_dir ${CMAKE_CURRENT_BINARY_DIR} "${ALS_BOOTSTRAPPING}")
739744
740745
if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
741-
set(deployment_version "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
746+
set(deployment_version "10.15") # TODO: once #38675 lands, replace this with
747+
# set(deployment_version "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
742748
endif()
743749
get_versioned_target_triple(target ${SWIFT_HOST_VARIANT_SDK}
744750
${SWIFT_HOST_VARIANT_ARCH} "${deployment_version}")
@@ -750,10 +756,15 @@ function(add_libswift name)
750756
get_target_property(module ${module_target} "module_name")
751757
get_target_property(sources ${module_target} SOURCES)
752758
get_target_property(dependencies ${module_target} "module_depends")
753-
if(dependencies)
754-
list(TRANSFORM dependencies PREPEND "LibSwift")
755-
else()
756-
set(dependencies "")
759+
set(deps, "")
760+
if (dependencies)
761+
foreach(dep_module ${dependencies})
762+
if (DEFINED "${dep_module}_dep_target")
763+
list(APPEND deps "${${dep_module}_dep_target}")
764+
else()
765+
message(FATAL_ERROR "libswift module dependency ${module} -> ${dep_module} not found. Make sure to add modules in dependency order")
766+
endif()
767+
endforeach()
757768
endif()
758769
759770
set(module_obj_file "${build_dir}/${module}.o")
@@ -765,8 +776,8 @@ function(add_libswift name)
765776
# Compile the libswift module into an object file
766777
add_custom_command_target(dep_target OUTPUT ${module_obj_file}
767778
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
768-
DEPENDS ${sources} ${dependencies}
769-
COMMAND ${CMAKE_Swift_COMPILER} "-c" "-o" ${module_obj_file}
779+
DEPENDS ${sources} ${deps} ${ALS_DEPENDS}
780+
COMMAND ${ALS_SWIFT_EXEC} "-c" "-o" ${module_obj_file}
770781
"-sdk" "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}"
771782
"-target" ${target}
772783
"-module-name" ${module} "-emit-module"
@@ -777,8 +788,7 @@ function(add_libswift name)
777788
"-I" "${build_dir}"
778789
COMMENT "Building libswift module ${module}")
779790
780-
add_dependencies(${module_target} ${dep_target})
781-
791+
set("${module}_dep_target" ${dep_target})
782792
endforeach()
783793
784794
# Create a static libswift library containing all module object files.
@@ -794,9 +804,19 @@ macro(add_swift_lib_subdirectory name)
794804
add_llvm_subdirectory(SWIFT LIB ${name})
795805
endmacro()
796806
807+
function(_link_built_compatibility_libs executable)
808+
set(platform ${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR})
809+
target_link_directories(${executable} PRIVATE
810+
${SWIFTLIB_DIR}/${platform})
811+
add_dependencies(${executable}
812+
"swiftCompatibility50-${platform}"
813+
"swiftCompatibility51-${platform}"
814+
"swiftCompatibilityDynamicReplacements-${platform}")
815+
endfunction()
816+
797817
function(add_swift_host_tool executable)
798818
set(options HAS_LIBSWIFT)
799-
set(single_parameter_options SWIFT_COMPONENT)
819+
set(single_parameter_options SWIFT_COMPONENT BOOTSTRAPPING)
800820
set(multiple_parameter_options LLVM_LINK_COMPONENTS)
801821
802822
cmake_parse_arguments(ASHT
@@ -835,76 +855,119 @@ function(add_swift_host_tool executable)
835855
add_dependencies(${executable} ${LLVM_COMMON_DEPENDS})
836856
endif()
837857
858+
if(NOT ${ASHT_BOOTSTRAPPING} STREQUAL "")
859+
# Strip the "-bootstrapping<n>" suffix from the target name to get the base
860+
# executable name.
861+
string(REGEX REPLACE "-bootstrapping.*" "" executable_filename ${executable})
862+
set_target_properties(${executable}
863+
PROPERTIES OUTPUT_NAME ${executable_filename})
864+
endif()
865+
838866
set_target_properties(${executable} PROPERTIES
839867
FOLDER "Swift executables")
840868
if(SWIFT_PARALLEL_LINK_JOBS)
841869
set_target_properties(${executable} PROPERTIES
842870
JOB_POOL_LINK swift_link_job_pool)
843871
endif()
844872
if(${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_DARWIN_PLATFORMS)
873+
874+
# Lists of rpaths that we are going to add to our executables.
875+
#
876+
# Please add each rpath separately below to the list, explaining why you are
877+
# adding it.
878+
set(RPATH_LIST)
879+
845880
# If we found a swift compiler and are going to use swift code in swift
846881
# host side tools but link with clang, add the appropriate -L paths so we
847882
# find all of the necessary swift libraries on Darwin.
848-
if (CMAKE_Swift_COMPILER)
849-
# Add in the toolchain directory so we can grab compatibility libraries
850-
get_filename_component(TOOLCHAIN_BIN_DIR ${CMAKE_Swift_COMPILER} DIRECTORY)
851-
get_filename_component(TOOLCHAIN_LIB_DIR "${TOOLCHAIN_BIN_DIR}/../lib/swift/macosx" ABSOLUTE)
852-
target_link_directories(${executable} PUBLIC ${TOOLCHAIN_LIB_DIR})
883+
if (ASHT_HAS_LIBSWIFT AND LIBSWIFT_BUILD_MODE)
884+
885+
if(LIBSWIFT_BUILD_MODE STREQUAL "HOSTTOOLS")
886+
# Add in the toolchain directory so we can grab compatibility libraries
887+
get_filename_component(TOOLCHAIN_BIN_DIR ${SWIFT_EXEC_FOR_LIBSWIFT} DIRECTORY)
888+
get_filename_component(TOOLCHAIN_LIB_DIR "${TOOLCHAIN_BIN_DIR}/../lib/swift/macosx" ABSOLUTE)
889+
target_link_directories(${executable} PUBLIC ${TOOLCHAIN_LIB_DIR})
890+
891+
# Add in the SDK directory for the host platform.
892+
target_link_directories(${executable} PRIVATE
893+
${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}/usr/lib/swift)
894+
895+
# Include the abi stable system stdlib in our rpath.
896+
list(APPEND RPATH_LIST "/usr/lib/swift")
897+
898+
elseif(LIBSWIFT_BUILD_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS")
899+
# Pick up the built libswiftCompatibility<n>.a libraries
900+
_link_built_compatibility_libs(${executable})
901+
902+
# Add in the SDK directory for the host platform.
903+
target_link_directories(${executable} PRIVATE
904+
${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}/usr/lib/swift)
853905
854-
# Add in the SDK directory for the host platform and add an rpath.
906+
# Include the abi stable system stdlib in our rpath.
907+
list(APPEND RPATH_LIST "/usr/lib/swift")
908+
909+
elseif(LIBSWIFT_BUILD_MODE STREQUAL "BOOTSTRAPPING")
910+
# Pick up the built libswiftCompatibility<n>.a libraries
911+
_link_built_compatibility_libs(${executable})
912+
913+
# At build time link against the built swift libraries from the
914+
# previous bootstrapping stage.
915+
get_bootstrapping_swift_lib_dir(bs_lib_dir "${bootstrapping}")
916+
target_link_directories(${executable} PRIVATE ${bs_lib_dir})
917+
918+
# At runtime link against the built swift libraries from the current
919+
# bootstrapping stage.
920+
list(APPEND RPATH_LIST "@executable_path/../lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
921+
else()
922+
message(FATAL_ERROR "Unknown LIBSWIFT_BUILD_MODE '${LIBSWIFT_BUILD_MODE}'")
923+
endif()
924+
925+
# Workaround to make lldb happy: we have to explicitly add all libswift modules
926+
# to the linker command line.
927+
set(libswift_ast_path_flags "-Wl")
928+
get_property(modules GLOBAL PROPERTY "libswift_modules")
929+
foreach(module ${modules})
930+
get_target_property(module_file "LibSwift${module}" "module_file")
931+
string(APPEND libswift_ast_path_flags ",-add_ast_path,${module_file}")
932+
endforeach()
933+
934+
set_property(TARGET ${executable} APPEND_STRING PROPERTY
935+
LINK_FLAGS ${libswift_ast_path_flags})
936+
937+
# Workaround for a linker crash related to autolinking: rdar://77839981
938+
set_property(TARGET ${executable} APPEND_STRING PROPERTY
939+
LINK_FLAGS " -lobjc ")
940+
941+
else() # ASHT_HAS_LIBSWIFT AND LIBSWIFT_BUILD_MODE
942+
943+
# TODO: do we really need this? Do any tools which don't link libswift include other swift code?
944+
945+
# Add in the SDK directory for the host platform.
855946
#
856947
# NOTE: We do this /after/ target_link_directorying TOOLCHAIN_LIB_DIR to
857948
# ensure that we first find libraries from the toolchain, rather than from
858-
# the SDK. The reason why this is important is that when we perform a
859-
# stage2 build, this path is into the stage1 build. This is not a pure SDK
860-
# and also contains compatibility libraries. We need to make sure that the
861-
# compiler sees the actual toolchain's compatibility libraries first
862-
# before the just built compability libraries or build errors occur.
949+
# the SDK.
863950
target_link_directories(${executable} PRIVATE
864951
${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}/usr/lib/swift)
865952
866-
if (ASHT_HAS_LIBSWIFT AND SWIFT_TOOLS_ENABLE_LIBSWIFT)
867-
# Workaround to make lldb happy: we have to explicitly add all libswift modules
868-
# to the linker command line.
869-
set(libswift_ast_path_flags "-Wl")
870-
get_property(modules GLOBAL PROPERTY "libswift_modules")
871-
foreach(module ${modules})
872-
get_target_property(module_file "LibSwift${module}" "module_file")
873-
string(APPEND libswift_ast_path_flags ",-add_ast_path,${module_file}")
874-
endforeach()
875-
876-
set_property(TARGET ${executable} APPEND_STRING PROPERTY
877-
LINK_FLAGS ${libswift_ast_path_flags})
878-
879-
# Workaround for a linker crash related to autolinking: rdar://77839981
880-
set_property(TARGET ${executable} APPEND_STRING PROPERTY
881-
LINK_FLAGS " -lobjc ")
882-
endif()
883-
endif()
953+
# We also want to be able to find libraries from the base toolchain
954+
# directory. This is so swiftc can rely on its own host side dylibs that may
955+
# contain swift content.
956+
list(APPEND RPATH_LIST "@executable_path/../lib")
884957
885-
# Lists of rpaths that we are going to add to our executables.
886-
#
887-
# Please add each rpath separately below to the list, explaining why you are
888-
# adding it.
889-
set(RPATH_LIST)
890-
891-
# We also want to be able to find libraries from the base toolchain
892-
# directory. This is so swiftc can rely on its own host side dylibs that may
893-
# contain swift content.
894-
list(APPEND RPATH_LIST "@executable_path/../lib")
895-
896-
# Also include the abi stable system stdlib in our rpath.
897-
list(APPEND RPATH_LIST "/usr/lib/swift")
958+
# Also include the abi stable system stdlib in our rpath.
959+
list(APPEND RPATH_LIST "/usr/lib/swift")
960+
endif()
898961
899962
set_target_properties(${executable} PROPERTIES
900963
BUILD_WITH_INSTALL_RPATH YES
901964
INSTALL_RPATH "${RPATH_LIST}")
902965
903-
elseif(SWIFT_HOST_VARIANT_SDK STREQUAL "LINUX")
904-
if (ASHT_HAS_LIBSWIFT AND SWIFT_TOOLS_ENABLE_LIBSWIFT)
966+
elseif(SWIFT_HOST_VARIANT_SDK STREQUAL "LINUX" AND ASHT_HAS_LIBSWIFT AND LIBSWIFT_BUILD_MODE)
967+
if(LIBSWIFT_BUILD_MODE STREQUAL "HOSTTOOLS")
905968
# At build time and and run time, link against the swift libraries in the
906969
# installed host toolchain.
907-
get_filename_component(swift_bin_dir ${CMAKE_Swift_COMPILER} DIRECTORY)
970+
get_filename_component(swift_bin_dir ${SWIFT_EXEC_FOR_LIBSWIFT} DIRECTORY)
908971
get_filename_component(swift_dir ${swift_bin_dir} DIRECTORY)
909972
set(host_lib_dir "${swift_dir}/lib/swift/linux")
910973
@@ -914,14 +977,36 @@ function(add_swift_host_tool executable)
914977
set_target_properties(${executable} PROPERTIES
915978
BUILD_WITH_INSTALL_RPATH YES
916979
INSTALL_RPATH "${host_lib_dir}")
980+
981+
elseif(LIBSWIFT_BUILD_MODE STREQUAL "BOOTSTRAPPING")
982+
# At build time link against the built swift libraries from the
983+
# previous bootstrapping stage.
984+
get_bootstrapping_swift_lib_dir(bs_lib_dir "${bootstrapping}")
985+
target_link_directories(${executable} PRIVATE ${bs_lib_dir})
986+
987+
# At runtime link against the built swift libraries from the current
988+
# bootstrapping stage.
989+
set_target_properties(${executable} PROPERTIES
990+
BUILD_WITH_INSTALL_RPATH YES
991+
INSTALL_RPATH "$ORIGIN/../lib/swift/${SWIFT_SDK_LINUX_LIB_SUBDIR}")
992+
993+
elseif(LIBSWIFT_BUILD_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS")
994+
message(FATAL_ERROR "LIBSWIFT_BUILD_MODE 'BOOTSTRAPPING-WITH-HOSTLIBS' not supported on Linux")
995+
else()
996+
message(FATAL_ERROR "Unknown LIBSWIFT_BUILD_MODE '${LIBSWIFT_BUILD_MODE}'")
917997
endif()
918998
endif()
919999
9201000
llvm_update_compile_flags(${executable})
9211001
swift_common_llvm_config(${executable} ${ASHT_LLVM_LINK_COMPONENTS})
1002+
1003+
get_bootstrapping_path(out_bin_dir
1004+
${SWIFT_RUNTIME_OUTPUT_INTDIR} "${ASHT_BOOTSTRAPPING}")
1005+
get_bootstrapping_path(out_lib_dir
1006+
${SWIFT_LIBRARY_OUTPUT_INTDIR} "${ASHT_BOOTSTRAPPING}")
9221007
set_output_directory(${executable}
923-
BINARY_DIR ${SWIFT_RUNTIME_OUTPUT_INTDIR}
924-
LIBRARY_DIR ${SWIFT_LIBRARY_OUTPUT_INTDIR})
1008+
BINARY_DIR ${out_bin_dir}
1009+
LIBRARY_DIR ${out_lib_dir})
9251010
9261011
if(SWIFT_HOST_VARIANT_SDK STREQUAL WINDOWS)
9271012
swift_windows_include_for_arch(${SWIFT_HOST_VARIANT_ARCH}
@@ -940,13 +1025,15 @@ function(add_swift_host_tool executable)
9401025
endif()
9411026
endif()
9421027
943-
add_dependencies(${ASHT_SWIFT_COMPONENT} ${executable})
944-
swift_install_in_component(TARGETS ${executable}
945-
RUNTIME
946-
DESTINATION bin
947-
COMPONENT ${ASHT_SWIFT_COMPONENT})
1028+
if(NOT ${ASHT_SWIFT_COMPONENT} STREQUAL "no_component")
1029+
add_dependencies(${ASHT_SWIFT_COMPONENT} ${executable})
1030+
swift_install_in_component(TARGETS ${executable}
1031+
RUNTIME
1032+
DESTINATION bin
1033+
COMPONENT ${ASHT_SWIFT_COMPONENT})
9481034
949-
swift_is_installing_component(${ASHT_SWIFT_COMPONENT} is_installing)
1035+
swift_is_installing_component(${ASHT_SWIFT_COMPONENT} is_installing)
1036+
endif()
9501037
9511038
if(NOT is_installing)
9521039
set_property(GLOBAL APPEND PROPERTY SWIFT_BUILDTREE_EXPORTS ${executable})

0 commit comments

Comments
 (0)