Merge bitcoin/bitcoin#30824: cmake: decouple `FORTIFY_SOURCE` check from `Debug` build type

30803a35d5 cmake: decouple FORTIFY_SOURCE check from Debug build type (fanquake)

Pull request description:

  `FORTIFY_SOURCE` should be used if `ENABLE_HARDENING=ON` and optimisations are being used. This should not be coupled to any particular build type, because even if the build type is `Debug`, optimisations might still be in use.

  Fixes: #30800.
  Also somewhat of a followup to https://github.com/bitcoin/bitcoin/pull/30778#discussion_r1742257436.

ACKs for top commit:
  ryanofsky:
    Code review ACK 30803a35d5
  TheCharlatan:
    ACK 30803a35d5

Tree-SHA512: 298f8805a5bb2f1ff54e51ea31324d712c2070cc3eba26561c31001ace4bfa37ae6d18531cbd45e2faf610a0a1b83b420fcde6e329e17f02b021d26563583913
pull/27038/head
merge-script 2 months ago
commit 94bc3c4cc0
No known key found for this signature in database
GPG Key ID: 2EEB9F5CC09526C1

@ -480,18 +480,21 @@ if(ENABLE_HARDENING)
try_append_linker_flag("/HIGHENTROPYVA" TARGET hardening_interface)
try_append_linker_flag("/NXCOMPAT" TARGET hardening_interface)
else()
# _FORTIFY_SOURCE requires that there is some level of optimization,
# otherwise it does nothing and just creates a compiler warning.
try_append_cxx_flags("-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3"
RESULT_VAR cxx_supports_fortify_source
SOURCE "int main() {
# if !defined __OPTIMIZE__ || __OPTIMIZE__ <= 0
#error
#endif
}"
)
if(cxx_supports_fortify_source)
# When the build configuration is Debug, all optimizations are disabled.
# However, _FORTIFY_SOURCE requires that there is some level of optimization,
# otherwise it does nothing and just creates a compiler warning.
# Since _FORTIFY_SOURCE is a no-op without optimizations, do not enable it
# when the build configuration is Debug.
target_compile_options(hardening_interface INTERFACE
$<$<NOT:$<CONFIG:Debug>>:-U_FORTIFY_SOURCE>
$<$<NOT:$<CONFIG:Debug>>:-D_FORTIFY_SOURCE=3>
-U_FORTIFY_SOURCE
-D_FORTIFY_SOURCE=3
)
endif()
unset(cxx_supports_fortify_source)

Loading…
Cancel
Save