From 8f50f135aefa3f287a87c80cc8edb3deee8ee198 Mon Sep 17 00:00:00 2001 From: Andrea Pappacoda Date: Mon, 1 Aug 2022 00:52:06 +0200 Subject: FPHSA: Improve error message when VERSION_VAR is empty or has been unset() Previously, when `VERSION_VAR` is set but empty, FPHSA emitted a confusing error message, saying that the package was found but of an unsuitable version "". Fix the error message to state that the package was not found. Fixes: #23807 --- Modules/FindPackageHandleStandardArgs.cmake | 6 ++++-- Tests/RunCMake/FPHSA/RunCMakeTest.cmake | 5 ++++- Tests/RunCMake/FPHSA/empty_version-result.txt | 1 + Tests/RunCMake/FPHSA/empty_version-stderr.txt | 9 +++++++++ Tests/RunCMake/FPHSA/empty_version.cmake | 1 + 5 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 Tests/RunCMake/FPHSA/empty_version-result.txt create mode 100644 Tests/RunCMake/FPHSA/empty_version-stderr.txt create mode 100644 Tests/RunCMake/FPHSA/empty_version.cmake diff --git a/Modules/FindPackageHandleStandardArgs.cmake b/Modules/FindPackageHandleStandardArgs.cmake index fbcf7cd..6de7dbf 100644 --- a/Modules/FindPackageHandleStandardArgs.cmake +++ b/Modules/FindPackageHandleStandardArgs.cmake @@ -535,9 +535,10 @@ function(FIND_PACKAGE_HANDLE_STANDARD_ARGS _NAME _FIRST_ARG) set(VERSION_MSG "") set(VERSION_OK TRUE) - # check with DEFINED here as the requested or found version may be "0" + # check that the version variable is not empty to avoid emitting a misleading + # message (i.e. `Found unsuitable version ""`) if (DEFINED ${_NAME}_FIND_VERSION) - if(DEFINED ${FPHSA_VERSION_VAR}) + if(NOT "${${FPHSA_VERSION_VAR}}" STREQUAL "") set(_FOUND_VERSION ${${FPHSA_VERSION_VAR}}) if (FPHSA_HANDLE_VERSION_RANGE) set (FPCV_HANDLE_VERSION_RANGE HANDLE_VERSION_RANGE) @@ -547,6 +548,7 @@ function(FIND_PACKAGE_HANDLE_STANDARD_ARGS _NAME _FIRST_ARG) find_package_check_version ("${_FOUND_VERSION}" VERSION_OK RESULT_MESSAGE_VARIABLE VERSION_MSG ${FPCV_HANDLE_VERSION_RANGE}) else() + set(VERSION_OK FALSE) # if the package was not found, but a version was given, add that to the output: if(${_NAME}_FIND_VERSION_EXACT) set(VERSION_MSG "(Required is exact version \"${${_NAME}_FIND_VERSION}\")") diff --git a/Tests/RunCMake/FPHSA/RunCMakeTest.cmake b/Tests/RunCMake/FPHSA/RunCMakeTest.cmake index 28b8570..e389e6b 100644 --- a/Tests/RunCMake/FPHSA/RunCMakeTest.cmake +++ b/Tests/RunCMake/FPHSA/RunCMakeTest.cmake @@ -38,9 +38,12 @@ run_cmake(exact_1.2.3.5) unset(RunCMake_DEFAULT_stderr) # check if searching for a version 0 works -list(APPEND RunCMake_TEST_OPTIONS "-DCMAKE_MODULE_PATH=${CMAKE_CURRENT_LIST_DIR}" "-DPseudo_VERSION=0") +set(RunCMake_TEST_OPTIONS "-DCMAKE_MODULE_PATH=${CMAKE_CURRENT_LIST_DIR}" "-DPseudo_VERSION=0") run_cmake(exact_0_matching) +set(RunCMake_TEST_OPTIONS "-DCMAKE_MODULE_PATH=${CMAKE_CURRENT_LIST_DIR}" "-DPseudo_VERSION=") +run_cmake(empty_version) + # check custom error message set(RunCMake_TEST_OPTIONS "-DCMAKE_MODULE_PATH=${CMAKE_CURRENT_LIST_DIR}" "-DCustomMessage_VERSION=1.2.3.4") run_cmake(custom_message_1) diff --git a/Tests/RunCMake/FPHSA/empty_version-result.txt b/Tests/RunCMake/FPHSA/empty_version-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/FPHSA/empty_version-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/FPHSA/empty_version-stderr.txt b/Tests/RunCMake/FPHSA/empty_version-stderr.txt new file mode 100644 index 0000000..3b60c7a --- /dev/null +++ b/Tests/RunCMake/FPHSA/empty_version-stderr.txt @@ -0,0 +1,9 @@ +^CMake Error at [^ +]*/Modules/FindPackageHandleStandardArgs.cmake:[0-9]+ \(message\): + Could NOT find Pseudo: \(Required is at least version "1"\) \(found TRUE\) +Call Stack \(most recent call first\): + [^ +]*/Modules/FindPackageHandleStandardArgs.cmake:[0-9]+ \(_FPHSA_FAILURE_MESSAGE\) + FindPseudo.cmake:[0-9]+ \(find_package_handle_standard_args\) + empty_version.cmake:[0-9]+ \(find_package\) + CMakeLists.txt:[0-9]+ \(include\)$ diff --git a/Tests/RunCMake/FPHSA/empty_version.cmake b/Tests/RunCMake/FPHSA/empty_version.cmake new file mode 100644 index 0000000..7b7ff34 --- /dev/null +++ b/Tests/RunCMake/FPHSA/empty_version.cmake @@ -0,0 +1 @@ +find_package(Pseudo 1 REQUIRED) -- cgit v0.12