From e2b2cd9397c76e91ac1ebe493bcac7696767c02e Mon Sep 17 00:00:00 2001 From: Alexandru Croitor Date: Thu, 18 Jun 2020 17:06:22 +0200 Subject: CMake: Improve automatic compiler embedding into the Qt toolchain Embedding the initial CMAKE_CXX_COMPILER into qt.toolchain.cmake breaks Boot2Qt builds, because the CXX environment variable is not used anymore when building qtsvg or other projects. Disable automatic embedding when cross-compiling, while keeping it enabled for non-cross-compiling cases (to keep Windows and and ICC configurations working). Allow opting in or out of the embedding in case if the default is wrong, via QT_EMBED_TOOLCHAIN_COMPILER. Task-number: QTBUG-85067 Change-Id: I1d8f9f580bc379b77c34eefb5728bb49f93cc81a Reviewed-by: Joerg Bornemann --- cmake/QtBaseGlobalTargets.cmake | 46 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/cmake/QtBaseGlobalTargets.cmake b/cmake/QtBaseGlobalTargets.cmake index 099d4328c6..1c35270096 100644 --- a/cmake/QtBaseGlobalTargets.cmake +++ b/cmake/QtBaseGlobalTargets.cmake @@ -108,10 +108,48 @@ if(VCPKG_TARGET_TRIPLET) list(APPEND init_vcpkg "set(VCPKG_TARGET_TRIPLET \"${VCPKG_TARGET_TRIPLET}\" CACHE STRING \"\")") endif() -# On Windows compilers aren't easily mixed. Avoid that qtbase is built using cl.exe for example and then for another -# build gcc is picked up from %PATH%. The same goes when using a custom compiler on other platforms, such as ICC. -list(APPEND init_platform "set(CMAKE_CXX_COMPILER \"${CMAKE_CXX_COMPILER}\" CACHE STRING \"\")") -list(APPEND init_platform "set(CMAKE_C_COMPILER \"${CMAKE_C_COMPILER}\" CACHE STRING \"\")") +# 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. +# We want to avoid that qtbase is built using cl.exe for example, and then for another repo +# gcc is picked up from %PATH%. +# The same goes when using a custom compiler on other platforms, such as ICC. +# +# There are a few exceptions though. +# +# When crosscompiling using Boot2Qt, the environment setup shell script sets up the CXX env var, +# which is used by CMake to determine the initial compiler that should be used. +# Unfortunately, the CXX env var contains not only the compiler name, but also a few required +# arch-specific compiler flags. This means that when building qtsvg, if the Qt created toolchain +# file sets the CMAKE_CXX_COMPILER variable, the CXX env var is ignored and thus the extra +# arch specific compiler flags are not picked up anymore, leading to a configuration failure. +# +# To avoid this issue, disable automatic embedding of the compilers into the qt toolchain when +# cross compiling. This is merely a heuristic, becacuse we don't have enough data to decide +# when to do it or not. +# For example on Linux one might want to allow mixing of clang and gcc (maybe). +# +# To allow such use cases when the default is wrong, one can provide a flag to explicitly opt-in +# or opt-out of the compiler embedding into the Qt toolchain. +# +# Passing -DQT_EMBED_TOOLCHAIN_COMPILER=ON will force embedding of the compilers. +# Passing -DQT_EMBED_TOOLCHAIN_COMPILER=OFF will disable embedding of the compilers. +set(__qt_embed_toolchain_compilers TRUE) +if(CMAKE_CROSSCOMPILING) + set(__qt_embed_toolchain_compilers FALSE) +endif() +if(DEFINED QT_EMBED_TOOLCHAIN_COMPILER) + if(QT_EMBED_TOOLCHAIN_COMPILER) + set(__qt_embed_toolchain_compilers TRUE) + else() + set(__qt_embed_toolchain_compilers FALSE) + endif() +endif() +if(__qt_embed_toolchain_compilers) + list(APPEND init_platform "set(CMAKE_CXX_COMPILER \"${CMAKE_CXX_COMPILER}\" CACHE STRING \"\")") + list(APPEND init_platform "set(CMAKE_C_COMPILER \"${CMAKE_C_COMPILER}\" CACHE STRING \"\")") +endif() +unset(__qt_embed_toolchain_compilers) if(APPLE) # For simulator_and_device build, we should not explicitly set the sysroot. -- cgit v1.2.3