问题
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 ?
回答1:
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
)
来源:https://stackoverflow.com/questions/44796465/cmake-zip-folder-but-remove-leading-path