CMake: Fix cross-building examples in prefix builds as ExternalProjects

For examples to successfully find not yet installed Config files in a
prefix cross-build, both CMAKE_FIND_ROOT_PATH and CMAKE_PREFIX_PATH
need to be adjusted when toolchain files set
CMAKE_FIND_ROOT_PATH_MODE_PACKAGE to ONLY.

Before this change, we lacked two things.

1) If the chain-loaded toolchain file sets CMAKE_FIND_ROOT_PATH
(instead of appending), then the initial value of CMAKE_FIND_ROOT_PATH
that was passed by ExternalProject's initial cache would be
overridden.

2) The prefixes we passed via QT_EXAMPLES_CMAKE_PREFIX_PATH did not
end in lib/cmake, to work around the path re-rooting problem in CMake.

We can handle both of these points by using
QT_ADDITIONAL_PACKAGES_PREFIX_PATH which was introduced for Conan
usage.

Each value assigned to QT_ADDITIONAL_PACKAGES_PREFIX_PATH
gets prepended to CMAKE_FIND_ROOT_PATH by qt.toolchain.cmake AFTER the
original toolchain file is chain-loaded.

And each value also gets lib/cmake appended and assigned to
both CMAKE_PREFIX_PATH and _qt_additional_packages_prefix_paths (used
by Qt6Config.cmake.in to work with find_package + NO_DEFAULT_PATH).

This is exactly what we need to ensure examples build.

Pick-to: 6.2 6.3
Task-number: QTBUG-96232
Change-Id: I11a52457ff795a6661a3a7b68e823e0615d0ce89
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Jörg Bornemann <joerg.bornemann@qt.io>
This commit is contained in:
Alexandru Croitor 2022-02-03 14:03:42 +01:00
parent 9943cf7371
commit c89da21249

View File

@ -944,8 +944,6 @@ function(qt_internal_add_example_external_project subdir)
set(arg_NAME "${arg_NAME}-${short_hash}")
endif()
# TODO: Fix 'prefix Qt' example builds not to rely on CMAKE_FIND_ROOT_PATH_MODE_PACKAGE=BOTH
# for finding Qt packages when cross-compiling.
# TODO: Fix example builds when using Conan / install prefixes are different for each repo.
if(QT_SUPERBUILD OR QtBase_BINARY_DIR)
# When doing a top-level build or when building qtbase,
@ -963,27 +961,28 @@ function(qt_internal_add_example_external_project subdir)
# for all repos except the one that is currently built. For the repo that is currently
# built, we pick up the Config files from the current repo build dir instead.
# For non-prefix builds, there's only one prefix, the main build dir.
# Both are handled by this assignment.
set(qt_prefixes "${QT_BUILD_DIR}")
# Appending to QT_ADDITIONAL_PACKAGES_PREFIX_PATH helps find Qt6 components in
# non-qtbase prefix builds because we use NO_DEFAULT_PATH in find_package calls.
# It also handles the cross-compiling scenario where we need to adjust both the root path
# and prefixes, with the prefixes containing lib/cmake. This leverages the infrastructure
# previously added for Conan.
list(APPEND QT_ADDITIONAL_PACKAGES_PREFIX_PATH ${qt_prefixes})
# In a prefix build, look up all repo Config files in the install prefix,
# except for the current repo, which will look in the build dir (handled above).
if(QT_WILL_INSTALL)
list(APPEND qt_prefixes "${QT6_INSTALL_PREFIX}")
# Appending to QT_EXAMPLES_CMAKE_PREFIX_PATH helps find Qt6 components in
# non-qtbase prefix builds because we use NO_DEFAULT_PATH in find_package calls.
set(QT_EXAMPLES_CMAKE_PREFIX_PATH "${qt_prefixes}")
endif()
endif()
set(vars_to_pass_if_defined)
set(var_defs)
if(QT_HOST_PATH OR CMAKE_CROSSCOMPILING)
# Android NDK forces CMAKE_FIND_ROOT_PATH_MODE_PACKAGE to ONLY, so we
# can't rely on this setting here making it through to the example
# project.
# TODO: Remove CMAKE_FIND_ROOT_PATH_MODE_PACKAGE once cross-compiling examples uses
# CMAKE_FIND_ROOT_PATH + CMAKE_PREFIX_PATH instead.
list(APPEND var_defs
-DCMAKE_TOOLCHAIN_FILE:FILEPATH=${qt_cmake_dir}/qt.toolchain.cmake
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE:STRING=BOTH
)
else()
list(PREPEND CMAKE_PREFIX_PATH ${qt_prefixes})
@ -1027,8 +1026,8 @@ function(qt_internal_add_example_external_project subdir)
CMAKE_CONFIGURATION_TYPES:STRING
CMAKE_PREFIX_PATH:STRING
QT_EXAMPLES_CMAKE_PREFIX_PATH:STRING
QT_ADDITIONAL_PACKAGES_PREFIX_PATH:STRING
CMAKE_FIND_ROOT_PATH:STRING
CMAKE_FIND_ROOT_PATH_MODE_PACKAGE:STRING
BUILD_SHARED_LIBS:BOOL
CMAKE_OSX_ARCHITECTURES:STRING
CMAKE_OSX_DEPLOYMENT_TARGET:STRING