[CMake] add_custom_target dependency list issue

Michael Hertling mhertling at online.de
Mon Sep 27 00:33:08 EDT 2010


On 09/26/2010 09:09 PM, Szilárd Páll wrote:
>> The DEPENDS option of ADD_CUSTOM_TARGET() is meant for file-level
>> dependencies only; use ADD_DEPENDENCIES() for target-level ones.
> 
> Silly mistake, thanks for pointing it out. However, even if I add
> target dependencies using ADD_DEPENDENCIES() it seems to take into
> account only the dependent the binary targets (bar, see above), but
> the custom ones (dep1,2,3) are completely ignored. What could be still
> wrong?

The following works for me:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(CUSTOM_TARGETS_1 NONE)
ADD_CUSTOM_TARGET(t0 COMMAND ${CMAKE_COMMAND} -E echo "Building t0")
ADD_CUSTOM_TARGET(t COMMAND ${CMAKE_COMMAND} -E echo "Building t")
ADD_DEPENDENCIES(t t0)

When making "t" I can see "Building t0" followed by "Building t" each
time. If this doesn't work for you, could you perhaps post a minimal
but complete CMakeLists.txt in order to demonstrate the issue?

> Also, strangely enough, if I list the binary target in the
> add_custom_target's DEPENDS section (like in my initial example), it
> does work. Is this just because I am lucky enough that the the
> dependency itself is a binary with the same name as the target I
> actually intended to refer to?

You don't need to be lucky for this to work, at least on *nix: ;)

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(CUSTOM_TARGETS_2 C)
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n")
ADD_EXECUTABLE(main main.c)
SET_TARGET_PROPERTIES(main PROPERTIES OUTPUT_NAME "prog")
ADD_CUSTOM_TARGET(t COMMAND ${CMAKE_COMMAND} -E echo "Building t"
                    DEPENDS main)

After CMaking, ${CMAKE_BINARY_DIR}/CMakeFiles/t.dir/build.make has

CMakeFiles/t: prog

so the custom target actually tracks the executable target's output
name but not the target itself. Anyway, that's not evident from the
documentation and I don't know if it works on every CMake-supported
platform. Maybe, someone can tell us if that behaviour is intended
and reliable.

Regards,

Michael


More information about the CMake mailing list