您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

171 行
7.2 KiB

  1. # The MIT License (MIT)
  2. #
  3. # Copyright (c)
  4. # 2013 Matthew Arsenault
  5. # 2015-2016 RWTH Aachen University, Federal Republic of Germany
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in all
  15. # copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. # SOFTWARE.
  24. # Helper function to get the language of a source file.
  25. function (sanitizer_lang_of_source FILE RETURN_VAR)
  26. get_filename_component(FILE_EXT "${FILE}" EXT)
  27. string(TOLOWER "${FILE_EXT}" FILE_EXT)
  28. string(SUBSTRING "${FILE_EXT}" 1 -1 FILE_EXT)
  29. get_property(ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
  30. foreach (LANG ${ENABLED_LANGUAGES})
  31. list(FIND CMAKE_${LANG}_SOURCE_FILE_EXTENSIONS "${FILE_EXT}" TEMP)
  32. if (NOT ${TEMP} EQUAL -1)
  33. set(${RETURN_VAR} "${LANG}" PARENT_SCOPE)
  34. return()
  35. endif ()
  36. endforeach()
  37. set(${RETURN_VAR} "" PARENT_SCOPE)
  38. endfunction ()
  39. # Helper function to get compilers used by a target.
  40. function (sanitizer_target_compilers TARGET RETURN_VAR)
  41. # Check if all sources for target use the same compiler. If a target uses
  42. # e.g. C and Fortran mixed and uses different compilers (e.g. clang and
  43. # gfortran) this can trigger huge problems, because different compilers may
  44. # use different implementations for sanitizers.
  45. set(BUFFER "")
  46. get_target_property(TSOURCES ${TARGET} SOURCES)
  47. foreach (FILE ${TSOURCES})
  48. # If expression was found, FILE is a generator-expression for an object
  49. # library. Object libraries will be ignored.
  50. string(REGEX MATCH "TARGET_OBJECTS:([^ >]+)" _file ${FILE})
  51. if ("${_file}" STREQUAL "")
  52. sanitizer_lang_of_source(${FILE} LANG)
  53. if (LANG)
  54. list(APPEND BUFFER ${CMAKE_${LANG}_COMPILER_ID})
  55. endif ()
  56. endif ()
  57. endforeach ()
  58. list(REMOVE_DUPLICATES BUFFER)
  59. set(${RETURN_VAR} "${BUFFER}" PARENT_SCOPE)
  60. endfunction ()
  61. # Helper function to check compiler flags for language compiler.
  62. function (sanitizer_check_compiler_flag FLAG LANG VARIABLE)
  63. if (${LANG} STREQUAL "C")
  64. include(CheckCCompilerFlag)
  65. check_c_compiler_flag("${FLAG}" ${VARIABLE})
  66. elseif (${LANG} STREQUAL "CXX")
  67. include(CheckCXXCompilerFlag)
  68. check_cxx_compiler_flag("${FLAG}" ${VARIABLE})
  69. elseif (${LANG} STREQUAL "Fortran")
  70. # CheckFortranCompilerFlag was introduced in CMake 3.x. To be compatible
  71. # with older Cmake versions, we will check if this module is present
  72. # before we use it. Otherwise we will define Fortran coverage support as
  73. # not available.
  74. include(CheckFortranCompilerFlag OPTIONAL RESULT_VARIABLE INCLUDED)
  75. if (INCLUDED)
  76. check_fortran_compiler_flag("${FLAG}" ${VARIABLE})
  77. elseif (NOT CMAKE_REQUIRED_QUIET)
  78. message(STATUS "Performing Test ${VARIABLE}")
  79. message(STATUS "Performing Test ${VARIABLE}"
  80. " - Failed (Check not supported)")
  81. endif ()
  82. endif()
  83. endfunction ()
  84. # Helper function to test compiler flags.
  85. function (sanitizer_check_compiler_flags FLAG_CANDIDATES NAME PREFIX)
  86. set(CMAKE_REQUIRED_QUIET ${${PREFIX}_FIND_QUIETLY})
  87. get_property(ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
  88. foreach (LANG ${ENABLED_LANGUAGES})
  89. # Sanitizer flags are not dependend on language, but the used compiler.
  90. # So instead of searching flags foreach language, search flags foreach
  91. # compiler used.
  92. set(COMPILER ${CMAKE_${LANG}_COMPILER_ID})
  93. if (NOT DEFINED ${PREFIX}_${COMPILER}_FLAGS)
  94. foreach (FLAG ${FLAG_CANDIDATES})
  95. if(NOT CMAKE_REQUIRED_QUIET)
  96. message(STATUS "Try ${COMPILER} ${NAME} flag = [${FLAG}]")
  97. endif()
  98. set(CMAKE_REQUIRED_FLAGS "${FLAG}")
  99. unset(${PREFIX}_FLAG_DETECTED CACHE)
  100. sanitizer_check_compiler_flag("${FLAG}" ${LANG}
  101. ${PREFIX}_FLAG_DETECTED)
  102. if (${PREFIX}_FLAG_DETECTED)
  103. # If compiler is a GNU compiler, search for static flag, if
  104. # SANITIZE_LINK_STATIC is enabled.
  105. if (SANITIZE_LINK_STATIC AND (${COMPILER} STREQUAL "GNU"))
  106. string(TOLOWER ${PREFIX} PREFIX_lower)
  107. sanitizer_check_compiler_flag(
  108. "-static-lib${PREFIX_lower}" ${LANG}
  109. ${PREFIX}_STATIC_FLAG_DETECTED)
  110. if (${PREFIX}_STATIC_FLAG_DETECTED)
  111. set(FLAG "-static-lib${PREFIX_lower} ${FLAG}")
  112. endif ()
  113. endif ()
  114. set(${PREFIX}_${COMPILER}_FLAGS "${FLAG}" CACHE STRING
  115. "${NAME} flags for ${COMPILER} compiler.")
  116. mark_as_advanced(${PREFIX}_${COMPILER}_FLAGS)
  117. break()
  118. endif ()
  119. endforeach ()
  120. if (NOT ${PREFIX}_FLAG_DETECTED)
  121. set(${PREFIX}_${COMPILER}_FLAGS "" CACHE STRING
  122. "${NAME} flags for ${COMPILER} compiler.")
  123. mark_as_advanced(${PREFIX}_${COMPILER}_FLAGS)
  124. message(WARNING "${NAME} is not available for ${COMPILER} "
  125. "compiler. Targets using this compiler will be "
  126. "compiled without ${NAME}.")
  127. endif ()
  128. endif ()
  129. endforeach ()
  130. endfunction ()
  131. # Helper to assign sanitizer flags for TARGET.
  132. function (saitizer_add_flags TARGET NAME PREFIX)
  133. # Get list of compilers used by target and check, if sanitizer is available
  134. # for this target. Other compiler checks like check for conflicting
  135. # compilers will be done in add_sanitizers function.
  136. sanitizer_target_compilers(${TARGET} TARGET_COMPILER)
  137. list(LENGTH TARGET_COMPILER NUM_COMPILERS)
  138. if ("${${PREFIX}_${TARGET_COMPILER}_FLAGS}" STREQUAL "")
  139. return()
  140. endif()
  141. # Set compile- and link-flags for target.
  142. set_property(TARGET ${TARGET} APPEND_STRING
  143. PROPERTY COMPILE_FLAGS " ${${PREFIX}_${TARGET_COMPILER}_FLAGS}")
  144. set_property(TARGET ${TARGET} APPEND_STRING
  145. PROPERTY COMPILE_FLAGS " ${SanBlist_${TARGET_COMPILER}_FLAGS}")
  146. set_property(TARGET ${TARGET} APPEND_STRING
  147. PROPERTY LINK_FLAGS " ${${PREFIX}_${TARGET_COMPILER}_FLAGS}")
  148. endfunction ()