[CMake] delayed target

Michael Hertling mhertling at online.de
Wed Feb 22 16:37:20 EST 2012


On 02/22/2012 07:21 PM, Andrea Crotti wrote:
> On 02/22/2012 05:14 PM, Michael Hertling wrote:
>> On 02/22/2012 05:02 PM, Andrea Crotti wrote:
>>> Again I'm having some troubles with the different building stages:
>>>
>>> I would like to have a target that simply unzips all the files contained
>>> in a directory,
>>> which can be found with a simple globbing.
>>>
>>>    add_custom_target(unzip_all_eggs
>>>      file(GLOB eggs RELATIVE ${EGG_BUILD_DIRECTORY}/*egg)
>>>      COMMAND ${PYTHON_EXECUTABLE} ${UNZIP_SCRIPT} ${egg}
>>>      )
>>>
>>>
>>> The problem is that [...]
>> ...FILE(GLOB ...) is a CMake command executed by CMake, whereas the
>> custom target's COMMANDs are executed by the build tool at build
>> time, so this approach fails from the first.
>>
>>> A possible solution is to make my UNZIP_SCRIPT smarter and just do the
>>> globbing
>>> itself, is there any other more CMake-like solution?
>> ADD_CUSTOM_TARGET(unzip_all_eggs
>>      ${CMAKE_COMMAND}
>>          -DEGGDIR=${EGG_BUILD_DIRECTORY}
>>          -P ${CMAKE_SOURCE_DIR}/unzip_all_eggs.cmake)
>>
>> # ${CMAKE_SOURCE_DIR}/unzip_all_eggs.cmake:
>> SET(PYTHON_EXECUTABLE ...)
>> SET(UNZIP_SCRIPT ...)
>> FILE(GLOB eggs RELATIVE ${EGGDIR}/*egg)
>> EXECUTE_PROCESS(
>>      COMMAND ${PYTHON_EXECUTABLE} ${UNZIP_SCRIPT} ${eggs}
>>      WORKING_DIRECTORY ...
>> )
>>
>> You might want to provide an unzip_all_eggs.cmake.in template including
>>
>> SET(PYTHON_EXECUTABLE @PYTHON_EXECUTABLE@)
>> SET(UNZIP_SCRIPT @UNZIP_SCRIPT@)
>>
>> use CONFIGURE_FILE(unzip_all_eggs.cmake.in unzip_all_eggs.cmake @ONLY)
>> to generate the actual unzip_all_eggs.cmake after searching Python and
>> your script, and specify -P ${CMAKE_BINARY_DIR}/unzip_all_eggs.cmake.
>>
> 
> Thanks, this is really nice in general and I might use it in the future.
> The problem is that in this specific case it doesn't buy me much and 
> adds complexity, because
> I would still have only one target unzipping all the eggs.
> 
> It would be nice to be able to generate N target 1 for each egg.

In order to define one target per egg, you'd need to know the eggs at
configuration time since you cannot define targets at build time. So,
gathering the eggs with a custom target/command will not work. As an
alternative, you might gather the eggs at configuration time with a
FILE(GLOB ...) command, loop over the resulting list and define one
target per item. However, your Makefiles would not be aware of any
changes among the eggs - additions, removals, renamings - and you
would need to remember to reconfigure your project by hand if you
don't want to miss any changes. That's somewhat error-prone and
means relinquishing one of CMake's benefits.

Regards,

Michael


More information about the CMake mailing list