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