diff options
authorAlexandru Croitor <>2020-06-18 17:06:22 +0200
committerAlexandru Croitor <>2020-06-18 19:13:11 +0200
commite2b2cd9397c76e91ac1ebe493bcac7696767c02e (patch)
parent28ef8d283db336d0d1641b8fbab31eac5b46a498 (diff)
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 <>
1 files 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)
-# 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_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)
+ set(__qt_embed_toolchain_compilers FALSE)
+ set(__qt_embed_toolchain_compilers TRUE)
+ else()
+ set(__qt_embed_toolchain_compilers FALSE)
+ endif()
+ 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 \"\")")
# For simulator_and_device build, we should not explicitly set the sysroot.