[CMake] target dependency in subdirectory not found

Michael Hertling mhertling at online.de
Tue Dec 13 03:50:49 EST 2011


On 12/12/2011 08:42 PM, Jos van den Oever wrote:
> I'm trying to get get CMake to do the equivalent from this Makefile. I'm using 
> cp in this simple example, but want to use different commands in my project.
> 
> ==Makefile==
> srcdir=..
> 
> hij: a/efg
>     cp a/efg hij
> a/efg: $(srcdir)/a/abc
>     -mkdir a
>     cp $(srcdir)/a/abc a/efg
> ==========
> 
> For this I am using two CMakeLists.txt files.
> 
> ==CMakeLists.txt==
> cmake_minimum_required(VERSION 2.8)
> add_subdirectory(a)
> add_custom_command(
>   OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/hij
>   COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different
>     ${CMAKE_CURRENT_BINARY_DIR}/a/efg
>     ${CMAKE_CURRENT_BINARY_DIR}/hij
>   DEPENDS
>     ${CMAKE_CURRENT_BINARY_DIR}/a/efg
>   VERBATIM
> )
> add_custom_target(hij_target ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/hij)
> ==================
> ==a/CMakeLists.txt==
> add_custom_command(
>   OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/efg
>   COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different
>     ${CMAKE_CURRENT_SOURCE_DIR}/abc
>     ${CMAKE_CURRENT_BINARY_DIR}/efg
>   DEPENDS
>     ${CMAKE_CURRENT_SOURCE_DIR}/abc
>   VERBATIM
> )
> ====================
> 
> This gives me the error:
> 
> make[2]: *** No rule to make target `a/efg', needed by `hij'.  Stop.
> make[1]: *** [CMakeFiles/hij_target.dir/all] Error 2
> make: *** [all] Error 2
> 
> How can cmake be made to understand this simple dependency?
> 
> Cheers,
> Jos

OUTPUT-style custom commands must be triggered by an {executable,
library,custom} target from within the *same* CMakeLists.txt file;
refer to the documentation for more information. Thus, you need an

ADD_CUSTOM_TARGET(efg_target ALL
    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/efg)

or the like in a/CMakeLists.txt and an

ADD_DEPENDENCIES(hij_target efg_target)

somewhere, preferably in your top-level CMakeLists.txt after the
ADD_SUBDIRECTORY() and ADD_CUSTOM_TARGET() commands. Finally, the

DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/a/efg

clause in the custom command for ${CMAKE_CURRENT_BINARY_DIR}/hij is
pointless since ${CMAKE_CURRENT_BINARY_DIR}/a/efg isn't generated
by a custom command in the same CMakeLists.txt file.

Regards,

Michael


More information about the CMake mailing list