To much for a comment but not a full answer:
A typical sequence in the projects I generate is using the CMakePackageConfigHelper and GNUInstallDirs modules
from CMake.
There is no need to generate a foo-config-version.cmake
template. Just use the write_basic_package_version_file
command from this module to get things done.
The export
command comes handy if you want to use the config packages from the build directory without the need to install them.
See example:
# Support find_package(Foo NO_MODULE).
set(FOO_DOC_DIR ${CMAKE_INSTALL_DOCDIR})
set(FOO_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR})
set(FOO_LIB_DIR ${CMAKE_INSTALL_LIBDIR})
set(FOODIR ${CMAKE_INSTALL_PREFIX})
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
configure_package_config_file(foo-config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/foo-config.cmake"
INSTALL_DESTINATION ${FOO_CONFIG_PACKAGE_LOCATION}
PATH_VARS FOODIR FOO_INCLUDE_DIR FOO_LIB_DIR FOO_DOC_DIR
)
write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/foo-config-version.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
# To make the component usable not only from the install directory but also from the build directory
export(
TARGETS Foo
FILE foo-export.cmake
)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/foo-config.cmake" "${CMAKE_CURRENT_BINARY_DIR}/foo-config-version.cmake"
DESTINATION ${FOO_CONFIG_PACKAGE_LOCATION}
COMPONENT development
)
install(EXPORT Foo
DESTINATION ${FOO_CONFIG_PACKAGE_LOCATION}
NAMESPACE ${PROJECT_NAME}::
FILE foo-export.cmake
COMPONENT development
)