From bdb21915e767be69475c720c2b69dc15ff51506c Mon Sep 17 00:00:00 2001 From: Marco Antonio Jaguaribe Costa Date: Fri, 22 May 2020 20:57:49 -0300 Subject: [PATCH] [build] set _FORTIFY_SOURCE=2 for -O* builds only Summary: currently building with -DCMAKE_BUILD_TYPE=Debug floods the cli with warnings about how _FORTIFY_SOURCE=2 requires optimization. Our Debug build compiles with -O0 because apparently -Og drops some symbols, so this flag should only be set for Release, RelWithDebInfo and RelMinSize h/t @Fabien for help with the CMake generator expressions Test Plan: cmake .. -GNinja -DCMAKE_BUILD_TYPE=Debug ninja see no warnings that _FORTIFY_SOURCE requires compiling with optimization cmake .. -GNinja -DCMAKE_BUILD_TYPE=[the other three] ninja -nv see the correct flags `-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2` set in the dry run output Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Subscribers: Fabien Differential Revision: https://reviews.bitcoinabc.org/D6237 --- src/CMakeLists.txt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cd21087c56..100ed99b20 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -126,8 +126,10 @@ if(ENABLE_HARDENING) # Enable stack protection add_cxx_compiler_flags(-fstack-protector-all -Wstack-protector) - # Enable some buffer overflow checking - add_compiler_flags(-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2) + # Enable some buffer overflow checking, except in -O0 builds which + # do not support them + add_compiler_flags(-U_FORTIFY_SOURCE) + add_compile_options($<$>:-D_FORTIFY_SOURCE=2>) # Enable ASLR (these flags are primarily targeting MinGw) add_linker_flags(-Wl,--dynamicbase -Wl,--nxcompat -Wl,--high-entropy-va)