CMake: option to disable swift in swift

Adding `SWIFT_ENABLE_SWIFT_IN_SWIFT` option to enable or disable the
parts of Swift that require a Swift compiler to build. This is meant for
bootstrapping compilers on new platforms and is not guaranteed to result
in a compiler that will pass the test suite.

This option is on by default so that folks won't forget.
If the option is off, the resulting compiler does not include the Swift
optimizer sources in SwiftCompilerSources nor does the resulting
compiler have swift macro support.
This commit is contained in:
Evan Wilde
2025-03-04 16:06:56 -08:00
parent 157e71a791
commit eeee0dfddd
4 changed files with 92 additions and 69 deletions

View File

@@ -352,17 +352,24 @@ option(SWIFT_TOOLS_LD64_LTO_CODEGEN_ONLY_FOR_SUPPORTING_TARGETS
debugging Swift)"
FALSE)
set(BOOTSTRAPPING_MODE HOSTTOOLS CACHE STRING [=[
How to build the swift compiler modules. Possible values are
HOSTTOOLS: build with a pre-installed toolchain
BOOTSTRAPPING: build with a 2-stage bootstrapping process
BOOTSTRAPPING-WITH-HOSTLIBS: build with a 2-stage bootstrapping process,
but the compiler links against the host system swift libs (macOS only)
CROSSCOMPILE: cross-compiledwith a native host compiler, provided in
`SWIFT_NATIVE_SWIFT_TOOLS_PATH` (non-Darwin only)
CROSSCOMPILE-WITH-HOSTLIBS: build with a bootstrapping-with-hostlibs compiled
compiler, provided in `SWIFT_NATIVE_SWIFT_TOOLS_PATH`
]=])
option(SWIFT_ENABLE_SWIFT_IN_SWIFT "Enable Swift sources in Swift compiler" ON)
if(SWIFT_ENABLE_SWIFT_IN_SWIFT)
set(BOOTSTRAPPING_MODE HOSTTOOLS CACHE STRING [=[
How to build the swift compiler modules. Possible values are
HOSTTOOLS: build with a pre-installed toolchain
BOOTSTRAPPING: build with a 2-stage bootstrapping process
BOOTSTRAPPING-WITH-HOSTLIBS: build with a 2-stage bootstrapping process,
but the compiler links against the host system swift libs (macOS only)
CROSSCOMPILE: cross-compiledwith a native host compiler, provided in
`SWIFT_NATIVE_SWIFT_TOOLS_PATH` (non-Darwin only)
CROSSCOMPILE-WITH-HOSTLIBS: build with a bootstrapping-with-hostlibs compiled
compiler, provided in `SWIFT_NATIVE_SWIFT_TOOLS_PATH`
]=])
else()
set(BOOTSTRAPPING_MODE OFF)
set(SWIFT_BUILD_SWIFT_SYNTAX OFF)
endif()
option(BRIDGING_MODE [=[
How swift-C++ bridging code is compiled:
@@ -939,41 +946,44 @@ set(SWIFT_MAIN_INCLUDE_DIR "${SWIFT_SOURCE_DIR}/include")
set(SWIFT_SHIMS_INCLUDE_DIR "${SWIFT_SOURCE_DIR}/stdlib/public/SwiftShims")
set(SWIFT_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/include")
if (NOT BOOTSTRAPPING_MODE)
if (NOT BOOTSTRAPPING_MODE AND SWIFT_ENABLE_SWIFT_IN_SWIFT)
message(FATAL_ERROR "turning off bootstrapping is not supported anymore")
endif()
set(SWIFT_RUNTIME_OUTPUT_INTDIR "${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin")
set(SWIFT_LIBRARY_OUTPUT_INTDIR "${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib")
if("${SWIFT_NATIVE_SWIFT_TOOLS_PATH}" STREQUAL "")
# This is the normal case. We are not cross-compiling.
set(SWIFT_NATIVE_SWIFT_TOOLS_PATH "${SWIFT_RUNTIME_OUTPUT_INTDIR}")
set(SWIFT_EXEC_FOR_SWIFT_MODULES "${CMAKE_Swift_COMPILER}")
if(NOT SWIFT_EXEC_FOR_SWIFT_MODULES)
message(WARNING "BOOTSTRAPPING set to OFF because no Swift compiler is defined")
set(BOOTSTRAPPING_MODE "OFF")
if(SWIFT_ENABLE_SWIFT_IN_SWIFT)
if(NOT SWIFT_NATIVE_SWIFT_TOOLS_PATH)
# This is the normal case. We are not cross-compiling.
set(SWIFT_NATIVE_SWIFT_TOOLS_PATH "${SWIFT_RUNTIME_OUTPUT_INTDIR}")
set(SWIFT_EXEC_FOR_SWIFT_MODULES "${CMAKE_Swift_COMPILER}")
if(NOT SWIFT_EXEC_FOR_SWIFT_MODULES)
message(WARNING "BOOTSTRAPPING set to OFF because no Swift compiler is defined")
set(BOOTSTRAPPING_MODE "OFF")
endif()
elseif(BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*")
# If cross-compiling, we don't have to bootstrap. We can just use the previously
# built native swiftc to build the swift compiler modules.
message(STATUS "Building swift modules with previously built tools instead of bootstrapping")
set(SWIFT_EXEC_FOR_SWIFT_MODULES "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swiftc")
if(BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS")
set(BOOTSTRAPPING_MODE "CROSSCOMPILE-WITH-HOSTLIBS")
elseif(BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING")
set(BOOTSTRAPPING_MODE "CROSSCOMPILE")
else()
set(BOOTSTRAPPING_MODE "HOSTTOOLS")
endif()
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()
elseif(BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*")
# If cross-compiling, we don't have to bootstrap. We can just use the previously
# built native swiftc to build the swift compiler modules.
message(STATUS "Building swift modules with previously built tools instead of bootstrapping")
set(SWIFT_EXEC_FOR_SWIFT_MODULES "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swiftc")
if(BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS")
set(BOOTSTRAPPING_MODE "CROSSCOMPILE-WITH-HOSTLIBS")
elseif(BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING")
set(BOOTSTRAPPING_MODE "CROSSCOMPILE")
else()
set(BOOTSTRAPPING_MODE "HOSTTOOLS")
endif()
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_INCLUDE_TOOLS AND SWIFT_BUILD_SWIFT_SYNTAX)
if(SWIFT_INCLUDE_TOOLS AND SWIFT_BUILD_SWIFT_SYNTAX AND SWIFT_ENABLE_SWIFT_IN_SWIFT)
# Only "HOSTTOOLS" is supported in Linux when Swift parser integration is enabled.
if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|OPENBSD|FREEBSD" AND NOT BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS")
message(WARNING "Force setting BOOTSTRAPPING=HOSTTOOLS because Swift parser integration is enabled")
@@ -1530,14 +1540,16 @@ if(SWIFT_INCLUDE_TOOLS)
# https://github.com/apple/swift/issues/48534
add_subdirectory(tools)
# Localization targets are configured in a way that assume the swift
# frontend is being built, so trying to include them for other builds
# (like stdlib) fail!
#
# Diagnostics information is only useful for the frontend compiler
# anyway, so let's only include it if the compiler is being built,
# which at the moment seems like if SWIFT_INCLUDE_TOOLS is defined.
add_subdirectory(localization)
if(SWIFT_NATIVE_SWIFT_TOOLS_PATH)
# Localization targets are configured in a way that assume the swift
# frontend is being built, so trying to include them for other builds
# (like stdlib) fail!
#
# Diagnostics information is only useful for the frontend compiler
# anyway, so let's only include it if the compiler is being built,
# which at the moment seems like if SWIFT_INCLUDE_TOOLS is defined.
add_subdirectory(localization)
endif()
endif()
add_subdirectory(utils)

View File

@@ -856,7 +856,12 @@ function(_compile_swift_files
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
set(HOST_EXECUTABLE_SUFFIX .exe)
endif()
if(SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER)
if(NOT SWIFT_ENABLE_SWIFT_IN_SWIFT)
# This is only for bootstrapping purposes. The just-built Swift is very
# limited and only built for the builder to build the next stages with
# hosttools.
set(swift_compiler_tool "${Swift_BINARY_DIR}/bin/swiftc")
elseif(SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER)
if(SWIFT_PREBUILT_SWIFT)
set(swift_compiler_tool "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swiftc${HOST_EXECUTABLE_SUFFIX}")
elseif(CMAKE_Swift_COMPILER)
@@ -886,8 +891,10 @@ function(_compile_swift_files
# cross-compiling the compiler.
list(APPEND swift_compiler_tool_dep "swift-frontend${target_suffix}")
# If we aren't cross compiling, also depend on SwiftMacros.
list(APPEND swift_compiler_tool_dep SwiftMacros)
if(SWIFT_ENABLE_SWIFT_IN_SWIFT)
# If we aren't cross compiling, also depend on SwiftMacros.
list(APPEND swift_compiler_tool_dep SwiftMacros)
endif()
endif()
# If there are more than one output files, we assume that they are specified

View File

@@ -1,5 +1,7 @@
# Keep in sync with test/CMakeLists.txt: swift-reflection-test is
# only used when testing dynamic stdlib.
if(SWIFT_BUILD_DYNAMIC_STDLIB AND (SWIFT_INCLUDE_TESTS OR SWIFT_INCLUDE_TEST_BINARIES))
if(SWIFT_BUILD_REMOTE_MIRROR AND
SWIFT_BUILD_DYNAMIC_STDLIB AND
(SWIFT_INCLUDE_TESTS OR SWIFT_INCLUDE_TEST_BINARIES))
add_subdirectory(swift-reflection-test)
endif()

View File

@@ -5,25 +5,27 @@ add_swift_host_tool(swift-compatibility-symbols
DOES_NOT_USE_SWIFT
)
set(syms_file "${CMAKE_BINARY_DIR}/share/swift/compatibility-symbols")
if(SWIFT_NATIVE_SWIFT_TOOLS_PATH)
set(syms_file "${CMAKE_BINARY_DIR}/share/swift/compatibility-symbols")
add_custom_command_target(copy_compat_target
COMMAND
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swift-compatibility-symbols"
--output-filename ${syms_file}
OUTPUT
${syms_file}
DEPENDS
swift-compatibility-symbols
)
add_custom_command_target(copy_compat_target
COMMAND
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swift-compatibility-symbols"
--output-filename ${syms_file}
OUTPUT
${syms_file}
DEPENDS
swift-compatibility-symbols
)
add_dependencies(swift-frontend "${copy_compat_target}")
add_dependencies(swift-frontend "${copy_compat_target}")
swift_install_in_component(
FILES
${syms_file}
DESTINATION
"share/swift"
COMPONENT
compiler
)
swift_install_in_component(
FILES
${syms_file}
DESTINATION
"share/swift"
COMPONENT
compiler
)
endif()