Skip to content

Commit b83dd52

Browse files
xnorpxrillian
authored andcommitted
cmake - MINGW check for ssp lib and link if security features is enabled
1 parent d4b8707 commit b83dd52

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

CMakeLists.txt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,19 @@ cmake_dependent_option(OPUS_STACK_PROTECTOR
109109
OFF)
110110
add_feature_info(OPUS_STACK_PROTECTOR OPUS_STACK_PROTECTOR ${OPUS_STACK_PROTECTOR_HELP_STR})
111111

112-
if(NOT MSVC AND NOT MINGW)
113-
set(OPUS_FORTIFY_SOURCE_HELP_STR "add protection against buffer overflows.")
114-
option(OPUS_FORTIFY_SOURCE ${OPUS_FORTIFY_SOURCE_HELP_STR} ON)
115-
add_feature_info(OPUS_FORTIFY_SOURCE OPUS_FORTIFY_SOURCE ${OPUS_FORTIFY_SOURCE_HELP_STR})
112+
if(NOT MSVC)
113+
set(OPUS_FORTIFY_SOURCE_HELP_STR "add protection against buffer overflows.")
114+
cmake_dependent_option(OPUS_FORTIFY_SOURCE
115+
${OPUS_FORTIFY_SOURCE_HELP_STR}
116+
ON
117+
"FORTIFY_SOURCE_SUPPORTED"
118+
OFF)
119+
add_feature_info(OPUS_FORTIFY_SOURCE OPUS_FORTIFY_SOURCE ${OPUS_FORTIFY_SOURCE_HELP_STR})
120+
endif()
121+
122+
if(MINGW AND (OPUS_FORTIFY_SOURCE OR OPUS_STACK_PROTECTOR))
123+
# ssp lib is needed for security features for MINGW
124+
list(APPEND OPUS_REQUIRED_LIBRARIES ssp)
116125
endif()
117126

118127
if(OPUS_CPU_X86 OR OPUS_CPU_X64)
@@ -247,8 +256,9 @@ target_include_directories(
247256
target_link_libraries(opus PRIVATE ${OPUS_REQUIRED_LIBRARIES})
248257
target_compile_definitions(opus PRIVATE OPUS_BUILD ENABLE_HARDENING)
249258

250-
if(OPUS_FORTIFY_SOURCE)
251-
target_compile_definitions(opus PRIVATE _FORTIFY_SOURCE=2)
259+
if(OPUS_FORTIFY_SOURCE AND NOT MSVC)
260+
target_compile_definitions(opus PRIVATE
261+
$<$<NOT:$<CONFIG:debug>>:_FORTIFY_SOURCE=2>)
252262
endif()
253263

254264
if(OPUS_FLOAT_APPROX)

cmake/OpusConfig.cmake

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ else()
7777
check_flag(FAST_MATH -ffast-math)
7878
check_flag(STACK_PROTECTOR -fstack-protector-strong)
7979
check_flag(HIDDEN_VISIBILITY -fvisibility=hidden)
80+
set(FORTIFY_SOURCE_SUPPORTED 1)
81+
endif()
82+
83+
if(MINGW)
84+
# For MINGW we need to link ssp lib for security features such as
85+
# stack protector and fortify_sources
86+
check_library_exists(ssp __stack_chk_fail "" HAVE_LIBSSP)
87+
if(NOT HAVE_LIBSSP)
88+
message(WARNING "Could not find libssp in MinGW, disabling STACK_PROTECTOR and FORTIFY_SOURCE")
89+
set(STACK_PROTECTOR_SUPPORTED 0)
90+
set(FORTIFY_SOURCE_SUPPORTED 0)
91+
endif()
8092
endif()
8193

8294
if(NOT MSVC)

0 commit comments

Comments
 (0)