本文主要是介绍CMake -- CPack工具,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
CMake – CPack工具
简介
基于上一篇的 cmake与qmake转换,然后写了这一篇CMake打包工具。
官网介绍:Configure generators for binary installers and source packages
本篇主要介绍打包的为deb安装包。
支持打包格式
- 7Z(7-Zip file format)
- DEB (Debian packages)
- External (CPack External packages)
- IFW (Qt Installer Framework)
- NSIS (Null Soft Installer)
- NSIS64 (Null Soft Installer (64-bit))
- NuGet (NuGet packages)
- RPM (RPM packages)
- STGZ (Self extracting Tar GZip compression
- TBZ2 (Tar GZip compression)
- TXZ (Tar XZ compression)
- TZ (Tar Compress compression)
- ZIP (ZIP file format)
语法介绍
demo
demo是直接粘贴在CMakeList.txt最后。
include (InstallRequiredSystemLibraries)set(_VERSION_MAJOR 1)
set(_VERSION_MINOR 1)
set(_VERSION_PATCH 0)set(CPACK_GENERATOR "DEB")set(CPACK_PACKAGE_VERSION_MAJOR "${_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${_VERSION_MINOR}")
set(CPACK_PACKAGE_VERSION_PATCH "${_VERSION_PATCH}")set(CPACK_PACKAGING_NAME "xxxx")
set(CPACK_SET_DESTDIR ON)
set(CPACK_INSTALL_PREFIX "/usr/local/xxxx")
set(CPACK_DEBIAN_PACKAGE_NAME "xxxx")
set(CPACK_PACKAGE_DESCRIPTION "xxxxx Package")
set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "${CMAKE_HOST_SYSTEM_PROCESSOR}")
set(CPACK_DEBIAN_PACKAGE_DEPENDS "freerdp (>= 2.2.0), spice-gtk (>=0.35), cJSON (), celt (>=0.5.1.3), paho.mqtt.c (>=1.3.1), spice-protocol (>=0.12.14), usbredir (>=0.8.0)"
set(CPACK_PACKAGE_DESCRIPTION "xxxxx")
set(CPACK_PACKAGE_CONTACT "xxxx")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "SiYuetian")
set(CPACK_PACKAGE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../output)install_files(/depends FILES ${CMAKE_CURRENT_SOURCE_DIR}/../depends/)
install_files(/res FILES ${CMAKE_CURRENT_SOURCE_DIR}/res/)
install_files(/lib FILES ./lib/)
install_files(. FILES ./RdpClient)
include(CPack)
#引入 InstallRequiredSystemLibraries 模块,支持cpack
include (InstallRequiredSystemLibraries)#设置打包格式 deb
set(CPACK_GENERATOR "DEB")#设置版本信息:
##主版本信息
set(_VERSION_MAJOR 1)
##次版本信息
set(_VERSION_MINOR 1)
##补丁信息
set(_VERSION_PATCH 0)##主版本信息
set(CPACK_PACKAGE_VERSION_MAJOR "${_VERSION_MAJOR}")
##次版本信息
set(CPACK_PACKAGE_VERSION_MINOR "${_VERSION_MINOR}")
##补丁信息
set(CPACK_PACKAGE_VERSION_PATCH "${_VERSION_PATCH}")#设置生成包名
set(CPACK_PACKAGING_NAME "xxxx")
#开启包重定向
set(CPACK_SET_DESTDIR ON)
#设置安装位置
set(CPACK_INSTALL_PREFIX "/usr/local/xxxx")
#设置deb包名,包名格式 ${CPACK_DEBIAN_PACKAGE_NAME}.版本信息.平台信息.deb
set(CPACK_DEBIAN_PACKAGE_NAME "xxxx")
#设置包描述
set(CPACK_PACKAGE_DESCRIPTION "xxxxx Package")
#设置包安装平台信息
set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "${CMAKE_HOST_SYSTEM_PROCESSOR}")
#设置依赖包信息
set(CPACK_DEBIAN_PACKAGE_DEPENDS "freerdp (>= 2.2.0), spice-gtk (>=0.35), cJSON (), celt (>=0.5.1.3), paho.mqtt.c (>=1.3.1), spice-protocol (>=0.12.14), usbredir (>=0.8.0)"
#设置联系人信息
set(CPACK_PACKAGE_CONTACT "xxxx")
#设置维护人信息
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "SiYuetian")
#设置包输出路径
set(CPACK_PACKAGE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../output)#设置安装文件
##设置依赖文件-${CPACK_INSTALL_PREFIX}/depends
install_files(/depends FILES ${CMAKE_CURRENT_SOURCE_DIR}/../depends/)
##设置资源文件-${CPACK_INSTALL_PREFIX}/res
install_files(/res FILES ${CMAKE_CURRENT_SOURCE_DIR}/res/)
##设置依赖库文件-${CPACK_INSTALL_PREFIX}/lib
install_files(/lib FILES ./lib/)
##设置可执行文件-${CPACK_INSTALL_PREFIX}/
install_files(. FILES ./RdpClient)
#引入CPack
include(CPack)
语法详解
install()
查看该博客: install详解
cmake官方解释:install()
install_files()
-
tall_files(<dir> extension file file ...)
Create rules to install the listed files with the given extension into the given directory. Only files existing in the current source tree or its corresponding location in the binary tree may be listed. If a file specified already has an extension, that extension will be removed first. This is useful for providing lists of source files such as foo.cxx when you want the corresponding foo.h to be installed. A typical extension is .h
-
install_files(<dir> regexp)
Any files in the current source directory that match the regular expression will be installed.
-
install_files(<dir> FILES file file ...)
Any files listed after the FILES keyword will be installed explicitly from the names given. Full paths are allowed in this form.
install_targets()
-
install_targets(<dir> [RUNTIME_DIRECTORY dir] target target)
Create rules to install the listed targets into the given directory. The directory
<dir>
is relative to the installation prefix, which is stored in the variableCMAKE_INSTALL_PREFIX
. IfRUNTIME_DIRECTORY
is specified, then on systems with special runtime files (Windows DLL), the files will be copied to that directory.
install_programs()
-
install_programs(<dir> file1 file2 [file3 ...]) install_programs(<dir> FILES file1 [file2 ...])
Create rules to install the listed programs into the given directory. Use the
FILES
argument to guarantee that the file list version of the command will be used even when there is only one argument. -
install_programs(<dir> regexp)
In the second form any program in the current source directory that matches the regular expression will be installed.
注意:<dir>为默认路径,例如上文install_files(/lib FILES ./lib/),安装文件路径为 ${CPACK_INSTALL_PREFIX}/lib,重定向安装路径已经默认引入,<dir>默认等同于${CPACK_INSTALL_PREFIX}/<dir>,为重定向路径的相对路径.
生成
下边是一个简单的生成脚本.
#!/bin/bash
#cd 'dirname $0'mkdir -p build
cd build
cmake .. $1 #$1是CMake编译参数,可自由扩展
make -j`lscpu -J|grep '"CPU(s):"'|awk 'BEGIN{FS="\""}{print$8}'`
mkdir -p lib
cp `ldd ./RdpClient | cut -d ">" -f 2 |grep lib|cut -d "(" -f 1|xargs` lib/
cpack --config CPackConfig.cmake --verbose
cd ..
rm -rf build
这篇关于CMake -- CPack工具的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!