🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
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.

177 lines
7.5 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(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. # If compiler is a GNU compiler, search for static flag, if
  111. # SANITIZE_LINK_STATIC is enabled.
  112. if (SANITIZE_LINK_STATIC AND (${COMPILER} STREQUAL "GNU"))
  113. string(TOLOWER ${PREFIX} PREFIX_lower)
  114. sanitizer_check_compiler_flag(
  115. "-static-lib${PREFIX_lower}" ${LANG}
  116. ${PREFIX}_STATIC_FLAG_DETECTED)
  117. if (${PREFIX}_STATIC_FLAG_DETECTED)
  118. set(FLAG "-static-lib${PREFIX_lower} ${FLAG}")
  119. endif ()
  120. endif ()
  121. set(${PREFIX}_${COMPILER}_FLAGS "${FLAG}" CACHE STRING
  122. "${NAME} flags for ${COMPILER} compiler.")
  123. mark_as_advanced(${PREFIX}_${COMPILER}_FLAGS)
  124. break()
  125. endif ()
  126. endforeach ()
  127. if (NOT ${PREFIX}_FLAG_DETECTED)
  128. set(${PREFIX}_${COMPILER}_FLAGS "" CACHE STRING
  129. "${NAME} flags for ${COMPILER} compiler.")
  130. mark_as_advanced(${PREFIX}_${COMPILER}_FLAGS)
  131. message(WARNING "${NAME} is not available for ${COMPILER} "
  132. "compiler. Targets using this compiler will be "
  133. "compiled without ${NAME}.")
  134. endif ()
  135. endif ()
  136. endforeach ()
  137. endfunction ()
  138. # Helper to assign sanitizer flags for TARGET.
  139. function (sanitizer_add_flags TARGET NAME PREFIX)
  140. # Get list of compilers used by target and check, if sanitizer is available
  141. # for this target. Other compiler checks like check for conflicting
  142. # compilers will be done in add_sanitizers function.
  143. sanitizer_target_compilers(${TARGET} TARGET_COMPILER)
  144. list(LENGTH TARGET_COMPILER NUM_COMPILERS)
  145. if ("${${PREFIX}_${TARGET_COMPILER}_FLAGS}" STREQUAL "")
  146. return()
  147. endif()
  148. # Set compile- and link-flags for target.
  149. set_property(TARGET ${TARGET} APPEND_STRING
  150. PROPERTY COMPILE_FLAGS " ${${PREFIX}_${TARGET_COMPILER}_FLAGS}")
  151. set_property(TARGET ${TARGET} APPEND_STRING
  152. PROPERTY COMPILE_FLAGS " ${SanBlist_${TARGET_COMPILER}_FLAGS}")
  153. set_property(TARGET ${TARGET} APPEND_STRING
  154. PROPERTY LINK_FLAGS " ${${PREFIX}_${TARGET_COMPILER}_FLAGS}")
  155. endfunction ()