[CMake] Top level target depending on a file

Michael Hertling mhertling at online.de
Tue Dec 13 09:21:12 EST 2011


On 12/12/2011 11:40 AM, Alexander Broekhuis wrote:
> Hi,
> 
> Can anyone help me with this? I haven't found a proper solution myself yet..

Does the following examplary project do what you intend?

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(P C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
# The library target:
FILE(WRITE ${CMAKE_BINARY_DIR}/f.c "void f(void){}\n")
ADD_LIBRARY(f SHARED f.c)
# The README file:
FILE(WRITE ${CMAKE_BINARY_DIR}/README "Very important information!\n")
# The ZIP file command:
ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_BINARY_DIR}/f.zip
    COMMAND zip -j ${CMAKE_BINARY_DIR}/f.zip
                   ${CMAKE_BINARY_DIR}/README
                   $<TARGET_FILE:f>
    DEPENDS ${CMAKE_BINARY_DIR}/README)
# The ZIP file target:
ADD_CUSTOM_TARGET(zip ALL DEPENDS ${CMAKE_BINARY_DIR}/f.zip)
# Trigger ZIP file target after library target:
ADD_CUSTOM_COMMAND(TARGET f POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/f.zip
    COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}
                             --config $<CONFIGURATION>
                             --target zip)

The basic idea is the decomposition of the ZIP file's generation into a
custom target, an OUTPUT-style custom command and a TARGET-style custom
command, the latter in conjunction with CMake's --build switch.

Regards,

Michael

> 2011/12/8 Alexander Broekhuis <a.broekhuis at gmail.com>
> 
>> Hi all,
>>
>> In my project, some top level targets depend on simple text files. These
>> targets produce a zip file as output, this is done using a custom command
>> with a post-build to a library target.
>> Part of the zip file are some simple text files, which are included using
>> some custom CPack handling inside the custom command.
>>
>> How can I let the top level target depend on these text files as well? In
>> other words, if a text files changes, the zip file has to be regenerated.
>> These text files are not generated or copied etc. So a simple DEPENDS would
>> suffice. I know add_custom_command(OUTPUT has support for this, but am
>> wondering how to do this with add_custom_command(TARGET.
>>
>> TiA!
>>
>> --
>> Met vriendelijke groet,
>>
>> Alexander Broekhuis


More information about the CMake mailing list