CMake: Fix QT_ADDITIONAL_PACKAGES_PREFIX_PATH for cross-builds
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 https://gitlab.kitware.com/cmake/cmake/-/issues/21937 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 Dependencies.cmake.in 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. Amends7bb91398f2
Amends60c87c6801
Amends5bbd700124
Pick-to: 6.2 Fixes: QTBUG-95854 Change-Id: I35ae82330fec427d0d38fc9a0542ffafff52556a Reviewed-by: Alexey Edelev <alexey.edelev@qt.io> Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
This commit is contained in:
parent
d65ccd3999
commit
6b6d42f6b8
@ -47,9 +47,41 @@ elseif(APPLE AND CMAKE_SYSTEM_NAME STREQUAL "iOS")
|
|||||||
list(APPEND CMAKE_MODULE_PATH "${_qt_import_prefix}/ios")
|
list(APPEND CMAKE_MODULE_PATH "${_qt_import_prefix}/ios")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
set(QT_ADDITIONAL_PACKAGES_PREFIX_PATH "" CACHE STRING "Additional directories where find(Qt6 ...) components are searched")
|
set(QT_ADDITIONAL_PACKAGES_PREFIX_PATH "" CACHE STRING
|
||||||
file(TO_CMAKE_PATH "${QT_ADDITIONAL_PACKAGES_PREFIX_PATH}" _qt_additional_packages_prefix_path)
|
"Additional directories where find(Qt6 ...) components are searched")
|
||||||
file(TO_CMAKE_PATH "$ENV{QT_ADDITIONAL_PACKAGES_PREFIX_PATH}" _qt_additional_packages_prefix_path_env)
|
|
||||||
|
# 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 "")
|
||||||
|
if(QT_ADDITIONAL_PACKAGES_PREFIX_PATH)
|
||||||
|
list(APPEND _qt_additional_packages_prefixes ${QT_ADDITIONAL_PACKAGES_PREFIX_PATH})
|
||||||
|
endif()
|
||||||
|
if(DEFINED ENV{QT_ADDITIONAL_PACKAGES_PREFIX_PATH}
|
||||||
|
AND NOT "$ENV{QT_ADDITIONAL_PACKAGES_PREFIX_PATH}" STREQUAL "")
|
||||||
|
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)
|
||||||
|
endif()
|
||||||
|
|
||||||
# Public helpers available to all Qt packages.
|
# Public helpers available to all Qt packages.
|
||||||
include("${CMAKE_CURRENT_LIST_DIR}/QtFeature.cmake")
|
include("${CMAKE_CURRENT_LIST_DIR}/QtFeature.cmake")
|
||||||
@ -121,8 +153,7 @@ foreach(module ${@INSTALL_CMAKE_NAMESPACE@_FIND_COMPONENTS})
|
|||||||
${_@INSTALL_CMAKE_NAMESPACE@_FIND_PARTS_QUIET}
|
${_@INSTALL_CMAKE_NAMESPACE@_FIND_PARTS_QUIET}
|
||||||
PATHS
|
PATHS
|
||||||
${_qt_cmake_dir}
|
${_qt_cmake_dir}
|
||||||
${_qt_additional_packages_prefix_path}
|
${_qt_additional_packages_prefix_paths}
|
||||||
${_qt_additional_packages_prefix_path_env}
|
|
||||||
${QT_EXAMPLES_CMAKE_PREFIX_PATH}
|
${QT_EXAMPLES_CMAKE_PREFIX_PATH}
|
||||||
${__qt_find_package_host_qt_path}
|
${__qt_find_package_host_qt_path}
|
||||||
${__qt_use_no_default_path_for_qt_packages}
|
${__qt_use_no_default_path_for_qt_packages}
|
||||||
|
@ -8,8 +8,8 @@ endif()
|
|||||||
find_dependency(@INSTALL_CMAKE_NAMESPACE@ @PROJECT_VERSION@
|
find_dependency(@INSTALL_CMAKE_NAMESPACE@ @PROJECT_VERSION@
|
||||||
PATHS
|
PATHS
|
||||||
"${CMAKE_CURRENT_LIST_DIR}/.."
|
"${CMAKE_CURRENT_LIST_DIR}/.."
|
||||||
${_qt_additional_packages_prefix_path}
|
"${_qt_cmake_dir}"
|
||||||
${_qt_additional_packages_prefix_path_env}
|
${_qt_additional_packages_prefix_paths}
|
||||||
${QT_EXAMPLES_CMAKE_PREFIX_PATH}
|
${QT_EXAMPLES_CMAKE_PREFIX_PATH}
|
||||||
${__qt_use_no_default_path_for_qt_packages}
|
${__qt_use_no_default_path_for_qt_packages}
|
||||||
)
|
)
|
||||||
@ -70,8 +70,7 @@ foreach(__qt_@target@_target_dep ${__qt_@target@_tool_deps})
|
|||||||
endif()
|
endif()
|
||||||
find_package(${__qt_@target@_pkg} ${__qt_@target@_version} ${__qt_@target@_find_package_args}
|
find_package(${__qt_@target@_pkg} ${__qt_@target@_version} ${__qt_@target@_find_package_args}
|
||||||
PATHS
|
PATHS
|
||||||
${_qt_additional_packages_prefix_path}
|
${_qt_additional_packages_prefix_paths}
|
||||||
${_qt_additional_packages_prefix_path_env}
|
|
||||||
)
|
)
|
||||||
if (NOT ${__qt_@target@_pkg}_FOUND)
|
if (NOT ${__qt_@target@_pkg}_FOUND)
|
||||||
if(NOT "${QT_HOST_PATH}" STREQUAL "")
|
if(NOT "${QT_HOST_PATH}" STREQUAL "")
|
||||||
@ -88,7 +87,7 @@ endif()
|
|||||||
|
|
||||||
# note: target_deps example: "Qt6Core\;5.12.0;Qt6Gui\;5.12.0"
|
# note: target_deps example: "Qt6Core\;5.12.0;Qt6Gui\;5.12.0"
|
||||||
set(__qt_@target@_target_deps "@target_deps@")
|
set(__qt_@target@_target_deps "@target_deps@")
|
||||||
set(__qt_@target@_find_dependency_paths "${CMAKE_CURRENT_LIST_DIR}/..")
|
set(__qt_@target@_find_dependency_paths "${CMAKE_CURRENT_LIST_DIR}/.." "${_qt_cmake_dir}")
|
||||||
_qt_internal_find_dependencies(__qt_@target@_target_deps __qt_@target@_find_dependency_paths)
|
_qt_internal_find_dependencies(__qt_@target@_target_deps __qt_@target@_find_dependency_paths)
|
||||||
|
|
||||||
set(_@QT_CMAKE_EXPORT_NAMESPACE@@target@_MODULE_DEPENDENCIES "@qt_module_dependencies@")
|
set(_@QT_CMAKE_EXPORT_NAMESPACE@@target@_MODULE_DEPENDENCIES "@qt_module_dependencies@")
|
||||||
|
@ -292,12 +292,15 @@ function(qt_internal_create_plugin_depends_file target)
|
|||||||
|
|
||||||
if(third_party_deps OR target_deps)
|
if(third_party_deps OR target_deps)
|
||||||
# Setup build and install paths
|
# Setup build and install paths
|
||||||
set(find_dependency_paths "\${CMAKE_CURRENT_LIST_DIR}/..")
|
|
||||||
|
# Plugins should look for their dependencies in their associated module package folder as
|
||||||
|
# well as the Qt6 package folder which is stored by the Qt6 package in _qt_cmake_dir.
|
||||||
|
set(find_dependency_paths "\${CMAKE_CURRENT_LIST_DIR}/..;\${_qt_cmake_dir}")
|
||||||
if(plugin_install_package_suffix)
|
if(plugin_install_package_suffix)
|
||||||
set(path_suffix "${INSTALL_CMAKE_NAMESPACE}${plugin_install_package_suffix}")
|
set(path_suffix "${INSTALL_CMAKE_NAMESPACE}${plugin_install_package_suffix}")
|
||||||
if(plugin_install_package_suffix MATCHES "/QmlPlugins")
|
if(plugin_install_package_suffix MATCHES "/QmlPlugins")
|
||||||
# Qml plugins are one folder deeper.
|
# Qml plugins are one folder deeper.
|
||||||
set(find_dependency_paths "\${CMAKE_CURRENT_LIST_DIR}/../..")
|
set(find_dependency_paths "\${CMAKE_CURRENT_LIST_DIR}/../..;\${_qt_cmake_dir}")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
else()
|
else()
|
||||||
|
@ -16,8 +16,7 @@ macro(_qt_internal_find_dependencies target_dep_list find_dependency_path_list)
|
|||||||
${__qt_pkg_names}
|
${__qt_pkg_names}
|
||||||
PATHS
|
PATHS
|
||||||
${${find_dependency_path_list}}
|
${${find_dependency_path_list}}
|
||||||
${_qt_additional_packages_prefix_path}
|
${_qt_additional_packages_prefix_paths}
|
||||||
${_qt_additional_packages_prefix_path_env}
|
|
||||||
${QT_EXAMPLES_CMAKE_PREFIX_PATH}
|
${QT_EXAMPLES_CMAKE_PREFIX_PATH}
|
||||||
${__qt_use_no_default_path_for_qt_packages}
|
${__qt_use_no_default_path_for_qt_packages}
|
||||||
)
|
)
|
||||||
|
@ -58,13 +58,58 @@ get_filename_component(QT_TOOLCHAIN_RELOCATABLE_INSTALL_PREFIX
|
|||||||
# one level higher is what we're looking for.
|
# one level higher is what we're looking for.
|
||||||
get_filename_component(QT_TOOLCHAIN_RELOCATABLE_CMAKE_DIR "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
|
get_filename_component(QT_TOOLCHAIN_RELOCATABLE_CMAKE_DIR "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
|
||||||
|
|
||||||
|
# REROOT_PATH_ISSUE_MARKER
|
||||||
# There's a subdirectory check in cmake's cmFindCommon::RerootPaths() function, that doesn't handle
|
# There's a subdirectory check in cmake's cmFindCommon::RerootPaths() function, that doesn't handle
|
||||||
# the case of CMAKE_PREFIX_PATH == CMAKE_FIND_ROOT_PATH for a particular pair of entries.
|
# the case of CMAKE_PREFIX_PATH == CMAKE_FIND_ROOT_PATH for a particular pair of entries.
|
||||||
# Instead of collapsing the search prefix (which is the case when one is a subdir of the other),
|
# Instead of collapsing the search prefix (which is the case when one is a subdir of the other),
|
||||||
# it concatenates them creating an invalid path. Workaround it by setting the root path to the
|
# it concatenates them creating an invalid path. Workaround it by setting the root path to the
|
||||||
# Qt install prefix, and the prefix path to the lib/cmake subdir.
|
# Qt install prefix, and the prefix path to the lib/cmake subdir.
|
||||||
set(CMAKE_PREFIX_PATH "${QT_TOOLCHAIN_RELOCATABLE_CMAKE_DIR};${CMAKE_PREFIX_PATH}")
|
list(PREPEND CMAKE_PREFIX_PATH "${QT_TOOLCHAIN_RELOCATABLE_CMAKE_DIR}")
|
||||||
set(CMAKE_FIND_ROOT_PATH "${QT_TOOLCHAIN_RELOCATABLE_INSTALL_PREFIX};${CMAKE_FIND_ROOT_PATH}")
|
list(PREPEND CMAKE_FIND_ROOT_PATH "${QT_TOOLCHAIN_RELOCATABLE_INSTALL_PREFIX}")
|
||||||
|
|
||||||
|
# Handle packages located in QT_ADDITIONAL_PACKAGES_PREFIX_PATH when cross-compiling. Needed for
|
||||||
|
# Conan.
|
||||||
|
# We prepend to CMAKE_PREFIX_PATH so that a find_package(Qt6Foo) call works, without having to go
|
||||||
|
# through the Qt6 umbrella package. The paths must end in lib/cmake to esnsure the package is found.
|
||||||
|
# See REROOT_PATH_ISSUE_MARKER.
|
||||||
|
# We prepend to CMAKE_FIND_ROOT_PATH, due to the bug mentioned at REROOT_PATH_ISSUE_MARKER.
|
||||||
|
set(__qt_toolchain_additional_packages_prefixes "")
|
||||||
|
if(QT_ADDITIONAL_PACKAGES_PREFIX_PATH)
|
||||||
|
list(APPEND __qt_toolchain_additional_packages_prefixes
|
||||||
|
${QT_ADDITIONAL_PACKAGES_PREFIX_PATH})
|
||||||
|
endif()
|
||||||
|
if(DEFINED ENV{QT_ADDITIONAL_PACKAGES_PREFIX_PATH}
|
||||||
|
AND NOT "$ENV{QT_ADDITIONAL_PACKAGES_PREFIX_PATH}" STREQUAL "")
|
||||||
|
list(APPEND __qt_toolchain_additional_packages_prefixes
|
||||||
|
$ENV{QT_ADDITIONAL_PACKAGES_PREFIX_PATH})
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(__qt_toolchain_additional_packages_prefixes)
|
||||||
|
set(__qt_toolchain_additional_packages_root_paths "")
|
||||||
|
set(__qt_toolchain_additional_packages_prefix_paths "")
|
||||||
|
|
||||||
|
foreach(__qt_additional_path IN LISTS __qt_toolchain_additional_packages_prefixes)
|
||||||
|
file(TO_CMAKE_PATH "${__qt_additional_path}" __qt_additional_path)
|
||||||
|
get_filename_component(__qt_additional_path "${__qt_additional_path}" ABSOLUTE)
|
||||||
|
set(__qt_additional_path_lib_cmake "${__qt_additional_path}")
|
||||||
|
if(NOT __qt_additional_path_lib_cmake MATCHES "/lib/cmake$")
|
||||||
|
string(APPEND __qt_additional_path_lib_cmake "/lib/cmake")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
list(APPEND __qt_toolchain_additional_packages_root_paths
|
||||||
|
"${__qt_additional_path}")
|
||||||
|
list(APPEND __qt_toolchain_additional_packages_prefix_paths
|
||||||
|
"${__qt_additional_path_lib_cmake}")
|
||||||
|
endforeach()
|
||||||
|
list(PREPEND CMAKE_PREFIX_PATH ${__qt_toolchain_additional_packages_prefix_paths})
|
||||||
|
list(PREPEND CMAKE_FIND_ROOT_PATH ${__qt_toolchain_additional_packages_root_paths})
|
||||||
|
|
||||||
|
unset(__qt_additional_path)
|
||||||
|
unset(__qt_additional_path_lib_cmake)
|
||||||
|
unset(__qt_toolchain_additional_packages_root_paths)
|
||||||
|
unset(__qt_toolchain_additional_packages_prefix_paths)
|
||||||
|
endif()
|
||||||
|
unset(__qt_toolchain_additional_packages_prefixes)
|
||||||
|
|
||||||
@init_qt_host_path_checks@
|
@init_qt_host_path_checks@
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user