[CMake] add_custom_command location

Michael Hertling mhertling at online.de
Mon Nov 7 17:35:55 EST 2011


On 11/07/2011 05:51 PM, Tomasz Grobelny wrote:
> I have a project which is configured in several CMakeLists.txt files. The
> problem is that add_custom_command works or doesn't work depending on where
> the command is located. If I put add_custom_command just after add_library
> then it works fine. Like this:
>   add_library(mylib SHARED ${SOURCES} ${PRIVATE_HEADERS}
> ${PUBLIC_HEADERS})
>   add_custom_command(TARGET mylib POST_BUILD 
>     COMMAND cmd
>   )
> 
> But when I try to do something like this:
> CMakeLists.txt
>   add_directory(src)
>   add_custom_command(TARGET mylib POST_BUILD 
>     COMMAND cmd
>   )
> src/CMakeLists.txt
>   add_library(mylib SHARED ${SOURCES} ${PRIVATE_HEADERS}
> ${PUBLIC_HEADERS})
> 
> Then it doesn't work (no error, just cmd is not executed). Why is that?
> And what can I do about it? If this is due to target mylib not being
> visible in root CMakeLists.txt then is there a way to forward declare it?
> And either way: if the command is not going to work anyway then an error
> would be expected IMHO.

Custom commands must be defined in the *same* CMakeLists.txt file as
the target(s) they're associated with. This is explicitly documented
for ADD_CUSTOM_COMMAND(OUTPUT ...) but holds for the TARGET flavor,
too. Otherwise, the custom commands are silently ignored. Possibly,
the latter case should indeed issue a warning --> feature request?
If you need to have a custom command in another CMakeLists.txt as
the concerned target(s), you must use a custom target to trigger
the custom command and ADD_DEPENDENCIES() to establish the
targets' correct build order.

Regards,

Michael


More information about the CMake mailing list