[CMake] Top level target depending on a file

Michael Hertling mhertling at online.de
Wed Dec 14 05:05:00 EST 2011


On 12/14/2011 09:30 AM, Alexander Broekhuis wrote:
> Hi,
> 
> 
>> 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.
>>
> 
> In my case the output is the ZIP file. So the zip file is produced by the
> top level target, and I don't want/need a separate target for it.
> The generated zip file (bundle) is the deployable unit which contains a
> library and additional information needed (at runtime) to use the library,
> which is located in the manifest file. If the manifest file changes,
> without a change to the library source, the zip file still needs to be
> regenerated. As such the output of the target depends on the manifest file,
> hence the need for a dependency to a file.

First of all, you definitely need the library target in order to build
the library, and CMake ignores each file in the list of sources which
is not necessary for this purpose. IMO, that's quite reasonable; e.g.,
you wouldn't want to have the library relinked or even completely re-
built because the manifest has been touched. Actually, there's simply
no dependency of the library on the manifest. Furthermore, a TARGET-
style custom command is triggered only when its target is rebuilt, so
generating the ZIP file in this way is unsuitable since you will miss
its dependency on the manifest. Thus, to express this dependency, you
need to have an own target for the ZIP file, so you will end up with
two targets if you want to have the dependencies set up correctly,
although you'd prefer to have just one. BTW, is this really bad?

Regards,

Michael

>> PS: Please don't drop the ML.
>>
> 
> I am used to mailing lists having the reply-to set, will try to keep it in
> mind.
> 
> 
>>
>>> 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