Sanitizers.cmake 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. function(enable_sanitizers project_name)
  2. if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
  3. option(ENABLE_COVERAGE "Enable coverage reporting for gcc/clang" FALSE)
  4. if(ENABLE_COVERAGE)
  5. target_compile_options(${project_name} INTERFACE --coverage -O0 -g)
  6. target_link_libraries(${project_name} INTERFACE --coverage)
  7. endif()
  8. set(SANITIZERS "")
  9. option(ENABLE_SANITIZER_ADDRESS "Enable address sanitizer" FALSE)
  10. if(ENABLE_SANITIZER_ADDRESS)
  11. list(APPEND SANITIZERS "address")
  12. endif()
  13. option(ENABLE_SANITIZER_LEAK "Enable leak sanitizer" FALSE)
  14. if(ENABLE_SANITIZER_LEAK)
  15. list(APPEND SANITIZERS "leak")
  16. endif()
  17. option(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "Enable undefined behavior sanitizer" FALSE)
  18. if(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR)
  19. list(APPEND SANITIZERS "undefined")
  20. endif()
  21. option(ENABLE_SANITIZER_THREAD "Enable thread sanitizer" FALSE)
  22. if(ENABLE_SANITIZER_THREAD)
  23. if("address" IN_LIST SANITIZERS OR "leak" IN_LIST SANITIZERS)
  24. message(WARNING "Thread sanitizer does not work with Address and Leak sanitizer enabled")
  25. else()
  26. list(APPEND SANITIZERS "thread")
  27. endif()
  28. endif()
  29. option(ENABLE_SANITIZER_MEMORY "Enable memory sanitizer" FALSE)
  30. if(ENABLE_SANITIZER_MEMORY AND CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
  31. if("address" IN_LIST SANITIZERS
  32. OR "thread" IN_LIST SANITIZERS
  33. OR "leak" IN_LIST SANITIZERS)
  34. message(WARNING "Memory sanitizer does not work with Address, Thread and Leak sanitizer enabled")
  35. else()
  36. list(APPEND SANITIZERS "memory")
  37. endif()
  38. endif()
  39. list(
  40. JOIN
  41. SANITIZERS
  42. ","
  43. LIST_OF_SANITIZERS)
  44. endif()
  45. if(LIST_OF_SANITIZERS)
  46. if(NOT
  47. "${LIST_OF_SANITIZERS}"
  48. STREQUAL
  49. "")
  50. target_compile_options(${project_name} INTERFACE -fsanitize=${LIST_OF_SANITIZERS})
  51. target_link_libraries(${project_name} INTERFACE -fsanitize=${LIST_OF_SANITIZERS})
  52. endif()
  53. endif()
  54. endfunction()