Skip to content

Commit b17297f

Browse files
committed
conditionally add FORTIFY_SOURCE
1 parent 04259b3 commit b17297f

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

CMake/unix_config.cmake

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,17 @@ macro(os_set_flags)
7272

7373
if (CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|armv7l" OR APPLE OR # Some flags are not recognized or some systems / gcc versions
7474
(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0")) #
75-
set(ADDITIONAL_COMPILER_FLAGS "-Wformat -Wformat-security -fPIC -D_FORTIFY_SOURCE=2 -fstack-protector")
75+
set(ADDITIONAL_COMPILER_FLAGS "-Wformat -Wformat-security -fPIC -fstack-protector")
7676
else()
7777
#‘-mfunction-return’ and ‘-fcf-protection’ are not compatible, so specifing -fcf-protection=none
78-
set(ADDITIONAL_COMPILER_FLAGS "-Wformat -Wformat-security -fPIC -D_FORTIFY_SOURCE=2 -fcf-protection=none -mfunction-return=thunk -mindirect-branch=thunk -mindirect-branch-register -fstack-protector")
78+
set(ADDITIONAL_COMPILER_FLAGS "-Wformat -Wformat-security -fPIC -fcf-protection=none -mfunction-return=thunk -mindirect-branch=thunk -mindirect-branch-register -fstack-protector")
7979
endif()
8080
set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -pie")
8181

82+
string(FIND "${CMAKE_CXX_FLAGS}" "-D_FORTIFY_SOURCE" _index)
83+
if (${_index} EQUAL -1) # Define D_FORTIFY_SOURCE is undefined
84+
set(ADDITIONAL_COMPILER_FLAGS "${ADDITIONAL_COMPILER_FLAGS} -D_FORTIFY_SOURCE=2")
85+
endif()
8286

8387
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
8488
message(STATUS "Configuring for Debug build")

0 commit comments

Comments
 (0)