Skip to content

Commit 43a5b01

Browse files
committed
[CMake] Temporarily workaround CMake dependencies bug
Workaround a CMake bug in order to make sure that ASTGen is rebuilt when the swift-syntax dependencies change.
1 parent 429f15f commit 43a5b01

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

lib/ASTGen/CMakeLists.txt

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,31 @@ if (SWIFT_SWIFT_PARSER)
4646

4747
target_compile_options(swiftASTGen PRIVATE $<$<COMPILE_LANGUAGE:Swift>:-target;${target}>)
4848

49-
# Link against the SwiftSyntax parser and libraries it depends on. The actual
50-
# formulation of this is a hack to work around a CMake bug in Ninja file
51-
# generation that results in multiple Ninja targets producing the same file in
52-
# a downstream SourceKit target. This should be expressed as:
53-
#
54-
# target_link_libraries(swiftASTGen
55-
# PRIVATE
56-
# SwiftSyntax::SwiftCompilerSupport
57-
# )
58-
target_link_libraries(swiftASTGen
59-
PRIVATE
60-
$<TARGET_OBJECTS:SwiftSyntax::SwiftBasicFormat>
61-
$<TARGET_OBJECTS:SwiftSyntax::SwiftParser>
62-
$<TARGET_OBJECTS:SwiftSyntax::SwiftParserDiagnostics>
63-
$<TARGET_OBJECTS:SwiftSyntax::SwiftDiagnostics>
64-
$<TARGET_OBJECTS:SwiftSyntax::SwiftSyntax>
65-
$<TARGET_OBJECTS:SwiftSyntax::SwiftOperators>
66-
$<TARGET_OBJECTS:SwiftSyntax::SwiftSyntaxBuilder>
67-
$<TARGET_OBJECTS:SwiftSyntax::_SwiftSyntaxMacros>
68-
$<TARGET_OBJECTS:SwiftSyntax::SwiftCompilerSupport>
49+
# Workaround a cmake bug, see the corresponding function in swift-syntax
50+
function(force_target_link_libraries TARGET)
51+
cmake_parse_arguments(ARGS "" "" "PUBLIC" ${ARGN})
6952

70-
swiftAST
71-
)
53+
foreach(DEPENDENCY ${ARGS_PUBLIC})
54+
# This is a hack to workaround a cmake bug that results in multiple ninja targets producing
55+
# the same file in a downstream SourceKit target. This should use `PUBLIC`, the dependency
56+
# directly (rather than `TARGET_OBJECTS`), and no `add_dependencies`.
57+
target_link_libraries(${TARGET} PRIVATE
58+
$<TARGET_OBJECTS:${DEPENDENCY}>
59+
)
60+
add_dependencies(${TARGET} ${DEPENDENCY})
7261

73-
target_include_directories(swiftASTGen PUBLIC
74-
"${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/swift")
62+
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift
63+
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift
64+
DEPENDS ${DEPENDENCY}
65+
)
66+
target_sources(${TARGET} PRIVATE
67+
${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift
68+
)
69+
endforeach()
70+
endfunction()
7571

76-
add_dependencies(swiftASTGen
72+
# TODO: Change to target_link_libraries when cmake is fixed
73+
force_target_link_libraries(swiftASTGen PUBLIC
7774
SwiftSyntax::SwiftBasicFormat
7875
SwiftSyntax::SwiftParser
7976
SwiftSyntax::SwiftParserDiagnostics
@@ -83,8 +80,13 @@ if (SWIFT_SWIFT_PARSER)
8380
SwiftSyntax::SwiftSyntaxBuilder
8481
SwiftSyntax::_SwiftSyntaxMacros
8582
SwiftSyntax::SwiftCompilerSupport
83+
)
84+
target_link_libraries(swiftASTGen PUBLIC
8685
swiftAST
8786
)
8887

88+
target_include_directories(swiftASTGen PUBLIC
89+
"${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/swift")
90+
8991
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS swiftASTGen)
9092
endif()

0 commit comments

Comments
 (0)