src.dualinventive.com/fw/dncm/libdi_fw/common/cmake/CFlagsConfig.txt

164 lines
4.8 KiB
Plaintext

include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
function(add_cflag flag)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE)
endfunction()
function(add_cxxflag flag)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE)
endfunction()
function(add_cflag_if_supported flag)
get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
if("C" IN_LIST languages)
string(REPLACE "-" "_" flagclean ${flag})
string(REPLACE "=" "_" flagclean ${flagclean})
string(REPLACE "+" "_" flagclean ${flagclean})
string(REPLACE "," "_" flagclean ${flagclean})
string(TOUPPER ${flagclean} flagclean)
check_c_compiler_flag(${flag} C_SUPPORTS${flagclean})
if (C_SUPPORTS${flagclean})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE)
endif()
endif()
endfunction()
function(add_cxxflag_if_supported flag)
get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
if("CXX" IN_LIST languages)
string(REPLACE "-" "_" flagclean ${flag})
string(REPLACE "=" "_" flagclean ${flagclean})
string(REPLACE "+" "_" flagclean ${flagclean})
string(REPLACE "," "_" flagclean ${flagclean})
string(TOUPPER ${flagclean} flagclean)
check_cxx_compiler_flag(${flag} CXX_SUPPORTS${flagclean})
if (CXX_SUPPORTS${flagclean})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE)
endif()
endif()
endfunction()
###
# Code-coverage flags
###
if (CMAKE_BUILD_TYPE STREQUAL "Debug" AND NOT DEFINED STM32_CHIP AND "${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
message("-- Code coverage enabled")
include("${CMAKE_CURRENT_LIST_DIR}/GcovReport.txt")
# This is not detected by add_cflag_if_supported(), therefore it is done here
set(CMAKE_C_FLAGS "-fprofile-arcs -ftest-coverage")
set(CMAKE_CXX_FLAGS "-fprofile-arcs -ftest-coverage")
else()
message("-- Code coverage disabled")
endif()
##
# C-compiler flags
##
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions(-Dprotected=public -Dprivate=public -DDEBUG_BUILD)
add_definitions(-DDI_DEBUG)
if (NOT DEFINED OPTIMALIZATION_DEBUG)
add_cflag(-O0)
else()
message(STATUS "Optimalization is overridden to: ${OPTIMALIZATION_DEBUG}")
add_cflag(${OPTIMALIZATION_DEBUG})
endif()
add_cflag(-ggdb)
#add_cflag_if_supported(-fsanitize=address)
#add_cflag_if_supported(-fsanitize=null)
#add_cflag_if_supported(-fsanitize=unreachable)
#add_cflag_if_supported(-fsanitize=bool)
#add_cflag_if_supported(-fsanitize=enum)
#add_cflag_if_supported(-fsanitize=vptr)
else()
add_cflag(-O2)
endif()
add_cflag(-Wall)
if (NOT DI_LEGACY_APPLICATION)
add_cflag(-Werror)
endif()
add_cflag(-Wextra)
add_cflag(-D_FORTIFY_SOURCE=2)
add_cflag(-fstrict-aliasing)
add_cflag(-std=c11)
add_cflag(-Wformat)
add_cflag(-Wformat-security)
add_cflag(-Wshadow)
add_cflag(-Wundef)
add_cflag(-Wimplicit-function-declaration)
add_cflag(-Wredundant-decls)
add_cflag(-Wconversion)
add_cflag(-Wno-float-conversion)
if (NOT DEFINED STM32_CHIP)
add_cflag(-fPIC)
if (NOT DI_LEGACY_APPLICATION)
add_cflag(-fPIE)
add_cflag_if_supported(-pie)
endif()
endif()
add_cflag_if_supported(-Wmaybe-uninitialized)
add_cflag_if_supported(-Wenum-conversion)
add_cflag_if_supported(-Wmissing-variable-declarations)
add_cflag_if_supported(-Wshorten-64-to-32)
add_cflag_if_supported(-Wl,-Bsymbolic-functions)
add_cflag_if_supported(-Wl,-z,relro)
add_cflag_if_supported(-Wl,-z,now)
add_cflag_if_supported("-Wmisleading-indentation")
add_cflag_if_supported("-Wpointer-arith")
##
# CXX-compiler flags
##
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
if (NOT DEFINED OPTIMALIZATION_DEBUG)
add_cxxflag(-O0)
else()
message(STATUS "Optimalization is overridden to: ${OPTIMALIZATION_DEBUG}")
add_cxxflag(${OPTIMALIZATION_DEBUG})
endif()
add_cxxflag(-ggdb)
#add_cxxflag_if_supported(-fsanitize=address)
#add_cxxflag_if_supported(-fsanitize=null)
#add_cxxflag_if_supported(-fsanitize=unreachable)
#add_cxxflag_if_supported(-fsanitize=bool)
#add_cxxflag_if_supported(-fsanitize=enum)
#add_cxxflag_if_supported(-fsanitize=vptr)
else()
add_cxxflag(-O2)
endif()
add_cxxflag(-Wall)
add_cxxflag(-Werror)
add_cxxflag(-Wextra)
add_cxxflag(-D_FORTIFY_SOURCE=2)
add_cxxflag(-std=c++14)
add_cxxflag(-fstrict-aliasing)
add_cxxflag(-Wformat)
add_cxxflag(-Wformat-security)
add_cxxflag(-Wshadow)
add_cxxflag(-fPIC)
add_cxxflag(-Wnon-virtual-dtor)
add_cxxflag(-Wredundant-decls)
add_cxxflag(-Wundef)
add_cxxflag(-Wconversion)
add_cxxflag(-Wold-style-cast)
add_cxxflag(-Wno-float-conversion)
if (NOT DI_LEGACY_APPLICATION)
add_cxxflag(-fPIE)
add_cxxflag_if_supported(-pie)
endif()
add_cxxflag_if_supported(-Wmissing-variable-declarations)
add_cxxflag_if_supported(-Wshorten-64-to-32)
add_cxxflag_if_supported(-Wimplicit-function-declaration)
add_cxxflag_if_supported(-Wmaybe-uninitialized)
add_cxxflag_if_supported(-Wl,-Bsymbolic-functions)
add_cxxflag_if_supported(-Wl,-z,relro)
add_cxxflag_if_supported(-Wl,-z,now)
add_cxxflag_if_supported("-Wmisleading-indentation")
add_cxxflag_if_supported("-Wpointer-arith")