174 lines
7.9 KiB
CMake
174 lines
7.9 KiB
CMake
# Função para verificar se uma flag do compilador é suportada e retornar essa flag
|
|
function(check_compiler_flag flag required result_var)
|
|
string(REPLACE "-" "_" flag_var "${flag}")
|
|
string(TOUPPER "${flag_var}" flag_var) # Converter para maiúsculas para evitar problemas de case sensitivity
|
|
|
|
# Verifique se a flag é suportada pelo compilador
|
|
check_cxx_compiler_flag("${flag}" "HAVE_${flag_var}_SUPPORT")
|
|
if(${HAVE_${flag_var}_SUPPORT})
|
|
set(${result_var}
|
|
${flag}
|
|
PARENT_SCOPE)
|
|
else()
|
|
if(required)
|
|
message(FATAL_ERROR "O compilador não suporta a flag obrigatória ${flag}")
|
|
else()
|
|
set(${result_var}
|
|
""
|
|
PARENT_SCOPE)
|
|
endif()
|
|
endif()
|
|
endfunction()
|
|
|
|
# Função para verificar se uma flag do linker é suportada e retornar essa flag
|
|
function(check_linker_flag flag required result_var)
|
|
string(REPLACE "-" "_" flag_var "${flag}")
|
|
string(TOUPPER "${flag_var}" flag_var) # Converter para maiúsculas para evitar problemas de case sensitivity
|
|
|
|
# Verifique se a flag é suportada pelo compilador (usando o linker)
|
|
check_cxx_compiler_flag("${flag}" "HAVE_${flag_var}_SUPPORT" LINKER)
|
|
if(${HAVE_${flag_var}_SUPPORT})
|
|
set(${result_var}
|
|
${flag}
|
|
PARENT_SCOPE)
|
|
else()
|
|
if(required)
|
|
message(FATAL_ERROR "O linker não suporta a flag obrigatória ${flag}")
|
|
else()
|
|
set(${result_var}
|
|
""
|
|
PARENT_SCOPE)
|
|
endif()
|
|
endif()
|
|
endfunction()
|
|
|
|
# Função para filtrar flags de compilador/linker não suportadas
|
|
function(filter_unsupported_flags flags_var check_linker_flag)
|
|
# Lista temporária para armazenar flags suportadas
|
|
set(supported_flags)
|
|
|
|
# Itera sobre a lista de flags
|
|
foreach(flag ${${flags_var}})
|
|
if(check_linker_flag)
|
|
check_linker_flag(${flag} FALSE supported_flag)
|
|
else()
|
|
check_compiler_flag(${flag} FALSE supported_flag)
|
|
endif()
|
|
|
|
# Verifique e adicione as flags suportadas
|
|
if(NOT "${supported_flag}" STREQUAL "")
|
|
list(APPEND supported_flags ${supported_flag})
|
|
endif()
|
|
endforeach()
|
|
|
|
# Atualiza a variável original com a lista filtrada
|
|
set(${flags_var}
|
|
${supported_flags}
|
|
PARENT_SCOPE)
|
|
endfunction()
|
|
|
|
set(C_COMPILE_FLAGS
|
|
# Enable warnings for constructs often associated with defects
|
|
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html#enable-additional-format-function-warnings
|
|
-Wall
|
|
-Wextra
|
|
# Enable additional format function warnings
|
|
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html#enable-additional-format-function-warnings
|
|
-Wformat
|
|
-Wformat-security
|
|
# Enable implicit conversion warnings
|
|
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html#enable-implicit-conversion-warnings
|
|
-Wconversion
|
|
-Wsign-conversion
|
|
# Enable warning about trampolines that require executable stacks
|
|
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html#enable-warning-about-trampolines-that-require-executable-stacks
|
|
-Wtrampolines
|
|
# Warn about implicit fallthrough in switch statements
|
|
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html#warn-about-implicit-fallthrough-in-switch-statements
|
|
-Wimplicit-fallthrough)
|
|
|
|
set(CXX_COMPILE_FLAGS # Flags enabled only for CPP
|
|
# Do not delete null pointer checks
|
|
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html#do-not-delete-null-pointer-checks
|
|
-fno-delete-null-pointer-checks
|
|
# Integer overflow may occur
|
|
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html#integer-overflow-may-occur
|
|
-fno-strict-overflow
|
|
# Do not assume strict aliasing
|
|
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html#do-not-assume-strict-aliasing
|
|
-fno-strict-aliasing
|
|
# Perform trivial auto variable initialization
|
|
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html#perform-trivial-auto-variable-initialization
|
|
-ftrivial-auto-var-init=zero
|
|
# Enable exception propagation to harden multi-threaded C code
|
|
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html#enable-exception-propagation-to-harden-multi-threaded-c-code
|
|
-fexceptions)
|
|
|
|
# Flags enabled only on Release Mode
|
|
set(RELEASE_COMPILE_FLAGS
|
|
# Keeps only relevant symbols available to the library users
|
|
-fvisibility=hidden
|
|
-fvisibility-inlines-hidden
|
|
-ffunction-sections
|
|
-fdata-sections
|
|
# Enable code instrumentation of control-flow transfers to increase program security by checking that target addresses of
|
|
# control-flow transfer instructions are valid
|
|
# https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html#index-fcf-protection
|
|
-fcf-protection=full
|
|
# Enable run-time checks for stack-based buffer overflows
|
|
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html#enable-run-time-checks-for-stack-based-buffer-overflows
|
|
-fstack-protector-strong
|
|
# Build as position-independent code
|
|
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html#build-as-position-independent-code
|
|
-pie
|
|
-fPIE
|
|
# Enable run-time checks for variable-size stack allocation validity
|
|
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html#enable-run-time-checks-for-variable-size-stack-allocation-validity
|
|
-fstack-clash-protection
|
|
# Enable strict flexible arrays
|
|
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html#enable-strict-flexible-arrays
|
|
-fstrict-flex-arrays=3
|
|
# Precondition checks for C++ standard library calls
|
|
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html#precondition-checks-for-c-standard-library-calls
|
|
-D_GLIBCXX_ASSERTIONS
|
|
# Mark relocation table entries resolved at load-time as read-only
|
|
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html#mark-relocation-table-entries-resolved-at-load-time-as-read-only
|
|
-Wl,-z,relro,-z,now
|
|
# Enable data execution prevention
|
|
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html#enable-data-execution-prevention
|
|
-Wl,-z,noexecstack
|
|
# Restrict dlopen calls to shared objects
|
|
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html#restrict-dlopen-calls-to-shared-objects
|
|
-Wl,-z,nodlopen
|
|
-s
|
|
# Fortify sources for unsafe libc usage and buffer overflows
|
|
# https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html#fortify-sources-for-unsafe-libc-usage-and-buffer-overflows
|
|
-U_FORTIFY_SOURCE
|
|
-D_FORTIFY_SOURCE=3
|
|
-O2
|
|
-DNDEBUG)
|
|
|
|
# Flags enabled only on Debug Mode
|
|
set(DEBUG_COMPILE_FLAGS -g3 -Og -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-common)
|
|
|
|
filter_unsupported_flags(C_COMPILE_FLAGS OFF)
|
|
filter_unsupported_flags(CXX_COMPILE_FLAGS OFF)
|
|
filter_unsupported_flags(RELEASE_COMPILE_FLAGS OFF)
|
|
filter_unsupported_flags(DEBUG_COMPILE_FLAGS OFF)
|
|
|
|
include(SetSanitizers)
|
|
|
|
set(COMPILE_FLAGS ${C_COMPILE_FLAGS})
|
|
|
|
if(CMAKE_CXX_COMPILER)
|
|
list(APPEND COMPILE_FLAGS ${CXX_COMPILE_FLAGS})
|
|
endif()
|
|
|
|
if(CMAKE_BUILD_TYPE STREQUAL "Release")
|
|
list(APPEND COMPILE_FLAGS ${RELEASE_COMPILE_FLAGS})
|
|
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
|
list(APPEND COMPILE_FLAGS ${DEBUG_COMPILE_FLAGS})
|
|
list(APPEND COMPILE_FLAGS ${SANITIZE_FLAGS})
|
|
set(LINK_FLAGS ${SANITIZE_FLAGS})
|
|
endif()
|