1

I would like to zip folder in my project from CMake. For that I use following code snippet:

ADD_CUSTOM_COMMAND (
    TARGET ${PROJECT_NAME}
    PRE_BUILD
    COMMAND ${CMAKE_COMMAND}
    ARGS -E tar cvf ${ZIP_OUT_DIR}/my_archive.zip --format=zip -- ${FOLDER_TO_ZIP}/another_folder/
)

The problem with this code is that the files after unzipping contain path component (../../my_file.txt in my case). I tried to use tar cvf -C ${FOLDER_TO_ZIP}/another_folder but unfortunatelly CMake doesn't accept this option.

How can I get rid of leading path from zip archive when using CMake ?

tommyk
  • 3,187
  • 7
  • 39
  • 61

1 Answers1

2

The paths are relative to the working directory. So you just need to specify the WORKING_DIRECTORY:

ADD_CUSTOM_COMMAND(
    TARGET ${PROJECT_NAME}
    PRE_BUILD
    COMMAND ${CMAKE_COMMAND} -E tar cvf ${ZIP_OUT_DIR}/my_archive.zip --format=zip -- .
    WORKING_DIRECTORY ${FOLDER_TO_ZIP}/another_folder
)
Florian
  • 39,996
  • 9
  • 133
  • 149
  • That solution runs well, if you want to archive files only in that directory. But how to do it, if I need files from different directories to be archived? I could add a second custom command, but I would like to avoid that. Any idea? – tangoal May 11 '19 at 10:33
  • I didn't see any clever solution for a single call to tar command, but of course it is possible to execute several commands in one `add_custom_command` similar as it is done in https://stackoverflow.com/questions/7050997/zip-files-using-cmake. – tangoal May 11 '19 at 13:52