[libreplace] fix the standalone build of libreplace
[jerry/samba.git] / source / lib / replace / libreplace_macros.m4
1 #
2 # This is a collection of useful autoconf macros
3 #
4
5 ############################################
6 # Check if the compiler handles c99 struct initialization, and if not try -AC99 and -c99 flags
7 # Usage: LIBREPLACE_C99_STRUCT_INIT(success-action,failure-action)
8 # changes CFLAGS to add -AC99 or -c99 if needed
9 AC_DEFUN([LIBREPLACE_C99_STRUCT_INIT],
10 [
11 saved_CFLAGS="$CFLAGS";
12 c99_init=no
13 if test x"$c99_init" = x"no"; then
14     AC_MSG_CHECKING(for C99 designated initializers)
15     CFLAGS="$saved_CFLAGS";
16     AC_TRY_COMPILE([#include <stdio.h>],
17      [ struct foo {int x;char y;};
18        struct foo bar = { .y = 'X', .x = 1 };    
19      ],
20      [AC_MSG_RESULT(yes); c99_init=yes],[AC_MSG_RESULT(no)])
21 fi
22 if test x"$c99_init" = x"no"; then
23     AC_MSG_CHECKING(for C99 designated initializers with -AC99)
24     CFLAGS="$saved_CFLAGS -AC99";
25     AC_TRY_COMPILE([#include <stdio.h>],
26      [ struct foo {int x;char y;};
27        struct foo bar = { .y = 'X', .x = 1 };    
28      ],
29      [AC_MSG_RESULT(yes); c99_init=yes],[AC_MSG_RESULT(no)])
30 fi
31 if test x"$c99_init" = x"no"; then
32     AC_MSG_CHECKING(for C99 designated initializers with -qlanglvl=extc99)
33     CFLAGS="$saved_CFLAGS -qlanglvl=extc99";
34     AC_TRY_COMPILE([#include <stdio.h>],
35      [ struct foo {int x;char y;};
36        struct foo bar = { .y = 'X', .x = 1 };    
37      ],
38      [AC_MSG_RESULT(yes); c99_init=yes],[AC_MSG_RESULT(no)])
39 fi
40 if test x"$c99_init" = x"no"; then
41     AC_MSG_CHECKING(for C99 designated initializers with -qlanglvl=stdc99)
42     CFLAGS="$saved_CFLAGS -qlanglvl=stdc99";
43     AC_TRY_COMPILE([#include <stdio.h>],
44      [ struct foo {int x;char y;};
45        struct foo bar = { .y = 'X', .x = 1 };    
46      ],
47      [AC_MSG_RESULT(yes); c99_init=yes],[AC_MSG_RESULT(no)])
48 fi
49 if test x"$c99_init" = x"no"; then
50     AC_MSG_CHECKING(for C99 designated initializers with -c99)
51     CFLAGS="$saved_CFLAGS -c99"
52     AC_TRY_COMPILE([#include <stdio.h>],
53      [ struct foo {int x;char y;};
54        struct foo bar = { .y = 'X', .x = 1 };    
55      ],
56      [AC_MSG_RESULT(yes); c99_init=yes],[AC_MSG_RESULT(no)])
57 fi
58
59 if test "`uname`" = "HP-UX"; then
60   if test "$ac_cv_c_compiler_gnu" = no; then
61         # special override for broken HP-UX compiler - I can't find a way to test
62         # this properly (its a compiler bug)
63         CFLAGS="$CFLAGS -AC99";
64         c99_init=yes;
65   fi
66 fi
67
68 if test x"$c99_init" = x"yes"; then
69     saved_CFLAGS=""
70     $1
71 else
72     CFLAGS="$saved_CFLAGS"
73     saved_CFLAGS=""
74     $2
75 fi
76 ])
77
78 dnl AC_PROG_CC_FLAG(flag)
79 AC_DEFUN(AC_PROG_CC_FLAG,
80 [AC_CACHE_CHECK(whether ${CC-cc} accepts -$1, ac_cv_prog_cc_$1,
81 [echo 'void f(){}' > conftest.c
82 if test -z "`${CC-cc} -$1 -c conftest.c 2>&1`"; then
83   ac_cv_prog_cc_$1=yes
84 else
85   ac_cv_prog_cc_$1=no
86 fi
87 rm -f conftest*
88 ])])
89
90 AC_DEFUN([AC_EXTENSION_FLAG],
91 [
92   cat >>confdefs.h <<\EOF
93 #ifndef $1
94 # define $1 1
95 #endif
96 EOF
97 AH_VERBATIM([$1], [#ifndef $1
98 # define $1 1
99 #endif])
100 ])
101
102
103 dnl see if a declaration exists for a function or variable
104 dnl defines HAVE_function_DECL if it exists
105 dnl AC_HAVE_DECL(var, includes)
106 AC_DEFUN(AC_HAVE_DECL,
107 [
108  AC_CACHE_CHECK([for $1 declaration],ac_cv_have_$1_decl,[
109     AC_TRY_COMPILE([$2],[int i = (int)$1],
110         ac_cv_have_$1_decl=yes,ac_cv_have_$1_decl=no)])
111  if test x"$ac_cv_have_$1_decl" = x"yes"; then
112     AC_DEFINE([HAVE_]translit([$1], [a-z], [A-Z])[_DECL],1,[Whether $1() is available])
113  fi
114 ])
115
116
117 # AC_CHECK_LIB_EXT(LIBRARY, [EXT_LIBS], [FUNCTION],
118 #              [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND],
119 #              [ADD-ACTION-IF-FOUND],[OTHER-LIBRARIES])
120 # ------------------------------------------------------
121 #
122 # Use a cache variable name containing both the library and function name,
123 # because the test really is for library $1 defining function $3, not
124 # just for library $1.  Separate tests with the same $1 and different $3s
125 # may have different results.
126 #
127 # Note that using directly AS_VAR_PUSHDEF([ac_Lib], [ac_cv_lib_$1_$3])
128 # is asking for trouble, since AC_CHECK_LIB($lib, fun) would give
129 # ac_cv_lib_$lib_fun, which is definitely not what was meant.  Hence
130 # the AS_LITERAL_IF indirection.
131 #
132 # FIXME: This macro is extremely suspicious.  It DEFINEs unconditionally,
133 # whatever the FUNCTION, in addition to not being a *S macro.  Note
134 # that the cache does depend upon the function we are looking for.
135 #
136 # It is on purpose we used `ac_check_lib_ext_save_LIBS' and not just
137 # `ac_save_LIBS': there are many macros which don't want to see `LIBS'
138 # changed but still want to use AC_CHECK_LIB_EXT, so they save `LIBS'.
139 # And ``ac_save_LIBS' is too tempting a name, so let's leave them some
140 # freedom.
141 AC_DEFUN([AC_CHECK_LIB_EXT],
142 [
143 AH_CHECK_LIB_EXT([$1])
144 ac_check_lib_ext_save_LIBS=$LIBS
145 LIBS="-l$1 $$2 $7 $LIBS"
146 AS_LITERAL_IF([$1],
147       [AS_VAR_PUSHDEF([ac_Lib_ext], [ac_cv_lib_ext_$1])],
148       [AS_VAR_PUSHDEF([ac_Lib_ext], [ac_cv_lib_ext_$1''])])dnl
149
150 m4_ifval([$3],
151  [
152     AH_CHECK_FUNC_EXT([$3])
153     AS_LITERAL_IF([$1],
154               [AS_VAR_PUSHDEF([ac_Lib_func], [ac_cv_lib_ext_$1_$3])],
155               [AS_VAR_PUSHDEF([ac_Lib_func], [ac_cv_lib_ext_$1''_$3])])dnl
156     AC_CACHE_CHECK([for $3 in -l$1], ac_Lib_func,
157         [AC_TRY_LINK_FUNC($3,
158                  [AS_VAR_SET(ac_Lib_func, yes);
159                   AS_VAR_SET(ac_Lib_ext, yes)],
160                  [AS_VAR_SET(ac_Lib_func, no);
161                   AS_VAR_SET(ac_Lib_ext, no)])
162         ])
163     AS_IF([test AS_VAR_GET(ac_Lib_func) = yes],
164         [AC_DEFINE_UNQUOTED(AS_TR_CPP(HAVE_$3))])dnl
165     AS_VAR_POPDEF([ac_Lib_func])dnl
166  ],[
167     AC_CACHE_CHECK([for -l$1], ac_Lib_ext,
168         [AC_TRY_LINK_FUNC([main],
169                  [AS_VAR_SET(ac_Lib_ext, yes)],
170                  [AS_VAR_SET(ac_Lib_ext, no)])
171         ])
172  ])
173 LIBS=$ac_check_lib_ext_save_LIBS
174
175 AS_IF([test AS_VAR_GET(ac_Lib_ext) = yes],
176     [m4_default([$4], 
177         [AC_DEFINE_UNQUOTED(AS_TR_CPP(HAVE_LIB$1))
178                 case "$$2" in
179                     *-l$1*)
180                         ;;
181                     *)
182                         $2="-l$1 $$2"
183                         ;;
184                 esac])
185                 [$6]
186             ],
187             [$5])dnl
188 AS_VAR_POPDEF([ac_Lib_ext])dnl
189 ])# AC_CHECK_LIB_EXT
190
191 # AH_CHECK_LIB_EXT(LIBNAME)
192 # ---------------------
193 m4_define([AH_CHECK_LIB_EXT],
194 [AH_TEMPLATE(AS_TR_CPP(HAVE_LIB$1),
195              [Define to 1 if you have the `]$1[' library (-l]$1[).])])
196
197 dnl AC_SEARCH_LIBS_EXT(FUNCTION, SEARCH-LIBS, EXT_LIBS,
198 dnl                    [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND],
199 dnl                    [OTHER-LIBRARIES])
200 dnl --------------------------------------------------------
201 dnl Search for a library defining FUNC, if it's not already available.
202 AC_DEFUN([AC_SEARCH_LIBS_EXT],
203 [AC_CACHE_CHECK([for library containing $1], [ac_cv_search_ext_$1],
204 [
205 ac_func_search_ext_save_LIBS=$LIBS
206 ac_cv_search_ext_$1=no
207 AC_LINK_IFELSE([AC_LANG_CALL([], [$1])],
208                [ac_cv_search_ext_$1="none required"])
209 if test "$ac_cv_search_ext_$1" = no; then
210   for ac_lib in $2; do
211     LIBS="-l$ac_lib $$3 $6 $ac_func_search_save_ext_LIBS"
212     AC_LINK_IFELSE([AC_LANG_CALL([], [$1])],
213                    [ac_cv_search_ext_$1="-l$ac_lib"
214 break])
215   done
216 fi
217 LIBS=$ac_func_search_ext_save_LIBS])
218 AS_IF([test "$ac_cv_search_ext_$1" != no],
219   [test "$ac_cv_search_ext_$1" = "none required" || $3="$ac_cv_search_ext_$1 $$3"
220   $4],
221       [$5])dnl
222 ])
223
224 dnl check for a function in a $LIBS and $OTHER_LIBS libraries variable.
225 dnl AC_CHECK_FUNC_EXT(func,OTHER_LIBS,IF-TRUE,IF-FALSE)
226 AC_DEFUN([AC_CHECK_FUNC_EXT],
227 [
228     AH_CHECK_FUNC_EXT($1)       
229     ac_check_func_ext_save_LIBS=$LIBS
230     LIBS="$2 $LIBS"
231     AS_VAR_PUSHDEF([ac_var], [ac_cv_func_ext_$1])dnl
232     AC_CACHE_CHECK([for $1], ac_var,
233         [AC_LINK_IFELSE([AC_LANG_FUNC_LINK_TRY([$1])],
234                 [AS_VAR_SET(ac_var, yes)],
235                 [AS_VAR_SET(ac_var, no)])])
236     LIBS=$ac_check_func_ext_save_LIBS
237     AS_IF([test AS_VAR_GET(ac_var) = yes], 
238             [AC_DEFINE_UNQUOTED(AS_TR_CPP([HAVE_$1])) $3], 
239             [$4])dnl
240 AS_VAR_POPDEF([ac_var])dnl
241 ])# AC_CHECK_FUNC
242
243 # AH_CHECK_FUNC_EXT(FUNCNAME)
244 # ---------------------
245 m4_define([AH_CHECK_FUNC_EXT],
246 [AH_TEMPLATE(AS_TR_CPP(HAVE_$1),
247              [Define to 1 if you have the `]$1[' function.])])
248
249 dnl Define an AC_DEFINE with ifndef guard.
250 dnl AC_N_DEFINE(VARIABLE [, VALUE])
251 define(AC_N_DEFINE,
252 [cat >> confdefs.h <<\EOF
253 [#ifndef] $1
254 [#define] $1 ifelse($#, 2, [$2], $#, 3, [$2], 1)
255 [#endif]
256 EOF
257 ])
258
259 dnl Add an #include
260 dnl AC_ADD_INCLUDE(VARIABLE)
261 define(AC_ADD_INCLUDE,
262 [cat >> confdefs.h <<\EOF
263 [#include] $1
264 EOF
265 ])
266
267 dnl remove an #include
268 dnl AC_REMOVE_INCLUDE(VARIABLE)
269 define(AC_REMOVE_INCLUDE,
270 [
271 grep -v '[#include] $1' confdefs.h >confdefs.h.tmp
272 cat confdefs.h.tmp > confdefs.h
273 rm confdefs.h.tmp
274 ])
275
276 dnl remove an #define
277 dnl AC_REMOVE_DEFINE(VARIABLE)
278 define(AC_REMOVE_DEFINE,
279 [
280 grep -v '[#define] $1 ' confdefs.h |grep -v '[#define] $1[$]'>confdefs.h.tmp
281 cat confdefs.h.tmp > confdefs.h
282 rm confdefs.h.tmp
283 ])
284
285 dnl AS_HELP_STRING is not available in autoconf 2.57, and AC_HELP_STRING is deprecated
286 dnl in autoconf 2.59, so define AS_HELP_STRING to be AC_HELP_STRING unless it is already
287 dnl defined.
288 m4_ifdef([AS_HELP_STRING], , [m4_define([AS_HELP_STRING], m4_defn([AC_HELP_STRING]))])
289
290 dnl check if the prototype in the header matches the given one
291 dnl AC_VERIFY_C_PROTOTYPE(prototype,functionbody,[IF-TRUE].[IF-FALSE],[extraheaders])
292 AC_DEFUN(AC_VERIFY_C_PROTOTYPE,
293 [AC_CACHE_CHECK([for prototype $1], AS_TR_SH([ac_cv_c_prototype_$1]),
294         AC_COMPILE_IFELSE([
295                 AC_INCLUDES_DEFAULT
296                 $5
297                 $1
298                 {
299                         $2
300                 }
301         ],[
302                 AS_TR_SH([ac_cv_c_prototype_$1])=yes
303         ],[
304                 AS_TR_SH([ac_cv_c_prototype_$1])=no
305         ])
306 )
307 AS_IF([test $AS_TR_SH([ac_cv_c_prototype_$1]) = yes],[$3],[$4])
308 ])
309
310 AC_DEFUN(LIBREPLACE_PROVIDE_HEADER, 
311 [AC_CHECK_HEADER([$1], 
312                 [ AC_CONFIG_COMMANDS(rm-$1, [rm -f $libreplacedir/$1], [libreplacedir=$libreplacedir]) ],
313                 [ AC_CONFIG_COMMANDS(mk-$1, [echo "#include \"replace.h\"" > $libreplacedir/$1], [libreplacedir=$libreplacedir]) ]
314         )
315 ])
316
317 dnl AC_HAVE_TYPE(TYPE,INCLUDES)
318 AC_DEFUN([AC_HAVE_TYPE], [
319 AC_REQUIRE([AC_HEADER_STDC])
320 cv=`echo "$1" | sed 'y%./+- %__p__%'`
321 AC_MSG_CHECKING(for $1)
322 AC_CACHE_VAL([ac_cv_type_$cv],
323 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
324 AC_INCLUDES_DEFAULT
325 $2]],
326 [[$1 foo;]])],
327 [eval "ac_cv_type_$cv=yes"],
328 [eval "ac_cv_type_$cv=no"]))dnl
329 ac_foo=`eval echo \\$ac_cv_type_$cv`
330 AC_MSG_RESULT($ac_foo)
331 if test "$ac_foo" = yes; then
332   ac_tr_hdr=HAVE_`echo $1 | sed 'y%abcdefghijklmnopqrstuvwxyz./- %ABCDEFGHIJKLMNOPQRSTUVWXYZ____%'`
333 if false; then
334         AC_CHECK_TYPES($1)
335 fi
336   AC_DEFINE_UNQUOTED($ac_tr_hdr, 1, [Define if you have type `$1'])
337 fi
338 ])