51Testing软件测试论坛

标题: CMake实战:安装测试和添加环境生成安装包(下) [打印本页]

作者: lsekfe    时间: 2022-1-14 13:34
标题: CMake实战:安装测试和添加环境生成安装包(下)
 2、添加版本号
  首先修改顶层 CMakeLists 文件,在 project 命令之后加入如下两行:
  1.  set (Demo_VERSION_MAJOR 1)
  2.   set (Demo_VERSION_MINOR 0)
复制代码
 分别指定当前的项目的主版本号和副版本号。
  之后,为了在代码中获取版本信息,我们可以修改 config.h.in 文件,添加两个预定义变量:
  1. // the configured options and settings for Tutorial
  2.   #define Demo_VERSION_MAJOR @Demo_VERSION_MAJOR@
  3.   #define Demo_VERSION_MINOR @Demo_VERSION_MINOR@
复制代码
这样就可以直接在代码中打印版本信息了:
  1. #include <stdio.h>
  2.   #include <stdlib.h>
  3.   #include "config.h"
  4.   #ifdef USE_MYMATH
  5.     #include "math/myMath.h"
  6.   #else
  7.     #include <math.h>
  8.   #endif
  9.   int sub(int a, int b) {
  10.    return (a - b);
  11.   }
  12.   int main(int argc, char *argv[]) {
  13.       if (argc < 3) {
  14.           printf("Usage: %s argv[1] argv[2] \n", argv[0]);
  15.           return 1;
  16.       }
  17.       printf("version : %d.%d\n", Demo_VERSION_MAJOR, Demo_VERSION_MINOR);
  18.       int a = atof(argv[1]);
  19.       int b = atoi(argv[2]);
  20.   #ifdef USE_MYMATH
  21.       printf("Now we use our own Math library. \n");
  22.       int result = add(a, b);
  23.       printf("%d + %d = %d\n", a, b, result);
  24.   #else
  25.       printf("Now we use the main.cpp sub function. \n");
  26.       int result = sub(a, b);
  27.       printf("%d - %d = %d\n", a, b, result);
  28.   #endif
  29.       return 0;
  30.   }
复制代码
运行结果:
  1.  [root@hackett demo7]# cmake .
  2.   -- Configuring done
  3.   -- Generating done
  4.   -- Build files have been written to: /root/workspace/cmake/demo7
  5.   [root@hackett demo7]# make
  6.   Consolidate compiler generated dependencies of target MathFunctions
  7.   [ 25%] Building CXX object math/CMakeFiles/MathFunctions.dir/myMath.cpp.o
  8.   [ 50%] Linking CXX static library libMathFunctions.a
  9.   [ 50%] Built target MathFunctions
  10.   [ 75%] Building CXX object CMakeFiles/demo.dir/main.cpp.o
  11.   [100%] Linking CXX executable demo
  12.   [100%] Built target demo
  13.   [root@hackett demo7]# ./demo 2 3
  14.   version : 1.0
  15.   Now we use our own Math library.
  16.   2 + 3 = 5
复制代码
3、生成安装包
  如何配置生成各种平台上的安装包,包括二进制安装包和源码安装包。为了完成这个任务,我们需要用到 CPack ,它同样也是由 CMake 提供的一个工具,专门用于打包。
  首先在顶层的 CMakeLists.txt 文件尾部添加下面几行:
  1.  # 构建一个 CPack 安装包
  2.   include (InstallRequiredSystemLibraries)
  3.   set (CPACK_RESOURCE_FILE_LICENSE
  4.     "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
  5.   set (CPACK_PACKAGE_VERSION_MAJOR "${Demo_VERSION_MAJOR}")
  6.   set (CPACK_PACKAGE_VERSION_MINOR "${Demo_VERSION_MINOR}")
  7.   include (CPack)
复制代码
上面的代码做了以下几个工作:
  1. 导入 InstallRequiredSystemLibraries 模块,以便之后导入 CPack 模块;
  2. 设置一些 CPack 相关变量,包括版权信息和版本信息,其中版本信息用了上一节定义的版本号;
  3. 导入 CPack 模块。
  创建一个``License.txt`文件
touch License.txt

接下来的工作是像往常一样构建工程,并执行 cpack 命令。
  生成二进制安装包:
  1. cpack -C CPackConfig.cmake
复制代码
 生成源码安装包:

  1. cpack -C CPackSourceConfig.cmake
复制代码
我们可以试一下。在生成项目后,执行 cpack -C CPackConfig.cmake 命令:

  1. [root@hackett demo8]# cmake .
  2.   -- Configuring done
  3.   -- Generating done
  4.   -- Build files have been written to: /root/workspace/cmake/demo8
  5.   [root@hackett demo8]# cpack -C CPackConfig.cmake
  6.   CPack: Create package using STGZ
  7.   CPack: Install projects
  8.   CPack: - Run preinstall target for: demo8
  9.   CPack: - Install project: demo8 [CPackConfig.cmake]
  10.   CPack: Create package
  11.   CPack: - package: /root/workspace/cmake/demo8/demo8-1.0.1-Linux.sh generated.
  12.   CPack: Create package using TGZ
  13.   CPack: Install projects
  14.   CPack: - Run preinstall target for: demo8
  15.   CPack: - Install project: demo8 [CPackConfig.cmake]
  16.   CPack: Create package
  17.   CPack: - package: /root/workspace/cmake/demo8/demo8-1.0.1-Linux.tar.gz generated.
  18.   CPack: Create package using TZ
  19.   CPack: Install projects
  20.   CPack: - Run preinstall target for: demo8
  21.   CPack: - Install project: demo8 [CPackConfig.cmake]
  22.   CPack: Create package
  23.   CPack: - package: /root/workspace/cmake/demo8/demo8-1.0.1-Linux.tar.Z generated.
  24.   [root@hackett demo8]# ls
  25.   CMakeCache.txt  cmake_install.cmake  config.h     CPackConfig.cmake  CPackSourceConfig.cmake  demo                  demo8-1.0.1-Linux.tar.gz  install_manifest.txt  main.cpp  math
  26.   CMakeFiles      CMakeLists.txt       config.h.in  _CPack_Packages    CTestTestfile.cmake      demo8-1.0.1-Linux.sh  demo8-1.0.1-Linux.tar.Z   License.txt           Makefile
复制代码
这 3 个二进制包文件所包含的内容是完全相同的。我们可以执行其中一个。此时会出现一个由 CPack 自动生成的交互式安装界面:

  1. [root@hackett demo8]# sh demo8-1.0.1-Linux.sh
  2.   demo8 Installer Version: 1.0.1, Copyright (c) Humanity
  3.   This is a self-extracting archive.
  4.   The archive will be extracted to: /root/workspace/cmake/demo8
  5.   If you want to stop extracting, please press <ctrl-C>.
  6.   Do you accept the license  [yn]:
  7.   y
  8.   By default the demo8 will be installed in:
  9.     "/root/workspace/cmake/demo8/demo8-1.0.1-Linux"
  10.   Do you want to include the subdirectory demo8-1.0.1-Linux
  11.   Saying no will install in: "/root/workspace/cmake/demo8" [Yn]:
  12.   y
  13.   Using target directory: /root/workspace/cmake/demo8/demo8-1.0.1-Linux
  14.   Extracting, please wait...
  15.   Unpacking finished successfully
复制代码
完成后提示安装到了 demo8-1.0.1-Linux 子目录中,我们可以进去执行该程序:

  1.  [root@hackett demo8]# ls
  2.   CMakeCache.txt  cmake_install.cmake  config.h     CPackConfig.cmake  CPackSourceConfig.cmake  demo               demo8-1.0.1-Linux.sh      demo8-1.0.1-Linux.tar.Z  main.cpp  math
  3.   CMakeFiles      CMakeLists.txt       config.h.in  _CPack_Packages    CTestTestfile.cmake      demo8-1.0.1-Linux  demo8-1.0.1-Linux.tar.gz  install_manifest.txt     Makefile
  4.   [root@hackett demo8]# ./demo8-1.0.1-Linux/bin/demo 3 2
  5.   version : 1.0
  6.   Now we use our own Math library.
  7.   3 + 2 = 5
复制代码












欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2