sanitize-helpers.cmake 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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(LONGEST_EXT "${FILE}" EXT)
  27. # If extension is empty return. This can happen for extensionless headers
  28. if("${LONGEST_EXT}" STREQUAL "")
  29. set(${RETURN_VAR} "" PARENT_SCOPE)
  30. return()
  31. endif()
  32. # Get shortest extension as some files can have dot in their names
  33. string(REGEX REPLACE "^.*(\\.[^.]+)$" "\\1" FILE_EXT ${LONGEST_EXT})
  34. string(TOLOWER "${FILE_EXT}" FILE_EXT)
  35. string(SUBSTRING "${FILE_EXT}" 1 -1 FILE_EXT)
  36. get_property(ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
  37. foreach (LANG ${ENABLED_LANGUAGES})
  38. list(FIND CMAKE_${LANG}_SOURCE_FILE_EXTENSIONS "${FILE_EXT}" TEMP)
  39. if (NOT ${TEMP} EQUAL -1)
  40. set(${RETURN_VAR} "${LANG}" PARENT_SCOPE)
  41. return()
  42. endif ()
  43. endforeach()
  44. set(${RETURN_VAR} "" PARENT_SCOPE)
  45. endfunction ()
  46. # Helper function to get compilers used by a target.
  47. function (sanitizer_target_compilers TARGET RETURN_VAR)
  48. # Check if all sources for target use the same compiler. If a target uses
  49. # e.g. C and Fortran mixed and uses different compilers (e.g. clang and
  50. # gfortran) this can trigger huge problems, because different compilers may
  51. # use different implementations for sanitizers.
  52. set(BUFFER "")
  53. get_target_property(TSOURCES ${TARGET} SOURCES)
  54. foreach (FILE ${TSOURCES})
  55. # If expression was found, FILE is a generator-expression for an object
  56. # library. Object libraries will be ignored.
  57. string(REGEX MATCH "TARGET_OBJECTS:([^ >]+)" _file ${FILE})
  58. if ("${_file}" STREQUAL "")
  59. sanitizer_lang_of_source(${FILE} LANG)
  60. if (LANG)
  61. list(APPEND BUFFER ${CMAKE_${LANG}_COMPILER_ID})
  62. endif ()
  63. endif ()
  64. endforeach ()
  65. list(REMOVE_DUPLICATES BUFFER)
  66. set(${RETURN_VAR} "${BUFFER}" PARENT_SCOPE)
  67. endfunction ()
  68. # Helper function to check compiler flags for language compiler.
  69. function (sanitizer_check_compiler_flag FLAG LANG VARIABLE)
  70. if (${LANG} STREQUAL "C")
  71. include(CheckCCompilerFlag)
  72. check_c_compiler_flag("${FLAG}" ${VARIABLE})
  73. elseif (${LANG} STREQUAL "CXX")
  74. include(CheckCXXCompilerFlag)
  75. check_cxx_compiler_flag("${FLAG}" ${VARIABLE})
  76. elseif (${LANG} STREQUAL "Fortran")
  77. # CheckFortranCompilerFlag was introduced in CMake 3.x. To be compatible
  78. # with older Cmake versions, we will check if this module is present
  79. # before we use it. Otherwise we will define Fortran coverage support as
  80. # not available.
  81. include(CheckFortranCompilerFlag OPTIONAL RESULT_VARIABLE INCLUDED)
  82. if (INCLUDED)
  83. check_fortran_compiler_flag("${FLAG}" ${VARIABLE})
  84. elseif (NOT CMAKE_REQUIRED_QUIET)
  85. message(STATUS "Performing Test ${VARIABLE}")
  86. message(STATUS "Performing Test ${VARIABLE}"
  87. " - Failed (Check not supported)")
  88. endif ()
  89. endif()
  90. endfunction ()
  91. # Helper function to test compiler flags.
  92. function (sanitizer_check_compiler_flags FLAG_CANDIDATES NAME PREFIX)
  93. set(CMAKE_REQUIRED_QUIET ${${PREFIX}_FIND_QUIETLY})
  94. get_property(ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
  95. foreach (LANG ${ENABLED_LANGUAGES})
  96. # Sanitizer flags are not dependend on language, but the used compiler.
  97. # So instead of searching flags foreach language, search flags foreach
  98. # compiler used.
  99. set(COMPILER ${CMAKE_${LANG}_COMPILER_ID})
  100. if (NOT DEFINED ${PREFIX}_${COMPILER}_FLAGS)
  101. foreach (FLAG ${FLAG_CANDIDATES})
  102. if(NOT CMAKE_REQUIRED_QUIET)
  103. message(STATUS "Try ${COMPILER} ${NAME} flag = [${FLAG}]")
  104. endif()
  105. set(CMAKE_REQUIRED_FLAGS "${FLAG}")
  106. unset(${PREFIX}_FLAG_DETECTED CACHE)
  107. sanitizer_check_compiler_flag("${FLAG}" ${LANG}
  108. ${PREFIX}_FLAG_DETECTED)
  109. if (${PREFIX}_FLAG_DETECTED)
  110. set(${PREFIX}_${COMPILER}_FLAGS "${FLAG}" CACHE STRING
  111. "${NAME} flags for ${COMPILER} compiler.")
  112. mark_as_advanced(${PREFIX}_${COMPILER}_FLAGS)
  113. break()
  114. endif ()
  115. endforeach ()
  116. if (NOT ${PREFIX}_FLAG_DETECTED)
  117. set(${PREFIX}_${COMPILER}_FLAGS "" CACHE STRING
  118. "${NAME} flags for ${COMPILER} compiler.")
  119. mark_as_advanced(${PREFIX}_${COMPILER}_FLAGS)
  120. message(WARNING "${NAME} is not available for ${COMPILER} "
  121. "compiler. Targets using this compiler will be "
  122. "compiled without ${NAME}.")
  123. endif ()
  124. endif ()
  125. endforeach ()
  126. endfunction ()
  127. # Helper to assign sanitizer flags for TARGET.
  128. function (sanitizer_add_flags TARGET NAME PREFIX)
  129. # Get list of compilers used by target and check, if sanitizer is available
  130. # for this target. Other compiler checks like check for conflicting
  131. # compilers will be done in add_sanitizers function.
  132. sanitizer_target_compilers(${TARGET} TARGET_COMPILER)
  133. list(LENGTH TARGET_COMPILER NUM_COMPILERS)
  134. if ("${${PREFIX}_${TARGET_COMPILER}_FLAGS}" STREQUAL "")
  135. return()
  136. endif()
  137. # Set compile- and link-flags for target.
  138. set_property(TARGET ${TARGET} APPEND_STRING
  139. PROPERTY COMPILE_FLAGS " ${${PREFIX}_${TARGET_COMPILER}_FLAGS}")
  140. set_property(TARGET ${TARGET} APPEND_STRING
  141. PROPERTY COMPILE_FLAGS " ${SanBlist_${TARGET_COMPILER}_FLAGS}")
  142. set_property(TARGET ${TARGET} APPEND_STRING
  143. PROPERTY LINK_FLAGS " ${${PREFIX}_${TARGET_COMPILER}_FLAGS}")
  144. endfunction ()