CMake/Tests/RunCMake/add_custom_target/TargetOrder.cmake

32 lines
1.2 KiB
CMake
Raw Normal View History

Ninja: Fix inter-target order-only dependencies of custom commands Custom command dependencies are followed for each target's source files and add their transitive closure to the corresponding target. This means that when a custom command in one target has a dependency on a custom command in another target, both will appear in the dependent target's sources. For the Makefile, VS IDE, and Xcode generators this is not a problem because each target gets its own independent build system that is evaluated in target dependency order. By the time the dependent target is built the custom command that belongs to one of its dependencies will already have been brought up to date. For the Ninja generator we need to generate a monolithic build system covering all targets so we can have only one copy of a custom command. This means that we need to reconcile the target-level ordering dependencies from its appearance in multiple targets to include only the least-dependent common set. This is done by computing the set intersection of the dependencies of all the targets containing a custom command. However, we previously included only the direct dependencies so any target-level dependency not directly added to all targets into which a custom command propagates was discarded. Fix this by computing the transitive closure of dependencies for each target and then intersecting those sets. That will get the common set of dependencies. Also add a test to cover a case in which the incorrectly dropped target ordering dependencies would fail.
2016-07-20 16:32:32 +03:00
# Add a target that requires step1 to run first but enforces
# it only by target-level ordering dependency.
add_custom_command(
OUTPUT step2.txt
COMMAND ${CMAKE_COMMAND} -E copy step1.txt step2.txt
)
add_custom_target(step2 DEPENDS step2.txt)
add_dependencies(step2 step1)
# Add a target that requires step1 and step2 to work,
# only depends on step1 transitively through step2, but
# also gets a copy of step2's custom command.
# The Ninja generator in particular must be careful with
# this case because it needs to compute the proper set of
# target ordering dependencies for the step2 custom command
# even though it appears in both the step2 and step3
# targets due to dependency propagation.
add_custom_command(
OUTPUT step3.txt
COMMAND ${CMAKE_COMMAND} -E copy step1.txt step3-1.txt
COMMAND ${CMAKE_COMMAND} -E copy step2.txt step3.txt
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/step2.txt
)
add_custom_target(step3 ALL DEPENDS step3.txt)
add_dependencies(step3 step2)
# We want this target to always run first. Add it last so
# that serial builds require dependencies to order it first.
add_custom_target(step1
COMMAND ${CMAKE_COMMAND} -E touch step1.txt
)