diff options
Diffstat (limited to 'cmake/QtToolchainHelpers.cmake')
-rw-r--r-- | cmake/QtToolchainHelpers.cmake | 207 |
1 files changed, 151 insertions, 56 deletions
diff --git a/cmake/QtToolchainHelpers.cmake b/cmake/QtToolchainHelpers.cmake index 2f4c23eb1e..26b44bb10c 100644 --- a/cmake/QtToolchainHelpers.cmake +++ b/cmake/QtToolchainHelpers.cmake @@ -1,42 +1,21 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + # Create a CMake toolchain file for convenient configuration of both internal Qt builds # as well as CMake application projects. # Expects various global variables to be set. function(qt_internal_create_toolchain_file) - if(NOT "${QT_HOST_PATH}" STREQUAL "") - # TODO: Figure out how to make these relocatable. - - get_filename_component(__qt_host_path_absolute "${QT_HOST_PATH}" ABSOLUTE) - set(init_qt_host_path " - set(__qt_initial_qt_host_path \"${__qt_host_path_absolute}\") - if(NOT DEFINED QT_HOST_PATH AND EXISTS \"\${__qt_initial_qt_host_path}\") - set(QT_HOST_PATH \"\${__qt_initial_qt_host_path}\" CACHE PATH \"\" FORCE) - endif()") - - get_filename_component(__qt_host_path_cmake_dir_absolute - "${Qt${PROJECT_VERSION_MAJOR}HostInfo_DIR}/.." ABSOLUTE) - set(init_qt_host_path_cmake_dir - " - set(__qt_initial_qt_host_path_cmake_dir \"${__qt_host_path_cmake_dir_absolute}\") - if(NOT DEFINED QT_HOST_PATH_CMAKE_DIR AND EXISTS \"\${__qt_initial_qt_host_path_cmake_dir}\") - set(QT_HOST_PATH_CMAKE_DIR \"\${__qt_initial_qt_host_path_cmake_dir}\" CACHE PATH \"\" FORCE) - endif()") - - set(init_qt_host_path_checks " - if(\"\${QT_HOST_PATH}\" STREQUAL \"\" OR NOT EXISTS \"\${QT_HOST_PATH}\") - message(FATAL_ERROR \"To use a cross-compiled Qt, please specify a path to a host Qt installation by setting the QT_HOST_PATH cache variable.\") - endif() - if(\"\${QT_HOST_PATH_CMAKE_DIR}\" STREQUAL \"\" OR NOT EXISTS \"\${QT_HOST_PATH_CMAKE_DIR}\") - message(FATAL_ERROR \"To use a cross-compiled Qt, please specify a path to a host Qt installation CMake directory by setting the QT_HOST_PATH_CMAKE_DIR cache variable.\") - endif()") - endif() - if(CMAKE_TOOLCHAIN_FILE) file(TO_CMAKE_PATH "${CMAKE_TOOLCHAIN_FILE}" __qt_chainload_toolchain_file) set(init_original_toolchain_file - "set(__qt_chainload_toolchain_file \"${__qt_chainload_toolchain_file}\")") + " +set(__qt_initially_configured_toolchain_file \"${__qt_chainload_toolchain_file}\") +set(__qt_chainload_toolchain_file \"\${__qt_initially_configured_toolchain_file}\") +") endif() if(VCPKG_CHAINLOAD_TOOLCHAIN_FILE) + file(TO_CMAKE_PATH "${VCPKG_CHAINLOAD_TOOLCHAIN_FILE}" VCPKG_CHAINLOAD_TOOLCHAIN_FILE) list(APPEND init_vcpkg "set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE \"${VCPKG_CHAINLOAD_TOOLCHAIN_FILE}\")") endif() @@ -52,6 +31,65 @@ function(qt_internal_create_toolchain_file) list(APPEND init_platform "set(CMAKE_SYSTEM_PROCESSOR arm64 CACHE STRING \"\")") endif() + if(QT_QMAKE_TARGET_MKSPEC) + list(APPEND init_platform + "if(NOT QT_QMAKE_TARGET_MKSPEC)" + " set(QT_QMAKE_TARGET_MKSPEC ${QT_QMAKE_TARGET_MKSPEC} CACHE STRING \"\")" + "endif()" + ) + endif() + + if("${QT_QMAKE_TARGET_MKSPEC}" STREQUAL "linux-g++-32" AND NOT QT_NO_AUTO_DETECT_LINUX_X86) + set(__qt_toolchain_common_flags_init "-m32") + + if(NOT QT_NO_OVERRIDE_LANG_FLAGS_INIT) + list(APPEND init_platform + "if(NOT QT_NO_OVERRIDE_LANG_FLAGS_INIT)") + + list(APPEND init_platform + " set(__qt_toolchain_common_flags_init \"-m32\")") + list(APPEND init_platform + " set(CMAKE_C_FLAGS_INIT \"\${__qt_toolchain_common_flags_init}\")") + list(APPEND init_platform + " set(CMAKE_CXX_FLAGS_INIT \"\${__qt_toolchain_common_flags_init}\")") + list(APPEND init_platform + " set(CMAKE_ASM_FLAGS_INIT \"\${__qt_toolchain_common_flags_init}\")") + + list(APPEND init_platform "endif()") + endif() + + # Ubuntu-specific paths are used below. + # See comments of qt_auto_detect_linux_x86() for details. + if(NOT QT_NO_OVERRIDE_CMAKE_IGNORE_PATH) + list(APPEND init_platform + "if(NOT QT_NO_OVERRIDE_CMAKE_IGNORE_PATH)") + + get_property(linux_x86_ignore_path GLOBAL PROPERTY _qt_internal_linux_x86_ignore_path) + + string(REPLACE ";" "LITERAL_SEMICOLON" + linux_x86_ignore_path "${linux_x86_ignore_path}") + + list(APPEND init_platform + " set(CMAKE_IGNORE_PATH \"${linux_x86_ignore_path}\")") + + list(APPEND init_platform "endif()") + endif() + + if(NOT QT_NO_OVERRIDE_PKG_CONFIG_LIBDIR) + list(APPEND init_platform + "if(NOT QT_NO_OVERRIDE_PKG_CONFIG_LIBDIR)") + + get_property(pc_config_libdir GLOBAL PROPERTY _qt_internal_linux_x86_pc_config_libdir) + + list(APPEND init_platform + " set(ENV{PKG_CONFIG_LIBDIR} \"${pc_config_libdir}\")") + list(APPEND init_platform + " set(ENV{PKG_CONFIG_DIR} \"\")") + + list(APPEND init_platform "endif()") + endif() + endif() + # By default we don't want to allow mixing compilers for building different repositories, so we # embed the initially chosen compilers into the toolchain. # This is because on Windows compilers aren't easily mixed. @@ -91,26 +129,41 @@ function(qt_internal_create_toolchain_file) endif() if(__qt_embed_toolchain_compilers) list(APPEND init_platform " - set(__qt_initial_c_compiler \"${CMAKE_C_COMPILER}\") - set(__qt_initial_cxx_compiler \"${CMAKE_CXX_COMPILER}\") - if(NOT DEFINED CMAKE_C_COMPILER AND EXISTS \"\${__qt_initial_c_compiler}\") - set(CMAKE_C_COMPILER \"\${__qt_initial_c_compiler}\" CACHE STRING \"\") - endif() - if(NOT DEFINED CMAKE_CXX_COMPILER AND EXISTS \"\${__qt_initial_cxx_compiler}\") - set(CMAKE_CXX_COMPILER \"\${__qt_initial_cxx_compiler}\" CACHE STRING \"\") - endif()") +set(__qt_initial_c_compiler \"${CMAKE_C_COMPILER}\") +set(__qt_initial_cxx_compiler \"${CMAKE_CXX_COMPILER}\") +if(QT_USE_ORIGINAL_COMPILER AND NOT DEFINED CMAKE_C_COMPILER + AND EXISTS \"\${__qt_initial_c_compiler}\") + set(CMAKE_C_COMPILER \"\${__qt_initial_c_compiler}\" CACHE STRING \"\") +endif() +if(QT_USE_ORIGINAL_COMPILER AND NOT DEFINED CMAKE_CXX_COMPILER + AND EXISTS \"\${__qt_initial_cxx_compiler}\") + set(CMAKE_CXX_COMPILER \"\${__qt_initial_cxx_compiler}\" CACHE STRING \"\") +endif()") endif() unset(init_additional_used_variables) if(APPLE) - # For simulator_and_device build, we should not explicitly set the sysroot. + + # For an iOS simulator_and_device build, we should not explicitly set the sysroot, + # but let CMake do it's universal build magic to use one sysroot / sdk per-arch. + # For a single arch / sysroot build, try to use the initially configured sysroot + # by name. + # + # Also allow to opt out just in case. + # + # TODO: Figure out if the same should apply to universal macOS builds. + + # We want to preserve the sysroot as an SDK name, instead of the path + # that CMake transforms it into in Darwin-initialize.cmake, so we pick + # it out from the cache, where it hasn't been touched by CMake. + set(cmake_sysroot_name "$CACHE{CMAKE_OSX_SYSROOT}") + list(LENGTH CMAKE_OSX_ARCHITECTURES _qt_osx_architectures_count) - if(CMAKE_OSX_SYSROOT AND NOT _qt_osx_architectures_count GREATER 1 AND UIKIT) + if(cmake_sysroot_name AND NOT _qt_osx_architectures_count GREATER 1 AND UIKIT) list(APPEND init_platform " - set(__qt_initial_cmake_osx_sysroot \"${CMAKE_OSX_SYSROOT}\") - if(NOT DEFINED CMAKE_OSX_SYSROOT AND EXISTS \"\${__qt_initial_cmake_osx_sysroot}\") - set(CMAKE_OSX_SYSROOT \"\${__qt_initial_cmake_osx_sysroot}\" CACHE PATH \"\") - endif()") +if(NOT DEFINED CMAKE_OSX_SYSROOT) + set(CMAKE_OSX_SYSROOT \"${cmake_sysroot_name}\" CACHE STRING \"\") +endif()") endif() if(CMAKE_OSX_DEPLOYMENT_TARGET) @@ -130,11 +183,13 @@ function(qt_internal_create_toolchain_file) # When building another qt repo, ensure the same list of architectures is used by default. # Detection of a qt repo is done by checking for QT_REPO_MODULE_VERSION which is set in # the repo's .cmake.conf file. - # Standalone tests will be not be built with multiple architectures to avoid issues in the - # CI when trying to run cmake build tests on VMs that do not have a universal macOS + # Most standalone tests will also be built with multiple architectures. + # Certain tests will be built with a single arch only (like tests/auto/cmake) to avoid + # issues in the CI when trying to build them on VMs that do not have a universal macOS # SDK. - list(APPEND init_platform "# Only build multiple architectures when building Qt itself") - list(APPEND init_platform "if((QT_REPO_MODULE_VERSION AND NOT QT_BUILD_STANDALONE_TESTS) OR QT_FORCE_QT_OSX_ARCHITECTURES)") + list(APPEND init_platform + "# Only build multiple architectures when building Qt itself. Can be explicitly enabled or disabled.") + list(APPEND init_platform "if((QT_REPO_MODULE_VERSION AND NOT QT_FORCE_SINGLE_QT_OSX_ARCHITECTURE) OR QT_FORCE_ALL_QT_OSX_ARCHITECTURES)") list(APPEND init_platform " set(__qt_toolchain_building_qt_repo TRUE)") list(APPEND init_platform " set(CMAKE_OSX_ARCHITECTURES \"\${QT_OSX_ARCHITECTURES}\" CACHE STRING \"\")") list(APPEND init_platform "endif()") @@ -148,14 +203,22 @@ function(qt_internal_create_toolchain_file) # This is line with default CMake behavior for user projects. # # For iOS, we provide a bit more convenience. - # When the user project is built using the Xcode generator, don't specify a default - # architecture and let Xcode and the developer handle it. + # When the user project is built using the Xcode generator, we only specify the architecture + # if this is a single architecture Qt for iOS build. If we wouldn't, invoking just + # xcodebuild from the command line would try to build with the wrong architecture. Also + # provide an opt-out option just in case. + # + # For a multi-architecture build (so simulator_and_device) we set an explicit + # architecture for simulator only, via _qt_internal_set_ios_simulator_arch. + # # When using the Ninja generator, specify the first architecture from QT_OSX_ARCHITECTURES # (even with a simulator_and_device Qt build). This ensures that the default configuration # at least tries to build something. if(UIKIT) qt_internal_get_first_osx_arch(osx_first_arch) - list(APPEND init_platform "if(NOT CMAKE_GENERATOR STREQUAL \"Xcode\" AND NOT __qt_toolchain_building_qt_repo)") + list(APPEND init_platform +"if((NOT CMAKE_GENERATOR STREQUAL \"Xcode\" AND NOT __qt_toolchain_building_qt_repo) + OR (CMAKE_GENERATOR STREQUAL \"Xcode\" AND __qt_apple_sdk AND NOT QT_NO_SET_OSX_ARCHITECTURES))") list(APPEND init_platform " set(CMAKE_OSX_ARCHITECTURES \"${osx_first_arch}\" CACHE STRING \"\")") list(APPEND init_platform "endif()") @@ -165,18 +228,29 @@ function(qt_internal_create_toolchain_file) if(UIKIT) list(APPEND init_platform "set(CMAKE_SYSTEM_NAME \"${CMAKE_SYSTEM_NAME}\" CACHE STRING \"\")") - list(APPEND init_platform "if(CMAKE_GENERATOR STREQUAL \"Xcode\" AND NOT QT_NO_XCODE_EMIT_EPN)") - list(APPEND init_platform " set_property(GLOBAL PROPERTY XCODE_EMIT_EFFECTIVE_PLATFORM_NAME OFF)") - list(APPEND init_platform "endif()") endif() elseif(ANDROID) - foreach(var ANDROID_NATIVE_API_LEVEL ANDROID_STL ANDROID_ABI - ANDROID_SDK_ROOT ANDROID_NDK_ROOT) + list(APPEND init_platform +"# Detect Android SDK/NDK from environment before loading the Android platform toolchain file." +"if(\"$\{ANDROID_SDK_ROOT}\" STREQUAL \"\" AND NOT \"\$ENV{ANDROID_SDK_ROOT}\" STREQUAL \"\")" +" set(ANDROID_SDK_ROOT \"\$ENV{ANDROID_SDK_ROOT}\" CACHE STRING \"Path to the Android SDK\")" +"endif()" +"if(\"$\{ANDROID_NDK_ROOT}\" STREQUAL \"\" AND NOT \"\$ENV{ANDROID_NDK_ROOT}\" STREQUAL \"\")" +" set(ANDROID_NDK_ROOT \"\$ENV{ANDROID_NDK_ROOT}\" CACHE STRING \"Path to the Android NDK\")" +"endif()" + ) + + foreach(var ANDROID_PLATFORM ANDROID_NATIVE_API_LEVEL ANDROID_STL + ANDROID_ABI ANDROID_SDK_ROOT ANDROID_NDK_ROOT) list(APPEND init_additional_used_variables "list(APPEND __qt_toolchain_used_variables ${var})") endforeach() + + list(APPEND init_platform + "if(NOT DEFINED ANDROID_PLATFORM AND NOT DEFINED ANDROID_NATIVE_API_LEVEL)") list(APPEND init_platform - "set(ANDROID_NATIVE_API_LEVEL \"${ANDROID_NATIVE_API_LEVEL}\" CACHE STRING \"\")") + " set(ANDROID_PLATFORM \"${ANDROID_PLATFORM}\" CACHE STRING \"\")") + list(APPEND init_platform "endif()") list(APPEND init_platform "set(ANDROID_STL \"${ANDROID_STL}\" CACHE STRING \"\")") list(APPEND init_platform "set(ANDROID_ABI \"${ANDROID_ABI}\" CACHE STRING \"\")") list(APPEND init_platform "if (NOT DEFINED ANDROID_SDK_ROOT)") @@ -199,13 +273,34 @@ function(qt_internal_create_toolchain_file) " \"Please specify the toolchain file with -DQT_CHAINLOAD_TOOLCHAIN_FILE=<file>.\")") list(APPEND init_platform " endif()") list(APPEND init_platform "endif()") + elseif(EMSCRIPTEN) + list(APPEND init_platform +"include(\${CMAKE_CURRENT_LIST_DIR}/QtPublicWasmToolchainHelpers.cmake) +if(DEFINED ENV{EMSDK} AND NOT \"\$ENV{EMSDK}\" STREQUAL \"\") + __qt_internal_get_emroot_path_suffix_from_emsdk_env(__qt_toolchain_emroot_path) + __qt_internal_get_emscripten_cmake_toolchain_file_path_from_emsdk_env( + \"\${__qt_toolchain_emroot_path}\" _qt_candidate_emscripten_toolchain_path) + set(__qt_chainload_toolchain_file \"\${_qt_candidate_emscripten_toolchain_path}\") +endif() +") + list(APPEND init_post_chainload_toolchain " +if(NOT __qt_chainload_toolchain_file_included) + __qt_internal_show_error_no_emscripten_toolchain_file_found_when_using_qt() +endif() +") endif() string(REPLACE ";" "\n" init_additional_used_variables "${init_additional_used_variables}") string(REPLACE ";" "\n" init_vcpkg "${init_vcpkg}") + string(REPLACE ";" "\n" init_platform "${init_platform}") string(REPLACE "LITERAL_SEMICOLON" ";" init_platform "${init_platform}") + + string(REPLACE ";" "\n" init_post_chainload_toolchain "${init_post_chainload_toolchain}") + string(REPLACE "LITERAL_SEMICOLON" ";" init_post_chainload_toolchain + "${init_post_chainload_toolchain}") + qt_compute_relative_path_from_cmake_config_dir_to_prefix() configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/cmake/qt.toolchain.cmake.in" |