From 28c8f8e92abb453fef80c5df25d7a30bbcb21ca0 Mon Sep 17 00:00:00 2001 From: Alexandru Croitor Date: Thu, 7 Mar 2024 18:02:56 +0100 Subject: [PATCH] CMake: Pass along the version in FindWrap cmake scripts The QtFindWrapHelper include used in FindWrap scripts can choose to call find_package() on one of two packages. The packages might set a VERSION variable. We should set the same version for the FindWrap package as well. There are two sources where the version will be queried from. First from the underlying ${package_name}_VERSION variable. And if that is empty, from a property that is set by qt_find_package() The former might be empty because find_package might be called from a function instead of a macro, which is the case for any find_package call in configure.cmake files that get loaded by the qt_feature_evaluate_features function. Thus we need the indirection via the property that qt_find_package will set. Change-Id: I57fd818cb9dedf5e27a6d805e3d817d8d18be36d Reviewed-by: Alexey Edelev --- cmake/QtFindPackageHelpers.cmake | 9 +++++++++ cmake/QtFindWrapHelper.cmake | 31 ++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/cmake/QtFindPackageHelpers.cmake b/cmake/QtFindPackageHelpers.cmake index dd10bde75a1..76b67572072 100644 --- a/cmake/QtFindPackageHelpers.cmake +++ b/cmake/QtFindPackageHelpers.cmake @@ -190,6 +190,15 @@ macro(qt_find_package) PROPERTIES INTERFACE_QT_PACKAGE_VERSION ${ARGV1}) endif() + # Save the retrieved package version. + set(_qt_find_package_found_version "") + if(${ARGV0}_VERSION) + set(_qt_find_package_found_version "${${ARGV0}_VERSION}") + set_target_properties(${qt_find_package_target_name} + PROPERTIES + _qt_package_found_version "${_qt_find_package_found_version}") + endif() + if(arg_COMPONENTS) string(REPLACE ";" " " components_as_string "${arg_COMPONENTS}") set_property(TARGET ${qt_find_package_target_name} diff --git a/cmake/QtFindWrapHelper.cmake b/cmake/QtFindWrapHelper.cmake index a8d3da49d1a..b17f2133bba 100644 --- a/cmake/QtFindWrapHelper.cmake +++ b/cmake/QtFindWrapHelper.cmake @@ -80,10 +80,39 @@ macro(qt_find_package_system_or_bundled _unique_prefix) INTERFACE_QT_3RD_PARTY_PACKAGE_TYPE "${${_unique_prefix}_qt_package_type}") + + # This might not be set, because qt_find_package() is called from a configure.cmake file via + # qt_feature_evaluate_features, which means the _VERSION var is confined to the function + # scope. + if(${${_unique_prefix}_qt_package_name_to_use}_VERSION) + set(_qfwrap_${_unique_prefix}_package_version + "${${${_unique_prefix}_qt_package_name_to_use}_VERSION}" + ) + else() + # We set this in qt_find_package, so try to retrieve it. + get_target_property(_qfwrap_${_unique_prefix}_package_version_from_prop + "${${_unique_prefix}_qt_package_target_to_use}" _qt_package_found_version) + if(_qfwrap_${_unique_prefix}_package_version_from_prop) + set(_qfwrap_${_unique_prefix}_package_version + "${_qfwrap_${_unique_prefix}_package_version_from_prop}" + ) + else() + set(_qfwrap_${_unique_prefix}_package_version "") + endif() + endif() + + if(_qfwrap_${_unique_prefix}_package_version) + set(_qfwrap_${_unique_prefix}_package_version_option + VERSION_VAR "_qfwrap_${_unique_prefix}_package_version" + ) + endif() + include(FindPackageHandleStandardArgs) find_package_handle_standard_args( Wrap${_qfwrap_${_unique_prefix}_FRIENDLY_PACKAGE_NAME} - DEFAULT_MSG ${_qfwrap_${_unique_prefix}_WRAP_PACKAGE_FOUND_VAR_NAME}) + REQUIRED_VARS ${_qfwrap_${_unique_prefix}_WRAP_PACKAGE_FOUND_VAR_NAME} + ${_qfwrap_${_unique_prefix}_package_version_option} + ) elseif(${_unique_prefix}_qt_package_type STREQUAL "bundled") message(FATAL_ERROR "Can't find ${${_unique_prefix}_qt_package_target_to_use}.") endif()