summaryrefslogtreecommitdiffstats
path: root/cmake/QtCompilerOptimization.cmake
diff options
context:
space:
mode:
authorAlexandru Croitor <alexandru.croitor@qt.io>2020-08-17 17:32:28 +0200
committerAlexandru Croitor <alexandru.croitor@qt.io>2020-08-19 18:25:59 +0200
commitcb1600145b7086388c5e5ca0292629ade9d1da58 (patch)
tree4fa289b83616e75b96eba185890bbad173ffd2f3 /cmake/QtCompilerOptimization.cmake
parenta1cc2fbbda4955a496e609fa8edd683e42f2118b (diff)
CMake: Adjust compiler flag optimizations to qmake mkspec ones
There are inconsistencies in the default optimization flags added by CMake across configurations like Release and RelWithDebInfo. In particular Release uses -O3, whereas RelWithDebInfo uses -O2, as well as usage of /INCREMENTAL in release configs with MSVC, etc. To make sure that the Qt 6 binaries built with CMake are consistent across configs, as well as consistent with the flags we used when building Qt 5 with qmake, add a horrible search and replace mechanism to replaces the CMake flags with what our mkspecs indicate to use. Ideally this would be done by providing custom CMake toolchain files for each platform we support, and we might revisit that later if the need really arises. To implement the replacing, we first need the flags that should be added. Port the QMAKE_CFLAGS_OPTIMIZE variables to CMake, which is done in QtCompilerOptimization.cmake. Then a new function called qt_internal_set_up_config_optimizations_like_in_qmake will look for any kind of optimization flags set in the CMAKE_<LANG>_FLAGS_<CONFIG> style variables, remove them, and add the appropriate flags that qmake mkspecs provide. On some platforms (like Windows MSVC) the function also alters the linker CMAKE_${TYPE}_LINKER_FLAGS_<CONFIG> style variables. The mechanism allows opting out of this replacing by setting the QT_USE_DEFAULT_CMAKE_OPTIMIZATION_FLAGS value. It also allows opting into removal of flags for custom configs by providing QT_ADDITIONAL_OPTIMIZATION_FLAG_CONFIGS. It's only removal, because we wouldn't know what kind of config it is, and thus what flags to add. The currently modified configs are: Release, RelWithDebInfo, MinSizeRel, Debug aka the usual default CMake provided ones. The mechanism is only applied to C-like languages. ASM is not handled to be on the safe side due to not knowing what kind of compiler flags the platform assembler might take. It's also important to skip RC on MSVC platforms. Task-number: QTBUG-85992 Change-Id: I3712d5cd5a34fceab54f56a6fa46b4e678952362 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Cristian Adam <cristian.adam@qt.io>
Diffstat (limited to 'cmake/QtCompilerOptimization.cmake')
-rw-r--r--cmake/QtCompilerOptimization.cmake54
1 files changed, 54 insertions, 0 deletions
diff --git a/cmake/QtCompilerOptimization.cmake b/cmake/QtCompilerOptimization.cmake
index fc2922089c..4716888752 100644
--- a/cmake/QtCompilerOptimization.cmake
+++ b/cmake/QtCompilerOptimization.cmake
@@ -122,3 +122,57 @@ if (ICC)
set(QT_CFLAGS_SHANI "-msha")
endif()
endif()
+
+# Fall through is important, so that more specific flags that might be missing are set by the
+# previous base cases.
+# This mirrors qmake's mkspecs QMAKE_CFLAGS_OPTIMIZE assignments (mostly).
+#
+# TODO: Missing mkspecs flags we don't handle below: win32-clang-g++, win32-clang-msvc, rtems-base
+#
+# gcc and clang base
+if(GCC OR CLANG)
+ set(QT_CFLAGS_OPTIMIZE "-O2")
+ set(QT_CFLAGS_OPTIMIZE_FULL "-O3")
+ set(QT_CFLAGS_OPTIMIZE_DEBUG "-Og")
+ set(QT_CFLAGS_OPTIMIZE_SIZE "-Os")
+
+ if(CLANG)
+ set(QT_CFLAGS_OPTIMIZE_SIZE "-Oz")
+ endif()
+endif()
+
+# Flags that CMake might set, aka flags the compiler would see as valid values.
+if(GCC OR CLANG OR QCC OR ICC)
+ set(QT_CFLAGS_OPTIMIZE_VALID_VALUES "-O0" "-O1" "-O2" "-O3" "-Os" "-Oz")
+endif()
+
+
+# Windows MSVC
+if(MSVC)
+ set(QT_CFLAGS_OPTIMIZE "-O2")
+ set(QT_CFLAGS_OPTIMIZE_DEBUG "-Od")
+ set(QT_CFLAGS_OPTIMIZE_SIZE "-O1")
+ set(QT_CFLAGS_OPTIMIZE_VALID_VALUES "/O2" "/O1" "/Od" "/Ob0" "/Ob1" "/Ob2" "/O0" "-O0")
+endif()
+
+# Android Clang
+if(CLANG AND ANDROID)
+ set(QT_CFLAGS_OPTIMIZE "-Oz")
+ set(QT_CFLAGS_OPTIMIZE_FULL "-Oz")
+endif()
+
+# qcc
+if (QCC)
+ set(QT_CFLAGS_OPTIMIZE "-O2")
+ set(QT_CFLAGS_OPTIMIZE_FULL "-O3")
+endif()
+
+if(ICC)
+ if(MSVC)
+ set(QT_CFLAGS_OPTIMIZE_FULL "-O3")
+ else()
+ # Should inherit gcc base
+ set(QT_CFLAGS_OPTIMIZE "-O2")
+ set(QT_CFLAGS_OPTIMIZE_SIZE "-Os")
+ endif()
+endif()