From 886db3a2f3600d15ceb2447c9502f20ce9b3f000 Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Tue, 3 Nov 2020 12:02:01 +0800 Subject: [PATCH] MSVC: Enable control flow guard for release builds This makes the applications and libraries more safe and won't cause any binary incompatibility. It's not supported for MSVC 2015, but since we only support MSVC 2019 or later in Qt6, it's safe to use this parameter. The "-guard:cf" parameter is supported by clang-cl and intel-cl as well, MinGW doesn't support it. [1] https://docs.microsoft.com/en-us/cpp/build/reference/guard-enable-control-flow-guard?view=msvc-160 [2] https://docs.microsoft.com/en-us/cpp/build/reference/guard-enable-guard-checks?view=msvc-160 Change-Id: I7b035b4b0f22617a7f3c067cddde0bed2e13dd1c Reviewed-by: Alexandru Croitor --- cmake/QtInternalTargets.cmake | 8 ++++++-- mkspecs/features/qt_module.prf | 9 +++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/cmake/QtInternalTargets.cmake b/cmake/QtInternalTargets.cmake index 2fd99c7be3..7dbaed0d31 100644 --- a/cmake/QtInternalTargets.cmake +++ b/cmake/QtInternalTargets.cmake @@ -167,7 +167,7 @@ if (MSVC) -FS -Zc:rvalueCast -Zc:inline - ) + ) endif() if (MSVC_VERSION GREATER_EQUAL 1899) target_compile_options(PlatformCommonInternal INTERFACE @@ -192,9 +192,13 @@ if (MSVC) target_compile_options(PlatformCommonInternal INTERFACE -Zc:wchar_t) + target_compile_options(PlatformCommonInternal INTERFACE + $<$>:-guard:cf> + ) + target_link_options(PlatformCommonInternal INTERFACE -DYNAMICBASE -NXCOMPAT - $<$>:-OPT:REF -OPT:ICF> + $<$>:-OPT:REF -OPT:ICF -GUARD:CF> ) endif() diff --git a/mkspecs/features/qt_module.prf b/mkspecs/features/qt_module.prf index 3cacc8dfde..62d225b14d 100644 --- a/mkspecs/features/qt_module.prf +++ b/mkspecs/features/qt_module.prf @@ -336,4 +336,13 @@ win32 { } DEFINES *= QT_DEPRECATED_WARNINGS_SINCE=0x060000 +msvc { + QMAKE_CFLAGS_RELEASE += -guard:cf + QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += -guard:cf + QMAKE_CXXFLAGS_RELEASE += -guard:cf + QMAKE_CXXFLAGS_RELEASE_WITH_DEBUGINFO += -guard:cf + QMAKE_LFLAGS_RELEASE += /GUARD:CF + QMAKE_LFLAGS_RELEASE_WITH_DEBUGINFO += /GUARD:CF +} + TARGET = $$qt5LibraryTarget($$TARGET$$QT_LIBINFIX) # Do this towards the end