diff options
Diffstat (limited to 'cmake/QtToolchainHelpers.cmake')
-rw-r--r-- | cmake/QtToolchainHelpers.cmake | 152 |
1 files changed, 106 insertions, 46 deletions
diff --git a/cmake/QtToolchainHelpers.cmake b/cmake/QtToolchainHelpers.cmake index de8c1e8f52..26b44bb10c 100644 --- a/cmake/QtToolchainHelpers.cmake +++ b/cmake/QtToolchainHelpers.cmake @@ -1,22 +1,10 @@ +# 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) - set(qt_host_path_required FALSE) - - if(NOT "${QT_HOST_PATH}" STREQUAL "") - # If a QT_HOST_PATH is provided when configuring qtbase, we assume it's a cross build - # and thus we require the QT_HOST_PATH to be provided also when using the cross-built Qt. - # This tells the Qt toolchain file to do appropriate requirement checks. - set(qt_host_path_required TRUE) - - # TODO: Figure out how to make the initial QT_HOST_PATH var relocatable in relation - # to the target CMAKE_INSTALL_DIR, if at all possible to do so in a reliable way. - get_filename_component(qt_host_path_absolute "${QT_HOST_PATH}" ABSOLUTE) - get_filename_component(qt_host_path_cmake_dir_absolute - "${Qt${PROJECT_VERSION_MAJOR}HostInfo_DIR}/.." ABSOLUTE) - endif() - if(CMAKE_TOOLCHAIN_FILE) file(TO_CMAKE_PATH "${CMAKE_TOOLCHAIN_FILE}" __qt_chainload_toolchain_file) set(init_original_toolchain_file @@ -43,6 +31,65 @@ set(__qt_chainload_toolchain_file \"\${__qt_initially_configured_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. @@ -82,39 +129,41 @@ set(__qt_chainload_toolchain_file \"\${__qt_initially_configured_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 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 iOS build, try to use the initially configured sysroot - # path if it exists, otherwise just set the name of the sdk to be used. - # The latter "name" part is important for user projects so that running 'xcodebuild' from - # the command line chooses the correct sdk. + # 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_uikit_sdk \"${QT_UIKIT_SDK}\") - 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 \"\") - elseif(NOT DEFINED CMAKE_OSX_SYSROOT AND NOT QT_NO_SET_OSX_SYSROOT) - set(CMAKE_OSX_SYSROOT \"\${__qt_uikit_sdk}\" 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) @@ -159,8 +208,8 @@ set(__qt_chainload_toolchain_file \"\${__qt_initially_configured_toolchain_file} # 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 don't set an explicit - # architecture and let Xcode and the developer handle it. + # 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 @@ -169,7 +218,7 @@ set(__qt_chainload_toolchain_file \"\${__qt_initially_configured_toolchain_file} 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) - OR (CMAKE_GENERATOR STREQUAL \"Xcode\" AND __qt_uikit_sdk AND NOT QT_NO_SET_OSX_ARCHITECTURES))") + 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()") @@ -179,18 +228,29 @@ set(__qt_chainload_toolchain_file \"\${__qt_initially_configured_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 - "set(ANDROID_NATIVE_API_LEVEL \"${ANDROID_NATIVE_API_LEVEL}\" CACHE STRING \"\")") + "if(NOT DEFINED ANDROID_PLATFORM AND NOT DEFINED ANDROID_NATIVE_API_LEVEL)") + list(APPEND init_platform + " 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)") |