CMake unzip automatically(while building) when the zip file changes
Asked Answered
O

1

6

I am currently using execute_command to unzip some files before building. I would like to unzip the folder that is in effect replace the old files with new file in the destination folder when the zip file in the source changes. Can anyone give me some suggestions?

Currently I am doing something like this,

execute_process(
COMMAND ${CMAKE_COMMAND} -E tar xzf ${CMAKE_SOURCE_DIR}/abc.zip
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

I tried with add_custom_command() but I am executing it for every build. I only want it to unzip if the source zip file changes.

add_custom_command( OUTPUT ${LibsList} 
                    COMMAND ${CMAKE_COMMAND} -E tar xzf ${CMAKE_SOURCE_DIR}/libs.zip 
                    WORKING_DIRECTORY ${CMAKE_BINARY_DIR} 
                    DEPENDS ${CMAKE_SOURCE_DIR}/libs.zip ) 

They mostly contain header files and a few shared libraries. I usually need the header files at build time and shared libraries at run time. If possible I would not give the list of files in OUTPUT as it is very large

Overstuff answered 20/2, 2018 at 18:26 Comment(2)
Can you please add the add_custom_command() you have tried? And are the files that are unzipped used as an input for another build-step?Himation
Do not use comment for the code, add this to your question post instead.Sulphurous
O
10

I achieve this, at least for my scenario using the code below,

add_custom_target( unZip ALL)
add_custom_command(TARGET unZip PRE_BUILD
   COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_BINARY_DIR}/abc/
   COMMAND ${CMAKE_COMMAND} -E tar xzf ${CMAKE_SOURCE_DIR}/abc.zip
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
DEPENDS ${CMAKE_SOURCE_DIR}/abc.zip
COMMENT "Unpacking abc.zip"
VERBATIM)

Now I made all target dependent on "unZip". This way when rebuilding unZip occurs or at build time if abc.zip changes, unZip occurs.

Overstuff answered 21/2, 2018 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.