Skip to content

[CMake] Temporarily workaround CMake dependencies bug #62129

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
Nov 17, 2022
Merged
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
54 changes: 28 additions & 26 deletions lib/ASTGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,31 @@ if (SWIFT_SWIFT_PARSER)

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

# Link against the SwiftSyntax parser and libraries it depends on. The actual
# formulation of this is a hack to work around a CMake bug in Ninja file
# generation that results in multiple Ninja targets producing the same file in
# a downstream SourceKit target. This should be expressed as:
#
# target_link_libraries(swiftASTGen
# PRIVATE
# SwiftSyntax::SwiftCompilerSupport
# )
target_link_libraries(swiftASTGen
PRIVATE
$<TARGET_OBJECTS:SwiftSyntax::SwiftBasicFormat>
$<TARGET_OBJECTS:SwiftSyntax::SwiftParser>
$<TARGET_OBJECTS:SwiftSyntax::SwiftParserDiagnostics>
$<TARGET_OBJECTS:SwiftSyntax::SwiftDiagnostics>
$<TARGET_OBJECTS:SwiftSyntax::SwiftSyntax>
$<TARGET_OBJECTS:SwiftSyntax::SwiftOperators>
$<TARGET_OBJECTS:SwiftSyntax::SwiftSyntaxBuilder>
$<TARGET_OBJECTS:SwiftSyntax::_SwiftSyntaxMacros>
$<TARGET_OBJECTS:SwiftSyntax::SwiftCompilerSupport>
# Workaround a cmake bug, see the corresponding function in swift-syntax
function(force_target_link_libraries TARGET)
cmake_parse_arguments(ARGS "" "" "PUBLIC" ${ARGN})

swiftAST
swift_CompilerPluginSupport
)
foreach(DEPENDENCY ${ARGS_PUBLIC})
# This is a hack to workaround a cmake bug that results in multiple ninja targets producing
# the same file in a downstream SourceKit target. This should use `PUBLIC`, the dependency
# directly (rather than `TARGET_OBJECTS`), and no `add_dependencies`.
target_link_libraries(${TARGET} PRIVATE
$<TARGET_OBJECTS:${DEPENDENCY}>
)
add_dependencies(${TARGET} ${DEPENDENCY})

target_include_directories(swiftASTGen PUBLIC
"${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/swift")
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift
DEPENDS ${DEPENDENCY}
)
target_sources(${TARGET} PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift
)
endforeach()
endfunction()

add_dependencies(swiftASTGen
# TODO: Change to target_link_libraries when cmake is fixed
force_target_link_libraries(swiftASTGen PUBLIC
SwiftSyntax::SwiftBasicFormat
SwiftSyntax::SwiftParser
SwiftSyntax::SwiftParserDiagnostics
Expand All @@ -84,8 +80,14 @@ if (SWIFT_SWIFT_PARSER)
SwiftSyntax::SwiftSyntaxBuilder
SwiftSyntax::_SwiftSyntaxMacros
SwiftSyntax::SwiftCompilerSupport
)
target_link_libraries(swiftASTGen PUBLIC
swiftAST
swift_CompilerPluginSupport
)

target_include_directories(swiftASTGen PUBLIC
"${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/swift")

set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS swiftASTGen)
endif()