CMake: Fix qt_find_package to work when CMP00126 is set to NEW

When CMP00126 is set to NEW, set(CACHE) doesn't remove regular
variable bindings with the same name as the cache variable.

This introduced an issue in qt_find_package, which called
find_package(CONFIG QUIET) to try and find a package config file, but
did not clean up the non-cache _FOUND variable which is automatically
set to 0 by CMake if no Config file is found.

Make sure to unset both the regular and cache _FOUND variables if either
the package config file was not found, or if none of the provided
targets found even if the Config file was found.

Also move the _DIR cache variable cleaning into the same code block.

Amends 4c31ce68d5
Amends 34b1c1c23c

Fixes: QTBUG-95635
Pick-to: 6.2
Change-Id: I871987217526e0f0a20038a8da52625838e49488
Reviewed-by: Craig Scott <craig.scott@qt.io>
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
This commit is contained in:
Alexandru Croitor 2021-08-06 18:39:04 +02:00
parent 32725806c8
commit 72a1e55472

View File

@ -86,16 +86,31 @@ macro(qt_find_package)
# in their own way. CMake has FindSQLite3.cmake and with the original
# qt_find_package(SQLite3) call it is our intention to use the cmake package
# in module mode.
if (${ARGV0}_FOUND AND arg_PROVIDED_TARGETS)
unset(any_target_found)
unset(_qt_any_target_found)
unset(_qt_should_unset_found_var)
if(${ARGV0}_FOUND AND arg_PROVIDED_TARGETS)
foreach(expected_target ${arg_PROVIDED_TARGETS})
if (TARGET ${expected_target})
set(any_target_found TRUE)
set(_qt_any_target_found TRUE)
break()
endif()
endforeach()
if(NOT any_target_found)
unset(${ARGV0}_FOUND)
if(NOT _qt_any_target_found)
set(_qt_should_unset_found_var TRUE)
endif()
endif()
# If we consider the package not to be found, make sure to unset both regular
# and CACHE vars, otherwise CMP0126 set to NEW might cause issues with
# packages not being found correctly.
if(NOT ${ARGV0}_FOUND OR _qt_should_unset_found_var)
unset(${ARGV0}_FOUND)
unset(${ARGV0}_FOUND CACHE)
# Unset the NOTFOUND ${package}_DIR var that might have been set by the previous
# find_package call, to get rid of "not found" messages in the feature summary
# if the package is found by the next find_package call.
if(DEFINED CACHE{${ARGV0}_DIR} AND NOT ${ARGV0}_DIR)
unset(${ARGV0}_DIR CACHE)
endif()
endif()
endif()
@ -111,13 +126,6 @@ macro(qt_find_package)
# E.g. find_package(Qt6 COMPONENTS BuildInternals) followed by
# qt_find_package(Qt6 COMPONENTS Core) doesn't end up calling find_package(Qt6Core).
if (NOT ${ARGV0}_FOUND AND NOT _qt_find_package_skip_find_package)
# Unset the NOTFOUND ${package}_DIR var that might have been set by the previous
# find_package call, to get rid of "not found" messagees in the feature summary
# if the package is found by the next find_package call.
if(DEFINED CACHE{${ARGV0}_DIR} AND NOT ${ARGV0}_DIR)
unset(${ARGV0}_DIR CACHE)
endif()
# Call original function without our custom arguments.
find_package(${arg_UNPARSED_ARGUMENTS})
endif()