Skip to content

[CMake] Propagate header changes to pure Swift modules #70859

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 1 commit into from
Jan 12, 2024
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
34 changes: 0 additions & 34 deletions SwiftCompilerSources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
20 changes: 15 additions & 5 deletions cmake/modules/AddPureSwift.cmake
Original file line number Diff line number Diff line change
@@ -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})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split force_target_link_libraries so that we can just add dependencies without linking.

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
Expand All @@ -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.
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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}
Expand Down
29 changes: 29 additions & 0 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file(GENERATE only updates the file when the content has changed. We don't need the .tmp/add_custom_command hack.

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()