使用CMake压缩文件? [英] zip files using CMake?

查看:487
本文介绍了使用CMake压缩文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tl; dr版本:

使用CMake(> = 2.8)是否可以从某些文件生成zip文件并将打包的zip文件放入

Is it possible with CMake (>= 2.8) to generate zip files from some files and put the packed zip file in a specific location?

较长版本:

我有一个CMakeLists.txt,可将我的项目构建为。 exe文件,此exe文件将从zip文件读取数据。打包在zip文件中的内容在我的git存储库中,因此也可以对其进行编辑。但是,程序需要将此数据保存在zip文件中。因此,如果CMake脚本可以获取数据,将其放入zip文件中,然后将其放置在exe旁边,那就很好了。我已经听说过CPack,但是没有找到任何简单的示例,并且不确定这是否是执行任务的正确工具。

I have a CMakeLists.txt that builds my project into a .exe file, and this exe file will read data from a zip file. The content to be packed in the zip file is in my git repository so that it can be edited, too. But, the program needs this data in a zip file. So it would be good if the CMake script could take the data, put it in a zip file, and place it next to the exe. I already heard of CPack, but I did not find any easy examples and am not sure if this is even the right tool for my task.

这可能吗?如果是,怎么办?

Is this possible? If yes, how?

推荐答案

从3.2版开始,CMake具有生成内置zip文件的功能。 CMake 命令行模式子命令 tar 支持创建zip和7zip归档文件。

Since version 3.2 CMake has the functionality to generate a zip file built-in. The CMake command-line mode sub-command tar supports both the creation of zip and 7zip archives.

例如,如果当前的CMake源目录包含文件 testfile.txt 和目录 testdir ,您可以使用以下CMake命令创建一个zip文件包含两个项目的文件:

For example, if the current CMake source directory contains the file testfile.txt and the directory testdir, you can use the following CMake commands to create a zip file containing both items:

add_custom_target(create_zip COMMAND
    ${CMAKE_COMMAND} -E tar "cfv" "archive.zip" --format=zip
       "${CMAKE_CURRENT_SOURCE_DIR}/testfile.txt"
       "${CMAKE_CURRENT_SOURCE_DIR}/testdir")

作为早期CMake版本的解决方法,您可以使用 jar 命令,它是标准Java JRE安装的一部分。

As a work-around for earlier CMake versions, you can use the jar command that is part of a standard Java JRE installation.

find_package(Java)

execute_process(
    COMMAND
        "${Java_JAR_EXECUTABLE}" "cfM" "archive.zip" 
        "-C" "${CMAKE_CURRENT_SOURCE_DIR}" "testfile.txt" 
        "-C" "${CMAKE_CURRENT_SOURCE_DIR}" "testdir"
    RESULT_VARIABLE _result
)

该zip文件将在当前CMake二进制目录( CMAKE_CURRENT_BINARY_DIR )。

The zip file will be generated in the current CMake binary dir (CMAKE_CURRENT_BINARY_DIR).

这篇关于使用CMake压缩文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆