Commit 7473e184 authored by Daniel Black's avatar Daniel Black Committed by Eugene Kosov

check_linker_flag: use for linker flags

-Wl,-z,relro,-z,now are linker flags and should
be checked as such.

TODO: perform module, exe shared checks separately
rather than a pure linker check.
parent dfdfeecb
...@@ -186,6 +186,7 @@ ENDIF() ...@@ -186,6 +186,7 @@ ENDIF()
OPTION(NOT_FOR_DISTRIBUTION "Allow linking with GPLv2-incompatible system libraries. Only set it you never plan to distribute the resulting binaries" OFF) OPTION(NOT_FOR_DISTRIBUTION "Allow linking with GPLv2-incompatible system libraries. Only set it you never plan to distribute the resulting binaries" OFF)
INCLUDE(check_compiler_flag) INCLUDE(check_compiler_flag)
INCLUDE(check_linker_flag)
OPTION(WITH_ASAN "Enable address sanitizer" OFF) OPTION(WITH_ASAN "Enable address sanitizer" OFF)
IF (WITH_ASAN) IF (WITH_ASAN)
...@@ -234,7 +235,7 @@ IF(SECURITY_HARDENED) ...@@ -234,7 +235,7 @@ IF(SECURITY_HARDENED)
ENDIF() ENDIF()
# security-enhancing flags # security-enhancing flags
MY_CHECK_AND_SET_COMPILER_FLAG("-pie -fPIC") MY_CHECK_AND_SET_COMPILER_FLAG("-pie -fPIC")
MY_CHECK_AND_SET_COMPILER_FLAG("-Wl,-z,relro,-z,now") MY_CHECK_AND_SET_LINKER_FLAG("-Wl,-z,relro,-z,now")
MY_CHECK_AND_SET_COMPILER_FLAG("-fstack-protector --param=ssp-buffer-size=4") MY_CHECK_AND_SET_COMPILER_FLAG("-fstack-protector --param=ssp-buffer-size=4")
MY_CHECK_AND_SET_COMPILER_FLAG("-D_FORTIFY_SOURCE=2" RELEASE RELWITHDEBINFO) MY_CHECK_AND_SET_COMPILER_FLAG("-D_FORTIFY_SOURCE=2" RELEASE RELWITHDEBINFO)
ENDIF() ENDIF()
......
include(CheckCXXSourceCompiles)
FUNCTION(MY_CHECK_AND_SET_LINKER_FLAG flag_to_set)
# Let's avoid expensive compiler tests on Windows:
IF(WIN32)
RETURN()
ENDIF()
STRING(REGEX REPLACE "[-,= +]" "_" result "HAVE_LINK_FLAG_${flag_to_set}")
SET(SAVE_CMAKE_REQUIRED_LINK_OPTIONS "${CMAKE_REQUIRED_LINK_OPTIONS}")
STRING(REGEX REPLACE "^-Wno-" "-W" flag_to_check ${flag_to_set})
SET(CMAKE_REQUIRED_LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS} ${flag_to_check})
CHECK_CXX_SOURCE_COMPILES("int main(void) { return 0; }" ${result})
SET(CMAKE_REQUIRED_LINK_OPTIONS "${SAVE_CMAKE_REQUIRED_LINK_OPTIONS}")
IF (${result})
FOREACH(linktype SHARED MODULE EXE)
IF(ARGN)
FOREACH(type ${ARGN})
SET(CMAKE_${linktype}_LINKER_FLAGS_${type}
"${CMAKE_${linktype}_LINKER_FLAGS_${type}} ${flag_to_set}" PARENT_SCOPE)
ENDFOREACH()
ELSE()
SET(CMAKE_${linktype}_LINKER_FLAGS
"${CMAKE_${linktype}_LINKER_FLAGS} ${flag_to_set}" PARENT_SCOPE)
ENDIF()
ENDFOREACH()
ENDIF()
ENDFUNCTION()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment