Eet: add nice m4 macros to check functions and libraries. Will be usable easily in...
[framework/uifw/eet.git] / m4 / efl_check_funcs.m4
1 dnl Copyright (C) 2012 Vincent Torri <vincent dot torri at gmail dot com>
2 dnl This code is public domain and can be freely used or copied.
3
4 dnl Macros that check functions availability for the EFL:
5
6 dnl dirfd
7 dnl dladdr
8 dnl dlopen
9 dnl fnmatch
10 dnl iconv
11 dnl setxattr (an al.)
12 dnl shm_open
13
14
15 dnl _EFL_CHECK_FUNC_DIRFD is for internal use
16 dnl _EFL_CHECK_FUNC_DIRFD(EFL, ACTION-IF-FOUND, ACTION-IF-NOT-FOUND)
17
18 AC_DEFUN([_EFL_CHECK_FUNC_DIRFD],
19 [
20 AC_LINK_IFELSE(
21    [
22     AC_LANG_PROGRAM(
23        [[
24 #ifdef HAVE_DIRENT_H
25 # include <dirent.h>
26 #endif
27        ]],
28        [[
29 int main(void)
30 {
31   DIR *dirp;
32   return dirfd(dirp);
33 }
34        ]])
35    ],
36    [_efl_have_fct="yes"],
37    [_efl_have_fct="no"])
38
39 AS_IF([test "x${_efl_have_fct}" = "xyes"], [$2], [$3])
40 ])
41
42 dnl _EFL_CHECK_FUNC_DLADDR_PRIV is for internal use
43 dnl _EFL_CHECK_FUNC_DLADDR_PRIV(EFL, LIB, ACTION-IF-FOUND, ACTION-IF-NOT-FOUND)
44
45 AC_DEFUN([_EFL_CHECK_FUNC_DLADDR_PRIV],
46 [
47 m4_pushdef([UPEFL], m4_translit([$1], [-a-z], [_A-Z]))dnl
48 m4_pushdef([DOWNEFL], m4_translit([$1], [-A-Z], [_a-z]))dnl
49
50 LIBS_save="${LIBS}"
51 LIBS="${LIBS} $2"
52 AC_LINK_IFELSE(
53    [AC_LANG_PROGRAM(
54        [[
55 #define _GNU_SOURCE
56 #include <dlfcn.h>
57          ]],
58          [[
59 int res = dladdr(0, 0);
60        ]])],
61    [
62     m4_defn([UPEFL])[]_LIBS="${m4_defn([UPEFL])[]_LIBS} $2"
63     requirements_libs_[]m4_defn([DOWNEFL])="${requirements_libs_[]m4_defn([DOWNEFL])} $2"
64     _efl_have_fct="yes"
65    ],
66    [_efl_have_fct="no"])
67
68 LIBS="${LIBS_save}"
69
70 AS_IF([test "x${_efl_have_fct}" = "xyes"], [$3], [$4])
71
72 m4_popdef([DOWNEFL])
73 m4_popdef([UPEFL])
74 ])
75
76 dnl _EFL_CHECK_FUNC_DLADDR is for internal use
77 dnl _EFL_CHECK_FUNC_DLADDR(EFL, ACTION-IF-FOUND, ACTION-IF-NOT-FOUND)
78
79 AC_DEFUN([_EFL_CHECK_FUNC_DLADDR],
80 [
81 m4_pushdef([UPEFL], m4_translit([$1], [-a-z], [_A-Z]))dnl
82 m4_pushdef([DOWNEFL], m4_translit([$1], [-A-Z], [_a-z]))dnl
83
84 case "$host_os" in
85    mingw*)
86       _efl_have_fct="yes"
87       requirements_libs_[]m4_defn([DOWNEFL])="${requirements_libs_[]m4_defn([DOWNEFL])} -ldl"
88       m4_defn([UPEFL])[]_LIBS="${m4_defn([UPEFL])[]_LIBS} -ldl"
89    ;;
90    *)
91       _efl_have_fct="no"
92
93 dnl Check is dladdr is in libc
94       _EFL_CHECK_FUNC_DLADDR_PRIV([$1], [], [_efl_have_fct="yes"], [_efl_have_fct="no"])
95
96 dnl Check is dlopen is in libdl
97       if test "x${_efl_have_fct}" = "xno" ; then
98          _EFL_CHECK_FUNC_DLADDR_PRIV([$1], [-ldl], [_efl_have_fct="yes"], [_efl_have_fct="no"])
99       fi
100    ;;
101 esac
102
103 AS_IF([test "x${_efl_have_fct}" = "xyes"], [$2], [$3])
104
105 m4_popdef([DOWNEFL])
106 m4_popdef([UPEFL])
107 ])
108
109 dnl _EFL_CHECK_FUNC_DLOPEN_PRIV is for internal use
110 dnl _EFL_CHECK_FUNC_DLOPEN_PRIV(EFL, LIB, ACTION-IF-FOUND, ACTION-IF-NOT-FOUND)
111
112 AC_DEFUN([_EFL_CHECK_FUNC_DLOPEN_PRIV],
113 [
114 m4_pushdef([UPEFL], m4_translit([$1], [-a-z], [_A-Z]))dnl
115 m4_pushdef([DOWNEFL], m4_translit([$1], [-A-Z], [_a-z]))dnl
116
117 LIBS_save="${LIBS}"
118 LIBS="${LIBS} $2"
119 AC_LINK_IFELSE(
120    [AC_LANG_PROGRAM(
121        [[
122 #include <dlfcn.h>
123          ]],
124          [[
125 void *h = dlopen(0, 0);
126        ]])],
127    [
128     m4_defn([UPEFL])[]_LIBS="${m4_defn([UPEFL])[]_LIBS} $2"
129     requirements_libs_[]m4_defn([DOWNEFL])="${requirements_libs_[]m4_defn([DOWNEFL])} $2"
130     _efl_have_fct="yes"
131    ],
132    [_efl_have_fct="no"])
133
134 LIBS="${LIBS_save}"
135
136 AS_IF([test "x${_efl_have_fct}" = "xyes"], [$3], [$4])
137
138 m4_popdef([DOWNEFL])
139 m4_popdef([UPEFL])
140 ])
141
142 dnl _EFL_CHECK_FUNC_DLOPEN is for internal use
143 dnl _EFL_CHECK_FUNC_DLOPEN(EFL, ACTION-IF-FOUND, ACTION-IF-NOT-FOUND)
144
145 AC_DEFUN([_EFL_CHECK_FUNC_DLOPEN],
146 [
147 m4_pushdef([UPEFL], m4_translit([$1], [-a-z], [_A-Z]))dnl
148 m4_pushdef([DOWNEFL], m4_translit([$1], [-A-Z], [_a-z]))dnl
149
150 case "$host_os" in
151    mingw*)
152       _efl_have_fct="yes"
153       requirements_libs_[]m4_defn([DOWNEFL])="${requirements_libs_[]m4_defn([DOWNEFL])} -ldl"
154       m4_defn([UPEFL])[]_LIBS="${m4_defn([UPEFL])[]_LIBS} -ldl"
155    ;;
156    *)
157       _efl_have_fct="no"
158
159 dnl Check is dlopen is in libc
160       _EFL_CHECK_FUNC_DLOPEN_PRIV([$1], [], [_efl_have_fct="yes"], [_efl_have_fct="no"])
161
162 dnl Check is dlopen is in libdl
163       if test "x${_efl_have_fct}" = "xno" ; then
164          _EFL_CHECK_FUNC_DLOPEN_PRIV([$1], [-ldl], [_efl_have_fct="yes"], [_efl_have_fct="no"])
165       fi
166    ;;
167 esac
168
169 AS_IF([test "x${_efl_have_fct}" = "xyes"], [$2], [$3])
170
171 m4_popdef([DOWNEFL])
172 m4_popdef([UPEFL])
173 ])
174
175 dnl _EFL_CHECK_FUNC_FNMATCH_PRIV is for internal use
176 dnl _EFL_CHECK_FUNC_FNMATCH_PRIV(EFL, LIB, ACTION-IF-FOUND, ACTION-IF-NOT-FOUND)
177
178 AC_DEFUN([_EFL_CHECK_FUNC_FNMATCH_PRIV],
179 [
180 m4_pushdef([UPEFL], m4_translit([$1], [-a-z], [_A-Z]))dnl
181 m4_pushdef([DOWNEFL], m4_translit([$1], [-A-Z], [_a-z]))dnl
182
183 LIBS_save="${LIBS}"
184 LIBS="${LIBS} $2"
185 AC_LINK_IFELSE(
186    [AC_LANG_PROGRAM(
187        [[
188 #include <stdlib.h>
189 #include <fnmatch.h>
190          ]],
191          [[
192 int g = fnmatch(NULL, NULL, 0);
193        ]])],
194    [
195     m4_defn([UPEFL])[]_LIBS="${m4_defn([UPEFL])[]_LIBS} $2"
196     requirements_libs_[]m4_defn([DOWNEFL])="${requirements_libs_[]m4_defn([DOWNEFL])} $2"
197     _efl_have_fct="yes"
198    ],
199    [_efl_have_fct="no"])
200
201 LIBS="${LIBS_save}"
202
203 AS_IF([test "x${_efl_have_fct}" = "xyes"], [$3], [$4])
204
205 m4_popdef([DOWNEFL])
206 m4_popdef([UPEFL])
207 ])
208
209 dnl _EFL_CHECK_FUNC_FNMATCH is for internal use
210 dnl _EFL_CHECK_FUNC_FNMATCH(EFL, ACTION-IF-FOUND, ACTION-IF-NOT-FOUND)
211
212 AC_DEFUN([_EFL_CHECK_FUNC_FNMATCH],
213 [
214 case "$host_os" in
215    mingw*)
216       _efl_have_fct="yes"
217    ;;
218    *)
219 dnl Check is fnmatch is in libc
220       _EFL_CHECK_FUNC_FNMATCH_PRIV([$1], [], [_efl_have_fct="yes"], [_efl_have_fct="no"])
221
222 dnl Check is fnmatch is in libfnmatch
223       if test "x${_efl_have_fct}" = "xno" ; then
224          _EFL_CHECK_FUNC_FNMATCH_PRIV([$1], [-lfnmatch], [_efl_have_fct="yes"], [_efl_have_fct="no"])
225       fi
226
227 dnl Check is fnmatch is in libiberty
228       if test "x${_efl_have_fct}" = "xno" ; then
229          _EFL_CHECK_FUNC_FNMATCH_PRIV([$1], [-liberty], [_efl_have_fct="yes"], [_efl_have_fct="no"])
230       fi
231    ;;
232 esac
233
234 AS_IF([test "x${_efl_have_fct}" = "xyes"], [$2], [$3])
235 ])
236
237 dnl _EFL_CHECK_FUNC_ICONV_PRIV is for internal use
238 dnl _EFL_CHECK_FUNC_ICONV_PRIV(EFL, LIB, ACTION-IF-FOUND, ACTION-IF-NOT-FOUND)
239
240 AC_DEFUN([_EFL_CHECK_FUNC_ICONV_PRIV],
241 [
242 m4_pushdef([UPEFL], m4_translit([$1], [-a-z], [_A-Z]))dnl
243 m4_pushdef([DOWNEFL], m4_translit([$1], [-A-Z], [_a-z]))dnl
244
245 LIBS_save="${LIBS}"
246 LIBS="${LIBS} $2"
247 AC_LINK_IFELSE(
248    [AC_LANG_PROGRAM(
249        [[
250 #include <stdlib.h>
251 #include <iconv.h>
252          ]],
253          [[
254 iconv_t ic;
255 size_t count = iconv(ic, NULL, NULL, NULL, NULL);
256        ]])],
257    [
258     m4_defn([UPEFL])[]_LIBS="${m4_defn([UPEFL])[]_LIBS} $2"
259     requirements_libs_[]m4_defn([DOWNEFL])="${requirements_libs_[]m4_defn([DOWNEFL])} $2"
260     _efl_have_fct="yes"
261    ],
262    [_efl_have_fct="no"])
263
264 LIBS="${LIBS_save}"
265
266 AS_IF([test "x${_efl_have_fct}" = "xyes"], [$3], [$4])
267
268 m4_popdef([DOWNEFL])
269 m4_popdef([UPEFL])
270 ])
271
272 dnl _EFL_CHECK_FUNC_ICONV is for internal use
273 dnl _EFL_CHECK_FUNC_ICONV(EFL, ACTION-IF-FOUND, ACTION-IF-NOT-FOUND)
274
275 AC_DEFUN([_EFL_CHECK_FUNC_ICONV],
276 [
277 AC_ARG_WITH([iconv-link],
278    AC_HELP_STRING([--with-iconv-link=ICONV_LINK], [explicitly specify an iconv link option]),
279    [
280     _efl_have_fct="yes"
281     iconv_libs=${withval}
282    ],
283    [_efl_have_fct="no"])
284
285 AC_MSG_CHECKING([for explicit iconv link options])
286 if test "x${iconv_libs}" = "x" ; then
287    AC_MSG_RESULT([no explicit iconv link option])
288 else
289    AC_MSG_RESULT([${iconv_libs}])
290 fi
291
292 dnl Check is iconv is in libc
293 if test "x${_efl_have_fct}" = "xno" ; then
294    _EFL_CHECK_FUNC_ICONV_PRIV([$1], [], [_efl_have_fct="yes"], [_efl_have_fct="no"])
295 fi
296
297 dnl Check is iconv is in libiconv
298 if test "x${_efl_have_fct}" = "xno" ; then
299    _EFL_CHECK_FUNC_ICONV_PRIV([$1], [-liconv], [_efl_have_fct="yes"], [_efl_have_fct="no"])
300 fi
301
302 dnl Check is iconv is in libiconv_plug
303 if test "x${_efl_have_fct}" = "xno" ; then
304    _EFL_CHECK_FUNC_ICONV_PRIV([$1], [-liconv_plug], [_efl_have_fct="yes"], [_efl_have_fct="no"])
305 fi
306
307 AS_IF([test "x${_efl_have_fct}" = "xyes"], [$2], [$3])
308 ])
309
310 dnl _EFL_CHECK_FUNC_SETXATTR is for internal use
311 dnl _EFL_CHECK_FUNC_SETXATTR(EFL, ACTION-IF-FOUND, ACTION-IF-NOT-FOUND)
312
313 AC_DEFUN([_EFL_CHECK_FUNC_SETXATTR],
314 [
315 AC_COMPILE_IFELSE(
316    [AC_LANG_PROGRAM(
317        [[
318 #include <stdlib.h>
319 #include <sys/types.h>
320 #include <sys/xattr.h>
321        ]],
322        [[
323 size_t tmp = listxattr("/", NULL, 0);
324 tmp = getxattr("/", "user.ethumb.md5", NULL, 0);
325 setxattr("/", "user.ethumb.md5", NULL, 0, 0);
326        ]])],
327    [_efl_have_fct="yes"],
328    [_efl_have_fct="no"])
329
330 AS_IF([test "x${_efl_have_fct}" = "xyes"], [$2], [$3])
331 ])
332
333 dnl _EFL_CHECK_FUNC_SHM_OPEN_PRIV is for internal use
334 dnl _EFL_CHECK_FUNC_SHM_OPEN_PRIV(EFL, LIB, ACTION-IF-FOUND, ACTION-IF-NOT-FOUND)
335
336 AC_DEFUN([_EFL_CHECK_FUNC_SHM_OPEN_PRIV],
337 [
338 m4_pushdef([UPEFL], m4_translit([$1], [-a-z], [_A-Z]))dnl
339 m4_pushdef([DOWNEFL], m4_translit([$1], [-A-Z], [_a-z]))dnl
340
341 LIBS_save="${LIBS}"
342 LIBS="${LIBS} $2"
343 AC_LINK_IFELSE(
344    [AC_LANG_PROGRAM(
345        [[
346 #include <sys/mman.h>
347 #include <sys/stat.h>        /* For mode constants */
348 #include <fcntl.h>           /* For O_* constants */
349        ]],
350        [[
351 int fd;
352
353 fd = shm_open("/dev/null", O_RDONLY, S_IRWXU | S_IRWXG | S_IRWXO);
354        ]])],
355    [
356     m4_defn([UPEFL])[]_LIBS="$m4_defn([UPEFL])[]_LIBS $2"
357     requirements_libs_[]m4_defn([DOWNEFL])="${requirements_libs_[]m4_defn([DOWNEFL])} $2"
358     _efl_have_fct="yes"
359    ],
360    [_efl_have_fct="no"])
361
362 LIBS="${LIBS_save}"
363
364 AS_IF([test "x${_efl_have_fct}" = "xyes"], [$3], [$4])
365
366 m4_popdef([DOWNEFL])
367 m4_popdef([UPEFL])
368 ])
369
370 dnl _EFL_CHECK_FUNC_SHM_OPEN is for internal use
371 dnl _EFL_CHECK_FUNC_SHM_OPEN(EFL, ACTION-IF-FOUND, ACTION-IF-NOT-FOUND)
372
373 AC_DEFUN([_EFL_CHECK_FUNC_SHM_OPEN],
374 [
375 _efl_have_fct="no"
376
377 dnl Check is shm_open is in libc
378 _EFL_CHECK_FUNC_SHM_OPEN_PRIV([$1], [],
379    [_efl_have_fct="yes"],
380    [_efl_have_fct="no"])
381
382 dnl Check is shm_open is in librt
383 if test "x${_efl_have_fct}" = "xno" ; then
384    _EFL_CHECK_FUNC_SHM_OPEN_PRIV([$1], [-lrt],
385       [_efl_have_fct="yes"],
386       [_efl_have_fct="no"])
387 fi
388
389 AS_IF([test "x${_efl_have_fct}" = "xyes"], [$2], [$3])
390 ])
391
392 dnl Macro that checks function availability
393 dnl
394 dnl EFL_CHECK_FUNC(EFL, FUNCTION)
395 dnl AC_DEFINE : EFL_HAVE_FUNCTION (FUNCTION being replaced by its value)
396 dnl result in efl_func_function (function being replaced by its value)
397
398 AC_DEFUN([EFL_CHECK_FUNC],
399 [
400 m4_pushdef([UP], m4_translit([$2], [-a-z], [_A-Z]))dnl
401 m4_pushdef([DOWN], m4_translit([$2], [-A-Z], [_a-z]))dnl
402
403 m4_default([_EFL_CHECK_FUNC_]m4_defn([UP]))($1, [have_fct="yes"], [have_fct="no"])
404
405 if test "x$2" = "xsetxattr" ; then
406    AC_MSG_CHECKING([for extended attributes])
407 else
408    AC_MSG_CHECKING([for ]m4_defn([DOWN]))
409 fi
410
411 AC_MSG_RESULT([${have_fct}])
412
413 if test "x${have_fct}" = "xyes" ; then
414    if test "x$2" = "xsetxattr" ; then
415       AC_DEFINE([HAVE_XATTR], [1], [Define to 1 if you have the `listxattr', `setxattr' and `getxattr' functions.])
416    else
417       AC_DEFINE([HAVE_]m4_defn([UP]), [1], [Define to 1 if you have the `]m4_defn([DOWN])[' function.])
418    fi
419 fi
420
421 efl_func_[]m4_defn([DOWN])="${have_fct}"
422
423 m4_popdef([DOWN])
424 m4_popdef([UP])
425 ])
426
427 dnl Macro that iterates over a sequence of space separated functions
428 dnl and that calls EFL_CHECK_FUNC() for each of these functions
429 dnl
430 dnl EFL_CHECK_FUNCS(EFL, FUNCTIONS)
431
432 AC_DEFUN([EFL_CHECK_FUNCS],
433 [
434 m4_foreach_w([fct], [$2], [EFL_CHECK_FUNC($1, m4_defn([fct]))])
435 ])