The documentation of add_custom_command states (in part) the following:
“Do not list the output in more than one independent target that may
build in parallel or the two instances of the rule may conflict
(instead use the
add_custom_target() command to drive the command
and make the other targets depend on that one).”
I have a use case (building some LaTeX-based documentation where both
intermediate and final products depend on many different generated
files and associated targets) where I have many interdependent file
dependencies to insure all custom commands get rebuilt as necessary.
And the targets have many different target dependencies between them.
So this complex build system definitely but necessarily has different
targets depending indirectly on the OUTPUT from a given custom
command. So to avoid parallel build conflicts (or race conditions)
for this complex case my interpretation of the parenthesized part of
the quote above is I have to create custom targets corresponding to
custom comands and use add_dependencies between those targets as for
the simple example below. But could some CMake expert here look at
this simple example and confirm there are no parallel-build race
conditions for it?
# First custom command and associated target add_custom_command( OUTPUT file_a COMMAND ... ) add_custom_target(target_a DEPENDS file_a ) # Second custom command and associated target add_custom_command( OUTPUT file_b COMMAND ... DEPENDS # This command should be rebuilt whenever file_a is updated file_a ) add_custom_target(target_b DEPENDS file_b ) add_dependencies(target_b target_a) # Third custom command and associated target add_custom_command( OUTPUT file_c COMMAND ... DEPENDS # This command should be rebuilt whenever file_a or file_b are updated file_a file_b ) add_custom_target(target_c DEPENDS file_c ) add_dependencies(target_c target_b target_a)
Given those custom command file DEPENDS are necessary (to get those
custom commands rebuilt as needed) and those targets are necessary
(due to other needs for the build system) are the above
add_dependencies commands sufficient to insure there are no
parallel-build race conditions in the above simple example?
Let’s assume the above general pattern with add_dependencies is
correct, but I have inadvertently not followed this pattern for my
complex build system. In this case, is there an unambiguous way with
CMake of detecting parallel-build race conditions for any given build
system? Or do build-system implementers that use add_custom_command
have to be constantly on guard about this issue?