FindSanitizers.cmake 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. set(FIND_QUIETLY_FLAG "")
  25. if (DEFINED Sanitizers_FIND_QUIETLY)
  26. set(FIND_QUIETLY_FLAG "QUIET")
  27. endif ()
  28. find_package(ASan ${FIND_QUIETLY_FLAG})
  29. find_package(TSan ${FIND_QUIETLY_FLAG})
  30. find_package(MSan ${FIND_QUIETLY_FLAG})
  31. find_package(UBSan ${FIND_QUIETLY_FLAG})
  32. function(sanitizer_add_blacklist_file FILE)
  33. if(NOT IS_ABSOLUTE ${FILE})
  34. set(FILE "${CMAKE_CURRENT_SOURCE_DIR}/${FILE}")
  35. endif()
  36. get_filename_component(FILE "${FILE}" REALPATH)
  37. sanitizer_check_compiler_flags("-fsanitize-blacklist=${FILE}"
  38. "SanitizerBlacklist" "SanBlist")
  39. endfunction()
  40. function(add_sanitizers ...)
  41. # If no sanitizer is enabled, return immediately.
  42. if (NOT (SANITIZE_ADDRESS OR SANITIZE_MEMORY OR SANITIZE_THREAD OR
  43. SANITIZE_UNDEFINED))
  44. return()
  45. endif ()
  46. foreach (TARGET ${ARGV})
  47. # Check if this target will be compiled by exactly one compiler. Other-
  48. # wise sanitizers can't be used and a warning should be printed once.
  49. get_target_property(TARGET_TYPE ${TARGET} TYPE)
  50. if (TARGET_TYPE STREQUAL "INTERFACE_LIBRARY")
  51. message(WARNING "Can't use any sanitizers for target ${TARGET}, "
  52. "because it is an interface library and cannot be "
  53. "compiled directly.")
  54. return()
  55. endif ()
  56. sanitizer_target_compilers(${TARGET} TARGET_COMPILER)
  57. list(LENGTH TARGET_COMPILER NUM_COMPILERS)
  58. if (NUM_COMPILERS GREATER 1)
  59. message(WARNING "Can't use any sanitizers for target ${TARGET}, "
  60. "because it will be compiled by incompatible compilers. "
  61. "Target will be compiled without sanitizers.")
  62. return()
  63. # If the target is compiled by no or no known compiler, give a warning.
  64. elseif (NUM_COMPILERS EQUAL 0)
  65. message(WARNING "Sanitizers for target ${TARGET} may not be"
  66. " usable, because it uses no or an unknown compiler. "
  67. "This is a false warning for targets using only "
  68. "object lib(s) as input.")
  69. endif ()
  70. # Add sanitizers for target.
  71. add_sanitize_address(${TARGET})
  72. add_sanitize_thread(${TARGET})
  73. add_sanitize_memory(${TARGET})
  74. add_sanitize_undefined(${TARGET})
  75. endforeach ()
  76. endfunction(add_sanitizers)