build: Enabled various hardening flags on GCC and Clang

This commit is contained in:
WerWolv
2025-02-15 19:15:34 +01:00
parent 8b2184f8e3
commit d8877e7d8b

View File

@@ -628,21 +628,24 @@ endmacro()
macro(setupCompilerFlags target)
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
addCommonFlag("/W4" ${target})
addCommonFlag("/wd4242" ${target})
addCommonFlag("/wd4244" ${target})
addCommonFlag("/wd4267" ${target})
addCommonFlag("/wd4996" ${target})
addCommonFlag("/wd4127" ${target})
if (IMHEX_STRICT_WARNINGS)
addCommonFlag("/W4" ${target})
addCommonFlag("/wd4242" ${target})
addCommonFlag("/wd4244" ${target})
addCommonFlag("/wd4267" ${target})
addCommonFlag("/wd4996" ${target})
addCommonFlag("/wd4127" ${target})
addCommonFlag("/WX" ${target})
endif()
elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
addCommonFlag("-Wall" ${target})
addCommonFlag("-Wextra" ${target})
addCommonFlag("-Wpedantic" ${target})
# Define strict compilation flags
if (IMHEX_STRICT_WARNINGS)
addCommonFlag("-Wall" ${target})
addCommonFlag("-Wextra" ${target})
addCommonFlag("-Wpedantic" ${target})
addCommonFlag("-Werror" ${target})
addCommonFlag("-Werror" ${target})
endif()
if (UNIX AND NOT APPLE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU")
@@ -656,6 +659,11 @@ macro(setupCompilerFlags target)
addCCXXFlag("-Wno-array-bounds" ${target})
addCCXXFlag("-Wno-deprecated-declarations" ${target})
addCCXXFlag("-Wno-unknown-pragmas" ${target})
# Enable hardening flags
addCommonFlag("-U_FORTIFY_SOURCE" ${target})
addCommonFlag("-D_FORTIFY_SOURCE=3" ${target})
addCommonFlag("-fstack-protector-strong" ${target})
endif()
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
@@ -669,6 +677,12 @@ macro(setupCompilerFlags target)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L${LLVM_PREFIX}/lib/c++")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L${LLVM_PREFIX}/lib/c++")
addCCXXFlag("-Wno-unknown-warning-option" ${target})
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_definitions(_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG)
else()
add_compile_definitions(_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE)
endif()
endif()
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")