File: FindAtomic.cmake

package info (click to toggle)
fastdds 3.1.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 58,132 kB
  • sloc: cpp: 779,516; xml: 15,119; python: 4,356; sh: 190; makefile: 93; ansic: 12
file content (77 lines) | stat: -rw-r--r-- 1,979 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Check if platform needs to explicitly specify linking with libatomic.
# It creates an interface target eProsima_atomic that will include the dependency if needed.
# The variable FASTDDS_REQUIRED_FLAGS can be used to pass specific compiler flags use in the build
# and that we want to mimick in the testing like --std=c++20

if(TARGET eProsima_atomic)
    return()
endif()

set(Atomic_FOUND FALSE CACHE BOOL "The atomic module testing has already been performed")

set(ATOMIC_TEST_CODE
    "#define _ENABLE_ATOMIC_ALIGNMENT_FIX

    #include <atomic>
    #include <cstdint>

    std::atomic<bool> b;

    int main()
    {
        using namespace std;

	bool b2 = true;

        volatile atomic_ullong i(0xcafebabedeadbeeful);
        i += 0xfeedbadc0de8f00dul;

        struct Pointer
        {
            uint32_t write_p;
            uint32_t free_cells;
        };

        Pointer dummy{1,1};
        atomic<Pointer> pointer;
        pointer = dummy;

        return !b.compare_exchange_strong(b2, false);
    }"
)

include(CheckCXXSourceCompiles)

# Test linking without atomic
unset(ATOMIC_WITHOUT_LIB)
check_cxx_source_compiles(
    "${ATOMIC_TEST_CODE}"
    ATOMIC_WITHOUT_LIB
)

if(NOT ATOMIC_WITHOUT_LIB) 
    unset(ATOMIC_WITH_LIB)
    set(CMAKE_REQUIRED_LIBRARIES -latomic)
    # Test linking with atomic
    check_cxx_source_compiles(
        "${ATOMIC_TEST_CODE}"
        ATOMIC_WITH_LIB
    )
endif()

# add interface target associated (cannot be imported to avoid propagation on static libraries)
add_library(eProsima_atomic INTERFACE)

# Populate the interface target properties
if (ATOMIC_WITH_LIB)
    # force to link to atomic when the dummy target is present
    target_link_libraries(eProsima_atomic INTERFACE atomic)
elseif(NOT ATOMIC_WITHOUT_LIB)
    message(FATAL_ERROR "Unable to create binaries with atomic dependencies")
endif()

# clean local variables
unset(ATOMIC_TEST_CODE)
unset(ATOMIC_WITH_LIB)
unset(ATOMIC_WITHOUT_LIB)
unset(HAVE_LIBATOMIC)