[CMake] problem with add_custom command

Michael Wild themiwi at gmail.com
Wed Mar 25 08:24:38 EDT 2009


On 25. Mar, 2009, at 10:28, ankit jain wrote:

> 2009/3/25 Michael Wild <themiwi at gmail.com>
>
>>
>> On 25. Mar, 2009, at 9:33, ankit jain wrote:
>>
>> 2009/3/25 Michael Wild <themiwi at gmail.com>
>>>
>>
>> [...]
>>
>>
>>>> What I usually do is this:
>>>>
>>>> add_custom_command( OUTPUT ${TARDIR}/t1
>>>> COMMAND ${CMAKE_COMMAND} -E tar xvf ${TARDIR}/mytar.tar
>>>> WORKING_DIRECTORY ${TARDIR}
>>>> COMMENT "Extracting ${TARDIR}/mytar.tar"
>>>> VERBATIM
>>>> )
>>>>
>>>> add_custom_target( extract_mytar
>>>> DEPENDS ${TARDIR}/t1
>>>> )
>>>>
>>>> This tells CMake how to obtain the file ${TARDIR}/t1 by unpacking
>>>> mytar.tar. Then it adds a target which depends on that file. You  
>>>> then can
>>>> have other targets depend on that by using add_dependencies.
>>>>
>>>>
>>>
>>> Thanks for your suggestions it works but the problem is that iam  
>>> making a
>>> library which requires some source files which will come after  
>>> extracting
>>> it
>>> from tar.
>>>
>>> In that making an custom target for it and then add_dependencies  
>>> to that
>>> library to this custom build target does not solve the purpose.
>>>
>>> then how to include those files which is required by the library  
>>> which
>>> came
>>> from tar.
>>>
>>
>>
>> if you add all the files from the tar archive to the OUTPUT list of
>> add_custom_command, CMake should automatically set the GENERATED  
>> property of
>> those file to TRUE. If you don't want to do that, you still can set  
>> that
>> property manually, using e.g.:
>>
>> set_source_files_properties( ${TARDIR}/t1 ${TARDIR}/t2 PROPERTIES  
>> GENERATED
>> TRUE )
>>
>> you then can use those files in a normal add_library or  
>> add_executable
>> command. if you use the first approach (listing all files in the  
>> OUTPUT
>> list), you don't even need the custom target, since CMake then will  
>> know how
>> to "create" these files (by invoking the custom command).
>>
>>
> Is there any way by which we just give the name of folder where  
> files has
> extracted and add_library command will take it by some means it is  
> becoz if
> we dont know what files will be genrated inside that folder or if  
> there are
> large no.of files then listing them in OUTPUT is really cumbersome..
>
>
> ankit

you could use

execute_process(
   COMMAND ${CMAKE_COMMAND} -E tar tf ${TARDIR}/mytar.tar
   OUTPUT_VARIABLE tar_files
   )

to get a list of files contained in the tar file. However, I don't  
recommend this. It is much safer to really write the files out. If it  
hurts your eyes to put it in the CMakeLists.txt file, you can put the  
list in e.g. a file called files.cmake and INCLUDE that one from your  
CMakeLists.txt.

DON'T use file(GLOB ...) or similar. That's a pretty bad idea...

Michael


More information about the CMake mailing list