FindASan.cmake 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #
  2. # The MIT License (MIT)
  3. #
  4. # Copyright (c) 2013 Matthew Arsenault
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. # THE SOFTWARE.
  23. #
  24. # This module tests if address sanitizer is supported by the compiler,
  25. # and creates a ASan build type (i.e. set CMAKE_BUILD_TYPE=ASan to use
  26. # it). This sets the following variables:
  27. #
  28. # CMAKE_C_FLAGS_ASAN - Flags to use for C with asan
  29. # CMAKE_CXX_FLAGS_ASAN - Flags to use for C++ with asan
  30. # HAVE_ADDRESS_SANITIZER - True or false if the ASan build type is available
  31. include(CheckCXXCompilerFlag)
  32. # Set -Werror to catch "argument unused during compilation" warnings
  33. set(CMAKE_REQUIRED_FLAGS "-Werror -fsanitize=address") # Also needs to be a link flag for test to pass
  34. check_cxx_compiler_flag("-fsanitize=address" HAVE_FLAG_SANITIZE_ADDRESS)
  35. unset(CMAKE_REQUIRED_FLAGS)
  36. if(HAVE_FLAG_SANITIZE_ADDRESS)
  37. # Clang 3.2+ use this version
  38. set(ADDRESS_SANITIZER_FLAG "-fsanitize=address")
  39. endif()
  40. if(NOT ADDRESS_SANITIZER_FLAG)
  41. return()
  42. else(NOT ADDRESS_SANITIZER_FLAG)
  43. set(HAVE_ADDRESS_SANITIZER FALSE)
  44. endif()
  45. set(HAVE_ADDRESS_SANITIZER TRUE)
  46. set(CMAKE_C_FLAGS_ASAN "-O0 -g ${ADDRESS_SANITIZER_FLAG} -fno-omit-frame-pointer -fno-optimize-sibling-calls"
  47. CACHE STRING "Flags used by the C compiler during ASan builds."
  48. FORCE)
  49. set(CMAKE_CXX_FLAGS_ASAN "-O0 -g ${ADDRESS_SANITIZER_FLAG} -fno-omit-frame-pointer -fno-optimize-sibling-calls"
  50. CACHE STRING "Flags used by the C++ compiler during ASan builds."
  51. FORCE)
  52. set(CMAKE_EXE_LINKER_FLAGS_ASAN "${ADDRESS_SANITIZER_FLAG}"
  53. CACHE STRING "Flags used for linking binaries during ASan builds."
  54. FORCE)
  55. set(CMAKE_SHARED_LINKER_FLAGS_ASAN "${ADDRESS_SANITIZER_FLAG}"
  56. CACHE STRING "Flags used by the shared libraries linker during ASan builds."
  57. FORCE)
  58. mark_as_advanced(CMAKE_C_FLAGS_ASAN
  59. CMAKE_CXX_FLAGS_ASAN
  60. CMAKE_EXE_LINKER_FLAGS_ASAN
  61. CMAKE_SHARED_LINKER_FLAGS_ASAN)
  62. check_cxx_compiler_flag("-Og" HAVE_OPTIMIZE_DEBUG)
  63. if(HAVE_OPTIMIZE_DEBUG)
  64. set(CMAKE_C_FLAGS_ASANOPT "-Og -g ${ADDRESS_SANITIZER_FLAG} -fno-omit-frame-pointer -fno-optimize-sibling-calls"
  65. CACHE STRING "Flags used by the C compiler during ASan Optimized builds."
  66. FORCE)
  67. set(CMAKE_CXX_FLAGS_ASANOPT "-Og -g ${ADDRESS_SANITIZER_FLAG} -fno-omit-frame-pointer -fno-optimize-sibling-calls"
  68. CACHE STRING "Flags used by the C++ compiler during ASan Optimized builds."
  69. FORCE)
  70. set(CMAKE_EXE_LINKER_FLAGS_ASANOPT "${ADDRESS_SANITIZER_FLAG}"
  71. CACHE STRING "Flags used for linking binaries during ASan Optimized builds."
  72. FORCE)
  73. set(CMAKE_SHARED_LINKER_FLAGS_ASANOPT "${ADDRESS_SANITIZER_FLAG}"
  74. CACHE STRING "Flags used by the shared libraries linker during ASan Optimized builds."
  75. FORCE)
  76. mark_as_advanced(CMAKE_C_FLAGS_ASANOPT
  77. CMAKE_CXX_FLAGS_ASANOPT
  78. CMAKE_EXE_LINKER_FLAGS_ASANOPT
  79. CMAKE_SHARED_LINKER_FLAGS_ASANOPT)
  80. endif()