CMake: Extract package dependency finding into functions

Aside from moving the code to reduce duplication, the only changes in
the code are the replacement of @target@ to ${target}, using IN LISTS
with variable names to avoid macro expansion issues and replacing
the if(var) conditions with double variable evaluation
+ NOT STREQUAL "".

The function implementations are now in the same order as they
are used in the Dependencies.cmake files.

Pick-to: 6.4
Task-number: QTBUG-104998
Change-Id: Iaae926414fd2a7cc09c2f5716376caaa0aade74b
Reviewed-by: Jörg Bornemann <joerg.bornemann@qt.io>
This commit is contained in:
Alexandru Croitor 2022-07-13 15:11:40 +02:00
parent 2d2cf95fe1
commit 7853f80e39
3 changed files with 82 additions and 96 deletions

View File

@ -20,76 +20,11 @@ find_dependency(@INSTALL_CMAKE_NAMESPACE@ @main_qt_package_version@
# note: _third_party_deps example: "ICU\\;FALSE\\;1.0\\;i18n uc data;ZLIB\\;FALSE\\;\\;"
set(__qt_@target@_third_party_deps "@third_party_deps@")
foreach(__qt_@target@_target_dep ${__qt_@target@_third_party_deps})
list(GET __qt_@target@_target_dep 0 __qt_@target@_pkg)
list(GET __qt_@target@_target_dep 1 __qt_@target@_is_optional)
list(GET __qt_@target@_target_dep 2 __qt_@target@_version)
list(GET __qt_@target@_target_dep 3 __qt_@target@_components)
list(GET __qt_@target@_target_dep 4 __qt_@target@_optional_components)
set(__qt_@target@_find_package_args "${__qt_@target@_pkg}")
if(__qt_@target@_version)
list(APPEND __qt_@target@_find_package_args "${__qt_@target@_version}")
endif()
if(__qt_@target@_components)
string(REPLACE " " ";" __qt_@target@_components "${__qt_@target@_components}")
list(APPEND __qt_@target@_find_package_args COMPONENTS ${__qt_@target@_components})
endif()
if(__qt_@target@_optional_components)
string(REPLACE " " ";" __qt_@target@_optional_components "${__qt_@target@_optional_components}")
list(APPEND __qt_@target@_find_package_args OPTIONAL_COMPONENTS ${__qt_@target@_optional_components})
endif()
if(__qt_@target@_is_optional)
if(${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY)
list(APPEND __qt_@target@_find_package_args QUIET)
endif()
find_package(${__qt_@target@_find_package_args})
else()
find_dependency(${__qt_@target@_find_package_args})
endif()
endforeach()
_qt_internal_find_third_party_dependencies("@target@" __qt_@target@_third_party_deps)
# Find Qt tool package.
set(__qt_@target@_tool_deps "@main_module_tool_deps@")
if(__qt_@target@_tool_deps AND NOT "${QT_HOST_PATH}" STREQUAL "")
# Make sure that the tools find the host tools first
set(BACKUP_@target@_CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH})
set(BACKUP_@target@_CMAKE_FIND_ROOT_PATH ${CMAKE_FIND_ROOT_PATH})
list(PREPEND CMAKE_PREFIX_PATH "${QT_HOST_PATH_CMAKE_DIR}"
"${_qt_additional_host_packages_prefix_paths}")
list(PREPEND CMAKE_FIND_ROOT_PATH "${QT_HOST_PATH}"
"${_qt_additional_host_packages_root_paths}")
endif()
foreach(__qt_@target@_target_dep ${__qt_@target@_tool_deps})
list(GET __qt_@target@_target_dep 0 __qt_@target@_pkg)
list(GET __qt_@target@_target_dep 1 __qt_@target@_version)
unset(__qt_@target@_find_package_args)
if(${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY)
list(APPEND __qt_@target@_find_package_args QUIET)
endif()
if(${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED)
list(APPEND __qt_@target@_find_package_args REQUIRED)
endif()
find_package(${__qt_@target@_pkg} ${__qt_@target@_version} ${__qt_@target@_find_package_args}
PATHS
${_qt_additional_packages_prefix_paths}
)
if (NOT ${__qt_@target@_pkg}_FOUND)
if(NOT "${QT_HOST_PATH}" STREQUAL "")
set(CMAKE_PREFIX_PATH ${BACKUP_@target@_CMAKE_PREFIX_PATH})
set(CMAKE_FIND_ROOT_PATH ${BACKUP_@target@_CMAKE_FIND_ROOT_PATH})
endif()
return()
endif()
endforeach()
if(__qt_@target@_tool_deps AND NOT "${QT_HOST_PATH}" STREQUAL "")
set(CMAKE_PREFIX_PATH ${BACKUP_@target@_CMAKE_PREFIX_PATH})
set(CMAKE_FIND_ROOT_PATH ${BACKUP_@target@_CMAKE_FIND_ROOT_PATH})
endif()
_qt_internal_find_tool_dependencies("@target@" __qt_@target@_tool_deps)
# note: target_deps example: "Qt6Core\;5.12.0;Qt6Gui\;5.12.0"
set(__qt_@target@_target_deps "@target_deps@")

View File

@ -2,35 +2,7 @@ set(@target@_FOUND FALSE)
# note: _third_party_deps example: "ICU\\;FALSE\\;1.0\\;i18n uc data;ZLIB\\;FALSE\\;\\;"
set(__qt_@target@_third_party_deps "@third_party_deps@")
foreach(__qt_@target@_target_dep ${__qt_@target@_third_party_deps})
list(GET __qt_@target@_target_dep 0 __qt_@target@_pkg)
list(GET __qt_@target@_target_dep 1 __qt_@target@_is_optional)
list(GET __qt_@target@_target_dep 2 __qt_@target@_version)
list(GET __qt_@target@_target_dep 3 __qt_@target@_components)
list(GET __qt_@target@_target_dep 4 __qt_@target@_optional_components)
set(__qt_@target@_find_package_args "${__qt_@target@_pkg}")
if(__qt_@target@_version)
list(APPEND __qt_@target@_find_package_args "${__qt_@target@_version}")
endif()
if(__qt_@target@_components)
string(REPLACE " " ";" __qt_@target@_components "${__qt_@target@_components}")
list(APPEND __qt_@target@_find_package_args COMPONENTS ${__qt_@target@_components})
endif()
if(__qt_@target@_optional_components)
string(REPLACE " " ";" __qt_@target@_optional_components "${__qt_@target@_optional_components}")
list(APPEND __qt_@target@_find_package_args OPTIONAL_COMPONENTS ${__qt_@target@_optional_components})
endif()
if(__qt_@target@_is_optional)
if(${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY)
list(APPEND __qt_@target@_find_package_args QUIET)
endif()
find_package(${__qt_@target@_find_package_args})
else()
find_dependency(${__qt_@target@_find_package_args})
endif()
endforeach()
_qt_internal_find_third_party_dependencies("@target@" __qt_@target@_third_party_deps)
set(__qt_use_no_default_path_for_qt_packages "NO_DEFAULT_PATH")
if(QT_DISABLE_NO_DEFAULT_PATH_IN_QT_PACKAGES)

View File

@ -1,3 +1,82 @@
# Note that target_dep_list does not accept a list of values, but a var name that contains the
# list of dependencies. See foreach block for reference.
macro(_qt_internal_find_third_party_dependencies target target_dep_list)
foreach(__qt_${target}_target_dep IN LISTS ${target_dep_list})
list(GET __qt_${target}_target_dep 0 __qt_${target}_pkg)
list(GET __qt_${target}_target_dep 1 __qt_${target}_is_optional)
list(GET __qt_${target}_target_dep 2 __qt_${target}_version)
list(GET __qt_${target}_target_dep 3 __qt_${target}_components)
list(GET __qt_${target}_target_dep 4 __qt_${target}_optional_components)
set(__qt_${target}_find_package_args "${__qt_${target}_pkg}")
if(__qt_${target}_version)
list(APPEND __qt_${target}_find_package_args "${__qt_${target}_version}")
endif()
if(__qt_${target}_components)
string(REPLACE " " ";" __qt_${target}_components "${__qt_${target}_components}")
list(APPEND __qt_${target}_find_package_args COMPONENTS ${__qt_${target}_components})
endif()
if(__qt_${target}_optional_components)
string(REPLACE " " ";"
__qt_${target}_optional_components "${__qt_${target}_optional_components}")
list(APPEND __qt_${target}_find_package_args
OPTIONAL_COMPONENTS ${__qt_${target}_optional_components})
endif()
if(__qt_${target}_is_optional)
if(${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY)
list(APPEND __qt_${target}_find_package_args QUIET)
endif()
find_package(${__qt_${target}_find_package_args})
else()
find_dependency(${__qt_${target}_find_package_args})
endif()
endforeach()
endmacro()
# Note that target_dep_list does not accept a list of values, but a var name that contains the
# list of dependencies. See foreach block for reference.
macro(_qt_internal_find_tool_dependencies target target_dep_list)
if(NOT "${${target_dep_list}}" STREQUAL "" AND NOT "${QT_HOST_PATH}" STREQUAL "")
# Make sure that the tools find the host tools first
set(BACKUP_${target}_CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH})
set(BACKUP_${target}_CMAKE_FIND_ROOT_PATH ${CMAKE_FIND_ROOT_PATH})
list(PREPEND CMAKE_PREFIX_PATH "${QT_HOST_PATH_CMAKE_DIR}"
"${_qt_additional_host_packages_prefix_paths}")
list(PREPEND CMAKE_FIND_ROOT_PATH "${QT_HOST_PATH}"
"${_qt_additional_host_packages_root_paths}")
endif()
foreach(__qt_${target}_target_dep IN LISTS ${target_dep_list})
list(GET __qt_${target}_target_dep 0 __qt_${target}_pkg)
list(GET __qt_${target}_target_dep 1 __qt_${target}_version)
unset(__qt_${target}_find_package_args)
if(${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY)
list(APPEND __qt_${target}_find_package_args QUIET)
endif()
if(${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED)
list(APPEND __qt_${target}_find_package_args REQUIRED)
endif()
find_package(${__qt_${target}_pkg}
${__qt_${target}_version}
${__qt_${target}_find_package_args}
PATHS
${_qt_additional_packages_prefix_paths}
)
if (NOT ${__qt_${target}_pkg}_FOUND)
if(NOT "${QT_HOST_PATH}" STREQUAL "")
set(CMAKE_PREFIX_PATH ${BACKUP_${target}_CMAKE_PREFIX_PATH})
set(CMAKE_FIND_ROOT_PATH ${BACKUP_${target}_CMAKE_FIND_ROOT_PATH})
endif()
return()
endif()
endforeach()
if(NOT "${${target_dep_list}}" STREQUAL "" AND NOT "${QT_HOST_PATH}" STREQUAL "")
set(CMAKE_PREFIX_PATH ${BACKUP_${target}_CMAKE_PREFIX_PATH})
set(CMAKE_FIND_ROOT_PATH ${BACKUP_${target}_CMAKE_FIND_ROOT_PATH})
endif()
endmacro()
# Please note the target_dep_list accepts not the actual list values but the list names that
# contain preformed dependencies. See foreach block for reference.
# The same applies for find_dependency_path_list.