xref: /php-src/Zend/Zend.m4 (revision f952263b)
1dnl This file contains Zend specific autoconf macros.
2
3dnl
4dnl ZEND_CHECK_FLOAT_PRECISION
5dnl
6dnl x87 floating point internal precision control checks
7dnl See: https://wiki.php.net/rfc/rounding
8dnl
9AC_DEFUN([ZEND_CHECK_FLOAT_PRECISION], [dnl
10AC_CACHE_CHECK([for usable _FPU_SETCW],
11  [php_cv_have__fpu_setcw],
12  [AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <fpu_control.h>], [dnl
13    fpu_control_t fpu_oldcw, fpu_cw;
14    volatile double result;
15    double a = 2877.0;
16    volatile double b = 1000000.0;
17
18    _FPU_GETCW(fpu_oldcw);
19    fpu_cw = (fpu_oldcw & ~_FPU_EXTENDED & ~_FPU_SINGLE) | _FPU_DOUBLE;
20    _FPU_SETCW(fpu_cw);
21    result = a / b;
22    _FPU_SETCW(fpu_oldcw);
23    (void)result;
24  ])],
25  [php_cv_have__fpu_setcw=yes],
26  [php_cv_have__fpu_setcw=no])])
27AS_VAR_IF([php_cv_have__fpu_setcw], [yes],
28  [AC_DEFINE([HAVE__FPU_SETCW], [1],
29    [Define to 1 if _FPU_SETCW is present and usable.])])
30
31AC_CACHE_CHECK([for usable fpsetprec],
32  [php_cv_have_fpsetprec],
33  [AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <machine/ieeefp.h>], [dnl
34    fp_prec_t fpu_oldprec;
35    volatile double result;
36    double a = 2877.0;
37    volatile double b = 1000000.0;
38
39    fpu_oldprec = fpgetprec();
40    fpsetprec(FP_PD);
41    result = a / b;
42    fpsetprec(fpu_oldprec);
43    (void)result;
44  ])],
45  [php_cv_have_fpsetprec=yes],
46  [php_cv_have_fpsetprec=no])])
47AS_VAR_IF([php_cv_have_fpsetprec], [yes],
48  [AC_DEFINE([HAVE_FPSETPREC], [1],
49    [Define to 1 if fpsetprec is present and usable.])])
50
51AC_CACHE_CHECK([for usable _controlfp],
52  [php_cv_have__controlfp],
53  [AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <float.h>], [dnl
54    unsigned int fpu_oldcw;
55    volatile double result;
56    double a = 2877.0;
57    volatile double b = 1000000.0;
58
59    fpu_oldcw = _controlfp(0, 0);
60    _controlfp(_PC_53, _MCW_PC);
61    result = a / b;
62    _controlfp(fpu_oldcw, _MCW_PC);
63    (void)result;
64  ])],
65  [php_cv_have__controlfp=yes],
66  [php_cv_have__controlfp=no])])
67AS_VAR_IF([php_cv_have__controlfp], [yes],
68  [AC_DEFINE([HAVE__CONTROLFP], [1],
69    [Define to 1 if _controlfp is present and usable.])])
70
71AC_CACHE_CHECK([for usable _controlfp_s],
72  [php_cv_have__controlfp_s],
73  [AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <float.h>], [dnl
74    unsigned int fpu_oldcw, fpu_cw;
75    volatile double result;
76    double a = 2877.0;
77    volatile double b = 1000000.0;
78
79    _controlfp_s(&fpu_cw, 0, 0);
80    fpu_oldcw = fpu_cw;
81    _controlfp_s(&fpu_cw, _PC_53, _MCW_PC);
82    result = a / b;
83    _controlfp_s(&fpu_cw, fpu_oldcw, _MCW_PC);
84    (void)result;
85  ])],
86  [php_cv_have__controlfp_s=yes],
87  [php_cv_have__controlfp_s=no])])
88AS_VAR_IF([php_cv_have__controlfp_s], [yes],
89  [AC_DEFINE([HAVE__CONTROLFP_S], [1],
90    [Define to 1 if _controlfp_s is present and usable.])])
91
92AC_CACHE_CHECK([whether FPU control word can be manipulated by inline assembler],
93  [php_cv_have_fpu_inline_asm_x86],
94  [AC_LINK_IFELSE([AC_LANG_PROGRAM([], [dnl
95    unsigned int oldcw, cw;
96    volatile double result;
97    double a = 2877.0;
98    volatile double b = 1000000.0;
99
100    __asm__ __volatile__ ("fnstcw %0" : "=m" (*&oldcw));
101    cw = (oldcw & ~0x0 & ~0x300) | 0x200;
102    __asm__ __volatile__ ("fldcw %0" : : "m" (*&cw));
103    result = a / b;
104    __asm__ __volatile__ ("fldcw %0" : : "m" (*&oldcw));
105    (void)result;
106  ])],
107  [php_cv_have_fpu_inline_asm_x86=yes],
108  [php_cv_have_fpu_inline_asm_x86=no])])
109AS_VAR_IF([php_cv_have_fpu_inline_asm_x86], [yes],
110  [AC_DEFINE([HAVE_FPU_INLINE_ASM_X86], [1],
111    [Define to 1 if FPU control word can be manipulated by inline assembler.])])
112])
113
114dnl
115dnl ZEND_DLSYM_CHECK
116dnl
117dnl Ugly hack to check if dlsym() requires a leading underscore in symbol name.
118dnl
119AC_DEFUN([ZEND_DLSYM_CHECK], [dnl
120AC_MSG_CHECKING([whether dlsym() requires a leading underscore in symbol names])
121_LT_AC_TRY_DLOPEN_SELF([AC_MSG_RESULT([no])], [
122  AC_MSG_RESULT([yes])
123  AC_DEFINE([DLSYM_NEEDS_UNDERSCORE], [1],
124    [Define to 1 if 'dlsym()' requires a leading underscore in symbol names.])
125], [AC_MSG_RESULT([no])], [])
126])
127
128dnl
129dnl ZEND_INIT
130dnl
131dnl Configure checks and initialization specific for the Zend engine library.
132dnl
133AC_DEFUN([ZEND_INIT], [dnl
134AC_REQUIRE([AC_PROG_CC])
135
136AC_CHECK_HEADERS(m4_normalize([
137  cpuid.h
138  libproc.h
139]))
140
141dnl Check for library functions.
142AC_CHECK_FUNCS(m4_normalize([
143  getpid
144  gettid
145  kill
146  mremap
147  pthread_attr_get_np
148  pthread_attr_getstack
149  pthread_get_stackaddr_np
150  pthread_getattr_np
151  pthread_stackseg_np
152]))
153
154dnl
155dnl Check for sigsetjmp. If sigsetjmp is defined as a macro, use AC_CHECK_DECL
156dnl as a fallback since AC_CHECK_FUNC cannot detect macros.
157dnl
158AC_CHECK_FUNC([sigsetjmp],,
159  [AC_CHECK_DECL([sigsetjmp],,
160    [AC_MSG_FAILURE([Required sigsetjmp not found.])],
161    [#include <setjmp.h>])])
162
163ZEND_CHECK_STACK_DIRECTION
164ZEND_CHECK_FLOAT_PRECISION
165ZEND_DLSYM_CHECK
166ZEND_CHECK_GLOBAL_REGISTER_VARIABLES
167ZEND_CHECK_CPUID_COUNT
168
169AC_MSG_CHECKING([whether to enable thread safety])
170AC_MSG_RESULT([$ZEND_ZTS])
171AS_VAR_IF([ZEND_ZTS], [yes], [
172  AC_DEFINE([ZTS], [1], [Define to 1 if thread safety (ZTS) is enabled.])
173  AS_VAR_APPEND([CFLAGS], [" -DZTS"])
174])
175
176AC_MSG_CHECKING([whether to enable Zend debugging])
177AC_MSG_RESULT([$ZEND_DEBUG])
178AH_TEMPLATE([ZEND_DEBUG],
179  [Define to 1 if debugging is enabled, and to 0 if not.])
180AS_VAR_IF([ZEND_DEBUG], [yes], [
181  AC_DEFINE([ZEND_DEBUG], [1])
182  echo " $CFLAGS" | grep ' -g' >/dev/null || CFLAGS="$CFLAGS -g"
183], [AC_DEFINE([ZEND_DEBUG], [0])])
184
185AS_VAR_IF([GCC], [yes],
186  [CFLAGS="-Wall -Wextra -Wno-unused-parameter -Wno-sign-compare $CFLAGS"])
187
188dnl Check if compiler supports -Wno-clobbered (only GCC).
189AX_CHECK_COMPILE_FLAG([-Wno-clobbered],
190  [CFLAGS="-Wno-clobbered $CFLAGS"],,
191  [-Werror])
192dnl Check for support for implicit fallthrough level 1, also add after previous
193dnl CFLAGS as level 3 is enabled in -Wextra.
194AX_CHECK_COMPILE_FLAG([-Wimplicit-fallthrough=1],
195  [CFLAGS="$CFLAGS -Wimplicit-fallthrough=1"],,
196  [-Werror])
197AX_CHECK_COMPILE_FLAG([-Wduplicated-cond],
198  [CFLAGS="-Wduplicated-cond $CFLAGS"],,
199  [-Werror])
200AX_CHECK_COMPILE_FLAG([-Wlogical-op],
201  [CFLAGS="-Wlogical-op $CFLAGS"],,
202  [-Werror])
203AX_CHECK_COMPILE_FLAG([-Wformat-truncation],
204  [CFLAGS="-Wformat-truncation $CFLAGS"],,
205  [-Werror])
206AX_CHECK_COMPILE_FLAG([-Wstrict-prototypes],
207  [CFLAGS="-Wstrict-prototypes $CFLAGS"],,
208  [-Werror])
209AX_CHECK_COMPILE_FLAG([-fno-common],
210  [CFLAGS="-fno-common $CFLAGS"],,
211  [-Werror])
212
213ZEND_CHECK_ALIGNMENT
214ZEND_CHECK_SIGNALS
215ZEND_CHECK_MAX_EXECUTION_TIMERS
216])
217
218dnl
219dnl ZEND_CHECK_STACK_DIRECTION
220dnl
221dnl Check whether the stack grows downwards, assumes contiguous stack.
222dnl
223AC_DEFUN([ZEND_CHECK_STACK_DIRECTION],
224[AC_CACHE_CHECK([whether the stack grows downwards],
225  [php_cv_have_stack_limit],
226  [AC_RUN_IFELSE([AC_LANG_SOURCE([dnl
227#include <stdint.h>
228
229#ifdef __has_builtin
230# if __has_builtin(__builtin_frame_address)
231#  define builtin_frame_address __builtin_frame_address(0)
232# endif
233#endif
234
235int (*volatile f)(uintptr_t);
236
237int stack_grows_downwards(uintptr_t arg) {
238#ifdef builtin_frame_address
239  uintptr_t addr = (uintptr_t)builtin_frame_address;
240#else
241  int local;
242  uintptr_t addr = (uintptr_t)&local;
243#endif
244
245  return addr < arg;
246}
247
248int main(void) {
249#ifdef builtin_frame_address
250  uintptr_t addr = (uintptr_t)builtin_frame_address;
251#else
252  int local;
253  uintptr_t addr = (uintptr_t)&local;
254#endif
255
256  f = stack_grows_downwards;
257  return f(addr) ? 0 : 1;
258}])],
259  [php_cv_have_stack_limit=yes],
260  [php_cv_have_stack_limit=no],
261  [php_cv_have_stack_limit=no])])
262AS_VAR_IF([php_cv_have_stack_limit], [yes],
263  [AC_DEFINE([ZEND_CHECK_STACK_LIMIT], [1],
264    [Define to 1 if checking the stack limit is supported.])])
265])
266
267dnl
268dnl ZEND_CHECK_GLOBAL_REGISTER_VARIABLES
269dnl
270dnl Check whether to enable global register variables if supported.
271dnl
272AC_DEFUN([ZEND_CHECK_GLOBAL_REGISTER_VARIABLES], [dnl
273AC_ARG_ENABLE([gcc-global-regs],
274  [AS_HELP_STRING([--disable-gcc-global-regs],
275    [Disable GCC global register variables])],
276  [ZEND_GCC_GLOBAL_REGS=$enableval],
277  [ZEND_GCC_GLOBAL_REGS=yes])
278
279AS_VAR_IF([ZEND_GCC_GLOBAL_REGS], [no],,
280  [AC_CACHE_CHECK([whether system supports global register variables],
281    [php_cv_have_global_register_vars],
282    [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
283#if defined(__GNUC__)
284# define ZEND_GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
285#else
286# define ZEND_GCC_VERSION 0
287#endif
288#if defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(i386)
289# define ZEND_VM_FP_GLOBAL_REG "%esi"
290# define ZEND_VM_IP_GLOBAL_REG "%edi"
291#elif defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(__x86_64__)
292# define ZEND_VM_FP_GLOBAL_REG "%r14"
293# define ZEND_VM_IP_GLOBAL_REG "%r15"
294#elif defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(__powerpc64__)
295# define ZEND_VM_FP_GLOBAL_REG "r28"
296# define ZEND_VM_IP_GLOBAL_REG "r29"
297#elif defined(__IBMC__) && ZEND_GCC_VERSION >= 4002 && defined(__powerpc64__)
298# define ZEND_VM_FP_GLOBAL_REG "r28"
299# define ZEND_VM_IP_GLOBAL_REG "r29"
300#elif defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(__aarch64__)
301# define ZEND_VM_FP_GLOBAL_REG "x27"
302# define ZEND_VM_IP_GLOBAL_REG "x28"
303#elif defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(__riscv) && __riscv_xlen == 64
304# define ZEND_VM_FP_GLOBAL_REG "x18"
305# define ZEND_VM_IP_GLOBAL_REG "x19"
306#else
307# error "global register variables are not supported"
308#endif
309typedef int (*opcode_handler_t)(void);
310register void *FP  __asm__(ZEND_VM_FP_GLOBAL_REG);
311register const opcode_handler_t *IP __asm__(ZEND_VM_IP_GLOBAL_REG);
312int emu(const opcode_handler_t *ip, void *fp) {
313  const opcode_handler_t *orig_ip = IP;
314  void *orig_fp = FP;
315  IP = ip;
316  FP = fp;
317  while ((*ip)());
318  FP = orig_fp;
319  IP = orig_ip;
320}], [])],
321  [php_cv_have_global_register_vars=yes],
322  [php_cv_have_global_register_vars=no])
323])
324AS_VAR_IF([php_cv_have_global_register_vars], [yes],
325  [AC_DEFINE([HAVE_GCC_GLOBAL_REGS], [1],
326    [Define to 1 if the target system has support for global register
327    variables.])],
328  [ZEND_GCC_GLOBAL_REGS=no])
329])
330AC_MSG_CHECKING([whether to enable global register variables support])
331AC_MSG_RESULT([$ZEND_GCC_GLOBAL_REGS])
332])
333
334dnl
335dnl ZEND_CHECK_CPUID_COUNT
336dnl
337dnl Check whether __cpuid_count is available.
338dnl
339AC_DEFUN([ZEND_CHECK_CPUID_COUNT],
340[AC_CACHE_CHECK([whether __cpuid_count is available],
341  [php_cv_have___cpuid_count],
342  [AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <cpuid.h>], [dnl
343    unsigned eax, ebx, ecx, edx;
344    __cpuid_count(0, 0, eax, ebx, ecx, edx);
345  ])],
346  [php_cv_have___cpuid_count=yes],
347  [php_cv_have___cpuid_count=no])])
348AS_VAR_IF([php_cv_have___cpuid_count], [yes],
349  [AC_DEFINE([HAVE_CPUID_COUNT], [1],
350    [Define to 1 if '__cpuid_count' is available.])])
351])
352
353dnl
354dnl ZEND_CHECK_ALIGNMENT
355dnl
356dnl Test and set the alignment defines for the Zend memory manager (ZEND_MM).
357dnl This also does the logarithmic test.
358dnl
359AC_DEFUN([ZEND_CHECK_ALIGNMENT],
360[AC_CACHE_CHECK([for Zend memory manager alignment and log values],
361[php_cv_align_mm],
362[AC_RUN_IFELSE([AC_LANG_SOURCE([
363#include <stdio.h>
364#include <stdlib.h>
365
366typedef union _mm_align_test {
367  void *ptr;
368  double dbl;
369  long lng;
370} mm_align_test;
371
372#if (defined (__GNUC__) && __GNUC__ >= 2)
373#define ZEND_MM_ALIGNMENT (__alignof__ (mm_align_test))
374#else
375#define ZEND_MM_ALIGNMENT (sizeof(mm_align_test))
376#endif
377
378int main(void)
379{
380  size_t i = ZEND_MM_ALIGNMENT;
381  int zeros = 0;
382  FILE *fp;
383
384  while (i & ~0x1) {
385    zeros++;
386    i = i >> 1;
387  }
388
389  fp = fopen("conftest.zend", "w");
390  fprintf(fp, "(size_t)%zu (size_t)%d %d\n",
391    ZEND_MM_ALIGNMENT, zeros, ZEND_MM_ALIGNMENT < 4);
392  fclose(fp);
393
394  return 0;
395}
396])],
397  [php_cv_align_mm=$(cat conftest.zend)],
398  [php_cv_align_mm=failed],
399  [php_cv_align_mm="(size_t)8 (size_t)3 0"])])
400AS_VAR_IF([php_cv_align_mm], [failed],
401  [AC_MSG_FAILURE([ZEND_MM alignment defines failed.])],
402  [zend_mm_alignment=$(echo $php_cv_align_mm | cut -d ' ' -f 1)
403  zend_mm_alignment_log2=$(echo $php_cv_align_mm | cut -d ' ' -f 2)
404  zend_mm_8byte_realign=$(echo $php_cv_align_mm | cut -d ' ' -f 3)
405  AC_DEFINE_UNQUOTED([ZEND_MM_ALIGNMENT],
406    [$zend_mm_alignment],
407    [Number of bytes for the ZEND_MM alignment.])
408  AC_DEFINE_UNQUOTED([ZEND_MM_ALIGNMENT_LOG2],
409    [$zend_mm_alignment_log2],
410    [Number of bytes for the logarithmic ZEND_MM alignment.])
411  AC_DEFINE_UNQUOTED([ZEND_MM_NEED_EIGHT_BYTE_REALIGNMENT],
412    [$zend_mm_8byte_realign],
413    [Define to 1 if ZEND_MM needs 8-byte realignment, and to 0 if not.])
414])
415])
416
417dnl
418dnl ZEND_CHECK_SIGNALS
419dnl
420dnl Check whether to enable Zend signal handling if supported by the system.
421dnl
422AC_DEFUN([ZEND_CHECK_SIGNALS], [dnl
423AC_ARG_ENABLE([zend-signals],
424  [AS_HELP_STRING([--disable-zend-signals],
425    [Disable Zend signal handling])],
426  [ZEND_SIGNALS=$enableval],
427  [ZEND_SIGNALS=yes])
428
429AC_CHECK_FUNCS([sigaction],, [ZEND_SIGNALS=no])
430AS_VAR_IF([ZEND_SIGNALS], [yes],
431  [AC_DEFINE([ZEND_SIGNALS], [1],
432    [Define to 1 if Zend signal handling is supported and enabled.])
433  AS_VAR_APPEND([CFLAGS], [" -DZEND_SIGNALS"])])
434
435AC_MSG_CHECKING([whether to enable Zend signal handling])
436AC_MSG_RESULT([$ZEND_SIGNALS])
437])
438
439dnl
440dnl ZEND_CHECK_MAX_EXECUTION_TIMERS
441dnl
442dnl Check whether to enable Zend max execution timers.
443dnl
444AC_DEFUN([ZEND_CHECK_MAX_EXECUTION_TIMERS], [dnl
445AC_ARG_ENABLE([zend-max-execution-timers],
446  [AS_HELP_STRING([--enable-zend-max-execution-timers],
447    [Enable Zend max execution timers; when building with thread safety
448    (--enable-zts), they are automatically enabled by default based on the
449    system support])],
450    [ZEND_MAX_EXECUTION_TIMERS=$enableval],
451    [ZEND_MAX_EXECUTION_TIMERS=$ZEND_ZTS])
452
453AS_CASE([$host_alias], [*linux*|*freebsd*],,
454  [ZEND_MAX_EXECUTION_TIMERS=no])
455
456AS_VAR_IF([ZEND_MAX_EXECUTION_TIMERS], [yes],
457  [AC_SEARCH_LIBS([timer_create], [rt],,
458    [ZEND_MAX_EXECUTION_TIMERS=no])])
459
460AS_VAR_IF([ZEND_MAX_EXECUTION_TIMERS], [yes],
461  [AC_DEFINE([ZEND_MAX_EXECUTION_TIMERS], [1],
462    [Define to 1 if Zend max execution timers are supported and enabled.])
463    AS_VAR_APPEND([CFLAGS], [" -DZEND_MAX_EXECUTION_TIMERS"])])
464
465AC_MSG_CHECKING([whether to enable Zend max execution timers])
466AC_MSG_RESULT([$ZEND_MAX_EXECUTION_TIMERS])
467])
468