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

Giampiero Salvi giampi at kth.se
Tue Oct 21 15:09:59 EDT 2008


Alexander Neundorf wrote:
> 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.

Hi Alex,
thanks for the reply.

That's exactly what I was trying to do with the ADD_DEPENDENCIES 
command. Are you suggesting that I should run
ADD_DEPENDENCIES(${CMAKE_SOURCE_DIR}/bar2.txt foo)
instead?
I tried that, but I get an error: "Adding dependency to non-existent 
target".
How can I add such a non-file dependency to ${CMAKE_SOURCE_DIR}/bar2.txt 
if the above doesn't work and if the DEPENDS field in the 
ADD_CUSTOM_COMMAND only works with file dependencies?

> Doesn't the custom target foo create some file ?

In reality the foo target (or rather targets because I have many like 
that) crates many files that the "real" bar2 target wraps into one 
executable using tclkit and sdx (http://www.equi4.com/starkit/sdx.html).
For each foo target there is usually a library (.so or .dll) and a 
number of tcl files. The reason why I don't want to put file 
dependencies is that I don't want to keep track of every changes I do in 
every foo target in the main CMakeLists.txt file.

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

The CMAKE_SOURCE_DIR was just for the example, in reality the foo-like 
targets create files in a "lib" directory and subdirectories, and the 
bar2-like target wraps them into an executable in the "bin" directory.

By the way, this is not a very special case when you work with mixed 
C/tcl code as you usually create a number of packages composed by a 
mixture of C libraries and tcl files, and then a main tcl file that 
includes the packages and runs something useful.

I'd be tankful for any suggestion.
Giampiero


More information about the CMake mailing list