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
10# Check for c++11 std::atomic.
11#
12# Sets:
13#     HAVE_CXX_ATOMIC
14#     HAVE_CXX_ATOMIC_STATIC
15
16include(CheckCXXSourceCompiles)
17include(CheckLibraryExists)
18
19function(CheckCXXAtomic)
20
21   unset(HAVE_CXX_ATOMIC CACHE)
22   unset(HAVE_CXX_ATOMIC_STATIC CACHE)
23
24   unset(CMAKE_REQUIRED_FLAGS)
25   unset(CMAKE_REQUIRED_LIBRARIES)
26   unset(CMAKE_REQUIRED_LINK_OPTIONS)
27
28   set(CheckCXXAtomic_CODE
29      "
30      #include<cstdint>
31      #include<atomic>
32      int main(void)
33      {
34         std::atomic<std::ptrdiff_t> x(0);
35         std::atomic<std::intmax_t> y(0);
36         return x + y;
37      }
38      ")
39
40   set(CMAKE_REQUIRED_FLAGS "-std=c++11")
41
42   check_cxx_source_compiles(
43      "${CheckCXXAtomic_CODE}"
44      HAVE_CXX_ATOMIC)
45
46   if(HAVE_CXX_ATOMIC)
47      # CMAKE_REQUIRED_LINK_OPTIONS was introduced in CMake 3.14.
48      if(CMAKE_VERSION VERSION_LESS "3.14")
49         set(CMAKE_REQUIRED_LINK_OPTIONS "-static")
50      else()
51         set(CMAKE_REQUIRED_FLAGS "-std=c++11 -static")
52      endif()
53      check_cxx_source_compiles(
54         "${CheckCXXAtomic_CODE}"
55         HAVE_CXX_ATOMIC_STATIC)
56   endif()
57
58   unset(CMAKE_REQUIRED_FLAGS)
59   unset(CMAKE_REQUIRED_LIBRARIES)
60   unset(CMAKE_REQUIRED_LINK_OPTIONS)
61
62endfunction(CheckCXXAtomic)
63