選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

125 行
4.4 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. foreach (LANG C CXX)
  38. if (NOT CMAKE_${LANG}_COMPILER_LOADED)
  39. continue()
  40. endif()
  41. list(APPEND _ASAN_REQUIRED_VARS ASAN_${LANG}_FLAGS)
  42. # If flags for this compiler were already found, do not try to find them
  43. # again.
  44. if (ASAN_${LANG}_FLAGS)
  45. continue()
  46. endif ()
  47. foreach (FLAG ${ASAN_FLAG_CANDIDATES})
  48. if(NOT CMAKE_REQUIRED_QUIET)
  49. message(STATUS "Try Address sanitizer ${LANG} flag = [${FLAG}]")
  50. endif()
  51. set(CMAKE_REQUIRED_FLAGS "${FLAG}")
  52. unset(ASAN_FLAG_DETECTED CACHE)
  53. if (${LANG} STREQUAL "C")
  54. include(CheckCCompilerFlag)
  55. check_c_compiler_flag("${FLAG}" ASAN_FLAG_DETECTED)
  56. elseif (${LANG} STREQUAL "CXX")
  57. include(CheckCXXCompilerFlag)
  58. check_cxx_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. endforeach ()
  67. set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE})
  68. if (_ASAN_REQUIRED_VARS)
  69. include(FindPackageHandleStandardArgs)
  70. find_package_handle_standard_args(ASan REQUIRED_VARS ${_ASAN_REQUIRED_VARS})
  71. mark_as_advanced(${_ASAN_REQUIRED_VARS})
  72. unset(_ASAN_REQUIRED_VARS)
  73. else()
  74. message(SEND_ERROR "FindASan requires C or CXX language to be enabled")
  75. endif()
  76. # add build target ASan
  77. if (ASan_FOUND)
  78. set(CMAKE_C_FLAGS_ASAN "${ASAN_C_FLAGS}" CACHE
  79. STRING "Flags used by the C compiler during ASan builds.")
  80. set(CMAKE_CXX_FLAGS_ASAN "${ASAN_CXX_FLAGS}" CACHE
  81. STRING "Flags used by the C++ compiler during ASan builds.")
  82. set(CMAKE_EXE_LINKER_FLAGS_ASAN "${ASAN_C_FLAGS}" CACHE
  83. STRING "Flags used for linking binaries during ASan builds.")
  84. set(CMAKE_SHARED_LINKER_FLAGS_ASAN "${ASAN_C_FLAGS}" CACHE
  85. STRING "Flags used by the shared libraries linker during ASan builds.")
  86. set(CMAKE_MODULE_LINKER_FLAGS_ASAN "${ASAN_C_FLAGS}" CACHE
  87. STRING "Flags used by the module libraries linker during ASan builds.")
  88. mark_as_advanced(CMAKE_C_FLAGS_ASAN
  89. CMAKE_CXX_FLAGS_ASAN
  90. CMAKE_EXE_LINKER_FLAGS_ASAN
  91. CMAKE_SHARED_LINKER_FLAGS_ASAN
  92. CMAKE_MODULE_LINKER_FLAGS_ASAN)
  93. endif ()
  94. function (sanitize_address TARGET)
  95. if (NOT SANITIZE_ADDRESS)
  96. return()
  97. endif ()
  98. set_property(TARGET ${TARGET} APPEND_STRING PROPERTY
  99. COMPILE_FLAGS " ${ASAN_C_FLAGS}")
  100. set_property(TARGET ${TARGET} APPEND_STRING PROPERTY
  101. LINK_FLAGS " ${ASAN_C_FLAGS}")
  102. endfunction ()