summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/librdkafka-2.1.0/mklove/modules/configure.atomics
blob: 31639a7e453c35257a1d27613f7e08f5178c80fc (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
#
# Checks for atomic ops:
#  compiler builtin (__sync_..) and portable libatomic's (__atomic_..)
# Will also provide abstraction by defining the prefix to use.
#
# Sets:
#  HAVE_ATOMICS
#  HAVE_ATOMICS_32
#  HAVE_ATOMICS_64
#  HAVE_ATOMICS_32_ATOMIC   __atomic interface
#  HAVE_ATOMICS_32_SYNC     __sync interface
#  HAVE_ATOMICS_64_ATOMIC   __atomic interface
#  HAVE_ATOMICS_64_SYNC     __sync interface
#  WITH_LIBATOMIC
#  LIBS
#
#  ATOMIC_OP(OP1,OP2,PTR,VAL)
#  ATOMIC_OP32(OP1,OP2,PTR,VAL)
#  ATOMIC_OP64(OP1,OP2,PTR,VAL)
#   where op* is 'add,sub,fetch'
#   e.g:  ATOMIC_OP32(add, fetch, &i, 10)
#         becomes __atomic_add_fetch(&i, 10, ..) or
#                 __sync_add_and_fetch(&i, 10)
#

function checks {


    # We prefer the newer __atomic stuff, but 64-bit atomics might
    # require linking with -latomic, so we need to perform these tests
    # in the proper order:
    #   __atomic 32
    #   __atomic 32 -latomic
    #   __sync 32
    #
    #   __atomic 64
    #   __atomic 64 -latomic
    #   __sync 64

    local _libs=
    local _a32="__atomic_ ## OP1 ## _ ## OP2(PTR, VAL, __ATOMIC_SEQ_CST)"
    local _a64="__atomic_ ## OP1 ## _ ## OP2(PTR, VAL, __ATOMIC_SEQ_CST)"

    # 32-bit:
    # Try fully builtin __atomic
    if ! mkl_compile_check __atomic_32 HAVE_ATOMICS_32 cont CC "" \
        "
#include <inttypes.h>
int32_t foo (int32_t i) {
  return __atomic_add_fetch(&i, 1, __ATOMIC_SEQ_CST);
}"
        then
        # Try __atomic with -latomic
        if mkl_compile_check --ldflags="-latomic" __atomic_32_lib HAVE_ATOMICS_32 \
            cont CC "" \
            "
#include <inttypes.h>
int32_t foo (int32_t i) {
  return __atomic_add_fetch(&i, 1, __ATOMIC_SEQ_CST);
}"
        then
            _libs="-latomic"
            mkl_allvar_set "__atomic_32_lib" "HAVE_ATOMICS_32_ATOMIC" "y"
        else
            # Try __sync interface
            if mkl_compile_check __sync_32 HAVE_ATOMICS_32 disable CC "" \
                "
#include <inttypes.h>
int32_t foo (int32_t i) {
  return __sync_add_and_fetch(&i, 1);
}"
                then
                _a32="__sync_ ## OP1 ## _and_ ## OP2(PTR, VAL)"
                mkl_allvar_set "__sync_32" "HAVE_ATOMICS_32_SYNC" "y"
            else
                _a32=""
            fi
        fi
    else
        mkl_allvar_set "__atomic_32" "HAVE_ATOMICS_32_ATOMIC" "y"
    fi


    if [[ ! -z $_a32 ]]; then
        mkl_define_set "atomic_32" "ATOMIC_OP32(OP1,OP2,PTR,VAL)" "code:$_a32"
    fi



    # 64-bit:
    # Try fully builtin __atomic
    if ! mkl_compile_check __atomic_64 HAVE_ATOMICS_64 cont CC "" \
        "
#include <inttypes.h>
int64_t foo (int64_t i) {
  return __atomic_add_fetch(&i, 1, __ATOMIC_SEQ_CST);
}"
        then
        # Try __atomic with -latomic
        if mkl_compile_check --ldflags="-latomic" __atomic_64_lib HAVE_ATOMICS_64 \
            cont CC "" \
            "
#include <inttypes.h>
int64_t foo (int64_t i) {
  return __atomic_add_fetch(&i, 1, __ATOMIC_SEQ_CST);
}"
        then
            _libs="-latomic"
            mkl_allvar_set "__atomic_64_lib" "HAVE_ATOMICS_64_ATOMIC" "y"
        else
            # Try __sync interface
            if mkl_compile_check __sync_64 HAVE_ATOMICS_64 disable CC "" \
                "
#include <inttypes.h>
int64_t foo (int64_t i) {
  return __sync_add_and_fetch(&i, 1);
}"
                then
                _a64="__sync_ ## OP1 ## _and_ ## OP2 (PTR, VAL)"
                mkl_allvar_set "__sync_64" "HAVE_ATOMICS_64_SYNC" "y"
            else
                _a64=""
            fi
        fi
    else
        mkl_allvar_set "__atomic_64" "HAVE_ATOMICS_64_ATOMIC" "y"
    fi


    if [[ ! -z $_a64 ]]; then
        mkl_define_set "atomic_64" "ATOMIC_OP64(OP1,OP2,PTR,VAL)" "code:$_a64"

        # Define generic ATOMIC() macro identical to 64-bit atomics"
        mkl_define_set "atomic_64" "ATOMIC_OP(OP1,OP2,PTR,VAL)" "code:$_a64"
    fi


    if [[ ! -z $_libs ]]; then
        mkl_mkvar_append LDFLAGS LDFLAGS "-Wl,--as-needed"
        mkl_mkvar_append LIBS LIBS "$_libs"
    fi

}