Why is add_custom_target always considered out of date?

According to the documentation, add_custom_target() creates a target that "ALWAYS CONSIDERED OUT OF DATE". However, the documentation also says that add_dependencies() can add dependencies between top-level targets, including one added by add_custom_target(). If add_custom_target() is always executed, is there any practical purpose to using it with add_dependencies()?

2 answers

  • answered 2018-02-13 08:00 Tsyvarev

    If add_custom_target() is always executed, is there any practical purpose to using it with add_dependencies()?

    1. Without ALL option add_custom_target() isn't built automatically.
    2. Call add_dependencies(A B) makes sure that a target B will be built before a target A, so a target A may safetly use files, created for target B.

  • answered 2018-02-13 14:08 ComicSansMS

    As far as CMake is concerned, add_custom_target does not produce anything that it could track to determine whether the target is out-of-date.

    Contrast that with add_custom_command, where you have the ability to specify an OUTPUT produced by the command. As far as CMake knows, a custom target is just a black box, where anything could happen. That's what makes them so difficult to integrate correctly into a build.

    Note that by default, custom targets are skipped altogether, you can only build them explicitly (eg. by calling make <my_target_name>). You can make them part of the default build target by either specifying the ALL option when calling add_custom_target or make another target, which is already part of the default build target, depend on your custom target.

    You usually cannot add this dependency by depending on one of its output artifacts, since, as far as CMake is concerned, the custom target does not produce anything. That's why you have to use the more archaic add_dependencies instead.