fix ptrdiff_t and _GNU_SOURCE detection
[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, [
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_H
110 #define PSI_STDINC_H
111
112 #ifdef HAVE_CONFIG_H
113 # include "config.h"
114 #else
115 # include "php_config.h"
116 #endif
117
118 #ifndef _GNU_SOURCE
119 # define _GNU_SOURCE
120 #endif
121 #ifndef _REENTRANT
122 # define _REENTRANT
123 #endif
124
125 PSI_INCLUDES
126 #endif
127 EOF
128 for i in $PSI_STDTYPES $PSI_TYPES $PSI_STRUCTS $PSI_UNIONS $PSI_CONSTS $PSI_REDIRS $PSI_DECLS $PSI_VA_DECLS $PSI_FN_DECLS; do
129 cat >>$i <<EOF
130 {0}
131 };
132 EOF
133 done
134 ])
135
136 dnl PSI_SH_CONFIG_POSIX_ENABLED(section)
137 dnl Expand to $psi_config_posix_<section>
138 AC_DEFUN(PSI_SH_CONFIG_POSIX_ENABLED, [$AS_TR_SH([psi_config_posix_]$1)])
139
140 dnl PSI_CONFIG_POSIX_ENABLED(section, action-if-yes, action-if-not)
141 dnl Internal. Used to check if --enable-psi-posix=section was given.
142 AC_DEFUN(PSI_CONFIG_POSIX_ENABLED, [
143 AS_TR_SH([psi_config_posix_]$1)=false
144 case "$PHP_PSI_POSIX" in
145 yes|all)
146 AS_TR_SH([psi_config_posix_]$1)=true
147 ;;
148 *)
149 if expr "$PHP_PSI_POSIX" : '\b$1\b' >/dev/null; then
150 AS_TR_SH([psi_config_posix_]$1)=true
151 fi
152 ;;
153 esac
154 if $AS_TR_SH([psi_config_posix_]$1); then
155 ifelse([$2],,:,[$2])
156 else
157 ifelse([$3],,:,[$3])
158 fi
159 ])
160
161 dnl PSI_TEST_POSIX_ENABLED(section, action-if-yes, action-if-not)
162 dnl Shell-if test if PSI POSIX section was configured.
163 AC_DEFUN(PSI_SH_TEST_POSIX_ENABLED, [
164 if test "PSI_SH_CONFIG_POSIX_ENABLED([$1])" && $PSI_SH_CONFIG_POSIX_ENABLED([$1]); then
165 ifelse([$2],,:,[$2])
166 else
167 ifelse([$3],,:,[$3])
168 fi
169 ])
170
171 dnl PSI_CONFIG_POSIX(section, headers)
172 AC_DEFUN(PSI_CONFIG_POSIX, [
173 PSI_CONFIG_POSIX_ENABLED($1, [
174 PHP_CONFIGURE_PART(Configuring PSI POSIX: $1)
175 ifelse([$2],,:,[AC_CHECK_HEADERS($2)])
176 ], [
177 return 0
178 ])
179 ])
180
181 AC_DEFUN(PSI_PTHREAD_ONCE, [
182 AX_PTHREAD([
183 LIBS="$PTHREAD_LIBS $LIBS"
184 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
185 ])
186 ])
187 AC_DEFUN(PSI_PTHREAD, [
188 AC_REQUIRE([PSI_PTHREAD_ONCE])
189 ])
190
191 dnl PSI_INCLUDES()
192 dnl Expands to a complete list of include statements including
193 dnl AC_INCLUDES_DEFAULT().
194 AC_DEFUN(PSI_INCLUDES, [dnl
195 #define PSI_INCLUDES
196 #ifndef _GNU_SOURCE
197 # define _GNU_SOURCE
198 #endif
199 #ifndef _REENTRANT
200 # define _REENTRANT
201 #endif
202 AC_INCLUDES_DEFAULT()
203 #ifdef HAVE_STDBOOL_H
204 # include <stdbool.h>
205 #else
206 # ifndef HAVE__BOOL
207 # ifdef __cplusplus
208 typedef bool _Bool;
209 # else
210 # define _Bool signed char
211 # endif
212 # endif
213 # define bool _Bool
214 # define false 0
215 # define true 1
216 # define __bool_true_false_are_defined 1
217 #endif
218 #ifdef HAVE_ERRNO_H
219 # include <errno.h>
220 #endif
221 #ifdef HAVE_GLOB_H
222 # include <glob.h>
223 #endif
224 #ifdef HAVE_LOCALE_H
225 # include <locale.h>
226 #endif
227 #ifdef HAVE_XLOCALE_H
228 # include <xlocale.h>
229 #endif
230 #ifdef HAVE_NETINET_IN_H
231 # include <netinet/in.h>
232 #endif
233 #ifdef HAVE_NETINET_TCP_H
234 # include <netinet/tcp.h>
235 #endif
236 #ifdef HAVE_ARPA_NAMESER_H
237 # include <arpa/nameser.h>
238 #endif
239 #ifdef HAVE_FCNTL_H
240 # include <fcntl.h>
241 #endif
242 #ifdef HAVE_RELIC_H
243 # include <relic.h>
244 #elif HAVE_NDBM_H
245 # include <ndbm.h>
246 #elif HAVE_GDBM_NDBM_H
247 # include <gdbm-ndbm.h>
248 #endif
249 #ifdef HAVE_NETDB_H
250 # include <netdb.h>
251 #endif
252 #ifdef HAVE_POLL_H
253 # include <poll.h>
254 #endif
255 #ifdef HAVE_RESOLV_H
256 # include <resolv.h>
257 #endif
258 #ifdef HAVE_SYS_SELECT_H
259 # include <sys/select.h>
260 #endif
261 #ifdef HAVE_SYS_SOCKET_H
262 # include <sys/socket.h>
263 #endif
264 #ifdef HAVE_SYS_TIME_H
265 # include <sys/time.h>
266 #endif
267 #ifdef HAVE_SYS_TIMES_H
268 # include <sys/times.h>
269 #endif
270 #ifdef HAVE_SYS_UIO_H
271 # include <sys/uio.h>
272 #endif
273 #ifdef HAVE_SYS_UTSNAME_H
274 # include <sys/utsname.h>
275 #endif
276 #ifdef HAVE_TIME_H
277 # include <time.h>
278 #endif
279 #ifdef HAVE_SIGNAL_H
280 # include <signal.h>
281 #endif
282 #ifdef HAVE_SYSLOG_H
283 # include <syslog.h>
284 #endif
285 #ifdef HAVE_WCHAR_H
286 # include <wchar.h>
287 #endif
288 #ifdef HAVE_WCTYPE_H
289 # include <wctype.h>
290 #endif
291 ])
292
293 dnl PSI_LEMON()
294 dnl Declare $LEMON precious, and check for a `lemon` in $PATH.
295 AC_DEFUN(PSI_LEMON, [
296 AC_ARG_VAR(LEMON, The lemon parser generator of the SQLite project)
297 AC_PATH_PROG(LEMON, lemon, ./lemon)
298 PHP_SUBST(LEMON)
299 ])
300
301 dnl PSI_PKG_CONFIG()
302 dnl Check for `pkg-config` and add possible libjit and libffi directories to
303 dnl $PKG_CONFIG_PATH, because those libs often ship with headers etc. in
304 dnl arch-dependent locations.
305 AC_DEFUN([PSI_PKG_CONFIG], [
306 if test -z "$PKG_CONFIG"
307 then
308 AC_PATH_PROG(PKG_CONFIG, pkg-config, false)
309 fi
310 export PKG_CONFIG_PATH="$PHP_PSI_LIBFFI/lib/pkgconfig:$PHP_PSI_LIBJIT/lib/pkgconfig:$PKG_CONFIG_PATH"
311 ])
312
313 dnl PSI_SH_SIZEOF(type)
314 dnl expand to shell variable $ac_cv_sizeof_<TYPE>
315 AC_DEFUN([PSI_SH_SIZEOF], [$AS_TR_SH([ac_cv_sizeof_]$1)])
316
317 dnl PSI_SH_OFFSETOF(type)
318 dnl Expand to shell variable $ac_cv_offsetof_<TYPE>
319 AC_DEFUN([PSI_SH_OFFSETOF], [$AS_TR_SH([ac_cv_offsetof_]$1)])
320
321 dnl PSI_SH_ALIGNOF(type)
322 dnl Expand to shell variable $ac_cv_offsetof_<TYPE>
323 AC_DEFUN([PSI_SH_ALIGNOF], [$AS_TR_SH([ac_cv_alignof_]$1)])
324
325 dnl PSI_SH_TEST_SIZEOF(type)
326 dnl `if` condition to test if $ac_cv_sizeof_$1 is greater than 0.
327 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])
328
329 dnl PSI_SH_TEST_ALIGNOF(type)
330 dnl `if` condition to test if $ac_cv_alignof_$1 is greater than 0.
331 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])
332
333 dnl PSI_CHECK_SIZEOF(type, special-includes)
334 dnl AC_CHECK_SIZEOF wrapper with PSI_INCLUDES
335 dnl Defines psi\\SIZEOF_<TYPE> pre-defined constant in $PSI_CONSTS.
336 AC_DEFUN(PSI_CHECK_SIZEOF, [
337 AC_CHECK_SIZEOF($1, [], PSI_INCLUDES
338 $2)
339 if PSI_SH_TEST_SIZEOF($1); then
340 psi_add_int_const "AS_TR_CPP([SIZEOF_]$1)" "$AS_TR_SH([ac_cv_sizeof_]$1)"
341 fi
342 ])
343
344 dnl PSI_CHECK_ALIGNOF(type, special-includes)
345 dnl AC_CHECK_ALIGNOF wrapper with PSI_INCLUDES
346 dnl Defines psi\\ALIGNOF_<TYPE> pre-defined constant in $PSI_CONSTS.
347 AC_DEFUN(PSI_CHECK_ALIGNOF, [
348 AC_CHECK_ALIGNOF($1, PSI_INCLUDES
349 $2)
350 if PSI_SH_TEST_ALIGNOF($1); then
351 psi_add_int_const "AS_TR_CPP([ALIGNOF_]$1)" "$AS_TR_SH([ac_cv_alignof_]$1)"
352 fi
353 ])
354
355 dnl PSI_CHECK_OFFSETOF(struct, element)
356 dnl Check the offset of a struct element, implemented in the similar manner
357 dnl like AC_CHECK_SIZEOF.
358 dnl AC_DEFINEs OFFSETOF_<STRUCT>_<ELEMENT>.
359 AC_DEFUN(PSI_CHECK_OFFSETOF, [
360 _AC_CACHE_CHECK_INT(
361 [offset of $2 in $1],
362 [AS_TR_SH([ac_cv_offsetof_$1_$2])],
363 [(long int) (offsetof ($1, $2))],
364 [PSI_INCLUDES],
365 [AC_MSG_FAILURE([cannot compute offsetof ($1, $2)])]
366 )
367 AC_DEFINE_UNQUOTED(
368 AS_TR_CPP(offsetof_$1_$2),
369 $AS_TR_SH([ac_cv_offsetof_$1_$2]),
370 [The offset of `$2' in `$1', as computed by offsetof.]
371 )
372 ])
373
374
375 dnl PSI_COMPUTE_STR(variable, string or expression)
376 dnl Compute a string constant value in a similar manner like AC_COMPUTE_INT.
377 AC_DEFUN(PSI_COMPUTE_STR, [
378 AC_TRY_RUN(
379 PSI_INCLUDES
380 [int main() {
381 return EOF == fputs($2, fopen("conftest.out", "w"));
382 }
383 ], [
384 eval $1=\\\"`cat conftest.out`\\\"
385 ])
386 ])
387
388 dnl PSI_CHECK_LIBJIT()
389 dnl Check for libjit in $PHP_PSI_LIBJIT or standard locations
390 dnl AC_DEFINEs HAVE_LIBJIT.
391 AC_DEFUN(PSI_CHECK_LIBJIT, [
392 AC_CACHE_CHECK(for libjit, psi_cv_libjit_dir, [
393 for psi_cv_libjit_dir in $PHP_PSI_LIBJIT {/usr{,/local},/opt}{,/libjit}
394 do
395 if test -e $psi_cv_libjit_dir/include/jit/jit.h
396 then
397 break
398 fi
399 psi_cv_libjit_dir=
400 done
401 ])
402 if test -n "$psi_cv_libjit_dir"
403 then
404 PHP_ADD_INCLUDE($psi_cv_libjit_dir/include)
405 PHP_ADD_LIBRARY_WITH_PATH(jit, $psi_cv_libjit_dir/$PHP_LIBDIR, PSI_SHARED_LIBADD)
406 AC_DEFINE(HAVE_LIBJIT, 1, Have libjit)
407 else
408 AC_MSG_WARN([Could not find libjit, please provide the base install path])
409 fi
410 ])
411
412 dnl PSI_CHECK_LIBFFI()
413 dnl Check for libffi with `pkg-config`. If that fails, `configure` looks into
414 dnl $PHP_PSI_LIBFFI or standard locations to find libjit deps.
415 dnl Checks for availability of recent closure API:
416 dnl \ffi_closure_alloc and \ffi_prep_closure.
417 dnl Checks for availability of recent vararg API:
418 dnl \ffi_prep_cif_var.
419 dnl AC_DEFINEs HAVE_LIBFFI, PSI_HAVE_FFI_CLOSURE_ALLOC,
420 dnl PSI_HAVE_FFI_PREP_CLOSURE and PSI_HAVE_FFO_PREP_VIF_VAR.
421 AC_DEFUN(PSI_CHECK_LIBFFI, [
422 AC_REQUIRE([PSI_PKG_CONFIG])dnl
423
424 AC_CACHE_CHECK(for libffi through pkg-config, psi_cv_libffi, [
425 if $PKG_CONFIG --exists libffi
426 then
427 psi_cv_libffi=true
428 else
429 psi_cv_libffi=false
430 fi])
431
432 if $psi_cv_libffi
433 then
434 AC_MSG_CHECKING(for libffi)
435 psi_cv_libffi_dir=`$PKG_CONFIG --variable=prefix libffi`
436 AC_MSG_RESULT($psi_cv_libffi_dir)
437 PHP_EVAL_INCLINE(`$PKG_CONFIG --cflags libffi`)
438 PHP_EVAL_LIBLINE(`$PKG_CONFIG --libs libffi`, PSI_SHARED_LIBADD)
439 AC_DEFINE(HAVE_LIBFFI, 1, Have libffi)
440 AC_DEFINE_UNQUOTED([PHP_PSI_LIBFFI_VERSION], ["`$PKG_CONFIG --modversion libffi`"], [libffi version])
441 else
442 AC_CACHE_CHECK(for libffi, psi_cv_libffi_dir, [
443 for psi_cv_libffi_dir in $PHP_PSI_LIBFFI {/usr{,/local},/opt}{,/libffi}
444 do
445 if test -e $psi_cv_libffi_dir/include/ffi/ffi.h
446 then
447 break
448 fi
449 psi_cv_libffi_dir=
450 done])
451 if test -n "$psi_cv_libffi_dir"
452 then
453 PHP_ADD_INCLUDE($psi_cv_libffi_dir/include/ffi)
454 PHP_ADD_LIBRARY_WITH_PATH(ffi, $psi_cv_libffi_dir/$PHP_LIBDIR, PSI_SHARED_LIBADD)
455 AC_DEFINE(HAVE_LIBFFI, 1, Have libffi)
456 else
457 AC_MSG_WARN([Could not find libffi, please provide the base install path])
458 fi
459 fi
460 PHP_CHECK_LIBRARY(ffi, ffi_closure_alloc, [
461 PHP_CHECK_LIBRARY(ffi, ffi_prep_closure_loc, [
462 AC_DEFINE(PSI_HAVE_FFI_PREP_CLOSURE_LOC, 1, [ ])
463 ], [], -L$psi_cv_libffi_dir/$PHP_LIBDIR)
464 AC_DEFINE(PSI_HAVE_FFI_CLOSURE_ALLOC, 1, [ ])
465 ], [
466 PHP_CHECK_LIBRARY(ffi, ffi_prep_closure, [
467 AC_CHECK_HEADERS(sys/mman.h)
468 PHP_CHECK_FUNC(mmap)
469 AC_DEFINE(PSI_HAVE_FFI_PREP_CLOSURE, 1, [ ])
470 ], [
471 ], -L$psi_cv_libffi_dir/$PHP_LIBDIR)
472 ], -L$psi_cv_libffi_dir/$PHP_LIBDIR)
473 PHP_CHECK_LIBRARY(ffi, ffi_prep_cif_var, [
474 AC_DEFINE(PSI_HAVE_FFI_PREP_CIF_VAR, 1, [ ])
475 ], [
476 ], -L$psi_cv_libffi_dir/$PHP_LIBDIR)
477 ])