CheckCXXAtomic.cmake 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #
  2. # SRT - Secure, Reliable, Transport
  3. # Copyright (c) 2021 Haivision Systems Inc.
  4. #
  5. # This Source Code Form is subject to the terms of the Mozilla Public
  6. # License, v. 2.0. If a copy of the MPL was not distributed with this
  7. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. #
  9. # Check for c++11 std::atomic.
  10. #
  11. # Sets:
  12. # HAVE_CXX_ATOMIC
  13. # HAVE_CXX_ATOMIC_STATIC
  14. include(CheckCXXSourceCompiles)
  15. include(CheckLibraryExists)
  16. function(CheckCXXAtomic)
  17. unset(HAVE_CXX_ATOMIC CACHE)
  18. unset(HAVE_CXX_ATOMIC_STATIC CACHE)
  19. unset(CMAKE_REQUIRED_FLAGS)
  20. unset(CMAKE_REQUIRED_LIBRARIES)
  21. unset(CMAKE_REQUIRED_LINK_OPTIONS)
  22. set(CheckCXXAtomic_CODE
  23. "
  24. #include<cstdint>
  25. #include<atomic>
  26. int main(void)
  27. {
  28. std::atomic<std::ptrdiff_t> x(0);
  29. std::atomic<std::intmax_t> y(0);
  30. return x + y;
  31. }
  32. ")
  33. set(CMAKE_REQUIRED_FLAGS "-std=c++11")
  34. check_cxx_source_compiles(
  35. "${CheckCXXAtomic_CODE}"
  36. HAVE_CXX_ATOMIC)
  37. if(HAVE_CXX_ATOMIC)
  38. # CMAKE_REQUIRED_LINK_OPTIONS was introduced in CMake 3.14.
  39. if(CMAKE_VERSION VERSION_LESS "3.14")
  40. set(CMAKE_REQUIRED_LINK_OPTIONS "-static")
  41. else()
  42. set(CMAKE_REQUIRED_FLAGS "-std=c++11 -static")
  43. endif()
  44. check_cxx_source_compiles(
  45. "${CheckCXXAtomic_CODE}"
  46. HAVE_CXX_ATOMIC_STATIC)
  47. endif()
  48. unset(CMAKE_REQUIRED_FLAGS)
  49. unset(CMAKE_REQUIRED_LIBRARIES)
  50. unset(CMAKE_REQUIRED_LINK_OPTIONS)
  51. endfunction(CheckCXXAtomic)