[CMake] Adding non-file dependencies to a custom target

Alexander Neundorf a.neundorf-work at gmx.net
Tue Oct 21 14:34:16 EDT 2008


On Tuesday 21 October 2008, Giampiero Salvi wrote:
> Perhaps simplifying the code would make it clearer:
>
> This is my CMakeLists.txt file (I modified the code from a post in August):
>
> cmake_minimum_required(VERSION 2.6)
> PROJECT(TEST)
>
> ADD_CUSTOM_TARGET(foo
>   COMMAND echo "foo"
>   DEPENDS foo.txt
>   COMMENT "Fooing .."
> )
>
> ADD_CUSTOM_TARGET(bar ALL
>   COMMAND echo "bar"
>   COMMENT "Bar-tender .."
> )
> ADD_DEPENDENCIES(bar foo)
>
> ADD_CUSTOM_COMMAND(
>    OUTPUT ${CMAKE_SOURCE_DIR}/bar2.txt
>    COMMAND echo bar2 > ${CMAKE_SOURCE_DIR}/bar2.txt
>    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
> )
> ADD_CUSTOM_TARGET(bar2 ALL
>    DEPENDS ${CMAKE_SOURCE_DIR}/bar2.txt
> )
> ADD_DEPENDENCIES(bar2 foo)
>
>
> In the case of target bar everything works because bar is built every
> time, but this is not what I want. I want to build the target if and
> *only* if the dependencies have changed. So I use the form in bar2
> instead, as suggested by the documentation. However, in this case if I
> change foo.txt, target foo is built again (as a matter of fact it is
> always built), but target bar2 is not rebuilt.

Because the file ${CMAKE_SOURCE_DIR}/bar2.txt doesn't depend on foo or a file 
generated by foo.
Doesn't the custom target foo create some file ?

Also, to keep the source tree clean, you should never create files in 
CMAKE_SOURCE_DIR, but always in the build dir.

Alex


More information about the CMake mailing list