[CMake] Top level target depending on a file

Michael Hertling mhertling at online.de
Tue Dec 13 10:26:50 EST 2011


On 12/13/2011 03:24 PM, Alexander Broekhuis wrote:
> Hi,
> 
> Thanks for the hint, I haven't tried it yet, but looks good at the first
> glance!
> 
> I'll try this.
> 
> Is this worth a feature request? To be able to add a file as a dependency
> to a target would make sense to me.. The posted issue (
> http://public.kitware.com/Bug/view.php?id=8438) talks about target
> dependencies, not as much about file dependenies.

CMake takes great care regarding dependencies on files which are needed
to build targets, i.e. usually, it's not necessary to specify any extra
dependencies of a target on a file. In my exemplary project, the README
file is actually not needed to build the library target, so CMake does
not add an appropriate dependency. However, the README file is needed
to generate the ZIP file, so the latter must be a separate target with
dependencies on its own, whereas the approach to generate the ZIP file
by a TARGET-style custom command for the library target is wrong, IMO;
one can just trigger the ZIP file target in this way. In other words:
You have two targets with different dependencies - don't mix them up.

What might be really worth a feature request are generator expressions
in the DEPENDS clause of custom commands/targets. If we're able to say

DEPENDS ${CMAKE_BINARY_DIR}/README) $<TARGET_FILE:f>
                                    ^^^^^^^^^^^^^^^^
in the ZIP file's custom command, it wouldn't be necessary to remove
the ZIP file prior to triggering its target after the library has
been (re)built. This could also be slightly more efficient.

Regards,

Michael

PS: Please don't drop the ML.

> 2011/12/13 Michael Hertling <mhertling at online.de>
> 
>> 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