[CMake] proper setup to create file and have it installed

Michael Hertling mhertling at online.de
Wed Dec 29 02:55:15 EST 2010


On 12/28/2010 09:10 AM, edA-qa mort-ora-y wrote:
> On 12/27/2010 09:51 PM, Michael Hertling wrote:
>> You might use the ALL option of ADD_CUSTOM_TARGET() to incorporate the
>> language target in CMake's "all" target which, in turn, is built as a
>> prerequisite of the "install" target. Alternatively, if the language
>> file should be installed only if the language target has been built
>> before, you might use the OPTIONAL flag of the INSTALL(FILES ...)
>> command to avoid the fatal error due to a missing file.
> 
> Thank you, that works.
> 
> Now, a related question. Is there some way to successively add files
> dependencies to an add_custom_target? I know that DEPENDS does this, but
> it'd be nice if I could create a "install-files" target and in various
> points in the file add the files which are needed as dependencies.
> Otherwise I will basically have a new target for every install line
> which installs a generated file.

AFAIK, the ADD_CUSTOM_TARGET() command isn't open in this sense, i.e.
you can't enhance the dependencies after the target has been defined.
So, you must actually use multiple targets or collect everything and
define the custom target at the end, e.g. with your language files:

UNSET(LANGUAGES)
...
LIST(APPEND LANGUAGES "English")
...
LIST(APPEND LANGUAGES "French")
...
LIST(APPEND LANGUAGES "German")
...
UNSET(LANGFILES)
FOREACH(i IN LISTS LANGUAGES)
    SET(LANGFILE ${CMAKE_CURRENT_BINARY_DIR}/${i}.qm)
    ADD_CUSTOM_COMMAND(OUTPUT ${LANGFILE} COMMAND ... DEPENDS ...)
    LIST(APPEND LANGFILES ${LANGFILE})
ENDFOREACH()
ADD_CUSTOM_TARGET(languages ALL DEPENDS ${LANGFILES})
INSTALL(FILES ${LANGFILES} DESTINATION ...)

The tricky part is how to set up the custom command within the loop.

Regards,

Michael


More information about the CMake mailing list