You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
5.5 KiB

  1. # The MIT License (MIT)
  2. #
  3. # Copyright (c) 2013 Matthew Arsenault
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in all
  13. # copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. # This module tests if address sanitizer is supported by the compiler. The
  23. # necessary flags for compiler and linker will be stored in variables. ASan can
  24. # be enabled for all targets with CMake build type "ASan", individual targets
  25. # can enable ASan with the saitize_address() function.
  26. option(SANITIZE_ADDRESS "Selects wheter Address Sanitizer will be enabled for
  27. individual targets" Off)
  28. set(ASAN_FLAG_CANDIDATES
  29. # Clang 3.2+ use this version
  30. "-fsanitize=address"
  31. # Older deprecated flag for ASan
  32. "-faddress-sanitizer"
  33. )
  34. set(CMAKE_REQUIRED_QUIET_SAVE ${CMAKE_REQUIRED_QUIET})
  35. set(CMAKE_REQUIRED_QUIET ${ASan_FIND_QUIETLY})
  36. set(_ASAN_REQUIRED_VARS)
  37. get_property(ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
  38. foreach (LANG ${ENABLED_LANGUAGES})
  39. if (CMAKE_${LANG}_COMPILER_LOADED)
  40. list(APPEND _ASAN_REQUIRED_VARS ASAN_${LANG}_FLAGS)
  41. # If flags for this compiler were already found, do not try to find them
  42. # again.
  43. if (NOT ASAN_${LANG}_FLAGS)
  44. foreach (FLAG ${ASAN_FLAG_CANDIDATES})
  45. if(NOT CMAKE_REQUIRED_QUIET)
  46. message(STATUS "Try Address sanitizer ${LANG} flag = [${FLAG}]")
  47. endif()
  48. set(CMAKE_REQUIRED_FLAGS "${FLAG}")
  49. unset(ASAN_FLAG_DETECTED CACHE)
  50. if (${LANG} STREQUAL "C")
  51. include(CheckCCompilerFlag)
  52. check_c_compiler_flag("${FLAG}" ASAN_FLAG_DETECTED)
  53. elseif (${LANG} STREQUAL "CXX")
  54. include(CheckCXXCompilerFlag)
  55. check_cxx_compiler_flag("${FLAG}" ASAN_FLAG_DETECTED)
  56. elseif (${LANG} STREQUAL "Fortran")
  57. include(CheckFortranCompilerFlag)
  58. check_fortran_compiler_flag("${FLAG}" ASAN_FLAG_DETECTED)
  59. endif()
  60. if (ASAN_FLAG_DETECTED)
  61. set(ASAN_${LANG}_FLAGS "${FLAG}"
  62. CACHE STRING "${LANG} compiler flags for Address sanitizer")
  63. break()
  64. endif ()
  65. endforeach()
  66. endif ()
  67. endif ()
  68. endforeach ()
  69. set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE})
  70. if (_ASAN_REQUIRED_VARS)
  71. include(FindPackageHandleStandardArgs)
  72. find_package_handle_standard_args(ASan REQUIRED_VARS ${_ASAN_REQUIRED_VARS})
  73. mark_as_advanced(${_ASAN_REQUIRED_VARS})
  74. unset(_ASAN_REQUIRED_VARS)
  75. else()
  76. message(SEND_ERROR "FindASan requires C, CXX or Fortran language to be enabled")
  77. endif()
  78. # add build target ASan
  79. if (ASan_FOUND)
  80. get_property(ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
  81. foreach (LANG ${ENABLED_LANGUAGES})
  82. set(CMAKE_${LANG}_FLAGS_ASAN "${ASAN_${LANG}_FLAGS}" CACHE
  83. STRING "Flags used by the ${LANG} compiler during ASan builds.")
  84. mark_as_advanced(CMAKE_${LANG}_FLAGS_ASAN)
  85. endforeach ()
  86. set(CMAKE_EXE_LINKER_FLAGS_ASAN "${ASAN_C_FLAGS}" CACHE
  87. STRING "Flags used for linking binaries during ASan builds.")
  88. set(CMAKE_SHARED_LINKER_FLAGS_ASAN "${ASAN_C_FLAGS}" CACHE
  89. STRING "Flags used by the shared libraries linker during ASan builds.")
  90. set(CMAKE_MODULE_LINKER_FLAGS_ASAN "${ASAN_C_FLAGS}" CACHE
  91. STRING "Flags used by the module libraries linker during ASan builds.")
  92. mark_as_advanced(CMAKE_EXE_LINKER_FLAGS_ASAN
  93. CMAKE_SHARED_LINKER_FLAGS_ASAN
  94. CMAKE_MODULE_LINKER_FLAGS_ASAN)
  95. endif ()
  96. function (sanitize_address TARGET)
  97. if (NOT SANITIZE_ADDRESS)
  98. return()
  99. endif ()
  100. get_property(ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
  101. get_target_property(SOURCE_FILES ${TARGET} SOURCES)
  102. foreach (SOURCE_FILE ${SOURCE_FILES})
  103. foreach (LANG ${ENABLED_LANGUAGES})
  104. get_filename_component(FILE_EXT "${SOURCE_FILE}" EXT)
  105. string(TOLOWER "${FILE_EXT}" FILE_EXT)
  106. string(SUBSTRING "${FILE_EXT}" 1 -1 FILE_EXT)
  107. list(FIND CMAKE_${LANG}_SOURCE_FILE_EXTENSIONS "${FILE_EXT}" TEMP)
  108. if (NOT ${TEMP} EQUAL -1)
  109. if (DEFINED ASAN_${LANG}_FLAGS})
  110. set_property(SOURCE ${SOURCE_FILE} APPEND_STRING PROPERTY
  111. COMPILE_FLAGS " ${ASAN_${LANG}_FLAGS}")
  112. endif ()
  113. endif ()
  114. endforeach()
  115. endforeach (SOURCE_FILE)
  116. set_property(TARGET ${TARGET} APPEND_STRING PROPERTY
  117. LINK_FLAGS " ${ASAN_C_FLAGS}")
  118. endfunction ()