Files
CMake/Tests/RunCMake/Autogen_1/AutogenDuplicateDependency.cmake
T
Alexandru Croitor 2b314e9009 Autogen: Fix missing autogen dependencies if a target is linked twice
Autogen contains code to forward dependencies from an origin target
to its associated <origin>_autogen target. This code also contains a
check to skip forwarding a dependency if it does not appear in the
dependency graph for all configured build types.
This is done by counting the number of times a dependency appears in
the graph for each configured build type.

Unfortunately the code did not account for the case when a dependency
appears more than once in the link graph for a single build type. This
means that for a single-config build, if the same dependency is linked
twice, the dependency will be skipped altogether.

This can lead to build errors in a project where a hypothetical App
target depends on a Lib target, and thus expects App_autogen to depend
on Lib_autogen and any of its dependencies, but the latter is skipped.

Fix this by incrementing the count of a target in the dependency graph
only once per build type.

Fixes: #26700
2025-02-13 14:33:41 -05:00

39 lines
1.4 KiB
CMake

enable_language(CXX)
find_package(Qt${with_qt_version} REQUIRED COMPONENTS Core)
set(CMAKE_AUTOMOC ON)
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/lib.cpp"
CONTENT "void foo() {}")
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/main.cpp"
CONTENT "int main() {return 0;}")
# Test case.
# App depends on Lib, which means App_autogen depends on Lib_autogen by default.
# Which means building App_autogen should trigger the generation of the
# fancy_generated.txt file, which is a dependency of Lib_autogen.
# Create a shared library and an executable.
add_library(Lib SHARED "${CMAKE_CURRENT_BINARY_DIR}/lib.cpp")
add_executable(App "${CMAKE_CURRENT_BINARY_DIR}/main.cpp")
# Link Lib into App more than once. Previously this was not properly handled by AUTOGEN,
# which discarded the Lib_autogen dependency from App_autogen entirely, and the
# file was not generated.
foreach(i RANGE 1 ${LIB_LINK_COUNT})
target_link_libraries(App PRIVATE Lib)
endforeach()
# Add a custom target that generates a file.
set(generated_file_path "${CMAKE_CURRENT_BINARY_DIR}/fancy_generated.txt")
add_custom_command(
OUTPUT "${generated_file_path}"
COMMAND ${CMAKE_COMMAND} -E touch "${generated_file_path}"
)
add_custom_target(generate_file DEPENDS "${generated_file_path}")
# Make sure the file is generated as part of building the Lib_autogen target.
set_target_properties(Lib PROPERTIES
AUTOGEN_TARGET_DEPENDS generate_file)