Changeset View
Changeset View
Standalone View
Standalone View
cmake/modules/CheckAtomic.cmake
- This file was added.
1 | # ============================================================================== | ||||
---|---|---|---|---|---|
2 | # LLVM Release License | ||||
3 | # ============================================================================== | ||||
4 | # University of Illinois/NCSA | ||||
5 | # Open Source License | ||||
6 | # | ||||
7 | # Copyright (c) 2003-2018 University of Illinois at Urbana-Champaign. | ||||
8 | # All rights reserved. | ||||
9 | # | ||||
10 | # Developed by: | ||||
11 | # | ||||
12 | # LLVM Team | ||||
13 | # | ||||
14 | # University of Illinois at Urbana-Champaign | ||||
15 | # | ||||
16 | # http://llvm.org | ||||
17 | # | ||||
18 | # Permission is hereby granted, free of charge, to any person obtaining a copy of | ||||
19 | # this software and associated documentation files (the "Software"), to deal with | ||||
20 | # the Software without restriction, including without limitation the rights to | ||||
21 | # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||||
22 | # of the Software, and to permit persons to whom the Software is furnished to do | ||||
23 | # so, subject to the following conditions: | ||||
24 | # | ||||
25 | # * Redistributions of source code must retain the above copyright notice, | ||||
26 | # this list of conditions and the following disclaimers. | ||||
27 | # | ||||
28 | # * Redistributions in binary form must reproduce the above copyright notice, | ||||
29 | # this list of conditions and the following disclaimers in the | ||||
30 | # documentation and/or other materials provided with the distribution. | ||||
31 | # | ||||
32 | # * Neither the names of the LLVM Team, University of Illinois at | ||||
33 | # Urbana-Champaign, nor the names of its contributors may be used to | ||||
34 | # endorse or promote products derived from this Software without specific | ||||
35 | # prior written permission. | ||||
36 | # | ||||
37 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
38 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||||
39 | # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
40 | # CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
41 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
42 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE | ||||
43 | # SOFTWARE. | ||||
44 | | ||||
45 | INCLUDE(CheckCXXSourceCompiles) | ||||
46 | INCLUDE(CheckLibraryExists) | ||||
47 | | ||||
48 | # Sometimes linking against libatomic is required for atomic ops, if | ||||
49 | # the platform doesn't support lock-free atomics. | ||||
50 | | ||||
51 | function(check_working_cxx_atomics varname) | ||||
52 | set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) | ||||
53 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11") | ||||
54 | CHECK_CXX_SOURCE_COMPILES(" | ||||
55 | #include <atomic> | ||||
56 | std::atomic<int> x; | ||||
57 | int main() { | ||||
58 | return std::atomic_is_lock_free(&x); | ||||
59 | } | ||||
60 | " ${varname}) | ||||
61 | set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) | ||||
62 | endfunction(check_working_cxx_atomics) | ||||
63 | | ||||
64 | function(check_working_cxx_atomics64 varname) | ||||
65 | set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) | ||||
66 | set(CMAKE_REQUIRED_FLAGS "-std=c++11 ${CMAKE_REQUIRED_FLAGS}") | ||||
67 | CHECK_CXX_SOURCE_COMPILES(" | ||||
68 | #include <atomic> | ||||
69 | #include <cstdint> | ||||
70 | std::atomic<uint64_t> x (0); | ||||
71 | int main() { | ||||
72 | uint64_t i = x.load(std::memory_order_relaxed); | ||||
73 | return std::atomic_is_lock_free(&x); | ||||
74 | } | ||||
75 | " ${varname}) | ||||
76 | set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) | ||||
77 | endfunction(check_working_cxx_atomics64) | ||||
78 | | ||||
79 | | ||||
80 | # This isn't necessary on MSVC, so avoid command-line switch annoyance | ||||
81 | # by only running on GCC-like hosts. | ||||
82 | if (LLVM_COMPILER_IS_GCC_COMPATIBLE) | ||||
83 | # First check if atomics work without the library. | ||||
84 | check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITHOUT_LIB) | ||||
85 | # If not, check if the library exists, and atomics work with it. | ||||
86 | if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB) | ||||
87 | check_library_exists(atomic __atomic_fetch_add_4 "" HAVE_LIBATOMIC) | ||||
88 | if( HAVE_LIBATOMIC ) | ||||
89 | list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic") | ||||
90 | check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITH_LIB) | ||||
91 | if (NOT HAVE_CXX_ATOMICS_WITH_LIB) | ||||
92 | message(FATAL_ERROR "Host compiler must support std::atomic!") | ||||
93 | endif() | ||||
94 | else() | ||||
95 | message(FATAL_ERROR "Host compiler appears to require libatomic, but cannot find it.") | ||||
96 | endif() | ||||
97 | endif() | ||||
98 | endif() | ||||
99 | # Check for 64 bit atomic operations. | ||||
100 | if(MSVC) | ||||
101 | set(HAVE_CXX_ATOMICS64_WITHOUT_LIB True) | ||||
102 | else() | ||||
103 | check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITHOUT_LIB) | ||||
104 | endif() | ||||
105 | | ||||
106 | # If not, check if the library exists, and atomics work with it. | ||||
107 | if(NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB) | ||||
108 | check_library_exists(atomic __atomic_load_8 "" HAVE_CXX_LIBATOMICS64) | ||||
109 | if(HAVE_CXX_LIBATOMICS64) | ||||
110 | list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic") | ||||
111 | check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITH_LIB) | ||||
112 | if (NOT HAVE_CXX_ATOMICS64_WITH_LIB) | ||||
113 | message(FATAL_ERROR "Host compiler must support std::atomic!") | ||||
114 | endif() | ||||
115 | else() | ||||
116 | message(FATAL_ERROR "Host compiler appears to require libatomic, but cannot find it.") | ||||
117 | endif() | ||||
118 | endif() |