FindTSan.cmake 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 thread sanitizer is supported by the compiler,
  25. # and creates a TSan build type (i.e. set CMAKE_BUILD_TYPE=TSan to use
  26. # it). This sets the following variables:
  27. #
  28. # CMAKE_C_FLAGS_TSAN - Flags to use for C with tsan
  29. # CMAKE_CXX_FLAGS_TSAN - Flags to use for C++ with tsan
  30. # HAVE_THREAD_SANITIZER - True or false if the TSan build type is available
  31. include(CheckCXXCompilerFlag)
  32. # Set -Werror to catch "argument unused during compilation" warnings
  33. set(CMAKE_REQUIRED_FLAGS "-Werror -fsanitize=thread") # Also needs to be a link flag for test to pass
  34. check_cxx_compiler_flag("-fsanitize=thread" HAVE_FLAG_SANITIZE_THREAD)
  35. unset(CMAKE_REQUIRED_FLAGS)
  36. # A special test that uses threads seems to not be necessary. tsan
  37. # symbols are used even in just int main() { return 0; }
  38. if(HAVE_FLAG_SANITIZE_THREAD)
  39. # Clang 3.2+ use this version
  40. set(THREAD_SANITIZER_FLAG "-fsanitize=thread")
  41. else()
  42. set(HAVE_THREAD_SANITIZER FALSE)
  43. return()
  44. endif()
  45. set(HAVE_THREAD_SANITIZER TRUE)
  46. set(CMAKE_C_FLAGS_TSAN "-O1 -g ${THREAD_SANITIZER_FLAG} -fno-omit-frame-pointer"
  47. CACHE STRING "Flags used by the C compiler during TSan builds."
  48. FORCE
  49. )
  50. set(CMAKE_CXX_FLAGS_TSAN "-O1 -g ${THREAD_SANITIZER_FLAG} -fno-omit-frame-pointer"
  51. CACHE STRING "Flags used by the C++ compiler during TSan builds."
  52. FORCE)
  53. set(CMAKE_EXE_LINKER_FLAGS_TSAN "${THREAD_SANITIZER_FLAG}"
  54. CACHE STRING "Flags used for linking binaries during TSan builds."
  55. FORCE)
  56. set(CMAKE_SHARED_LINKER_FLAGS_TSAN "${THREAD_SANITIZER_FLAG}"
  57. CACHE STRING "Flags used by the shared libraries linker during TSan builds."
  58. FORCE)
  59. mark_as_advanced(CMAKE_C_FLAGS_TSAN
  60. CMAKE_CXX_FLAGS_TSAN
  61. CMAKE_EXE_LINKER_FLAGS_TSAN
  62. CMAKE_SHARED_LINKER_FLAGS_TSAN)