configure: improve checks for funcs not in stdlib
[m6w6/ext-psi] / m4 / psi / psi.m4
1 dnl Generated headers with pre-defined types, structs, consts and decls.
2 PSI_STDINC=$PHP_PSI_SRCDIR/php_psi_stdinc.h
3 PSI_STDTYPES=$PHP_PSI_SRCDIR/php_psi_stdtypes.h
4 PSI_TYPES=$PHP_PSI_SRCDIR/php_psi_types.h
5 PSI_STRUCTS=$PHP_PSI_SRCDIR/php_psi_structs.h
6 PSI_UNIONS=$PHP_PSI_SRCDIR/php_psi_unions.h
7 PSI_CONSTS=$PHP_PSI_SRCDIR/php_psi_consts.h
8 PSI_REDIRS=$PHP_PSI_SRCDIR/php_psi_redirs.h
9 PSI_MACROS=$PHP_PSI_SRCDIR/php_psi_macros.h
10 PSI_DECLS=$PHP_PSI_SRCDIR/php_psi_decls.h
11 PSI_VA_DECLS=$PHP_PSI_SRCDIR/php_psi_va_decls.h
12 PSI_FN_DECLS=$PHP_PSI_SRCDIR/php_psi_fn_decls.h
13
14 dnl PSI_CONFIG_INIT()
15 dnl Creates stubs of the headers with pre-defined types etc.
16 dnl These headers are included by src/context.c.
17 dnl This macro must be called prior any checks for a type, struct, decl etc.
18 AC_DEFUN(PSI_CONFIG_INIT, [AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])dnl
19 for i in $PSI_STDTYPES $PSI_TYPES $PSI_STRUCTS $PSI_UNIONS $PSI_CONSTS $PSI_REDIRS $PSI_MACROS $PSI_DECLS $PSI_VA_DECLS $PSI_FN_DECLS; do
20 cat >$i <<EOF
21 /* generated by configure */
22 #include "php_psi_stdinc.h"
23 EOF
24 done
25 cat >>$PSI_STDTYPES <<EOF
26 static struct psi_std_type {
27 token_t type_tag;
28 const char *type_name;
29 const char *alias;
30 } psi_std_types@<:@@:>@ = {
31 {PSI_T_INT8, "int8_t", NULL},
32 {PSI_T_INT16, "int16_t", NULL},
33 {PSI_T_INT32, "int32_t", NULL},
34 {PSI_T_INT64, "int64_t", NULL},
35 {PSI_T_UINT8, "uint8_t", NULL},
36 {PSI_T_UINT16, "uint16_t", NULL},
37 {PSI_T_UINT32, "uint32_t", NULL},
38 {PSI_T_UINT64, "uint64_t", NULL},
39 EOF
40 cat >>$PSI_TYPES <<EOF
41 static struct psi_predef_type {
42 token_t type_tag;
43 const char *type_name;
44 const char *alias;
45 } psi_predef_types@<:@@:>@ = {
46 EOF
47 cat >>$PSI_STRUCTS <<EOF
48 static struct psi_predef_struct {
49 token_t type_tag;
50 const char *type_name;
51 const char *var_name;
52 size_t offset;
53 size_t size;
54 size_t pointer_level;
55 size_t array_size;
56 } psi_predef_structs@<:@@:>@ = {
57 EOF
58 cat >>$PSI_UNIONS <<EOF
59 static struct psi_predef_union {
60 token_t type_tag;
61 const char *type_name;
62 const char *var_name;
63 size_t offset;
64 size_t size;
65 size_t pointer_level;
66 size_t array_size;
67 } psi_predef_unions@<:@@:>@ = {
68 EOF
69 cat >>$PSI_CONSTS <<EOF
70 static struct psi_predef_const {
71 token_t type_tag;
72 const char *type_name;
73 const char *var_name;
74 const char *val_text;
75 token_t val_type_tag;
76 } psi_predef_consts@<:@@:>@ = {
77 EOF
78 cat >>$PSI_REDIRS <<EOF
79 typedef void (*psi_func_ptr)();
80 static struct psi_func_redir {
81 const char *name;
82 psi_func_ptr func;
83 } psi_func_redirs@<:@@:>@ = {
84 EOF
85 cat >>$PSI_MACROS <<EOF
86 EOF
87 cat >>$PSI_DECLS <<EOF
88 static struct psi_predef_decl {
89 token_t type_tag;
90 const char *type_name;
91 const char *var_name;
92 size_t pointer_level;
93 size_t array_size;
94 } psi_predef_decls@<:@@:>@ = {
95 EOF
96 cat >>$PSI_VA_DECLS <<EOF
97 static struct psi_predef_decl psi_predef_vararg_decls@<:@@:>@ = {
98 EOF
99 cat >>$PSI_FN_DECLS <<EOF
100 static struct psi_predef_decl psi_predef_functor_decls@<:@@:>@ = {
101 EOF
102 ])
103
104 dnl PSI_CONFIG_DONE()
105 dnl Finish the headers with the pre-defined types etc.
106 AC_DEFUN(PSI_CONFIG_DONE, [
107 cat >$PSI_STDINC <<EOF
108 /* generated by configure */
109 #ifndef _PSI_STDINC
110 # define _PSI_STDINC
111 PSI_INCLUDES
112 #endif
113 EOF
114 for i in $PSI_STDTYPES $PSI_TYPES $PSI_STRUCTS $PSI_UNIONS $PSI_CONSTS $PSI_REDIRS $PSI_DECLS $PSI_VA_DECLS $PSI_FN_DECLS; do
115 cat >>$i <<EOF
116 {0}
117 };
118 EOF
119 done
120 ])
121
122 dnl PSI_SH_CONFIG_POSIX_ENABLED(section)
123 dnl Expand to $psi_config_posix_<section>
124 AC_DEFUN(PSI_SH_CONFIG_POSIX_ENABLED, [$AS_TR_SH([psi_config_posix_]$1)])
125
126 dnl PSI_CONFIG_POSIX_ENABLED(section, action-if-yes, action-if-not)
127 dnl Internal. Used to check if --enable-psi-posix=section was given.
128 AC_DEFUN(PSI_CONFIG_POSIX_ENABLED, [
129 AS_TR_SH([psi_config_posix_]$1)=false
130 case "$PHP_PSI_POSIX" in
131 yes|all)
132 AS_TR_SH([psi_config_posix_]$1)=true
133 ;;
134 *)
135 if expr "$PHP_PSI_POSIX" : '\b$1\b' >/dev/null; then
136 AS_TR_SH([psi_config_posix_]$1)=true
137 fi
138 ;;
139 esac
140 if $AS_TR_SH([psi_config_posix_]$1); then
141 ifelse([$2],,:,[$2])
142 else
143 ifelse([$3],,:,[$3])
144 fi
145 ])
146
147 dnl PSI_TEST_POSIX_ENABLED(section, action-if-yes, action-if-not)
148 dnl Shell-if test if PSI POSIX section was configured.
149 AC_DEFUN(PSI_SH_TEST_POSIX_ENABLED, [
150 if test "PSI_SH_CONFIG_POSIX_ENABLED([$1])" && $PSI_SH_CONFIG_POSIX_ENABLED([$1]); then
151 ifelse([$2],,:,[$2])
152 else
153 ifelse([$3],,:,[$3])
154 fi
155 ])
156
157 dnl PSI_CONFIG_POSIX(section, headers)
158 AC_DEFUN(PSI_CONFIG_POSIX, [
159 PSI_CONFIG_POSIX_ENABLED($1, [
160 PHP_CONFIGURE_PART(Configuring PSI POSIX: $1)
161 ifelse([$2],,:,[AC_CHECK_HEADERS($2)])
162 ], [
163 return 0
164 ])
165 ])
166
167 AC_DEFUN(PSI_PTHREAD_ONCE, [
168 AX_PTHREAD([
169 LIBS="$PTHREAD_LIBS $LIBS"
170 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
171 ])
172 ])
173 AC_DEFUN(PSI_PTHREAD, [
174 AC_REQUIRE([PSI_PTHREAD_ONCE])
175 ])
176
177 dnl PSI_INCLUDES()
178 dnl Expands to a complete list of include statements including
179 dnl AC_INCLUDES_DEFAULT().
180 AC_DEFUN(PSI_INCLUDES, [#define PSI_INCLUDES
181 #ifndef _GNU_SOURCE
182 # define _GNU_SOURCE
183 #endif
184 #ifndef _REENTRANT
185 # define _REENTRANT
186 #endif
187 AC_INCLUDES_DEFAULT()
188 #ifdef HAVE_ERRNO_H
189 # include <errno.h>
190 #endif
191 #ifdef HAVE_GLOB_H
192 # include <glob.h>
193 #endif
194 #ifdef HAVE_LOCALE_H
195 # include <locale.h>
196 #endif
197 #ifdef HAVE_XLOCALE_H
198 # include <xlocale.h>
199 #endif
200 #ifdef HAVE_NETINET_IN_H
201 # include <netinet/in.h>
202 #endif
203 #ifdef HAVE_NETINET_TCP_H
204 # include <netinet/tcp.h>
205 #endif
206 #ifdef HAVE_ARPA_NAMESER_H
207 # include <arpa/nameser.h>
208 #endif
209 #ifdef HAVE_FCNTL_H
210 # include <fcntl.h>
211 #endif
212 #ifdef HAVE_RELIC_H
213 # include <relic.h>
214 #elif HAVE_NDBM_H
215 # include <ndbm.h>
216 #elif HAVE_GDBM_NDBM_H
217 # include <gdbm-ndbm.h>
218 #endif
219 #ifdef HAVE_NETDB_H
220 # include <netdb.h>
221 #endif
222 #ifdef HAVE_POLL_H
223 # include <poll.h>
224 #endif
225 #ifdef HAVE_RESOLV_H
226 # include <resolv.h>
227 #endif
228 #ifdef HAVE_SYS_SELECT_H
229 # include <sys/select.h>
230 #endif
231 #ifdef HAVE_SYS_SOCKET_H
232 # include <sys/socket.h>
233 #endif
234 #ifdef HAVE_SYS_TIME_H
235 # include <sys/time.h>
236 #endif
237 #ifdef HAVE_SYS_TIMES_H
238 # include <sys/times.h>
239 #endif
240 #ifdef HAVE_SYS_UIO_H
241 # include <sys/uio.h>
242 #endif
243 #ifdef HAVE_SYS_UTSNAME_H
244 # include <sys/utsname.h>
245 #endif
246 #ifdef HAVE_TIME_H
247 # include <time.h>
248 #endif
249 #ifdef HAVE_SIGNAL_H
250 # include <signal.h>
251 #endif
252 #ifdef HAVE_SYSLOG_H
253 # include <syslog.h>
254 #endif
255 #ifdef HAVE_WCHAR_H
256 # include <wchar.h>
257 #endif
258 #ifdef HAVE_WCTYPE_H
259 # include <wctype.h>
260 #endif
261 ])
262
263 dnl PSI_LEMON()
264 dnl Declare $LEMON precious, and check for a `lemon` in $PATH.
265 AC_DEFUN(PSI_LEMON, [
266 AC_ARG_VAR(LEMON, The lemon parser generator of the SQLite project)
267 AC_PATH_PROG(LEMON, lemon, ./lemon)
268 PHP_SUBST(LEMON)
269 ])
270
271 dnl PSI_PKG_CONFIG()
272 dnl Check for `pkg-config` and add possible libjit and libffi directories to
273 dnl $PKG_CONFIG_PATH, because those libs often ship with headers etc. in
274 dnl arch-dependent locations.
275 AC_DEFUN([PSI_PKG_CONFIG], [
276 if test -z "$PKG_CONFIG"
277 then
278 AC_PATH_PROG(PKG_CONFIG, pkg-config, false)
279 fi
280 export PKG_CONFIG_PATH="$PHP_PSI_LIBFFI/lib/pkgconfig:$PHP_PSI_LIBJIT/lib/pkgconfig:$PKG_CONFIG_PATH"
281 ])
282
283 dnl PSI_SH_SIZEOF(type)
284 dnl expand to shell variable $ac_cv_sizeof_<TYPE>
285 AC_DEFUN([PSI_SH_SIZEOF], [$AS_TR_SH([ac_cv_sizeof_]$1)])
286
287 dnl PSI_SH_OFFSETOF(type)
288 dnl Expand to shell variable $ac_cv_offsetof_<TYPE>
289 AC_DEFUN([PSI_SH_OFFSETOF], [$AS_TR_SH([ac_cv_offsetof_]$1)])
290
291 dnl PSI_SH_ALIGNOF(type)
292 dnl Expand to shell variable $ac_cv_offsetof_<TYPE>
293 AC_DEFUN([PSI_SH_ALIGNOF], [$AS_TR_SH([ac_cv_alignof_]$1)])
294
295 dnl PSI_SH_TEST_SIZEOF(type)
296 dnl `if` condition to test if $ac_cv_sizeof_$1 is greater than 0.
297 AC_DEFUN([PSI_SH_TEST_SIZEOF], [test -n "$AS_TR_SH([ac_cv_sizeof_]$1)" && test "$AS_TR_SH([ac_cv_sizeof_]$1)" -gt 0])
298
299 dnl PSI_SH_TEST_ALIGNOF(type)
300 dnl `if` condition to test if $ac_cv_alignof_$1 is greater than 0.
301 AC_DEFUN([PSI_SH_TEST_ALIGNOF], [test -n "$AS_TR_SH([ac_cv_alignof_]$1)" && test "$AS_TR_SH([ac_cv_alignof_]$1)" -gt 0])
302
303 dnl PSI_CHECK_SIZEOF(type, special-includes)
304 dnl AC_CHECK_SIZEOF wrapper with PSI_INCLUDES
305 dnl Defines psi\\SIZEOF_<TYPE> pre-defined constant in $PSI_CONSTS.
306 AC_DEFUN(PSI_CHECK_SIZEOF, [
307 AC_CHECK_SIZEOF($1, [], PSI_INCLUDES
308 $2)
309 if PSI_SH_TEST_SIZEOF($1); then
310 psi_add_int_const "AS_TR_CPP([SIZEOF_]$1)" "$AS_TR_SH([ac_cv_sizeof_]$1)"
311 fi
312 ])
313
314 dnl PSI_CHECK_ALIGNOF(type, special-includes)
315 dnl AC_CHECK_ALIGNOF wrapper with PSI_INCLUDES
316 dnl Defines psi\\ALIGNOF_<TYPE> pre-defined constant in $PSI_CONSTS.
317 AC_DEFUN(PSI_CHECK_ALIGNOF, [
318 AC_CHECK_ALIGNOF($1, PSI_INCLUDES
319 $2)
320 if PSI_SH_TEST_ALIGNOF($1); then
321 psi_add_int_const "AS_TR_CPP([ALIGNOF_]$1)" "$AS_TR_SH([ac_cv_alignof_]$1)"
322 fi
323 ])
324
325 dnl PSI_CHECK_OFFSETOF(struct, element)
326 dnl Check the offset of a struct element, implemented in the similar manner
327 dnl like AC_CHECK_SIZEOF.
328 dnl AC_DEFINEs OFFSETOF_<STRUCT>_<ELEMENT>.
329 AC_DEFUN(PSI_CHECK_OFFSETOF, [
330 _AC_CACHE_CHECK_INT(
331 [offset of $2 in $1],
332 [AS_TR_SH([ac_cv_offsetof_$1_$2])],
333 [(long int) (offsetof ($1, $2))],
334 [PSI_INCLUDES],
335 [AC_MSG_FAILURE([cannot compute offsetof ($1, $2)])]
336 )
337 AC_DEFINE_UNQUOTED(
338 AS_TR_CPP(offsetof_$1_$2),
339 $AS_TR_SH([ac_cv_offsetof_$1_$2]),
340 [The offset of `$2' in `$1', as computed by offsetof.]
341 )
342 ])
343
344
345 dnl PSI_COMPUTE_STR(variable, string or expression)
346 dnl Compute a string constant value in a similar manner like AC_COMPUTE_INT.
347 AC_DEFUN(PSI_COMPUTE_STR, [
348 AC_TRY_RUN(
349 PSI_INCLUDES
350 [int main() {
351 return EOF == fputs($2, fopen("conftest.out", "w"));
352 }
353 ], [
354 eval $1=\\\"`cat conftest.out`\\\"
355 ])
356 ])
357
358 dnl PSI_CHECK_LIBJIT()
359 dnl Check for libjit in $PHP_PSI_LIBJIT or standard locations
360 dnl AC_DEFINEs HAVE_LIBJIT.
361 AC_DEFUN(PSI_CHECK_LIBJIT, [
362 AC_CACHE_CHECK(for libjit, psi_cv_libjit_dir, [
363 for psi_cv_libjit_dir in $PHP_PSI_LIBJIT {/usr{,/local},/opt}{,/libjit}
364 do
365 if test -e $psi_cv_libjit_dir/include/jit/jit.h
366 then
367 break
368 fi
369 psi_cv_libjit_dir=
370 done
371 ])
372 if test -n "$psi_cv_libjit_dir"
373 then
374 PHP_ADD_INCLUDE($psi_cv_libjit_dir/include)
375 PHP_ADD_LIBRARY_WITH_PATH(jit, $psi_cv_libjit_dir/$PHP_LIBDIR, PSI_SHARED_LIBADD)
376 AC_DEFINE(HAVE_LIBJIT, 1, Have libjit)
377 else
378 AC_MSG_WARN([Could not find libjit, please provide the base install path])
379 fi
380 ])
381
382 dnl PSI_CHECK_LIBFFI()
383 dnl Check for libffi with `pkg-config`. If that fails, `configure` looks into
384 dnl $PHP_PSI_LIBFFI or standard locations to find libjit deps.
385 dnl Checks for availability of recent closure API:
386 dnl \ffi_closure_alloc and \ffi_prep_closure.
387 dnl Checks for availability of recent vararg API:
388 dnl \ffi_prep_cif_var.
389 dnl AC_DEFINEs HAVE_LIBFFI, PSI_HAVE_FFI_CLOSURE_ALLOC,
390 dnl PSI_HAVE_FFI_PREP_CLOSURE and PSI_HAVE_FFO_PREP_VIF_VAR.
391 AC_DEFUN(PSI_CHECK_LIBFFI, [
392 AC_REQUIRE([PSI_PKG_CONFIG])dnl
393
394 AC_CACHE_CHECK(for libffi through pkg-config, psi_cv_libffi, [
395 if $PKG_CONFIG --exists libffi
396 then
397 psi_cv_libffi=true
398 else
399 psi_cv_libffi=false
400 fi])
401
402 if $psi_cv_libffi
403 then
404 AC_MSG_CHECKING(for libffi)
405 psi_cv_libffi_dir=`$PKG_CONFIG --variable=prefix libffi`
406 AC_MSG_RESULT($psi_cv_libffi_dir)
407 PHP_EVAL_INCLINE(`$PKG_CONFIG --cflags libffi`)
408 PHP_EVAL_LIBLINE(`$PKG_CONFIG --libs libffi`, PSI_SHARED_LIBADD)
409 AC_DEFINE(HAVE_LIBFFI, 1, Have libffi)
410 else
411 AC_CACHE_CHECK(for libffi, psi_cv_libffi_dir, [
412 for psi_cv_libffi_dir in $PHP_PSI_LIBFFI {/usr{,/local},/opt}{,/libffi}
413 do
414 if test -e $psi_cv_libffi_dir/include/ffi/ffi.h
415 then
416 break
417 fi
418 psi_cv_libffi_dir=
419 done])
420 if test -n "$psi_cv_libffi_dir"
421 then
422 PHP_ADD_INCLUDE($psi_cv_libffi_dir/include/ffi)
423 PHP_ADD_LIBRARY_WITH_PATH(ffi, $psi_cv_libffi_dir/$PHP_LIBDIR, PSI_SHARED_LIBADD)
424 AC_DEFINE(HAVE_LIBFFI, 1, Have libffi)
425 else
426 AC_MSG_WARN([Could not find libffi, please provide the base install path])
427 fi
428 fi
429 PHP_CHECK_LIBRARY(ffi, ffi_closure_alloc, [
430 PHP_CHECK_LIBRARY(ffi, ffi_prep_closure_loc, [
431 AC_DEFINE(PSI_HAVE_FFI_PREP_CLOSURE_LOC, 1, [ ])
432 ], [], -L$psi_cv_libffi_dir/$PHP_LIBDIR)
433 AC_DEFINE(PSI_HAVE_FFI_CLOSURE_ALLOC, 1, [ ])
434 ], [
435 PHP_CHECK_LIBRARY(ffi, ffi_prep_closure, [
436 AC_CHECK_HEADERS(sys/mman.h)
437 PHP_CHECK_FUNC(mmap)
438 AC_DEFINE(PSI_HAVE_FFI_PREP_CLOSURE, 1, [ ])
439 ], [
440 ], -L$psi_cv_libffi_dir/$PHP_LIBDIR)
441 ], -L$psi_cv_libffi_dir/$PHP_LIBDIR)
442 PHP_CHECK_LIBRARY(ffi, ffi_prep_cif_var, [
443 AC_DEFINE(PSI_HAVE_FFI_PREP_CIF_VAR, 1, [ ])
444 ], [
445 ], -L$psi_cv_libffi_dir/$PHP_LIBDIR)
446 ])