From 212b0afb66d293bb88261710a3365f95b8104898 Mon Sep 17 00:00:00 2001 From: Marc Chevrier Date: Tue, 29 Sep 2020 13:02:23 +0200 Subject: find_package: raise error on empty version range --- Modules/FindPackageHandleStandardArgs.cmake | 10 ++---- Source/cmFindPackageCommand.cxx | 15 ++++++++- Tests/RunCMake/FPHSA/RunCMakeTest.cmake | 2 -- Tests/RunCMake/FPHSA/range_empty-1-result.txt | 1 - Tests/RunCMake/FPHSA/range_empty-1-stderr.txt | 6 ---- Tests/RunCMake/FPHSA/range_empty-1.cmake | 1 - Tests/RunCMake/FPHSA/range_empty-2-result.txt | 1 - Tests/RunCMake/FPHSA/range_empty-2-stderr.txt | 6 ---- Tests/RunCMake/FPHSA/range_empty-2.cmake | 1 - .../find_package/EmptyVersionRange-result.txt | 1 + .../find_package/EmptyVersionRange-stderr.txt | 10 ++++++ .../RunCMake/find_package/EmptyVersionRange.cmake | 3 ++ Tests/RunCMake/find_package/FindVersionRange.cmake | 22 ++++++------ Tests/RunCMake/find_package/RunCMakeTest.cmake | 2 ++ Tests/RunCMake/find_package/VersionRange.cmake | 5 +++ Tests/RunCMake/find_package/VersionRange2.cmake | 5 +++ Tests/RunCMake/find_package/VersionRange3.cmake | 5 +++ Tests/RunCMake/find_package/VersionRange4.cmake | 39 ++++++++++++++++++++++ 18 files changed, 98 insertions(+), 37 deletions(-) delete mode 100644 Tests/RunCMake/FPHSA/range_empty-1-result.txt delete mode 100644 Tests/RunCMake/FPHSA/range_empty-1-stderr.txt delete mode 100644 Tests/RunCMake/FPHSA/range_empty-1.cmake delete mode 100644 Tests/RunCMake/FPHSA/range_empty-2-result.txt delete mode 100644 Tests/RunCMake/FPHSA/range_empty-2-stderr.txt delete mode 100644 Tests/RunCMake/FPHSA/range_empty-2.cmake create mode 100644 Tests/RunCMake/find_package/EmptyVersionRange-result.txt create mode 100644 Tests/RunCMake/find_package/EmptyVersionRange-stderr.txt create mode 100644 Tests/RunCMake/find_package/EmptyVersionRange.cmake create mode 100644 Tests/RunCMake/find_package/VersionRange4.cmake diff --git a/Modules/FindPackageHandleStandardArgs.cmake b/Modules/FindPackageHandleStandardArgs.cmake index b06433d..7af0171 100644 --- a/Modules/FindPackageHandleStandardArgs.cmake +++ b/Modules/FindPackageHandleStandardArgs.cmake @@ -294,16 +294,12 @@ function(FIND_PACKAGE_CHECK_VERSION version result) unset (version_msg) if (FPCV_HANDLE_VERSION_RANGE AND ${package}_FIND_VERSION_RANGE) - if (${package}_FIND_VERSION_MIN VERSION_GREATER ${package}_FIND_VERSION_MAX - OR (${package}_FIND_VERSION_MIN VERSION_EQUAL ${package}_FIND_VERSION_MAX - AND ${package}_FIND_VERSION_RANGE_MAX STREQUAL "EXCLUDE")) - set (version_msg "Found unsuitable version \"${version}\", required range is empty (\"${${package}_FIND_VERSION_RANGE}\")") - elseif ((${package}_FIND_VERSION_RANGE_MIN STREQUAL "INCLUDE" - AND version VERSION_GREATER_EQUAL ${package}_FIND_VERSION_MIN) + if ((${package}_FIND_VERSION_RANGE_MIN STREQUAL "INCLUDE" + AND version VERSION_GREATER_EQUAL ${package}_FIND_VERSION_MIN) AND ((${package}_FIND_VERSION_RANGE_MAX STREQUAL "INCLUDE" AND version VERSION_LESS_EQUAL ${package}_FIND_VERSION_MAX) OR (${package}_FIND_VERSION_RANGE_MAX STREQUAL "EXCLUDE" - AND version VERSION_LESS ${package}_FIND_VERSION_MAX))) + AND version VERSION_LESS ${package}_FIND_VERSION_MAX))) set (version_ok TRUE) set(version_msg "(found suitable version \"${version}\", required range is \"${${package}_FIND_VERSION_RANGE}\")") else() diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index 51137b3..86251d2 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -426,7 +426,7 @@ bool cmFindPackageCommand::InitialPass(std::vector const& args) // fill various parts of version specification if (!this->VersionComplete.empty()) { if (!versionRegex.find(this->VersionComplete)) { - this->SetError("called with invalid version specification"); + this->SetError("called with invalid version specification."); return false; } @@ -440,6 +440,19 @@ bool cmFindPackageCommand::InitialPass(std::vector const& args) } } + if (!this->VersionRange.empty()) { + // version range must not be empty + if ((this->VersionRangeMax == VERSION_ENDPOINT_INCLUDED && + cmSystemTools::VersionCompareGreater(this->Version, + this->VersionMax)) || + (this->VersionRangeMax == VERSION_ENDPOINT_EXCLUDED && + cmSystemTools::VersionCompareGreaterEq(this->Version, + this->VersionMax))) { + this->SetError("specified version range is empty."); + return false; + } + } + if (this->VersionExact && !this->VersionRange.empty()) { this->SetError("EXACT cannot be specified with a version range."); return false; diff --git a/Tests/RunCMake/FPHSA/RunCMakeTest.cmake b/Tests/RunCMake/FPHSA/RunCMakeTest.cmake index e4b8700..28b8570 100644 --- a/Tests/RunCMake/FPHSA/RunCMakeTest.cmake +++ b/Tests/RunCMake/FPHSA/RunCMakeTest.cmake @@ -61,8 +61,6 @@ set(RunCMake_TEST_OPTIONS "-DCMAKE_MODULE_PATH=${CMAKE_CURRENT_LIST_DIR}" "-DPse run_cmake(range_ignored) set(RunCMake_TEST_OPTIONS "-DCMAKE_MODULE_PATH=${CMAKE_CURRENT_LIST_DIR}" "-DPseudoRange_VERSION=2.0") run_cmake(range_no-range) -run_cmake(range_empty-1) -run_cmake(range_empty-2) run_cmake(range_1-3) run_cmake(range_1-2-include) run_cmake(range_1-2-exclude) diff --git a/Tests/RunCMake/FPHSA/range_empty-1-result.txt b/Tests/RunCMake/FPHSA/range_empty-1-result.txt deleted file mode 100644 index d00491f..0000000 --- a/Tests/RunCMake/FPHSA/range_empty-1-result.txt +++ /dev/null @@ -1 +0,0 @@ -1 diff --git a/Tests/RunCMake/FPHSA/range_empty-1-stderr.txt b/Tests/RunCMake/FPHSA/range_empty-1-stderr.txt deleted file mode 100644 index e1c741a..0000000 --- a/Tests/RunCMake/FPHSA/range_empty-1-stderr.txt +++ /dev/null @@ -1,6 +0,0 @@ -CMake Error at .+FindPackageHandleStandardArgs.cmake:[0-9]+ \(message\): - Could NOT find PseudoRange: Found unsuitable version "2\.0", required range - is empty \("3\.0\.\.\.2\.0"\) \(found TRUE\) -Call Stack \(most recent call first\): - .+FindPackageHandleStandardArgs.cmake:[0-9]+ \(_FPHSA_FAILURE_MESSAGE\) - FindPseudoRange.cmake:[0-9]+ \(find_package_handle_standard_args\) diff --git a/Tests/RunCMake/FPHSA/range_empty-1.cmake b/Tests/RunCMake/FPHSA/range_empty-1.cmake deleted file mode 100644 index c510d7d..0000000 --- a/Tests/RunCMake/FPHSA/range_empty-1.cmake +++ /dev/null @@ -1 +0,0 @@ -find_package(PseudoRange 3.0...2.0 REQUIRED) diff --git a/Tests/RunCMake/FPHSA/range_empty-2-result.txt b/Tests/RunCMake/FPHSA/range_empty-2-result.txt deleted file mode 100644 index d00491f..0000000 --- a/Tests/RunCMake/FPHSA/range_empty-2-result.txt +++ /dev/null @@ -1 +0,0 @@ -1 diff --git a/Tests/RunCMake/FPHSA/range_empty-2-stderr.txt b/Tests/RunCMake/FPHSA/range_empty-2-stderr.txt deleted file mode 100644 index ee0e79a..0000000 --- a/Tests/RunCMake/FPHSA/range_empty-2-stderr.txt +++ /dev/null @@ -1,6 +0,0 @@ -CMake Error at .+FindPackageHandleStandardArgs.cmake:[0-9]+ \(message\): - Could NOT find PseudoRange: Found unsuitable version "2\.0", required range - is empty \("2\.0\.\.\.<2.0"\) \(found TRUE\) -Call Stack \(most recent call first\): - .+FindPackageHandleStandardArgs.cmake:[0-9]+ \(_FPHSA_FAILURE_MESSAGE\) - FindPseudoRange.cmake:[0-9]+ \(find_package_handle_standard_args\) diff --git a/Tests/RunCMake/FPHSA/range_empty-2.cmake b/Tests/RunCMake/FPHSA/range_empty-2.cmake deleted file mode 100644 index 3ce90e8..0000000 --- a/Tests/RunCMake/FPHSA/range_empty-2.cmake +++ /dev/null @@ -1 +0,0 @@ -find_package(PseudoRange 2.0...<2.0 REQUIRED) diff --git a/Tests/RunCMake/find_package/EmptyVersionRange-result.txt b/Tests/RunCMake/find_package/EmptyVersionRange-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/find_package/EmptyVersionRange-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/find_package/EmptyVersionRange-stderr.txt b/Tests/RunCMake/find_package/EmptyVersionRange-stderr.txt new file mode 100644 index 0000000..9c00b96 --- /dev/null +++ b/Tests/RunCMake/find_package/EmptyVersionRange-stderr.txt @@ -0,0 +1,10 @@ +CMake Error at EmptyVersionRange.cmake:[0-9]+ \(find_package\): + find_package specified version range is empty. +Call Stack \(most recent call first\): + CMakeLists.txt:[0-9]+ \(include\) + + +CMake Error at EmptyVersionRange.cmake:[0-9]+ \(find_package\): + find_package specified version range is empty. +Call Stack \(most recent call first\): + CMakeLists.txt:[0-9]+ \(include\) diff --git a/Tests/RunCMake/find_package/EmptyVersionRange.cmake b/Tests/RunCMake/find_package/EmptyVersionRange.cmake new file mode 100644 index 0000000..553e297 --- /dev/null +++ b/Tests/RunCMake/find_package/EmptyVersionRange.cmake @@ -0,0 +1,3 @@ +find_package(VersionRange 2.3...1.2) + +find_package(VersionRange 2.3...<2.3) diff --git a/Tests/RunCMake/find_package/FindVersionRange.cmake b/Tests/RunCMake/find_package/FindVersionRange.cmake index ff84e93..27e5b90 100644 --- a/Tests/RunCMake/find_package/FindVersionRange.cmake +++ b/Tests/RunCMake/find_package/FindVersionRange.cmake @@ -3,19 +3,19 @@ if (NOT VersionRange_FIND_VERSION_COMPLETE STREQUAL VersionRange_SPECIFIED_VERSI message (SEND_ERROR "Wrong value for VersionRange_FIND_VERSION_COMPLETE: ${VersionRange_FIND_VERSION_COMPLETE}") endif() -if (NOT VersionRange_FIND_VERSION VERSION_EQUAL "1.2.3.4") +if (NOT VersionRange_FIND_VERSION VERSION_EQUAL VersionRange_SPECIFIED_VERSION_MIN) message (SEND_ERROR "Wrong value for VersionRange_FIND_VERSION: ${VersionRange_FIND_VERSION}") endif() -if (NOT VersionRange_FIND_VERSION_MAJOR VERSION_EQUAL "1") +if (NOT VersionRange_FIND_VERSION_MAJOR VERSION_EQUAL VersionRange_SPECIFIED_VERSION_MIN_MAJOR) message (SEND_ERROR "Wrong value for VersionRange_FIND_VERSION_MAJOR: ${VersionRange_FIND_VERSION_MAJOR}") endif() -if (NOT VersionRange_FIND_VERSION_MINOR VERSION_EQUAL "2") +if (NOT VersionRange_FIND_VERSION_MINOR VERSION_EQUAL VersionRange_SPECIFIED_VERSION_MIN_MINOR) message (SEND_ERROR "Wrong value for VersionRange_FIND_VERSION_MINOR: ${VersionRange_FIND_VERSION_MINOR}") endif() -if (NOT VersionRange_FIND_VERSION_PATCH VERSION_EQUAL "3") +if (NOT VersionRange_FIND_VERSION_PATCH VERSION_EQUAL VersionRange_SPECIFIED_VERSION_MIN_PATCH) message (SEND_ERROR "Wrong value for VersionRange_FIND_VERSION_PATCH: ${VersionRange_FIND_VERSION_PATCH}") endif() -if (NOT VersionRange_FIND_VERSION_TWEAK VERSION_EQUAL "4") +if (NOT VersionRange_FIND_VERSION_TWEAK VERSION_EQUAL VersionRange_SPECIFIED_VERSION_MIN_TWEAK) message (SEND_ERROR "Wrong value for VersionRange_FIND_VERSION_TWEAK: ${VersionRange_FIND_VERSION_TWEAK}") endif() @@ -35,19 +35,19 @@ else() endif() endif() -if (NOT VersionRange_FIND_VERSION_MIN VERSION_EQUAL "1.2.3.4") - message (SEND_ERROR "Wrong value for VersionRange_FIND_VERSION_MIN: ${VersionRange_FIND_VERSION}") +if (NOT VersionRange_FIND_VERSION_MIN VERSION_EQUAL VersionRange_SPECIFIED_VERSION_MIN) + message (SEND_ERROR "Wrong value for VersionRange_FIND_VERSION_MIN: ${VersionRange_FIND_VERSION_MIN}") endif() -if (NOT VersionRange_FIND_VERSION_MIN_MAJOR VERSION_EQUAL "1") +if (NOT VersionRange_FIND_VERSION_MIN_MAJOR VERSION_EQUAL VersionRange_SPECIFIED_VERSION_MIN_MAJOR) message (SEND_ERROR "Wrong value for VersionRange_FIND_VERSION_MIN_MAJOR: ${VersionRange_FIND_VERSION_MIN_MAJOR}") endif() -if (NOT VersionRange_FIND_VERSION_MIN_MINOR VERSION_EQUAL "2") +if (NOT VersionRange_FIND_VERSION_MIN_MINOR VERSION_EQUAL VersionRange_SPECIFIED_VERSION_MIN_MINOR) message (SEND_ERROR "Wrong value for VersionRange_FIND_VERSION_MIN_MINOR: ${VersionRange_FIND_VERSION_MIN_MINOR}") endif() -if (NOT VersionRange_FIND_VERSION_MIN_PATCH VERSION_EQUAL "3") +if (NOT VersionRange_FIND_VERSION_MIN_PATCH VERSION_EQUAL VersionRange_SPECIFIED_VERSION_MIN_PATCH) message (SEND_ERROR "Wrong value for VersionRange_FIND_VERSION_MIN_PATCH: ${VersionRange_FIND_VERSION_MIN_PATCH}") endif() -if (NOT VersionRange_FIND_VERSION_MIN_TWEAK VERSION_EQUAL "4") +if (NOT VersionRange_FIND_VERSION_MIN_TWEAK VERSION_EQUAL VersionRange_SPECIFIED_VERSION_MIN_TWEAK) message (SEND_ERROR "Wrong value for VersionRange_FIND_VERSION_MIN_TWEAK: ${VersionRange_FIND_VERSION_MIN_TWEAK}") endif() diff --git a/Tests/RunCMake/find_package/RunCMakeTest.cmake b/Tests/RunCMake/find_package/RunCMakeTest.cmake index 7755a70..a899f46 100644 --- a/Tests/RunCMake/find_package/RunCMakeTest.cmake +++ b/Tests/RunCMake/find_package/RunCMakeTest.cmake @@ -30,10 +30,12 @@ run_cmake(CMP0084-OLD) run_cmake(CMP0084-WARN) run_cmake(CMP0084-NEW) run_cmake(WrongVersionRange) +run_cmake(EmptyVersionRange) run_cmake(VersionRangeWithEXACT) run_cmake(VersionRange) run_cmake(VersionRange2) run_cmake(VersionRange3) +run_cmake(VersionRange4) run_cmake(VersionRangeConfig) run_cmake(VersionRangeConfig2) run_cmake(VersionRangeConfigStd) diff --git a/Tests/RunCMake/find_package/VersionRange.cmake b/Tests/RunCMake/find_package/VersionRange.cmake index b3d8950..30b88a8 100644 --- a/Tests/RunCMake/find_package/VersionRange.cmake +++ b/Tests/RunCMake/find_package/VersionRange.cmake @@ -3,6 +3,11 @@ set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}") set (VersionRange_SPECIFIED_VERSION_COMPLETE 1.2.3.4...5.6.7.8) set (VersionRange_SPECIFIED_VERSION_RANGE 1.2.3.4...5.6.7.8) +set (VersionRange_SPECIFIED_VERSION_MIN 1.2.3.4) +set (VersionRange_SPECIFIED_VERSION_MIN_MAJOR 1) +set (VersionRange_SPECIFIED_VERSION_MIN_MINOR 2) +set (VersionRange_SPECIFIED_VERSION_MIN_PATCH 3) +set (VersionRange_SPECIFIED_VERSION_MIN_TWEAK 4) set (VersionRange_SPECIFIED_VERSION_MAX 5.6.7.8) set (VersionRange_SPECIFIED_VERSION_MAX_MAJOR 5) set (VersionRange_SPECIFIED_VERSION_MAX_MINOR 6) diff --git a/Tests/RunCMake/find_package/VersionRange2.cmake b/Tests/RunCMake/find_package/VersionRange2.cmake index d0e685b..9adcc64 100644 --- a/Tests/RunCMake/find_package/VersionRange2.cmake +++ b/Tests/RunCMake/find_package/VersionRange2.cmake @@ -3,6 +3,11 @@ set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}") set (VersionRange_SPECIFIED_VERSION_COMPLETE 1.2.3.4...<5.6.7.8) set (VersionRange_SPECIFIED_VERSION_RANGE 1.2.3.4...<5.6.7.8) +set (VersionRange_SPECIFIED_VERSION_MIN 1.2.3.4) +set (VersionRange_SPECIFIED_VERSION_MIN_MAJOR 1) +set (VersionRange_SPECIFIED_VERSION_MIN_MINOR 2) +set (VersionRange_SPECIFIED_VERSION_MIN_PATCH 3) +set (VersionRange_SPECIFIED_VERSION_MIN_TWEAK 4) set (VersionRange_SPECIFIED_VERSION_MAX 5.6.7.8) set (VersionRange_SPECIFIED_VERSION_MAX_MAJOR 5) set (VersionRange_SPECIFIED_VERSION_MAX_MINOR 6) diff --git a/Tests/RunCMake/find_package/VersionRange3.cmake b/Tests/RunCMake/find_package/VersionRange3.cmake index 76eef1d..41efac4 100644 --- a/Tests/RunCMake/find_package/VersionRange3.cmake +++ b/Tests/RunCMake/find_package/VersionRange3.cmake @@ -5,6 +5,11 @@ set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}") set (VersionRange_SPECIFIED_VERSION_COMPLETE 1.2.3.4...<2.3.4.5) set (VersionRange_SPECIFIED_VERSION_RANGE 1.2.3.4...<2.3.4.5) +set (VersionRange_SPECIFIED_VERSION_MIN 1.2.3.4) +set (VersionRange_SPECIFIED_VERSION_MIN_MAJOR 1) +set (VersionRange_SPECIFIED_VERSION_MIN_MINOR 2) +set (VersionRange_SPECIFIED_VERSION_MIN_PATCH 3) +set (VersionRange_SPECIFIED_VERSION_MIN_TWEAK 4) set (VersionRange_SPECIFIED_VERSION_MAX 2.3.4.5) set (VersionRange_SPECIFIED_VERSION_MAX_MAJOR 2) set (VersionRange_SPECIFIED_VERSION_MAX_MINOR 3) diff --git a/Tests/RunCMake/find_package/VersionRange4.cmake b/Tests/RunCMake/find_package/VersionRange4.cmake new file mode 100644 index 0000000..0953d04 --- /dev/null +++ b/Tests/RunCMake/find_package/VersionRange4.cmake @@ -0,0 +1,39 @@ + +# show the effect of the exclusion or inclusion of the upper endpoint + +set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}") + +set (VersionRange_SPECIFIED_VERSION_COMPLETE 2.3.4.5...2.3.4.5) +set (VersionRange_SPECIFIED_VERSION_RANGE 2.3.4.5...2.3.4.5) +set (VersionRange_SPECIFIED_VERSION_MIN 2.3.4.5) +set (VersionRange_SPECIFIED_VERSION_MIN_MAJOR 2) +set (VersionRange_SPECIFIED_VERSION_MIN_MINOR 3) +set (VersionRange_SPECIFIED_VERSION_MIN_PATCH 4) +set (VersionRange_SPECIFIED_VERSION_MIN_TWEAK 5) +set (VersionRange_SPECIFIED_VERSION_MAX 2.3.4.5) +set (VersionRange_SPECIFIED_VERSION_MAX_MAJOR 2) +set (VersionRange_SPECIFIED_VERSION_MAX_MINOR 3) +set (VersionRange_SPECIFIED_VERSION_MAX_PATCH 4) +set (VersionRange_SPECIFIED_VERSION_MAX_TWEAK 5) + +find_package (VersionRange ${VersionRange_SPECIFIED_VERSION_RANGE}) + +if (NOT VersionRange_FOUND) + message (FATAL_ERROR "Package VersionRange not found.") +endif() + +if (NOT VersionRange_VERSION VERSION_EQUAL "2.3.4.5") + message (SEND_ERROR "Wrong version : ${VersionRange_VERSION}") +endif() +if (NOT VersionRange_VERSION_MAJOR VERSION_EQUAL "2") + message (SEND_ERROR "Wrong major version : ${VersionRange_VERSION_MAJOR}") +endif() +if (NOT VersionRange_VERSION_MINOR VERSION_EQUAL "3") + message (SEND_ERROR "Wrong minor version : ${VersionRange_VERSION_MINOR}") +endif() +if (NOT VersionRange_VERSION_PATCH VERSION_EQUAL "4") + message (SEND_ERROR "Wrong patch version : ${VersionRange_VERSION_PATCH}") +endif() +if (NOT VersionRange_VERSION_TWEAK VERSION_EQUAL "5") + message (SEND_ERROR "Wrong tweak version : ${VersionRange_VERSION_TWEAK}") +endif() -- cgit v0.12