summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexandru Croitor <alexandru.croitor@qt.io>2020-07-21 17:34:40 +0200
committerAlexandru Croitor <alexandru.croitor@qt.io>2020-07-24 17:33:21 +0200
commitec90f9013b4c6b63d7e03a964f66f97329be7885 (patch)
tree50de686e193a7e607a73489ab122b80faa36753c
parent716e41e86fb1999696fcfceace3d42e143631c29 (diff)
CMake: Fix handling of CMAKE_FIND_ROOT_PATH and CMAKE_PREFIX_PATH
While trying to implement the 'host artifact reuse' Coin instructions change, a bug surfaced where the qemu configurations didn't find the host tools and instead tried to use the cross-compiled tools while building qtbase, which failed due to not finding the runtime linker (another unsolved issue). Before the host artifact reuse change, the host tools were found successfully. The difference that caused the issue is that the target install prefix was a direct subfolder of the host prefix. host - /home/qt/work/qt/install target - /home/qt/work/qt/install/target Before the host reuse change the install prefixes were as follows host - /home/qt/work/qt/install/host target - /home/qt/work/qt/install/target While looking for the Qt6CoreTools package, we temporarily set CMAKE_FIND_ROOT_PATH and CMAKE_PREFIX_PATH to contain first '/home/qt/work/qt/install' and then '/home/qt/work/qt/install/target'. CMake then reroots the CMAKE_PREFIX_PATH values onto values in CMAKE_FIND_ROOT_PATH, making an MxN list of prefixes to search. Rerooting essentially means concatenating 2 paths, unless the considered prefix is a subfolder of the root path. What happened was that the first considered value was '/home/qt/work/qt/install/home/qt/work/qt/install', a non-existent location that gets discarded. The second considered value was '/home/qt/work/qt/install/target. The second value is the result of seeing that '/home/qt/work/qt/install/target' is a subfolder of '/home/qt/work/qt/install' and thus the root path is stripped. All of this is done in cmFindPackageCommand::FindConfig() -> cmFindCommon::RerootPaths. The behavior above caused the target tools be found instead of the host ones. Before the host reuse change, both of the initial constructed prefixes were discared due to them not existing, e.g. '/home/qt/work/qt/install/target/home/qt/work/qt/install/target' and '/home/qt/work/qt/install/host/home/qt/work/qt/install/host' One of the later prefixes combined CMAKE_FIND_ROOT_PATH == '/home/qt/work/qt/install/host' + CMAKE_PREFIX_PATH == '/' resulting in '/home/qt/work/qt/install/host/' and this accidentally found the host tools package. We actually stumbled upon this issue a while ago when implementing Qt 5.14 Android CMake support in 52c799ed4425076df4353c02950ea1444fe5f102 That commit message mentions the fix is to add a "lib/cmake" suffix to the PATHS option of find_package(). This would cause the subfolder => strip root behavior mentioned above. So finally the fix. First, make sure not to append QT_HOST_PATH in the toolchain file, there shouldn't be any need to do that, give that we temporarily set it when looking for Tools packages. Second, recreate the subdir scenario in the Qt toolchain file by setting CMAKE_FIND_ROOT_PATH to the current (relocated) install prefix as usual, but also setting CMAKE_PREFIX_PATH to a new value poining to the CMake directory. Aka '/home/alex/qt' and '/home/alex/qt/lib/cmake'. Third, when a QT_HOST_PATH is given, save 2 paths in the generated Qt toolchain: QT_HOST_PATH and QT_HOST_PATH_CMAKE_DIR. There are the host equivalents of the target ones above. Use these values when looking for host tools in Qt6CoreModuleDependencies.cmake, again facilitaing the subdir behavior. Note these are currently absolute paths and are not relocatable. We'll have to figure out if it's even possible to make the host path relocatable. Finally as a cleanup, look for the Qt6HostInfo package in QtSetup strictly in the given QT_HOST_PATH, so CMake doesn't accidentally find a system Qt package. Change-Id: Iefbcfbbcedd35f1c33417ab7e9f44eaf35ff6337 Reviewed-by: Cristian Adam <cristian.adam@qt.io>
-rw-r--r--cmake/QtBaseGlobalTargets.cmake5
-rw-r--r--cmake/QtModuleDependencies.cmake.in2
-rw-r--r--cmake/QtSetup.cmake13
-rw-r--r--cmake/qt.toolchain.cmake.in17
4 files changed, 25 insertions, 12 deletions
diff --git a/cmake/QtBaseGlobalTargets.cmake b/cmake/QtBaseGlobalTargets.cmake
index ec17164153..b630c3fe8d 100644
--- a/cmake/QtBaseGlobalTargets.cmake
+++ b/cmake/QtBaseGlobalTargets.cmake
@@ -93,7 +93,12 @@ set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
# Generate toolchain file for convenience
if(QT_HOST_PATH)
get_filename_component(init_qt_host_path "${QT_HOST_PATH}" ABSOLUTE)
+ # TODO: Figure out how to make these relocatable.
set(init_qt_host_path "set(QT_HOST_PATH \"${init_qt_host_path}\" CACHE PATH \"\" FORCE)")
+ get_filename_component(QT_HOST_PATH_CMAKE_DIR
+ "${Qt${PROJECT_VERSION_MAJOR}HostInfo_DIR}/.." ABSOLUTE)
+ set(init_qt_host_path_cmake_dir
+ "set(QT_HOST_PATH_CMAKE_DIR \"${QT_HOST_PATH_CMAKE_DIR}\" CACHE PATH \"\" FORCE)")
endif()
if(CMAKE_TOOLCHAIN_FILE)
diff --git a/cmake/QtModuleDependencies.cmake.in b/cmake/QtModuleDependencies.cmake.in
index 2396539109..56819f8816 100644
--- a/cmake/QtModuleDependencies.cmake.in
+++ b/cmake/QtModuleDependencies.cmake.in
@@ -44,7 +44,7 @@ if(QT_HOST_PATH)
# 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}")
+ list(PREPEND CMAKE_PREFIX_PATH "${QT_HOST_PATH_CMAKE_DIR}")
list(PREPEND CMAKE_FIND_ROOT_PATH "${QT_HOST_PATH}")
endif()
diff --git a/cmake/QtSetup.cmake b/cmake/QtSetup.cmake
index ede764b542..a3584190ab 100644
--- a/cmake/QtSetup.cmake
+++ b/cmake/QtSetup.cmake
@@ -165,14 +165,15 @@ option(QT_BUILD_BENCHMARKS "Build Qt Benchmarks" ${__build_benchmarks})
set(QT_HOST_PATH "" CACHE PATH "Installed Qt host directory path, used for cross compiling.")
if (CMAKE_CROSSCOMPILING)
- if(NOT IS_DIRECTORY ${QT_HOST_PATH})
+ if(NOT IS_DIRECTORY "${QT_HOST_PATH}")
message(FATAL_ERROR "You need to set QT_HOST_PATH to cross compile Qt.")
endif()
- list(PREPEND CMAKE_PREFIX_PATH "${QT_HOST_PATH}")
- list(PREPEND CMAKE_FIND_ROOT_PATH "${QT_HOST_PATH}")
- find_package(Qt${PROJECT_VERSION_MAJOR}HostInfo REQUIRED)
- list(POP_FRONT CMAKE_PREFIX_PATH)
- list(POP_FRONT CMAKE_FIND_ROOT_PATH)
+ find_package(Qt${PROJECT_VERSION_MAJOR}HostInfo
+ CONFIG
+ REQUIRED
+ PATHS "${QT_HOST_PATH}" "${QT_HOST_PATH}/lib/cmake"
+ NO_CMAKE_FIND_ROOT_PATH
+ NO_DEFAULT_PATH)
endif()
## Android platform settings
diff --git a/cmake/qt.toolchain.cmake.in b/cmake/qt.toolchain.cmake.in
index e7af642c05..c4884e9dfa 100644
--- a/cmake/qt.toolchain.cmake.in
+++ b/cmake/qt.toolchain.cmake.in
@@ -1,6 +1,7 @@
@init_platform@
@init_qt_host_path@
+@init_qt_host_path_cmake_dir@
@init_original_toolchain_file@
@@ -27,10 +28,16 @@ get_filename_component(QT_TOOLCHAIN_RELOCATABLE_INSTALL_PREFIX
${CMAKE_CURRENT_LIST_DIR}/../@qt_path_from_cmake_config_dir_to_prefix@
ABSOLUTE)
-list(PREPEND CMAKE_PREFIX_PATH "${QT_TOOLCHAIN_RELOCATABLE_INSTALL_PREFIX}")
+# Compute the path to the installed Qt lib/cmake folder.
+# We assume that the Qt toolchain location is inside the CMake Qt6 package, and thus the directory
+# one level higher is what we're looking for.
+get_filename_component(QT_TOOLCHAIN_RELOCATABLE_CMAKE_DIR "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
+
+# 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.
+# 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
+# Qt install prefix, and the prefix path to the lib/cmake subdir.
+list(PREPEND CMAKE_PREFIX_PATH "${QT_TOOLCHAIN_RELOCATABLE_CMAKE_DIR}")
list(PREPEND CMAKE_FIND_ROOT_PATH "${QT_TOOLCHAIN_RELOCATABLE_INSTALL_PREFIX}")
-if(QT_HOST_PATH)
- list(APPEND CMAKE_PREFIX_PATH "${QT_HOST_PATH}")
- list(APPEND CMAKE_FIND_ROOT_PATH "${QT_HOST_PATH}")
-endif()