1dnl Checks for libcthreads required headers and functions
2dnl
3dnl Version: 20190308
4
5dnl Function to detect if libcthreads is available
6dnl ac_libcthreads_dummy is used to prevent AC_CHECK_LIB adding unnecessary -l<library> arguments
7AC_DEFUN([AX_LIBCTHREADS_CHECK_LIB],
8  [AS_IF(
9    [test "x$ac_cv_enable_shared_libs" = xno || test "x$ac_cv_with_libcthreads" = xno],
10    [ac_cv_libcthreads=no],
11    [ac_cv_libcthreads=check
12    dnl Check if the directory provided as parameter exists
13    AS_IF(
14      [test "x$ac_cv_with_libcthreads" != x && test "x$ac_cv_with_libcthreads" != xauto-detect],
15      [AS_IF(
16        [test -d "$ac_cv_with_libcthreads"],
17        [CFLAGS="$CFLAGS -I${ac_cv_with_libcthreads}/include"
18        LDFLAGS="$LDFLAGS -L${ac_cv_with_libcthreads}/lib"],
19        [AC_MSG_FAILURE(
20          [no such directory: $ac_cv_with_libcthreads],
21          [1])
22        ])
23      ],
24      [dnl Check for a pkg-config file
25      AS_IF(
26        [test "x$cross_compiling" != "xyes" && test "x$PKGCONFIG" != "x"],
27        [PKG_CHECK_MODULES(
28          [libcthreads],
29          [libcthreads >= 20160404],
30          [ac_cv_libcthreads=yes],
31          [ac_cv_libcthreads=check])
32        ])
33      AS_IF(
34        [test "x$ac_cv_libcthreads" = xyes],
35        [ac_cv_libcthreads_CPPFLAGS="$pkg_cv_libcthreads_CFLAGS"
36        ac_cv_libcthreads_LIBADD="$pkg_cv_libcthreads_LIBS"])
37      ])
38
39    AS_IF(
40      [test "x$ac_cv_libcthreads" = xcheck],
41      [dnl Check for headers
42      AC_CHECK_HEADERS([libcthreads.h])
43
44      AS_IF(
45        [test "x$ac_cv_header_libcthreads_h" = xno],
46        [ac_cv_libcthreads=no],
47        [dnl Check for the individual functions
48        ac_cv_libcthreads=yes
49
50        AC_CHECK_LIB(
51          cthreads,
52          libcthreads_get_version,
53          [ac_cv_libcthreads_dummy=yes],
54          [ac_cv_libcthreads=no])
55
56        dnl Thread functions
57        AC_CHECK_LIB(
58          cthreads,
59          libcthreads_thread_create,
60          [ac_cv_libcthreads_dummy=yes],
61          [ac_cv_libcthreads=no])
62        AC_CHECK_LIB(
63          cthreads,
64          libcthreads_thread_join,
65          [ac_cv_libcthreads_dummy=yes],
66          [ac_cv_libcthreads=no])
67
68        dnl Thread attributes functions
69        AC_CHECK_LIB(
70          cthreads,
71          libcthreads_thread_attributes_initialize,
72          [ac_cv_libcthreads_dummy=yes],
73          [ac_cv_libcthreads=no])
74        AC_CHECK_LIB(
75          cthreads,
76          libcthreads_thread_attributes_free,
77          [ac_cv_libcthreads_dummy=yes],
78          [ac_cv_libcthreads=no])
79
80        dnl Condition functions
81        AC_CHECK_LIB(
82          cthreads,
83          libcthreads_condition_initialize,
84          [ac_cv_libcthreads_dummy=yes],
85          [ac_cv_libcthreads=no])
86        AC_CHECK_LIB(
87          cthreads,
88          libcthreads_condition_free,
89          [ac_cv_libcthreads_dummy=yes],
90          [ac_cv_libcthreads=no])
91        AC_CHECK_LIB(
92          cthreads,
93          libcthreads_condition_broadcast,
94          [ac_cv_libcthreads_dummy=yes],
95          [ac_cv_libcthreads=no])
96        AC_CHECK_LIB(
97          cthreads,
98          libcthreads_condition_signal,
99          [ac_cv_libcthreads_dummy=yes],
100          [ac_cv_libcthreads=no])
101        AC_CHECK_LIB(
102          cthreads,
103          libcthreads_condition_wait,
104          [ac_cv_libcthreads_dummy=yes],
105          [ac_cv_libcthreads=no])
106
107        dnl Lock functions
108        AC_CHECK_LIB(
109          cthreads,
110          libcthreads_lock_initialize,
111          [ac_cv_libcthreads_dummy=yes],
112          [ac_cv_libcthreads=no])
113        AC_CHECK_LIB(
114          cthreads,
115          libcthreads_lock_free,
116          [ac_cv_libcthreads_dummy=yes],
117          [ac_cv_libcthreads=no])
118        AC_CHECK_LIB(
119          cthreads,
120          libcthreads_lock_grab,
121          [ac_cv_libcthreads_dummy=yes],
122          [ac_cv_libcthreads=no])
123        AC_CHECK_LIB(
124          cthreads,
125          libcthreads_lock_release,
126          [ac_cv_libcthreads_dummy=yes],
127          [ac_cv_libcthreads=no])
128
129        dnl Mutex functions
130        AC_CHECK_LIB(
131          cthreads,
132          libcthreads_mutex_initialize,
133          [ac_cv_libcthreads_dummy=yes],
134          [ac_cv_libcthreads=no])
135        AC_CHECK_LIB(
136          cthreads,
137          libcthreads_mutex_free,
138          [ac_cv_libcthreads_dummy=yes],
139          [ac_cv_libcthreads=no])
140        AC_CHECK_LIB(
141          cthreads,
142          libcthreads_mutex_grab,
143          [ac_cv_libcthreads_dummy=yes],
144          [ac_cv_libcthreads=no])
145        AC_CHECK_LIB(
146          cthreads,
147          libcthreads_mutex_try_grab,
148          [ac_cv_libcthreads_dummy=yes],
149          [ac_cv_libcthreads=no])
150        AC_CHECK_LIB(
151          cthreads,
152          libcthreads_mutex_release,
153          [ac_cv_libcthreads_dummy=yes],
154          [ac_cv_libcthreads=no])
155
156        dnl Read/Write lock functions
157        AC_CHECK_LIB(
158          cthreads,
159          libcthreads_read_write_lock_initialize,
160          [ac_cv_libcthreads_dummy=yes],
161          [ac_cv_libcthreads=no])
162        AC_CHECK_LIB(
163          cthreads,
164          libcthreads_read_write_lock_free,
165          [ac_cv_libcthreads_dummy=yes],
166          [ac_cv_libcthreads=no])
167        AC_CHECK_LIB(
168          cthreads,
169          libcthreads_read_write_lock_grab_for_read,
170          [ac_cv_libcthreads_dummy=yes],
171          [ac_cv_libcthreads=no])
172        AC_CHECK_LIB(
173          cthreads,
174          libcthreads_read_write_lock_grab_for_write,
175          [ac_cv_libcthreads_dummy=yes],
176          [ac_cv_libcthreads=no])
177        AC_CHECK_LIB(
178          cthreads,
179          libcthreads_read_write_lock_release_for_read,
180          [ac_cv_libcthreads_dummy=yes],
181          [ac_cv_libcthreads=no])
182        AC_CHECK_LIB(
183          cthreads,
184          libcthreads_read_write_lock_release_for_write,
185          [ac_cv_libcthreads_dummy=yes],
186          [ac_cv_libcthreads=no])
187
188        dnl Queue functions
189        AC_CHECK_LIB(
190          cthreads,
191          libcthreads_queue_initialize,
192          [ac_cv_libcthreads_dummy=yes],
193          [ac_cv_libcthreads=no])
194        AC_CHECK_LIB(
195          cthreads,
196          libcthreads_queue_free,
197          [ac_cv_libcthreads_dummy=yes],
198          [ac_cv_libcthreads=no])
199        AC_CHECK_LIB(
200          cthreads,
201          libcthreads_queue_type_pop,
202          [ac_cv_libcthreads_dummy=yes],
203          [ac_cv_libcthreads=no])
204        AC_CHECK_LIB(
205          cthreads,
206          libcthreads_queue_pop,
207          [ac_cv_libcthreads_dummy=yes],
208          [ac_cv_libcthreads=no])
209        AC_CHECK_LIB(
210          cthreads,
211          libcthreads_queue_try_push,
212          [ac_cv_libcthreads_dummy=yes],
213          [ac_cv_libcthreads=no])
214        AC_CHECK_LIB(
215          cthreads,
216          libcthreads_queue_push,
217          [ac_cv_libcthreads_dummy=yes],
218          [ac_cv_libcthreads=no])
219        AC_CHECK_LIB(
220          cthreads,
221          libcthreads_queue_push_sorted,
222          [ac_cv_libcthreads_dummy=yes],
223          [ac_cv_libcthreads=no])
224
225        dnl Thread pool functions
226        AC_CHECK_LIB(
227          cthreads,
228          libcthreads_thread_pool_create,
229          [ac_cv_libcthreads_dummy=yes],
230          [ac_cv_libcthreads=no])
231        AC_CHECK_LIB(
232          cthreads,
233          libcthreads_thread_pool_push,
234          [ac_cv_libcthreads_dummy=yes],
235          [ac_cv_libcthreads=no])
236        AC_CHECK_LIB(
237          cthreads,
238          libcthreads_thread_pool_push_sorted,
239          [ac_cv_libcthreads_dummy=yes],
240          [ac_cv_libcthreads=no])
241        AC_CHECK_LIB(
242          cthreads,
243          libcthreads_thread_pool_join,
244          [ac_cv_libcthreads_dummy=yes],
245          [ac_cv_libcthreads=no])
246
247        ac_cv_libcthreads_LIBADD="-lcthreads"])
248      ])
249
250    AS_IF(
251      [test "x$ac_cv_with_libcthreads" != x && test "x$ac_cv_with_libcthreads" != xauto-detect && test "x$ac_cv_libcthreads" != xyes],
252      [AC_MSG_FAILURE(
253        [unable to find supported libcthreads in directory: $ac_cv_with_libcthreads],
254        [1])
255      ])
256    ])
257
258  AS_IF(
259    [test "x$ac_cv_libcthreads" = xyes],
260    [AC_DEFINE(
261      [HAVE_LIBCTHREADS],
262      [1],
263      [Define to 1 if you have the `cthreads' library (-lcthreads).])
264    ])
265
266  AS_IF(
267    [test "x$ac_cv_libcthreads" = xyes],
268    [ac_cv_libcthreads_multi_threading=libcthreads],
269    [ac_cv_libcthreads_multi_threading=no])
270
271  AS_IF(
272    [test "x$ac_cv_libcthreads" = xyes],
273    [AC_SUBST(
274      [HAVE_LIBCTHREADS],
275      [1]) ],
276    [AC_SUBST(
277      [HAVE_LIBCTHREADS],
278      [0])
279    ])
280  ])
281
282dnl Function to detect if libcthreads dependencies are available
283AC_DEFUN([AX_LIBCTHREADS_CHECK_LOCAL],
284  [AS_IF(
285    [test "x$ac_cv_enable_winapi" = xno],
286    [dnl Check for enabling pthread support
287    AX_PTHREAD_CHECK_ENABLE
288      ac_cv_libcthreads_multi_threading=$ac_cv_pthread],
289    [ac_cv_libcthreads_multi_threading="winapi"])
290
291  AS_IF(
292    [test "x$ac_cv_libcthreads_multi_threading" != xno],
293    [ac_cv_libcthreads_CPPFLAGS="-I../libcthreads";
294    ac_cv_libcthreads_LIBADD="../libcthreads/libcthreads.la";
295
296    ac_cv_libcthreads=local],
297    [ac_cv_libcthreads=no])
298  ])
299
300dnl Function to detect how to enable libcthreads
301AC_DEFUN([AX_LIBCTHREADS_CHECK_ENABLE],
302  [AX_COMMON_ARG_ENABLE(
303    [multi-threading-support],
304    [multi_threading_support],
305    [enable multi-threading support],
306    [yes])
307  AX_COMMON_ARG_WITH(
308    [libcthreads],
309    [libcthreads],
310    [search for libcthreads in includedir and libdir or in the specified DIR, or no if to use local version],
311    [auto-detect],
312    [DIR])
313
314  AS_IF(
315    [test "x$ac_cv_enable_multi_threading_support" = xno],
316    [ac_cv_libcthreads="no"
317    ac_cv_libcthreads_multi_threading="no"],
318    [dnl Check for a shared library version
319    AX_LIBCTHREADS_CHECK_LIB
320
321    dnl Check if the dependencies for the local library version
322    AS_IF(
323      [test "x$ac_cv_libcthreads" != xyes],
324      [AX_LIBCTHREADS_CHECK_LOCAL
325
326      AC_DEFINE(
327        [HAVE_LOCAL_LIBCTHREADS],
328        [1],
329        [Define to 1 if the local version of libcthreads is used.])
330      AC_SUBST(
331        [HAVE_LOCAL_LIBCTHREADS],
332        [1])
333      ])
334    ])
335
336  AM_CONDITIONAL(
337    [HAVE_LOCAL_LIBCTHREADS],
338    [test "x$ac_cv_libcthreads" = xlocal])
339  AS_IF(
340    [test "x$ac_cv_libcthreads_CPPFLAGS" != "x"],
341    [AC_SUBST(
342      [LIBCTHREADS_CPPFLAGS],
343      [$ac_cv_libcthreads_CPPFLAGS])
344    ])
345  AS_IF(
346    [test "x$ac_cv_libcthreads_LIBADD" != "x"],
347    [AC_SUBST(
348      [LIBCTHREADS_LIBADD],
349      [$ac_cv_libcthreads_LIBADD])
350    ])
351
352  AS_IF(
353    [test "x$ac_cv_libcthreads" != xno],
354    [AC_DEFINE(
355      [HAVE_MULTI_THREAD_SUPPORT],
356      [1],
357      [Define to 1 if multi thread support should be used.])
358    AC_SUBST(
359      [HAVE_MULTI_THREAD_SUPPORT],
360      [1]) ],
361    [AC_SUBST(
362      [HAVE_MULTI_THREAD_SUPPORT],
363      [0])
364    ])
365
366  AS_IF(
367    [test "x$ac_cv_libcthreads" = xyes],
368    [AC_SUBST(
369      [ax_libcthreads_pc_libs_private],
370      [-lcthreads])
371    ])
372
373  AS_IF(
374    [test "x$ac_cv_libcthreads" = xyes],
375    [AC_SUBST(
376      [ax_libcthreads_spec_requires],
377      [libcthreads])
378    AC_SUBST(
379      [ax_libcthreads_spec_build_requires],
380      [libcthreads-devel])
381    ])
382  ])
383
384