[CMake] COMMAND dependencies with add_custom_command()

Matthew Woehlke matthew.woehlke at kitware.com
Wed Feb 1 15:12:37 EST 2017


On 2017-01-25 20:25, Craig Scott wrote:
> The documentation for add_custom_command() contains this little nugget when
> discussing the COMMAND keyword:
> 
> If COMMAND specifies an executable target name ...<snip>... Additionally a
>> target-level dependency will be added so that the executable target will be
>> built before any target using this custom command. However this does NOT
>> add a file-level dependency that would cause the custom command to re-run
>> whenever the executable is recompiled.
> 
> I'm struggling to come up with a scenario where that last part about file
> level dependencies becomes relevant. Can anyone come up with a simple use
> case which highlights that particular aspect of dependencies and commands?

Let's start with an example, so we can talk about this concretely:

  add_executable(my_generator generate.cpp)
  add_custom_command(
    OUTPUT generated.out
    DEPENDS content.in
    COMMAND my_generator ...)

I think what this is saying is that the dependencies will be such that
if `generated.out` needs to be built, the build will ensure that
`my_generator` is built first (since the command would obviously fail
otherwise). However, the build will NOT be set up such that a change to
`generate.cpp` causes `generated.out` to be recreated. IOW, if
`generated.out` exists, it will only be considered "dirty" if it is
older than `content.in`, but NOT if it is older than `my_generator`.

Does that help?

-- 
Matthew


More information about the CMake mailing list