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.
 
 

64 lines
2.7 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. include(CheckCCompilerFlag)
  25. # Set -Werror to catch "argument unused during compilation" warnings
  26. set(CMAKE_REQUIRED_FLAGS "-Werror -faddress-sanitizer") # Also needs to be a link flag for test to pass
  27. check_c_compiler_flag("-faddress-sanitizer" HAVE_FLAG_ADDRESS_SANITIZER)
  28. set(CMAKE_REQUIRED_FLAGS "-Werror -fsanitize=address") # Also needs to be a link flag for test to pass
  29. check_c_compiler_flag("-fsanitize=address" HAVE_FLAG_SANITIZE_ADDRESS)
  30. unset(CMAKE_REQUIRED_FLAGS)
  31. if(HAVE_FLAG_SANITIZE_ADDRESS)
  32. # Clang 3.2+ use this version
  33. set(ADDRESS_SANITIZER_FLAG "-fsanitize=address")
  34. elseif(HAVE_FLAG_ADDRESS_SANITIZER)
  35. # Older deprecated flag for ASan
  36. set(ADDRESS_SANITIZER_FLAG "-faddress-sanitizer")
  37. endif()
  38. if(NOT ADDRESS_SANITIZER_FLAG)
  39. return()
  40. endif()
  41. set(CMAKE_C_FLAGS_ASAN "-O1 -g ${ADDRESS_SANITIZER_FLAG} -fno-omit-frame-pointer -fno-optimize-sibling-calls"
  42. CACHE STRING "Flags used by the C compiler during ASan builds."
  43. FORCE)
  44. set(CMAKE_CXX_FLAGS_ASAN "-O1 -g ${ADDRESS_SANITIZER_FLAG} -fno-omit-frame-pointer -fno-optimize-sibling-calls"
  45. CACHE STRING "Flags used by the C++ compiler during ASan builds."
  46. FORCE)
  47. set(CMAKE_EXE_LINKER_FLAGS_ASAN "${ADDRESS_SANITIZER_FLAG}"
  48. CACHE STRING "Flags used for linking binaries during ASan builds."
  49. FORCE)
  50. set(CMAKE_SHARED_LINKER_FLAGS_ASAN "${ADDRESS_SANITIZER_FLAG}"
  51. CACHE STRING "Flags used by the shared libraries linker during ASan builds."
  52. FORCE)
  53. mark_as_advanced(CMAKE_C_FLAGS_ASAN
  54. CMAKE_CXX_FLAGS_ASAN
  55. CMAKE_EXE_LINKER_FLAGS_ASAN
  56. CMAKE_SHARED_LINKER_FLAGS_ASAN)