Cmake zip folder but remove leading path
Asked Answered
B

1

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 ?

Bellows answered 28/6, 2017 at 7:44 Comment(0)
O
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
)
Orris answered 28/6, 2017 at 19:16 Comment(2)
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?Balder
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://mcmap.net/q/468556/-zip-files-using-cmake.Balder

© 2022 - 2024 — McMap. All rights reserved.