Skip to content

Commit c8fe36d

Browse files
authored
Merge pull request #70859 from rintaro/cmake-headerdeps-rdar120863405
[CMake] Propagate header changes to pure Swift modules
2 parents eecb99e + b39dba3 commit c8fe36d

File tree

3 files changed

+44
-39
lines changed

3 files changed

+44
-39
lines changed

SwiftCompilerSources/CMakeLists.txt

-34
Original file line numberDiff line numberDiff line change
@@ -264,40 +264,6 @@ else()
264264

265265
add_subdirectory(Sources)
266266

267-
# TODO: generate this dynamically through the modulemap; this cannot use `sed`
268-
# as that is not available on all platforms (e.g. Windows).
269-
#
270-
# step 1: generate a dummy source file, which just includes all headers
271-
# defined in include/swift/module.modulemap
272-
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp.tmp"
273-
"
274-
#define COMPILED_WITH_SWIFT
275-
276-
#include \"swift/Basic/BasicBridging.h\"
277-
#include \"swift/SIL/SILBridging.h\"
278-
#include \"swift/SILOptimizer/OptimizerBridging.h\"
279-
")
280-
add_custom_command(
281-
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp"
282-
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp.tmp"
283-
COMMAND ${CMAKE_COMMAND} -E copy_if_different
284-
"${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp.tmp"
285-
"${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp"
286-
)
287-
288-
# step 2: build a library containing that source file. This library depends on all the included header files.
289-
# The swift modules can now depend on that target.
290-
# Note that this library is unused, i.e. not linked to anything.
291-
add_library(importedHeaderDependencies "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp")
292-
293-
# When building unified, we need to make sure all the Clang headers are
294-
# generated before we try to use them.
295-
# When building Swift against an already compiled LLVM/Clang, like
296-
# build-script does, this target does not exist, but the headers
297-
# are already completely generated at that point.
298-
if(TARGET clang-tablegen-targets)
299-
add_dependencies(importedHeaderDependencies clang-tablegen-targets)
300-
endif()
301267

302268
if(BOOTSTRAPPING_MODE MATCHES "HOSTTOOLS|CROSSCOMPILE")
303269

cmake/modules/AddPureSwift.cmake

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
include(macCatalystUtils)
22

33
# Workaround a cmake bug, see the corresponding function in swift-syntax
4-
function(force_target_link_libraries TARGET)
5-
target_link_libraries(${TARGET} ${ARGN})
6-
7-
cmake_parse_arguments(ARGS "PUBLIC;PRIVATE;INTERFACE" "" "" ${ARGN})
8-
foreach(DEPENDENCY ${ARGS_UNPARSED_ARGUMENTS})
4+
function(force_add_dependencies TARGET)
5+
foreach(DEPENDENCY ${ARGN})
96
string(REGEX REPLACE [<>:\"/\\|?*] _ sanitized ${DEPENDENCY})
107
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
118
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
@@ -17,6 +14,13 @@ function(force_target_link_libraries TARGET)
1714
endforeach()
1815
endfunction()
1916

17+
function(force_target_link_libraries TARGET)
18+
target_link_libraries(${TARGET} ${ARGN})
19+
20+
cmake_parse_arguments(ARGS "PUBLIC;PRIVATE;INTERFACE" "" "" ${ARGN})
21+
force_add_dependencies(${TARGET} ${ARGS_UNPARSED_ARGUMENTS})
22+
endfunction()
23+
2024
# Add compile options shared between libraries and executables.
2125
function(_add_host_swift_compile_options name)
2226
# Avoid introducing an implicit dependency on the string-processing library.
@@ -175,6 +179,9 @@ function(add_pure_swift_host_library name)
175179
add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
176180
endif()
177181

182+
# Depends on all '*.h' files in 'include/module.modulemap'.
183+
force_add_dependencies(${name} importedHeaderDependencies)
184+
178185
# Workaround to touch the library and its objects so that we don't
179186
# continually rebuild (again, see corresponding change in swift-syntax).
180187
add_custom_command(
@@ -334,6 +341,9 @@ function(add_pure_swift_host_tool name)
334341
add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
335342
endif()
336343

344+
# Depends on all '*.h' files in 'include/module.modulemap'.
345+
force_add_dependencies(${name} importedHeaderDependencies)
346+
337347
# Link against dependencies.
338348
target_link_libraries(${name} PUBLIC
339349
${APSHT_DEPENDENCIES}

include/CMakeLists.txt

+29
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
11
add_subdirectory(swift)
2+
3+
4+
# Create a library that depends on all headers defined in include/swift/module.modulemap
5+
#
6+
# TODO: generate this dynamically through the modulemap; this cannot use `sed`
7+
# as that is not available on all platforms (e.g. Windows).
8+
file(GENERATE
9+
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp"
10+
CONTENT "
11+
#define COMPILED_WITH_SWIFT
12+
#define SWIFT_TARGET
13+
14+
#include \"swift/Basic/BasicBridging.h\"
15+
#include \"swift/AST/ASTBridging.h\"
16+
#include \"swift/IDE/IDEBridging.h\"
17+
#include \"swift/Parse/ParseBridging.h\"
18+
#include \"swift/SIL/SILBridging.h\"
19+
#include \"swift/SILOptimizer/OptimizerBridging.h\"
20+
")
21+
add_library(importedHeaderDependencies "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp")
22+
23+
# When building unified, we need to make sure all the Clang headers are
24+
# generated before we try to use them.
25+
# When building Swift against an already compiled LLVM/Clang, like
26+
# build-script does, this target does not exist, but the headers
27+
# are already completely generated at that point.
28+
if(TARGET clang-tablegen-targets)
29+
add_dependencies(importedHeaderDependencies clang-tablegen-targets)
30+
endif()

0 commit comments

Comments
 (0)