Skip to content

cmake: link the compiler against a statically built libswiftCore when building libswift in "bootstrapping" mode #40308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ cmake_dependent_option(LIBSWIFT_BUILD_MODE "How to build libswift. Possible valu
BOOTSTRAPPING: libswift is built with a 2-stage bootstrapping process
BOOTSTRAPPING-WITH-HOSTLIBS: libswift is built with a 2-stage bootstrapping process,
but the compiler links against the host system swift libs (macOS only)
CROSSCOMPILE: libswift is built with a bootstrapping compiled compiler, provided in
`SWIFT_NATIVE_SWIFT_TOOLS_PATH`
CROSSCOMPILE-WITH-HOSTLIBS: libswift is built with a bootstrapping-with-hostlibs compiled
compiler, provided in `SWIFT_NATIVE_SWIFT_TOOLS_PATH`"
OFF "NOT CMAKE_GENERATOR STREQUAL \"Xcode\"" OFF)
Expand Down Expand Up @@ -610,6 +612,8 @@ elseif(LIBSWIFT_BUILD_MODE MATCHES "BOOTSTRAPPING.*")
set(SWIFT_EXEC_FOR_LIBSWIFT "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swiftc")
if(LIBSWIFT_BUILD_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS")
set(LIBSWIFT_BUILD_MODE "CROSSCOMPILE-WITH-HOSTLIBS")
elseif(LIBSWIFT_BUILD_MODE STREQUAL "BOOTSTRAPPING")
set(LIBSWIFT_BUILD_MODE "CROSSCOMPILE")
else()
set(LIBSWIFT_BUILD_MODE "HOSTTOOLS")
endif()
Expand Down
38 changes: 11 additions & 27 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,8 @@ function(add_libswift name)
# The compiler will instead pick up the stdlib from the SDK.
get_filename_component(swift_exec_bin_dir ${ALS_SWIFT_EXEC} DIRECTORY)
set(sdk_option ${sdk_option} "-resource-dir" "${swift_exec_bin_dir}/../bootstrapping0/lib/swift")
elseif(${LIBSWIFT_BUILD_MODE} STREQUAL "CROSSCOMPILE")
set(sdk_option ${sdk_option} "-resource-dir" "${build_dir}/../bootstrapping1/lib/swift")
endif()
endif()
get_versioned_target_triple(target ${SWIFT_HOST_VARIANT_SDK}
Expand Down Expand Up @@ -789,7 +791,6 @@ function(add_libswift name)
# Create a static libswift library containing all module object files.
add_library(${name} STATIC ${all_obj_files})
set_target_properties(${name} PROPERTIES LINKER_LANGUAGE CXX)
set_property(GLOBAL APPEND PROPERTY SWIFT_BUILDTREE_EXPORTS ${name})
endfunction()

macro(add_swift_tool_subdirectory name)
Expand Down Expand Up @@ -917,19 +918,7 @@ function(add_swift_host_tool executable)
# Include the abi stable system stdlib in our rpath.
list(APPEND RPATH_LIST "/usr/lib/swift")

elseif(LIBSWIFT_BUILD_MODE STREQUAL "BOOTSTRAPPING")
# Pick up the built libswiftCompatibility<n>.a libraries
_link_built_compatibility_libs(${executable})

# At build time link against the built swift libraries from the
# previous bootstrapping stage.
get_bootstrapping_swift_lib_dir(bs_lib_dir "${ASHT_BOOTSTRAPPING}")
target_link_directories(${executable} PRIVATE ${bs_lib_dir})

# At runtime link against the built swift libraries from the current
# bootstrapping stage.
list(APPEND RPATH_LIST "@executable_path/../lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
else()
elseif(NOT LIBSWIFT_BUILD_MODE MATCHES "CROSSCOMPILE|BOOTSTRAPPING")
message(FATAL_ERROR "Unknown LIBSWIFT_BUILD_MODE '${LIBSWIFT_BUILD_MODE}'")
endif()

Expand Down Expand Up @@ -991,24 +980,19 @@ function(add_swift_host_tool executable)
INSTALL_RPATH "${host_lib_dir}")

elseif(LIBSWIFT_BUILD_MODE STREQUAL "BOOTSTRAPPING")
# At build time link against the built swift libraries from the
# previous bootstrapping stage.
if (NOT "${ASHT_BOOTSTRAPPING}" STREQUAL "0")
get_bootstrapping_swift_lib_dir(bs_lib_dir "${ASHT_BOOTSTRAPPING}")
target_link_directories(${executable} PRIVATE ${bs_lib_dir})
target_link_libraries(${executable} PRIVATE ${swiftrt})
target_link_libraries(${executable} PRIVATE "swiftCore")
if(SWIFT_PATH_TO_LIBICU_BUILD)
set(icu_dir "${SWIFT_PATH_TO_LIBICU_BUILD}/lib")
target_link_libraries(${executable} PRIVATE
${icu_dir}/libicui18nswift.a
${icu_dir}/libicuucswift.a
${icu_dir}/libicudataswift.a)
endif()
endif()

# At runtime link against the built swift libraries from the current
# bootstrapping stage.
set_target_properties(${executable} PROPERTIES
BUILD_WITH_INSTALL_RPATH YES
INSTALL_RPATH "$ORIGIN/../lib/swift/${SWIFT_SDK_LINUX_LIB_SUBDIR}")

elseif(LIBSWIFT_BUILD_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS")
message(FATAL_ERROR "LIBSWIFT_BUILD_MODE 'BOOTSTRAPPING-WITH-HOSTLIBS' not supported on Linux")
else()
elseif(NOT LIBSWIFT_BUILD_MODE STREQUAL "BOOTSTRAPPING")
message(FATAL_ERROR "Unknown LIBSWIFT_BUILD_MODE '${LIBSWIFT_BUILD_MODE}'")
endif()
endif()
Expand Down
2 changes: 1 addition & 1 deletion cmake/modules/SwiftUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function(get_bootstrapping_swift_lib_dir bs_lib_dir bootstrapping)
set(bs_lib_dir "")
if(LIBSWIFT_BUILD_MODE STREQUAL "BOOTSTRAPPING")
set(lib_dir
"${SWIFTLIB_DIR}/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
"${SWIFTSTATICLIB_DIR}/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
# If building the stdlib with bootstrapping, the compiler has to pick up
# the libswift of the previous bootstrapping level (because in the current
# lib-directory it's not built yet.
Expand Down
52 changes: 32 additions & 20 deletions libswift/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,54 @@ else()

add_subdirectory(Sources)

if(${LIBSWIFT_BUILD_MODE} MATCHES "HOSTTOOLS|CROSSCOMPILE-WITH-HOSTLIBS")
set(bs1_libs)
set(final_libs)
if(${LIBSWIFT_BUILD_MODE} MATCHES "BOOTSTRAPPING|CROSSCOMPILE")
set(bs1_libs ${bs1_libs} swiftCore-bootstrapping0-static)
set(final_libs ${final_libs} swiftCore-bootstrapping1-static)
set_property(GLOBAL APPEND PROPERTY SWIFT_BUILDTREE_EXPORTS swiftCore-bootstrapping1-static)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(bs1_libs ${bs1_libs} swiftOnoneSupport-bootstrapping0-static)
set(final_libs ${final_libs} swiftOnoneSupport-bootstrapping1-static)
set_property(GLOBAL APPEND PROPERTY SWIFT_BUILDTREE_EXPORTS swiftSwiftOnoneSupport-bootstrapping1-static)
endif()
if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
set(bs1_libs ${bs1_libs} swiftDarwin-bootstrapping0-static)
set(final_libs ${final_libs} swiftDarwin-bootstrapping1-static)
set_property(GLOBAL APPEND PROPERTY SWIFT_BUILDTREE_EXPORTS swiftDarwin-bootstrapping1-static)
endif()
endif()


if(${LIBSWIFT_BUILD_MODE} MATCHES "HOSTTOOLS|CROSSCOMPILE.*")

if (NOT SWIFT_EXEC_FOR_LIBSWIFT)
message(FATAL_ERROR "Need a swift toolchain for building libswift")
endif()

add_libswift("libswift"
SWIFT_EXEC "${SWIFT_EXEC_FOR_LIBSWIFT}")
SWIFT_EXEC "${SWIFT_EXEC_FOR_LIBSWIFT}"
DEPENDS ${final_libs})

elseif(${LIBSWIFT_BUILD_MODE} MATCHES "BOOTSTRAPPING.*")
target_link_libraries(libswift PUBLIC ${final_libs})

set(b0_deps swift-frontend-bootstrapping0 symlink-headers-bootstrapping0)
set(b1_deps swift-frontend-bootstrapping1 symlink-headers-bootstrapping1)
if(${LIBSWIFT_BUILD_MODE} STREQUAL "BOOTSTRAPPING")
set(b0_deps ${b0_deps} swiftCore-bootstrapping0)
set(b1_deps ${b1_deps} swiftCore-bootstrapping1)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(b0_deps ${b0_deps} swiftOnoneSupport-bootstrapping0)
set(b1_deps ${b1_deps} swiftOnoneSupport-bootstrapping1)
endif()
if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
set(b0_deps ${b0_deps} swiftDarwin-bootstrapping0)
set(b1_deps ${b1_deps} swiftDarwin-bootstrapping1)
endif()
endif()
elseif(${LIBSWIFT_BUILD_MODE} MATCHES "BOOTSTRAPPING.*")

# Bootstrapping - stage 1, using the compiler from level 0

add_libswift("libswift-bootstrapping1"
SWIFT_EXEC "${CMAKE_BINARY_DIR}/bootstrapping0/bin/swiftc"
DEPENDS ${b0_deps}
DEPENDS swift-frontend-bootstrapping0 symlink-headers-bootstrapping0 ${bs1_libs}
BOOTSTRAPPING 1)

# The final build, using the compiler from stage 1

add_libswift("libswift"
SWIFT_EXEC "${CMAKE_BINARY_DIR}/bootstrapping1/bin/swiftc"
DEPENDS ${b1_deps})
DEPENDS swift-frontend-bootstrapping0 symlink-headers-bootstrapping0 ${final_libs})

target_link_libraries(libswift-bootstrapping1 PUBLIC ${bs1_libs})
target_link_libraries(libswift PUBLIC ${final_libs})

if(LIBSWIFT_BUILD_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS")
file(GLOB module_dirs "${CMAKE_BINARY_DIR}/bootstrapping*/lib/swift/macosx/*.swiftmodule")
Expand All @@ -69,5 +79,7 @@ else()
else()
message(FATAL_ERROR "Unknown LIBSWIFT_BUILD_MODE '${LIBSWIFT_BUILD_MODE}'")
endif()

set_property(GLOBAL APPEND PROPERTY SWIFT_BUILDTREE_EXPORTS libswift)
endif()

34 changes: 10 additions & 24 deletions stdlib/cmake/modules/SwiftSource.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,9 @@ function(_compile_swift_files
endif()

if(SWIFTFILE_IS_SDK_OVERLAY)
list(APPEND swift_flags "-autolink-force-load")
if("${SWIFTFILE_BOOTSTRAPPING}" STREQUAL "")
list(APPEND swift_flags "-autolink-force-load")
endif()
endif()

# Don't need to link runtime compatibility libraries for older runtimes
Expand Down Expand Up @@ -538,6 +540,7 @@ function(_compile_swift_files
set(interface_file_static)

get_bootstrapping_path(lib_dir ${SWIFTLIB_DIR} "${SWIFTFILE_BOOTSTRAPPING}")
get_bootstrapping_path(static_lib_dir ${SWIFTSTATICLIB_DIR} "${SWIFTFILE_BOOTSTRAPPING}")

if(NOT SWIFTFILE_IS_MAIN)
# Determine the directory where the module file should be placed.
Expand All @@ -553,7 +556,7 @@ function(_compile_swift_files

set(module_base "${module_dir}/${SWIFTFILE_MODULE_NAME}")

set(module_dir_static "${SWIFTSTATICLIB_DIR}/${library_subdir}")
set(module_dir_static "${static_lib_dir}/${library_subdir}")
set(module_base_static "${module_dir_static}/${SWIFTFILE_MODULE_NAME}")

set(module_triple ${SWIFT_SDK_${library_subdir_sdk}_ARCH_${SWIFTFILE_ARCHITECTURE}_MODULE})
Expand Down Expand Up @@ -690,11 +693,11 @@ function(_compile_swift_files
else()
set(swift_compiler_tool "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swiftc${HOST_EXECUTABLE_SUFFIX}")

get_bootstrapping_path(swift_compiler_tool
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swiftc${HOST_EXECUTABLE_SUFFIX}"
"${SWIFTFILE_BOOTSTRAPPING}")
if(NOT ${SWIFTFILE_BOOTSTRAPPING} STREQUAL "" AND NOT ${LIBSWIFT_BUILD_MODE} MATCHES "CROSSCOMPILE.*")
get_bootstrapping_path(swift_compiler_tool
"${swift_compiler_tool}"
"${SWIFTFILE_BOOTSTRAPPING}")

if(NOT ${SWIFTFILE_BOOTSTRAPPING} STREQUAL "")
set(target_suffix "-bootstrapping${SWIFTFILE_BOOTSTRAPPING}")
endif()

Expand All @@ -704,7 +707,7 @@ function(_compile_swift_files
endif()

set(swift_compiler_tool_dep)
if(SWIFT_INCLUDE_TOOLS)
if(SWIFT_INCLUDE_TOOLS AND NOT ${LIBSWIFT_BUILD_MODE} MATCHES "CROSSCOMPILE.*")
# Depend on the binary itself, in addition to the symlink.
set(swift_compiler_tool_dep "swift-frontend${target_suffix}")
endif()
Expand All @@ -726,23 +729,6 @@ function(_compile_swift_files
set(swift_compiler_tool "${SWIFT_SOURCE_DIR}/utils/check-incremental" "${swift_compiler_tool}")
endif()

if(SWIFTFILE_IS_STDLIB)
get_bootstrapping_swift_lib_dir(bs_lib_dir "${SWIFTFILE_BOOTSTRAPPING}")
if(bs_lib_dir)
# When building the stdlib with libswift 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}")
elseif(SWIFT_HOST_VARIANT_SDK STREQUAL "LINUX")
set(set_environment_args "${CMAKE_COMMAND}" "-E" "env" "LD_LIBRARY_PATH=${bs_lib_dir}")
else()
message(FATAL_ERROR "TODO: bootstrapping support for ${SWIFT_HOST_VARIANT_SDK}")
endif()
endif()

endif()

if (SWIFT_REPORT_STATISTICS)
list(GET dirs_to_create 0 first_obj_dir)
list(APPEND swift_flags "-stats-output-dir" ${first_obj_dir})
Expand Down
3 changes: 2 additions & 1 deletion stdlib/public/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ if(SWIFT_BUILD_STDLIB OR SWIFT_BUILD_REMOTE_MIRROR)
add_subdirectory(SwiftRemoteMirror)
endif()

if(SWIFT_BUILD_SDK_OVERLAY OR SWIFT_BUILD_TEST_SUPPORT_MODULES)
if(SWIFT_BUILD_SDK_OVERLAY OR SWIFT_BUILD_TEST_SUPPORT_MODULES OR
${LIBSWIFT_BUILD_MODE} MATCHES "BOOTSTRAPPING|CROSSCOMPILE")
add_subdirectory(Platform)
endif()

Expand Down
8 changes: 6 additions & 2 deletions stdlib/public/Platform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ set(swiftDarwin_common_options
DEPENDS ${darwin_depends})


if(${LIBSWIFT_BUILD_MODE} STREQUAL "BOOTSTRAPPING" AND
if(${LIBSWIFT_BUILD_MODE} MATCHES "BOOTSTRAPPING|CROSSCOMPILE" AND
${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_DARWIN_PLATFORMS)

set(swiftDarwin_common_bootstrapping_options
${swiftDarwin_common_options}
SHARED
STATIC
IS_STDLIB
SDK ${SWIFT_HOST_VARIANT_SDK}
ARCHITECTURE ${SWIFT_HOST_VARIANT_ARCH}
Expand All @@ -71,6 +71,10 @@ if(${LIBSWIFT_BUILD_MODE} STREQUAL "BOOTSTRAPPING" AND
BOOTSTRAPPING 1)
endif()

if(NOT SWIFT_BUILD_SDK_OVERLAY AND NOT SWIFT_BUILD_TEST_SUPPORT_MODULES)
return()
endif()

add_swift_target_library(swiftDarwin ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES}
${swiftDarwin_common_options}
TARGET_SDKS "${swiftDarwin_target_sdks}"
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/SwiftOnoneSupport/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ set(swiftOnoneSupport_common_options
SWIFT_COMPILE_FLAGS "-parse-stdlib" "-Xllvm" "-sil-inline-generics=false" "-Xfrontend" "-validate-tbd-against-ir=none" "-Xfrontend" "-check-onone-completeness" "-Xfrontend" "-disable-access-control" "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}" "${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}"
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}")

if(CMAKE_BUILD_TYPE STREQUAL "Debug" AND ${LIBSWIFT_BUILD_MODE} STREQUAL "BOOTSTRAPPING")
if(CMAKE_BUILD_TYPE STREQUAL "Debug" AND ${LIBSWIFT_BUILD_MODE} MATCHES "BOOTSTRAPPING|CROSSCOMPILE")

set(swiftOnoneSupport_common_bootstrapping_options
SHARED
STATIC
${swiftOnoneSupport_common_options}
SDK ${SWIFT_HOST_VARIANT_SDK}
ARCHITECTURE ${SWIFT_HOST_VARIANT_ARCH}
Expand Down
29 changes: 16 additions & 13 deletions stdlib/public/SwiftShims/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

set(sources
AssertionReporting.h
CoreFoundationShims.h
Expand Down Expand Up @@ -83,19 +84,21 @@ endif()
# First extract the "version" used for Clang's resource directory.
string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
"${LLVM_PACKAGE_VERSION}")
if(SWIFT_COMPILER_IS_MSVC_LIKE)
execute_process(COMMAND ${CMAKE_C_COMPILER} /clang:-print-resource-dir
OUTPUT_VARIABLE host_clang_headers_location
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET)
else()
execute_process(COMMAND ${CMAKE_C_COMPILER} -print-resource-dir
OUTPUT_VARIABLE host_clang_headers_location
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET)
endif()

if(NOT SWIFT_INCLUDE_TOOLS AND
(SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER OR SWIFT_PREBUILT_CLANG))
if(SWIFT_COMPILER_IS_MSVC_LIKE)
execute_process(COMMAND ${CMAKE_C_COMPILER} /clang:-print-resource-dir
OUTPUT_VARIABLE clang_headers_location
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET)
else()
execute_process(COMMAND ${CMAKE_C_COMPILER} -print-resource-dir
OUTPUT_VARIABLE clang_headers_location
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET)
endif()
set(clang_headers_location "${host_clang_headers_location}")
message(STATUS "Using clang Resource Directory: ${clang_headers_location}")
else()
set(clang_headers_location "${LLVM_LIBRARY_OUTPUT_INTDIR}/clang/${CLANG_VERSION}")
Expand Down Expand Up @@ -156,7 +159,7 @@ if(NOT SWIFT_BUILT_STANDALONE)
endforeach()
endif()

if(${LIBSWIFT_BUILD_MODE} MATCHES "BOOTSTRAPPING.*")
if(${LIBSWIFT_BUILD_MODE} MATCHES "CROSSCOMPILE|BOOTSTRAPPING.*")
foreach(bootstrapping "0" "1")
get_bootstrapping_path(outdir ${SWIFTLIB_DIR} ${bootstrapping})
set(target_name "symlink-headers-bootstrapping${bootstrapping}")
Expand All @@ -169,7 +172,7 @@ if(${LIBSWIFT_BUILD_MODE} MATCHES "BOOTSTRAPPING.*")
${output_dir} "${outdir}/shims"
COMMAND
"${CMAKE_COMMAND}" "-E" "${cmake_symlink_option}"
"${clang_headers_location}"
"${host_clang_headers_location}"
"${outdir}/clang"

CUSTOM_TARGET_NAME ${target_name}
Expand Down
Loading