Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get started with CPack

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is about the introduction to CPack. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.

Background

CPack is a built-in tool after CMake 2.4.2 for creating binary and source packages for software.

The location of CPack throughout the CMake tool chain.

The following types of package formats are supported by CPack:

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)

Why use packing tools?

If software programs want to be used quickly in the production environment, they need an one-click installation package, so that the production environment can be easily deployed and used.

Reasons for choosing CPack

Most of the C++ projects are compiled with CMake configuration, and CPack is a built-in tool for CMake that supports installation packages packaged into multiple formats. Because it is a built-in tool for CMake, the way we use it is by configuring parameters in CMakeLists.txt to meet our needs. It is easy to use and easy to use.

How to install CPack

When you install CMake, you will install CPack together, directly through yum or apt-get.

A simple example of basic configuration

Here is how to package the rpm package. The packaging of deb is the same, but the difference lies in some configurations. Cpack packages rpm using a CPack RPM generator, and the configuration variables used are prefixed with CPACK_RPM_XXX. Finally, it is packaged through the rpm-build tool, so you need to install the rpm-build tool, which can be installed through the sudo yum install-y rpm-build. The following configuration is tested with 3.14.5 CMake.

Now there is a project example whose directory structure is as follows:

Example |-- CMakeLists.txt / / example's main CMakeLists.txt file |-- Readme.txt |-- License.txt |-- src | |-- CMakeLists.txt | |-- MainA.cpp / / the source code of the executable file Aprogram | | _ _ MainB.cpp / / executable Source code of file Bprogram | |-- etc | |-- CMakeLists.txt | |-- A.conf / / configuration file of executable file Aprogram | | _ _ B.conf / / configuration file of executable file Bprogram | | _ _ scripts |-- preinst / / execute before installation | Script |-- postinst / / script executed after installation |-- prerm / / script executed before uninstallation | _ _ postrm / / script executed after uninstallation

Just add the following configuration to the example/CMakeLists.txt file

# set the generated installation package name set (CPACK_PACKAGE_NAME "example") # set to specify the control of the installation directory to ON set (CPACK_SET_DESTDIR ON) # set the directory path set (CPACK_INSTALL_PREFIX "/ home/vesoft/install") to which the installation is installed # this is the version number information of the generated installation Set (CPACK_PACKAGE_VERSION "1.0.0") # set group name set (CPACK_RPM_PACKAGE_GROUP "vesoft") # set vendor name set (CPACK_PACKAGE_VENDOR "vesoft") # set license information set (CPACK _ RPM_PACKAGE_LICENSE "Apache 2.0 + Common Clause 1.0") include (CPack)

After executing the cmake command, you will find that there are two more files, CPackConfig.cmake and CPackSourceConfig.cmake, in the current directory. After the compilation is completed, execute cpack-G RPM to package the files into a rpm package. The current directory will generate a _ CPack_Packages directory and a file with the suffix .rpm example-1.0.0-Linux.rpm,example-1.0.0-Linux.rpm is the installation package file we want.

If you want to see the detailed output of the packaging process, you can add-- verbose after the command. CPack generates a _ CPack_Packages/Linux/RPM/SPECS/example.spec file based on the user's configuration and then lets rpm-build use it.

The installation package example-1.0.0-Linux.rpm generated by the above configuration contains the following files:

⚠️ Note: if file / home from install of example-1.0.0-1.x86_64 conflicts with file from package filesystem-3.2-25.el7.x86_64 appears during installation, you need to add the following configuration to the configuration file so that the generated rpm file does not contain / home and / home/vesoft.

Set (CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION "/ home") list (APPEND CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION "/ home/vesoft") add behavior

When we want to do something before and after installation and uninstallation, we can write the corresponding script file:

Preinst: pre-installation script fil

Postinst: post-installation script fil

Prerm: pre-uninstall fil

Postrm: files after uninstallation

Add the following configuration to the above CMakeLists.txt file:

# set script file preinstset (CPACK_RPM_PRE_INSTALL_SCRIPT_FILE ${CMAKE_CURRENT_SOURCE_DIR} / scripts/preinst) # set script file prermset (CPACK_RPM_PRE_UNINSTALL_SCRIPT_FILE ${CMAKE_CURRENT_SOURCE_DIR} / scripts/prerm) executed before uninstallation # set script file postinstset (CPACK_RPM_POST_INSTALL_SCRIPT_FILE ${CMAKE_CURRENT_SOURCE_DIR) executed after installation } / scripts/postinst) # set the script file postrmset (CPACK_RPM_POST_UNINSTALL_SCRIPT_FILE ${CMAKE_CURRENT_SOURCE_DIR} / scripts/postrm) to be executed after uninstallation

CPack will write the contents of the script configured above to the generated SPEC file.

⚠️ Note: the above four script files require permissions that all users and user groups can execute. After creating the script files, modify the permissions of the script files under the scripts directory through chmod 755 scripts/*.

Executing the sudo rpm-ivh example-1.0.0-Linux.rpm command will have the following output

Executing sudo rpm-e example-1.0.0 will have the following output

You can see the green and red words in the picture, which are the printouts of the four script files, corresponding to before and after installation and before and after uninstallation. So users can implement the functions they want in these four scripts.

Pack multiple bags separately

The above configuration is to package all the files that need to be packaged into an installation package, but a project often has multiple different services, which need to be installed on different machines during deployment. At this time, if all the services are packaged together, it will cause the package to be too large during deployment. At this point, you need to use the Components function of CPack.

Here are three functions you need to use in this process: cpack_add_component and cpack_add_component_group, and install.

The following is the function definition for adding install

The following is the function definition for adding component

The following is the function definition for adding group

Taking the above as an example, if we want to package program An and its configuration file A.conf into a rpm package, and program B and its configuration file B.conf into a rpm package, we need to add the following to CMakeLists.txt to move the include (CPack) of the above configuration to the location of the following configuration:

# set each packet to be packaged into a rpm package set (CPACK_COMPONENTS_GROUPING ONE_PER_GROUP) # set to support COMPONENTset (CPACK_RPM_COMPONENT_INSTALL ON) include (CPack) # add a componentcpack_add_component named AComponent (AComponent DISPLAY_NAME "Aprogram" DESCRIPTION "The program for test" GROUP Aprogram) # add a componentcpack_add_component named BComponent (BComponent DISPLAY_NAME "B program" DESCRIPTION "The program for test" GROUP Bprogram) # add a group named Aprogram This name adds a groupcpack_add_component_group (Bprogram) named Bprogram as part of the rpm package name cpack_add_component_group (Aprogram) #

Then modify src/CMakeLists.txt, see the red box below, and configure the program A binary to AComponent and the program B binary to BComponent.

Modify the etc/CMakeLists.txt, see the red box below, and configure the configuration file A.conf to AComponent and the configuration file B.conf to BComponent.

After updating the configuration of CMakeLists.txt, re-execute the cmake command to generate a new makefile file, and execute cpack-G RPM. You can see that two files, example-1.0.0-Linux-Aprogram.rpm and example-1.0.0-Linux-Bprogram.rpm, are generated under the current directory, each containing the following files:

Other commonly used parameters

Install to the specified directory: for the above configuration, the generated installation package can only be installed to the / home/vesoft/install directory. If you want to be able to install the specified location, you need to add the following configuration before include (CPack).

# set the above configuration settings to specify the directory and set this option to OFFset (CPACK_SET_DESTDIR OFF) # set the option of the retargetable directory to ONset (CPACK_RPM_PACKAGE_RELOCATABLE ON) # set the default reset directory set (CPACK_PACKAGING_INSTALL_PREFIX "/ home/vesoft/install")

With the above configuration, the regenerated rpm package can be installed to another specified directory. Here is to install it to / home/test/install, using the following:

Sudo rpm-ivh example-1.0.0-Linux-Aprogram.rpm-- prefix=/home/test/install

Users can use the parameter CPACK_RPM_SPEC_MORE_DEFINE to add corresponding macros to the generated SEPC file to apply some of the rpmbuild function switches.

More...

CPack has many parameters, and there are some differences between different versions. If you want to know more, you can check it on the official website of CMake and see CPack. Or get the parameter description directly through CPack-- help.

Nebula Graph is also packaged with CPack into rpm and deb packages, and you can get the packages released by Nebula Graph each time release is released through https://github.com/vesoft-inc/nebula/releases.

The above is the introduction to CPack. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report