From d7daf98db88e9e8b47bf27c5328b42c9499f1d9f Mon Sep 17 00:00:00 2001 From: olszomal Date: Fri, 29 Jul 2022 13:58:39 +0200 Subject: [PATCH] improve add compile and linker flags --- cmake/SetCompilerFlags.cmake | 143 ++++++++++++++++++++++------------- 1 file changed, 92 insertions(+), 51 deletions(-) diff --git a/cmake/SetCompilerFlags.cmake b/cmake/SetCompilerFlags.cmake index 98a1694..edfc3a4 100644 --- a/cmake/SetCompilerFlags.cmake +++ b/cmake/SetCompilerFlags.cmake @@ -1,61 +1,99 @@ include(CheckCCompilerFlag) -function(add_compile_flags_target target) - if (CMAKE_C_COMPILER_ID MATCHES "Clang|AppleClang|GNU" ) - target_compile_options(${target} PRIVATE $<$:-ggdb -g>) +function(add_linker_flag_if_supported flagname targets) + check_c_compiler_flag("${flagname}" HAVE_FLAG_${flagname}) + if (HAVE_FLAG_${flagname}) + foreach(target ${targets}) + target_link_options(${target} PRIVATE ${flagname}) + endforeach() endif() +endfunction() - if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang") +function(add_compile_flag_if_supported flagname targets) + check_c_compiler_flag("${flagname}" HAVE_FLAG_${flagname}) + if (HAVE_FLAG_${flagname}) + foreach(target ${targets}) + target_compile_options(${target} PRIVATE ${flagname}) + endforeach() + endif() +endfunction() + +function(add_linker_flag_to_targets targets) + set(CHECKED_FLAGS + "-fstack-protector-all" + "-fstack-protector" + "-fstack-check" + "-fPIE" + "-pie" + "-Wl,-z,relro" + "-Wl,-z,now" + "-Wl,-z,noexecstack" + ) + foreach(flag ${CHECKED_FLAGS}) + add_linker_flag_if_supported(${flag} "${targets}") + endforeach() +endfunction() + +function(add_debug_flag_if_supported flagname targets) + check_c_compiler_flag("${flagname}" HAVE_FLAG_${flagname}) + if (HAVE_FLAG_${flagname}) + foreach(target ${targets}) + target_compile_options(${target} PRIVATE $<$:${flagname}>) + endforeach() + endif() +endfunction() + +function(add_compile_flag_to_targets targets) + set(CHECKED_FLAGS # Support address space layout randomization (ASLR) - target_compile_options(${target} PRIVATE -fPIE) - check_c_compiler_flag("-fstack-protector-all" HAVE_STACK_PROTECTOR_ALL) - if(HAVE_STACK_PROTECTOR_ALL) - target_link_options(${target} PRIVATE -fstack-protector-all) - else() - check_c_compiler_flag("-fstack-protector" HAVE_STACK_PROTECTOR) - if(HAVE_STACK_PROTECTOR) - target_link_options(${target} PRIVATE -fstack-protector) - else() - message(WARNING "No stack protection supported") - endif() - endif() - target_link_options(${target} PRIVATE -fstack-check) - #target_link_options(${target} PRIVATE -fPIE -pie) - #target_link_options(${target} PRIVATE -Wl,-z,relro) - #target_link_options(${target} PRIVATE -Wl,-z,now) - #target_link_options(${target} PRIVATE -Wl,-z,noexecstack) - - target_compile_options(${target} PRIVATE $<$:-O2>) - target_compile_options(${target} PRIVATE $<$:-pedantic>) - target_compile_options(${target} PRIVATE $<$:-Wno-long-long>) - target_compile_options(${target} PRIVATE $<$:-Wconversion>) - target_compile_options(${target} PRIVATE $<$:-D_FORTIFY_SOURCE=2>) - target_compile_options(${target} PRIVATE $<$:-Wformat=2>) - target_compile_options(${target} PRIVATE $<$:-Wundef>) - target_compile_options(${target} PRIVATE $<$:-Wshadow>) - target_compile_options(${target} PRIVATE $<$:-Wredundant-decls>) - target_compile_options(${target} PRIVATE $<$:-Wcast-qual>) - target_compile_options(${target} PRIVATE $<$:-Wnull-dereference>) - target_compile_options(${target} PRIVATE $<$:-Wmissing-declarations>) - target_compile_options(${target} PRIVATE $<$:-Wmissing-prototypes>) - endif() - - if(CMAKE_C_COMPILER_ID MATCHES "GNU") - target_compile_options(${target} PRIVATE $<$:-Wall>) - target_compile_options(${target} PRIVATE $<$:-Wextra>) - target_compile_options(${target} PRIVATE $<$:-Wno-deprecated-declarations>) - target_compile_options(${target} PRIVATE $<$:-Wstrict-aliasing=3>) - target_compile_options(${target} PRIVATE $<$:-Wstrict-overflow=2>) - target_compile_options(${target} PRIVATE $<$:-Wlogical-op>) - target_compile_options(${target} PRIVATE $<$:-Wwrite-strings>) - target_compile_options(${target} PRIVATE $<$:-Wcast-align=strict>) - target_compile_options(${target} PRIVATE $<$:-Wdisabled-optimization>) - target_compile_options(${target} PRIVATE $<$:-Wshift-overflow=2>) - endif() + "-fPIE" + ) + set(CHECKED_DEBUG_FLAGS + "-ggdb" + "-g" + "-O2" + "-pedantic" + "-Wall" + "-Wextra" + "-Wno-long-long" + "-Wconversion" + "-D_FORTIFY_SOURCE=2" + "-Wformat=2" + "-Wredundant-decls" + "-Wcast-qual" + "-Wnull-dereference" + "-Wno-deprecated-declarations" + "-Wmissing-declarations" + "-Wmissing-prototypes" + "-Wmissing-noreturn" + "-Wmissing-braces" + "-Wparentheses" + "-Wstrict-aliasing=3" + "-Wstrict-overflow=2" + "-Wlogical-op" + "-Wwrite-strings" + "-Wcast-align=strict" + "-Wdisabled-optimization" + "-Wshift-overflow=2" + "-Wundef" + "-Wshadow" + "-Wmisleading-indentation" + "-Wabsolute-value" + "-Wunused-parameter" + "-Wunused-function" + ) + foreach(flag ${CHECKED_FLAGS}) + add_compile_flag_if_supported(${flag} ${targets}) + endforeach() + foreach(flag ${CHECKED_DEBUG_FLAGS}) + add_debug_flag_if_supported(${flag} ${targets}) + endforeach() +endfunction() +function(add_compile_flags target) if(MSVC) # Enable parallel builds - add_definitions(/MP) + target_compile_options(${target} PRIVATE /MP) # Use address space layout randomization, generate PIE code for ASLR (default on) target_link_options(${target} PRIVATE /DYNAMICBASE) # Create terminal server aware application (default on) @@ -86,7 +124,10 @@ function(add_compile_flags_target target) target_compile_options(${target} PRIVATE $<$:/D_FORTIFY_SOURCE=2>) # Unrecognized compiler options are errors target_compile_options(${target} PRIVATE $<$:/options:strict>) + else() + add_linker_flag_to_targets(${target}) + add_compile_flag_to_targets(${target}) endif() endfunction() -add_compile_flags_target(osslsigncode) +add_compile_flags(osslsigncode)