improve add compile and linker flags

This commit is contained in:
olszomal 2022-07-29 13:58:39 +02:00 committed by Michał Trojnara
parent 1d0918c84d
commit d7daf98db8

View File

@ -1,61 +1,99 @@
include(CheckCCompilerFlag) include(CheckCCompilerFlag)
function(add_compile_flags_target target) function(add_linker_flag_if_supported flagname targets)
if (CMAKE_C_COMPILER_ID MATCHES "Clang|AppleClang|GNU" ) check_c_compiler_flag("${flagname}" HAVE_FLAG_${flagname})
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-ggdb -g>) if (HAVE_FLAG_${flagname})
foreach(target ${targets})
target_link_options(${target} PRIVATE ${flagname})
endforeach()
endif() 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 $<$<CONFIG:DEBUG>:${flagname}>)
endforeach()
endif()
endfunction()
function(add_compile_flag_to_targets targets)
set(CHECKED_FLAGS
# Support address space layout randomization (ASLR) # Support address space layout randomization (ASLR)
target_compile_options(${target} PRIVATE -fPIE) "-fPIE"
check_c_compiler_flag("-fstack-protector-all" HAVE_STACK_PROTECTOR_ALL) )
if(HAVE_STACK_PROTECTOR_ALL) set(CHECKED_DEBUG_FLAGS
target_link_options(${target} PRIVATE -fstack-protector-all) "-ggdb"
else() "-g"
check_c_compiler_flag("-fstack-protector" HAVE_STACK_PROTECTOR) "-O2"
if(HAVE_STACK_PROTECTOR) "-pedantic"
target_link_options(${target} PRIVATE -fstack-protector) "-Wall"
else() "-Wextra"
message(WARNING "No stack protection supported") "-Wno-long-long"
endif() "-Wconversion"
endif() "-D_FORTIFY_SOURCE=2"
target_link_options(${target} PRIVATE -fstack-check) "-Wformat=2"
#target_link_options(${target} PRIVATE -fPIE -pie) "-Wredundant-decls"
#target_link_options(${target} PRIVATE -Wl,-z,relro) "-Wcast-qual"
#target_link_options(${target} PRIVATE -Wl,-z,now) "-Wnull-dereference"
#target_link_options(${target} PRIVATE -Wl,-z,noexecstack) "-Wno-deprecated-declarations"
"-Wmissing-declarations"
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-O2>) "-Wmissing-prototypes"
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-pedantic>) "-Wmissing-noreturn"
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wno-long-long>) "-Wmissing-braces"
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wconversion>) "-Wparentheses"
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-D_FORTIFY_SOURCE=2>) "-Wstrict-aliasing=3"
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wformat=2>) "-Wstrict-overflow=2"
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wundef>) "-Wlogical-op"
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wshadow>) "-Wwrite-strings"
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wredundant-decls>) "-Wcast-align=strict"
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wcast-qual>) "-Wdisabled-optimization"
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wnull-dereference>) "-Wshift-overflow=2"
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wmissing-declarations>) "-Wundef"
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wmissing-prototypes>) "-Wshadow"
endif() "-Wmisleading-indentation"
"-Wabsolute-value"
if(CMAKE_C_COMPILER_ID MATCHES "GNU") "-Wunused-parameter"
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wall>) "-Wunused-function"
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wextra>) )
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wno-deprecated-declarations>) foreach(flag ${CHECKED_FLAGS})
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wstrict-aliasing=3>) add_compile_flag_if_supported(${flag} ${targets})
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wstrict-overflow=2>) endforeach()
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wlogical-op>) foreach(flag ${CHECKED_DEBUG_FLAGS})
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wwrite-strings>) add_debug_flag_if_supported(${flag} ${targets})
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wcast-align=strict>) endforeach()
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wdisabled-optimization>) endfunction()
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wshift-overflow=2>)
endif()
function(add_compile_flags target)
if(MSVC) if(MSVC)
# Enable parallel builds # Enable parallel builds
add_definitions(/MP) target_compile_options(${target} PRIVATE /MP)
# Use address space layout randomization, generate PIE code for ASLR (default on) # Use address space layout randomization, generate PIE code for ASLR (default on)
target_link_options(${target} PRIVATE /DYNAMICBASE) target_link_options(${target} PRIVATE /DYNAMICBASE)
# Create terminal server aware application (default on) # Create terminal server aware application (default on)
@ -86,7 +124,10 @@ function(add_compile_flags_target target)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:/D_FORTIFY_SOURCE=2>) target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:/D_FORTIFY_SOURCE=2>)
# Unrecognized compiler options are errors # Unrecognized compiler options are errors
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:/options:strict>) target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:/options:strict>)
else()
add_linker_flag_to_targets(${target})
add_compile_flag_to_targets(${target})
endif() endif()
endfunction() endfunction()
add_compile_flags_target(osslsigncode) add_compile_flags(osslsigncode)