CMake Tutorial 巡礼(7)_打包一个安装文件

CMake Tutorial 巡礼(7)_ 打包一个安装文件

这是本系列的第八篇。
上一篇我们学习了如何添加自定义命令及生成文件。
本篇我们要学习一下如何打包一个安装文件,这与CMake Tutorial(4)_安装与测试中提到的略有区别。那一篇中是通过源码直接编译安装,这一篇将会通过cpack打包。这一篇中打包的安装文件将会包含所有的系统依赖库,这样就可以真正地部署到其他非开发环境中去。

本章导读

在这里插入图片描述

第七步 打包一个安装文件

Next suppose that we want to distribute our project to other people so that they can use it. We want to provide both binary and source distributions on a variety of platforms. This is a little different from the install we did previously in Installing and Testing, where we were installing the binaries that we had built from the source code. In this example we will be building installation packages that support binary installations and package management features. To accomplish this we will use CPack to create platform specific installers. Specifically we need to add a few lines to the bottom of our top-level CMakeLists.txt file.

接下来假设我们想要把我们的项目部署到别人那里让别人使用。我们想要向许多不同的平台提供二进制文件和源文件。这与我们之前在Installing and Testing中做的有些许区别,当时我们通过编译源代码的方式编译生成了安装包。在本例中我们将要编译一个支持二进制安装的安装包,并且增加一些管理特性。为了实现这个目的我们使用CPack工具来创建平台相关的安装包。特别地我们需要在顶层的CMakeLists.txt文件末尾添加一些行。

CMakeLists.txt

include(InstallRequiredSystemLibraries)
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
set(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}")
set(CPACK_SOURCE_GENERATOR "TGZ")
include(CPack)

小白按:尽管没有必要,但是小白还是贴出这个文件的完整代码:

cmake_minimum_required(VERSION 3.10)

# set the project name and version
project(Tutorial VERSION 1.0)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# should we use our own math functions
option(USE_MYMATH "Use tutorial provided math implementation" ON)

# configure a header file to pass some of the CMake settings
# to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)

# add the MathFunctions library
if(USE_MYMATH)
  add_subdirectory(MathFunctions)
  list(APPEND EXTRA_LIBS MathFunctions)
endif()

# add the executable
add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})

# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
target_include_directories(Tutorial PUBLIC
                           "${PROJECT_BINARY_DIR}"
                           )

# add the install targets
install(TARGETS Tutorial DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
  DESTINATION include
  )

# enable testing
enable_testing()

# does the application run
add_test(NAME Runs COMMAND Tutorial 25)

# does the usage message work?
add_test(NAME Usage COMMAND Tutorial)
set_tests_properties(Usage
  PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number"
  )

# define a function to simplify adding tests
function(do_test target arg result)
  add_test(NAME Comp${arg} COMMAND ${target} ${arg})
  set_tests_properties(Comp${arg}
    PROPERTIES PASS_REGULAR_EXPRESSION ${result}
    )
endfunction()

# do a bunch of result based tests
do_test(Tutorial 4 "4 is 2")
do_test(Tutorial 9 "9 is 3")
do_test(Tutorial 5 "5 is 2.236")
do_test(Tutorial 7 "7 is 2.645")
do_test(Tutorial 25 "25 is 5")
do_test(Tutorial -25 "-25 is (-nan|nan|0)")
do_test(Tutorial 0.0001 "0.0001 is 0.01")

include(InstallRequiredSystemLibraries)
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
set(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}")
set(CPACK_SOURCE_GENERATOR "TGZ")
include(CPack)

That is all there is to it. We start by including InstallRequiredSystemLibraries. This module will include any runtime libraries that are needed by the project for the current platform. Next we set some CPack variables to where we have stored the license and version information for this project. The version information was set earlier in this tutorial and the License.txt has been included in the top-level source directory for this step. The CPACK_SOURCE_GENERATOR variable selects a file format for the source package.

这就是我们需要做的一切了。我们从包含 InstallRequiredSystemLibraries开始。这个模块将要包含对于当前平台项目所需要的所有的运行库。接下来我们设置一些CPack变量,用以指定我们为项目存储license和版本信息的路径。版本信息在tutorial中早就设置好了,License.txt在这一步的顶层源码路径中也已包含在内。 CPACK_SOURCE_GENERATOR 变量则为源码包选定了一种文件格式。

Finally we include the CPack module which will use these variables and some other properties of the current system to setup an installer.

最终我们包含了 CPack module ,足以使用这些变量及当前系统具备的一些其他特性来创建安装包。

The next step is to build the project in the usual manner and then run the cpack executable. To build a binary distribution, from the binary directory run:

接下来一个步骤是用惯常的方式编译项目,然后运行cpack可执行文件。为了编译一个二进制安装包,在二进制路径下执行:

cpack

小白按:熟悉的坑点再次出现,还好在CMake Tutorial 巡礼(4)_安装与测试中这个坑我们已经踩过了。这里所 谓的“二进制路径”,指的再次是CMake在系统中的安装路径,对于小白来说,安装位置在C:\Program Files\CMake\bin路径下。

To specify the generator, use the -G option. For multi-config builds, use -C to specify the configuration. For example:

为了指定生成器,使用-G选项。若是为了多参数编译,使用-C来指定参数,例如:

cpack -G ZIP -C Debug

小白按:小白贴出完整命令:

"C:\Program Files\CMake\bin\cpack" -G ZIP -C Debug

生成得到的安装包在一个名为_CPack_Packages的目录下。
在这里插入图片描述

其路径下有一个win64\ZIP文件夹,其中包含有安装包
在这里插入图片描述

进入到bin目录下可以看到安装包内包含了依赖的系统库:
在这里插入图片描述

For a list of available generators, see cpack-generators(7) or call cpack --help. An archive generator like ZIP creates a compressed archive of all installed files.

若是想查看可用的生成器列表,请参阅 cpack-generators(7)或者调用cpack --help。一个archive generator,例如ZIP创建一个关于所有安装文件的压缩包。

To create an archive of the full source tree you would type:

若是想要创建所有源码的存档,你可以键入:

cpack --config CPackSourceConfig.cmake

小白按:同样地,这里的命令应该是:

"C:\Program Files\CMake\bin\cpack" --config CPackSourceConfig.cmake

小白用的是windows系统,但是这句命令打包的格式是linux的压缩包文件,后缀是tar.gz
里面是所有的源码。

Alternatively, run make package or right click the Package target and Build Project from an IDE.

可选地,亦可在目标包上右键使用make package,从IDE编译项目。

Run the installer found in the binary directory. Then run the installed executable and verify that it works.

运行在二进制路径下找到的安装包。然后执行可执行文件,验证它是否能够正常工作。

小白按:这里的验证留给读者你来进行了,只要注意路径位置是在Step7_build\_CPack_Packages\win64\ZIP\Tutorial-1.0-win64\bin下。

下一篇我们将继续学习“为测试白板添加支持”。

【水平所限,错漏难免,创作不易,轻喷勿骂】

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值