'How to create custom ConfigVersion.cmake

I am creating a c++ header only library and using CMake as my build system. I am pretty new with CMake, but after I read some blog posts, documentation and looked at other projects I managed to write working CMakeLists.txt.

I have a problem with expressing compatibility of different versions of my project. I am currently using in write_basic_package_version_file, COMPATIBILITY SameMinorVersion. But it doesn't work for me. As I just started writing my library I expect there to be a lot of API breaking changes before reaching version 1.0. So I want my project to have following rules:

  1. When major version is 0 then minor versions are incompatible with each other
  2. When major version is 1 or greater minor versions are compatible with each oher

I want a mix of SameMajorVersion and SameMinorVersion depending on current major version. CMake however doesn't give me this option. It's official documentation says:

If your project has more elaborated version matching rules, you will need to write your own custom ConfigVersion.cmake file instead of using this macro.

I have searched in many places, but I can't find anywhere information about how to create this custom file. I looked into one that CMake generated (by calling write_basic_package_version_file macro), but it looks really intimidating and I don't understand it really well.

So my request is: Could someone show me how to write ConfigVersion.cmake file so that it follows rules I specified above? Or point me to documentation that describes this or even link me to some project that already does this?

I include for reference part of my CMakeLists.txt that describes how to export and install my project:

include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

write_basic_package_version_file(
  "${PROJECT_NAME}ConfigVersion.cmake"
  VERSION ${PROJECT_VERSION}
  COMPATIBILITY SameMinorVersion
)

configure_package_config_file(
  "${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake.in"
  "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
  INSTALL_DESTINATION
  ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake
)

install(TARGETS ${PROJECT_NAME}
  EXPORT ${PROJECT_NAME}Targets
  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

install(EXPORT ${PROJECT_NAME}Targets
  FILE ${PROJECT_NAME}Targets.cmake
  NAMESPACE ${PROJECT_NAME}::
  DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake
)

install(FILES
  "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
  "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
  DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)

install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/chimp DESTINATION include)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source