diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt index f97d007768..8c05c6a2b2 100644 --- a/src/nvim/CMakeLists.txt +++ b/src/nvim/CMakeLists.txt @@ -1,5 +1,11 @@ include(CheckLibraryExists) +option(SANITIZE "Enable Clang sanitizers for nvim binary" OFF) +if(SANITIZE AND NOT CMAKE_C_COMPILER_ID MATCHES "Clang") + message(WARNING "SANITIZE is only supported for Clang ... disabling") + set(SANITIZE OFF) +endif() + set(GENERATED_DIR ${PROJECT_BINARY_DIR}/src/nvim/auto) set(DISPATCH_GENERATOR ${PROJECT_SOURCE_DIR}/scripts/msgpack-gen.lua) file(GLOB API_HEADERS api/*.h) @@ -94,26 +100,15 @@ endforeach() set_source_files_properties( ${CONV_SOURCES} PROPERTIES COMPILE_FLAGS "${COMPILE_FLAGS} -Wno-conversion") -option(SANITIZE "Enable sanitizers when using Clang" OFF) -if(CMAKE_C_COMPILER_ID MATCHES "Clang") - if(SANITIZE) - message(STATUS "Enabling the sanitizers") - add_definitions(-DEXITFREE) # is this necessary for LeakSanitizer? - add_definitions(-fno-sanitize-recover -fno-omit-frame-pointer - -fno-optimize-sibling-calls -fsanitize=address -fsanitize=undefined) - set(CMAKE_EXE_LINKER_FLAGS - "-fsanitize=address -fsanitize=undefined ${CMAKE_EXE_LINKER_FLAGS}") - set(CMAKE_SHARED_LINKER_FLAGS - "-fsanitize=address -fsanitize=undefined ${CMAKE_SHARED_LINKER_FLAGS}") - endif() -endif() - get_directory_property(gen_cdefs COMPILE_DEFINITIONS) foreach(gen_cdef ${gen_cdefs} DO_NOT_DEFINE_EMPTY_ATTRIBUTES) if(NOT "${gen_cdef}" MATCHES "INCLUDE_GENERATED_DECLARATIONS") list(APPEND gen_cflags "-D${gen_cdef}") endif() endforeach() +if (SANITIZE) + list(APPEND gen_cflags "-DEXITFREE") +endif() get_directory_property(gen_includes INCLUDE_DIRECTORIES) foreach(gen_include ${gen_includes}) @@ -206,6 +201,13 @@ add_executable(nvim ${NEOVIM_GENERATED_SOURCES} ${NEOVIM_SOURCES} target_link_libraries(nvim ${NVIM_LINK_LIBRARIES}) install_helper(TARGETS nvim) +if(SANITIZE) + message(STATUS "Enabling Clang sanitizers for nvim") + set_property(TARGET nvim APPEND_STRING PROPERTY COMPILE_FLAGS "-DEXITFREE ") + set_property(TARGET nvim APPEND_STRING PROPERTY COMPILE_FLAGS "-fno-sanitize-recover -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize=address -fsanitize=undefined ") + set_property(TARGET nvim APPEND_STRING PROPERTY LINK_FLAGS "-fsanitize=address -fsanitize=undefined ") +endif() + add_library(nvim-test MODULE EXCLUDE_FROM_ALL ${NEOVIM_GENERATED_SOURCES} ${NEOVIM_SOURCES} ${NEOVIM_HEADERS}) target_link_libraries(nvim-test ${NVIM_LINK_LIBRARIES})