File: FindAtomic.cmake

package info (click to toggle)
libcifpp 9.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,528 kB
  • sloc: cpp: 33,979; sh: 105; makefile: 12
file content (63 lines) | stat: -rw-r--r-- 1,532 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Simple check to see if we need a library for std::atomic

if(TARGET std::atomic)
	return()
endif()

cmake_minimum_required(VERSION 3.10)

include(CMakePushCheckState)
include(CheckIncludeFileCXX)
include(CheckCXXSourceRuns)

cmake_push_check_state()

check_include_file_cxx("atomic" _CXX_ATOMIC_HAVE_HEADER)
mark_as_advanced(_CXX_ATOMIC_HAVE_HEADER)

set(code [[
#include <atomic>
int main(int argc, char** argv) {
  std::atomic<long long> s;
  ++s;
  return 0;
}
]])

check_cxx_source_runs("${code}" _CXX_ATOMIC_BUILTIN)

if(_CXX_ATOMIC_BUILTIN)
	set(_found 1)
else()
  list(APPEND CMAKE_REQUIRED_LIBRARIES atomic)
  list(APPEND FOLLY_LINK_LIBRARIES atomic)

  check_cxx_source_runs("${code}" _CXX_ATOMIC_LIB_NEEDED)
  if (NOT _CXX_ATOMIC_LIB_NEEDED)
    message(FATAL_ERROR "unable to link C++ std::atomic code: you may need \
      to install GNU libatomic")
  else()
	set(_found 1)
  endif()
endif()

if(_found)
	add_library(std::atomic INTERFACE IMPORTED)
	set_property(TARGET std::atomic APPEND PROPERTY INTERFACE_COMPILE_FEATURES cxx_std_14)

	if(_CXX_ATOMIC_BUILTIN)
		# Nothing to add...
	elseif(_CXX_ATOMIC_LIB_NEEDED)
		set_target_properties(std::atomic PROPERTIES IMPORTED_LIBNAME atomic)
		set(STDCPPATOMIC_LIBRARY atomic)
	endif()
endif()

cmake_pop_check_state()

set(Atomic_FOUND ${_found} CACHE BOOL "TRUE if we can run a program using std::atomic" FORCE)
mark_as_advanced(Atomic_FOUND)

if(Atomic_FIND_REQUIRED AND NOT Atomic_FOUND)
    message(FATAL_ERROR "Cannot run simple program using std::atomic")
endif()