path: root/cmake/
diff options
authorAlexandru Croitor <>2021-08-17 17:03:02 +0200
committerAlexandru Croitor <>2021-08-19 16:42:56 +0200
commit6b6d42f6b814ab7814c795744fd6212b9b4d54fb (patch)
treee13d7a23150bb51c2403f2ceef6536579c363073 /cmake/
parentd65ccd399929ae51f192655f23b521904767bbcf (diff)
The QT_ADDITIONAL_PACKAGES_PREFIX_PATH variable was introduced to allow specifying extra locations to find Qt packages. The reason it was introduced instead of just using CMAKE_PREFIX_PATH is because the Qt6 component find_package call uses NO_DEFAULT_PATH which means CMAKE_PREFIX_PATH is ignored. We use NO_DEFAULT_PATH to ensure we don't accidentally pick up system / distro Qt packages. The paths from QT_ADDITIONAL_PACKAGES_PREFIX_PATH are added to the find_package PATHS option in the Qt6 package, each ModuleDependencies.cmake file and some other places. Unfortunately that's not enough to make it work for cross-builds. Imagine the following scenario. host qtbase, qtdeclarative installed in /host_qt target qtbase installed in /target_qtbase target qtdeclarative installed in /target_qtdeclarative We want to cross-build qtlottie. We configure qtlottie as follows /target_qtbase/bin/qt-configure-module /qtlottie_src -- -DQT_ADDITIONAL_PACKAGES_PREFIX_PATH=/target_qtdeclarative We expect the target QtQuick package to be found, but it won't be. The reason is that QT_ADDITIONAL_PACKAGES_PREFIX_PATH is added to the PATHs option, but we don't adjust CMAKE_FIND_ROOT_PATH. Without adding the new paths in CMAKE_FIND_ROOT_PATH, CMake will re-root the passed PATHs under the existing CMAKE_FIND_ROOT_PATH, which is QT_TOOLCHAIN_RELOCATABLE_INSTALL_PREFIX, which evaluates to /target_qtbase. There is no QtQuick package there. To fix this, prepend the values of QT_ADDITIONAL_PACKAGES_PREFIX_PATH to CMAKE_FIND_ROOT_PATH. The location where we currently do CMAKE_FIND_ROOT_PATH manipulations is in the qt.toolchain.cmake file, so to be consistent, we prepend the new prefixes there as well. We need to adjust both CMAKE_FIND_ROOT_PATH and CMAKE_PREFIX_PATH, due the path re-rooting bug in CMake. See as well as the existing comment in qt.toolchain.cmake marked with REROOT_PATH_ISSUE_MARKER. We also need to do a few more things to make the setup work Because Qt6Config uses NO_DEFAULT_PATH, the CMAKE_PREFIX_PATH adjustments we do in the toolchain file are not enough, so we still need to add the same prefixes to the Qt6Config find_package PATHS option. One would ask why do we need to adjust CMAKE_PREFIX_PATH at all then. It's for find_package(Qt6Foo) calls to work which don't go through the Qt6Config umbrella package. To make the CMake re-rooting behavior happy, we need to ensure the provided paths are absolute. So we iterate over the values of QT_ADDITIONAL_PACKAGES_PREFIX_PATH, to make them absolute. We do the same for the environment variable. We need to append lib/cmake to the prefixes which are added to CMAKE_PREFIX_PATH, otherwise the CMake re-rooting bug is hit. We need to specify the Qt6 package location (${_qt_cmake_dir}) to the PATHS option in the various files, to ensure that dependency resolution can jump around between the Qt6 dir and the additional prefixes. Previously the dependency lookup code assumed that all dependencies would be within the same prefix. The same is needed for qt and qml plugin dependency lookup. Amends 7bb91398f25cb2016c0558fd397b376f413e3e96 Amends 60c87c68016c6f02b0eddd4002f75a49ab51d4a8 Amends 5bbd700124d13a292ff8bae6045316112500e230 Pick-to: 6.2 Fixes: QTBUG-95854 Change-Id: I35ae82330fec427d0d38fc9a0542ffafff52556a Reviewed-by: Alexey Edelev <> Reviewed-by: Joerg Bornemann <>
Diffstat (limited to 'cmake/')
1 files changed, 36 insertions, 5 deletions
diff --git a/cmake/ b/cmake/
index 2c4058f0da..7ad92faeb2 100644
--- a/cmake/
+++ b/cmake/
@@ -47,9 +47,41 @@ elseif(APPLE AND CMAKE_SYSTEM_NAME STREQUAL "iOS")
list(APPEND CMAKE_MODULE_PATH "${_qt_import_prefix}/ios")
-set(QT_ADDITIONAL_PACKAGES_PREFIX_PATH "" CACHE STRING "Additional directories where find(Qt6 ...) components are searched")
-file(TO_CMAKE_PATH "${QT_ADDITIONAL_PACKAGES_PREFIX_PATH}" _qt_additional_packages_prefix_path)
-file(TO_CMAKE_PATH "$ENV{QT_ADDITIONAL_PACKAGES_PREFIX_PATH}" _qt_additional_packages_prefix_path_env)
+ "Additional directories where find(Qt6 ...) components are searched")
+# Collect additional package prefix paths to look for Qt packages, both from command line and the
+# env variable.
+if(NOT DEFINED _qt_additional_packages_prefix_paths)
+ set(_qt_additional_packages_prefix_paths "")
+ set(_qt_additional_packages_prefixes "")
+ list(APPEND _qt_additional_packages_prefixes ${QT_ADDITIONAL_PACKAGES_PREFIX_PATH})
+ endif()
+ list(APPEND _qt_additional_packages_prefixes $ENV{QT_ADDITIONAL_PACKAGES_PREFIX_PATH})
+ endif()
+ foreach(_qt_additional_path IN LISTS _qt_additional_packages_prefixes)
+ file(TO_CMAKE_PATH "${_qt_additional_path}" _qt_additional_path)
+ # The prefix paths need to end with lib/cmake to ensure the packages are found when
+ # cross compiling. Search for REROOT_PATH_ISSUE_MARKER in the qt.toolchain.cmake file for
+ # details.
+ # We must pass the values via the PATHS options because the main find_package call uses
+ # NO_DEFAULT_PATH, and thus CMAKE_PREFIX_PATH values are discarded.
+ # CMAKE_FIND_ROOT_PATH values are not discarded and togegher with the PATHS option, it
+ # ensures packages from additional prefixes are found.
+ if(NOT _qt_additional_path MATCHES "/lib/cmake$")
+ string(APPEND _qt_additional_path "/lib/cmake")
+ endif()
+ list(APPEND _qt_additional_packages_prefix_paths "${_qt_additional_path}")
+ endforeach()
+ unset(_qt_additional_path)
+ unset(_qt_additional_packages_prefixes)
# Public helpers available to all Qt packages.
@@ -121,8 +153,7 @@ foreach(module ${@INSTALL_CMAKE_NAMESPACE@_FIND_COMPONENTS})
- ${_qt_additional_packages_prefix_path}
- ${_qt_additional_packages_prefix_path_env}
+ ${_qt_additional_packages_prefix_paths}