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)
function(add_compile_flags_target target)
if (CMAKE_C_COMPILER_ID MATCHES "Clang|AppleClang|GNU" )
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-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 $<$<CONFIG:DEBUG>:${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 $<$<CONFIG:DEBUG>:-O2>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-pedantic>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wno-long-long>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wconversion>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-D_FORTIFY_SOURCE=2>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wformat=2>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wundef>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wshadow>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wredundant-decls>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wcast-qual>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wnull-dereference>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wmissing-declarations>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wmissing-prototypes>)
endif()
if(CMAKE_C_COMPILER_ID MATCHES "GNU")
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wall>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wextra>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wno-deprecated-declarations>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wstrict-aliasing=3>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wstrict-overflow=2>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wlogical-op>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wwrite-strings>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wcast-align=strict>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-Wdisabled-optimization>)
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:-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 $<$<CONFIG:DEBUG>:/D_FORTIFY_SOURCE=2>)
# Unrecognized compiler options are errors
target_compile_options(${target} PRIVATE $<$<CONFIG:DEBUG>:/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)