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.
 
 

76 line
3.1 KiB

  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 memory sanitizer is supported by the compiler,
  25. # and creates a MSan build type (i.e. set CMAKE_BUILD_TYPE=MSan to use
  26. # it). This sets the following variables:
  27. #
  28. # CMAKE_C_FLAGS_MSAN - Flags to use for C with msan
  29. # CMAKE_CXX_FLAGS_MSAN - Flags to use for C++ with msan
  30. # HAVE_MEMORY_SANITIZER - True or false if the MSan build type is available
  31. include(CheckCCompilerFlag)
  32. # Set -Werror to catch "argument unused during compilation" warnings
  33. set(CMAKE_REQUIRED_FLAGS "-Werror -fmemory-sanitizer") # Also needs to be a link flag for test to pass
  34. check_c_compiler_flag("-fmemory-sanitizer" HAVE_FLAG_MEMORY_SANITIZER)
  35. set(CMAKE_REQUIRED_FLAGS "-Werror -fsanitize=memory") # Also needs to be a link flag for test to pass
  36. check_c_compiler_flag("-fsanitize=memory" HAVE_FLAG_SANITIZE_MEMORY)
  37. unset(CMAKE_REQUIRED_FLAGS)
  38. if(HAVE_FLAG_SANITIZE_MEMORY)
  39. # Clang 3.2+ use this version
  40. set(MEMORY_SANITIZER_FLAG "-fsanitize=memory")
  41. elseif(HAVE_FLAG_MEMORY_SANITIZER)
  42. # Older deprecated flag for MSan
  43. set(MEMORY_SANITIZER_FLAG "-fmemory-sanitizer")
  44. endif()
  45. if(NOT MEMORY_SANITIZER_FLAG)
  46. return()
  47. else(NOT MEMORY_SANITIZER_FLAG)
  48. set(HAVE_MEMORY_SANITIZER TRUE)
  49. endif()
  50. set(HAVE_MEMORY_SANITIZER TRUE)
  51. set(CMAKE_C_FLAGS_MSAN "-O1 -g ${MEMORY_SANITIZER_FLAG} -fno-omit-frame-pointer -fno-optimize-sibling-calls"
  52. CACHE STRING "Flags used by the C compiler during MSan builds."
  53. FORCE)
  54. set(CMAKE_CXX_FLAGS_MSAN "-O1 -g ${MEMORY_SANITIZER_FLAG} -fno-omit-frame-pointer -fno-optimize-sibling-calls"
  55. CACHE STRING "Flags used by the C++ compiler during MSan builds."
  56. FORCE)
  57. set(CMAKE_EXE_LINKER_FLAGS_MSAN "${MEMORY_SANITIZER_FLAG}"
  58. CACHE STRING "Flags used for linking binaries during MSan builds."
  59. FORCE)
  60. set(CMAKE_SHARED_LINKER_FLAGS_MSAN "${MEMORY_SANITIZER_FLAG}"
  61. CACHE STRING "Flags used by the shared libraries linker during MSan builds."
  62. FORCE)
  63. mark_as_advanced(CMAKE_C_FLAGS_MSAN
  64. CMAKE_CXX_FLAGS_MSAN
  65. CMAKE_EXE_LINKER_FLAGS_MSAN
  66. CMAKE_SHARED_LINKER_FLAGS_MSAN)