[CMake] Propagate header changes to pure Swift modules

Changes to the headers imported by Swift files aren't not correctly
propagated in CMake/Ninja. So changes to C/C++ headers didn't rebuild
ASTGen modules. Move the similar hack from SwiftCompilerSources and use
it in ASTGen as well.

rdar://120863405
This commit is contained in:
Rintaro Ishizaki
2024-01-11 14:45:54 -08:00
parent bfe1bd6ea2
commit b39dba32f3
3 changed files with 44 additions and 39 deletions

View File

@@ -264,40 +264,6 @@ else()
add_subdirectory(Sources)
# TODO: generate this dynamically through the modulemap; this cannot use `sed`
# as that is not available on all platforms (e.g. Windows).
#
# step 1: generate a dummy source file, which just includes all headers
# defined in include/swift/module.modulemap
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp.tmp"
"
#define COMPILED_WITH_SWIFT
#include \"swift/Basic/BasicBridging.h\"
#include \"swift/SIL/SILBridging.h\"
#include \"swift/SILOptimizer/OptimizerBridging.h\"
")
add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp"
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp.tmp"
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp.tmp"
"${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp"
)
# step 2: build a library containing that source file. This library depends on all the included header files.
# The swift modules can now depend on that target.
# Note that this library is unused, i.e. not linked to anything.
add_library(importedHeaderDependencies "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp")
# When building unified, we need to make sure all the Clang headers are
# generated before we try to use them.
# When building Swift against an already compiled LLVM/Clang, like
# build-script does, this target does not exist, but the headers
# are already completely generated at that point.
if(TARGET clang-tablegen-targets)
add_dependencies(importedHeaderDependencies clang-tablegen-targets)
endif()
if(BOOTSTRAPPING_MODE MATCHES "HOSTTOOLS|CROSSCOMPILE")

View File

@@ -1,11 +1,8 @@
include(macCatalystUtils)
# Workaround a cmake bug, see the corresponding function in swift-syntax
function(force_target_link_libraries TARGET)
target_link_libraries(${TARGET} ${ARGN})
cmake_parse_arguments(ARGS "PUBLIC;PRIVATE;INTERFACE" "" "" ${ARGN})
foreach(DEPENDENCY ${ARGS_UNPARSED_ARGUMENTS})
function(force_add_dependencies TARGET)
foreach(DEPENDENCY ${ARGN})
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
@@ -17,6 +14,13 @@ function(force_target_link_libraries TARGET)
endforeach()
endfunction()
function(force_target_link_libraries TARGET)
target_link_libraries(${TARGET} ${ARGN})
cmake_parse_arguments(ARGS "PUBLIC;PRIVATE;INTERFACE" "" "" ${ARGN})
force_add_dependencies(${TARGET} ${ARGS_UNPARSED_ARGUMENTS})
endfunction()
# Add compile options shared between libraries and executables.
function(_add_host_swift_compile_options name)
# Avoid introducing an implicit dependency on the string-processing library.
@@ -175,6 +179,9 @@ function(add_pure_swift_host_library name)
add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
endif()
# Depends on all '*.h' files in 'include/module.modulemap'.
force_add_dependencies(${name} importedHeaderDependencies)
# Workaround to touch the library and its objects so that we don't
# continually rebuild (again, see corresponding change in swift-syntax).
add_custom_command(
@@ -334,6 +341,9 @@ function(add_pure_swift_host_tool name)
add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
endif()
# Depends on all '*.h' files in 'include/module.modulemap'.
force_add_dependencies(${name} importedHeaderDependencies)
# Link against dependencies.
target_link_libraries(${name} PUBLIC
${APSHT_DEPENDENCIES}

View File

@@ -1 +1,30 @@
add_subdirectory(swift)
# Create a library that depends on all headers defined in include/swift/module.modulemap
#
# TODO: generate this dynamically through the modulemap; this cannot use `sed`
# as that is not available on all platforms (e.g. Windows).
file(GENERATE
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp"
CONTENT "
#define COMPILED_WITH_SWIFT
#define SWIFT_TARGET
#include \"swift/Basic/BasicBridging.h\"
#include \"swift/AST/ASTBridging.h\"
#include \"swift/IDE/IDEBridging.h\"
#include \"swift/Parse/ParseBridging.h\"
#include \"swift/SIL/SILBridging.h\"
#include \"swift/SILOptimizer/OptimizerBridging.h\"
")
add_library(importedHeaderDependencies "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp")
# When building unified, we need to make sure all the Clang headers are
# generated before we try to use them.
# When building Swift against an already compiled LLVM/Clang, like
# build-script does, this target does not exist, but the headers
# are already completely generated at that point.
if(TARGET clang-tablegen-targets)
add_dependencies(importedHeaderDependencies clang-tablegen-targets)
endif()