From b39dba32f39bd6f0987beed98465ff55de6b3d19 Mon Sep 17 00:00:00 2001 From: Rintaro Ishizaki Date: Thu, 11 Jan 2024 14:45:54 -0800 Subject: [PATCH] [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 --- SwiftCompilerSources/CMakeLists.txt | 34 ----------------------------- cmake/modules/AddPureSwift.cmake | 20 ++++++++++++----- include/CMakeLists.txt | 29 ++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 39 deletions(-) diff --git a/SwiftCompilerSources/CMakeLists.txt b/SwiftCompilerSources/CMakeLists.txt index 89f63572325..fbff4baf6fd 100644 --- a/SwiftCompilerSources/CMakeLists.txt +++ b/SwiftCompilerSources/CMakeLists.txt @@ -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") diff --git a/cmake/modules/AddPureSwift.cmake b/cmake/modules/AddPureSwift.cmake index adf981b97a1..f8df5ba7e2c 100644 --- a/cmake/modules/AddPureSwift.cmake +++ b/cmake/modules/AddPureSwift.cmake @@ -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} diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 4c3a2a59cfe..604df6228d2 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -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()