I have a cmake module included in the top level of CmakeList.txt:
# Generate and install package config files include(PackageConfigInstall)
In the general PackageConfigInstall.cmake file, configuration files are created from cmake.in files and installed. This module can be reused for other packages.
include(CMakePackageConfigHelpers) # Generate package config cmake files set(${PACKAGE_NAME}_LIBRARY_NAME ${CMAKE_SHARED_LIBRARY_PREFIX}${PACKAGE_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}) configure_package_config_file(${PACKAGE_NAME}-config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME}-config.cmake INSTALL_DESTINATION ${CMAKE_INSTALL_DIR}/${PACKAGE_NAME} PATH_VARS LIB_INSTALL_DIR INCLUDE_INSTALL_DIR APP_INCLUDE_INSTALL_DIR ) configure_file(${PACKAGE_NAME}-config-version.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake @ONLY) # Install package config cmake files install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME}-config.cmake ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake DESTINATION ${CMAKE_INSTALL_DIR}/${PACKAGE_NAME} COMPONENT devel )
You will need a batch file for your library, such as your_lib-config.cmake.in, which will become your_lib-config.cmake. This will contain the include and library variables that can be used.
get_filename_component(YOUR_LIB_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) # flag required by CMakePackageConfigHelpers @ [email protected] set_and_check(YOUR_LIB_INCLUDE_DIR @ [email protected] /hal) set_and_check(YOUR_LIB_LIBRARY @ [email protected] /@ [email protected] @ [email protected] @ [email protected] ) set_and_check(YOUR_LIB_LIBRARIES @ [email protected] /@ [email protected] @ [email protected] @ [email protected] )
You will also need the config-version.cmake.in file as follows:
set(PACKAGE_VERSION @ [email protected] ) # Check whether the requested PACKAGE_FIND_VERSION is compatible if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}") set(PACKAGE_VERSION_COMPATIBLE FALSE) else() set(PACKAGE_VERSION_COMPATIBLE TRUE) if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}") set(PACKAGE_VERSION_EXACT TRUE) endif() endif()
In the packaging scenarios, itโs not enough for everything to be in order. I went through a lot of trial and error to finally get something that works for different purposes (both the linux server and the built-in target). I could leave something, so please just comment and I will update the answer.
source share