From 1c3d093c85dd021eabb87fb67fe942487869480e Mon Sep 17 00:00:00 2001 From: Ross Smith Date: Fri, 12 Dec 2014 12:45:21 -0500 Subject: [PATCH] Detect and propagate _FORTIFY_SOURCE prefix Get prefix to a -D_FORTIFY_SOURCE string if it is present in CFLAGS and apply the prefix to flags added to redefine _FORTIFY_SOURCE in CFLAGS and CPPFLAGS * fixes 1569 --- CMakeLists.txt | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 20d653a082..9f41f03241 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,10 +86,16 @@ main(void) " _FORTIFY_SOURCE_ACCEPTABLE) if(NOT _FORTIFY_SOURCE_ACCEPTABLE) + # Extract possible prefix to _FORTIFY_SOURCE (e.g. -Wp,-D_FORTIFY_SOURCE). + STRING(REGEX MATCH "[^\ ]+-D_FORTIFY_SOURCE" _FORTIFY_SOURCE_PREFIX "${CMAKE_C_FLAGS}") + STRING(REPLACE "-D_FORTIFY_SOURCE" "" _FORTIFY_SOURCE_PREFIX "${_FORTIFY_SOURCE_PREFIX}" ) + if (NOT _FORTIFY_SOURCE_PREFIX STREQUAL "") + message(STATUS "Detected _FORTIFY_SOURCE Prefix=${_FORTIFY_SOURCE_PREFIX}") + endif() # -U in add_definitions doesn't end up in the correct spot, so we add it to # the flags variable instead. - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_FORTIFY_SOURCE_PREFIX}-U_FORTIFY_SOURCE ${_FORTIFY_SOURCE_PREFIX}-D_FORTIFY_SOURCE=1") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_FORTIFY_SOURCE_PREFIX}-U_FORTIFY_SOURCE ${_FORTIFY_SOURCE_PREFIX}-D_FORTIFY_SOURCE=1") endif() add_definitions(-Wall -Wextra -pedantic -Wno-unused-parameter