1dnl Process this file with autoconf to produce a configure script.
2dnl configure.ac
3dnl
4dnl Developers, please strive to achieve this order:
5dnl
6dnl 0. Initialization and options processing
7dnl 1. Programs
8dnl 2. Libraries
9dnl 3. Header files
10dnl 4. Types
11dnl 5. Structures
12dnl 6. Compiler characteristics
13dnl 7. Functions, global variables
14dnl 8. System services
15dnl
16dnl Read the Autoconf manual for details.
17dnl
18m4_pattern_forbid(^PGAC_)dnl to catch undefined macros
19
20AC_INIT([PostgreSQL], [14.1], [pgsql-bugs@lists.postgresql.org], [], [https://www.postgresql.org/])
21
22m4_if(m4_defn([m4_PACKAGE_VERSION]), [2.69], [], [m4_fatal([Autoconf version 2.69 is required.
23Untested combinations of 'autoconf' and PostgreSQL versions are not
24recommended.  You can remove the check from 'configure.ac' but it is then
25your responsibility whether the result works or not.])])
26AC_COPYRIGHT([Copyright (c) 1996-2021, PostgreSQL Global Development Group])
27AC_CONFIG_SRCDIR([src/backend/access/common/heaptuple.c])
28AC_CONFIG_AUX_DIR(config)
29AC_PREFIX_DEFAULT(/usr/local/pgsql)
30AC_DEFINE_UNQUOTED(CONFIGURE_ARGS, ["$ac_configure_args"], [Saved arguments from configure])
31
32[PG_MAJORVERSION=`expr "$PACKAGE_VERSION" : '\([0-9][0-9]*\)'`]
33[PG_MINORVERSION=`expr "$PACKAGE_VERSION" : '.*\.\([0-9][0-9]*\)'`]
34test -n "$PG_MINORVERSION" || PG_MINORVERSION=0
35AC_SUBST(PG_MAJORVERSION)
36AC_DEFINE_UNQUOTED(PG_MAJORVERSION, "$PG_MAJORVERSION", [PostgreSQL major version as a string])
37AC_DEFINE_UNQUOTED(PG_MAJORVERSION_NUM, $PG_MAJORVERSION, [PostgreSQL major version number])
38AC_DEFINE_UNQUOTED(PG_MINORVERSION_NUM, $PG_MINORVERSION, [PostgreSQL minor version number])
39
40PGAC_ARG_REQ(with, extra-version, [STRING], [append STRING to version],
41             [PG_VERSION="$PACKAGE_VERSION$withval"],
42             [PG_VERSION="$PACKAGE_VERSION"])
43AC_DEFINE_UNQUOTED(PG_VERSION, "$PG_VERSION", [PostgreSQL version as a string])
44
45AC_CANONICAL_HOST
46
47template=
48AC_MSG_CHECKING([which template to use])
49
50PGAC_ARG_REQ(with, template, [NAME], [override operating system template],
51[
52  case $withval in
53    list)   echo; ls "$srcdir/src/template"; exit;;
54    *)      if test -f "$srcdir/src/template/$with_template" ; then
55              template=$withval
56            else
57              AC_MSG_ERROR(['$withval' is not a valid template name. Use 'list' for a list.])
58            fi;;
59  esac
60],
61[
62# --with-template not given
63
64case $host_os in
65     aix*) template=aix ;;
66  cygwin*|msys*) template=cygwin ;;
67  darwin*) template=darwin ;;
68dragonfly*) template=netbsd ;;
69 freebsd*) template=freebsd ;;
70    hpux*) template=hpux ;;
71 linux*|gnu*|k*bsd*-gnu)
72           template=linux ;;
73   mingw*) template=win32 ;;
74  netbsd*) template=netbsd ;;
75 openbsd*) template=openbsd ;;
76 solaris*) template=solaris ;;
77esac
78
79  if test x"$template" = x"" ; then
80    AC_MSG_ERROR([[
81*******************************************************************
82PostgreSQL has apparently not been ported to your platform yet.
83To try a manual configuration, look into the src/template directory
84for a similar platform and use the '--with-template=' option.
85
86Please also contact <]AC_PACKAGE_BUGREPORT[> to see about
87rectifying this.  Include the above 'checking host system type...'
88line.
89*******************************************************************
90]])
91  fi
92
93])
94
95AC_MSG_RESULT([$template])
96
97PORTNAME=$template
98AC_SUBST(PORTNAME)
99
100# Initialize default assumption that we do not need separate assembly code
101# for TAS (test-and-set).  This can be overridden by the template file
102# when it's executed.
103need_tas=no
104tas_file=dummy.s
105
106
107
108##
109## Command line options
110##
111
112#
113# Add non-standard directories to the include path
114#
115PGAC_ARG_REQ(with, includes, [DIRS], [look for additional header files in DIRS])
116
117
118#
119# Add non-standard directories to the library search path
120#
121PGAC_ARG_REQ(with, libraries, [DIRS], [look for additional libraries in DIRS],
122             [LIBRARY_DIRS=$withval])
123
124PGAC_ARG_REQ(with, libs,      [DIRS], [alternative spelling of --with-libraries],
125             [LIBRARY_DIRS=$withval])
126
127
128#
129# 64-bit integer date/time storage is now the only option, but to avoid
130# unnecessary breakage of build scripts, continue to accept an explicit
131# "--enable-integer-datetimes" switch.
132#
133PGAC_ARG_BOOL(enable, integer-datetimes, yes, [obsolete option, no longer supported],
134              [],
135              [AC_MSG_ERROR([--disable-integer-datetimes is no longer supported])])
136
137
138#
139# NLS
140#
141AC_MSG_CHECKING([whether NLS is wanted])
142PGAC_ARG_OPTARG(enable, nls,
143                [LANGUAGES], [enable Native Language Support],
144                [],
145                [WANTED_LANGUAGES=$enableval],
146                [AC_DEFINE(ENABLE_NLS, 1,
147                           [Define to 1 if you want National Language Support. (--enable-nls)])])
148AC_MSG_RESULT([$enable_nls])
149AC_SUBST(enable_nls)
150AC_SUBST(WANTED_LANGUAGES)
151
152#
153# Default port number (--with-pgport), default 5432
154#
155AC_MSG_CHECKING([for default port number])
156PGAC_ARG_REQ(with, pgport, [PORTNUM], [set default port number [5432]],
157             [default_port=$withval],
158             [default_port=5432])
159AC_MSG_RESULT([$default_port])
160# Need both of these because some places want an integer and some a string
161AC_DEFINE_UNQUOTED(DEF_PGPORT, ${default_port},
162[Define to the default TCP port number on which the server listens and
163to which clients will try to connect.  This can be overridden at run-time,
164but it's convenient if your clients have the right default compiled in.
165(--with-pgport=PORTNUM)])
166AC_DEFINE_UNQUOTED(DEF_PGPORT_STR, "${default_port}",
167                   [Define to the default TCP port number as a string constant.])
168AC_SUBST(default_port)
169
170# It's worth validating port; you can get very confusing errors otherwise
171if test x"$default_port" = x""; then
172  AC_MSG_ERROR([invalid --with-pgport specification: empty string])
173elif test ! x`echo "$default_port" | sed -e 's/[[0-9]]*//'` = x""; then
174  AC_MSG_ERROR([invalid --with-pgport specification: must be a number])
175elif test ! x`echo "$default_port" | sed -e 's/^0.//'` = x"$default_port"; then
176  AC_MSG_ERROR([invalid --with-pgport specification: must not have leading 0])
177elif test "$default_port" -lt "1" -o "$default_port" -gt "65535"; then
178  AC_MSG_ERROR([invalid --with-pgport specification: must be between 1 and 65535])
179fi
180
181#
182# '-rpath'-like feature can be disabled
183#
184PGAC_ARG_BOOL(enable, rpath, yes,
185              [do not embed shared library search path in executables])
186AC_SUBST(enable_rpath)
187
188#
189# Spinlocks
190#
191PGAC_ARG_BOOL(enable, spinlocks, yes,
192              [do not use spinlocks])
193
194#
195# Atomic operations
196#
197PGAC_ARG_BOOL(enable, atomics, yes,
198              [do not use atomic operations])
199
200#
201# --enable-debug adds -g to compiler flags
202#
203PGAC_ARG_BOOL(enable, debug, no,
204              [build with debugging symbols (-g)])
205AC_SUBST(enable_debug)
206
207#
208# --enable-profiling enables gcc profiling
209#
210PGAC_ARG_BOOL(enable, profiling, no,
211              [build with profiling enabled ])
212
213#
214# --enable-coverage enables generation of code coverage metrics with gcov
215#
216PGAC_ARG_BOOL(enable, coverage, no,
217              [build with coverage testing instrumentation],
218[PGAC_PATH_PROGS(GCOV, gcov)
219if test -z "$GCOV"; then
220  AC_MSG_ERROR([gcov not found])
221fi
222PGAC_PATH_PROGS(LCOV, lcov)
223if test -z "$LCOV"; then
224  AC_MSG_ERROR([lcov not found])
225fi
226PGAC_PATH_PROGS(GENHTML, genhtml)
227if test -z "$GENHTML"; then
228  AC_MSG_ERROR([genhtml not found])
229fi])
230AC_SUBST(enable_coverage)
231
232#
233# DTrace
234#
235PGAC_ARG_BOOL(enable, dtrace, no,
236              [build with DTrace support],
237[PGAC_PATH_PROGS(DTRACE, dtrace)
238if test -z "$DTRACE"; then
239  AC_MSG_ERROR([dtrace not found])
240fi
241AC_SUBST(DTRACEFLAGS)])
242AC_SUBST(enable_dtrace)
243
244#
245# TAP tests
246#
247PGAC_ARG_BOOL(enable, tap-tests, no,
248              [enable TAP tests (requires Perl and IPC::Run)])
249AC_SUBST(enable_tap_tests)
250
251#
252# Block size
253#
254AC_MSG_CHECKING([for block size])
255PGAC_ARG_REQ(with, blocksize, [BLOCKSIZE], [set table block size in kB [8]],
256             [blocksize=$withval],
257             [blocksize=8])
258case ${blocksize} in
259  1) BLCKSZ=1024;;
260  2) BLCKSZ=2048;;
261  4) BLCKSZ=4096;;
262  8) BLCKSZ=8192;;
263 16) BLCKSZ=16384;;
264 32) BLCKSZ=32768;;
265  *) AC_MSG_ERROR([Invalid block size. Allowed values are 1,2,4,8,16,32.])
266esac
267AC_MSG_RESULT([${blocksize}kB])
268
269AC_DEFINE_UNQUOTED([BLCKSZ], ${BLCKSZ}, [
270 Size of a disk block --- this also limits the size of a tuple.  You
271 can set it bigger if you need bigger tuples (although TOAST should
272 reduce the need to have large tuples, since fields can be spread
273 across multiple tuples).
274
275 BLCKSZ must be a power of 2.  The maximum possible value of BLCKSZ
276 is currently 2^15 (32768).  This is determined by the 15-bit widths
277 of the lp_off and lp_len fields in ItemIdData (see
278 include/storage/itemid.h).
279
280 Changing BLCKSZ requires an initdb.
281])
282
283#
284# Relation segment size
285#
286AC_MSG_CHECKING([for segment size])
287PGAC_ARG_REQ(with, segsize, [SEGSIZE], [set table segment size in GB [1]],
288             [segsize=$withval],
289             [segsize=1])
290# this expression is set up to avoid unnecessary integer overflow
291# blocksize is already guaranteed to be a factor of 1024
292RELSEG_SIZE=`expr '(' 1024 / ${blocksize} ')' '*' ${segsize} '*' 1024`
293test $? -eq 0 || exit 1
294AC_MSG_RESULT([${segsize}GB])
295
296AC_DEFINE_UNQUOTED([RELSEG_SIZE], ${RELSEG_SIZE}, [
297 RELSEG_SIZE is the maximum number of blocks allowed in one disk file.
298 Thus, the maximum size of a single file is RELSEG_SIZE * BLCKSZ;
299 relations bigger than that are divided into multiple files.
300
301 RELSEG_SIZE * BLCKSZ must be less than your OS' limit on file size.
302 This is often 2 GB or 4GB in a 32-bit operating system, unless you
303 have large file support enabled.  By default, we make the limit 1 GB
304 to avoid any possible integer-overflow problems within the OS.
305 A limit smaller than necessary only means we divide a large
306 relation into more chunks than necessary, so it seems best to err
307 in the direction of a small limit.
308
309 A power-of-2 value is recommended to save a few cycles in md.c,
310 but is not absolutely required.
311
312 Changing RELSEG_SIZE requires an initdb.
313])
314
315#
316# WAL block size
317#
318AC_MSG_CHECKING([for WAL block size])
319PGAC_ARG_REQ(with, wal-blocksize, [BLOCKSIZE], [set WAL block size in kB [8]],
320             [wal_blocksize=$withval],
321             [wal_blocksize=8])
322case ${wal_blocksize} in
323  1) XLOG_BLCKSZ=1024;;
324  2) XLOG_BLCKSZ=2048;;
325  4) XLOG_BLCKSZ=4096;;
326  8) XLOG_BLCKSZ=8192;;
327 16) XLOG_BLCKSZ=16384;;
328 32) XLOG_BLCKSZ=32768;;
329 64) XLOG_BLCKSZ=65536;;
330  *) AC_MSG_ERROR([Invalid WAL block size. Allowed values are 1,2,4,8,16,32,64.])
331esac
332AC_MSG_RESULT([${wal_blocksize}kB])
333
334AC_DEFINE_UNQUOTED([XLOG_BLCKSZ], ${XLOG_BLCKSZ}, [
335 Size of a WAL file block.  This need have no particular relation to BLCKSZ.
336 XLOG_BLCKSZ must be a power of 2, and if your system supports O_DIRECT I/O,
337 XLOG_BLCKSZ must be a multiple of the alignment requirement for direct-I/O
338 buffers, else direct I/O may fail.
339
340 Changing XLOG_BLCKSZ requires an initdb.
341])
342
343#
344# C compiler
345#
346
347# For historical reasons you can also use --with-CC to specify the C compiler
348# to use, although the standard way to do this is to set the CC environment
349# variable.
350PGAC_ARG_REQ(with, CC, [CMD], [set compiler (deprecated)], [CC=$with_CC])
351
352case $template in
353  aix) pgac_cc_list="gcc xlc"; pgac_cxx_list="g++ xlC";;
354    *) pgac_cc_list="gcc cc"; pgac_cxx_list="g++ c++";;
355esac
356
357AC_PROG_CC([$pgac_cc_list])
358AC_PROG_CC_C99()
359
360# Error out if the compiler does not support C99, as the codebase
361# relies on that.
362if test "$ac_cv_prog_cc_c99" = no; then
363    AC_MSG_ERROR([C compiler "$CC" does not support C99])
364fi
365
366AC_PROG_CXX([$pgac_cxx_list])
367
368# Check if it's Intel's compiler, which (usually) pretends to be gcc,
369# but has idiosyncrasies of its own.  We assume icc will define
370# __INTEL_COMPILER regardless of CFLAGS.
371AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [@%:@ifndef __INTEL_COMPILER
372choke me
373@%:@endif])], [ICC=yes], [ICC=no])
374
375# Check if it's Sun Studio compiler. We assume that
376# __SUNPRO_C will be defined for Sun Studio compilers
377AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [@%:@ifndef __SUNPRO_C
378choke me
379@%:@endif])], [SUN_STUDIO_CC=yes], [SUN_STUDIO_CC=no])
380
381AC_SUBST(SUN_STUDIO_CC)
382
383
384#
385# LLVM
386#
387# Checked early because subsequent tests depend on it.
388PGAC_ARG_BOOL(with, llvm, no, [build with LLVM based JIT support],
389              [AC_DEFINE([USE_LLVM], 1, [Define to 1 to build with LLVM based JIT support. (--with-llvm)])])
390AC_SUBST(with_llvm)
391dnl must use AS_IF here, else AC_REQUIRES inside PGAC_LLVM_SUPPORT malfunctions
392AS_IF([test "$with_llvm" = yes], [
393  PGAC_LLVM_SUPPORT()
394]) # fi
395
396
397unset CFLAGS
398unset CXXFLAGS
399
400#
401# Read the template
402#
403. "$srcdir/src/template/$template" || exit
404
405# C[XX]FLAGS are selected so:
406# If the user specifies something in the environment, that is used.
407# else:  If the template file set something, that is used.
408# else:  If coverage was enabled, don't set anything.
409# else:  If the compiler is GCC, then we use -O2.
410# else:  If the compiler is something else, then we use -O, unless debugging.
411
412if test "$ac_env_CFLAGS_set" = set; then
413  CFLAGS=$ac_env_CFLAGS_value
414elif test "${CFLAGS+set}" = set; then
415  : # (keep what template set)
416elif test "$enable_coverage" = yes; then
417  : # no optimization by default
418elif test "$GCC" = yes; then
419  CFLAGS="-O2"
420else
421  # if the user selected debug mode, don't use -O
422  if test "$enable_debug" != yes; then
423    CFLAGS="-O"
424  fi
425fi
426
427if test "$ac_env_CXXFLAGS_set" = set; then
428  CXXFLAGS=$ac_env_CXXFLAGS_value
429elif test "${CXXFLAGS+set}" = set; then
430  : # (keep what template set)
431elif test "$enable_coverage" = yes; then
432  : # no optimization by default
433elif test "$GCC" = yes; then
434  CXXFLAGS="-O2"
435else
436  # if the user selected debug mode, don't use -O
437  if test "$enable_debug" != yes; then
438    CXXFLAGS="-O"
439  fi
440fi
441
442# When generating bitcode (for inlining) we always want to use -O2
443# even when --enable-debug is specified. The bitcode it's not going to
444# be used for line-by-line debugging, and JIT inlining doesn't work
445# without at least -O1 (otherwise clang will emit 'noinline'
446# attributes everywhere), which is bad for testing.  Still allow the
447# environment to override if done explicitly.
448if test "$ac_env_BITCODE_CFLAGS_set" = set; then
449  BITCODE_CFLAGS=$ac_env_BITCODE_CFLAGS_value
450else
451  BITCODE_CFLAGS="-O2 $BITCODE_CFLAGS"
452fi
453if test "$ac_env_BITCODE_CXXFLAGS_set" = set; then
454  BITCODE_CXXFLAGS=$ac_env_BITCODE_CXXFLAGS_value
455else
456  BITCODE_CXXFLAGS="-O2 $BITCODE_CXXFLAGS"
457fi
458
459# C[XX]FLAGS we determined above will be added back at the end
460user_CFLAGS=$CFLAGS
461CFLAGS=""
462user_CXXFLAGS=$CXXFLAGS
463CXXFLAGS=""
464user_BITCODE_CFLAGS=$BITCODE_CFLAGS
465BITCODE_CFLAGS=""
466user_BITCODE_CXXFLAGS=$BITCODE_CXXFLAGS
467BITCODE_CXXFLAGS=""
468
469# set CFLAGS_UNROLL_LOOPS and CFLAGS_VECTORIZE from the environment, if present
470if test "$ac_env_CFLAGS_UNROLL_LOOPS_set" = set; then
471  CFLAGS_UNROLL_LOOPS=$ac_env_CFLAGS_UNROLL_LOOPS_value
472fi
473if test "$ac_env_CFLAGS_VECTORIZE_set" = set; then
474  CFLAGS_VECTORIZE=$ac_env_CFLAGS_VECTORIZE_value
475fi
476
477# Some versions of GCC support some additional useful warning flags.
478# Check whether they are supported, and add them to CFLAGS if so.
479# ICC pretends to be GCC but it's lying; it doesn't support these flags,
480# but has its own.  Also check other compiler-specific flags here.
481
482if test "$GCC" = yes -a "$ICC" = no; then
483  CFLAGS="-Wall -Wmissing-prototypes -Wpointer-arith"
484  CXXFLAGS="-Wall -Wpointer-arith"
485  # These work in some but not all gcc versions
486  save_CFLAGS=$CFLAGS
487  PGAC_PROG_CC_CFLAGS_OPT([-Wdeclaration-after-statement])
488  # -Wdeclaration-after-statement isn't applicable for C++.  Specific C files
489  # disable it, so AC_SUBST the negative form.
490  PERMIT_DECLARATION_AFTER_STATEMENT=
491  if test x"$save_CFLAGS" != x"$CFLAGS"; then
492    PERMIT_DECLARATION_AFTER_STATEMENT=-Wno-declaration-after-statement
493  fi
494  AC_SUBST(PERMIT_DECLARATION_AFTER_STATEMENT)
495  # Really don't want VLAs to be used in our dialect of C
496  PGAC_PROG_CC_CFLAGS_OPT([-Werror=vla])
497  # On macOS, complain about usage of symbols newer than the deployment target
498  PGAC_PROG_CC_CFLAGS_OPT([-Werror=unguarded-availability-new])
499  PGAC_PROG_CXX_CFLAGS_OPT([-Werror=unguarded-availability-new])
500  # -Wvla is not applicable for C++
501  PGAC_PROG_CC_CFLAGS_OPT([-Wendif-labels])
502  PGAC_PROG_CXX_CFLAGS_OPT([-Wendif-labels])
503  PGAC_PROG_CC_CFLAGS_OPT([-Wmissing-format-attribute])
504  PGAC_PROG_CXX_CFLAGS_OPT([-Wmissing-format-attribute])
505  PGAC_PROG_CC_CFLAGS_OPT([-Wimplicit-fallthrough=3])
506  PGAC_PROG_CXX_CFLAGS_OPT([-Wimplicit-fallthrough=3])
507  PGAC_PROG_CC_CFLAGS_OPT([-Wcast-function-type])
508  PGAC_PROG_CXX_CFLAGS_OPT([-Wcast-function-type])
509  # This was included in -Wall/-Wformat in older GCC versions
510  PGAC_PROG_CC_CFLAGS_OPT([-Wformat-security])
511  PGAC_PROG_CXX_CFLAGS_OPT([-Wformat-security])
512  # Disable strict-aliasing rules; needed for gcc 3.3+
513  PGAC_PROG_CC_CFLAGS_OPT([-fno-strict-aliasing])
514  PGAC_PROG_CXX_CFLAGS_OPT([-fno-strict-aliasing])
515  # Disable optimizations that assume no overflow; needed for gcc 4.3+
516  PGAC_PROG_CC_CFLAGS_OPT([-fwrapv])
517  PGAC_PROG_CXX_CFLAGS_OPT([-fwrapv])
518  # Disable FP optimizations that cause various errors on gcc 4.5+ or maybe 4.6+
519  PGAC_PROG_CC_CFLAGS_OPT([-fexcess-precision=standard])
520  PGAC_PROG_CXX_CFLAGS_OPT([-fexcess-precision=standard])
521  # Optimization flags for specific files that benefit from loop unrolling
522  PGAC_PROG_CC_VAR_OPT(CFLAGS_UNROLL_LOOPS, [-funroll-loops])
523  # Optimization flags for specific files that benefit from vectorization
524  PGAC_PROG_CC_VAR_OPT(CFLAGS_VECTORIZE, [-ftree-vectorize])
525  # We want to suppress clang's unhelpful unused-command-line-argument warnings
526  # but gcc won't complain about unrecognized -Wno-foo switches, so we have to
527  # test for the positive form and if that works, add the negative form
528  NOT_THE_CFLAGS=""
529  PGAC_PROG_CC_VAR_OPT(NOT_THE_CFLAGS, [-Wunused-command-line-argument])
530  if test -n "$NOT_THE_CFLAGS"; then
531    CFLAGS="$CFLAGS -Wno-unused-command-line-argument"
532  fi
533  # Similarly disable useless truncation warnings from gcc 8+
534  NOT_THE_CFLAGS=""
535  PGAC_PROG_CC_VAR_OPT(NOT_THE_CFLAGS, [-Wformat-truncation])
536  if test -n "$NOT_THE_CFLAGS"; then
537    CFLAGS="$CFLAGS -Wno-format-truncation"
538  fi
539  NOT_THE_CFLAGS=""
540  PGAC_PROG_CC_VAR_OPT(NOT_THE_CFLAGS, [-Wstringop-truncation])
541  if test -n "$NOT_THE_CFLAGS"; then
542    CFLAGS="$CFLAGS -Wno-stringop-truncation"
543  fi
544elif test "$ICC" = yes; then
545  # Intel's compiler has a bug/misoptimization in checking for
546  # division by NAN (NaN == 0), -mp1 fixes it, so add it to the CFLAGS.
547  PGAC_PROG_CC_CFLAGS_OPT([-mp1])
548  PGAC_PROG_CXX_CFLAGS_OPT([-mp1])
549  # Make sure strict aliasing is off (though this is said to be the default)
550  PGAC_PROG_CC_CFLAGS_OPT([-fno-strict-aliasing])
551  PGAC_PROG_CXX_CFLAGS_OPT([-fno-strict-aliasing])
552elif test "$PORTNAME" = "aix"; then
553  # AIX's xlc has to have strict aliasing turned off too
554  PGAC_PROG_CC_CFLAGS_OPT([-qnoansialias])
555  PGAC_PROG_CXX_CFLAGS_OPT([-qnoansialias])
556  PGAC_PROG_CC_CFLAGS_OPT([-qlonglong])
557  PGAC_PROG_CXX_CFLAGS_OPT([-qlonglong])
558elif test "$PORTNAME" = "hpux"; then
559  # On some versions of HP-UX, libm functions do not set errno by default.
560  # Fix that by using +Olibmerrno if the compiler recognizes it.
561  PGAC_PROG_CC_CFLAGS_OPT([+Olibmerrno])
562  PGAC_PROG_CXX_CFLAGS_OPT([+Olibmerrno])
563fi
564
565AC_SUBST(CFLAGS_UNROLL_LOOPS)
566AC_SUBST(CFLAGS_VECTORIZE)
567
568# Determine flags used to emit bitcode for JIT inlining. Need to test
569# for behaviour changing compiler flags, to keep compatibility with
570# compiler used for normal postgres code.
571if test "$with_llvm" = yes ; then
572  CLANGXX="$CLANG -xc++"
573
574  PGAC_PROG_VARCC_VARFLAGS_OPT(CLANG, BITCODE_CFLAGS, [-fno-strict-aliasing])
575  PGAC_PROG_VARCXX_VARFLAGS_OPT(CLANGXX, BITCODE_CXXFLAGS, [-fno-strict-aliasing])
576  PGAC_PROG_VARCC_VARFLAGS_OPT(CLANG, BITCODE_CFLAGS, [-fwrapv])
577  PGAC_PROG_VARCXX_VARFLAGS_OPT(CLANGXX, BITCODE_CXXFLAGS, [-fwrapv])
578  PGAC_PROG_VARCC_VARFLAGS_OPT(CLANG, BITCODE_CFLAGS, [-fexcess-precision=standard])
579  PGAC_PROG_VARCXX_VARFLAGS_OPT(CLANGXX, BITCODE_CXXFLAGS, [-fexcess-precision=standard])
580fi
581
582# supply -g if --enable-debug
583if test "$enable_debug" = yes && test "$ac_cv_prog_cc_g" = yes; then
584  CFLAGS="$CFLAGS -g"
585fi
586
587if test "$enable_debug" = yes && test "$ac_cv_prog_cxx_g" = yes; then
588  CXXFLAGS="$CXXFLAGS -g"
589fi
590
591# enable code coverage if --enable-coverage
592if test "$enable_coverage" = yes; then
593  if test "$GCC" = yes; then
594    CFLAGS="$CFLAGS -fprofile-arcs -ftest-coverage"
595    CXXFLAGS="$CXXFLAGS -fprofile-arcs -ftest-coverage"
596  else
597    AC_MSG_ERROR([--enable-coverage is supported only when using GCC])
598  fi
599fi
600
601# enable profiling if --enable-profiling
602if test "$enable_profiling" = yes && test "$ac_cv_prog_cc_g" = yes; then
603  if test "$GCC" = yes; then
604    AC_DEFINE([PROFILE_PID_DIR], 1,
605           [Define to 1 to allow profiling output to be saved separately for each process.])
606    CFLAGS="$CFLAGS -pg $PLATFORM_PROFILE_FLAGS"
607    CXXFLAGS="$CXXFLAGS -pg $PLATFORM_PROFILE_FLAGS"
608  else
609    AC_MSG_ERROR([--enable-profiling is supported only when using GCC])
610  fi
611fi
612
613# We already have this in Makefile.win32, but configure needs it too
614if test "$PORTNAME" = "win32"; then
615  CPPFLAGS="$CPPFLAGS -I$srcdir/src/include/port/win32"
616fi
617
618# Now that we're done automatically adding stuff to C[XX]FLAGS, put back the
619# user-specified flags (if any) at the end.  This lets users override
620# the automatic additions.
621CFLAGS="$CFLAGS $user_CFLAGS"
622CXXFLAGS="$CXXFLAGS $user_CXXFLAGS"
623BITCODE_CFLAGS="$BITCODE_CFLAGS $user_BITCODE_CFLAGS"
624BITCODE_CXXFLAGS="$BITCODE_CXXFLAGS $user_BITCODE_CXXFLAGS"
625
626AC_SUBST(BITCODE_CFLAGS)
627AC_SUBST(BITCODE_CXXFLAGS)
628
629# The template file must set up CFLAGS_SL; we don't support user override
630AC_SUBST(CFLAGS_SL)
631
632# Check if the compiler still works with the final flag settings
633# (note, we're not checking that for CXX, which is optional)
634AC_MSG_CHECKING([whether the C compiler still works])
635AC_LINK_IFELSE([AC_LANG_PROGRAM([], [return 0;])],
636  [AC_MSG_RESULT(yes)],
637  [AC_MSG_RESULT(no)
638   AC_MSG_ERROR([cannot proceed])])
639
640# Defend against gcc -ffast-math
641if test "$GCC" = yes; then
642AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [@%:@ifdef __FAST_MATH__
643choke me
644@%:@endif])], [], [AC_MSG_ERROR([do not put -ffast-math in CFLAGS])])
645fi
646
647# Defend against clang being used on x86-32 without SSE2 enabled.  As current
648# versions of clang do not understand -fexcess-precision=standard, the use of
649# x87 floating point operations leads to problems like isinf possibly returning
650# false for a value that is infinite when converted from the 80bit register to
651# the 8byte memory representation.
652#
653# Only perform the test if the compiler doesn't understand
654# -fexcess-precision=standard, that way a potentially fixed compiler will work
655# automatically.
656if test "$pgac_cv_prog_CC_cflags__fexcess_precision_standard" = no; then
657AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [
658@%:@if defined(__clang__) && defined(__i386__) && !defined(__SSE2_MATH__)
659choke me
660@%:@endif
661])], [],
662[AC_MSG_ERROR([Compiling PostgreSQL with clang, on 32bit x86, requires SSE2 support. Use -msse2 or use gcc.])])
663fi
664
665AC_PROG_CPP
666AC_SUBST(GCC)
667
668
669#
670# Set up TAS assembly code if needed; the template file has now had its
671# chance to request this.
672#
673AC_CONFIG_LINKS([src/backend/port/tas.s:src/backend/port/tas/${tas_file}])
674
675if test "$need_tas" = yes ; then
676  TAS=tas.o
677else
678  TAS=""
679fi
680AC_SUBST(TAS)
681
682#
683# Set up pkg_config in case we need it below
684#
685PKG_PROG_PKG_CONFIG
686
687#
688# Automatic dependency tracking
689#
690PGAC_ARG_BOOL(enable, depend, no, [turn on automatic dependency tracking],
691              [autodepend=yes])
692AC_SUBST(autodepend)
693
694
695#
696# Enable assert checks
697#
698PGAC_ARG_BOOL(enable, cassert, no, [enable assertion checks (for debugging)],
699              [AC_DEFINE([USE_ASSERT_CHECKING], 1,
700                         [Define to 1 to build with assertion checks. (--enable-cassert)])])
701
702
703#
704# Include directories
705#
706ac_save_IFS=$IFS
707IFS="${IFS}${PATH_SEPARATOR}"
708# SRCH_INC comes from the template file
709for dir in $with_includes $SRCH_INC; do
710  if test -d "$dir"; then
711    INCLUDES="$INCLUDES -I$dir"
712  else
713    AC_MSG_WARN([*** Include directory $dir does not exist.])
714  fi
715done
716IFS=$ac_save_IFS
717AC_SUBST(INCLUDES)
718
719
720#
721# Library directories
722#
723ac_save_IFS=$IFS
724IFS="${IFS}${PATH_SEPARATOR}"
725# LIBRARY_DIRS comes from command line, SRCH_LIB from template file.
726for dir in $LIBRARY_DIRS $SRCH_LIB; do
727  if test -d "$dir"; then
728    LIBDIRS="$LIBDIRS -L$dir"
729  else
730    AC_MSG_WARN([*** Library directory $dir does not exist.])
731  fi
732done
733IFS=$ac_save_IFS
734
735#
736# Enable thread-safe client libraries
737#
738AC_MSG_CHECKING([allow thread-safe client libraries])
739PGAC_ARG_BOOL(enable, thread-safety, yes, [disable thread-safety in client libraries])
740if test "$enable_thread_safety" = yes; then
741  AC_DEFINE([ENABLE_THREAD_SAFETY], 1,
742          [Define to 1 to build client libraries as thread-safe code. (--enable-thread-safety)])
743fi
744AC_MSG_RESULT([$enable_thread_safety])
745AC_SUBST(enable_thread_safety)
746
747#
748# ICU
749#
750AC_MSG_CHECKING([whether to build with ICU support])
751PGAC_ARG_BOOL(with, icu, no, [build with ICU support],
752              [AC_DEFINE([USE_ICU], 1, [Define to build with ICU support. (--with-icu)])])
753AC_MSG_RESULT([$with_icu])
754AC_SUBST(with_icu)
755
756if test "$with_icu" = yes; then
757  PKG_CHECK_MODULES(ICU, icu-uc icu-i18n)
758fi
759
760#
761# Optionally build Tcl modules (PL/Tcl)
762#
763AC_MSG_CHECKING([whether to build with Tcl])
764PGAC_ARG_BOOL(with, tcl, no, [build Tcl modules (PL/Tcl)])
765AC_MSG_RESULT([$with_tcl])
766AC_SUBST([with_tcl])
767
768# We see if the path to the Tcl/Tk configuration scripts is specified.
769# This will override the use of tclsh to find the paths to search.
770
771PGAC_ARG_REQ(with, tclconfig, [DIR], [tclConfig.sh is in DIR])
772
773#
774# Optionally build Perl modules (PL/Perl)
775#
776AC_MSG_CHECKING([whether to build Perl modules])
777PGAC_ARG_BOOL(with, perl, no, [build Perl modules (PL/Perl)])
778AC_MSG_RESULT([$with_perl])
779AC_SUBST(with_perl)
780
781#
782# Optionally build Python modules (PL/Python)
783#
784AC_MSG_CHECKING([whether to build Python modules])
785PGAC_ARG_BOOL(with, python, no, [build Python modules (PL/Python)])
786AC_MSG_RESULT([$with_python])
787AC_SUBST(with_python)
788
789#
790# GSSAPI
791#
792AC_MSG_CHECKING([whether to build with GSSAPI support])
793PGAC_ARG_BOOL(with, gssapi, no, [build with GSSAPI support],
794[
795  AC_DEFINE(ENABLE_GSS, 1, [Define to build with GSSAPI support. (--with-gssapi)])
796  krb_srvtab="FILE:\$(sysconfdir)/krb5.keytab"
797])
798AC_MSG_RESULT([$with_gssapi])
799AC_SUBST(with_gssapi)
800
801
802AC_SUBST(krb_srvtab)
803
804
805#
806# Kerberos configuration parameters
807#
808PGAC_ARG_REQ(with, krb-srvnam,
809             [NAME], [default service principal name in Kerberos (GSSAPI) [postgres]],
810             [],
811             [with_krb_srvnam="postgres"])
812AC_SUBST(with_krb_srvnam)
813AC_DEFINE_UNQUOTED([PG_KRB_SRVNAM], ["$with_krb_srvnam"],
814                   [Define to the name of the default PostgreSQL service principal in Kerberos (GSSAPI). (--with-krb-srvnam=NAME)])
815
816
817#
818# PAM
819#
820AC_MSG_CHECKING([whether to build with PAM support])
821PGAC_ARG_BOOL(with, pam, no,
822              [build with PAM support],
823              [AC_DEFINE([USE_PAM], 1, [Define to 1 to build with PAM support. (--with-pam)])])
824AC_MSG_RESULT([$with_pam])
825
826
827#
828# BSD AUTH
829#
830AC_MSG_CHECKING([whether to build with BSD Authentication support])
831PGAC_ARG_BOOL(with, bsd-auth, no,
832              [build with BSD Authentication support],
833              [AC_DEFINE([USE_BSD_AUTH], 1, [Define to 1 to build with BSD Authentication support. (--with-bsd-auth)])])
834AC_MSG_RESULT([$with_bsd_auth])
835
836
837#
838# LDAP
839#
840AC_MSG_CHECKING([whether to build with LDAP support])
841PGAC_ARG_BOOL(with, ldap, no,
842              [build with LDAP support],
843              [AC_DEFINE([USE_LDAP], 1, [Define to 1 to build with LDAP support. (--with-ldap)])])
844AC_MSG_RESULT([$with_ldap])
845AC_SUBST(with_ldap)
846
847
848#
849# Bonjour
850#
851AC_MSG_CHECKING([whether to build with Bonjour support])
852PGAC_ARG_BOOL(with, bonjour, no,
853              [build with Bonjour support],
854              [AC_DEFINE([USE_BONJOUR], 1, [Define to 1 to build with Bonjour support. (--with-bonjour)])])
855AC_MSG_RESULT([$with_bonjour])
856
857
858#
859# SELinux
860#
861AC_MSG_CHECKING([whether to build with SELinux support])
862PGAC_ARG_BOOL(with, selinux, no, [build with SELinux support])
863AC_SUBST(with_selinux)
864AC_MSG_RESULT([$with_selinux])
865
866#
867# Systemd
868#
869AC_MSG_CHECKING([whether to build with systemd support])
870PGAC_ARG_BOOL(with, systemd, no, [build with systemd support],
871              [AC_DEFINE([USE_SYSTEMD], 1, [Define to build with systemd support. (--with-systemd)])])
872AC_SUBST(with_systemd)
873AC_MSG_RESULT([$with_systemd])
874
875#
876# Readline
877#
878PGAC_ARG_BOOL(with, readline, yes,
879              [do not use GNU Readline nor BSD Libedit for editing])
880# readline on MinGW has problems with backslashes in psql and other bugs.
881# This is particularly a problem with non-US code pages.
882# Therefore disable its use until we understand the cause. 2004-07-20
883if test "$PORTNAME" = "win32"; then
884  if test "$with_readline" = yes; then
885    AC_MSG_WARN([*** Readline does not work on MinGW --- disabling])
886    with_readline=no
887  fi
888fi
889AC_SUBST(with_readline)
890
891
892#
893# Prefer libedit
894#
895PGAC_ARG_BOOL(with, libedit-preferred, no,
896              [prefer BSD Libedit over GNU Readline])
897
898
899#
900# UUID library
901#
902# There are at least three UUID libraries in common use: the FreeBSD/NetBSD
903# library, the e2fsprogs libuuid (now part of util-linux-ng), and the OSSP
904# UUID library.  More than one of these might be present on a given platform,
905# so we make the user say which one she wants.
906#
907PGAC_ARG_REQ(with, uuid, [LIB], [build contrib/uuid-ossp using LIB (bsd,e2fs,ossp)])
908if test x"$with_uuid" = x"" ; then
909  with_uuid=no
910fi
911PGAC_ARG_BOOL(with, ossp-uuid, no, [obsolete spelling of --with-uuid=ossp])
912if test "$with_ossp_uuid" = yes ; then
913  with_uuid=ossp
914fi
915
916if test "$with_uuid" != no ; then
917  if test "$with_uuid" = bsd ; then
918    AC_DEFINE([HAVE_UUID_BSD], 1, [Define to 1 if you have BSD UUID support.])
919  elif test "$with_uuid" = e2fs ; then
920    AC_DEFINE([HAVE_UUID_E2FS], 1, [Define to 1 if you have E2FS UUID support.])
921  elif test "$with_uuid" = ossp ; then
922    AC_DEFINE([HAVE_UUID_OSSP], 1, [Define to 1 if you have OSSP UUID support.])
923  else
924    AC_MSG_ERROR([--with-uuid must specify one of bsd, e2fs, or ossp])
925  fi
926fi
927AC_SUBST(with_uuid)
928
929
930#
931# XML
932#
933AC_MSG_CHECKING([whether to build with XML support])
934PGAC_ARG_BOOL(with, libxml, no, [build with XML support],
935              [AC_DEFINE([USE_LIBXML], 1, [Define to 1 to build with XML support. (--with-libxml)])])
936AC_MSG_RESULT([$with_libxml])
937AC_SUBST(with_libxml)
938
939if test "$with_libxml" = yes ; then
940  # Check pkg-config, then xml2-config.  But for backwards compatibility,
941  # setting XML2_CONFIG overrides pkg-config.
942  AC_ARG_VAR(XML2_CONFIG, [path to xml2-config utility])dnl
943  have_libxml2_pkg_config=no
944  if test -z "$XML2_CONFIG" -a -n "$PKG_CONFIG"; then
945    PKG_CHECK_MODULES(XML2, [libxml-2.0 >= 2.6.23],
946                      [have_libxml2_pkg_config=yes], [# do nothing])
947  fi
948  if test "$have_libxml2_pkg_config" = no ; then
949    PGAC_PATH_PROGS(XML2_CONFIG, xml2-config)
950    if test -n "$XML2_CONFIG"; then
951      XML2_CFLAGS=`$XML2_CONFIG --cflags`
952      XML2_LIBS=`$XML2_CONFIG --libs`
953    fi
954  fi
955  # Note the user could also set XML2_CFLAGS/XML2_LIBS directly
956  for pgac_option in $XML2_CFLAGS; do
957    case $pgac_option in
958      -I*|-D*) CPPFLAGS="$CPPFLAGS $pgac_option";;
959    esac
960  done
961  for pgac_option in $XML2_LIBS; do
962    case $pgac_option in
963      -L*) LDFLAGS="$LDFLAGS $pgac_option";;
964    esac
965  done
966fi
967
968
969#
970# XSLT
971#
972PGAC_ARG_BOOL(with, libxslt, no, [use XSLT support when building contrib/xml2],
973              [AC_DEFINE([USE_LIBXSLT], 1, [Define to 1 to use XSLT support when building contrib/xml2. (--with-libxslt)])])
974
975
976AC_SUBST(with_libxslt)
977
978#
979# tzdata
980#
981PGAC_ARG_REQ(with, system-tzdata,
982             [DIR], [use system time zone data in DIR])
983AC_SUBST(with_system_tzdata)
984
985#
986# Zlib
987#
988PGAC_ARG_BOOL(with, zlib, yes,
989              [do not use Zlib])
990AC_SUBST(with_zlib)
991
992#
993# LZ4
994#
995AC_MSG_CHECKING([whether to build with LZ4 support])
996PGAC_ARG_BOOL(with, lz4, no, [build with LZ4 support],
997              [AC_DEFINE([USE_LZ4], 1, [Define to 1 to build with LZ4 support. (--with-lz4)])])
998AC_MSG_RESULT([$with_lz4])
999AC_SUBST(with_lz4)
1000
1001if test "$with_lz4" = yes; then
1002  PKG_CHECK_MODULES(LZ4, liblz4)
1003  # We only care about -I, -D, and -L switches;
1004  # note that -llz4 will be added by AC_CHECK_LIB below.
1005  for pgac_option in $LZ4_CFLAGS; do
1006    case $pgac_option in
1007      -I*|-D*) CPPFLAGS="$CPPFLAGS $pgac_option";;
1008    esac
1009  done
1010  for pgac_option in $LZ4_LIBS; do
1011    case $pgac_option in
1012      -L*) LDFLAGS="$LDFLAGS $pgac_option";;
1013    esac
1014  done
1015fi
1016
1017#
1018# Assignments
1019#
1020
1021CPPFLAGS="$CPPFLAGS $INCLUDES"
1022LDFLAGS="$LDFLAGS $LIBDIRS"
1023
1024AC_ARG_VAR(LDFLAGS_EX, [extra linker flags for linking executables only])
1025AC_ARG_VAR(LDFLAGS_SL, [extra linker flags for linking shared libraries only])
1026
1027PGAC_PROG_LD
1028AC_SUBST(LD)
1029AC_SUBST(with_gnu_ld)
1030AC_PROG_RANLIB
1031PGAC_CHECK_STRIP
1032AC_CHECK_TOOL(AR, ar, ar)
1033if test "$PORTNAME" = "win32"; then
1034  AC_CHECK_TOOL(DLLTOOL, dlltool, dlltool)
1035  AC_CHECK_TOOL(DLLWRAP, dllwrap, dllwrap)
1036  AC_CHECK_TOOL(WINDRES, windres, windres)
1037fi
1038
1039AC_PROG_INSTALL
1040# When Autoconf chooses install-sh as install program it tries to generate
1041# a relative path to it in each makefile where it substitutes it. This clashes
1042# with our Makefile.global concept. This workaround helps.
1043case $INSTALL in
1044  *install-sh*) install_bin='';;
1045  *) install_bin=$INSTALL;;
1046esac
1047AC_SUBST(install_bin)
1048
1049PGAC_PATH_PROGS(TAR, tar)
1050AC_PROG_LN_S
1051AC_PROG_MKDIR_P
1052# When Autoconf chooses install-sh as mkdir -p program it tries to generate
1053# a relative path to it in each makefile where it substitutes it. This clashes
1054# with our Makefile.global concept. This workaround helps.
1055case $MKDIR_P in
1056  *install-sh*) MKDIR_P='\${SHELL} \${top_srcdir}/config/install-sh -c -d';;
1057esac
1058
1059PGAC_PATH_BISON
1060PGAC_PATH_FLEX
1061
1062PGAC_PATH_PERL
1063if test "$with_perl" = yes; then
1064  if test -z "$PERL"; then
1065    AC_MSG_ERROR([Perl not found])
1066  fi
1067  PGAC_CHECK_PERL_CONFIGS([archlibexp,privlibexp,useshrplib])
1068  if test "$perl_useshrplib" != yes && test "$perl_useshrplib" != true; then
1069    AC_MSG_ERROR([cannot build PL/Perl because libperl is not a shared library
1070You might have to rebuild your Perl installation.  Refer to the
1071documentation for details.  Use --without-perl to disable building
1072PL/Perl.])
1073  fi
1074  # On most platforms, archlibexp is also where the Perl include files live ...
1075  perl_includespec="-I$perl_archlibexp/CORE"
1076  # ... but on newer macOS versions, we must use -iwithsysroot to look
1077  # under $PG_SYSROOT
1078  if test \! -f "$perl_archlibexp/CORE/perl.h" ; then
1079    if test -f "$PG_SYSROOT$perl_archlibexp/CORE/perl.h" ; then
1080      perl_includespec="-iwithsysroot $perl_archlibexp/CORE"
1081    fi
1082  fi
1083  AC_SUBST(perl_includespec)dnl
1084  PGAC_CHECK_PERL_EMBED_CCFLAGS
1085  PGAC_CHECK_PERL_EMBED_LDFLAGS
1086fi
1087
1088if test "$with_python" = yes; then
1089  PGAC_PATH_PYTHON
1090  PGAC_CHECK_PYTHON_EMBED_SETUP
1091fi
1092
1093if test "$cross_compiling" = yes && test -z "$with_system_tzdata"; then
1094  PGAC_PATH_PROGS(ZIC, zic)
1095  if test -z "$ZIC"; then
1096    AC_MSG_ERROR([
1097When cross-compiling, either use the option --with-system-tzdata to use
1098existing time-zone data, or set the environment variable ZIC to a zic
1099program to use during the build.])
1100  fi
1101fi
1102
1103#
1104# Pthreads
1105#
1106# For each platform, we need to know about any special compile and link
1107# libraries, and whether the normal C function names are thread-safe.
1108# See the comment at the top of src/port/thread.c for more information.
1109# WIN32 doesn't need the pthread tests;  it always uses threads
1110#
1111# These tests are run before the library-tests, because linking with the
1112# other libraries can pull in the pthread functions as a side-effect.  We
1113# want to use the -pthread or similar flags directly, and not rely on
1114# the side-effects of linking with some other library.
1115
1116dnl note: We have to use AS_IF here rather than plain if. The AC_CHECK_HEADER
1117dnl invocation below is the first one in the script, and autoconf generates
1118dnl additional code for that, which must not be inside the if-block. AS_IF
1119dnl knows how to do that.
1120AS_IF([test "$enable_thread_safety" = yes -a "$PORTNAME" != "win32"],
1121[ # then
1122AX_PTHREAD	# set thread flags
1123
1124# Some platforms use these, so just define them.  They can't hurt if they
1125# are not supported.  For example, on Solaris -D_POSIX_PTHREAD_SEMANTICS
1126# enables 5-arg getpwuid_r, among other things.
1127PTHREAD_CFLAGS="$PTHREAD_CFLAGS -D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS"
1128
1129# Check for *_r functions
1130_CFLAGS="$CFLAGS"
1131_LIBS="$LIBS"
1132CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
1133LIBS="$LIBS $PTHREAD_LIBS"
1134
1135AC_CHECK_HEADER(pthread.h, [], [AC_MSG_ERROR([
1136pthread.h not found;  use --disable-thread-safety to disable thread safety])])
1137
1138AC_CHECK_FUNCS([strerror_r getpwuid_r gethostbyname_r])
1139
1140# Do test here with the proper thread flags
1141PGAC_FUNC_STRERROR_R_INT
1142
1143CFLAGS="$_CFLAGS"
1144LIBS="$_LIBS"
1145
1146], [ # else
1147# do not use values from template file
1148PTHREAD_CFLAGS=
1149PTHREAD_LIBS=
1150]) # fi
1151
1152AC_SUBST(PTHREAD_CFLAGS)
1153AC_SUBST(PTHREAD_LIBS)
1154
1155
1156##
1157## Libraries
1158##
1159## Most libraries are included only if they demonstrably provide a function
1160## we need, but libm is an exception: always include it, because there are
1161## too many compilers that play cute optimization games that will break
1162## probes for standard functions such as pow().
1163##
1164
1165AC_CHECK_LIB(m, main)
1166AC_SEARCH_LIBS(setproctitle, util)
1167AC_SEARCH_LIBS(dlopen, dl)
1168AC_SEARCH_LIBS(socket, [socket ws2_32])
1169AC_SEARCH_LIBS(shl_load, dld)
1170AC_SEARCH_LIBS(getopt_long, [getopt gnugetopt])
1171AC_SEARCH_LIBS(shm_open, rt)
1172AC_SEARCH_LIBS(shm_unlink, rt)
1173AC_SEARCH_LIBS(clock_gettime, [rt posix4])
1174# Solaris:
1175AC_SEARCH_LIBS(fdatasync, [rt posix4])
1176# Cygwin:
1177AC_SEARCH_LIBS(shmget, cygipc)
1178# *BSD:
1179AC_SEARCH_LIBS(backtrace_symbols, execinfo)
1180
1181if test "$enable_thread_safety" = yes; then
1182  AC_SEARCH_LIBS(gethostbyname_r, nsl)
1183  AC_SEARCH_LIBS(pthread_barrier_wait, pthread)
1184fi
1185
1186if test "$with_readline" = yes; then
1187  PGAC_CHECK_READLINE
1188  if test x"$pgac_cv_check_readline" = x"no"; then
1189    AC_MSG_ERROR([readline library not found
1190If you have readline already installed, see config.log for details on the
1191failure.  It is possible the compiler isn't looking in the proper directory.
1192Use --without-readline to disable readline support.])
1193  fi
1194fi
1195
1196if test "$with_zlib" = yes; then
1197  AC_CHECK_LIB(z, inflate, [],
1198               [AC_MSG_ERROR([zlib library not found
1199If you have zlib already installed, see config.log for details on the
1200failure.  It is possible the compiler isn't looking in the proper directory.
1201Use --without-zlib to disable zlib support.])])
1202fi
1203
1204if test "$enable_spinlocks" = yes; then
1205  AC_DEFINE(HAVE_SPINLOCKS, 1, [Define to 1 if you have spinlocks.])
1206else
1207  AC_MSG_WARN([
1208*** Not using spinlocks will cause poor performance.])
1209fi
1210
1211if test "$enable_atomics" = yes; then
1212  AC_DEFINE(HAVE_ATOMICS, 1, [Define to 1 if you want to use atomics if available.])
1213else
1214  AC_MSG_WARN([
1215*** Not using atomic operations will cause poor performance.])
1216fi
1217
1218if test "$with_gssapi" = yes ; then
1219  if test "$PORTNAME" != "win32"; then
1220    AC_SEARCH_LIBS(gss_init_sec_context, [gssapi_krb5 gss 'gssapi -lkrb5 -lcrypto'], [],
1221                   [AC_MSG_ERROR([could not find function 'gss_init_sec_context' required for GSSAPI])])
1222  else
1223    LIBS="$LIBS -lgssapi32"
1224  fi
1225fi
1226
1227#
1228# SSL Library
1229#
1230# There is currently only one supported SSL/TLS library: OpenSSL.
1231#
1232PGAC_ARG_REQ(with, ssl, [LIB], [use LIB for SSL/TLS support (openssl)])
1233if test x"$with_ssl" = x"" ; then
1234  with_ssl=no
1235fi
1236PGAC_ARG_BOOL(with, openssl, no, [obsolete spelling of --with-ssl=openssl])
1237if test "$with_openssl" = yes ; then
1238  with_ssl=openssl
1239fi
1240
1241if test "$with_ssl" = openssl ; then
1242  dnl Order matters!
1243  # Minimum required OpenSSL version is 1.0.1
1244  AC_DEFINE(OPENSSL_API_COMPAT, [0x10001000L],
1245            [Define to the OpenSSL API version in use. This avoids deprecation warnings from newer OpenSSL versions.])
1246  if test "$PORTNAME" != "win32"; then
1247     AC_CHECK_LIB(crypto, CRYPTO_new_ex_data, [], [AC_MSG_ERROR([library 'crypto' is required for OpenSSL])])
1248     AC_CHECK_LIB(ssl,    SSL_new, [], [AC_MSG_ERROR([library 'ssl' is required for OpenSSL])])
1249  else
1250     AC_SEARCH_LIBS(CRYPTO_new_ex_data, [eay32 crypto], [], [AC_MSG_ERROR([library 'eay32' or 'crypto' is required for OpenSSL])])
1251     AC_SEARCH_LIBS(SSL_new, [ssleay32 ssl], [], [AC_MSG_ERROR([library 'ssleay32' or 'ssl' is required for OpenSSL])])
1252  fi
1253  # Function introduced in OpenSSL 1.0.2.
1254  AC_CHECK_FUNCS([X509_get_signature_nid])
1255  # Functions introduced in OpenSSL 1.1.0. We used to check for
1256  # OPENSSL_VERSION_NUMBER, but that didn't work with 1.1.0, because LibreSSL
1257  # defines OPENSSL_VERSION_NUMBER to claim version 2.0.0, even though it
1258  # doesn't have these OpenSSL 1.1.0 functions. So check for individual
1259  # functions.
1260  AC_CHECK_FUNCS([OPENSSL_init_ssl BIO_get_data BIO_meth_new ASN1_STRING_get0_data HMAC_CTX_new HMAC_CTX_free])
1261  # OpenSSL versions before 1.1.0 required setting callback functions, for
1262  # thread-safety. In 1.1.0, it's no longer required, and CRYPTO_lock()
1263  # function was removed.
1264  AC_CHECK_FUNCS([CRYPTO_lock])
1265  AC_DEFINE([USE_OPENSSL], 1, [Define to 1 to build with OpenSSL support. (--with-ssl=openssl)])
1266elif test "$with_ssl" != no ; then
1267  AC_MSG_ERROR([--with-ssl must specify openssl])
1268fi
1269AC_SUBST(with_ssl)
1270
1271if test "$with_pam" = yes ; then
1272  AC_CHECK_LIB(pam,    pam_start, [], [AC_MSG_ERROR([library 'pam' is required for PAM])])
1273fi
1274
1275if test "$with_libxml" = yes ; then
1276  AC_CHECK_LIB(xml2, xmlSaveToBuffer, [], [AC_MSG_ERROR([library 'xml2' (version >= 2.6.23) is required for XML support])])
1277fi
1278
1279if test "$with_libxslt" = yes ; then
1280  AC_CHECK_LIB(xslt, xsltCleanupGlobals, [], [AC_MSG_ERROR([library 'xslt' is required for XSLT support])])
1281fi
1282
1283if test "$with_lz4" = yes ; then
1284  AC_CHECK_LIB(lz4, LZ4_compress_default, [], [AC_MSG_ERROR([library 'lz4' is required for LZ4 support])])
1285fi
1286
1287# Note: We can test for libldap_r only after we know PTHREAD_LIBS;
1288# also, on AIX, we may need to have openssl in LIBS for this step.
1289if test "$with_ldap" = yes ; then
1290  _LIBS="$LIBS"
1291  if test "$PORTNAME" != "win32"; then
1292    AC_CHECK_LIB(ldap, ldap_bind, [],
1293		 [AC_MSG_ERROR([library 'ldap' is required for LDAP])],
1294		 [$EXTRA_LDAP_LIBS])
1295    LDAP_LIBS_BE="-lldap $EXTRA_LDAP_LIBS"
1296    # This test is carried out against libldap.
1297    AC_CHECK_FUNCS([ldap_initialize])
1298    if test "$enable_thread_safety" = yes; then
1299      # Use ldap_r for FE if available, else assume ldap is thread-safe.
1300      # On some platforms ldap_r fails to link without PTHREAD_LIBS.
1301      LIBS="$_LIBS"
1302      AC_CHECK_LIB(ldap_r, ldap_bind,
1303		   [LDAP_LIBS_FE="-lldap_r $EXTRA_LDAP_LIBS"],
1304		   [LDAP_LIBS_FE="-lldap $EXTRA_LDAP_LIBS"],
1305		   [$PTHREAD_CFLAGS $PTHREAD_LIBS $EXTRA_LDAP_LIBS])
1306    else
1307      LDAP_LIBS_FE="-lldap $EXTRA_LDAP_LIBS"
1308    fi
1309  else
1310    AC_CHECK_LIB(wldap32, ldap_bind, [], [AC_MSG_ERROR([library 'wldap32' is required for LDAP])])
1311    LDAP_LIBS_FE="-lwldap32"
1312    LDAP_LIBS_BE="-lwldap32"
1313  fi
1314  LIBS="$_LIBS"
1315fi
1316AC_SUBST(LDAP_LIBS_FE)
1317AC_SUBST(LDAP_LIBS_BE)
1318
1319# for contrib/sepgsql
1320if test "$with_selinux" = yes; then
1321  AC_CHECK_LIB(selinux, security_compute_create_name, [],
1322               [AC_MSG_ERROR([library 'libselinux', version 2.1.10 or newer, is required for SELinux support])])
1323fi
1324
1325# for contrib/uuid-ossp
1326if test "$with_uuid" = bsd ; then
1327  # On BSD, the UUID functions are in libc
1328  AC_CHECK_FUNC(uuid_to_string,
1329    [UUID_LIBS=""],
1330    [AC_MSG_ERROR([BSD UUID functions are not present])])
1331elif test "$with_uuid" = e2fs ; then
1332  # On macOS, the UUID functions are in libc
1333  AC_CHECK_FUNC(uuid_generate,
1334    [UUID_LIBS=""],
1335    [AC_CHECK_LIB(uuid, uuid_generate,
1336      [UUID_LIBS="-luuid"],
1337      [AC_MSG_ERROR([library 'uuid' is required for E2FS UUID])])])
1338elif test "$with_uuid" = ossp ; then
1339  AC_CHECK_LIB(ossp-uuid, uuid_export,
1340    [UUID_LIBS="-lossp-uuid"],
1341    [AC_CHECK_LIB(uuid, uuid_export,
1342      [UUID_LIBS="-luuid"],
1343      [AC_MSG_ERROR([library 'ossp-uuid' or 'uuid' is required for OSSP UUID])])])
1344fi
1345AC_SUBST(UUID_LIBS)
1346
1347
1348##
1349## Header files
1350##
1351
1352AC_HEADER_STDBOOL
1353
1354AC_CHECK_HEADERS(m4_normalize([
1355	atomic.h
1356	copyfile.h
1357	execinfo.h
1358	getopt.h
1359	ifaddrs.h
1360	langinfo.h
1361	mbarrier.h
1362	poll.h
1363	sys/epoll.h
1364	sys/event.h
1365	sys/ipc.h
1366	sys/prctl.h
1367	sys/procctl.h
1368	sys/pstat.h
1369	sys/resource.h
1370	sys/select.h
1371	sys/sem.h
1372	sys/shm.h
1373	sys/sockio.h
1374	sys/tas.h
1375	sys/uio.h
1376	sys/un.h
1377	termios.h
1378	ucred.h
1379	wctype.h
1380]))
1381
1382# On BSD, test for net/if.h will fail unless sys/socket.h
1383# is included first.
1384AC_CHECK_HEADERS(net/if.h, [], [],
1385[AC_INCLUDES_DEFAULT
1386#include <sys/socket.h>
1387])
1388
1389# On OpenBSD, test for sys/ucred.h will fail unless sys/param.h
1390# is included first.
1391AC_CHECK_HEADERS(sys/ucred.h, [], [],
1392[AC_INCLUDES_DEFAULT
1393#include <sys/param.h>
1394])
1395
1396# At least on IRIX, test for netinet/tcp.h will fail unless
1397# netinet/in.h is included first.
1398AC_CHECK_HEADERS(netinet/tcp.h, [], [],
1399[AC_INCLUDES_DEFAULT
1400#include <netinet/in.h>
1401])
1402
1403if expr x"$pgac_cv_check_readline" : 'x-lreadline' >/dev/null ; then
1404  AC_CHECK_HEADERS(readline/readline.h, [],
1405        [AC_CHECK_HEADERS(readline.h, [],
1406                [AC_MSG_ERROR([readline header not found
1407If you have readline already installed, see config.log for details on the
1408failure.  It is possible the compiler isn't looking in the proper directory.
1409Use --without-readline to disable readline support.])])])
1410  AC_CHECK_HEADERS(readline/history.h, [],
1411        [AC_CHECK_HEADERS(history.h, [],
1412                [AC_MSG_ERROR([history header not found
1413If you have readline already installed, see config.log for details on the
1414failure.  It is possible the compiler isn't looking in the proper directory.
1415Use --without-readline to disable readline support.])])])
1416fi
1417
1418if expr x"$pgac_cv_check_readline" : 'x-ledit' >/dev/null ; then
1419# Some installations of libedit usurp /usr/include/readline/, which seems
1420# bad practice, since in combined installations readline will have its headers
1421# there.  We might have to resort to AC_EGREP checks to make sure we found
1422# the proper header...
1423  AC_CHECK_HEADERS(editline/readline.h, [],
1424        [AC_CHECK_HEADERS(readline.h, [],
1425                [AC_CHECK_HEADERS(readline/readline.h, [],
1426                        [AC_MSG_ERROR([readline header not found
1427If you have libedit already installed, see config.log for details on the
1428failure.  It is possible the compiler isn't looking in the proper directory.
1429Use --without-readline to disable libedit support.])])])])
1430# Note: in a libedit installation, history.h is sometimes a dummy, and may
1431# not be there at all.  Hence, don't complain if not found.  We must check
1432# though, since in yet other versions it is an independent header.
1433  AC_CHECK_HEADERS(editline/history.h, [],
1434        [AC_CHECK_HEADERS(history.h, [],
1435                [AC_CHECK_HEADERS(readline/history.h)])])
1436fi
1437
1438if test "$with_zlib" = yes; then
1439  AC_CHECK_HEADER(zlib.h, [], [AC_MSG_ERROR([zlib header not found
1440If you have zlib already installed, see config.log for details on the
1441failure.  It is possible the compiler isn't looking in the proper directory.
1442Use --without-zlib to disable zlib support.])])
1443fi
1444
1445if test "$with_lz4" = yes; then
1446  AC_CHECK_HEADERS(lz4.h, [], [AC_MSG_ERROR([lz4.h header file is required for LZ4])])
1447fi
1448
1449if test "$with_gssapi" = yes ; then
1450  AC_CHECK_HEADERS(gssapi/gssapi.h, [],
1451	[AC_CHECK_HEADERS(gssapi.h, [], [AC_MSG_ERROR([gssapi.h header file is required for GSSAPI])])])
1452fi
1453
1454if test "$with_ssl" = openssl ; then
1455  AC_CHECK_HEADER(openssl/ssl.h, [], [AC_MSG_ERROR([header file <openssl/ssl.h> is required for OpenSSL])])
1456  AC_CHECK_HEADER(openssl/err.h, [], [AC_MSG_ERROR([header file <openssl/err.h> is required for OpenSSL])])
1457fi
1458
1459if test "$with_pam" = yes ; then
1460  AC_CHECK_HEADERS(security/pam_appl.h, [],
1461                   [AC_CHECK_HEADERS(pam/pam_appl.h, [],
1462                                     [AC_MSG_ERROR([header file <security/pam_appl.h> or <pam/pam_appl.h> is required for PAM.])])])
1463fi
1464
1465if test "$with_bsd_auth" = yes ; then
1466  AC_CHECK_HEADER(bsd_auth.h, [], [AC_MSG_ERROR([header file <bsd_auth.h> is required for BSD Authentication support])])
1467fi
1468
1469if test "$with_systemd" = yes ; then
1470  AC_CHECK_HEADER(systemd/sd-daemon.h, [], [AC_MSG_ERROR([header file <systemd/sd-daemon.h> is required for systemd support])])
1471fi
1472
1473if test "$with_libxml" = yes ; then
1474  AC_CHECK_HEADER(libxml/parser.h, [], [AC_MSG_ERROR([header file <libxml/parser.h> is required for XML support])])
1475fi
1476
1477if test "$with_libxslt" = yes ; then
1478  AC_CHECK_HEADER(libxslt/xslt.h, [], [AC_MSG_ERROR([header file <libxslt/xslt.h> is required for XSLT support])])
1479fi
1480
1481if test "$with_ldap" = yes ; then
1482  if test "$PORTNAME" != "win32"; then
1483     AC_CHECK_HEADERS(ldap.h, [],
1484                      [AC_MSG_ERROR([header file <ldap.h> is required for LDAP])])
1485     PGAC_LDAP_SAFE
1486  else
1487     AC_CHECK_HEADERS(winldap.h, [],
1488                      [AC_MSG_ERROR([header file <winldap.h> is required for LDAP])],
1489                      [AC_INCLUDES_DEFAULT
1490#include <windows.h>
1491                      ])
1492  fi
1493fi
1494
1495if test "$with_bonjour" = yes ; then
1496  AC_CHECK_HEADER(dns_sd.h, [], [AC_MSG_ERROR([header file <dns_sd.h> is required for Bonjour])])
1497dnl At some point we might add something like
1498dnl AC_SEARCH_LIBS(DNSServiceRegister, dns_sd)
1499dnl but right now, what that would mainly accomplish is to encourage
1500dnl people to try to use the avahi implementation, which does not work.
1501dnl If you want to use Apple's own Bonjour code on another platform,
1502dnl just add -ldns_sd to LIBS manually.
1503fi
1504
1505# for contrib/uuid-ossp
1506if test "$with_uuid" = bsd ; then
1507  AC_CHECK_HEADERS(uuid.h,
1508    [AC_EGREP_HEADER([uuid_to_string], uuid.h, [],
1509      [AC_MSG_ERROR([header file <uuid.h> does not match BSD UUID library])])],
1510    [AC_MSG_ERROR([header file <uuid.h> is required for BSD UUID])])
1511elif test "$with_uuid" = e2fs ; then
1512  AC_CHECK_HEADERS(uuid/uuid.h,
1513    [AC_EGREP_HEADER([uuid_generate], uuid/uuid.h, [],
1514      [AC_MSG_ERROR([header file <uuid/uuid.h> does not match E2FS UUID library])])],
1515    [AC_CHECK_HEADERS(uuid.h,
1516      [AC_EGREP_HEADER([uuid_generate], uuid.h, [],
1517        [AC_MSG_ERROR([header file <uuid.h> does not match E2FS UUID library])])],
1518      [AC_MSG_ERROR([header file <uuid/uuid.h> or <uuid.h> is required for E2FS UUID])])])
1519elif test "$with_uuid" = ossp ; then
1520  AC_CHECK_HEADERS(ossp/uuid.h,
1521    [AC_EGREP_HEADER([uuid_export], ossp/uuid.h, [],
1522      [AC_MSG_ERROR([header file <ossp/uuid.h> does not match OSSP UUID library])])],
1523    [AC_CHECK_HEADERS(uuid.h,
1524      [AC_EGREP_HEADER([uuid_export], uuid.h, [],
1525        [AC_MSG_ERROR([header file <uuid.h> does not match OSSP UUID library])])],
1526      [AC_MSG_ERROR([header file <ossp/uuid.h> or <uuid.h> is required for OSSP UUID])])])
1527fi
1528
1529if test "$PORTNAME" = "win32" ; then
1530   AC_CHECK_HEADERS(crtdefs.h)
1531fi
1532
1533##
1534## Types, structures, compiler characteristics
1535##
1536
1537m4_defun([AC_PROG_CC_STDC], []) dnl We don't want that.
1538AC_C_BIGENDIAN
1539AC_C_INLINE
1540PGAC_PRINTF_ARCHETYPE
1541PGAC_C_FUNCNAME_SUPPORT
1542PGAC_C_STATIC_ASSERT
1543PGAC_C_TYPEOF
1544PGAC_C_TYPES_COMPATIBLE
1545PGAC_C_BUILTIN_CONSTANT_P
1546PGAC_C_BUILTIN_UNREACHABLE
1547PGAC_C_COMPUTED_GOTO
1548PGAC_STRUCT_TIMEZONE
1549PGAC_UNION_SEMUN
1550PGAC_STRUCT_SOCKADDR_UN
1551PGAC_STRUCT_SOCKADDR_STORAGE
1552PGAC_STRUCT_SOCKADDR_STORAGE_MEMBERS
1553PGAC_STRUCT_ADDRINFO
1554
1555PGAC_TYPE_LOCALE_T
1556
1557# MSVC doesn't cope well with defining restrict to __restrict, the
1558# spelling it understands, because it conflicts with
1559# __declspec(restrict). Therefore we define pg_restrict to the
1560# appropriate definition, which presumably won't conflict.
1561#
1562# Allow platforms with buggy compilers to force restrict to not be
1563# used by setting $FORCE_DISABLE_RESTRICT=yes in the relevant
1564# template.
1565AC_C_RESTRICT
1566if test "$ac_cv_c_restrict" = "no" -o "x$FORCE_DISABLE_RESTRICT" = "xyes"; then
1567  pg_restrict=""
1568else
1569  pg_restrict="$ac_cv_c_restrict"
1570fi
1571AC_DEFINE_UNQUOTED([pg_restrict], [$pg_restrict],
1572[Define to keyword to use for C99 restrict support, or to nothing if not
1573supported])
1574
1575AC_CHECK_TYPES([struct cmsgcred], [], [],
1576[#include <sys/socket.h>
1577#include <sys/param.h>
1578#ifdef HAVE_SYS_UCRED_H
1579#include <sys/ucred.h>
1580#endif])
1581
1582AC_CHECK_TYPES([struct option], [], [],
1583[#ifdef HAVE_GETOPT_H
1584#include <getopt.h>
1585#endif])
1586
1587if test "$with_zlib" = yes; then
1588  # Check that <zlib.h> defines z_streamp (versions before about 1.0.4
1589  # did not).  While we could work around the lack of z_streamp, it
1590  # seems unwise to encourage people to use such old zlib versions...
1591  AC_CHECK_TYPE(z_streamp, [], [AC_MSG_ERROR([zlib version is too old
1592Use --without-zlib to disable zlib support.])],
1593                [#include <zlib.h>])
1594fi
1595
1596case $host_cpu in
1597  x86_64)
1598    # On x86_64, check if we can compile a popcntq instruction
1599    AC_CACHE_CHECK([whether assembler supports x86_64 popcntq],
1600                   [pgac_cv_have_x86_64_popcntq],
1601    [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],
1602	[long long x = 1; long long r;
1603	 __asm__ __volatile__ (" popcntq %1,%0\n" : "=q"(r) : "rm"(x));])],
1604	[pgac_cv_have_x86_64_popcntq=yes],
1605	[pgac_cv_have_x86_64_popcntq=no])])
1606    if test x"$pgac_cv_have_x86_64_popcntq" = xyes ; then
1607	AC_DEFINE(HAVE_X86_64_POPCNTQ, 1, [Define to 1 if the assembler supports X86_64's POPCNTQ instruction.])
1608    fi
1609  ;;
1610  ppc*|powerpc*)
1611    # On PPC, check if assembler supports LWARX instruction's mutex hint bit
1612    AC_CACHE_CHECK([whether assembler supports lwarx hint bit],
1613                   [pgac_cv_have_ppc_mutex_hint],
1614    [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],
1615	[int a = 0; int *p = &a; int r;
1616	 __asm__ __volatile__ (" lwarx %0,0,%1,1\n" : "=&r"(r) : "r"(p));])],
1617	[pgac_cv_have_ppc_mutex_hint=yes],
1618	[pgac_cv_have_ppc_mutex_hint=no])])
1619    if test x"$pgac_cv_have_ppc_mutex_hint" = xyes ; then
1620	AC_DEFINE(HAVE_PPC_LWARX_MUTEX_HINT, 1, [Define to 1 if the assembler supports PPC's LWARX mutex hint bit.])
1621    fi
1622    # Check if compiler accepts "i"(x) when __builtin_constant_p(x).
1623    AC_CACHE_CHECK([whether __builtin_constant_p(x) implies "i"(x) acceptance],
1624                   [pgac_cv_have_i_constraint__builtin_constant_p],
1625    [AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
1626    [static inline int
1627     addi(int ra, int si)
1628     {
1629         int res = 0;
1630         if (__builtin_constant_p(si))
1631             __asm__ __volatile__(
1632                 " addi %0,%1,%2\n" : "=r"(res) : "b"(ra), "i"(si));
1633         return res;
1634     }
1635     int test_adds(int x) { return addi(3, x) + addi(x, 5); }], [])],
1636    [pgac_cv_have_i_constraint__builtin_constant_p=yes],
1637    [pgac_cv_have_i_constraint__builtin_constant_p=no])])
1638    if test x"$pgac_cv_have_i_constraint__builtin_constant_p" = xyes ; then
1639      AC_DEFINE(HAVE_I_CONSTRAINT__BUILTIN_CONSTANT_P, 1,
1640                [Define to 1 if __builtin_constant_p(x) implies "i"(x) acceptance.])
1641    fi
1642  ;;
1643esac
1644
1645# Check largefile support.  You might think this is a system service not a
1646# compiler characteristic, but you'd be wrong.  We must check this before
1647# probing existence of related functions such as fseeko, since the largefile
1648# defines can affect what is generated for that.
1649if test "$PORTNAME" != "win32"; then
1650   AC_SYS_LARGEFILE
1651   dnl Autoconf 2.69's AC_SYS_LARGEFILE believes it's a good idea to #define
1652   dnl _DARWIN_USE_64_BIT_INODE, but it isn't: on macOS 10.5 that activates a
1653   dnl bug that causes readdir() to sometimes return EINVAL.  On later macOS
1654   dnl versions where the feature actually works, it's on by default anyway.
1655   AH_VERBATIM([_DARWIN_USE_64_BIT_INODE],[])
1656fi
1657
1658dnl Check for largefile support (must be after AC_SYS_LARGEFILE)
1659AC_CHECK_SIZEOF([off_t])
1660
1661# If we don't have largefile support, can't handle segsize >= 2GB.
1662if test "$ac_cv_sizeof_off_t" -lt 8 -a "$segsize" != "1"; then
1663   AC_MSG_ERROR([Large file support is not enabled. Segment size cannot be larger than 1GB.])
1664fi
1665
1666AC_CHECK_SIZEOF([bool], [],
1667[#ifdef HAVE_STDBOOL_H
1668#include <stdbool.h>
1669#endif])
1670
1671dnl We use <stdbool.h> if we have it and it declares type bool as having
1672dnl size 1.  Otherwise, c.h will fall back to declaring bool as unsigned char.
1673if test "$ac_cv_header_stdbool_h" = yes -a "$ac_cv_sizeof_bool" = 1; then
1674  AC_DEFINE([PG_USE_STDBOOL], 1,
1675            [Define to 1 to use <stdbool.h> to define type bool.])
1676fi
1677
1678
1679##
1680## Functions, global variables
1681##
1682
1683PGAC_VAR_INT_TIMEZONE
1684AC_FUNC_ACCEPT_ARGTYPES
1685PGAC_FUNC_GETTIMEOFDAY_1ARG
1686PGAC_FUNC_WCSTOMBS_L
1687
1688# Some versions of libedit contain strlcpy(), setproctitle(), and other
1689# symbols that that library has no business exposing to the world.  Pending
1690# acquisition of a clue by those developers, ignore libedit (including its
1691# possible alias of libreadline) while checking for everything else.
1692LIBS_including_readline="$LIBS"
1693LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
1694
1695AC_CHECK_FUNCS(m4_normalize([
1696	backtrace_symbols
1697	clock_gettime
1698	copyfile
1699	fdatasync
1700	getifaddrs
1701	getpeerucred
1702	getrlimit
1703	kqueue
1704	mbstowcs_l
1705	memset_s
1706	poll
1707	posix_fallocate
1708	ppoll
1709	pstat
1710	pthread_is_threaded_np
1711	readlink
1712	readv
1713	setproctitle
1714	setproctitle_fast
1715	setsid
1716	shm_open
1717	strchrnul
1718	strsignal
1719	symlink
1720	syncfs
1721	sync_file_range
1722	uselocale
1723	wcstombs_l
1724	writev
1725]))
1726
1727# These typically are compiler builtins, for which AC_CHECK_FUNCS fails.
1728PGAC_CHECK_BUILTIN_FUNC([__builtin_bswap16], [int x])
1729PGAC_CHECK_BUILTIN_FUNC([__builtin_bswap32], [int x])
1730PGAC_CHECK_BUILTIN_FUNC([__builtin_bswap64], [long int x])
1731# We assume that we needn't test all widths of these explicitly:
1732PGAC_CHECK_BUILTIN_FUNC([__builtin_clz], [unsigned int x])
1733PGAC_CHECK_BUILTIN_FUNC([__builtin_ctz], [unsigned int x])
1734PGAC_CHECK_BUILTIN_FUNC([__builtin_popcount], [unsigned int x])
1735
1736# We require 64-bit fseeko() to be available, but run this check anyway
1737# in case it finds that _LARGEFILE_SOURCE has to be #define'd for that.
1738AC_FUNC_FSEEKO
1739
1740# posix_fadvise() is a no-op on Solaris, so don't incur function overhead
1741# by calling it, 2009-04-02
1742# http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libc/port/gen/posix_fadvise.c
1743dnl must use AS_IF here, else AC_REQUIRES inside AC_CHECK_DECLS malfunctions
1744AS_IF([test "$PORTNAME" != "solaris"], [
1745AC_CHECK_FUNCS(posix_fadvise)
1746AC_CHECK_DECLS(posix_fadvise, [], [], [#include <fcntl.h>])
1747]) # fi
1748
1749AC_CHECK_DECLS(fdatasync, [], [], [#include <unistd.h>])
1750AC_CHECK_DECLS([strlcat, strlcpy, strnlen])
1751
1752# We can't use AC_REPLACE_FUNCS to replace these functions, because it
1753# won't handle deployment target restrictions on macOS
1754AC_CHECK_DECLS([preadv], [], [AC_LIBOBJ(preadv)], [#include <sys/uio.h>])
1755AC_CHECK_DECLS([pwritev], [], [AC_LIBOBJ(pwritev)], [#include <sys/uio.h>])
1756
1757# This is probably only present on macOS, but may as well check always
1758AC_CHECK_DECLS(F_FULLFSYNC, [], [], [#include <fcntl.h>])
1759
1760AC_CHECK_DECLS([RTLD_GLOBAL, RTLD_NOW], [], [], [#include <dlfcn.h>])
1761
1762AC_CHECK_TYPE([struct sockaddr_in6],
1763        [AC_DEFINE(HAVE_IPV6, 1, [Define to 1 if you have support for IPv6.])],
1764        [],
1765[$ac_includes_default
1766#include <netinet/in.h>])
1767
1768AC_CACHE_CHECK([for PS_STRINGS], [pgac_cv_var_PS_STRINGS],
1769[AC_LINK_IFELSE([AC_LANG_PROGRAM(
1770[#include <machine/vmparam.h>
1771#include <sys/exec.h>
1772],
1773[PS_STRINGS->ps_nargvstr = 1;
1774PS_STRINGS->ps_argvstr = "foo";])],
1775[pgac_cv_var_PS_STRINGS=yes],
1776[pgac_cv_var_PS_STRINGS=no])])
1777if test "$pgac_cv_var_PS_STRINGS" = yes ; then
1778  AC_DEFINE([HAVE_PS_STRINGS], 1, [Define to 1 if the PS_STRINGS thing exists.])
1779fi
1780
1781AC_REPLACE_FUNCS(m4_normalize([
1782	dlopen
1783	explicit_bzero
1784	fls
1785	getopt
1786	getpeereid
1787	getrusage
1788	inet_aton
1789	link
1790	mkdtemp
1791	pread
1792	pwrite
1793	random
1794	srandom
1795	strlcat
1796	strlcpy
1797	strnlen
1798	strtof
1799]))
1800
1801if test "$enable_thread_safety" = yes; then
1802  AC_REPLACE_FUNCS(pthread_barrier_wait)
1803fi
1804
1805if test "$PORTNAME" = "win32" -o "$PORTNAME" = "cygwin"; then
1806	# Cygwin and (apparently, based on test results) Mingw both
1807	# have a broken strtof(), so substitute the same replacement
1808	# code we use with VS2013. That's not a perfect fix, since
1809	# (unlike with VS2013) it doesn't avoid double-rounding, but
1810	# we have no better options. To get that, though, we have to
1811	# force the file to be compiled despite HAVE_STRTOF.
1812	AC_LIBOBJ([strtof])
1813	AC_MSG_NOTICE([On $host_os we will use our strtof wrapper.])
1814fi
1815
1816case $host_os in
1817        # Windows uses a specialised env handler
1818        mingw*)
1819                AC_DEFINE(HAVE_SETENV, 1, [Define to 1 because replacement version used.])
1820                AC_DEFINE(HAVE_UNSETENV, 1, [Define to 1 because replacement version used.])
1821                ac_cv_func_setenv=yes
1822                ac_cv_func_unsetenv=yes
1823                ;;
1824        *)
1825                AC_REPLACE_FUNCS([setenv unsetenv])
1826                ;;
1827esac
1828
1829# System's version of getaddrinfo(), if any, may be used only if we found
1830# a definition for struct addrinfo; see notes in src/include/getaddrinfo.h.
1831# We use only our own getaddrinfo.c on Windows, but it's time to revisit that.
1832if test x"$ac_cv_type_struct_addrinfo" = xyes && \
1833   test "$PORTNAME" != "win32"; then
1834  AC_REPLACE_FUNCS([getaddrinfo])
1835else
1836  AC_LIBOBJ(getaddrinfo)
1837fi
1838
1839# Similarly, use system's getopt_long() only if system provides struct option.
1840if test x"$ac_cv_type_struct_option" = xyes ; then
1841  AC_REPLACE_FUNCS([getopt_long])
1842else
1843  AC_LIBOBJ(getopt_long)
1844fi
1845
1846# On OpenBSD and Solaris, getopt() doesn't do what we want for long options
1847# (i.e., allow '-' as a flag character), so use our version on those platforms.
1848if test "$PORTNAME" = "openbsd" -o "$PORTNAME" = "solaris"; then
1849  AC_LIBOBJ(getopt)
1850fi
1851
1852# mingw has adopted a GNU-centric interpretation of optind/optreset,
1853# so always use our version on Windows.
1854if test "$PORTNAME" = "win32"; then
1855  AC_LIBOBJ(getopt)
1856  AC_LIBOBJ(getopt_long)
1857fi
1858
1859# Win32 (really MinGW) support
1860if test "$PORTNAME" = "win32"; then
1861  AC_CHECK_FUNCS(_configthreadlocale)
1862  AC_REPLACE_FUNCS(gettimeofday)
1863  AC_LIBOBJ(dirmod)
1864  AC_LIBOBJ(kill)
1865  AC_LIBOBJ(open)
1866  AC_LIBOBJ(system)
1867  AC_LIBOBJ(win32env)
1868  AC_LIBOBJ(win32error)
1869  AC_LIBOBJ(win32security)
1870  AC_LIBOBJ(win32setlocale)
1871  AC_LIBOBJ(win32stat)
1872  AC_DEFINE([HAVE_SYMLINK], 1,
1873            [Define to 1 if you have the `symlink' function.])
1874  AC_CHECK_TYPES(MINIDUMP_TYPE, [pgac_minidump_type=yes], [pgac_minidump_type=no], [
1875#define WIN32_LEAN_AND_MEAN
1876#include <windows.h>
1877#include <string.h>
1878#include <dbghelp.h>])
1879fi
1880if test x"$pgac_minidump_type" = x"yes" ; then
1881  AC_SUBST(have_win32_dbghelp,yes)
1882else
1883  AC_SUBST(have_win32_dbghelp,no)
1884fi
1885
1886# Cygwin needs only a bit of that
1887if test "$PORTNAME" = "cygwin"; then
1888  AC_LIBOBJ(dirmod)
1889fi
1890
1891AC_CHECK_FUNC(syslog,
1892              [AC_CHECK_HEADER(syslog.h,
1893                               [AC_DEFINE(HAVE_SYSLOG, 1, [Define to 1 if you have the syslog interface.])])])
1894
1895AC_CACHE_CHECK([for opterr], pgac_cv_var_int_opterr,
1896[AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <unistd.h>],
1897  [extern int opterr; opterr = 1;])],
1898  [pgac_cv_var_int_opterr=yes],
1899  [pgac_cv_var_int_opterr=no])])
1900if test x"$pgac_cv_var_int_opterr" = x"yes"; then
1901  AC_DEFINE(HAVE_INT_OPTERR, 1, [Define to 1 if you have the global variable 'int opterr'.])
1902fi
1903
1904AC_CACHE_CHECK([for optreset], pgac_cv_var_int_optreset,
1905[AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <unistd.h>],
1906  [extern int optreset; optreset = 1;])],
1907  [pgac_cv_var_int_optreset=yes],
1908  [pgac_cv_var_int_optreset=no])])
1909if test x"$pgac_cv_var_int_optreset" = x"yes"; then
1910  AC_DEFINE(HAVE_INT_OPTRESET, 1, [Define to 1 if you have the global variable 'int optreset'.])
1911fi
1912
1913AC_CHECK_FUNCS([strtoll __strtoll strtoq], [break])
1914AC_CHECK_FUNCS([strtoull __strtoull strtouq], [break])
1915# strto[u]ll may exist but not be declared
1916AC_CHECK_DECLS([strtoll, strtoull])
1917
1918if test "$with_icu" = yes; then
1919  ac_save_CPPFLAGS=$CPPFLAGS
1920  CPPFLAGS="$ICU_CFLAGS $CPPFLAGS"
1921
1922  # Verify we have ICU's header files
1923  AC_CHECK_HEADER(unicode/ucol.h, [],
1924        [AC_MSG_ERROR([header file <unicode/ucol.h> is required for ICU])])
1925
1926  CPPFLAGS=$ac_save_CPPFLAGS
1927fi
1928
1929if test "$with_llvm" = yes; then
1930  PGAC_CHECK_LLVM_FUNCTIONS()
1931fi
1932
1933# Lastly, restore full LIBS list and check for readline/libedit symbols
1934LIBS="$LIBS_including_readline"
1935
1936if test "$with_readline" = yes; then
1937  PGAC_READLINE_VARIABLES
1938  AC_CHECK_FUNCS([rl_completion_matches rl_filename_completion_function rl_reset_screen_size])
1939  AC_CHECK_FUNCS([append_history history_truncate_file])
1940fi
1941
1942
1943# This test makes sure that run tests work at all.  Sometimes a shared
1944# library is found by the linker, but the runtime linker can't find it.
1945# This check should come after all modifications of compiler or linker
1946# variables, and before any other run tests.
1947AC_MSG_CHECKING([test program])
1948AC_RUN_IFELSE([AC_LANG_SOURCE([int main() { return 0; }])],
1949[AC_MSG_RESULT(ok)],
1950[AC_MSG_RESULT(failed)
1951AC_MSG_ERROR([[
1952Could not execute a simple test program.  This may be a problem
1953related to locating shared libraries.  Check the file 'config.log'
1954for the exact reason.]])],
1955[AC_MSG_RESULT([cross-compiling])])
1956
1957# --------------------
1958# Run tests below here
1959# --------------------
1960
1961dnl Check to see if we have a working 64-bit integer type.
1962dnl Since Postgres 8.4, we no longer support compilers without a working
1963dnl 64-bit type; but we have to determine whether that type is called
1964dnl "long int" or "long long int".
1965
1966PGAC_TYPE_64BIT_INT([long int])
1967
1968if test x"$HAVE_LONG_INT_64" = x"yes" ; then
1969  pg_int64_type="long int"
1970else
1971  PGAC_TYPE_64BIT_INT([long long int])
1972  if test x"$HAVE_LONG_LONG_INT_64" = x"yes" ; then
1973    pg_int64_type="long long int"
1974  else
1975    AC_MSG_ERROR([Cannot find a working 64-bit integer type.])
1976  fi
1977fi
1978
1979AC_DEFINE_UNQUOTED(PG_INT64_TYPE, $pg_int64_type,
1980  [Define to the name of a signed 64-bit integer type.])
1981
1982# Select the printf length modifier that goes with that, too.
1983if test x"$pg_int64_type" = x"long long int" ; then
1984  INT64_MODIFIER='"ll"'
1985else
1986  INT64_MODIFIER='"l"'
1987fi
1988
1989AC_DEFINE_UNQUOTED(INT64_MODIFIER, $INT64_MODIFIER,
1990                   [Define to the appropriate printf length modifier for 64-bit ints.])
1991
1992# has to be down here, rather than with the other builtins, because
1993# the test uses PG_INT64_TYPE.
1994PGAC_C_BUILTIN_OP_OVERFLOW
1995
1996# Check size of void *, size_t (enables tweaks for > 32bit address space)
1997AC_CHECK_SIZEOF([void *])
1998AC_CHECK_SIZEOF([size_t])
1999AC_CHECK_SIZEOF([long])
2000
2001# Determine memory alignment requirements for the basic C data types.
2002
2003AC_CHECK_ALIGNOF(short)
2004AC_CHECK_ALIGNOF(int)
2005AC_CHECK_ALIGNOF(long)
2006if test x"$HAVE_LONG_LONG_INT_64" = x"yes" ; then
2007  AC_CHECK_ALIGNOF(long long int)
2008fi
2009AC_CHECK_ALIGNOF(double)
2010
2011# Compute maximum alignment of any basic type.
2012# We assume long's alignment is at least as strong as char, short, or int;
2013# but we must check long long (if it is being used for int64) and double.
2014# Note that we intentionally do not consider any types wider than 64 bits,
2015# as allowing MAXIMUM_ALIGNOF to exceed 8 would be too much of a penalty
2016# for disk and memory space.
2017
2018MAX_ALIGNOF=$ac_cv_alignof_long
2019if test $MAX_ALIGNOF -lt $ac_cv_alignof_double ; then
2020  MAX_ALIGNOF=$ac_cv_alignof_double
2021fi
2022if test x"$HAVE_LONG_LONG_INT_64" = xyes && test $MAX_ALIGNOF -lt $ac_cv_alignof_long_long_int ; then
2023  MAX_ALIGNOF="$ac_cv_alignof_long_long_int"
2024fi
2025AC_DEFINE_UNQUOTED(MAXIMUM_ALIGNOF, $MAX_ALIGNOF, [Define as the maximum alignment requirement of any C data type.])
2026
2027
2028# Some platforms predefine the types int8, int16, etc.  Only check
2029# a (hopefully) representative subset.
2030AC_CHECK_TYPES([int8, uint8, int64, uint64], [], [],
2031[#include <stdio.h>])
2032
2033# Some compilers offer a 128-bit integer scalar type.
2034PGAC_TYPE_128BIT_INT
2035
2036# Check for various atomic operations now that we have checked how to declare
2037# 64bit integers.
2038PGAC_HAVE_GCC__SYNC_CHAR_TAS
2039PGAC_HAVE_GCC__SYNC_INT32_TAS
2040PGAC_HAVE_GCC__SYNC_INT32_CAS
2041PGAC_HAVE_GCC__SYNC_INT64_CAS
2042PGAC_HAVE_GCC__ATOMIC_INT32_CAS
2043PGAC_HAVE_GCC__ATOMIC_INT64_CAS
2044
2045
2046# Check for x86 cpuid instruction
2047AC_CACHE_CHECK([for __get_cpuid], [pgac_cv__get_cpuid],
2048[AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <cpuid.h>],
2049  [[unsigned int exx[4] = {0, 0, 0, 0};
2050  __get_cpuid(1, &exx[0], &exx[1], &exx[2], &exx[3]);
2051  ]])],
2052  [pgac_cv__get_cpuid="yes"],
2053  [pgac_cv__get_cpuid="no"])])
2054if test x"$pgac_cv__get_cpuid" = x"yes"; then
2055  AC_DEFINE(HAVE__GET_CPUID, 1, [Define to 1 if you have __get_cpuid.])
2056fi
2057
2058AC_CACHE_CHECK([for __cpuid], [pgac_cv__cpuid],
2059[AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <intrin.h>],
2060  [[unsigned int exx[4] = {0, 0, 0, 0};
2061  __get_cpuid(exx[0], 1);
2062  ]])],
2063  [pgac_cv__cpuid="yes"],
2064  [pgac_cv__cpuid="no"])])
2065if test x"$pgac_cv__cpuid" = x"yes"; then
2066  AC_DEFINE(HAVE__CPUID, 1, [Define to 1 if you have __cpuid.])
2067fi
2068
2069# Check for Intel SSE 4.2 intrinsics to do CRC calculations.
2070#
2071# First check if the _mm_crc32_u8 and _mm_crc32_u64 intrinsics can be used
2072# with the default compiler flags. If not, check if adding the -msse4.2
2073# flag helps. CFLAGS_SSE42 is set to -msse4.2 if that's required.
2074PGAC_SSE42_CRC32_INTRINSICS([])
2075if test x"$pgac_sse42_crc32_intrinsics" != x"yes"; then
2076  PGAC_SSE42_CRC32_INTRINSICS([-msse4.2])
2077fi
2078AC_SUBST(CFLAGS_SSE42)
2079
2080# Are we targeting a processor that supports SSE 4.2? gcc, clang and icc all
2081# define __SSE4_2__ in that case.
2082AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [
2083#ifndef __SSE4_2__
2084#error __SSE4_2__ not defined
2085#endif
2086])], [SSE4_2_TARGETED=1])
2087
2088# Check for ARMv8 CRC Extension intrinsics to do CRC calculations.
2089#
2090# First check if __crc32c* intrinsics can be used with the default compiler
2091# flags. If not, check if adding -march=armv8-a+crc flag helps.
2092# CFLAGS_ARMV8_CRC32C is set if the extra flag is required.
2093PGAC_ARMV8_CRC32C_INTRINSICS([])
2094if test x"$pgac_armv8_crc32c_intrinsics" != x"yes"; then
2095  PGAC_ARMV8_CRC32C_INTRINSICS([-march=armv8-a+crc])
2096fi
2097AC_SUBST(CFLAGS_ARMV8_CRC32C)
2098
2099# Select CRC-32C implementation.
2100#
2101# If we are targeting a processor that has Intel SSE 4.2 instructions, we can
2102# use the special CRC instructions for calculating CRC-32C. If we're not
2103# targeting such a processor, but we can nevertheless produce code that uses
2104# the SSE intrinsics, perhaps with some extra CFLAGS, compile both
2105# implementations and select which one to use at runtime, depending on whether
2106# SSE 4.2 is supported by the processor we're running on.
2107#
2108# Similarly, if we are targeting an ARM processor that has the CRC
2109# instructions that are part of the ARMv8 CRC Extension, use them. And if
2110# we're not targeting such a processor, but can nevertheless produce code that
2111# uses the CRC instructions, compile both, and select at runtime.
2112#
2113# You can override this logic by setting the appropriate USE_*_CRC32 flag to 1
2114# in the template or configure command line.
2115if test x"$USE_SLICING_BY_8_CRC32C" = x"" && test x"$USE_SSE42_CRC32C" = x"" && test x"$USE_SSE42_CRC32C_WITH_RUNTIME_CHECK" = x"" && test x"$USE_ARMV8_CRC32C" = x"" && test x"$USE_ARMV8_CRC32C_WITH_RUNTIME_CHECK" = x""; then
2116  # Use Intel SSE 4.2 if available.
2117  if test x"$pgac_sse42_crc32_intrinsics" = x"yes" && test x"$SSE4_2_TARGETED" = x"1" ; then
2118    USE_SSE42_CRC32C=1
2119  else
2120    # Intel SSE 4.2, with runtime check? The CPUID instruction is needed for
2121    # the runtime check.
2122    if test x"$pgac_sse42_crc32_intrinsics" = x"yes" && (test x"$pgac_cv__get_cpuid" = x"yes" || test x"$pgac_cv__cpuid" = x"yes"); then
2123      USE_SSE42_CRC32C_WITH_RUNTIME_CHECK=1
2124    else
2125      # Use ARM CRC Extension if available.
2126      if test x"$pgac_armv8_crc32c_intrinsics" = x"yes" && test x"$CFLAGS_ARMV8_CRC32C" = x""; then
2127        USE_ARMV8_CRC32C=1
2128      else
2129        # ARM CRC Extension, with runtime check?
2130        if test x"$pgac_armv8_crc32c_intrinsics" = x"yes"; then
2131          USE_ARMV8_CRC32C_WITH_RUNTIME_CHECK=1
2132        else
2133          # fall back to slicing-by-8 algorithm, which doesn't require any
2134          # special CPU support.
2135          USE_SLICING_BY_8_CRC32C=1
2136	fi
2137      fi
2138    fi
2139  fi
2140fi
2141
2142# Set PG_CRC32C_OBJS appropriately depending on the selected implementation.
2143AC_MSG_CHECKING([which CRC-32C implementation to use])
2144if test x"$USE_SSE42_CRC32C" = x"1"; then
2145  AC_DEFINE(USE_SSE42_CRC32C, 1, [Define to 1 use Intel SSE 4.2 CRC instructions.])
2146  PG_CRC32C_OBJS="pg_crc32c_sse42.o"
2147  AC_MSG_RESULT(SSE 4.2)
2148else
2149  if test x"$USE_SSE42_CRC32C_WITH_RUNTIME_CHECK" = x"1"; then
2150    AC_DEFINE(USE_SSE42_CRC32C_WITH_RUNTIME_CHECK, 1, [Define to 1 to use Intel SSE 4.2 CRC instructions with a runtime check.])
2151    PG_CRC32C_OBJS="pg_crc32c_sse42.o pg_crc32c_sb8.o pg_crc32c_sse42_choose.o"
2152    AC_MSG_RESULT(SSE 4.2 with runtime check)
2153  else
2154    if test x"$USE_ARMV8_CRC32C" = x"1"; then
2155      AC_DEFINE(USE_ARMV8_CRC32C, 1, [Define to 1 to use ARMv8 CRC Extension.])
2156      PG_CRC32C_OBJS="pg_crc32c_armv8.o"
2157      AC_MSG_RESULT(ARMv8 CRC instructions)
2158    else
2159      if test x"$USE_ARMV8_CRC32C_WITH_RUNTIME_CHECK" = x"1"; then
2160        AC_DEFINE(USE_ARMV8_CRC32C_WITH_RUNTIME_CHECK, 1, [Define to 1 to use ARMv8 CRC Extension with a runtime check.])
2161        PG_CRC32C_OBJS="pg_crc32c_armv8.o pg_crc32c_sb8.o pg_crc32c_armv8_choose.o"
2162        AC_MSG_RESULT(ARMv8 CRC instructions with runtime check)
2163      else
2164        AC_DEFINE(USE_SLICING_BY_8_CRC32C, 1, [Define to 1 to use software CRC-32C implementation (slicing-by-8).])
2165        PG_CRC32C_OBJS="pg_crc32c_sb8.o"
2166        AC_MSG_RESULT(slicing-by-8)
2167      fi
2168    fi
2169  fi
2170fi
2171AC_SUBST(PG_CRC32C_OBJS)
2172
2173
2174# Select semaphore implementation type.
2175if test "$PORTNAME" != "win32"; then
2176  if test x"$PREFERRED_SEMAPHORES" = x"NAMED_POSIX" ; then
2177    # Need sem_open for this
2178    AC_SEARCH_LIBS(sem_open, [rt pthread], [USE_NAMED_POSIX_SEMAPHORES=1])
2179  fi
2180  if test x"$PREFERRED_SEMAPHORES" = x"UNNAMED_POSIX" ; then
2181    # Need sem_init for this
2182    AC_SEARCH_LIBS(sem_init, [rt pthread], [USE_UNNAMED_POSIX_SEMAPHORES=1])
2183  fi
2184  AC_MSG_CHECKING([which semaphore API to use])
2185  if test x"$USE_NAMED_POSIX_SEMAPHORES" = x"1" ; then
2186    AC_DEFINE(USE_NAMED_POSIX_SEMAPHORES, 1, [Define to select named POSIX semaphores.])
2187    SEMA_IMPLEMENTATION="src/backend/port/posix_sema.c"
2188    sematype="named POSIX"
2189  else
2190    if test x"$USE_UNNAMED_POSIX_SEMAPHORES" = x"1" ; then
2191      AC_DEFINE(USE_UNNAMED_POSIX_SEMAPHORES, 1, [Define to select unnamed POSIX semaphores.])
2192      SEMA_IMPLEMENTATION="src/backend/port/posix_sema.c"
2193      sematype="unnamed POSIX"
2194    else
2195      AC_DEFINE(USE_SYSV_SEMAPHORES, 1, [Define to select SysV-style semaphores.])
2196      SEMA_IMPLEMENTATION="src/backend/port/sysv_sema.c"
2197      sematype="System V"
2198    fi
2199  fi
2200  AC_MSG_RESULT([$sematype])
2201else
2202  AC_DEFINE(USE_WIN32_SEMAPHORES, 1, [Define to select Win32-style semaphores.])
2203  SEMA_IMPLEMENTATION="src/backend/port/win32_sema.c"
2204fi
2205
2206
2207# Select shared-memory implementation type.
2208if test "$PORTNAME" != "win32"; then
2209  AC_DEFINE(USE_SYSV_SHARED_MEMORY, 1, [Define to select SysV-style shared memory.])
2210  SHMEM_IMPLEMENTATION="src/backend/port/sysv_shmem.c"
2211else
2212  AC_DEFINE(USE_WIN32_SHARED_MEMORY, 1, [Define to select Win32-style shared memory.])
2213  SHMEM_IMPLEMENTATION="src/backend/port/win32_shmem.c"
2214fi
2215
2216# Select random number source. If a TLS library is used then it will be the
2217# first choice, else the native platform sources (Windows API or /dev/urandom)
2218# will be used.
2219AC_MSG_CHECKING([which random number source to use])
2220if test x"$with_ssl" = x"openssl" ; then
2221  AC_MSG_RESULT([OpenSSL])
2222elif test x"$PORTNAME" = x"win32" ; then
2223  AC_MSG_RESULT([Windows native])
2224else
2225  AC_MSG_RESULT([/dev/urandom])
2226  AC_CHECK_FILE([/dev/urandom], [], [])
2227
2228  if test x"$ac_cv_file__dev_urandom" = x"no" ; then
2229    AC_MSG_ERROR([
2230no source of strong random numbers was found
2231PostgreSQL can use OpenSSL, native Windows API or /dev/urandom as a source of random numbers.])
2232  fi
2233fi
2234
2235# If not set in template file, set bytes to use libc memset()
2236if test x"$MEMSET_LOOP_LIMIT" = x"" ; then
2237  MEMSET_LOOP_LIMIT=1024
2238fi
2239AC_DEFINE_UNQUOTED(MEMSET_LOOP_LIMIT, ${MEMSET_LOOP_LIMIT}, [Define bytes to use libc memset().])
2240
2241
2242if test "$enable_nls" = yes ; then
2243  PGAC_CHECK_GETTEXT
2244fi
2245
2246# Check for Tcl configuration script tclConfig.sh
2247if test "$with_tcl" = yes; then
2248    PGAC_PATH_TCLCONFIGSH([$with_tclconfig])
2249    PGAC_EVAL_TCLCONFIGSH([$TCL_CONFIG_SH],
2250                          [TCL_INCLUDE_SPEC,TCL_LIBS,TCL_LIB_SPEC,TCL_SHARED_BUILD])
2251    AC_SUBST(TCL_SHLIB_LD_LIBS)dnl don't want to double-evaluate that one
2252    if test "$TCL_SHARED_BUILD" != 1; then
2253      AC_MSG_ERROR([cannot build PL/Tcl because Tcl is not a shared library
2254Use --without-tcl to disable building PL/Tcl.])
2255    fi
2256    # now that we have TCL_INCLUDE_SPEC, we can check for <tcl.h>
2257    ac_save_CPPFLAGS=$CPPFLAGS
2258    CPPFLAGS="$TCL_INCLUDE_SPEC $CPPFLAGS"
2259    AC_CHECK_HEADER(tcl.h, [], [AC_MSG_ERROR([header file <tcl.h> is required for Tcl])])
2260    CPPFLAGS=$ac_save_CPPFLAGS
2261fi
2262
2263# check for <perl.h>
2264if test "$with_perl" = yes; then
2265  ac_save_CPPFLAGS=$CPPFLAGS
2266  CPPFLAGS="$CPPFLAGS $perl_includespec"
2267  AC_CHECK_HEADER(perl.h, [], [AC_MSG_ERROR([header file <perl.h> is required for Perl])],
2268                  [#include <EXTERN.h>])
2269  # While we're at it, check that we can link to libperl.
2270  # On most platforms, if perl.h is there then libperl.so will be too, but at
2271  # this writing Debian packages them separately.  There is no known reason to
2272  # waste cycles on separate probes for the Tcl or Python libraries, though.
2273  # On some Red Hat platforms, the link attempt can fail if we don't use
2274  # CFLAGS_SL while building the test program.
2275  ac_save_CFLAGS=$CFLAGS
2276  CFLAGS="$CFLAGS $CFLAGS_SL"
2277  pgac_save_LIBS=$LIBS
2278  LIBS="$perl_embed_ldflags"
2279  AC_MSG_CHECKING([for libperl])
2280  AC_LINK_IFELSE([AC_LANG_PROGRAM([
2281#include <EXTERN.h>
2282#include <perl.h>
2283],  [perl_alloc();])],
2284    [AC_MSG_RESULT(yes)],
2285    [AC_MSG_RESULT(no)
2286     AC_MSG_ERROR([libperl library is required for Perl])])
2287  LIBS=$pgac_save_LIBS
2288  CFLAGS=$ac_save_CFLAGS
2289  CPPFLAGS=$ac_save_CPPFLAGS
2290fi
2291
2292# check for <Python.h>
2293if test "$with_python" = yes; then
2294  ac_save_CPPFLAGS=$CPPFLAGS
2295  CPPFLAGS="$python_includespec $CPPFLAGS"
2296  AC_CHECK_HEADER(Python.h, [], [AC_MSG_ERROR([header file <Python.h> is required for Python])])
2297  CPPFLAGS=$ac_save_CPPFLAGS
2298fi
2299
2300#
2301# Check for documentation-building tools
2302#
2303PGAC_PATH_PROGS(XMLLINT, xmllint)
2304PGAC_PATH_PROGS(XSLTPROC, xsltproc)
2305PGAC_PATH_PROGS(FOP, fop)
2306PGAC_PATH_PROGS(DBTOEPUB, dbtoepub)
2307
2308#
2309# Check for test tools
2310#
2311if test "$enable_tap_tests" = yes; then
2312  # Check for necessary modules, unless user has specified the "prove" to use;
2313  # in that case it's her responsibility to have a working configuration.
2314  # (prove might be part of a different Perl installation than perl, eg on
2315  # MSys, so the result of AX_PROG_PERL_MODULES could be irrelevant anyway.)
2316  if test -z "$PROVE"; then
2317    # Test::More and Time::HiRes are supposed to be part of core Perl,
2318    # but some distros omit them in a minimal installation.
2319    AX_PROG_PERL_MODULES([IPC::Run Test::More=0.87 Time::HiRes], ,
2320      [AC_MSG_ERROR([Additional Perl modules are required to run TAP tests])])
2321  fi
2322  # Now make sure we know where prove is
2323  PGAC_PATH_PROGS(PROVE, prove)
2324  if test -z "$PROVE"; then
2325    AC_MSG_ERROR([prove not found])
2326  fi
2327fi
2328
2329# If compiler will take -Wl,--as-needed (or various platform-specific
2330# spellings thereof) then add that to LDFLAGS.  This is much easier than
2331# trying to filter LIBS to the minimum for each executable.
2332# On (at least) some Red-Hat-derived systems, this switch breaks linking to
2333# libreadline; therefore we postpone testing it until we know what library
2334# dependencies readline has.  The test code will try to link with $LIBS.
2335if test "$with_readline" = yes; then
2336  link_test_func=readline
2337else
2338  link_test_func=exit
2339fi
2340
2341if test "$PORTNAME" = "darwin"; then
2342  PGAC_PROG_CC_LDFLAGS_OPT([-Wl,-dead_strip_dylibs], $link_test_func)
2343elif test "$PORTNAME" = "openbsd"; then
2344  PGAC_PROG_CC_LDFLAGS_OPT([-Wl,-Bdynamic], $link_test_func)
2345else
2346  PGAC_PROG_CC_LDFLAGS_OPT([-Wl,--as-needed], $link_test_func)
2347fi
2348
2349# Create compiler version string
2350if test x"$GCC" = x"yes" ; then
2351  cc_string=`${CC} --version | sed q`
2352  case $cc_string in [[A-Za-z]]*) ;; *) cc_string="GCC $cc_string";; esac
2353elif test x"$SUN_STUDIO_CC" = x"yes" ; then
2354  cc_string=`${CC} -V 2>&1 | sed q`
2355else
2356  cc_string=$CC
2357fi
2358
2359AC_DEFINE_UNQUOTED(PG_VERSION_STR,
2360                   ["PostgreSQL $PG_VERSION on $host, compiled by $cc_string, `expr $ac_cv_sizeof_void_p \* 8`-bit"],
2361                   [A string containing the version number, platform, and C compiler])
2362
2363# Supply a numeric version string for use by 3rd party add-ons
2364# awk -F is a regex on some platforms, and not on others, so make "." a tab
2365[PG_VERSION_NUM="`echo $PG_MAJORVERSION $PG_MINORVERSION |
2366$AWK '{printf "%d%04d", $1, $2}'`"]
2367AC_DEFINE_UNQUOTED(PG_VERSION_NUM, $PG_VERSION_NUM, [PostgreSQL version as a number])
2368AC_SUBST(PG_VERSION_NUM)
2369
2370# If we are inserting PG_SYSROOT into CPPFLAGS, do so symbolically not
2371# literally, so that it's possible to override it at build time using
2372# a command like "make ... PG_SYSROOT=path".  This has to be done after
2373# we've finished all configure checks that depend on CPPFLAGS.
2374# The same for LDFLAGS, too.
2375if test x"$PG_SYSROOT" != x; then
2376  CPPFLAGS=`echo "$CPPFLAGS" | sed -e "s| $PG_SYSROOT | \\\$(PG_SYSROOT) |"`
2377  LDFLAGS=`echo "$LDFLAGS" | sed -e "s| $PG_SYSROOT | \\\$(PG_SYSROOT) |"`
2378fi
2379AC_SUBST(PG_SYSROOT)
2380
2381
2382# Begin output steps
2383
2384AC_MSG_NOTICE([using compiler=$cc_string])
2385AC_MSG_NOTICE([using CFLAGS=$CFLAGS])
2386AC_MSG_NOTICE([using CPPFLAGS=$CPPFLAGS])
2387AC_MSG_NOTICE([using LDFLAGS=$LDFLAGS])
2388# Currently only used when LLVM is used
2389if test "$with_llvm" = yes ; then
2390   AC_MSG_NOTICE([using CXX=$CXX])
2391   AC_MSG_NOTICE([using CXXFLAGS=$CXXFLAGS])
2392   AC_MSG_NOTICE([using CLANG=$CLANG])
2393   AC_MSG_NOTICE([using BITCODE_CFLAGS=$BITCODE_CFLAGS])
2394   AC_MSG_NOTICE([using BITCODE_CXXFLAGS=$BITCODE_CXXFLAGS])
2395fi
2396
2397# prepare build tree if outside source tree
2398# Note 1: test -ef might not exist, but it's more reliable than `pwd`.
2399# Note 2: /bin/pwd might be better than shell's built-in at getting
2400#         a symlink-free name.
2401if ( test "$srcdir" -ef . ) >/dev/null 2>&1 || test "`cd $srcdir && /bin/pwd`" = "`/bin/pwd`"; then
2402  vpath_build=no
2403else
2404  vpath_build=yes
2405  if test "$no_create" != yes; then
2406    _AS_ECHO_N([preparing build tree... ])
2407    pgac_abs_top_srcdir=`cd "$srcdir" && pwd`
2408    $SHELL "$ac_aux_dir/prep_buildtree" "$pgac_abs_top_srcdir" "." \
2409      || AC_MSG_ERROR(failed)
2410    AC_MSG_RESULT(done)
2411  fi
2412fi
2413AC_SUBST(vpath_build)
2414
2415
2416AC_CONFIG_FILES([GNUmakefile src/Makefile.global])
2417
2418AC_CONFIG_LINKS([
2419  src/backend/port/pg_sema.c:${SEMA_IMPLEMENTATION}
2420  src/backend/port/pg_shmem.c:${SHMEM_IMPLEMENTATION}
2421  src/include/pg_config_os.h:src/include/port/${template}.h
2422  src/Makefile.port:src/makefiles/Makefile.${template}
2423])
2424
2425if test "$PORTNAME" = "win32"; then
2426AC_CONFIG_COMMANDS([check_win32_symlinks],[
2427# Links sometimes fail undetected on Mingw -
2428# so here we detect it and warn the user
2429for FILE in $CONFIG_LINKS
2430 do
2431	# test -e works for symlinks in the MinGW console
2432	test -e `expr "$FILE" : '\([[^:]]*\)'` || AC_MSG_WARN([*** link for $FILE -- please fix by hand])
2433 done
2434])
2435fi
2436
2437AC_CONFIG_HEADERS([src/include/pg_config.h],
2438[
2439# Update timestamp for pg_config.h (see Makefile.global)
2440echo >src/include/stamp-h
2441])
2442
2443AC_CONFIG_HEADERS([src/include/pg_config_ext.h],
2444[
2445# Update timestamp for pg_config_ext.h (see Makefile.global)
2446echo >src/include/stamp-ext-h
2447])
2448
2449AC_CONFIG_HEADERS([src/interfaces/ecpg/include/ecpg_config.h],
2450                  [echo >src/interfaces/ecpg/include/stamp-h])
2451
2452AC_OUTPUT
2453