From 5b3356263cf8f00c79c25aa9e3ac71ffd8cc7e7e Mon Sep 17 00:00:00 2001 From: Marc Chevrier Date: Sun, 13 Sep 2020 16:32:32 +0200 Subject: CMakePackageConfigHelpers: Add version range support --- .../dev/CMakePackageConfigHelpers-version_range.rst | 4 ++++ Modules/BasicConfigVersion-AnyNewerVersion.cmake.in | 21 ++++++++++++++++----- Modules/BasicConfigVersion-ExactVersion.cmake.in | 7 +++++++ .../BasicConfigVersion-SameMajorVersion.cmake.in | 7 +++++++ .../BasicConfigVersion-SameMinorVersion.cmake.in | 7 +++++++ Modules/CMakePackageConfigHelpers.cmake | 5 +++++ Tests/ExportImport/Import/CMakeLists.txt | 3 +++ .../Import/version_range/CMakeLists.txt | 15 +++++++++++++++ 8 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 Help/release/dev/CMakePackageConfigHelpers-version_range.rst create mode 100644 Tests/ExportImport/Import/version_range/CMakeLists.txt diff --git a/Help/release/dev/CMakePackageConfigHelpers-version_range.rst b/Help/release/dev/CMakePackageConfigHelpers-version_range.rst new file mode 100644 index 0000000..6900529 --- /dev/null +++ b/Help/release/dev/CMakePackageConfigHelpers-version_range.rst @@ -0,0 +1,4 @@ +CMakePackageConfigHelpers-version_range +--------------------------------------- + +* :module:`CMakePackageConfigHelpers` module learned to manage version range. diff --git a/Modules/BasicConfigVersion-AnyNewerVersion.cmake.in b/Modules/BasicConfigVersion-AnyNewerVersion.cmake.in index a6e5fda..46b8b2a 100644 --- a/Modules/BasicConfigVersion-AnyNewerVersion.cmake.in +++ b/Modules/BasicConfigVersion-AnyNewerVersion.cmake.in @@ -9,12 +9,23 @@ set(PACKAGE_VERSION "@CVF_VERSION@") -if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION) - set(PACKAGE_VERSION_COMPATIBLE FALSE) +if (PACKAGE_FIND_VERSION_RANGE) + # Package version must be in the requested version range + if ((PACKAGE_FIND_VERSION_RANGE_MIN STREQUAL "INCLUDE" AND PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION_MIN) + OR ((PACKAGE_FIND_VERSION_RANGE_MAX STREQUAL "INCLUDE" AND PACKAGE_VERSION VERSION_GREATER PACKAGE_FIND_VERSION_MAX) + OR (PACKAGE_FIND_VERSION_RANGE_MAX STREQUAL "EXCLUDE" AND PACKAGE_VERSION VERSION_GREATER_EQUAL PACKAGE_FIND_VERSION_MAX))) + set(PACKAGE_VERSION_COMPATIBLE FALSE) + else() + set(PACKAGE_VERSION_COMPATIBLE TRUE) + endif() else() - set(PACKAGE_VERSION_COMPATIBLE TRUE) - if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION) - set(PACKAGE_VERSION_EXACT TRUE) + if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION) + set(PACKAGE_VERSION_COMPATIBLE FALSE) + else() + set(PACKAGE_VERSION_COMPATIBLE TRUE) + if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION) + set(PACKAGE_VERSION_EXACT TRUE) + endif() endif() endif() diff --git a/Modules/BasicConfigVersion-ExactVersion.cmake.in b/Modules/BasicConfigVersion-ExactVersion.cmake.in index 43fc4d0..3507a22 100644 --- a/Modules/BasicConfigVersion-ExactVersion.cmake.in +++ b/Modules/BasicConfigVersion-ExactVersion.cmake.in @@ -9,6 +9,13 @@ # The variable CVF_VERSION must be set before calling configure_file(). +if (PACKAGE_FIND_VERSION_RANGE) + message(AUTHOR_WARNING + "`find_package()` specify a version range but the version strategy " + "(ExactVersion) of the module `${PACKAGE_FIND_NAME}` is incompatible " + "with this request. Only the lower endpoint of the range will be used.") +endif() + set(PACKAGE_VERSION "@CVF_VERSION@") if("@CVF_VERSION@" MATCHES "^([0-9]+\\.[0-9]+\\.[0-9]+)\\.") # strip the tweak version diff --git a/Modules/BasicConfigVersion-SameMajorVersion.cmake.in b/Modules/BasicConfigVersion-SameMajorVersion.cmake.in index 8c3b6a2..662900d 100644 --- a/Modules/BasicConfigVersion-SameMajorVersion.cmake.in +++ b/Modules/BasicConfigVersion-SameMajorVersion.cmake.in @@ -9,6 +9,13 @@ # The variable CVF_VERSION must be set before calling configure_file(). +if (PACKAGE_FIND_VERSION_RANGE) + message(AUTHOR_WARNING + "`find_package()` specify a version range but the version strategy " + "(SameMajorVersion) of the module `${PACKAGE_FIND_NAME}` is incompatible " + "with this request. Only the lower endpoint of the range will be used.") +endif() + set(PACKAGE_VERSION "@CVF_VERSION@") if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION) diff --git a/Modules/BasicConfigVersion-SameMinorVersion.cmake.in b/Modules/BasicConfigVersion-SameMinorVersion.cmake.in index e2030d2..bddf4ce 100644 --- a/Modules/BasicConfigVersion-SameMinorVersion.cmake.in +++ b/Modules/BasicConfigVersion-SameMinorVersion.cmake.in @@ -10,6 +10,13 @@ # The variable CVF_VERSION must be set before calling configure_file(). +if (PACKAGE_FIND_VERSION_RANGE) + message(AUTHOR_WARNING + "`find_package()` specify a version range but the version strategy " + "(SameMinorVersion) of the module `${PACKAGE_FIND_NAME}` is incompatible " + "with this request. Only the lower endpoint of the range will be used.") +endif() + set(PACKAGE_VERSION "@CVF_VERSION@") if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION) diff --git a/Modules/CMakePackageConfigHelpers.cmake b/Modules/CMakePackageConfigHelpers.cmake index 22fc953..50d7605 100644 --- a/Modules/CMakePackageConfigHelpers.cmake +++ b/Modules/CMakePackageConfigHelpers.cmake @@ -159,6 +159,11 @@ 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. +.. note:: ``COMPATIBILITY_MODE`` ``AnyNewerVersion`` handles the version range + if any is specified (see :command:`find_package` command for the details). + All other modes are incompatible with version range and will display an + author warning if a one is specified. + If ``ARCH_INDEPENDENT`` is given, the installed package version will be considered compatible even if it was built for a different architecture than the requested architecture. Otherwise, an architecture check will be performed, diff --git a/Tests/ExportImport/Import/CMakeLists.txt b/Tests/ExportImport/Import/CMakeLists.txt index 189f7a2..a8a98fc 100644 --- a/Tests/ExportImport/Import/CMakeLists.txt +++ b/Tests/ExportImport/Import/CMakeLists.txt @@ -23,3 +23,6 @@ add_subdirectory(try_compile) # Test package INTERFACE controls add_subdirectory(Interface) + +# Test package version range +add_subdirectory(version_range) diff --git a/Tests/ExportImport/Import/version_range/CMakeLists.txt b/Tests/ExportImport/Import/version_range/CMakeLists.txt new file mode 100644 index 0000000..73b1d1e --- /dev/null +++ b/Tests/ExportImport/Import/version_range/CMakeLists.txt @@ -0,0 +1,15 @@ + +cmake_minimum_required(VERSION 3.18) + +find_package(testLibRequired 2.0...3.0) + +if (NOT testLibRequired_FOUND) + message(SEND_ERROR "version_range: fail to find package testLibRequired(2.5) with range 2.0...3.0") +endif() + + +find_package(testLibRequired 2.0...<2.5) + +if (testLibRequired_FOUND) + message(SEND_ERROR "version_range: package testLibRequired(2.5) unexpectedly found with range 2.0...<2.5") +endif() -- cgit v0.12