Skip to content

[5.10][CMake] Replace early swift-syntax with FetchContent #68882

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

Merged
merged 7 commits into from
Oct 10, 2023
Merged
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
73 changes: 47 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ include(CMakeDependentOption)
include(CheckLanguage)
include(GNUInstallDirs)
include(SwiftImplicitImport)
include(FetchContent)

# Enable Swift for the host compiler build if we have the language. It is
# optional until we have a bootstrap story.
Expand Down Expand Up @@ -691,10 +692,9 @@ if(CMAKE_C_COMPILER_ID MATCHES Clang)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Werror=c++98-compat-extra-semi>)
endif()

# Make sure we know where swift-syntax is because we need it to build the parser.
if(NOT EXISTS "${SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE}")
message(SEND_ERROR "swift-syntax is required to build the Swift compiler. Please run update-checkout or specify SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE")
endif()
option(SWIFT_BUILD_SWIFT_SYNTAX
"Enable building swift syntax"
FALSE)

set(SWIFT_BUILD_HOST_DISPATCH FALSE)
if(SWIFT_ENABLE_DISPATCH AND NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
Expand Down Expand Up @@ -794,7 +794,6 @@ include(SwiftConfigureSDK)
include(SwiftComponents)
include(SwiftList)
include(AddPureSwift)
include(SetRPATH)

# Configure swift include, install, build components.
swift_configure_components()
Expand Down Expand Up @@ -830,14 +829,22 @@ elseif(BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*")
else()
set(BOOTSTRAPPING_MODE "HOSTTOOLS")
endif()
elseif(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS" OR SWIFT_SWIFT_PARSER)
elseif(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS" OR SWIFT_BUILD_SWIFT_SYNTAX)
# We are building using a pre-installed host toolchain but not bootstrapping
# the Swift modules. This happens when building using 'build-tooling-libs'
# where we haven't built a new Swift compiler. Use the Swift compiler from the
# pre-installed host toolchain to build the Swift modules.
set(SWIFT_EXEC_FOR_SWIFT_MODULES "${CMAKE_Swift_COMPILER}")
endif()

if(SWIFT_BUILD_SWIFT_SYNTAX)
# Only "HOSTTOOLS" is supported in Linux when Swift parser integration is enabled.
if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD" AND NOT BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS")
message(WARNING "Force setting BOOTSTRAPPING=HOSTTOOLS because Swift parser integration is enabled")
set(BOOTSTRAPPING_MODE "HOSTTOOLS")
endif()
endif()

if(BOOTSTRAPPING_MODE MATCHES "HOSTTOOLS|.*-WITH-HOSTLIBS")
if(SWIFT_ENABLE_ARRAY_COW_CHECKS)
message(STATUS "array COW checks disabled when building the swift modules with host libraries")
Expand Down Expand Up @@ -944,25 +951,6 @@ if(XCODE)
set(SWIFT_SDKS "OSX")
endif()

# When we have the early SwiftSyntax build, we can include its parser.
if(SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR)
set(SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS
${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/cmake/SwiftSyntaxTargets.cmake)
if(NOT EXISTS "${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS}")
message(STATUS "Skipping Swift Swift parser integration due to missing early SwiftSyntax")
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()


# FIXME: the parameters we specify in SWIFT_SDKS are lacking architecture specifics,
# so we need to hard-code it. For example, the SDK for Android is just 'ANDROID',
# and we have to specify SWIFT_SDK_ANDROID_ARCHITECTURES separately.
Expand Down Expand Up @@ -1171,13 +1159,17 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin" AND NOT CMAKE_CROSSCOMPILING)
set(CMAKE_OSX_DEPLOYMENT_TARGET "")
endif()

swift_get_host_triple(SWIFT_HOST_TRIPLE)
set(SWIFT_HOST_MODULE_TRIPLE "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_MODULE}")
set(SWIFT_HOST_LIBRARIES_DEST_DIR "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/swift/host")

if(SWIFT_INCLUDE_TOOLS)
message(STATUS "Building host Swift tools for ${SWIFT_HOST_VARIANT_SDK} ${SWIFT_HOST_VARIANT_ARCH}")
message(STATUS " Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS " Assertions: ${LLVM_ENABLE_ASSERTIONS}")
message(STATUS " LTO: ${SWIFT_TOOLS_ENABLE_LTO}")
message(STATUS " Bootstrapping: ${BOOTSTRAPPING_MODE}")
message(STATUS " Swift parser: ${SWIFT_SWIFT_PARSER}")
message(STATUS " Swift parser: ${SWIFT_BUILD_SWIFT_SYNTAX}")
message(STATUS "")
else()
message(STATUS "Not building host Swift tools")
Expand Down Expand Up @@ -1323,6 +1315,35 @@ endif()
add_subdirectory(include)

if(SWIFT_INCLUDE_TOOLS)
# Include 'swift-syntax'.
# This is a function because we want to set some 'CMAKE_*' variables temporarily.'
# TODO: Replace this with 'block()' after CMake 3.25
function(include_swift_syntax)
if(NOT SWIFT_BUILD_SWIFT_SYNTAX)
return()
endif()
if(NOT EXISTS "${SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE}")
message(SEND_ERROR "swift-syntax is required to build the Swift compiler. Please run update-checkout or specify SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE")
return()
endif()

set(CMAKE_Swift_COMPILER_TARGET ${SWIFT_HOST_TRIPLE})
set(BUILD_SHARED_LIBS ON)
# All libraries in 'swift-syntax' goes to 'lib/swift/host'.
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${SWIFT_HOST_LIBRARIES_DEST_DIR}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${SWIFT_HOST_LIBRARIES_DEST_DIR}")
if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD")
set(SWIFT_HOST_LIBRARIES_RPATH "$ORIGIN;$ORIGIN/../${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
endif()

file(TO_CMAKE_PATH "${SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE}" swift_syntax_path)
FetchContent_Declare(SwiftSyntax
SOURCE_DIR "${swift_syntax_path}"
)
FetchContent_MakeAvailable(SwiftSyntax)
endfunction()
include_swift_syntax()

add_subdirectory(lib)

# SwiftCompilerSources must come before "tools".
Expand Down
56 changes: 28 additions & 28 deletions cmake/modules/AddPureSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ include(macCatalystUtils)

# Workaround a cmake bug, see the corresponding function in swift-syntax
function(force_target_link_libraries TARGET)
cmake_parse_arguments(ARGS "" "" "PUBLIC" ${ARGN})

foreach(DEPENDENCY ${ARGS_PUBLIC})
target_link_libraries(${TARGET} PRIVATE ${DEPENDENCY})
add_dependencies(${TARGET} ${DEPENDENCY})
target_link_libraries(${TARGET} ${ARGN})

cmake_parse_arguments(ARGS "PUBLIC;PRIVATE;INTERFACE" "" "" ${ARGN})
foreach(DEPENDENCY ${ARGS_UNPARSED_ARGUMENTS})
string(REGEX REPLACE [<>:\"/\\|?*] _ sanitized ${DEPENDENCY})
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
DEPENDS ${DEPENDENCY}
)
)
target_sources(${TARGET} PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
)
Expand Down Expand Up @@ -46,21 +44,7 @@ function(_add_host_swift_compile_options name)
$<$<COMPILE_LANGUAGE:Swift>:-runtime-compatibility-version>
$<$<COMPILE_LANGUAGE:Swift>:none>)

# Set the appropriate target triple.
# FIXME: This should be set by CMake.
if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
set(DEPLOYMENT_VERSION "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
endif()

if(SWIFT_HOST_VARIANT_SDK STREQUAL "ANDROID")
set(DEPLOYMENT_VERSION ${SWIFT_ANDROID_API_LEVEL})
endif()

get_target_triple(target target_variant "${SWIFT_HOST_VARIANT_SDK}" "${SWIFT_HOST_VARIANT_ARCH}"
MACCATALYST_BUILD_FLAVOR ""
DEPLOYMENT_VERSION "${DEPLOYMENT_VERSION}")

target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:Swift>:-target;${target}>)
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:Swift>:-target;${SWIFT_HOST_TRIPLE}>)
_add_host_variant_swift_sanitizer_flags(${name})
endfunction()

Expand Down Expand Up @@ -121,7 +105,7 @@ endfunction()
# source1 ...
# Sources to add into this library.
function(add_pure_swift_host_library name)
if (NOT SWIFT_SWIFT_PARSER)
if (NOT SWIFT_BUILD_SWIFT_SYNTAX)
message(STATUS "Not building ${name} because swift-syntax is not available")
return()
endif()
Expand Down Expand Up @@ -196,13 +180,15 @@ function(add_pure_swift_host_library name)

# Make sure we can use the host libraries.
target_include_directories(${name} PUBLIC
${SWIFT_HOST_LIBRARIES_DEST_DIR})
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
target_link_directories(${name} PUBLIC
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")

if(APSHL_EMIT_MODULE)
# Determine where Swift modules will be built and installed.

set(module_triple ${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_MODULE})
set(module_dir ${SWIFT_HOST_LIBRARIES_DEST_DIR})
set(module_triple "${SWIFT_HOST_MODULE_TRIPLE}")
set(module_dir "${SWIFT_HOST_LIBRARIES_DEST_DIR}")
set(module_base "${module_dir}/${name}.swiftmodule")
set(module_file "${module_base}/${module_triple}.swiftmodule")
set(module_interface_file "${module_base}/${module_triple}.swiftinterface")
Expand Down Expand Up @@ -234,14 +220,20 @@ function(add_pure_swift_host_library name)
>)
endif()

if(LLVM_USE_LINKER)
target_link_options(${name} PRIVATE
"-use-ld=${LLVM_USE_LINKER}"
)
endif()

# Export this target.
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name})
endfunction()

# Add a new "pure" Swift host tool.
#
# "Pure" Swift host tools can only contain Swift code, and will be built
# with the host compiler.
# with the host compiler.
#
# Usage:
# add_pure_swift_host_tool(name
Expand All @@ -262,7 +254,7 @@ endfunction()
# source1 ...
# Sources to add into this tool.
function(add_pure_swift_host_tool name)
if (NOT SWIFT_SWIFT_PARSER)
if (NOT SWIFT_BUILD_SWIFT_SYNTAX)
message(STATUS "Not building ${name} because swift-syntax is not available")
return()
endif()
Expand Down Expand Up @@ -322,7 +314,15 @@ function(add_pure_swift_host_tool name)

# Make sure we can use the host libraries.
target_include_directories(${name} PUBLIC
${SWIFT_HOST_LIBRARIES_DEST_DIR})
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
target_link_directories(${name} PUBLIC
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")

if(LLVM_USE_LINKER)
target_link_options(${name} PRIVATE
"-use-ld=${LLVM_USE_LINKER}"
)
endif()

# Workaround to touch the library and its objects so that we don't
# continually rebuild (again, see corresponding change in swift-syntax).
Expand Down
42 changes: 22 additions & 20 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ function(_add_host_variant_swift_sanitizer_flags target)
endif()
endfunction()

# Usage:
# _add_host_variant_c_compile_link_flags(name)
function(_add_host_variant_c_compile_link_flags name)
function(swift_get_host_triple out_var)
if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
set(DEPLOYMENT_VERSION "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
endif()
Expand All @@ -123,26 +121,30 @@ function(_add_host_variant_c_compile_link_flags name)
set(DEPLOYMENT_VERSION ${SWIFT_ANDROID_API_LEVEL})
endif()

get_target_triple(target target_variant "${SWIFT_HOST_VARIANT_SDK}" "${SWIFT_HOST_VARIANT_ARCH}"
MACCATALYST_BUILD_FLAVOR ""
DEPLOYMENT_VERSION "${DEPLOYMENT_VERSION}")

set(${out_var} "${target}" PARENT_SCOPE)
endfunction()

# Usage:
# _add_host_variant_c_compile_link_flags(name)
function(_add_host_variant_c_compile_link_flags name)
# MSVC and gcc don't understand -target.
# clang-cl understands --target.
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
get_target_triple(target target_variant "${SWIFT_HOST_VARIANT_SDK}" "${SWIFT_HOST_VARIANT_ARCH}"
MACCATALYST_BUILD_FLAVOR ""
DEPLOYMENT_VERSION "${DEPLOYMENT_VERSION}")
if("${CMAKE_C_COMPILER_FRONTEND_VARIANT}" STREQUAL "MSVC") # clang-cl options
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:--target=${target}>)
target_link_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:--target=${target}>)
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:--target=${SWIFT_HOST_TRIPLE}>)
target_link_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:--target=${SWIFT_HOST_TRIPLE}>)
else()
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:-target;${target}>)
target_link_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:-target;${target}>)
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:-target;${SWIFT_HOST_TRIPLE}>)
target_link_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:-target;${SWIFT_HOST_TRIPLE}>)
endif()
endif()

if (CMAKE_Swift_COMPILER)
get_target_triple(target target_variant "${SWIFT_HOST_VARIANT_SDK}" "${SWIFT_HOST_VARIANT_ARCH}"
MACCATALYST_BUILD_FLAVOR ""
DEPLOYMENT_VERSION "${DEPLOYMENT_VERSION}")
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:Swift>:-target;${target}>)
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:Swift>:-target;${SWIFT_HOST_TRIPLE}>)

_add_host_variant_swift_sanitizer_flags(${name})
endif()
Expand Down Expand Up @@ -442,7 +444,7 @@ endfunction()

function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
if(NOT BOOTSTRAPPING_MODE)
if (SWIFT_SWIFT_PARSER)
if (SWIFT_BUILD_SWIFT_SYNTAX)
set(ASRLF_BOOTSTRAPPING_MODE "HOSTTOOLS")
else()
return()
Expand Down Expand Up @@ -579,7 +581,7 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
${SWIFT_PATH_TO_SWIFT_SDK}/usr/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}/${SWIFT_HOST_VARIANT_ARCH})
endif()

if(SWIFT_SWIFT_PARSER)
if(SWIFT_BUILD_SWIFT_SYNTAX)
# 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.*")
Expand Down Expand Up @@ -657,7 +659,7 @@ function(add_swift_host_library name)
translate_flags(ASHL "${options}")

# Once the new Swift parser is linked, everything has Swift modules.
if (SWIFT_SWIFT_PARSER AND ASHL_SHARED)
if (SWIFT_BUILD_SWIFT_SYNTAX AND ASHL_SHARED)
set(ASHL_HAS_SWIFT_MODULES ON)
endif()

Expand Down Expand Up @@ -703,7 +705,7 @@ function(add_swift_host_library name)

add_library(${name} ${libkind} ${ASHL_SOURCES})

target_link_directories(${name} PUBLIC ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
target_link_directories(${name} PUBLIC "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")

# Respect LLVM_COMMON_DEPENDS if it is set.
#
Expand Down Expand Up @@ -925,7 +927,7 @@ 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 AND NOT ASHT_DOES_NOT_USE_SWIFT)
if (SWIFT_BUILD_SWIFT_SYNTAX AND NOT ASHT_DOES_NOT_USE_SWIFT)
set(ASHT_HAS_SWIFT_MODULES ON)
endif()

Expand Down Expand Up @@ -964,7 +966,7 @@ function(add_swift_host_tool executable)
endif()
endif()

if(SWIFT_SWIFT_PARSER)
if(SWIFT_BUILD_SWIFT_SYNTAX)
set(extra_relative_rpath "")
if(NOT "${ASHT_BOOTSTRAPPING}" STREQUAL "")
if(executable MATCHES "-bootstrapping")
Expand Down
27 changes: 24 additions & 3 deletions cmake/modules/AddSwiftUnittests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,31 @@ function(add_swift_unittest test_dirname)
endif()
endif()

if (SWIFT_SWIFT_PARSER AND NOT ASU_IS_TARGET_TEST)
file(RELATIVE_PATH relative_lib_path "${CMAKE_CURRENT_BINARY_DIR}" "${SWIFT_LIBRARY_OUTPUT_INTDIR}")

if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
set_property(
TARGET ${test_dirname}
APPEND PROPERTY INSTALL_RPATH "@executable_path/${relative_lib_path}")
elseif(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD")
set_property(
TARGET ${test_dirname}
APPEND PROPERTY INSTALL_RPATH "$ORIGIN/${relative_lib_path}")
endif()

if (SWIFT_BUILD_SWIFT_SYNTAX 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)
_add_swift_runtime_link_flags(${test_dirname} "${relative_lib_path}" "")

if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
set_property(
TARGET ${test_dirname}
APPEND PROPERTY INSTALL_RPATH "@executable_path/${relative_lib_path}/swift/host")
elseif(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD")
set_property(
TARGET ${test_dirname}
APPEND PROPERTY INSTALL_RPATH "$ORIGIN/${relative_lib_path}/swift/host")
endif()
endif()
endfunction()

Loading