Feature request: cmake should have an option to detect parallel-build race conditions
The preliminary discussion concerning this feature request has occurred at https://discourse.cmake.org/t/how-to-avoid-parallel-build-race-conditions/727.
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).
So currently it is up to the build-system developer to avoid such parallel-build conflicts (i.e., race conditions) by implementing the appropriate custom targets and dependencies between those targets. And this is a substantial maintenance burden for such build systems whenever new custom commands, targets, or file or target dependencies are being developed.
Yet cmake already has all the information required to detect whenever the configuration will lead to such race conditions. For each custom target that is configured, it knows the chain of associated custom commands via the existing direct or indirect file dependencies that has been specified by the build-system developer. (Because if cmake did not have that information, it would not know how to configure the build of the custom target.)
In sum, what this feature request is asking for is an option to keep track of the chain of custom commands associated with each custom target for any given build-system configuration and simply report for that configuration whenever there is a conflict (a common custom command) between the two chains for any two different custom targets that are not serialized by an add_dependency between those two targets.
In the preliminary discussion of this feature request @brad.king has stated
This has been an issue since CMake started. There are probably open issues about it in the issue tracker already. No one has taken the time to try to implement the detection. If someone does I’d be happy to review the work. I do not plan to work on it myself.
I also have no plans to work on it because my spare time is already consumed by developing free software projects other than CMake. However, from my experience as a build-system developer is it is not unusual to introduce parallel-build race conditions through conflicts that are introduced by accident between the chain of custom commands associated with two different custom targets. Thus, it would be extremely useful to me (and presumably other build-system developers) to have a CMake option that unequivocally identifies whenever such conflicts occur for any given project configuration. Therefore, I hope some CMake developer is inspired by this new discussion of this long-standing CMake problem to implement this feature.