CMake quick build installation tool, CMake quick package script - PKG CMake
Have you ever written a lot of CMake build code before writing code? Have you ever written a bunch of CMake installation and export codes such as install(), export() when encapsulating your own library? Have you ever hoped that you can quickly build something that can be found by others_ Library from package? Have you ever thought that you can quickly build a multi-component library like boost? Then, the next content will brighten your eyes and refresh your ears.
Github link
introduce
Simplify a large amount of code in the project installation process, and use a single function to encapsulate some export and installation operations, so as to facilitate the construction and installation of a single project or multi-component project.
usage method
Simplest example of building an installation Library
PKG( _NAME PKG_lib _INCLUDE_DIRS "include/" # All files or directories in include will be installed, but not include itself )
After the library installation, you can reference PKG in other projects_ Lib Library:
find_package(PKG_lib REQUIRED)
Well, isn't it simple?
Let's take a look at how to build and install executable files (that is, do not include and lib) ↓
Simple build install executable example
PKG( _NAME PKG_exe _MODE Runtime _INSTALL_BIN_DIR "." # Applicable to Windows system: the default executable is installed in the bin directory of the installation directory. This code enables the executable to be installed on the root directory of the installation directory _DISABLE_INTERFACE _DISABLE_CONFIG _DISABLE_VERSION )
Compile and install (if you don't know how to use cmake for library compilation and installation, go to Baidu. This article doesn't belong to basic teaching). You will magically find that it is installed in C:\Program Files (x86)\PKG_exe directory. Of course, this installation directory can be modified. The above example just briefly introduces the basic functions.
A relatively complete dynamic library packet example
cmake_minimum_required(VERSION 3.20) project(PKG_shared VERSION 1.2.3.4) add_library(PKG_shared SHARED) add_library(PKGNS::PKG_shared ALIAS PKG_shared) set_target_properties( PKG_shared PROPERTIES VERSION 1.2.3.4 #SOVERSION 1.2 DEFINE_SYMBOL "${COMPONENT_NAME}_EXPORTS" MSVC_RUNTIME_LIBRARY "${MSVC_RUNTIME_LIB}" ) include(cmake/PKG.cmake) PKG( _NAME "PKG_shared" #_VERSION 1.2.3.4 # Because PKG_shared has defined the VERSION attribute. This sentence is omitted _DEBUG_POSTFIX "d" # Equivalent to 'set'_ target_ properties(PKG_shared PROPERTIES DEBUG_POSTFIX "d")’ _NAMESPACE "PKGNS" _INCLUDE_DIRS "include/" _INCLUDE_EXCLUDE_REG ".*\\.(svn|h\\.in|hpp\\.in)$" _INCLUDE_DESTINATION "include/${PROJECT_NAME}-1.2.3.4" _EXPORT_HEADER "comm1_export.h" # Relative to CMAKE_CURRENT_BINARY_DIR _INSTALL_PDB # Valid only for MSVC #_ADD_LIB_SUFFIX # If on a 64 bit computer_ BINARY_LIB_DIR and_ INSTALL_ LIB_ The value of dir will be suffixed with '64' _ADD_UNINSTALL ) target_sources( PKG_shared PRIVATE "src/add.cpp" ) target_include_directories( PKG_shared PRIVATE "include" "${PKG_PKG_shared_EXPORT_HEADER_DIR}" )
An example of multi-component project package with small sparrow and five dirty parts
If you want to package a multi-component PROJECT, this script will be very convenient for you. This feature provides two special options:_ IS_COMPONENT and_ IS_COMPONENTS. _ IS_COMPONENT specifies the current_ If the name target is a component of the PROJECT, this component will be attached to_ PROJECT_ PROJECT is defined in_ IS_ The component parameter defaults to PROJECT_NAME. _ PROJECT can be customized.
Main project part:
project(PKG VERSION 0.0.1) # Its components also set this value to the default value of their _INSTALL_DIR, unless custom _INSTALL_DIR set(PKG_PKG_INSTALL_DIR "/home/pkg/pkg_install") add_subdirectory(component) # Called after all child components PKG( _IS_COMPONENTS _NAME ${PROJECT_NAME} _VERSION 0.0.1 #_INCLUDE_DIRS "macro" # Here, you can add other include directories to_ INCLUDE_ In the path specified by destination _INCLUDE_FILES "macro/global.h" "macro/macro.h" # The result is the same as above, except that it is for files rather than directories _INCLUDE_EXCLUDE_REG ".*\\.(svn|h\\.in|hpp\\.in)$" #_INCLUDE_DESTINATION "include" # _ INCLUDE_ The default value of destination is_ INSTALL_INCLUDE_DIR, or "include" _SHARED_LIBS _ADD_UNINSTALL )
Component part:
add_library(component SHARED) PKG( _IS_COMPONENT _NAME component _PROJECT PKG _NAMESPACE "${PROJECT_NAME}" _DEBUG_POSTFIX "d" _INCLUDE_DIRS "include/" _INCLUDE_EXCLUDE_REG ".*\\.(svn|h\\.in|hpp\\.in)$" _INCLUDE_DESTINATION "include/${PROJECT_NAME}" _EXPORT_HEADER "include/component_export.h" # PKG will be defined_ PKG_ component_ EXPORT_ HEADER_ Dir variable _INSTALL_PDB )
Next, you can use find in other projects_ Package() to query the component:
find_package(PKG COMPONENTS component REQUIRED) ... target_link_libraries(... PRIVATE PKG::component) ...
PKG() has powerful functions. You can go to Github see.
Code comments for PKG()
The following is the Chinese comment at the beginning of PKG() (I deleted this part on Github to simplify the code):
#=============================================================================== # # @brief quick package project or components within a project # # @par available parameters # _ IS_ Component opt, the widget of the project is currently installed # _ IS_ Components opt, the currently installed component set cannot be a generatible target, that is to say`_ NAME ` cannot be ` add_library ` or ` add_ Targets generated by executable ` and other commands # _ Name one, project name / component name (no default) # _ PROJECT one,`_ IS_COMPONENT ` available when enabled. Specify the project name attached to this component (default: ${PROJECT_NAME}) # _ VERSION one, VERSION (default: the value of VERSION of the target (_NAME) attribute | is undefined) # _ Compatibility one, which defines the version compatibility of the target, # Supported values: ` AnyNewerVersion | samemajorversion | samminorversion | exactversion ` (default: AnyNewerVersion) # _ Debug_ Postfix one, add an identifier after the file name of the Debug compiled file, for example: "d", which is invalid for Release (no default) # _ SHARED_ LIBS opt, which specifies build within the scope of the function_ SHARED_ LIBS variable value will be displayed in PKG_components-config.cmake.in use # _ BINARY_ Dir one, specify the binary directory of the project (default: ${CMAKE_BINARY_DIR}) # _ BINARY_ bin_ Dir one, specifies the runtime directory of the binary directory of the project, relative to`_ BINARY_DIR `, you can also define an absolute path (default: bin) # _ BINARY_ lib_ Dir one, specifies the library Directory of the binary directory of the project, relative to`_ BINARY_DIR `, you can also define an absolute path (default: lib) # _ INSTALL_ Dir one, specify the installation directory of the project (default: ${CMAKE_INSTALL_PREFIX}) # _ INSTALL_ INCLUDE_ Dir one, which specifies the include directory of the installation directory of the project, relative to`_ INSTALL_DIR `, you can also define an absolute path (default: include) # _ INSTALL_ bin_ Dir one, specifies the runtime directory of the installation directory of the project, relative to`_ INSTALL_DIR `, you can also define an absolute path (default: bin) # _ INSTALL_ lib_ Dir one, which specifies the library Directory of the installation directory of the project, relative to`_ INSTALL_DIR `, you can also define an absolute path (default: lib) # _ ADD_ LIB_ Suffix opt, add the suffix "64" to the library directory name, which is only valid for 64 bit systems # _ INCLUDE_ Files mul, the file location of the target public header. It can be an absolute or relative path. The relative path is relative to ` CMAKE_CURRENT_SOURCE_DIR `, support generator expression (no default) # _ INCLUDE_ Dirs mul, the directory location of the target public header. It can be an absolute or relative path. The relative path is relative to ` CMAKE_CURRENT_SOURCE_DIR `, support generator expression (no default) # _ INCLUDE_ EXCLUDE_ Reg one, regular expression matching the full path of the file or directory ignored when installing the target public header (no default) # _ INCLUDE_ Destination one, match target ` INSTALL_INTERFACE ` include directory (default: ${_INSTALL_INCLUDE_DIR}) # _ DISABLE_ Interface opt, disable`_ INCLUDE_DESTINATION ` the specified directory is included in ` install_ In interface ` # _ Mode one, installation mode, supported values: ` Runtime | Development '(default: Development) # _ Namespace one, use the namespace to install your target. Do not add additional '::' (no default) # _ EXPORT_ Header one, set the absolute or relative path of the created export header file here, and the relative path is relative to ` CMAKE_CURRENT_BINARY_DIR ` (no default) # _ EXPORT_ Macro one, export the macro definition in the header (default: ` ${_NAME}_API|${_PROJECT}_${_NAME}_API ', which will be capitalized) # _ INSTALL_ PDB opt, install pdb file, valid only for MSVC # _ DISABLE_ Config opt, disable ` * - config Cmake ` file generation # _ DISABLE_ VERSION opt, always disable ` * - config VERSION Cmake ` file generation, if not based on`_ VERSION ` parameter value is undefined`_ Attribute VERSION of NAME `, ` * - config VERSION Cmake ` files will not be generated # _ CONFIG_ Template one, used to generate ` * - config Cmake ` file's config template file (default: ${CMAKE_SOURCE_DIR}/cmake/PKG_normal-config.cmake.in| # ${CMAKE_SOURCE_DIR}/cmake/PKG_components-config.cmake.in) # _ ADD_UNINSTALL opt,`_ IS_COMPONENT ` available when closing. Add uninstall command # _ UNINSTALL_TEMPLATE one,`_ IS_COMPONENT ` can be used when closing, and the template file for unloading operation (default: ${CMAKE_SOURCE_DIR}/cmake/PKG_cmake_uninstall.cmake.in) # _ UNINSTALL_ADDITIONAL mul,`_ IS_COMPONENT ` available when it is closed. The attached files or directories will be unloaded when the unloading operation is in progress (no default) # # @par global variable # PKG_< PROJECT>_ BINARY_DIR if not customized_ BINARY_DIR parameter, the change value will be used as its parameter value, which will affect the components attached to < project > # PKG_< PROJECT>_ INSTALL_DIR if not customized_ INSTALL_DIR parameter, the change value will be used as its parameter value, which will affect the components attached to < project > # # @Variables derived from par function # PKG_<_ PROJECT>_<_ NAME>_ EXPORT_ HEADER_ DIR _ IS_ It is valid when component is turned on. The value is the directory where the export header is located (ensure that the _EXPORT_HEADER parameter is defined) # PKG_<_ NAME>_ EXPORT_ HEADER_ DIR _ IS_ Component and_ IS_COMPONENTS are valid when all components are closed. The value is the directory where the export header is located (ensure that the _EXPORT_HEADER parameter is defined)
In fact, the above comments are enough to let you know how to use the script, which is nothing more than being familiar with a function - PKG().
Conclusion
PKG.cmake is just starting, and bug s are inevitable. I hope you can give more suggestions and offer more on GitHub Issues After receiving the information, I will test and correct it as soon as possible to let PKG Cmake is more helpful to you.