cmake: support libasan/-fsanitize

Add the ENABLE_ASAN cmake option, defaulting to `-fsanitize=address` if
ON, or the value specified for the option.
This commit is contained in:
Rafael Kitover 2019-01-02 08:23:26 -08:00
parent ff2d31bf5e
commit de0e8d6ba3
2 changed files with 18 additions and 3 deletions

View File

@ -25,6 +25,7 @@ option(ENABLE_SDL "Build the SDL port" OFF)
option(ENABLE_WX "Build the wxWidgets port" ON)
option(ENABLE_DEBUGGER "Enable the debugger" ON)
option(ENABLE_NLS "Enable translations" ON)
option(ENABLE_ASAN "Enable -fsanitize=<option>, address by default, requires debug build" OFF)
option(VBAM_STATIC "Try to link all libraries statically" OFF)
@ -389,12 +390,25 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID STREQUAL Clang)
if(X86_32 OR AMD64)
set(MY_C_OPT_FLAGS ${MY_C_OPT_FLAGS} -mtune=generic)
endif()
# common debug flags
if(CMAKE_COMPILER_IS_GNUCXX)
set(MY_C_DBG_FLAGS -ggdb3 -Og)
set(MY_C_DBG_FLAGS -ggdb3 -Og -fno-omit-frame-pointer)
else()
set(MY_C_DBG_FLAGS -g)
set(MY_C_DBG_FLAGS -g -fno-omit-frame-pointer)
endif()
if(ENABLE_ASAN)
string(TOLOWER ${CMAKE_BUILD_TYPE} build)
if(NOT build STREQUAL "debug")
message(FATAL_ERROR "asan requires debug build, set -DCMAKE_BUILD_TYPE=Debug")
endif()
string(TOLOWER ${ENABLE_ASAN} SANITIZER)
if(SANITIZER STREQUAL "on" OR SANITIZER STREQUAL "true")
set(SANITIZER address)
endif()
set(MY_C_DBG_FLAGS ${MY_C_DBG_FLAGS} -fsanitize=${SANITIZER} -lasan)
endif()
# common flags

View File

@ -147,6 +147,7 @@ Here is the complete list:
| ENABLE_XAUDIO2 | Enable xaudio2 sound output for wxWidgets (Windows only) | ON |
| ENABLE_OPENAL | Enable OpenAL for the wxWidgets port | OFF |
| ENABLE_SSP | Enable gcc stack protector support (gcc only) | OFF |
| ENABLE_ASAN | Enable libasan sanitizers (by default address, only in debug mode) | OFF |
| VBAM_STATIC | Try link all libs statically (the following are set to ON if ON) | OFF |
| SDL2_STATIC | Try to link static SDL2 libraries | OFF |
| SFML_STATIC_LIBRARIES | Try to link static SFML libraries | OFF |