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.

128 lines
5.0 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. "-g -O0 -fsanitize=address"
  31. # Older deprecated flag for ASan
  32. "-g -O0 -faddress-sanitizer"
  33. )
  34. set(CMAKE_REQUIRED_QUIET_SAVE ${CMAKE_REQUIRED_QUIET})
  35. set(CMAKE_REQUIRED_QUIET ${ASan_FIND_QUIETLY})
  36. get_property(ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
  37. foreach (LANG ${ENABLED_LANGUAGES})
  38. # Sanitizer flags are not dependend on language, but the used compiler. So
  39. # instead of searching flags foreach language, search flags foreach compiler
  40. # used.
  41. set(COMPILER ${CMAKE_${LANG}_COMPILER_ID})
  42. if (NOT ASAN_${COMPILER}_FLAGS)
  43. foreach (FLAG ${ASAN_FLAG_CANDIDATES})
  44. if(NOT CMAKE_REQUIRED_QUIET)
  45. message(STATUS
  46. "Try ${COMPILER} AddressSanitizer 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. # CheckFortranCompilerFlag was introduced in CMake 3.x. To be
  58. # compatible with older Cmake versions, we will check if this
  59. # module is present before we use it. Otherwise we will define
  60. # Fortran coverage support as not available.
  61. include(CheckFortranCompilerFlag OPTIONAL
  62. RESULT_VARIABLE INCLUDED)
  63. if (INCLUDED)
  64. check_fortran_compiler_flag("${FLAG}" ASAN_FLAG_DETECTED)
  65. elseif (NOT CMAKE_REQUIRED_QUIET)
  66. message("-- Performing Test ASAN_FLAG_DETECTED")
  67. message("-- Performing Test ASAN_FLAG_DETECTED - Failed "
  68. "(Check not supported)")
  69. endif ()
  70. endif()
  71. if (ASAN_FLAG_DETECTED)
  72. set(ASAN_${COMPILER}_FLAGS "${FLAG}"
  73. CACHE STRING "${LANG} compiler flags for AddressSanitizer.")
  74. mark_as_advanced(ASAN_${COMPILER}_FLAGS)
  75. break()
  76. endif ()
  77. endforeach ()
  78. endif ()
  79. endforeach ()
  80. set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE})
  81. include(sanitize-helpers)
  82. function (sanitize_address TARGET)
  83. if (NOT SANITIZE_ADDRESS)
  84. return()
  85. endif ()
  86. # Get list of compilers used by target and check, if target can be checked
  87. # by sanitizer.
  88. sanitizer_target_compilers(${TARGET} TARGET_COMPILER)
  89. list(LENGTH TARGET_COMPILER NUM_COMPILERS)
  90. if (NUM_COMPILERS GREATER 1)
  91. message(AUTHOR_WARNING "AddressSanitizer disabled for target ${TARGET} "
  92. "because it will be compiled by different compilers.")
  93. return()
  94. elseif ((NUM_COMPILERS EQUAL 0) OR
  95. (NOT DEFINED "ASAN_${TARGET_COMPILER}_FLAGS"))
  96. message(AUTHOR_WARNING "AddressSanitizer disabled for target ${TARGET} "
  97. "because there is no sanitizer available for target sources.")
  98. return()
  99. endif()
  100. # Set compile- and link-flags for target.
  101. set_property(TARGET ${TARGET} APPEND_STRING
  102. PROPERTY COMPILE_FLAGS " ${ASAN_${TARGET_COMPILER}_FLAGS}")
  103. set_property(TARGET ${TARGET} APPEND_STRING
  104. PROPERTY LINK_FLAGS " ${ASAN_${TARGET_COMPILER}_FLAGS}")
  105. endfunction ()