From b58aa58d49d8bdb0cac36a20a1c704d2f76dc48d Mon Sep 17 00:00:00 2001 From: msclock Date: Tue, 2 Apr 2024 09:11:21 +0800 Subject: [PATCH] refactor: target hardening for executables and shared libraries Signed-off-by: msclock --- cmake/build/Hardening.cmake | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/cmake/build/Hardening.cmake b/cmake/build/Hardening.cmake index 3a3c222..8721dc0 100644 --- a/cmake/build/Hardening.cmake +++ b/cmake/build/Hardening.cmake @@ -132,24 +132,20 @@ function(harden_target target) get_target_property(_target_type ${target} TYPE) - if(MSVC OR (NOT _target_type STREQUAL "EXECUTABLE" - AND NOT _target_type STREQUAL "SHARED_LIBRARY")) - set(FLAGS ${hardening_flags}) - set(LINKS ${hardening_links}) - else() + if(NOT MSVC AND (_target_type STREQUAL "EXECUTABLE" OR _target_type STREQUAL + "SHARED_LIBRARY")) if(_target_type STREQUAL "EXECUTABLE") - check_and_append_flag(FLAGS "-fPIE -pie" TARGETS exe_flags) - flags_to_list(exe_flags "${exe_flags}") - set(FLAGS ${hardening_flags} ${exe_flags}) - set(LINKS ${hardening_links} ${exe_flags}) + check_and_append_flag(FLAGS "-fPIE -pie" TARGETS target_flags) + flags_to_list(target_flags "${target_flags}") elseif(_target_type STREQUAL "SHARED_LIBRARY") - check_and_append_flag(FLAGS "-fPIC -shared" TARGETS shared_flags) - flags_to_list(shared_flags "${shared_flags}") - set(FLAGS ${hardening_flags} ${shared_flags}) - set(LINKS ${hardening_links} ${shared_flags}) + check_and_append_flag(FLAGS "-fPIC -shared" TARGETS target_flags) + flags_to_list(target_flags "${target_flags}") endif() endif() + set(FLAGS ${hardening_flags} ${target_flags}) + set(LINKS ${hardening_links} ${target_flags}) + message(VERBOSE "Hardening target ${target} by ${CMAKE_CURRENT_FUNCTION}: Hardening compiling flags: ${FLAGS} Hardening linking flags: ${LINKS}")