qt5base-lts/cmake/qt.toolchain.cmake.in
Alexandru Croitor 0a0949837e CMake: Allow customization of the generated CMake toolchain file
Provide two customization points:
 - optionally include a 'qt.toolchain.extra.cmake' file if it exists
   and is placed next to the main generated toolchain file.
   This use case is mostly for the Qt installer, so that it can create
   an extra file with correct installer-provided paths, instead of
   patching the toolchain file directly.
- optionally include a file passed via the command line CMake argument
  'QT_TOOLCHAIN_INCLUDE_FILE'.
  The use case is for application developers that might want to adjust
  the toolchain file after the modifications done by the Qt installer.

These options do not replace the existing QT_CHAINLOAD_TOOLCHAIN_FILE
option, which is meant to chainload a platform specific existing
toolchain file (like Android or Emscripten).

Task-number: QTBUG-87068
Change-Id: I956949840f55742cfbd3bc8fc0bd8c6b3f774d3d
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
2020-10-12 13:36:03 +02:00

66 lines
3.2 KiB
CMake

@init_platform@
@init_qt_host_path@
@init_qt_host_path_cmake_dir@
@init_original_toolchain_file@
@init_vcpkg@
if(NOT "${QT_CHAINLOAD_TOOLCHAIN_FILE}" STREQUAL "")
set(__qt_chainload_toolchain_file "${QT_CHAINLOAD_TOOLCHAIN_FILE}")
endif()
if(__qt_chainload_toolchain_file)
get_filename_component(__qt_chainload_toolchain_file_real_path
"${__qt_chainload_toolchain_file}" REALPATH)
if(__qt_chainload_toolchain_file_real_path STREQUAL CMAKE_CURRENT_LIST_FILE)
message(FATAL_ERROR
"Woah, the Qt toolchain file tried to include itself recusively! '${__qt_chainload_toolchain_file}' "
"Make sure to remove qtbase/CMakeCache.txt and reconfigure qtbase with 'cmake' "
"rather than 'qt-cmake', and then you can reconfigure your own project."
)
elseif(NOT EXISTS "${__qt_chainload_toolchain_file_real_path}")
message(WARNING "The toolchain file to be chainloaded "
"'${__qt_chainload_toolchain_file}' does not exist.")
else()
include("${__qt_chainload_toolchain_file}")
endif()
unset(__qt_chainload_toolchain_file)
endif()
# Compute dynamically the Qt installation prefix from the location of this file. This allows
# the usage of the toolchain file when the Qt installation is relocated.
get_filename_component(QT_TOOLCHAIN_RELOCATABLE_INSTALL_PREFIX
${CMAKE_CURRENT_LIST_DIR}/../@qt_path_from_cmake_config_dir_to_prefix@
ABSOLUTE)
# 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}")
@init_qt_host_path_checks@
# Allow customization of the toolchain file by placing an additional file next to it.
set(__qt_toolchain_extra_file "${CMAKE_CURRENT_LIST_DIR}/qt.toolchain.extra.cmake")
if(EXISTS "${__qt_toolchain_extra_file}")
include("${__qt_toolchain_extra_file}")
endif()
# Allow customization of the toolchain file by passing a path to an additional CMake file to be
# included.
if(QT_TOOLCHAIN_INCLUDE_FILE)
get_filename_component(__qt_toolchain_include_file_real_path
"${QT_TOOLCHAIN_INCLUDE_FILE}" REALPATH)
if(EXISTS "${__qt_toolchain_include_file_real_path}")
include("${__qt_toolchain_include_file_real_path}")
else()
message(WARNING "The passed extra toolchain file to be included does not exist: "
"${__qt_toolchain_include_file_real_path}")
endif()
endif()