Files
Steven Wu 72c8c21104 [build-script] Add --enable-caching support with clang-cache and Swift compilation caching
Add a new --enable-caching option that enables compilation caching for both
C/C++ (via clang-cache as compiler launcher) and Swift code (via
-cache-compile-job flags when bootstrapping=hosttools).

New options:
- --enable-caching: main toggle, incompatible with --sccache/--distcc
- --caching-cas-path: CAS directory (default: $BUILD_ROOT/cas)
- --caching-depscan-socket: depscan daemon socket path
- --caching-plugin-path: CAS plugin library path
- --caching-plugin-option: CAS plugin options (repeatable)
- --caching-prefix-map: enable source/SDK/toolchain prefix mapping
- --caching-remote-service-path: remote caching service with auto
  plugin inference from Xcode and implied prefix mapping

The build script starts a clang-cache depscan daemon with reliable cleanup
via atexit and SIGTERM handlers. Per-product build directories get .cas-config
and compilation-prefix-map.json files written automatically.

Caching flags are applied to all Swift host compilation targets: compiler
sources, pure-swift host libraries (ASTGen, macros), swift-syntax, and
the new runtime build when --build-runtime-with-host-compiler is used.

When not using --caching-remote-service-path, enables CAS backend
(-Xfrontend -cas-backend -Xllvm -cas-friendly-debug-info) unless
SWIFT_CACHE_DISABLE_MCCAS is set.

A ninja wrapper is generated at build/<subdir>/build-utils/ninja for
cached incremental builds outside the build-script.

rdar://155876033

Assisted-By: Claude
2026-04-29 14:42:17 -07:00

66 lines
2.8 KiB
CMake

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()
# Build swift-syntax libraries with FetchContent.
function(includeSwiftSyntax)
set(CMAKE_SYSTEM_PROCESSOR ${SWIFT_HOST_VARIANT_ARCH})
set(CMAKE_OSX_ARCHITECTURES ${SWIFT_HOST_VARIANT_ARCH})
set(CMAKE_C_COMPILER_TARGET ${SWIFT_HOST_TRIPLE})
set(CMAKE_CXX_COMPILER_TARGET ${SWIFT_HOST_TRIPLE})
set(CMAKE_Swift_COMPILER_TARGET ${SWIFT_HOST_TRIPLE})
set(BUILD_SHARED_LIBS ON)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${SWIFT_HOST_LIBRARIES_DEST_DIR}/compiler")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${SWIFT_HOST_LIBRARIES_DEST_DIR}/compiler")
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()
# Add unique ABI prefix to swift-syntax libraries so that compiler libraries (e.g. sourcekitdInProc)
# can be used from tools that has its own swift-syntax libraries as SwiftPM dependencies.
set(SWIFT_MODULE_ABI_NAME_PREFIX "_Compiler")
set(SWIFTSYNTAX_TARGET_NAMESPACE "_Compiler")
set(SWIFTSYNTAX_EMIT_MODULE OFF)
file(TO_CMAKE_PATH "${SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE}" swift_syntax_path)
FetchContent_Declare(CompilerSwiftSyntax SOURCE_DIR "${swift_syntax_path}")
FetchContent_MakeAvailable(CompilerSwiftSyntax)
# FIXME: Use FetchContent_Declare's EXCLUDE_FROM_ALL after CMake 3.28
FetchContent_GetProperties(CompilerSwiftSyntax BINARY_DIR binary_dir)
set_property(DIRECTORY "${binary_dir}" PROPERTY EXCLUDE_FROM_ALL TRUE)
endfunction()
includeSwiftSyntax()
set(compiler_swiftsyntax_libs
_CompilerSwiftSyntax
_CompilerSwiftIfConfig
_CompilerSwiftWarningControl
_CompilerSwiftLexicalLookup
_CompilerSwiftOperators
_CompilerSwiftSyntaxBuilder
_CompilerSwiftParser
_CompilerSwiftParserDiagnostics
_CompilerSwiftCompilerPluginMessageHandling
_CompilerSwiftSyntaxMacroExpansion
_CompilerSwiftSyntaxMacros
_CompilerSwiftBasicFormat
_CompilerSwiftDiagnostics
_CompilerSwiftIDEUtils
)
foreach(lib ${compiler_swiftsyntax_libs})
target_compile_options(${lib} PRIVATE "SHELL:-module-link-name ${lib}")
_add_swift_caching_compile_options(${lib})
endforeach()
swift_install_in_component(TARGETS ${compiler_swiftsyntax_libs}
ARCHIVE DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/host/compiler" COMPONENT compiler-swift-syntax-lib
LIBRARY DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/host/compiler" COMPONENT compiler-swift-syntax-lib
RUNTIME DESTINATION "bin" COMPONENT compiler-swift-syntax-lib)
add_dependencies(compiler-swift-syntax-lib ${compiler_swiftsyntax_libs})