#                                               -*- Autoconf -*-
#                                               vim: ft=config :

dnl This Source Code Form is subject to the terms of the Mozilla Public
dnl License, v. 2.0.  If a copy of the MPL was not distributed with this
dnl file, You can obtain one at http://mozilla.org/MPL/2.0/.
dnl
dnl Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V.

dnl Process this file with autoconf to produce a configure script.

dnl  Some help with the syntax:
dnl  AS_IF(test, body [, test, body]* [, body])
dnl     All tests are executed in order until one returns true, then
dnl     the corresponding body is executed, if no tests succeed, the
dnl     last body (if present) is executed.
dnl     This translates to a shell if statement with a bunch of elif
dnl     clauses and an else clause.

dnl  AS_CASE(word [, pattern, body]* [,body])
dnl     The word is tested against each successive pattern, the body
dnl     of the first matching pattern is executed, if no patterns
dnl     match, the final body (if present) is executed.
dnl     This translates to a shell case command with patterns and
dnl     bodies, the final body gets the pattern *.

dnl  AS_VERSION_COMPARE(version1, version2, lt-body, eq-body, gt-body)
dnl     The two versions are compared, the body corresponding with
dnl     whether the first version is less than, equal to, or greater
dnl     than the second version is executed.

dnl  AS_VAR_IF(var, word, eq-body, ne-body)
dnl     If $var is equal to word, execute eq-body, else execute ne-body.

dnl  @%:@
dnl     This is a quadrigraph for #.  We use this in C preprocessor
dnl     code that we inline in the script so that it doesn't get
dnl     confused with a start-of-comment.

dnl  Note that by using this syntax, there should not be any
dnl  unbalanced parentheses (due to there not being loose case
dnl  statements).  This makes editing easier.

dnl  Also note that comments that start with # are copied to the
dnl  configure file, whereas comments starting with dnl are elided
dnl  (including their trailing newline).

AC_PREREQ([2.63])
AC_INIT([MonetDB], [11.25.0], [https://bugs.monetdb.org/], [MonetDB], [https://www.monetdb.org/])
AC_CONFIG_AUX_DIR([buildtools/conf])
AC_CONFIG_MACRO_DIR([buildtools/conf])
AC_CANONICAL_HOST
AC_CANONICAL_TARGET
# use tar-ustar since we have long (longer than 99 characters) file names
AM_INIT_AUTOMAKE([tar-ustar no-dist-gzip dist-bzip2])
AC_CONFIG_SRCDIR([gdk/gdk.h])
AC_CONFIG_HEADERS([monetdb_config.h])
AC_SUBST([CONFIG_H], [monetdb_config.h])

HOST=[$target]
AC_DEFINE_UNQUOTED([HOST], ["$HOST"], [Host identifier])

AC_DEFINE([MONETDB_RELEASE], ["unreleased"], [Release name or "unreleased"])

AS_VAR_IF([target_os], [cygwin],
	[AC_DEFINE([WIN32], 1, [Define on Microsoft Windows (also under Cygwin)])])
AM_CONDITIONAL([NOT_WIN32], [test x"$target_os" != xcygwin])
AM_CONDITIONAL([NATIVE_WIN32], [false])

m4_ifndef([PKG_PROG_PKG_CONFIG],
  [m4_fatal([Could not locate the pkg-config autoconf
    macros. These are usually located in /usr/share/aclocal/pkg.m4.
    If your macros are in a different location, try setting the
    environment variable M4DIRS="-I/other/macro/dir" before running
    ./bootstrap or autoreconf again.])])

PKG_PROG_PKG_CONFIG

AS_VAR_IF([PKG_CONFIG], [],
	[AC_MSG_ERROR([pkg-config is required to configure MonetDB])])

# BUILD and SOURCE are *only* used for testing.

# The Q versions of various variables are used in places where \'s
# need to be escapes with an extra \.  Configure does not support \'s
# in path names, so there is no need to do anything special here
# except set the variables.  The command to set them in case we do
# need to escape the \'s is Qvar=`echo "$var" | sed 's/\\\\/\\\\\\\\/g'`
BUILD=[`pwd`]
AC_SUBST([BUILD])

SOURCE=[`(cd $srcdir && pwd)`]
AC_SUBST([SOURCE])

. $srcdir/libversions
AC_SUBST([GDK_VERSION])
AC_SUBST([MAPI_VERSION])
AC_SUBST([MONETDB5_VERSION])
AC_SUBST([STREAM_VERSION])

# if no --prefix option, we must set Qprefix to the default value
# the problem is that prefix is set very late in the process if it
# wasn't set by --prefix
AS_VAR_IF([prefix], [NONE],
	[Qprefix="${ac_default_prefix}"].
	[Qprefix="${prefix}"])

Qprefix=`echo "$Qprefix" | sed 's/\\\\/\\\\\\\\/g'`
AC_SUBST([Qprefix])
# exec_prefix is created very late, so let make do the expansion
AS_VAR_IF([exec_prefix], [NONE],
	[Qexec_prefix='${prefix}'],
	[Qexec_prefix=`echo "$exec_prefix" | sed 's/\\\\/\\\\\\\\/g'`])
AC_SUBST([Qexec_prefix])

# small hack to get icc -no-gcc, done here because AC_PROG_CC shouldn't
# set GCC=yes if we use icc.
AS_CASE([$CC],
	[*icc*-no-gcc*], [],
	[*icc*], [
		#  Since version 8.0, ecc/ecpc are also called icc/icpc,
		#  and icc/icpc requires "-no-gcc" to avoid predefining
		#  __GNUC__, __GNUC_MINOR__, and __GNUC_PATCHLEVEL__ macros.
		icc_ver=`$CC -dumpversion 2>/dev/null`
		AS_CASE([$icc_ver],
			[8.*], [CC="$CC -no-gcc"],
			[9.*], [CC="$CC -no-gcc"],
			[10.*], [CC="$CC -no-gcc"],
			[11.*], [CC="$CC -no-gcc"],
			[15.*], [CC="$CC -no-gcc"],
			[AC_MSG_WARN([icc ($CC) $icc_ver not handled, yet])])])

AS_IF([test -f "$srcdir"/vertoo.data],
	[
	# Developers compiling from Mercurial:
	# default is --enable-strict --enable-assert --enable-debug --disable-optimize --enable-developer
	dft_strict=yes
	dft_assert=yes
	dft_debug=yes
	dft_optimize=no
	dft_developer=yes],
	[
	# Users compiling from source tarball(s):
	# default is --disable-strict --disable-assert --disable-debug --disable-optimize --disable-developer
	dft_strict=no
	dft_assert=no
	dft_debug=no
	dft_optimize=no
	dft_developer=no])

AC_ARG_ENABLE([developer],
	[AS_HELP_STRING([--enable-developer],
		[enable support for MonetDB development (default=yes for development sources)])],
	[enable_developer=$enableval],
	[enable_developer=$dft_developer])

dft_testing=auto
AC_ARG_ENABLE([testing],
	[AS_HELP_STRING([--enable-testing],
		[enable support for testing (default=auto)])],
	[enable_testing=$enableval],
	[enable_testing=$dft_testing])

dft_mapi=yes
AC_ARG_ENABLE([mapi],
	[AS_HELP_STRING([--enable-mapi],
		[Enable MAPI (default=yes)])],
	[enable_mapi=$enableval],
	[enable_mapi=$dft_mapi])
AM_CONDITIONAL([HAVE_MAPI], [test x$enable_mapi != xno])
AS_VAR_IF([enable_mapi], [no],
	[],
	[AC_DEFINE([HAVE_MAPI], 1, [Enable MAPI])])

dft_odbc=auto
AC_ARG_ENABLE([odbc],
	[AS_HELP_STRING([--enable-odbc],
		[compile the MonetDB ODBC driver (default=auto)])],
	[enable_odbc=$enableval],
	[enable_odbc=$dft_odbc])

dft_gdk=yes
AC_ARG_ENABLE([gdk],
	[AS_HELP_STRING([--enable-gdk],
		[enable support for GDK (default=yes)])],
	[enable_gdk=$enableval],
	[enable_gdk=$dft_gdk])

dft_monetdb5=yes
AC_ARG_ENABLE([monetdb5],
	[AS_HELP_STRING([--enable-monetdb5],
		[enable support for MonetDB5 (default=yes)])],
	[enable_monetdb5=$enableval],
	[enable_monetdb5=$dft_monetdb5])
AS_CASE([$enable_gdk-$enable_monetdb5],
	[no-yes], [AC_MSG_ERROR([MonetDB5 requires GDK.])],
	[no-auto], [
		enable_monetdb5=no
		disable_monetdb5="(MonetDB5 requires GDK.)"])

dft_sql=yes
AC_ARG_ENABLE([sql],
	[AS_HELP_STRING([--enable-sql],
		[enable support for MonetDB/SQL (default=yes)])],
	[enable_sql=$enableval],
	[enable_sql=$dft_sql])
AS_CASE([$enable_monetdb5-$enable_sql],
[no-yes], [
	AC_MSG_ERROR([MonetDB/SQL requires MonetDB5.])],
[no-auto], [
	enable_sql=no
	disable_sql="(MonetDB/SQL requires MonetDB5.)"])

dft_geom=auto
AC_ARG_ENABLE([geom],
	[AS_HELP_STRING([--enable-geom],
		[enable support for geom module (default=auto)])],
	[enable_geom=$enableval],
	[enable_geom=$dft_geom])
AS_CASE([$enable_monetdb5-$enable_geom],
[no-yes], [
	AC_MSG_ERROR([geom module requires MonetDB5])],
[no-auto], [
	enable_geom=no
	disable_geom="(geom module requires MonetDB5)"])

dft_fits=auto
AC_ARG_ENABLE([fits],
	[AS_HELP_STRING([--enable-fits],
		[enable support for FITS (default=auto)])],
	[enable_fits=$enableval],
	[enable_fits=$dft_fits])

dft_netcdf=auto
AC_ARG_ENABLE([netcdf],
	[AS_HELP_STRING([--enable-netcdf],
		[enable support for netcdf (default=auto)])],
	[enable_netcdf=$enableval],
	[enable_netcdf=$dft_netcdf])

dft_lidar=auto
AC_ARG_ENABLE([lidar],
	[AS_HELP_STRING([--enable-lidar],
		[enable support for LiDAR data (default=auto)])],
	[have_lidar=$enableval],
	[have_lidar=$dft_lidar])

dft_shp=auto
AC_ARG_ENABLE([shp],
	[AS_HELP_STRING([--enable-shp],
		[enable support for ESRI Shapefiles (default=auto)])],
	[have_shp=$enableval],
	[have_shp=$dft_shp])
AS_CASE([$enable_geom-$have_shp],
[no-yes], [
	AC_MSG_ERROR([ESRI Shapefile vault requires the geom module])],
[no-auto], [
	have_shp=no
	disable_shp="(ESRI Shapefile vault requires the geom module)"])

dft_gsl=auto
AC_ARG_ENABLE([gsl],
	[AS_HELP_STRING([--enable-gsl],
		[enable support for GSL (default=auto)])],
	[enable_gsl=$enableval],
	[enable_gsl=$dft_gsl])

dft_embedded=no
AC_ARG_ENABLE([embedded],
	[AS_HELP_STRING([--enable-embedded],
		[enable support for running MonetDB as a library (default=no)])],
	[enable_embedded=$enableval],
	[enable_embedded=$dft_embedded])
AM_CONDITIONAL([HAVE_EMBEDDED], [test x$enable_embedded != xno])
AS_VAR_IF([enable_embedded], [no], [],
	[AC_DEFINE([HAVE_EMBEDDED], 1, [Support for MonetDB as a library])])

dft_embedded_r=no
AC_ARG_ENABLE([embedded-r],
	[AS_HELP_STRING([--enable-embedded-r],
		[enable support for running MonetDB as a library for R (default=no)])],
	[enable_embedded_r=$enableval],
	[enable_embedded_r=$dft_embedded_r])
AM_CONDITIONAL([HAVE_EMBEDDED_R], [test x$enable_embedded_r != xno])
AS_VAR_IF([enable_embedded_r], [no], [],
	[AC_DEFINE([HAVE_EMBEDDED_R], 1, [Support for MonetDB as a library for R])])


dft_embedded_py=no
AC_ARG_ENABLE(embedded-py,
	AS_HELP_STRING([--enable-embedded-py],
		[enable support for running MonetDB as a library for Python (default=no)]),
	enable_embedded_py=$enableval,
	enable_embedded_py=$dft_embedded_py)
AM_CONDITIONAL(HAVE_EMBEDDED_PY, test x$enable_embedded_py != xno)
if test "x$enable_embedded_py" != xno; then
	AC_DEFINE(HAVE_EMBEDDED_PY, 1, [Support for MonetDB as a library for Python])
fi

dft_rintegration=auto
AC_ARG_ENABLE([rintegration],
	[AS_HELP_STRING([--enable-rintegration],
		[enable support for R integration into MonetDB (default=auto)])],
	[enable_rintegration=$enableval],
	[enable_rintegration=$dft_rintegration])

dft_pyintegration=auto
AC_ARG_ENABLE([pyintegration],
	[AS_HELP_STRING([--enable-pyintegration],
		[enable support for Python integration into MonetDB (default=auto)])],
	[enable_pyintegration=$enableval],
	[enable_pyintegration=$dft_pyintegration])

# The console is a direct client hooked onto the kernel with full
# administrative privileges, bypassing any security checks.  It is
# handy only during development.
# We simply can't disable it for now because Testing gets hopelessly
# upset about it, but we can offer to the user to disable it for
# increased security.
dft_console=yes
AC_ARG_ENABLE([console],
	[AS_HELP_STRING([--enable-console],
		[enables direct console on the server (involves security risks) (default=yes)])],
	[enable_console="$enableval"],
	[enable_console=$dft_console])
AS_VAR_IF([enable_console], [no], [],
	[AC_DEFINE([HAVE_CONSOLE], 1, [If the console should be used])])

dft_int128=auto
AC_ARG_ENABLE([int128],
	[AS_HELP_STRING([--enable-int128],
		[enable support for 128-bit integers (default=auto)])],
	[enable_int128=$enableval],
	[enable_int128=$dft_int128])

AC_ARG_ENABLE([debug],
	[AS_HELP_STRING([--enable-debug],
		[enable full debugging (default=yes for development sources)])],
	[enable_debug=$enableval],
	[enable_debug=def_$dft_debug])

AC_ARG_ENABLE([assert],
	[AS_HELP_STRING([--enable-assert],
		[enable assertions in the code (default=yes for development sources)])],
	[enable_assert=$enableval],
	[enable_assert=def_$dft_assert])

AC_ARG_ENABLE([optimize],
	[AS_HELP_STRING([--enable-optimize],
		[enable extra optimization (default=no)])],
	[enable_optimize=$enableval],
	[enable_optimize=def_$dft_optimize])

AC_ARG_ENABLE([strict],
	[AS_HELP_STRING([--enable-strict],
		[enable strict compiler flags (default=yes for development sources)])],
	[enable_strict=$enableval],
	[enable_strict=$dft_strict])

dft_sanitizer=no
AC_ARG_ENABLE([sanitizer],
	[AS_HELP_STRING([--enable-sanitizer],
		[enable support for the GCC address sanitizer (default=no)])],
	[enable_sanitizer=$enableval],
	[enable_sanitizer=$dft_sanitizer])

AC_ARG_ENABLE([static-analysis],
	[AS_HELP_STRING([--enable-static-analysis], [configure for static code analysis (use only if you know what you are doing)])],
	AC_DEFINE([STATIC_CODE_ANALYSIS], 1, [compiling for static code analysis]))

need_profile=no
dft_profile=$need_profile
AC_ARG_ENABLE([profile],
	[AS_HELP_STRING([--enable-profile], [enable profiling (default=no)])],
	[enable_profile=$enableval],
	[enable_profile=def_$dft_profile])

need_instrument=no
dft_instrument=$need_instrument
AC_ARG_ENABLE([instrument],
	[AS_HELP_STRING([--enable-instrument],
		[enable instrument (default=no)])],
	[enable_instrument=$enableval],
	[enable_instrument=def_$dft_instrument])

# RIPEMD160 is patent free, academic and European, but unfortunately
# can't use it by default, as that would exclude JDBC usage (Java
# doesn't natively support RIPEMD160).
MONETDB5_PASSWDHASH="SHA512"
AC_ARG_WITH([password-backend],
	[AS_HELP_STRING([--with-password-backend=HASHALG],
		[password hash algorithm, one of MD5, SHA1, RIPEMD160, SHA224, SHA256, SHA384, SHA512, defaults to SHA512])],
	[password_backend="$withval"])
AS_CASE([$password_backend],
	[yes|no|auto|""],
		[],
	[MD5|SHA1|RIPEMD160|SHA224|SHA256|SHA384|SHA512],
		[MONETDB5_PASSWDHASH="$password_backend"],
	[AC_MSG_ERROR(['$password_backend' invalid, choose one of MD5, SHA1, RIPEMD160, SHA224, SHA256, SHA384, SHA512])])
AC_DEFINE_UNQUOTED([MONETDB5_PASSWDHASH], ["$MONETDB5_PASSWDHASH"],
	[The used password hash algorithm])
AC_DEFINE_UNQUOTED([MONETDB5_PASSWDHASH_TOKEN], [$MONETDB5_PASSWDHASH],
	[The used password hash algorithm])
AC_SUBST([MONETDB5_PASSWDHASH])

AC_DISABLE_STATIC
AC_ENABLE_SHARED
AM_CONDITIONAL([CROSS_COMPILING], [test x$cross_compiling = xyes])

# logs usually end up in /var/log
logdir='${localstatedir}/log/monetdb'
AC_ARG_WITH([logdir],
	[AS_HELP_STRING([--with-logdir=DIR],
		[Where to put log files (LOCALSTATEDIR/log/monetdb/)])],
	[AS_CASE([$withval],
		[yes|no|auto],
		[AC_MSG_WARN([--with-logdir called without argument - will use default])],
		[logdir="$withval"])])

# pidfiles usually end up in /var/run
rundir='${localstatedir}/run/monetdb'
AC_ARG_WITH([rundir],
	[AS_HELP_STRING([--with-rundir=DIR],
		[Where to put pid files (LOCALSTATEDIR/run/monetdb/)])],
	[AS_CASE([$withval],
		[yes|no|auto],
		[AC_MSG_WARN([--with-rundir called without argument - will use default])],
		[rundir="$withval"])])


#    checks for programs
AC_LANG([C])

# autoconf-2.60: enable extensions on systems that normally disable them
AC_USE_SYSTEM_EXTENSIONS()
# enable 64-bits file offsets, check this early, since it breaks other
# stuff (in particular on Solaris), which we do not want to detect as
# usable below in that case
AC_SYS_LARGEFILE

AC_PROG_CPP()
# check for compiler (also set GCC (yes/no)).
AC_PROG_CC()

# What do we need to get the msg_control and msg_controllen fields on
# struct msg (filedescriptor passing)?  In the future this could be a
# check to see if we can find it, such that we can disable it compile
# time if absent.  It looks however, as if it's available everywhere
# we try where UNIX domain sockets also exist.  So we go by the
# following simple implementation for now.
AS_CASE([$host],
	[*-solaris*], [
		# Solaris needs this to get msg_control and msg_controllen;
		# it disables, however, the availability of madvise, which is
		# in use use by GDK, so we cannot just AC_DEFINE this, but
		# rather have to enable it where it is necessary
		MSGCONTROL_FLAGS="-D_XOPEN_SOURCE=500"
		AC_SUBST([MSGCONTROL_FLAGS])])

# (try to) determine compiler type (gcc, icc, clang) (and version)
# independent of call ($CC) and result of AC_PROG_CC() ($GCC)
# (e.g., on OSX, Apple pretends its clang is gcc)
AC_MSG_CHECKING([compiler type & version])
gcc_ver=""
icc_ver=""
CC_ver=""
CC_version=`$CC --version 2>&1`
AS_CASE([$CC_version],
	[*'(GCC)'*|*'Copyright (C) '*' Free Software Foundation, Inc.'*], [
		gcc_ver=`$CC -dumpversion 2>/dev/null`
		CC_ver="gcc-$gcc_ver"],
	[*'(ICC)'*|*'Copyright (C) '*' Intel Corporation.  All rights reserved.'*], [
		icc_ver=`$CC -dumpversion 2>/dev/null`
		CC_ver="icc-$icc_ver"],
	[*clang*], [
	# `clang -dumpversion` reports the original/old gcc version it is based on
		gcc_ver=`$CC -v 2>&1 | grep -w '[[Vv]]ersion [[0-9]]\+\.[[0-9]]\+' | sed 's|^.*[[Vv]]ersion \([[0-9]][[0-9]]*\(\.[[0-9]][[0-9]]*\)\(\.[[0-9]][[0-9]]*\)*\).*$|\1|'`
		CC_ver="clang-$gcc_ver"],
	[CC_V=`$CC -V 2>&1`
	AS_CASE([$CC_V],
		[*'Sun Ceres C '*|*'Sun C '*], [
			# developer/sunstudioexpress and
			# developer/sunstudio12u1 respectively
			CC_ver="suncc-`echo "$CC_version" | sed -n 's/.*Sun Ceres C \(.*\) SunOS.*/\1/p;s/.*Sun C \(.*\) SunOS.*/\1/p'`"
			CC_version="$CC_V"],
		[AC_MSG_WARN([compiler not recognized:
`$CC --version` says
$CC_version
`$CC -V` says
$CC_V])])])
AC_MSG_RESULT([$CC_ver])

# sanity/consistency check
AS_CASE([$GCC!$CC!$CC_ver],
	[yes!*clang*!clang-*], [],
	[yes!*gcc*!clang-*], [],
	[yes!*gcc*!gcc-*], [],
	[!*icc*!icc-*], [],
	[*!*!suncc-*], [],
	[AC_MSG_WARN([compiler potentially not (correctly) recognized:
GCC=$GCC
CC=$CC
CC_ver=$CC_ver
CC_version=
$CC_version])])

AC_CHECK_SIZEOF([long])
bits=`expr $ac_cv_sizeof_long \* 8`
AC_SUBST([bits])
AM_CONDITIONAL([BITS32], [test x"$bits" = x32])
AM_CONDITIONAL([BITS64], [test x"$bits" = x64])

AC_C_BIGENDIAN()

LINUX_DIST=''
AS_CASE([$host_os],
	[linux*], [
		# Please keep this aligned / in sync with testing/Mtest.py.in !
		AC_MSG_CHECKING([which Linux distribution we're using])
		AS_IF([test -s /etc/os-release], [
				# this is the new way of getting release information
				# works for Fedora >= 17, Ubuntu >= 12.04 (Precise Pangolin)
				LINUX_DIST=`source /etc/os-release; echo ${ID}:${VERSION_ID}`],
			[test -s /etc/fedora-release ], [
				LINUX_DIST=`cat /etc/fedora-release | head -n1 \
					| sed 's|^.*\(Fedora\).* release \([[0-9]][[^ \n]]*\)\( .*\)*$|\1:\2|'`],
			[test -s /etc/centos-release ], [
				LINUX_DIST=`cat /etc/centos-release | head -n1 \
					| sed 's|^\(CentOS\).* release \([[0-9]][[^ \n]]*\)\( .*\)*$|\1:\2|'`],
			[test -s /etc/yellowdog-release ], [
				LINUX_DIST=`cat /etc/yellowdog-release | head -n1 \
					| sed 's|^\(Yellow\) Dog Linux release \([[0-9]][[^ \n]]*\)\( .*\)*$|\1:\2|'`],
			[test -s /etc/redhat-release ], [
				LINUX_DIST=`cat /etc/redhat-release | head -n1 \
					| sed 's|^.*\(Red\) \(Hat\).* Linux *\([[A-Z]]*\) release \([[0-9]][[^ \n]]*\)\( .*\)*$|\1\2:\4\3|' \
					| sed 's|^Red Hat Enterprise Linux \([[AW]]S\) release \([[0-9]][[^ \n]]*\)\( .*\)*$|RHEL:\2\1|' \
					| sed 's|^\(CentOS\).* release \([[0-9]][[^ \n]]*\)\( .*\)*$|\1:\2|' \
					| sed 's|^\(Scientific\) Linux.* release \([[0-9]][[^ \n]]*\)\( .*\)*$|\1:\2|'`],
			[test -s /etc/SuSE-release ], [
				LINUX_DIST=`cat /etc/SuSE-release   | head -n1 \
					| sed 's|^.*\(S[[Uu]]SE\) LINUX Enterprise \([[SD]]\)[[ervsktop]]* \([[0-9]][[^ \n]]*\)\( .*\)*$|\1:\3E\2|' \
					| sed 's|^S[[Uu]]SE LINUX Enterprise \([[SD]]\)[[ervsktop]]* \([[0-9]][[^ \n]]*\)\( .*\)*$|SLE\1:\2|' \
					| sed 's|^.*\(S[[Uu]]SE\) [[Ll]][[Ii]][[Nn]][[Uu]][[Xx]].* \([[0-9]][[^ \n]]*\)\( .*\)*$|\1:\2|' \
					| sed 's|^open\(S[[Uu]]SE\) \([[0-9]][[^ \n]]*\)\( .*\)*$|\1:\2|'`],
			[test -s /etc/gentoo-release ], [
				LINUX_DIST=`cat /etc/gentoo-release | head -n1 \
					| sed 's|^.*\(Gentoo\) Base System.* [[versionrelease]]* \([[0-9]][[^ \n]]*\)\( .*\)*$|\1:\2|'`],
			[test -s /etc/lsb-release ], [
				LINUX_DIST=`grep '^DISTRIB_ID=' /etc/lsb-release | cut -d'=' -f2`:`grep '^DISTRIB_RELEASE=' /etc/lsb-release | cut -d'=' -f2`],
			[test -s /etc/debian_version ], [
				LINUX_DIST=Debian:`cat /etc/debian_version | head -n1`],
			[LINUX_DIST=`uname -s`:`uname -r | sed 's|^\([[0-9\.]]*\)\([[^0-9\.]].*\)$|\1|'`])
		LINUX_DIST=`echo "$LINUX_DIST" | sed 's|:||'`
		AC_MSG_RESULT([$LINUX_DIST])])
AC_SUBST([LINUX_DIST])

AC_DEFUN([MCHECK_ADD_FLAG], [
	cache_var=AS_TR_SH([m_cv_cc_has_$1])
	AC_CACHE_CHECK([whether ${CC} supports $1], [${cache_var}], [
		SAVE_FLAGS="${CFLAGS}"
		# compile a small program that has enough challenges to trigger
		# the compiler to actually do something, such that it needs to
		# actually look at the flags given
		CFLAGS="${SAVE_FLAGS} ${X_CFLAGS} $1"
		dnl  do not use AC_LANG_PROGRAM here, because it generates a program
		dnl  with "int main() {" function, which triggers
		dnl  old-style-definition warnings
		AC_LINK_IFELSE([
			AC_LANG_SOURCE([[
				@%:@include <stdio.h>
				@%:@include <stdlib.h>
				@%:@ifdef HAVE_STRING_H
				@%:@include <string.h>
				@%:@endif

				@%:@if -_LARGEFILE64_SOURCE - -1 == 1
				@%:@include <stdio.h>
				@%:@endif
				int main(int argc, char **av) {
					char buf[24];
					double d = atof("4.2");
					if (argc == 0 || strchr(av[0], av[0][0]) != NULL)
						snprintf(buf, sizeof(buf), "%f", d);
					return 0;
				}
				]])],
			[eval ${cache_var}=yes],
			[eval ${cache_var}=no])
		CFLAGS="${SAVE_FLAGS}"
		])
	eval "test \"x\$${cache_var}\" = xyes && X_CFLAGS=\"${X_CFLAGS} $1\""
])

# Set compiler switches.
# The idea/goal is to be as strict as possible, i.e., enable preferably
# *all* warnings and make them errors. This should help keep the code
# as clean and portable as possible.
# It turned out, though, that this, especially turning all warnings into
# errors, is a bit too ambitious for configure/autoconf. Hence, we set
# all flags that enable specific warnings and those that make the
# compiler die on them (-Werror) in X_CFLAGS, which are added to the
# standard CFLAGS once configure/autoconf are done with their job,
# i.e., at the end of this configure[.ag] script.
# Only GNU (gcc) and Intel ([ie]cc/[ie]cpc on Linux) are done so far.
AS_VAR_IF([enable_strict], [yes], [
	AS_CASE([$GCC-$CC-$host_os],
	[yes-*-*], [
		# keep this one first
		MCHECK_ADD_FLAG([-Werror])

		MCHECK_ADD_FLAG([-Wall])
		MCHECK_ADD_FLAG([-Wextra])
		MCHECK_ADD_FLAG([-W])
		MCHECK_ADD_FLAG([-Werror-implicit-function-declaration])

		MCHECK_ADD_FLAG([-Wpointer-arith])
		MCHECK_ADD_FLAG([-Wdeclaration-after-statement])
		MCHECK_ADD_FLAG([-Wundef])
		MCHECK_ADD_FLAG([-Wformat=2])
		MCHECK_ADD_FLAG([-Wno-format-nonliteral])
		MCHECK_ADD_FLAG([-Winit-self])
		MCHECK_ADD_FLAG([-Winvalid-pch])
		MCHECK_ADD_FLAG([-Wmissing-declarations])
		MCHECK_ADD_FLAG([-Wmissing-format-attribute])
		MCHECK_ADD_FLAG([-Wmissing-prototypes])
		MCHECK_ADD_FLAG([-Wold-style-definition])
		MCHECK_ADD_FLAG([-Wpacked])
		MCHECK_ADD_FLAG([-Wunknown-pragmas])
		MCHECK_ADD_FLAG([-Wvariadic-macros])
		MCHECK_ADD_FLAG([-fstack-protector-all])
		MCHECK_ADD_FLAG([-Wstack-protector])
		MCHECK_ADD_FLAG([-Wpacked-bitfield-compat])
		MCHECK_ADD_FLAG([-Wsync-nand])
		MCHECK_ADD_FLAG([-Wjump-misses-init])
		MCHECK_ADD_FLAG([-Wmissing-include-dirs])
		MCHECK_ADD_FLAG([-Wlogical-op])

		# With clang 2.8, 3.3, 5.0 (no other tested) and gcc < 4.5
		# (tested 3.4.6, 4.2.1, 4.3.2, 4.4.4, 4.4.5, 4.5.1, 4.5.2),
		# "-Wunreachable-code" triggers numerous "will never be
		# executed" (at least) in our stream code, mostly (if
		# not all) false positives, though, as well as in
		# YACC/BISON-generated code; thus, we do not use
		# "-Wunreachable-code" with clang and gcc < 4.5 .
		# At the same time, Apple deprecated OpenSSL in favour of
		# their own CommonCrypto API.  We don't mind about that.
		AS_CASE([$CC_ver],
			[clang-*|gcc-[[0-3]].*|gcc-4.[[0-4]].*],
				[MCHECK_ADD_FLAG([-Wno-deprecated-declarations])],
			[MCHECK_ADD_FLAG([-Wunreachable-code])])

		dnl  Our code is not (yet?) up to these:
		dnl  MCHECK_ADD_FLAG([-pedantic-errors])
		dnl  MCHECK_ADD_FLAG([-Wno-long-long])
		dnl  MCHECK_ADD_FLAG([-Waggregate-return])
		dnl  MCHECK_ADD_FLAG([-Wbad-function-cast])
		dnl  MCHECK_ADD_FLAG([-Wcast-qual])
		dnl  MCHECK_ADD_FLAG([-Wconversion])
		dnl  MCHECK_ADD_FLAG([-Wdisabled-optimization])
		dnl  MCHECK_ADD_FLAG([-Wfloat-equal])
		dnl  MCHECK_ADD_FLAG([-Winline])
		dnl  MCHECK_ADD_FLAG([-Wmissing-noreturn])
		dnl  MCHECK_ADD_FLAG([-Wnested-externs])
		dnl  MCHECK_ADD_FLAG([-Wpadded])
		dnl  MCHECK_ADD_FLAG([-Wredundant-decls])
		dnl  MCHECK_ADD_FLAG([-Wshadow])
		dnl  MCHECK_ADD_FLAG([-Wstrict-prototypes])
		dnl  MCHECK_ADD_FLAG([-Wswitch-default])
		dnl  MCHECK_ADD_FLAG([-Wswitch-enum])
		dnl  MCHECK_ADD_FLAG([-Wtraditional])
		dnl  MCHECK_ADD_FLAG([-Wtraditional-conversion])
		dnl  MCHECK_ADD_FLAG([-Wwrite-strings])
		dnl  MCHECK_ADD_FLAG([-Wc++-compat])
		dnl  MCHECK_ADD_FLAG([-Woverlength-strings])
		dnl  MCHECK_ADD_FLAG([-Wsign-conversion])
		dnl  MCHECK_ADD_FLAG([-Wstrict-overflow=5])
		dnl  MCHECK_ADD_FLAG([-Wvla])
		dnl  MCHECK_ADD_FLAG([-Wunsuffixed-float-constants])
	],
	[-*icc*-linux*|-*ecc*-linux*] ,[
		# Intel ([ie]cc/[ie]cpc on Linux)
		AS_CASE([$icc_ver],
			[?.*],	[LDFLAGS="$LDFLAGS -i_dynamic"],
			[??.*],	[LDFLAGS="$LDFLAGS -shared-intel"])
		# Let warning #140 "too many arguments in function call"
		# become an error to make configure tests work properly.
		CFLAGS="$CFLAGS -we140"
		# Let warning #147 "declaration is incompatible"
		# become an error to make configure tests work properly.
		CFLAGS="$CFLAGS -we147"
		# Check for PIC does not work with Version 8.1, unless we disable
		# remark #1418: external definition with no prior declaration ... !?
		AS_CASE([$icc_ver],
			[8.1*], [CFLAGS="$CFLAGS -wd1418"],
			[9.*],  [CFLAGS="$CFLAGS -wd1418"],
			[10.*], [CFLAGS="$CFLAGS -wd1418"],
			[11.*], [CFLAGS="$CFLAGS -wd1418"])
		# Version 8.* doesn't find sigset_t when -ansi is set... !?
		AS_CASE([$icc_ver],
			[8.*],  [],
			[9.*],  [],
			[10.*], [],
			[11.*], [],
			[15.*], [],
			[CFLAGS="$CFLAGS -ansi"])
		# Be picky; "-Werror" seems to be too rigid for autoconf...
		CFLAGS="$CFLAGS -Wall -w2 -Wcheck"
		# Be rigid; MonetDB code is supposed to adhere to this... ;-)
		# Let the follows remarks and warning become errors:
		# #266: "function declared implicitly"
		# #181: "argument is incompatible with corresponding format string conversion"
		# #810: "conversion from "." to "." may lose significant bits"
		# #111: "statement is unreachable"
		# #271: "trailing comma is nonstandard"
		# #593: "variable "." was set but never used"
		X_CFLAGS="$X_CFLAGS -we266,181,810,271,593"
		X_CFLAGS="$X_CFLAGS -Werror -Werror-all"
		# ... however, some things aren't solved, yet:
		# (for the time being,) we need to disable some warnings (making them remarks doesn't seem to work with -Werror):
		X_CFLAGS="$X_CFLAGS -wd1418,1419,981,193,1357"
		# ICC 11 & 15 warn about non-pointer conversion from "<type>" to
		# "<smaller>" may lose significant bits, it does however, also for
		# constants like short s = 1234, which is too much
		AS_CASE([$icc_ver],
			[11.*], [X_CFLAGS="$X_CFLAGS,2259"],
			[15.*], [X_CFLAGS="$X_CFLAGS,2259"])
		AS_CASE([$icc_ver],
			[8.[[1-9]]*], [X_CFLAGS="$X_CFLAGS,1572"],
			[9.[[1-9]]*], [X_CFLAGS="$X_CFLAGS,1572,1599"],
			[10.*],       [X_CFLAGS="$X_CFLAGS,1572,1599"],
			[11.*],       [X_CFLAGS="$X_CFLAGS,1572,1599"])
		# #1418: external definition with no prior declaration
		# #1419: external declaration in primary source file
		# # 981: operands are evaluated in unspecified order
		# # 193: zero used for undefined preprocessing identifier
		# #1357: optimization disabled due to excessive resource requirements; contact Intel Premier Support for assistance
		# #1572: floating-point equality and inequality comparisons are unreliable
		# #1599: declaration hides variable
		# #2259: non-pointer conversion from "." to "." may lose significant bits
	])
])
AS_VAR_IF([enable_sanitizer], [no], [], [
	AS_CASE([$GCC],
		[yes], [
			# if no specific sanitizer was specified, use
			# address sanitizer
			AS_VAR_IF([enable_sanitizer], [yes],
				[MCHECK_ADD_FLAG([-fsanitize=address])],
				[MCHECK_ADD_FLAG([-fsanitize=$enable_sanitizer])
				AS_VAR_IF([enable_sanitizer], [thread],
					[X_CFLAGS="$X_CFLAGS -DNO_ATOMIC_INSTRUCTIONS"])])
			MCHECK_ADD_FLAG([-fno-omit-frame-pointer])],
		[AC_MSG_ERROR([--enable-sanitizer only supported with GCC])])])

AC_SUBST([CFLAGS])
AC_SUBST([X_CFLAGS])

# icc needs -fPIC (but the current autoconf still uses -KPIC)
AS_CASE([$GCC-$CC-$host_os],
	[-*icc*-linux*|-*ecc*-linux*], [
		AS_CASE([$icc_ver],
		[10.*], [CC="$CC -fPIC"],
		[11.*], [CC="$CC -fPIC"])])

# some dirty hacks
THREAD_SAVE_FLAGS="\$(thread_safe_flag_spec) -D_REENTRANT"
# only needed in monet
AS_CASE([$host_os],
	[solaris*], [
		AS_CASE([$GCC],
			[yes], [],
			[THREAD_SAVE_FLAGS="$THREAD_SAVE_FLAGS -mt"])],
	[aix*], [
		THREAD_SAVE_FLAGS="$THREAD_SAVE_FLAGS -D_THREAD_SAFE"
		AS_CASE([$GCC],
			[yes], [THREAD_SAVE_FLAGS="$THREAD_SAVE_FLAGS -mthreads"],
			[THREAD_SAVE_FLAGS="$THREAD_SAVE_FLAGS -qthreaded"])])
AC_SUBST([thread_safe_flag_spec])
AC_SUBST([THREAD_SAVE_FLAGS])

AC_PROG_INSTALL
AC_LIBTOOL_DLOPEN
AC_LIBTOOL_WIN32_DLL
AC_LIBTOOL_SETUP
AC_PROG_LIBTOOL
AM_PROG_LIBTOOL

READLINK='readlink -f'
AC_MSG_CHECKING([for $READLINK])
AS_IF([$READLINK /tmp >/dev/null 2>&1],
	[],
	[READLINK=echo])
AC_MSG_RESULT([$READLINK])
AC_SUBST([READLINK])

translatepath=echo
anttranslatepath=$READLINK

# --with-translatepath
AC_ARG_WITH([translatepath],
	[AS_HELP_STRING([--with-translatepath=PROG],
		[program to translate paths from configure-time format to execute-time format.  Take care that this program can be given paths like ${prefix}/etc which should be translated carefully.])],
	[translatepath="$withval"
	 anttranslatepath="$withval"],
	[AS_VAR_IF([cross_compiling], [yes],
		[AC_MSG_WARN([Cross compiling, but no --with-translatepath option given])])])
AC_SUBST([translatepath])

# --with-anttranslatepath
AC_ARG_WITH([anttranslatepath],
	[AS_HELP_STRING([--with-anttranslatepath=PROG],
		[program to translate paths from configure-time format to a format that can be given to the ant program (default: 'readlink -f' or value for --with-translatepath)])],
	[anttranslatepath="$withval"])
AC_SUBST([anttranslatepath])

# check whether no (explictly chosen) mutual exclusive combinations
# were made, compatability matrix:
#                deb  ass  opt  pro  ins
#  debug          \    C    X    C    C
#  assert         C    \    C    C    C
#  optimize       X    C    \    X    X
#  profile        C    C    X    \    C
#  instrument     C    C    X    C    \

# find conflicts
AS_CASE([x$enable_debug.$enable_optimize.$enable_profile.$enable_instrument],
	[xyes.yes.*.*],
		[AC_MSG_ERROR([combining --enable-optimize and --enable-debug is not possible])],
	[x*.yes.yes.*],
		[AC_MSG_ERROR([combining --enable-optimize and --enable-profile is not possible])],
	[x*.yes.*.yes],
		[AC_MSG_ERROR([combining --enable-optimize and --enable-instrument is not possible])],
	# handle defaults after user requests
	[xyes.*.*.*], [
		enable_debug=yes
		AS_VAR_IF([enable_optimize], [no], [],
			[disable_optimize="(as debug is enabled)"])
		enable_optimize=no
		enable_profile=`echo $enable_profile | sed 's:^def_::'`
		enable_instrument=`echo $enable_instrument | sed 's:^def_::'`
		],
	[x*.*.yes.*], [
		enable_debug=`echo $enable_debug | sed 's:^def_::'`
		AS_VAR_IF([enable_optimize], [no], [],
			[disable_optimize="(as profile is enabled)"])
		enable_optimize=no
		enable_profile=yes
		enable_instrument=`echo $enable_instrument | sed 's:^def_::'`
		],
	[x*.*.*.yes], [
		enable_debug=`echo $enable_debug | sed 's:^def_::'`
		AS_VAR_IF([enable_optimize], [no], [],
			[disable_optimize="(as instrument is enabled)"])
		enable_optimize=no
		enable_profile=`echo $enable_profile | sed 's:^def_::'`
		enable_instrument=yes
		],
	[x*.*no.*.*], [
		enable_debug=`echo $enable_debug | sed 's:^def_::'`
		enable_optimize=no
		enable_profile=`echo $enable_profile | sed 's:^def_::'`
		enable_instrument=`echo $enable_instrument | sed 's:^def_::'`
		],
	[x*.*yes.*.*], [
		# enable-optimize overrules other defaults
		enable_optimize=yes
		AS_VAR_IF([enable_debug], [no], [],
			[disable_debug="(as optimize is enabled)"])
		enable_debug=no
		AS_VAR_IF([enable_profile], [no], [],
			[disable_profile="(as optimize is enabled)"])
		enable_profile=no
		AS_VAR_IF([enable_instrument], [no], [],
			[disable_instrument="(as optimize is enabled)"])
		enable_instrument=no
		],
	[x*],
		[AC_MSG_WARN([unhandled configuration $enable_debug.$enable_optimize.$enable_profile.$enable_instrument, please file a bug on bugs.monetdb.org])])
# make defaults real for flags which don't conflict with anything
enable_assert=`echo $enable_assert | sed 's:^def_::'`

AC_MSG_CHECKING([for --enable-debug])
AS_VAR_IF([enable_debug], [yes], [
	origCFLAGS=$CFLAGS
	# remove "-Ox" as some compilers don't like "-g -Ox" combinations
	CFLAGS=" $CFLAGS "
	CFLAGS=`echo "$CFLAGS" | sed -e 's| -O[[0-9]] | |g' -e 's| -g | |g' -e 's|^ ||' -e 's| $||'`
	# add "-g"
	CFLAGS="$CFLAGS -g"
	AS_CASE([$GCC-$host_os],
		[yes-aix*], [CFLAGS="$CFLAGS -gxcoff"])
	changedCFLAGS=
	for flag in $origCFLAGS ; do
		AS_CASE([" $CFLAGS "],
			[*" $flag "*], [],
			[changedCFLAGS="$changedCFLAGS, removed $flag"])
	done
	for flag in $CFLAGS ; do
		AS_CASE([" $origCFLAGS "],
			[*" $flag "*], [],
			[changedCFLAGS="$changedCFLAGS, added $flag"])
	done
	changedCFLAGS=`echo $changedCFLAGS | sed -e 's|^, ||'`
	AC_MSG_RESULT([$enable_debug: $changedCFLAGS])],
	[AC_MSG_RESULT([no])])

AC_MSG_CHECKING([for --enable-assert])
AS_VAR_IF([enable_assert], [no], [
	AC_DEFINE([NDEBUG], 1, [Define if you do not want assertions])
	AC_MSG_RESULT([no])],
	[AC_MSG_RESULT([yes])])


AC_MSG_CHECKING([for --enable-optimize])
AS_VAR_IF([enable_optimize], [yes], [
	origCFLAGS="$CFLAGS"
	# remove "-g" as some compilers don't like "-g -Ox" combinations
	# remove "-O2" as we add "-Ox" and some compilers don't like "-Oy -Ox" combinations
	CFLAGS=" $CFLAGS "
	CFLAGS=`echo "$CFLAGS" | sed -e 's| -g | |g' -e 's| -O2 | |g' -e 's|^ ||' -e 's| $||'`
	# Optimization flags
	AS_CASE([$GCC-$CC_ver],
		[yes-clang-*],
			[CFLAGS="$CFLAGS -O3 -fomit-frame-pointer" # -finline-functions is no longer supported in llvm 3.6
			],
		[yes-*], [
			# -fomit-frame-pointer crashes memprof
			# -O1 on gcc enables all slight optimisations:
			#  -fauto-inc-dec -fcprop-registers -fdce -fdefer-pop
			#  -fdelayed-branch -fdse -fguess-branch-probability
			#  -fif-conversion2 -fif-conversion -fipa-pure-const
			#  -fipa-reference -fmerge-constants -fsplit-wide-types
			#  -ftree-builtin-call-dce -ftree-ccp -ftree-ch
			#  -ftree-copyrename -ftree-dce -ftree-dominator-opts
			#  -ftree-dse -ftree-forwprop -ftree-fre -ftree-phiprop
			#  -ftree-sra -ftree-pta -ftree-ter -funit-at-a-time
			# on top of this -fomit-frame-pointer is enabled on machines
			# where this does not interfere with debugging.
			# -O2 on gcc enables optimisations which do not involve a
			# speed-space tradeoff on top of -O1:
			#  -fthread-jumps -falign-functions  -falign-jumps
			#  -falign-loops -falign-labels -fcaller-saves -fcrossjumping
			#  -fcse-follow-jumps  -fcse-skip-blocks
			#  -fdelete-null-pointer-checks -fexpensive-optimizations
			#  -fgcse -fgcse-lm -finline-small-functions
			#  -findirect-inlining -fipa-sra -foptimize-sibling-calls
			#  -fpeephole2 -fregmove -freorder-blocks -freorder-functions
			#  -frerun-cse-after-loop -fsched-interblock -fsched-spec
			#  -fschedule-insns -fschedule-insns2 -fstrict-aliasing
			#  -fstrict-overflow -ftree-switch-conversion -ftree-pre
			#  -ftree-vrp
			# (Gentoo enables -D_FORTIFY_SOURCE=2 starting at -O2)
			# -O3 on gcc enables some more expensive optimisations on top
			# of -O2:
			# -finline-functions, -funswitch-loops,
			# -fpredictive-commoning, -fgcse-after-reload,
			# -ftree-vectorize and -fipa-cp-clone
			CFLAGS="-O3 -fomit-frame-pointer -pipe ${CFLAGS}"
			# because we explicitly disable debugging, we can explicitly
			# enable -fomit-frame-pointer here
			# notes on -funroll-all-loops:
			# - does nothing on i?86-linux and
			#  x86_64-linux
			# - speeds up and causes crashes on
			#  powerpc-linux
			# - slows down and makes output incorrect on
			#  sparc-solaris
			# - speeds up and fixes some outputs on
			#  sparcv9-solaris
			# notes on -fast for Darwin:
			# - ppc: -fast switch causes arithmetic
			#  differences, and slower running code for
			#  SQL, some faster for M5
			# - x86/x64: -fast switch results in json
			#  code producing wrong results, or
			#  segfaults (when used with Mtest)
			],
		[
		AS_CASE([$host-$icc_ver],
			dnl  handle non-Intel compilers ($icc_ver=""), first
			[*solaris*-],      [CFLAGS="$CFLAGS -xO5"],
			[*-*-*-],          [CFLAGS="$CFLAGS -O2"],
			dnl  With icc-8.*, Interprocedural (IP) Optimization does not seem to work with MonetDB:
			dnl  With "-ipo -ipo_obj", pass-through linker options ("-Wl,...") are not handled correctly,
			dnl  and with "-ip -ipo_obj", the resulting Mserver segfaults immediately.
			dnl  Hence, we skip Interprocedural (IP) Optimization with icc-8.*.
			[x86_64-*-*-8.*],  [CFLAGS="$CFLAGS -mp1 -O3 -restrict -unroll               -tpp7 -axWP   "],
			[x86_64-*-*-9.*],  [CFLAGS="$CFLAGS -mp1 -O3 -restrict -unroll               -tpp7 -axWP   "],

		  dnl   [x86_64-*-*-10.*], [CFLAGS="$CFLAGS -mp1 -O1 -restrict -unroll                     -axWPT  "], # K.O.!
		  dnl   [x86_64-*-*-10.*], [CFLAGS="$CFLAGS                                                        "], # OK.
		  dnl   [x86_64-*-*-10.*], [CFLAGS="$CFLAGS -mp1                                                   "], # OK.
		  dnl   [x86_64-*-*-10.*], [CFLAGS="$CFLAGS      -O1                                               "], # K.O.!
		  dnl   [x86_64-*-*-10.*], [CFLAGS="$CFLAGS      -O2                                               "], # OK.
			[x86_64-*-*-10.*], [CFLAGS="$CFLAGS      -O3                                               "], # OK.
		  dnl   [x86_64-*-*-10.*], [CFLAGS="$CFLAGS          -restrict                                     "],
		  dnl   [x86_64-*-*-10.*], [CFLAGS="$CFLAGS                    -unroll                             "],
		  dnl   [x86_64-*-*-10.*], [CFLAGS="$CFLAGS                                                -axWPT  "],

			# We need to use -mp instead of -mp1 as
			# otherwise nextafter() & nextafterf() do
			# not work correctly for 0
			[x86_64-*-*-11.*], [CFLAGS="$CFLAGS -mp  -O3 -restrict -unroll                     -axSSE4.2"],
			# -mp does no longer exist with icc 15;
			# using -mieee-fp, instead.  adding
			# -axSSE4.2 (or even -axCORE-AVX512)
			# results in errors like "error: ODBCFetch
			# has been targeted for automatic cpu
			# dispatch" ...
			[x86_64-*-*-15.*], [CFLAGS="$CFLAGS -mieee-fp -O3 -restrict -unroll -no-inline-max-size -no-inline-max-total-size -inline-level=1 -xHost"],

			[i*86-*-*-8.*],    [CFLAGS="$CFLAGS -mp1 -O3 -restrict -unroll               -tpp6 -axKWNPB"],
			[i*86-*-*-9.*],    [CFLAGS="$CFLAGS -mp1 -O3 -restrict -unroll               -tpp6 -axKWNPB"],

		  dnl   [i*86-*-*-10.*],   [CFLAGS="$CFLAGS -mp1 -O1 -restrict -unroll                     -axWPT  "], # K.O.!
		  dnl   [i*86-*-*-10.*],   [CFLAGS="$CFLAGS                                                        "], # OK.
		  dnl   [i*86-*-*-10.*],   [CFLAGS="$CFLAGS -mp1                                                   "], # OK.
		  dnl   [i*86-*-*-10.*],   [CFLAGS="$CFLAGS      -O1                                               "], # K.O.!
		  dnl   [i*86-*-*-10.*],   [CFLAGS="$CFLAGS      -O2                                               "], # OK.
			[i*86-*-*-10.*],   [CFLAGS="$CFLAGS      -O3                                               "], # OK.
		  dnl   [i*86-*-*-10.*],   [CFLAGS="$CFLAGS          -restrict                                     "],
		  dnl   [i*86-*-*-10.*],   [CFLAGS="$CFLAGS                    -unroll                             "],
		  dnl   [i*86-*-*-10.*],   [CFLAGS="$CFLAGS                                                -axWPT  "],

			[ia64-*-*-8.*],    [CFLAGS="$CFLAGS -mp1 -O2 -restrict -unroll               -tpp2 -mcpu=itanium2"],
			[ia64-*-*-9.*],    [CFLAGS="$CFLAGS -mp1 -O2 -restrict -unroll               -tpp2 -mcpu=itanium2"],

		  dnl   [ia64-*-*-10.*],   [CFLAGS="$CFLAGS -mp1 -O2 -restrict -unroll                     -mcpu=itanium2"], # OK.
			[ia64-*-*-10.*],   [CFLAGS="$CFLAGS -mp1 -O3 -restrict -unroll                     -mcpu=itanium2"],
		  dnl   [ia64-*-*-10.*],   [CFLAGS="$CFLAGS                                                              "],
		  dnl   [ia64-*-*-10.*],   [CFLAGS="$CFLAGS -mp1                                                         "],
		  dnl   [ia64-*-*-10.*],   [CFLAGS="$CFLAGS      -O1                                                     "],
		  dnl   [ia64-*-*-10.*],   [CFLAGS="$CFLAGS      -O2                                                     "],
		  dnl   [ia64-*-*-10.*],   [CFLAGS="$CFLAGS      -O3                                                     "],
		  dnl   [ia64-*-*-10.*],   [CFLAGS="$CFLAGS          -restrict                                           "],
		  dnl   [ia64-*-*-10.*],   [CFLAGS="$CFLAGS                    -unroll                                   "],
		  dnl   [ia64-*-*-10.*],   [CFLAGS="$CFLAGS                                                -mcpu=itanium2"],

			[i*86-*-*],        [CFLAGS="$CFLAGS -mp1 -O3 -restrict -unroll -ipo -ipo_obj -tpp6 -axiMKW"],
			[ia64-*-*],        [CFLAGS="$CFLAGS -mp1 -O2 -restrict -unroll -ipo -ipo_obj -tpp2 -mcpu=itanium2"]
			# With "-O3", ecc does not seem to produce stable/correct? binaries under Linux64
			# (Mserver produces some incorrect BATpropcheck warnings);
			# hence, we use only "-O2", here.
		)
	])
	# for stack-traces with assert, we DO want a frame-pointer
	AS_VAR_IF([enable_assert], [yes],
		[CFLAGS=`echo $CFLAGS | sed 's:-fomit-frame-pointer::'`])
	changedCFLAGS=
	for flag in $origCFLAGS ; do
		AS_CASE([" $CFLAGS "],
			[*" $flag "*], [],
			[changedCFLAGS="$changedCFLAGS, removed $flag"])
	done
	for flag in $CFLAGS ; do
		AS_CASE([" $origCFLAGS "],
			[*" $flag "*], [],
			[changedCFLAGS="$changedCFLAGS, added $flag"])
	done
	changedCFLAGS=`echo $changedCFLAGS | sed -e 's|^, ||'`
	AC_MSG_RESULT([yes: $changedCFLAGS])

	# The default configure invocation when doing an rpmbuild also uses this.
	# In fact, rpmbuild uses -Wp,-D_FORTIFY_SOURCE=2 (cf. /usr/lib/rpm/redhat/macros),
	# but clang 3.3 complains about "argument unused during compilation";
	# hence, we resort to plain "-D_FORTIFY_SOURCE=2", which appears to work fine
	# also with `make rpm`. Well, worst case is that we miss a check that during
	# "normal" build, that is then caught by '-Wp,-D_FORTIFY_SOURCE=2' during `make rpm`.
	MCHECK_ADD_FLAG([-D_FORTIFY_SOURCE=2])],
	[AC_MSG_RESULT([no])])

AC_MSG_CHECKING([for --enable-profile])
AS_VAR_IF([enable_profile], [yes], [
	AC_DEFINE([PROFILE], 1, [Compiler flag])
	need_profile=yes
	AS_VAR_IF([GCC], [yes], [
		CFLAGS="$CFLAGS -pg"
		AC_MSG_RESULT([yes: -pg])],
		[AC_MSG_RESULT([no])])],
	[AC_MSG_RESULT([no])])
AM_CONDITIONAL([PROFILING], [test "x$need_profile" = xyes])

AC_MSG_CHECKING([for --enable-instrument])
AS_VAR_IF([enable_instrument], [yes], [
	AC_DEFINE([PROFILE], 1, [Compiler flag])
	need_instrument=yes
	AS_VAR_IF([GCC], [yes], [
		CFLAGS="$CFLAGS -finstrument-functions -g"
		AC_MSG_RESULT([yes: -finstrument-functions -g])], [
		AC_MSG_RESULT([no])])],
		[AC_MSG_RESULT([no])])

AC_PATH_PROG([BASH], [bash], [/usr/bin/bash], [$PATH])
AC_CHECK_PROG([RM], [rm], [rm -f])
AC_CHECK_PROG([MV], [mv], [mv -f])
AC_PROG_LN_S
AC_CHECK_PROGS([DIFF], [gdiff diff])
AC_DEFINE_UNQUOTED([DIFF], ["$DIFF"], [Program to perform diffs])

AC_CHECK_PROGS([RPMBUILD], [rpmbuild])

# check major version of python
# check if python2 and python3 exist
# if python2 or python3 doesn't exist, use python if major matches

org_have_python2=auto
have_python2=$org_have_python2
PYTHON2=python2
org_have_python3=auto
have_python3=$org_have_python3
PYTHON3=python3

AC_ARG_WITH([python2],
	[AS_HELP_STRING([--with-python2=FILE], [python2 is installed as FILE])],
	[have_python2="$withval"])

AS_CASE([$have_python2],
	[yes|no|auto], [],
	[
		PYTHON2="$have_python2"])

AC_ARG_WITH([python3],
	[AS_HELP_STRING([--with-python3=FILE], [python3 is installed as FILE])],
	[have_python3="$withval"])

AS_CASE([$have_python3],
	[yes|no|auto], [],
	[
		PYTHON3="$have_python3"])

AC_ARG_WITH([pyconfig],
	[AS_HELP_STRING([--with-pyconfig=FILE], [python-config is installed as FILE])],
	[have_pyconfig="$withval"])

AC_ARG_WITH([pyversion],
	[AS_HELP_STRING([--with-pyversion=FILE], [python is installed as FILE])],
	[have_pyversion="$withval"])

# Figure out a default for PYTHON2 or PYTHON3
AC_PATH_PROG([PYTHON], [python], [no], [$PATH])
PYTHON_MAJ=unknown
AS_VAR_IF([PYTHON], [no], [], [
	AC_MSG_CHECKING([major version of $PYTHON])
	AS_CASE([`"$PYTHON" -V 2>&1`],
		["Python 2."[[67]]*], [
			PYTHON_MAJ=2],
		["Python 3."*], [
			PYTHON_MAJ=3])
	AC_MSG_RESULT([$PYTHON_MAJ])])

AS_VAR_IF([have_python2], [no],
	[],
	[AS_VAR_IF([cross_compiling], [yes],
		[],
		[AC_PATH_PROG([PYTHON2], [$PYTHON2], [no], [$PATH])
		 AS_VAR_IF([PYTHON2], [no],
			[AS_IF([( test "x$have_python2" = xyes || test "x$have_python2" = xauto ) && test "x$PYTHON_MAJ" = x2],
				[PYTHON2="$PYTHON"
				 have_python2=yes],
				[AS_VAR_IF([have_python], [auto],
					[have_python2=no
				 	 why_have_python2="(Python 2 executable not found)"],
					[AC_MSG_ERROR([Python 2 executable not found])])])],
			[AS_CASE([`"$PYTHON2" -V 2>&1`],
				["Python 2."[[67]]*], [],
				[AS_VAR_IF([have_python2], [auto], [
					have_python2=no
					why_have_python2="(Python 2 executable too old)"], [
					AC_MSG_ERROR([Python 2 executable too old])])])])])])

AS_VAR_IF([have_python2], [no],
	[# no Python 2 implies no Python 2 libraries
	 have_python2_libdir=no
	 PYTHON2_LIBDIR=""
	 # and no interpreter
	 PYTHON2=false],
	[have_python2_libdir=auto
	 AC_ARG_WITH([python2-libdir],
		[AS_HELP_STRING([--with-python2-libdir=DIR],
			[relative path for Python 2 library directory (where Python 2 modules should be installed)])],
		[have_python2_libdir="$withval"])
	 AS_CASE([$have_python2_libdir],
		[yes|auto], [
			AS_VAR_IF([cross_compiling], [yes],
				[AC_MSG_ERROR([Must specify --with-python2-libdir when cross compiling])])
			AS_CASE([$host_os-`"$PYTHON2" -V 2>&1`],
				[darwin9*-*2.5.1], [
					# This is the Python installed on OSX Leopard, in
					# later versions of OSX, Python is fixed to return
					# standard output
					PYTHON2_LIBDIR=`"$PYTHON2" -c 'import distutils.sysconfig; print distutils.sysconfig.get_python_lib(0,1,"'"$Qprefix"'")' 2>/dev/null`/site-packages],
				[
					# Use prefix field for Ubuntu Python, bug #3207
					PYTHON2_LIBDIR=`"$PYTHON2" -c 'import distutils.sysconfig; print distutils.sysconfig.get_python_lib(0,0,"'"$Qprefix"'")' 2>/dev/null`])
			PYTHON2_LIBDIR=`echo "$PYTHON2_LIBDIR" | sed "s|^$Qprefix/||"`],
		[no], [],
		[$Qprefix/*], [ # dubious
			PYTHON2_LIBDIR=`echo "$have_python2_libdir" | sed "s|^$Qprefix/||"`
			have_python2_libdir=yes],
		[	PYTHON2_LIBDIR="$have_python2_libdir"
			have_python2_libdir=yes])])

AC_SUBST([PYTHON2])
QPYTHON2="$PYTHON2"
XPYTHON2=`$translatepath "$QPYTHON2"`
QXPYTHON2=`echo "$XPYTHON2" | sed 's/\\\\/\\\\\\\\/g'`
AC_SUBST([QXPYTHON2])
AM_CONDITIONAL([HAVE_PYTHON2], [test x"$have_python2" != xno])
AC_SUBST([PYTHON2_LIBDIR])
QPYTHON2_LIBDIR="$PYTHON2_LIBDIR"
XPYTHON2_LIBDIR=`$translatepath "$QPYTHON2_LIBDIR"`
QXPYTHON2_LIBDIR=`echo "$XPYTHON2_LIBDIR" | sed 's/\\\\/\\\\\\\\/g'`
AC_SUBST([QXPYTHON2_LIBDIR])

AS_VAR_IF([have_python3], [no],
	[],
	[AS_VAR_IF([cross_compiling], [yes],
		[],
		[AC_PATH_PROG([PYTHON3], [$PYTHON3], [no], [$PATH])
		 AS_VAR_IF([PYTHON3], [no],
		 	[AS_IF([( test "x$have_python3" = xyes || test "x$have_python3" = xauto ) && test "x$PYTHON_MAJ" = x3],
				[PYTHON3="$PYTHON"
				 have_python3=yes],
				[AS_VAR_IF([have_python3], [auto],
					[have_python3=no
				 	 why_have_python3="(Python 3 executable not found)"],
					[AC_MSG_ERROR([Python3 executable not found])])])])])])

AS_VAR_IF([have_python3], [no],
	[# no Python 3 implies no Python 3 libraries
	 have_python3_libdir=no
	 PYTHON3_LIBDIR=""
	 # and no interpreter
	 PYTHON3=false],
	[have_python3_libdir=auto

	 AC_ARG_WITH([python3-libdir],
		[AS_HELP_STRING([--with-python3-libdir=DIR],
			[relative path for Python 3 library directory (where Python 3 modules should be installed)])],
		[have_python3_libdir="$withval"])

	 AS_CASE([$have_python3_libdir],
		[yes|auto], [
			AS_VAR_IF([cross_compiling], [yes],
				[AC_MSG_ERROR([Must specify --with-python3-libdir when cross compiling])])
			# Use prefix field for Ubuntu Python, bug #3207
			PYTHON3_LIBDIR=`"$PYTHON3" -c 'import distutils.sysconfig; print(distutils.sysconfig.get_python_lib(0,0,"'"$Qprefix"'"))' 2>/dev/null`
			PYTHON3_LIBDIR=`echo "$PYTHON3_LIBDIR" | sed "s|^$Qprefix/||"`],
		[no], [],
		[$Qprefix/*], [ # dubious
			PYTHON3_LIBDIR=`echo "$have_python3_libdir" | sed "s|^$Qprefix/||"`
			have_python3_libdir=yes],
		[	PYTHON3_LIBDIR="$have_python3_libdir"
			have_python3_libdir=yes])])

AC_SUBST([PYTHON3])
QPYTHON3="$PYTHON3"
XPYTHON3=`$translatepath "$QPYTHON3"`
QXPYTHON3=`echo "$XPYTHON3" | sed 's/\\\\/\\\\\\\\/g'`
AC_SUBST([QXPYTHON3])
AM_CONDITIONAL([HAVE_PYTHON3], [test x"$have_python3" != xno])
AC_SUBST([PYTHON3_LIBDIR])
QPYTHON3_LIBDIR="$PYTHON3_LIBDIR"
XPYTHON3_LIBDIR=`$translatepath "$QPYTHON3_LIBDIR"`
QXPYTHON3_LIBDIR=`echo "$XPYTHON3_LIBDIR" | sed 's/\\\\/\\\\\\\\/g'`
AC_SUBST([QXPYTHON3_LIBDIR])

AM_CONDITIONAL([HAVE_PYTHON], [test x"$have_python2" != xno -o x"$have_python3" != xno])

AS_VAR_IF([PYTHON], [no],
	[AS_VAR_IF([PYTHON2], [no],
		[AS_VAR_IF([PYTHON3], [no],
			[]
			[PYTHON="$PYTHON3"])],
		[PYTHON="$PYTHON2"])])
AC_SUBST([PYTHON])

# Mtest/Mfilter currently require Python 2
AS_VAR_IF([enable_testing], [no],
	[],
	[AS_VAR_IF([PYTHON2], [false],
		[enable_testing=no])])

AC_PROG_YACC
AS_CASE([$YACC],
	[bison*],
	[
		# Ubuntu still comes with an ancient bison that uses defines
		# without checking if they are defined.  This triggers a warning
		# from gcc with -Wundef.  If we have a bison < 2.4.3, then
		# disable the warning.
		BISON_VER=`$YACC --version | head -n1 | sed -e 's/^.* //'`
		AS_VERSION_COMPARE([$BISON_VER],
			[2.4.3],
			[X_CFLAGS=`echo ${X_CFLAGS} | sed -e 's/-Wundef//g'`])
	],
	[
		# in embedded mode, we ship the bison-generated files
		AS_VAR_IF([enable_embedded], [no], [
			AS_VAR_IF([enable_sql], [yes], [
				AC_MSG_ERROR([MonetDB/SQL requires bison])])
			enable_sql=no
			AS_VAR_IF([disable_sql], [], [
				disable_sql="(MonetDB/SQL requires bison)"])], [
			AC_MSG_WARN([ignoring missing bison in embedded configuration])])
	])

INSTALL_BACKUP=""
AC_MSG_CHECKING([$INSTALL --backup option])
AS_VAR_IF([INSTALL], [],
	[],
	[inst=`echo $INSTALL | sed 's/ .*//'`
	 AS_IF([test ! "`file $inst | grep 'shell script' 2>/dev/null`"],
		[echo "" > c 2>/dev/null
		 AS_IF([$INSTALL --backup=nil c d 1>/dev/null 2>/dev/null],
			[INSTALL_BACKUP="--backup=nil"])
		 AS_IF([$INSTALL -C --backup=nil c e 1>/dev/null 2>/dev/null],
			[INSTALL_BACKUP="-C --backup=nil"])
		 rm -f c d e 2>/dev/null])])
AC_MSG_RESULT([$INSTALL_BACKUP])
AC_SUBST([INSTALL_BACKUP])

SOPREF=lib
AS_CASE([$host_os],
[mac*], [
	# Mac OS 9 stuff
	DIRSEP=':'
	QDIRSEP=':'
	AC_MSG_ERROR([mac not supported])],
[*cygwin*], [
	DIRSEP='/'
	QDIRSEP='/'
	PATHSEP=':'
	SOEXT='-0.dll'
	SOPREF=cyg],
[*darwin*], [
	DIRSEP='/'
	QDIRSEP='/'
	PATHSEP=':'
	SOEXT='.dylib'],
[
	DIRSEP='/'
	QDIRSEP='/'
	PATHSEP=':'
	SOEXT='.so'])
AC_SUBST([DIRSEP])
AC_SUBST([QDIRSEP])
AC_SUBST([PATHSEP])
AC_SUBST([SOEXT])
AC_DEFINE_UNQUOTED([DIR_SEP], ['$QDIRSEP'], [Directory separator])
AC_DEFINE_UNQUOTED([DIR_SEP_STR], ["$QDIRSEP"], [Directory separator])
AC_DEFINE_UNQUOTED([PATH_SEP], ['$PATHSEP'], [Path separator])
AC_DEFINE_UNQUOTED([PATH_SEP_STR], ["$PATHSEP"], [Path separator])
AC_DEFINE_UNQUOTED([SO_PREFIX], ["$SOPREF"], [Shared Object prefix])
AC_DEFINE_UNQUOTED([SO_EXT], ["$SOEXT"], [Shared Object extension])

AS_CASE([$host_os],
    [cygwin*], [],
    [
	CYGPATH_W=echo
	CYGPATH_WP=echo
	AC_SUBST([CYGPATH_W])
	AC_SUBST([CYGPATH_WP])])

#    checks for libraries
# check for bz2 (de)compression library
org_have_bz2=auto
have_bz2=$org_have_bz2
BZ_CFLAGS=""
BZ_LIBS=""
AC_ARG_WITH([bz2],
	[AS_HELP_STRING([--with-bz2=DIR],
		[bz2 library is installed in DIR])],
	[have_bz2="$withval"])

AS_CASE([$have_bz2],
	[yes|no|auto], [],
	[
		BZ_CFLAGS="-I$withval/include"
		BZ_LIBS="-L$withval/lib"])

AC_MSG_CHECKING([for bzip2])
AS_VAR_IF([have_bz2], [no], [], [
	save_CPPFLAGS="$CPPFLAGS"
	CPPFLAGS="$CPPFLAGS $BZ_CFLAGS"
	save_LIBS="$LIBS"
	LIBS="$LIBS $BZ_LIBS -lbz2"
	AC_LINK_IFELSE([AC_LANG_PROGRAM([[@%:@include <stdio.h>
@%:@include <bzlib.h>]], [[(void)BZ2_bzopen("","");]])],
		BZ_LIBS="$BZ_LIBS -lbz2",
		[AS_VAR_IF([have_bz2], [auto], [], [
			AC_MSG_ERROR([bz2 library not found])])
		  have_bz2=no
		  why_have_bz2="(bz2 library not found)" ])
	LIBS="$save_LIBS"
	CPPFLAGS="$save_CPPFLAGS"])

AS_VAR_IF([have_bz2], [no],
	[BZ_CFLAGS=""
	 BZ_LIBS=""
	 AC_MSG_RESULT([no])],
	[AC_DEFINE([HAVE_LIBBZ2], 1, [Define if you have the bz2 library])
	 AC_MSG_RESULT([yes: $BZ_LIBS])])

AC_SUBST([BZ_CFLAGS])
AC_SUBST([BZ_LIBS])
AM_CONDITIONAL([HAVE_LIBBZ2], [test x$have_bz2 != xno])

DL_LIBS=""
AC_CHECK_LIB([dl], [dlopen], [DL_LIBS="-ldl" ] )
AC_SUBST([DL_LIBS])

MATH_LIBS=""
AC_CHECK_LIB([m], [sqrt], [MATH_LIBS="-lm" ] )
AC_SUBST([MATH_LIBS])

MALLOC_LIBS=""
AC_CHECK_LIB([umem], [malloc], [MALLOC_LIBS="-lumem" ],
	AC_CHECK_LIB([mtmalloc], [malloc], [MALLOC_LIBS="-lmtmalloc" ],
		AC_CHECK_LIB([malloc], [malloc], [MALLOC_LIBS="-lmalloc" ] )))
AC_SUBST([MALLOC_LIBS])

KVM_LIBS=""
AC_CHECK_LIB([kvm], [kvm_open], [KVM_LIBS="-lkvm" ] )
AC_SUBST([KVM_LIBS])

save_LIBS="$LIBS"
LIBS="$LIBS $MALLOC_LIBS"
AC_CHECK_FUNCS([mallopt])
AC_CHECK_FUNC([mallinfo],
	[AC_COMPILE_IFELSE(
		[AC_LANG_PROGRAM([[@%:@include <malloc.h>]],
			[[struct mallinfo m;int x[1+sizeof(m.usmblks)-sizeof(void *)]]])],
		[AC_DEFINE([HAVE_USEFUL_MALLINFO], 1,
			[Define if you have mallinfo])])])
LIBS="$save_LIBS"

# libpthread
org_have_pthread=auto
have_pthread=$org_have_pthread
PTHREAD_LIBS=""
PTHREAD_INCS=""
PTHREAD_EXTRA=""
AC_ARG_WITH([pthread],
	[AS_HELP_STRING([--with-pthread=DIR],
		[pthread library is installed in DIR])],
	[have_pthread="$withval"])

AS_CASE([$have_pthread],
[yes|no|auto], [],
[
	PTHREAD_LIBS="-L$withval/lib"
	PTHREAD_INCS="-I$withval/include"])

AS_VAR_IF([have_pthread], [no], [], [

	save_CPPFLAGS="$CPPFLAGS"
	CPPFLAGS="$CPPFLAGS $PTHREAD_INCS $PTHREAD_EXTRA"
	AC_CHECK_HEADERS([pthread.h semaphore.h sched.h])
	CPPFLAGS="$save_CPPFLAGS"

	save_LIBS="$LIBS"
	save_CPPFLAGS="$CPPFLAGS"
	AS_CASE([$GCC-$have_pthread-$CC_ver],
		[yes-auto-clang-5.*|yes-yes-clang-5.*|yes-auto-clang-6.*|yes-yes-clang-6.*|yes-auto-clang-7.*|yes-yes-clang-7.*|yes-auto-clang-8.*|yes-yes-clang-8.*], [
			# clang 5.*/6.*/7.* (Xcode 6.0) does not
			# seem to have / require -pthread as compiler
			# option; on Mac OS X Yosamite, "Apple LLVM
			# version 6.0 (clang-600.0.51) (based on LLVM 3.5svn)"
			# complains about "error: argument unused during
			# compilation: '-pthread'".
			# However, disabling -pthread completely might be
			# too redical/harsh; see also
			# https://www.monetdb.org/pipermail/developers-list/2014-March/004264.html
			# a better alternative might be required but is
			# still pending ...
			],
		[yes-auto-*|yes-yes-*], [
			# use GCC's knowledge about the target platform, sets flags
			# for both the preprocessor as well as the linker
			PTHREAD_INCS="-pthread"
			PTHREAD_LIBS="-pthread"
			CPPFLAGS="$CPPFLAGS $PTHREAD_INCS"
			LIBS="$LIBS $PTHREAD_LIBS"],
		[
			# ok, do old-fashioned stuff
			CPPFLAGS="$CPPFLAGS $PTHREAD_INCS" # in case user did --with-pthreads
			LIBS="$LIBS $PTHREAD_LIBS"
			pthread_found=yes
			AC_SEARCH_LIBS([sem_init], [pthreadGC2 pthreadGC1 pthreadGC pthread],
				[test "$ac_cv_search_sem_init" = "none required" || LIBS="$LIBS $ac_cv_search_sem_init"],
				[pthread_found=no])
			AS_VAR_IF([pthread_found], [no], [
				pthread_found=yes
				# sun
				AC_SEARCH_LIBS([sem_post], [pthread],
					[LIBS="$LIBS -lpthread -lposix4"],
					[pthread_found=no],
					["-lposix4"])])
			AS_VAR_IF([pthread_found], [no], [
				pthread_found=yes
				# hp-ux
				AC_SEARCH_LIBS([sem_post], [pthread],
					[LIBS="$LIBS -lpthread -lrt"],
					[pthread_found=no],
					["-lrt"])])
			AS_VAR_IF([pthread_found], [no], [
				AS_VAR_IF([have_pthread], [auto], [], [
					AC_MSG_ERROR([pthread library not found])])
				have_pthread=no
				why_have_pthread="(pthread library not found)"])
			PTHREAD_LIBS="$LIBS"])

	AC_SEARCH_LIBS([pthread_kill], [],
		[AC_DEFINE([HAVE_PTHREAD_KILL], 1,
			[Define if you have the pthread_kill function])])
	AC_SEARCH_LIBS([pthread_sigmask], [],
		[AC_DEFINE([HAVE_PTHREAD_SIGMASK], 1,
			[Define if you have the pthread_sigmask function])])
	# this function very ugly is overloaded with semaphore stuff
	# so we DO need to check for it, for platforms which have it
	# in a separate lib, like Solaris
	AC_SEARCH_LIBS([sem_wait], [rt])

	LIBS="$save_LIBS"
	CPPFLAGS="$save_CPPFLAGS"
])
AC_MSG_CHECKING([whether we have pthread support])
AS_VAR_IF([have_pthread], [no],
	[PTHREAD_LIBS=""
	 PTHREAD_INCS=""
	 AC_MSG_RESULT([no])
	 AC_MSG_ERROR([MonetDB requires libpthread (try --with-pthreada)])],
	[AC_DEFINE([HAVE_LIBPTHREAD], 1, [Define if you have the pthread library])
	 PTHREAD_INCS="$PTHREAD_INCS $PTHREAD_EXTRA"
	 # CPPFLAGS="$CPPFLAGS $PTHREAD_INCS"
	 AC_MSG_RESULT([yes: $PTHREAD_INCS $PTHREAD_LIBS])])
AC_SUBST([PTHREAD_LIBS])
AC_SUBST([PTHREAD_INCS])

# libreadline
org_have_readline=auto
have_readline=$org_have_readline
READLINE_LIBS=""
READLINE_INCS=""
AC_ARG_WITH([readline],
	[AS_HELP_STRING([--with-readline=DIR],
		[readline library is installed in DIR])],
	[have_readline="$withval"])

AS_CASE([$have_readline],
	[yes|no|auto], [],
	[
		READLINE_LIBS="-L$have_readline/lib"
		READLINE_INCS="-I$have_readline/include"])

save_LIBS="$LIBS"
LIBS="$LIBS $READLINE_LIBS"
save_CPPFLAGS="$CPPFLAGS"
CPPFLAGS="$CPPFLAGS $READLINE_INCS"
AS_VAR_IF([have_readline], [no], [], [
	# use different functions in the cascade of AC_CHECK_LIB
	# calls since configure may cache the results
	AC_CHECK_HEADER([readline/readline.h],
		AC_CHECK_LIB([readline], [readline],
			[READLINE_LIBS="$READLINE_LIBS -lreadline"],
			[AC_CHECK_LIB([readline], [rl_history_search_forward],
				[READLINE_LIBS="$READLINE_LIBS -lreadline -ltermcap"],
				[AC_CHECK_LIB([readline], [rl_reverse_search_history],
					[READLINE_LIBS="$READLINE_LIBS -lreadline -lncurses"],
					[AS_VAR_IF([have_readline], [yes], [
						AC_MSG_ERROR([readline library not found])])
					  have_readline=no
					  why_have_readline="(readline library not found)" ],
					[-lncurses])],
				[-ltermcap])],
			),
		[AS_VAR_IF([have_readline], [yes], [
			AC_MSG_ERROR([readline header file not found])])
		  have_readline=no
		  why_have_readline="(readline header file not found)" ])])

AS_VAR_IF([have_readline], [no],
	[],
	[
	 # provide an ACTION-IF-FOUND, or else all subsequent checks
	 # that involve linking will fail!
	 AC_CHECK_LIB([readline], [rl_completion_matches],
		[AC_MSG_CHECKING([whether rl_completion_func_t exists])
		 AC_COMPILE_IFELSE(
		 	[AC_LANG_PROGRAM(
				[[$ac_includes_default
				  @%:@include <readline/readline.h>]],
		 		[[rl_completion_func_t *func = NULL;]])],
			[AC_MSG_RESULT([yes])],
			[AS_VAR_IF([have_readline], [auto],
				[AC_MSG_RESULT([no])
				 have_readline=no
				 why_have_readline="(readline/readline.h does not contain rl_completion_func_t, is it GNU readline?)"],
				[AC_MSG_ERROR([readline/readline.h does not contain rl_completion_func_t, is it GNU readline?])])])],
		[AS_VAR_IF([have_readline], [auto], [], [
			AC_MSG_ERROR([readline library does not contain rl_completion_matches])])
		 have_readline=no
		 why_have_readline="(readline library does not contain rl_completion_matches)" ],
		[$READLINE_LIBS])])
CPPFLAGS="$save_CPPFLAGS"
LIBS="$save_LIBS"

AS_VAR_IF([have_readline], [no],
	[READLINE_LIBS=""
	 READLINE_INCS=""],
	[AC_DEFINE([HAVE_LIBREADLINE], 1,
		[Define if you have the readline library])])
AC_SUBST([READLINE_LIBS])
AC_SUBST([READLINE_INCS])

m4_ifndef([AM_ICONV],
  [m4_fatal([Could not locate the iconv autoconf
	macros. These are usually located in /usr/share/aclocal/iconv.m4 and
	provided by the gettext package.  If your macros are in a different
	location, try setting the environment variable
	M4DIRS="-I/other/macro/dir" before running ./bootstrap or autoreconf
	again.])])

AM_ICONV

org_have_uuid=yes
have_uuid=$org_have_uuid
AC_ARG_WITH([uuid],
	[AS_HELP_STRING([--with-uuid],
		[include UUID support (default=yes)])],
	[have_uuid=$withval])
AS_VAR_IF([have_uuid], [no], [], [
	# UUID: remember that most UNIX systems have uuid_* functions in their
	#      libc, not as separate library, such as from util-linux.  We
	#      can't just use pkg-config check because of that.
	save_LIBS="$LIBS"
	LIBS=
	AC_SEARCH_LIBS([uuid_generate], [uuid], [AC_DEFINE([HAVE_UUID], 1, [uuid])])
	UUID_LIBS="$LIBS"
	LIBS="$save_LIBS"
	AC_SUBST([UUID_LIBS])])
AM_CONDITIONAL([HAVE_UUID], [test x"$have_uuid" != xno])


# OpenSSL or CommonCrypto library
AS_CASE([$host],
[*-darwin*], [
	AC_DEFINE([HAVE_COMMONCRYPTO], 1, [Define if you have the CommonCrypto library])
	AC_CHECK_FUNC([CC_MD5_Update], [AC_DEFINE([HAVE_MD5_UPDATE], 1, [Define to 1 if you have the `MD5_Update' function.])])
	AC_CHECK_FUNC([CC_RIPEMD160_Update], [AC_DEFINE([HAVE_RIPEMD160_UPDATE], 1, [Define to 1 if you have the `RIPEMD160_Update' function.])])
	AC_CHECK_FUNC([CC_SHA1_Update], [AC_DEFINE([HAVE_SHA1_UPDATE], 1, [Define to 1 if you have the `SHA1_Update' function.])])
	AC_CHECK_FUNC([CC_SHA224_Update], [AC_DEFINE([HAVE_SHA224_UPDATE], 1, [Define to 1 if you have the `SHA224_Update' function.])])
	AC_CHECK_FUNC([CC_SHA256_Update], [AC_DEFINE([HAVE_SHA256_UPDATE], 1, [Define to 1 if you have the `SHA256_Update' function.])])
	AC_CHECK_FUNC([CC_SHA384_Update], [AC_DEFINE([HAVE_SHA384_UPDATE], 1, [Define to 1 if you have the `SHA384_Update' function.])])
	AC_CHECK_FUNC([CC_SHA512_Update], [AC_DEFINE([HAVE_SHA512_UPDATE], 1, [Define to 1 if you have the `SHA512_Update' function.])])],
[
	org_have_openssl=yes
	have_openssl=$org_have_openssl
	AC_ARG_WITH([openssl],
		[AS_HELP_STRING([--with-openssl],
			[include OpenSSL support (default=yes)])],
		[have_openssl=$withval])
	AS_VAR_IF([have_openssl], [no], [], [
		PKG_CHECK_MODULES([openssl], [openssl],
				[have_openssl=yes],
				[have_openssl=no; why_have_openssl="(OpenSSL library not found)"])
		AS_CASE([$enable_monetdb5-$have_openssl],
		[yes-no], [
			AC_MSG_ERROR([OpenSSL library not found but required for MonetDB5])],
		[auto-no], [
			enable_monetdb5=no
			AS_VAR_IF([disable_monetdb5], [], [
				disable_monetdb5="(OpenSSL library not found but required for MonetDB5)"])],
		[*-yes], [
			save_LIBS="$LIBS"
			LIBS="$LIBS $openssl_LIBS"
			AC_CHECK_FUNCS([MD5_Update RIPEMD160_Update SHA1_Update SHA224_Update SHA256_Update SHA384_Update SHA512_Update])
			LIBS="$save_LIBS"
			AS_IF([eval test \"x\$ac_cv_func_${MONETDB5_PASSWDHASH}_Update\" != x"yes"], [
				AS_CASE([$enable_monetdb5],
				[yes], [
					AC_MSG_ERROR([OpenSSL library found but checksum algorithm required for MonetDB5 not found])],
				[auto], [
					enable_monetdb5=no
					AS_VAR_IF([disable_monetdb5], [], [
						disable_monetdb5="(OpenSSL library found but checksum algorithm required for MonetDB5 not found)"])],
				[
					AC_MSG_WARN([OpenSSL library found but required checksum algorithm not found, so disabling])])
				have_openssl=no
				why_have_openssl="(OpenSSL library found but required checksum algorithm not found)"])])
		AS_VAR_IF([have_openssl], [yes], [
			AC_DEFINE([HAVE_OPENSSL], 1, [Define if you have the OpenSSL library])])])])


# PCRE library
# required for MonetDB5 (and SQL), optional otherwise
req_pcre_ver='4.5'
PKG_CHECK_MODULES([pcre], [libpcre >= $req_pcre_ver],
	[have_pcre=yes], [have_pcre=no; why_have_pcre="(libpcre >= $req_pcre_ver not found)"])
AS_VAR_IF([have_pcre], [yes], [
	AC_MSG_CHECKING([whether pcre comes with UTF-8 support])
	save_LIBS="$LIBS"
	LIBS="$LIBS $pcre_LIBS"
	save_CFLAGS="$CFLAGS"
	CFLAGS="$CFLAGS $pcre_CFLAGS"
	AC_RUN_IFELSE([AC_LANG_PROGRAM(
		[[@%:@include <pcre.h>]],
		[[int rc; pcre_config(PCRE_CONFIG_UTF8, &rc); exit(!rc);]])],
		[AC_MSG_RESULT([yes])],
		[have_pcre=no; why_have_pcre="(libpcre found but no UTF-8 support)"; AC_MSG_RESULT([no])],
		[AC_MSG_RESULT([assuming yes])])
	LIBS="$save_LIBS"
	CFLAGS="$save_CFLAGS"])
AS_VAR_IF([have_pcre], [yes], [
	AC_DEFINE([HAVE_LIBPCRE], 1, [Define if you have the pcre library])])
AM_CONDITIONAL([HAVE_PCRE], [test x"$have_pcre" != xno])
AS_CASE([$enable_monetdb5-$have_pcre],
[yes-no], [
	AC_MSG_ERROR([PCRE library not found but required for MonetDB5])],
[auto-no], [
	enable_monetdb5=no
	AS_VAR_IF([disable_monetdb5], [], [
		disable_monetdb5="(PCRE library not found but required for MonetDB5)"])])

org_have_libxml2=yes
have_libxml2=$org_have_libxml2
AC_ARG_WITH([libxml2],
	[AS_HELP_STRING([--with-libxml2],
		[include libxml2 support (default=yes)])],
	[have_libxml2=$withval])
AS_VAR_IF([have_libxml2], [no], [], [
	PKG_CHECK_MODULES([libxml2], [libxml-2.0], [have_libxml2="yes"], [have_libxml2="no"; why_have_libxml2="(libxml2 not found)"])
	AS_CASE([$enable_monetdb5-$have_libxml2],
	[yes-no], [
		AC_MSG_ERROR([libxml2 library not found but required for MonetDB5])],
	[auto-no], [
		enable_monetdb5=no
		AS_VAR_IF([disable_monetdb5], [], [
			disable_monetdb5="(libxml2 library not found but required for MonetDB5)"])])
	AS_VAR_IF([have_libxml2], [yes], [
		AC_DEFINE([HAVE_LIBXML], 1, [Define if you have the xml library])])])
AM_CONDITIONAL([HAVE_LIBXML], [test x"$have_liblxml2" != xno])


org_have_curl=yes
have_curl=$org_have_curl
AC_ARG_WITH([curl],
	[AS_HELP_STRING([--with-curl],
		[include cURL support (default=yes)])],
	[have_curl=$withval])
AS_VAR_IF([have_curl], [no], [], [
	PKG_CHECK_MODULES([curl], [libcurl], [have_curl="yes"], [have_curl="no"; why_have_curl="(libcurl not found)"])
	AS_VAR_IF([have_curl], [yes], [
		AC_DEFINE([HAVE_CURL], 1, [Define if you have the cURL library])
		AC_SUBST([PKG_CURL], [libcurl])])])
AM_CONDITIONAL([HAVE_CURL], [test x"$have_curl" != xno])


PKG_CHECK_MODULES([zlib], [zlib], [have_zlib="yes"], [have_zlib="no"; why_have_zlib="(zlib not found)"])
AS_CASE([$have_zlib],
	[no],
		[# Solaris doesn't provide zlib.pc, but zlib may well be present
		 AC_CHECK_LIB([z], [gzopen], [have_zlib=yes zlib_LIBS="-lz" zlib_CFLAGS= ])],
	[yes],
		[AC_SUBST([PKG_ZLIB], [zlib])])
AS_VAR_IF([have_zlib], [yes], [
	AC_DEFINE([HAVE_LIBZ], 1, [Define if you have the z library])])
AM_CONDITIONAL([HAVE_LIBZ], [test x"$have_zlib" != xno])

org_have_lzma=yes
have_lzma=$org_have_lzma
AC_ARG_WITH([lzma],
	[AS_HELP_STRING([--with-lzma],
		[include lzma support (default=yes)])],
	[have_lzma=$withval])
AS_VAR_IF([have_lzma], [no], [], [
	PKG_CHECK_MODULES([liblzma], [liblzma], [have_liblzma="yes"], [have_liblzma="no"; why_have_liblzma="(liblzma not found, install xz-libs and xz-devel)"])
	AS_VAR_IF([have_liblzma], [yes], [
		AC_SUBST([PKG_LIBLZMA], [liblzma])])
	AS_VAR_IF([have_liblzma], [yes], [
		AC_DEFINE([HAVE_LIBLZMA], 1, [Define if you have the lzma library])])])
AM_CONDITIONAL([HAVE_LIBLZMA], [test x"$have_liblzma" != xno])

org_have_valgrind=no
have_valgrind=$org_have_valgrind
AC_ARG_WITH([valgrind],
	[AS_HELP_STRING([--with-valgrind],
		[include valgrind support (default=no)])],
	[have_valgrind=$withval])
AS_VAR_IF([have_valgrind], [no], [], [
	PKG_CHECK_MODULES([valgrind], [valgrind],
		[AC_DEFINE([HAVE_VALGRIND], 1, [Define if you have valgrind installed and want to use it])],
		[AS_VAR_IF([have_valgrind], [yes], [AC_MSG_ERROR([no valgrind support found])])])])

# check for sphinxclient
org_have_sphinxclient="auto"
have_sphinxclient=$org_have_sphinxclient
AC_ARG_WITH([sphinxclient],
    [AS_HELP_STRING([--with-sphinxclient=DIR], [sphinxclient library is installed in DIR])],
    [have_sphinxclient="$withval"])
AS_VAR_IF([have_sphinxclient], [no], [], [
	AS_CASE([$have_sphinxclient],
	[auto|yes], [],
	[
	    SPHINXCLIENT_CFLAGS="-I$have_sphinxclient/include"
	    SPHINXCLIENT_LIBS="-L$have_sphinxclient/lib"])

	save_CPPFLAGS="$CPPFLAGS"
	save_LDFLAGS="$LDFLAGS"
	CPPFLAGS="$CPPFLAGS $SPHINXCLIENT_CFLAGS"
	LDFLAGS="$LDFLAGS $SPHINXCLIENT_LIBS"
	AC_CHECK_HEADER([sphinxclient.h],
		AC_CHECK_LIB([sphinxclient], [sphinx_create],
			[AC_DEFINE([HAVE_SPHINXCLIENT], 1, [Define if you have the sphinxclient library])]
			[have_sphinxclient=yes]
			[SPHINXCLIENT_LIBS="$SPHINXCLIENT_LIBS -lsphinxclient"],
			[AS_VAR_IF([have_sphinxclient], [auto], [], [AC_MSG_ERROR([-lsphinxclient library not found])])
			  have_sphinxclient=no; why_have_sphinxclient="(sphinxclient library not found)" ]),
		[AS_VAR_IF([have_sphinxclient], [auto], [], [AC_MSG_ERROR([sphinxclient.h header not found])])
		  have_sphinxclient=no; why_have_sphinxclient="(sphinxclient.h header not found)" ])
	LDFLAGS="$save_LDFLAGS"
	CPPFLAGS="$save_CPPFLAGS"])
AC_SUBST([SPHINXCLIENT_CFLAGS], [$SPHINXCLIENT_CFLAGS])
AC_SUBST([SPHINXCLIENT_LIBS], [$SPHINXCLIENT_LIBS])
AM_CONDITIONAL([HAVE_SPHINXCLIENT], [test x"$have_sphinxclient" != xno])

# R API (R UDFs)
have_libr=no
AS_VAR_IF([enable_rintegration], [no], [], [
	AS_CASE([$enable_rintegration],
		[yes|auto], [XPATH="$PATH"],
		[/*],  [XPATH="$enable_rintegration"
			enable_rintegration=yes],
		[AC_MSG_ERROR([--enable-rintegration value must be yes|no|auto|absolute path of R cmd])])
   	AC_PATH_PROG([RCMD], [R], [], [$XPATH])
   	AS_VAR_IF([RCMD], [],
		[AS_VAR_IF([enable_rintegration], [yes],
			[AC_MSG_ERROR([libR library required for R integration support])],
			[have_libr="no"
			 why_have_libr="(libR library not found)"
			 enable_rintegration=no
			 disable_rintegration="(libR library not found)"])],
		[AS_IF([RHOME=`$RCMD RHOME` && libr_CFLAGS=`$RCMD CMD config --cppflags` && libr_LIBS=`$RCMD CMD config --ldflags`],
			[have_libr=yes
			 AC_DEFINE([HAVE_LIBR], 1, [Define if you have libR installed])
			 AC_SUBST([libr_CFLAGS], [$libr_CFLAGS])
			 AC_SUBST([libr_LIBS], [$libr_LIBS])
			 AC_SUBST([RHOME], [$RHOME])
			 AC_DEFINE_UNQUOTED([RHOME], ["$RHOME"], [The home of R])])])])
AM_CONDITIONAL([HAVE_LIBR], [test x"$have_libr" != xno])

# Python API (Python UDFs)
have_libpy=no
AS_VAR_IF([enable_pyintegration], [no], [], [
	AS_CASE([$enable_pyintegration],
		[yes|auto], [XPATH="$PATH"],
		[/*], [XPATH="$enable_pyintegration"
		       enable_pyintegration=yes],
		[AC_MSG_ERROR([--enable-pyintegration value must be yes|no|auto|absolute path of python-config])])
	AS_VAR_IF([have_pyconfig], [], [
	   	AC_PATH_PROG([PYCMD], [python2-config], [no], [$PATH])
	   	AS_VAR_IF([PYCMD], [no], [
		   	AC_PATH_PROG([PYCMD2], [python-config], [no], [$PATH])
		   	AS_VAR_IF([PYCMD2], [no], [
		   		AS_VAR_IF([enable_pyintegration], [yes],
				[AC_MSG_ERROR([python-config library required for Python integration support])],
				[have_libpy="no"
			 	why_have_libpy="(python-config command not found)"
				enable_pyintegration="no"
				disable_pyintegration="(python-config command not found)"
				])
			], [PYCMD="$PYCMD2"])
	   	], [])
	], [PYCMD="$have_pyconfig"])
	AS_VAR_IF([have_pyversion], [],
		[PYTHON_CMD=$PYTHON2],
		[PYTHON_CMD=$have_pyversion])
	NUMPYVER=`$PYTHON_CMD -c "import numpy; print(int(numpy.__version__.split('.').__getitem__(1)) >= 7)"`
	# check numpyconfig.h because autoconf tests includes by
	# compiling a small C program, and other numpy headers do
	# not compile without Python.h
	AC_CHECK_HEADER([numpy/numpyconfig.h],
		[NUMPYHEADERS=True],
		[NUMPYHEADERS=`$PYTHON_CMD -c "import numpy, os; print(os.path.isfile(os.path.join(numpy.get_include(), 'numpy/arrayobject.h')))"`])
	AS_IF([test "x$NUMPYVER" = x || test "x$NUMPYVER" = xFalse],
		[AS_VAR_IF([enable_pyintegration], [yes],
			[AC_MSG_ERROR([numpy version >= 1.7.0 required for Python integration support])],
			[have_libpy="no"
			 why_have_libpy="(numpy version >= 1.7.0 not found)"
			 enable_pyintegration="no"
			 disable_pyintegration="(numpy version >= 1.7.0 not found)"])],
		[test "x$NUMPYHEADERS" = xFalse ],
		[AS_VAR_IF([enable_pyintegration], [yes],
			[AC_MSG_ERROR([numpy/arrayobject.h not found])],
			[have_libpy="no"
			 why_have_libpy="(numpy/arrayobject.h not found)"
			 enable_pyintegration="no"
			 disable_pyintegration="(numpy/arrayobject.h not found)"])],
		[libpy_CFLAGS=`$PYTHON_CMD -c "from distutils.sysconfig import get_python_inc; import numpy; print(' -I' + get_python_inc() + ' -I' + numpy.get_include());"`
		 libpy_LIBS=`$PYCMD --ldflags`
		 HAVEPYTHONHEADER=`$PYTHON_CMD -c "import distutils.sysconfig, os; print(os.path.isfile(os.path.join(distutils.sysconfig.get_python_inc(), 'Python.h')))"`
		 AS_VAR_IF([HAVEPYTHONHEADER], [True],
		 	[have_libpy=yes
			 AC_DEFINE([HAVE_LIBPY], 1, [Define if we can link to python])
			 AC_SUBST([libpy_CFLAGS], [$libpy_CFLAGS])
			 AC_SUBST([libpy_LIBS], [$libpy_LIBS])],
			[AS_VAR_IF([enable_pyintegration], [yes],
				[AC_MSG_ERROR([Python.h not found])],
				[have_libpy="no"
			 	 why_have_libpy="Python.h not found"
				 enable_pyintegration="no"
				 disable_pyintegration="Python.h not found"])])])])
AM_CONDITIONAL([HAVE_LIBPY], [test x"$have_libpy" != xno])

AC_CHECK_HEADER([sys/sem.h], [SEMOP=True], [SEMOP=False])
AS_VAR_IF([SEMOP], [True],
	[AC_CHECK_FUNC([semtimedop],
		[AC_DEFINE([HAVE_SEMTIMEDOP], 1, [Define if semtimedop exists])],)
	 AC_CHECK_FUNC([fork],
	 	[AC_DEFINE([HAVE_FORK], 1, [Define if fork exists])])])

# ODBC, only used by ODBC driver
AS_VAR_IF([enable_odbc], [no],
	[have_unixodbc=no
	 why_have_unixodbc="(ODBC disabled)"],
	[have_unixodbc=auto
	 ODBC_INCS=''
	 ODBCINST_LIBS=''
	 ODBC_LIBS=''
	 AC_ARG_WITH([unixodbc],
		[AS_HELP_STRING([--with-unixodbc=DIR],
			[unixODBC library is installed in DIR])],
		[have_unixodbc="$withval"])
	 AS_VAR_IF([have_unixodbc], [no], [], [
		AS_CASE([$have_unixodbc],
		[yes|auto], [],
		[
			ODBC_INCS="-I$have_unixodbc/include"
			ODBCINST_LIBS="-L$have_unixodbc/lib"
			ODBC_LIBS="-L$have_unixodbc/lib"])
		save_CPPFLAGS="$CPPFLAGS"
		CPPFLAGS="$CPPFLAGS $ODBC_INCS"
		AC_CHECK_HEADER([odbcinst.h],
			[AC_DEFINE([HAVE_ODBCINST_H], 1, [Define to 1 if you have the <odbcinst.h> header file.])],
			[AS_VAR_IF([have_unixodbc], [auto], [], [AC_MSG_ERROR([odbcinst.h not found])])
			 have_unixodbc=no; why_have_unixodbc="(odbcinst.h not found)"])
		CPPFLAGS="$save_CPPFLAGS"])
	 AS_VAR_IF([have_unixodbc], [no], [], [
		save_LIBS="$LIBS"
		LIBS="$LIBS $ODBCINST_LIBS"
		AC_CHECK_LIB([odbcinst],
			[SQLGetPrivateProfileString], [:],
			[AS_VAR_IF([have_unixodbc], [auto], [], [AC_MSG_ERROR([-lodbcinst not found])])
			 have_unixodbc=no; why_have_unixodbc="(odbcinst library not found)"])
		LIBS="$save_LIBS"])
	 AS_VAR_IF([have_unixodbc], [no], [], [
		save_LIBS="$LIBS"
		LIBS="$LIBS $ODBC_LIBS"
		AC_CHECK_LIB([odbc],
			[SQLGetDiagRec], [:],
			[AS_VAR_IF([have_unixodbc], [auto], [], [AC_MSG_ERROR([-lodbc not found])])
			 have_unixodbc=no; why_have_unixodbc="(odbc library not found)"])
		LIBS="$save_LIBS"])
	 AS_VAR_IF([have_unixodbc], [no], [], [
		ODBCINST_LIBS="$ODBCINST_LIBS -lodbcinst"
		ODBC_LIBS="$ODBC_LIBS -lodbc"
		AC_DEFINE([HAVE_SQLGETPRIVATEPROFILESTRING], 1,
			[Define if you have the SQLGetPrivateProfileString function])])
	 AS_VAR_IF([have_unixodbc], [no], [], [
		AC_MSG_CHECKING([checking whether SQLColAttribute uses SQLLEN * or SQLPOINTER])
		save_CPPFLAGS="$CPPFLAGS"
		CPPFLAGS="$CPPFLAGS $ODBC_INCS"
		AC_COMPILE_IFELSE([
			AC_LANG_PROGRAM([[
				@%:@include <sql.h>
				SQLRETURN SQL_API
				SQLColAttribute(SQLHSTMT hStmt, SQLUSMALLINT nCol, SQLUSMALLINT nFieldIdentifier, SQLPOINTER pszValue, SQLSMALLINT nValueLengthMax, SQLSMALLINT *pnValueLength, SQLLEN *pnValue) {
					(void) hStmt;
					(void) nCol;
					(void) nFieldIdentifier;
					(void) pszValue;
					(void) nValueLengthMax;
					(void) pnValueLength;
					(void) pnValue;
					return SQL_SUCCESS;}]],
				[[SQLColAttribute(0,0,0,0,0,0,0);]])],
			[lenp_or_pointer_t='SQLLEN *'],
			[lenp_or_pointer_t=SQLPOINTER])
		CPPFLAGS="$save_CPPFLAGS"
		AC_MSG_RESULT([using "$lenp_or_pointer_t"])
		AC_DEFINE_UNQUOTED([LENP_OR_POINTER_T], [$lenp_or_pointer_t],
			[Define as SQLLEN * or SQLPOINTER depending on the include file])])
	 AC_SUBST([ODBC_INCS])
	 AC_SUBST([ODBCINST_LIBS])
	 AC_SUBST([ODBC_LIBS])
	 AS_CASE([x${enable_odbc}x${have_unixodbc}],
		[xautoxno], [
			enable_odbc=no
			disable_odbc="(unixODBC library required for ODBC driver)"],
		[xyesxno], [
			AC_MSG_ERROR([unixODBC required for building ODBC driver])])])
AM_CONDITIONAL([HAVE_ODBC], [test x$have_unixodbc != xno])

# proj only used in geom module
org_have_proj="auto"
have_proj=$org_have_proj
PROJ_INCS=''
PROJ_LIBS=''
AC_ARG_WITH([proj],
	[AS_HELP_STRING([--with-proj=DIR],
		[proj library is installed in DIR])],
	[have_proj="$withval"])
AS_VAR_IF([have_proj], [no], [], [
	AS_CASE([$have_proj],
	[auto|yes], [],
	[
		PROJ_INCS="-I$have_proj/include"
		PROJ_LIBS="-L$have_proj/lib -lproj"])
	save_CPPFLAGS="$CPPFLAGS"
	save_LDFLAGS="$LDFLAGS"
	CPPFLAGS="$CPPFLAGS $PROJ_INCS"
	LDFLAGS="$LDFLAGS $PROJ_LIBS"
	AC_CHECK_HEADER([proj_api.h],
		AC_CHECK_LIB([proj], [pj_init_plus],
			[AC_DEFINE([HAVE_PROJ], 1, [Define if the proj library exists])],
			[AS_VAR_IF([have_proj], [auto], [], [AC_MSG_ERROR([-lproj library not found])])
			have_proj=no; why_have_proj="(proj library not found)" ], ),
		[AS_VAR_IF([have_proj], [auto], [], [AC_MSG_ERROR([proj_api.h header not found])])
		have_proj=no; why_have_proj="(proj_api.h header not found)" ])
	LDFLAGS="$save_LDFLAGS"
	CPPFLAGS="$save_CPPFLAGS"
	AS_IF([test "x$have_proj" = "xyes" -o "x$have_proj" = "xauto"], [
	    PROJ_INCS=
	    PROJ_LIBS="-lproj"])])
AC_SUBST([PROJ_INCS])
AC_SUBST([PROJ_LIBS])
AM_CONDITIONAL([HAVE_PROJ], [test x"$have_proj" != xno])


# samtools, only used by sql
org_have_samtools="auto"
have_samtools=$org_have_samtools
AC_ARG_WITH([samtools],
	[AS_HELP_STRING([--with-samtools=DIR],
		[samtools (providing bam library) are installed in DIR])],
	[have_samtools="$withval"])
AS_VAR_IF([have_samtools], [no], [], [
	AS_CASE([$have_samtools],
	[auto|yes], [],
	[
		SAMTOOLS_CFLAGS="-I$have_samtools/include"
		SAMTOOLS_LIBS="-L$have_samtools/lib -lbam"])

	save_CPPFLAGS="$CPPFLAGS"
	save_LDFLAGS="$LDFLAGS"
	CPPFLAGS="$CPPFLAGS $SAMTOOLS_CFLAGS"
	LDFLAGS="$LDFLAGS $SAMTOOLS_LIBS"

	AC_CHECK_HEADER([samtools/bam.h],
		[why_have_samtools=; AC_DEFINE([HAVE_SAMTOOLS_BAM_H], 1, [Define to 1 if you have the <samtools/bam.h> header file.]) ],
		[why_have_samtools="bam.h header not found" ])
	AS_VAR_IF([why_have_samtools], [],
		[],
		[AC_CHECK_HEADER([bam/bam.h],
			[why_have_samtools=; AC_DEFINE([HAVE_BAM_BAM_H], 1, [Define to 1 if you have the <bam/bam.h> header file.]) ],
			[why_have_samtools="Neither samtools/bam.h nor bam/bam.h header found" ])])

	AS_VAR_IF([why_have_samtools], [],
		[AC_CHECK_HEADER([samtools/sam.h],
			[why_have_samtools=; AC_DEFINE([HAVE_SAMTOOLS_SAM_H], 1, [Define to 1 if you have the <samtools/sam.h> header file.]) ],
			[why_have_samtools="samtools/sam.h header not found" ])])
	AS_VAR_IF([why_have_samtools], [],
		[],
		[AC_CHECK_HEADER([bam/sam.h],
			[why_have_samtools=; AC_DEFINE([HAVE_BAM_SAM_H], 1, [Define to 1 if you have the <bam/sam.h> header file.]) ],
			[why_have_samtools="Neither samtools/sam.h nor bam/sam.h header found" ])])

	AS_VAR_IF([why_have_samtools], [],
		[AC_CHECK_HEADER([samtools/bgzf.h],
			[why_have_samtools=; AC_DEFINE([HAVE_SAMTOOLS_BGZF_H], 1, [Define to 1 if you have the <samtools/bgzf.h> header file.]) ],
			[why_have_samtools="samtools/bgzf.h header not found" ])])
	AS_VAR_IF([why_have_samtools], [],
		[],
		[AC_CHECK_HEADER([bam/bgzf.h],
			[why_have_samtools=; AC_DEFINE([HAVE_BAM_BGZF_H], 1, [Define to 1 if you have the <bam/bgzf.h> header file.]) ],
			[why_have_samtools="Neither samtool/bgzf.h nor bam/bgzf.h header found" ])])

	AS_VAR_IF([why_have_samtools], [],
		[AC_CHECK_LIB([bam], [bam_header_read],
			[AC_DEFINE([HAVE_SAMTOOLS], 1, [Define if you have the samtools version 0.1.x (providing the bam library)])],
			[why_have_samtools="bam library not found" ],
			[-lm -lpthread -lz])])

	AS_VAR_IF([why_have_samtools], [],
		[],
		[AS_VAR_IF([have_samtools], [auto],
			[],
			[AC_MSG_ERROR([$why_have_samtools])])
		 have_samtools=no
		 why_have_samtools="($why_have_samtools)"])
	LDFLAGS="$save_LDFLAGS"
	CPPFLAGS="$save_CPPFLAGS"
	AS_IF([test "x$have_samtools" = "xyes" -o "x$have_samtools" = "xauto"], [
		SAMTOOLS_CFLAGS=
		SAMTOOLS_LIBS="-lbam"
		AC_CHECK_HEADERS([samtools/kstring.h bam/kstring.h])])])
AC_SUBST([SAMTOOLS_CFLAGS], [$SAMTOOLS_CFLAGS])
AC_SUBST([SAMTOOLS_LIBS], [$SAMTOOLS_LIBS])
AM_CONDITIONAL([HAVE_SAMTOOLS], [test x"$have_samtools" != xno])

# geos, only used in geom module
org_have_geos=auto
have_geos=$org_have_geos
GEOS_INCS=''
GEOS_LIBS=''
AC_ARG_WITH([geos],
	[AS_HELP_STRING([--with-geos=DIR],
		[geos library is installed in DIR])],
	[have_geos="$withval"])
AS_CASE([$enable_geom],
[yes|auto], [
	# geos >= 3.4.0 required for (a.o.) GEOSDelaunayTriangulation()
	GEOS_VERSION_REQUIRED_MIN=3.4.0
	GEOS_CONFIG=''
	AS_VAR_IF([have_geos], [no], [], [
		AS_CASE([$have_geos],
		[yes|auto], [
			XPATH="$PATH"],
		[
			XPATH="$have_geos/bin"
			have_geos=yes])
		AC_PATH_PROG([GEOS_CONFIG], [geos-config], [], [$XPATH])
		AS_VAR_IF([GEOS_CONFIG], [], [
			AS_VAR_IF([have_geos], [yes], [
				AC_MSG_ERROR([geos-config not found in $XPATH])], [
				have_geos=no
				why_have_geos="(geos-config not found)"])])])
	AS_VAR_IF([have_geos], [no], [], [
		AC_MSG_CHECKING([for geos >= $GEOS_VERSION_REQUIRED_MIN])
		GEOS_VERSION=`$GEOS_CONFIG --version`
		AC_MSG_RESULT([found $GEOS_VERSION])
		AS_VERSION_COMPARE([$GEOS_VERSION], [$GEOS_VERSION_REQUIRED_MIN],
			[AS_VAR_IF([have_geos], [yes],
				[AC_MSG_ERROR([geos version $GEOS_VERSION is too old])],
				[have_geos=no
				why_have_geos="(geos version $GEOS_VERSION is older than required version $GEOS_VERSION_REQUIRED_MIN)"])])])
	AS_VAR_IF([have_geos], [no], [], [
		GEOS_INCS=`$GEOS_CONFIG --cflags`
		AS_IF([GEOS_LIBS=`$GEOS_CONFIG --clibs 2> /dev/null`],
			[],
			[GEOS_LIBS=`$GEOS_CONFIG --libs | sed 's:-lgeos:-lgeos_c:'`])])
	AS_VAR_IF([have_geos], [no], [
		AS_VAR_IF([enable_geom], [yes], [
			AC_MSG_ERROR([geos library required for geom module])])
		# no geos, so don't compile geom
		enable_geom=no
		AS_VAR_IF([disable_geom], [], [
			disable_geom=${why_have_geos:-"(geos library required for geom module)"}])])])
AC_SUBST([GEOS_INCS])
AC_SUBST([GEOS_LIBS])

# fits, only used by sql
org_have_fits=no
have_fits=$org_have_fits
AS_VAR_IF([enable_fits], [no], [], [
	PKG_CHECK_MODULES([cfitsio], [cfitsio],
		[have_fits="yes"; AC_DEFINE([HAVE_FITS], 1, [Define if the fits module is to be enabled])],
		[have_fits="no"; why_have_fits="(cfitsio library not found)"
		 AS_VAR_IF([enable_fits], [yes], [AC_MSG_ERROR([cfitsio library required for FITS support])]);
		 enable_fits=no; disable_fits="(cfitsio library not found)"])])
AM_CONDITIONAL([HAVE_FITS], [test x"$have_fits" != xno])

# netcdf, only used by sql
org_have_netcdf=no
have_netcdf=$org_have_netcdf
AS_VAR_IF([enable_netcdf], [no], [], [
	req_netcdf='4.2'
	PKG_CHECK_MODULES([netcdf], [netcdf],
		[have_netcdf="yes"; AC_DEFINE([HAVE_NETCDF], 1, [Define if the netcdf module is to be enabled])],
		[have_netcdf="no"; why_have_netcdf="(netcdf library not found)"
		 AS_VAR_IF([enable_netcdf], [yes], [AC_MSG_ERROR([netcdf library required for NetCDF support])])
		 enable_netcdf=no; disable_netcdf="(netcdf library not found)"])])
AM_CONDITIONAL([HAVE_NETCDF], [test x"$have_netcdf" != xno])

# liblas, only used in lidar module
org_have_liblas=auto
have_liblas=$org_have_liblas
LIBLAS_INCS=''
LIBLAS_LIBS=''
AC_ARG_WITH([liblas],
	[AS_HELP_STRING([--with-liblas=DIR],
		[liblas library is installed in DIR])],
	[have_liblas="$withval"])
AS_CASE([$have_lidar],
[yes|auto], [
	# liblas >= 1.8.0 required
	LIBLAS_VERSION_REQUIRED_MIN=1.8.0
	LIBLAS_CONFIG=''
	# locate liblas-config
	AS_VAR_IF([have_liblas], [no], [], [
		AS_CASE([$have_liblas],
		[yes|auto], [XPATH="$PATH"],
		[
			XPATH="$have_liblas/bin"
			have_liblas=yes])
		AC_PATH_PROG([LIBLAS_CONFIG], [liblas-config], [], [$XPATH])
		AS_VAR_IF([LIBLAS_CONFIG], [],
			[AS_VAR_IF([have_liblas], [yes],
				[AC_MSG_ERROR([liblas-config not found in $XPATH])],
				[have_liblas=no
				 why_have_liblas="(liblas-config not found)"])])])
	# check the version if liblas
	AS_VAR_IF([have_liblas], [no], [], [
		AC_MSG_CHECKING([for liblas >= $LIBLAS_VERSION_REQUIRED_MIN])
		LIBLAS_VERSION=`$LIBLAS_CONFIG --version`
		AC_MSG_RESULT([found $LIBLAS_VERSION])
		AS_VERSION_COMPARE([$LIBLAS_VERSION], [$LIBLAS_VERSION_REQUIRED_MIN],
			[AS_VAR_IF([have_liblas], [yes],
				[AC_MSG_ERROR([liblas version $LIBLAS_VERSION is too old])],
				[have_liblas=no
				 why_have_liblas="(liblas version $LIBLAS_VERSION is older than required version $LIBLAS_VERSION_REQUIRED_MIN)"])])])
	# read include dirs, libs and cflags
	AS_VAR_IF([have_liblas], [no], [], [
		LIBLAS_INCS=`$LIBLAS_CONFIG --includes`
		LIBLAS_LIBS=`$LIBLAS_CONFIG --libs`])
	# if lidar is enabled and liblas was not found abort
	AS_VAR_IF([have_liblas], [no], [
		AS_VAR_IF([have_lidar], [yes],
			[AC_MSG_ERROR([liblas library required for lidar module])])
		# no liblas, so don't compile lidar
		have_lidar=no
		AS_VAR_IF([disable_lidar], [],
			[disable_lidar=${why_have_liblas:-"(liblas library required for lidar module)"}])])],
[no], [
	have_lidar=no
	have_liblas=no
	why_have_liblas="(lidar module is disabled)"])
AS_VAR_IF([have_lidar], [no],
	[LIBLAS_RPM=without],
	[LIBLAS_RPM=with])
# this is only used in rpm.mk.in to maybe avoid building MonetDB-lidar
AC_SUBST([LIBLAS_RPM])

AC_SUBST([LIBLAS_INCS])
AC_SUBST([LIBLAS_LIBS])
AC_MSG_CHECKING([enable lidar: $have_lidar])

AM_CONDITIONAL([HAVE_LIDAR], [test "x$have_lidar" = xyes -o "x$have_lidar" = xauto])

AC_MSG_CHECKING(['enable lidar: $have_lidar'])

# gdal
org_have_gdal="auto"
have_gdal=$org_have_gdal
AC_ARG_WITH([gdal],
	[AS_HELP_STRING([--with-gdal=DIR],
		[gdal library is installed in DIR])],
		[have_gdal="$withval"], [have_gdal="auto"])
# first autoconf for distributions that added a .pc file themselves (Fedora)
AS_VAR_IF([have_gdal], [no], [], [
	AS_CASE([$have_gdal],
	[auto|yes], [
		PKG_CHECK_MODULES([GDAL], [gdal], [have_gdal="pkgconf"], [have_gdal="$have_gdal"])],
	[
		GDAL_CFLAGS="-I$have_gdal/include"
		GDAL_LIBS="-L$have_gdal/lib"])])
# try to detect it manually like upstream provides it
AS_VAR_IF([have_gdal], [no],
	[],
	[AS_VAR_IF([have_gdal], [pkgconf],
		[],
		[save_CPPFLAGS="$CPPFLAGS"
		 save_LDFLAGS="$LDFLAGS"
		 save_LIBS="$LIBS"
		 CPPFLAGS="$CPPFLAGS $GDAL_CFLAGS"
		 LDFLAGS="$LDFLAGS $GDAL_LIBS"
		 LIBS=""
		 found_gdal_h=yes
		 AC_CHECK_HEADER([gdal.h], [],
			[AC_CHECK_HEADER([gdal/gdal.h],
				[AS_VAR_IF([GDAL_CFLAGS], [],
					[GDAL_CFLAGS="-I/usr/include/gdal"],
					[GDAL_CFLAGS="$GDAL_CFLAGS/gdal"])
				 CPPFLAGS="$save_CPPFLAGS $GDAL_CFLAGS"],
				[found_gdal_h=no])])
		 AS_VAR_IF([found_gdal_h], [yes],
		 	[AC_CHECK_LIB([gdal], [GDALAllRegister],
				[AC_DEFINE([HAVE_GDAL], 1, [Define if you have the gdal library])
					have_gdal=yes
					LIBS="$LIBS -lgdal"],
				[AS_VAR_IF([have_gdal], [auto],
					[],
					[AC_MSG_ERROR([-lgdal library not found])])
				 have_gdal=no
				 why_have_gdal="(gdal library not found)"],
				[$MATH_LIBS])],
			[AS_VAR_IF([have_gdal], [auto],
				[],
				[AC_MSG_ERROR([gdal.h header not found])])
			 have_gdal=no
			 why_have_gdal="(gdal.h header not found)"])
		 GDAL_LIBS="$GDAL_LIBS $LIBS"
		 LDFLAGS="$save_LDFLAGS"
		 CPPFLAGS="$save_CPPFLAGS"
		 LIBS="$save_LIBS"
		 AS_VAR_IF([have_gdal], [yes],
		 	[],
			[GDAL_CFLAGS=""
			 GDAL_LIBS=""])])])

AC_SUBST([gdal_CFLAGS], [$GDAL_CFLAGS])
AC_SUBST([gdal_LIBS], [$GDAL_LIBS])
AM_CONDITIONAL([HAVE_GDAL], [test x"$have_gdal" != xno])

# if shp is enabled and gdal was not found abort
AS_VAR_IF([have_gdal], [no], [
	AS_VAR_IF([have_shp], [yes], [
		AC_MSG_ERROR([gdal library required for ESRI Shapefile vault])])
	# no gdal, so don't compile shp
	have_shp=no
	AS_VAR_IF([disable_shp], [], [
		disable_shp=${why_have_shp:-"(gdal library required for ESRI Shapefile vault)"}])])
AM_CONDITIONAL([HAVE_SHP], [test x"$have_shp" != xno])

PKG_CHECK_MODULES([atomic_ops], [atomic_ops], [have_atomic_ops="yes"],
	[have_atomic_ops="no"; why_have_atomic_ops="(atomic_ops library not found)"])
AS_VAR_IF([have_atomic_ops], [yes], [
	AS_CASE([$GCC-$CC],
	[-*icc*], [
		# Even with libatomic_ops available, compilation with
		# atomic_ops enabled fails with Intel's icc (version 11.1);
		# hence, we refrain from from using atomic_ops with icc.
		have_atomic_ops="no"
		why_have_atomic_ops="(atomic_ops do not compile with Intel's icc)"],
	[
		# On some systems, notably OpenIndiana, atomic_ops may
		# seem to be installed in that there is a pkg-config
		# file for it, but that file contains incorrect
		# information, and can not, in fact, be used.  The
		# bogus pkg-config file for atomic_ops is part of the
		# library/gc package.
		save_CFLAGS="$CFLAGS"
		CFLAGS="$CFLAGS $atomic_ops_CFLAGS"
		AC_CHECK_HEADERS([atomic_ops.h], [have_atomic_ops=yes], [have_atomic_ops=no why_have_atomic_ops="(atomic_ops was incorrectly installed (happens on at least Solaris/OpenIndiana))"])
		CFAGS="$save_CFLAGS"
		AS_VAR_IF([have_atomic_ops], [yes], [
			AC_DEFINE([HAVE_LIBATOMIC_OPS], 1, [Define if you have the libatomic_ops library])
			CFLAGS="$CFLAGS $atomic_ops_CFLAGS"
			LIBS="$LIBS $atomic_ops_LIBS"])])])

# Check for gsl library for statistical functions
org_have_gsl=no
have_gsl=$org_have_gsl
AS_VAR_IF([enable_gsl], [no], [], [
	PKG_CHECK_MODULES([gsl], [gsl],
		[have_gsl=yes],
		[have_gsl=no; why_have_gsl="(gsl library not found)"
		 AS_VAR_IF([enable_gsl], [yes], [AC_MSG_ERROR([gsl library required for GSL support])])
		 enable_gsl=no; disable_gsl="(gsl library not found)"])])
AM_CONDITIONAL([HAVE_GSL], [test x"$have_gsl" != xno])

#    checks for header files
AC_HEADER_STDBOOL
AC_HEADER_STDC
AC_HEADER_TIME
AC_HEADER_DIRENT
# NOTE: these header files are in alphabetical order to ease maintenance
AC_CHECK_HEADERS([ \
	dlfcn.h \
	execinfo.h \
	fcntl.h \
	fenv.h \
	getopt.h \
	iconv.h \
	ieeefp.h \
	io.h \
	kvm.h \
	langinfo.h \
	libgen.h \
	libintl.h \
	limits.h \
	locale.h \
	mach/mach_init.h \
	mach/task.h \
	mach-o/dyld.h \
	malloc.h \
	netdb.h \
	netinet/in.h \
	procfs.h \
	pwd.h \
	signal.h \
	stdint.h \
	string.h \
	strings.h \
	stropts.h \
	sys/file.h \
	sys/ioctl.h \
	sys/mman.h \
	sys/param.h \
	sys/resource.h \
	sys/socket.h \
	sys/sysctl.h \
	sys/time.h \
	sys/times.h \
	sys/types.h \
	sys/uio.h \
	sys/un.h \
	sys/utime.h \
	sys/wait.h \
	termios.h \
	time.h \
	unistd.h \
	uuid/uuid.h \
	winsock.h \
	])

# on OpenBSD, sys/sysctl.h requires sys/param.h to be included beforehand
AC_CHECK_HEADERS([sys/sysctl.h], [], [],
[
  @%:@ifdef HAVE_SYS_PARAM_H
  @%:@ include <sys/param.h>
  @%:@endif
])


SOCKET_LIBS=""
org_have_setsockopt=no
have_setsockopt=$org_have_setsockopt

AC_CHECK_FUNC([gethostbyname_r], [], [
  AC_CHECK_LIB([nsl_r], [gethostbyname_r], [SOCKET_LIBS="-lnsl_r" ],
	AC_CHECK_LIB([nsl], [gethostbyname_r], [SOCKET_LIBS="-lnsl"   ] ))])
AC_CHECK_FUNC([hstrerror], [], [
  AC_CHECK_LIB([resolv], [hstrerror], [SOCKET_LIBS="$SOCKET_LIBS -lresolv" ])])

AS_VAR_IF([have_setsockopt], [no], [
	AC_CHECK_FUNC([setsockopt], [],
	  AC_CHECK_LIB([socket], [setsockopt], [SOCKET_LIBS="-lsocket $SOCKET_LIBS"; have_setsockopt=yes; ]))])

org_have_getaddrinfo=no
have_getaddrinfo=$org_have_getaddrinfo
save_LIBS="$LIBS"
LIBS="$LIBS $SOCKET_LIBS"

AC_CHECK_FUNC([getaddrinfo], [have_getaddrinfo=yes ], [
  AC_CHECK_LIB([socket], [getaddrinfo], [SOCKET_LIBS="$SOCKET_LIBS -lsocket"; have_getaddrinfo=yes ],
	AC_CHECK_LIB([nsl],  [getaddrinfo], [SOCKET_LIBS="$SOCKET_LIBS -lnsl"   ; have_getaddrinfo=yes ] ))])
LIBS="$save_LIBS"

AS_VAR_IF([have_getaddrinfo], [yes], [
	AC_DEFINE([HAVE_GETADDRINFO], 1, [Define to 1 if you have the `getaddrinfo' function.])])

# On Windows we need to use try_link because Windows uses the
# Pascal style of function calls and naming scheme. Therefore the
# function needs to be compiled with the correct header
AC_CHECK_TYPE([SOCKET], [], [AC_DEFINE([SOCKET], [int], [type used for sockets])], [[@%:@ifdef HAVE_WINSOCK_H
@%:@include <winsock.h>
@%:@endif]])
AC_CHECK_TYPE([socklen_t],
	[AC_DEFINE([HAVE_SOCKLEN_T], 1, [Define to 1 if the system has the type `socklen_t'.])],
	[AC_DEFINE([socklen_t], [int], [type used by connect])],
	[[@%:@include <sys/types.h>
@%:@include <sys/socket.h>]])

# don't check for closesocket on Cygwin: it'll be found but we don't want to use it
AC_DEFINE([closesocket], [close], [function to close a socket])

AC_SUBST([SOCKET_LIBS])

#    checks for types
AC_TYPE_SIZE_T
AC_TYPE_SSIZE_T
AC_TYPE_PID_T
AC_TYPE_OFF_T

AC_CHECK_TYPES([__int64, long long])

AS_CASE([$enable_int128],
[yes|auto], [
	have_hge=no
	why_have_hge='(no 128-bit integer type found)'
	AC_CHECK_TYPES([__int128, __int128_t, __uint128_t],
		[have_hge=yes; why_have_hge=''])
	AS_VAR_IF([have_hge], [no],
		[AS_VAR_IF([enable_int128], [yes], [
			AC_MSG_ERROR([128-bit integers not supported by platform])])])],
[no], [
	have_hge=no
	why_have_hge='(128-bit integer support disabled)'])
AM_CONDITIONAL([HAVE_HGE], [test "x$have_hge" = xyes])

AC_CHECK_TYPES([ptrdiff_t],,,[@%:@include <stddef.h>])
AC_CHECK_TYPES([intptr_t])
AC_CHECK_TYPES([uintptr_t])

AC_CHECK_SIZEOF([char])
AC_CHECK_SIZEOF([short])
AC_CHECK_SIZEOF([int])
AC_CHECK_SIZEOF([long])
AC_CHECK_SIZEOF([void *])
AC_CHECK_SIZEOF([size_t])
AC_CHECK_SIZEOF([ssize_t])
AC_CHECK_SIZEOF([ptrdiff_t],,[@%:@include <stddef.h>])
AC_CHECK_SIZEOF([wchar_t],,[@%:@include <stddef.h>])
AC_CHECK_SIZEOF([long long])
AC_CHECK_SIZEOF([__int64])
AC_CHECK_SIZEOF([__int128])
AC_CHECK_SIZEOF([__int128_t])
AC_C_CHAR_UNSIGNED

AC_DEFINE([LLFMT], ["%lld"], [Format to print 64 bit signed integers.])
AC_DEFINE([ULLFMT], ["%llu"], [Format to print 64 bit unsigned integers.])

#    checks for structures
#    checks for compiler characteristics
AC_C_CONST
AC_C_INLINE
AC_C_RESTRICT
AC_C_VOLATILE
AC_C_FLEXIBLE_ARRAY_MEMBER

AC_MSG_CHECKING([if your compiler understands `__hidden'])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
	@%:@include <stdlib.h>

	__hidden void foo (const char *a, ...);

	void foo (const char *a, ...)
		{ (void)a; }
	]],
	[[ foo ("%s", ""); ]]
	)],
	[AC_MSG_RESULT([yes])],
	[AC_MSG_RESULT([no])
	AC_DEFINE([__hidden], [/* empty */],
		[Does your compiler support the `__hidden' specifier?])])

#    checks for library functions
AS_CASE([$host],
	[*-darwin1[[0123456]]*], [
		# OSX 10.6 (Snow Leopard) and up somehow makes
		# configure believe that fdatasync exists, in
		# reality however, it does not on this platform.
		ac_cv_func_fdatasync=no],
	[
		AC_CHECK_FUNCS([fdatasync])])
AS_VAR_IF([ac_cv_func_fdatasync], [yes],
	[AC_DEFINE([HAVE_FDATASYNC], 1, [If the system has a working fdatasync])])

AC_FUNC_FSEEKO
# NOTE: these functions are in alphabetical order to ease maintenance
save_LIBS="$LIBS"
LIBS="$LIBS $MATH_LIBS"
AC_CHECK_FUNCS([\
	asctime_r \
	backtrace \
	ctime_r \
	fabsf \
	fallocate \
	fcntl \
	fpclass \
	fpclassify \
	fsync \
	ftime \
	getexecname \
	getlogin \
	getopt \
	getopt_long \
	gettimeofday \
	getuid \
	isinf \
	llabs \
	localtime_r \
	lockf \
	madvise \
	mremap \
	nextafterf \
	nl_langinfo \
	_NSGetExecutablePath \
	popen \
	posix_fadvise \
	posix_fallocate \
	posix_madvise \
	putenv \
	round \
	setenv \
	setlocale \
	setsid \
	shutdown \
	sigaction \
	strcasestr \
	strftime \
	strncasecmp \
	strptime \
	strsignal \
	strtod \
	strtof \
	strtoll \
	strtoull \
	sysconf \
	task_info \
	times \
	trunc \
	uname \
	])
LIBS="$save_LIBS"

AC_MSG_CHECKING([__builtin_add_overflow])
# Test for __builtin_mul_overflow with __int128 arguments, since at
# the time of writing, there is a bug in Clang that "forgets" to link
# to the runtime that contains the required function __muloti4.  Also
# see https://llvm.org/bugs/show_bug.cgi?id=16404 which is a similar
# issue.
# Note that the program being compiled is complicated so that we work
# around a too smart clang optimizer.
AC_LINK_IFELSE(
	[AC_LANG_PROGRAM([[@%:@include <stdlib.h>
@%:@include <stdio.h>]], [[__int128 a=(__int128)atoll(getenv("a"))<<64, b=(__int128)atoll(getenv("b"))<<64, c; if (!__builtin_mul_overflow(a, b, &c)) printf("%lld %lld\n",(long long)(c>>64), (long long)c);]])],
	[AC_DEFINE([HAVE___BUILTIN_ADD_OVERFLOW], 1,
		[Define if you have the `__builtin_{add,sub,mul}_overflow' functions.])
	 AC_MSG_RESULT(yes)],
	[AC_MSG_RESULT(no)])

asctime_r3=yes
AC_MSG_CHECKING([asctime_r3])
AC_LINK_IFELSE(
	[AC_LANG_PROGRAM([[@%:@include <time.h>]],
		[[char buf[26]; struct tm t; asctime_r(&t,buf,26);]])],
	[AC_DEFINE([HAVE_ASCTIME_R3], 1,
		[Define if you have asctime_r(struct tm*,char *buf,size_t s)])],
	[asctime_r3=no])
AC_MSG_RESULT([$asctime_r3])

ctime_r3=yes
AC_MSG_CHECKING([ctime_r3])
AC_LINK_IFELSE(
	[AC_LANG_PROGRAM([[@%:@include <time.h>]],
		[[char buf[26]; time_t t; ctime_r(&t,buf,26);]])],
	[AC_DEFINE([HAVE_CTIME_R3], 1,
		[Define if you have ctime_r(time_t*,char *buf,size_t s)])],
	[ctime_r3=no])
AC_MSG_RESULT([$ctime_r3])

AC_CACHE_CHECK([for _sys_siglist], [mn_cv_have__sys_siglist], [
AC_LINK_IFELSE(
	[AC_LANG_PROGRAM([[]], [[printf("%s\n", _sys_siglist[0]);]])],
	[mn_cv_have__sys_siglist=yes],
	[mn_cv_have__sys_siglist=no])])
AS_VAR_IF([mn_cv_have__sys_siglist], [yes],
	[AC_DEFINE([HAVE__SYS_SIGLIST], 1, [Define if you have _sys_siglist])])

AC_STRUCT_TIMEZONE

# check dependencies (might need more than just SQL)
AS_VAR_IF([enable_sql], [yes],
	[AS_VAR_IF([enable_monetdb5], [no],
		[AC_MSG_ERROR([MonetDB/SQL requires MonetDB5${disable_monetdb5:+ $disable_monetdb5}.])])])
AS_VAR_IF([enable_sql], [auto],
	[AS_VAR_IF([enable_monetdb5], [no],
		[enable_sql=no
		 AS_VAR_IF([disable_sql], [],
			[disable_sql="(MonetDB/SQL requires MonetDB5)"])])])

#    output
AM_CONDITIONAL([HAVE_GDK], [test "x$enable_gdk" = xyes -o "x$enable_gdk" = xauto])
AM_CONDITIONAL([HAVE_MONETDB5], [test "x$enable_monetdb5" = xyes -o "x$enable_monetdb5" = xauto])
AM_CONDITIONAL([HAVE_SQL], [test "x$enable_sql" = xyes -o "x$enable_sql" = xauto])
AM_CONDITIONAL([HAVE_GEOM], [test "x$enable_geom" = xyes -o "x$enable_geom" = xauto])
AM_CONDITIONAL([HAVE_EMBEDDED], [test "x$enable_embedded" = xyes])
AM_CONDITIONAL([HAVE_TESTING], [test "x$enable_testing" = xyes -o "x$enable_testing" = xauto])
AM_CONDITIONAL([HAVE_DEVELOPER], [test "x$enable_developer" = xyes])

AC_SUBST([compilercall], ["$CC $CFLAGS $X_CFLAGS"])
AC_SUBST([linkercall], ["$LD $LDFLAGS"])
AC_SUBST([builtby], ["${USER}@`hostname`"])
pcreversion="not linked to any PCRE library"
AS_VAR_IF([have_pcre], [yes], [
        pcreversion="compiled with `pkg-config --modversion libpcre 2>/dev/null`"])
AC_SUBST([pcreversion])
opensslversion="not linked to any openssl library"
AS_VAR_IF([have_openssl], [yes], [
        opensslversion="compiled with `openssl version 2>/dev/null`"])
AC_SUBST([opensslversion])
libxml2version="not linked to any libxml2 library"
AS_VAR_IF([have_libxml2], [yes], [
        libxml2version="compiled with `pkg-config --modversion libxml-2.0 2>/dev/null`"])
AC_SUBST([libxml2version])

# provide different versions of the paths derived above
AS_VAR_IF([prefix], [NONE],
	[prefix="${ac_default_prefix}"
	 Xprefix=`$translatepath "$Qprefix"`
	 QXprefix=`echo "$Xprefix" | sed 's/\\\\/\\\\\\\\/g'`],
	[Xprefix=`$translatepath "$prefix"`
	 QXprefix=`echo "$Xprefix" | sed 's/\\\\/\\\\\\\\/g'`])
AS_VAR_IF([exec_prefix], [NONE],
	[Xexec_prefix=`$translatepath "$Qexec_prefix"`
	 QXexec_prefix=`echo "$Xexec_prefix" | sed 's/\\\\/\\\\\\\\/g'`],
	[Xexec_prefix=`$translatepath "$exec_prefix"`
	 QXexec_prefix=`echo "$Xexec_prefix" | sed 's/\\\\/\\\\\\\\/g'`])
AC_SUBST([Xprefix])
AC_SUBST([QXprefix])
AC_SUBST([Xexec_prefix])
AC_SUBST([QXexec_prefix])
Qsrcdir="$srcdir"
Xsrcdir=`$translatepath "$srcdir"`
QXsrcdir=`echo "$Xsrcdir" | sed 's/\\\\/\\\\\\\\/g'`
AC_SUBST([Qsrcdir])
AC_SUBST([Xsrcdir])
AC_SUBST([QXsrcdir])
Qlocalstatedir="$localstatedir"
Xlocalstatedir=`$translatepath "$localstatedir"`
QXlocalstatedir=`echo "$Xlocalstatedir" | sed 's/\\\\/\\\\\\\\/g'`
eval "Elocalstatedir=${Xlocalstatedir}"
QElocalstatedir=`echo "$Elocalstatedir" | sed 's/\\\\/\\\\\\\\/g'`
AC_SUBST([Qlocalstatedir])
AC_SUBST([Xlocalstatedir])
AC_SUBST([Elocalstatedir])
AC_SUBST([QXlocalstatedir])
Xlogdir=`$translatepath "$logdir"`
eval "Elogdir=${Xlogdir}"
# need second eval because localstatedir defaults to being ${prefix}/var
eval "Elogdir=${Elogdir}"
AC_SUBST([logdir])
AC_SUBST([Elogdir])
Xrundir=`$translatepath "$rundir"`
eval "Erundir=${Xrundir}"
# need second eval because localstatedir defaults to being ${prefix}/var
eval "Erundir=${Erundir}"
AC_SUBST([rundir])
AC_SUBST([Erundir])
Qlibdir="$libdir"
Xlibdir=`$translatepath "$libdir"`
QXlibdir=`echo "$Xlibdir" | sed 's/\\\\/\\\\\\\\/g'`
Elibdir=`echo "$Xlibdir" | sed 's/{exec_prefix}/{prefix}/'`
eval "Elibdir=${Elibdir}"
QElibdir=`echo "$Elibdir" | sed 's/\\\\/\\\\\\\\/g'`
AC_SUBST([Qlibdir])
AC_SUBST([Xlibdir])
AC_SUBST([QXlibdir])
Qsysconfdir="$sysconfdir"
Xsysconfdir=`$translatepath "$sysconfdir"`
QXsysconfdir=`echo "$Xsysconfdir" | sed 's/\\\\/\\\\\\\\/g'`
AC_SUBST([Qsysconfdir])
AC_SUBST([Xsysconfdir])
AC_SUBST([QXsysconfdir])
Qbindir="$bindir"
Xbindir=`$translatepath "$bindir"`
QXbindir=`echo "$Xbindir" | sed 's/\\\\/\\\\\\\\/g'`
Ebindir=`echo "$Xbindir" | sed 's/{exec_prefix}/{prefix}/'`
eval "Ebindir=${Ebindir}"
QEbindir=`echo "$Ebindir" | sed 's/\\\\/\\\\\\\\/g'`
AC_SUBST([Qbindir])
AC_SUBST([Xbindir])
AC_SUBST([Ebindir])
AC_SUBST([QXbindir])
Qlibexecdir="$libexecdir"
Xlibexecdir=`$translatepath "$libexecdir"`
QXlibexecdir=`echo "$Xlibexecdir" | sed 's/\\\\/\\\\\\\\/g'`
AC_SUBST([Qlibexecdir])
AC_SUBST([Xlibexecdir])
AC_SUBST([QXlibexecdir])
Qdatarootdir="$datarootdir"
Xdatarootdir=`$translatepath "$datarootdir"`
QXdatarootdir=`echo "$Xdatarootdir" | sed 's/\\\\/\\\\\\\\/g'`
AC_SUBST([Qdatarootdir])
AC_SUBST([Xdatarootdir])
AC_SUBST([QXdatarootdir])
AS_VAR_IF([datadir], ['${datarootdir}'],
	[datadir="${datarootdir}"])
Qdatadir="$datadir"
Xdatadir=`$translatepath "$datadir"`
QXdatadir=`echo "$Xdatadir" | sed 's/\\\\/\\\\\\\\/g'`
eval "Edatadir=${Xdatadir}"
QEdatadir=`echo "$Edatadir" | sed 's/\\\\/\\\\\\\\/g'`
AC_SUBST([Qdatadir])
AC_SUBST([Xdatadir])
AC_SUBST([QXdatadir])
Qmandir="$mandir"
Xmandir=`$translatepath "$mandir"`
QXmandir=`echo "$Xmandir" | sed 's/\\\\/\\\\\\\\/g'`
AC_SUBST([Qmandir])
AC_SUBST([Xmandir])
AC_SUBST([QXmandir])
Qincludedir="$includedir"
Xincludedir=`$translatepath "$includedir"`
QXincludedir=`echo "$Xincludedir" | sed 's/\\\\/\\\\\\\\/g'`
AC_SUBST([Qincludedir])
AC_SUBST([Xincludedir])
AC_SUBST([QXincludedir])
Qinfodir="$infodir"
Xinfodir=`$translatepath "$infodir"`
QXinfodir=`echo "$Xinfodir" | sed 's/\\\\/\\\\\\\\/g'`
AC_SUBST([Qinfodir])
AC_SUBST([Xinfodir])
AC_SUBST([QXinfodir])

QBUILD="$BUILD"
XBUILD=`$translatepath "$BUILD"`
QXBUILD=`echo "$XBUILD" | sed 's/\\\\/\\\\\\\\/g'`
AC_SUBST([QBUILD])
AC_SUBST([XBUILD])
AC_SUBST([QXBUILD])
QSOURCE="$SOURCE"
XSOURCE=`$translatepath "$SOURCE"`
QXSOURCE=`echo "$XSOURCE" | sed 's/\\\\/\\\\\\\\/g'`
AC_SUBST([QSOURCE])
AC_SUBST([XSOURCE])
AC_SUBST([QXSOURCE])
AC_DEFINE_UNQUOTED([LOCALSTATEDIR], ["$QElocalstatedir"], [modifiable single-machine data])
AC_DEFINE_UNQUOTED([DATA_DIR], ["$QEdatadir"], [read-only architecture-independent data])
AC_DEFINE_UNQUOTED([BINDIR], ["$QEbindir"], [location where binaries are installed])
AC_DEFINE_UNQUOTED([LIBDIR], ["$QElibdir"], [location where libraries are installed])

# On Solaris, libxml2 enables threading only when _REENTRANT, __MT__
# or _POSIX_C_SOURCE >= 199506L is defined.  The latter is only
# defined on POSIX compliant Solaris systems (latest of the latest
# as of this writing), hence defining that here would cause a
# redefinition error.  To work around it, we define __MT__ here, as
# _REENTRANT might also enable stuff in other software packages.
AS_CASE([$GCC-$host_os],
[yes-solaris2.11], [
	AC_DEFINE([__MT__], 1, [libxml2 enable threading compiler flag])])

# add some includes that we want to be there always (by default)
# this is kind of bad practice, but consider it legacy that would be
# good to clean up some day
AH_TOP([
@%:@ifndef _SEEN_MONETDB_CONFIG_H
@%:@define _SEEN_MONETDB_CONFIG_H 1
])
AH_BOTTOM([
@%:@ifdef HAVE_SYS_TYPES_H
@%:@ include <sys/types.h>
@%:@endif

@%:@include <stdio.h>		/* NULL, printf etc. */

@%:@include <stdlib.h>
@%:@include <errno.h>
@%:@include <stdarg.h>		/* va_alist.. */
@%:@ifdef HAVE_STDINT_H
@%:@include <stdint.h>
@%:@endif
@%:@ifndef HAVE_INTPTR_T
typedef ssize_t intptr_t;
@%:@endif
@%:@ifndef HAVE_UINTPTR_T
typedef size_t uintptr_t;
@%:@endif
@%:@ifdef HAVE_STDBOOL_H
@%:@include <stdbool.h>
@%:@endif
@%:@ifndef __bool_true_false_are_defined
@%:@define true 1
@%:@define false 0
@%:@define __bool_true_false_are_defined 1
@%:@endif

@%:@include <assert.h>

@%:@define SLASH_2_DIR_SEP(s) {char *t; for(t=strchr(s, '/'    ); t; t=strchr(t+1, '/'    )) *t=DIR_SEP;}
@%:@define DIR_SEP_2_SLASH(s) {char *t; for(t=strchr(s, DIR_SEP); t; t=strchr(t+1, DIR_SEP)) *t='/'    ;}

@%:@ifdef HAVE_LONG_LONG
typedef long long lng;
typedef unsigned long long ulng;
@%:@ define SIZEOF_LNG SIZEOF_LONG_LONG
@%:@else
@%:@ ifdef HAVE___INT64
typedef __int64 lng;
typedef unsigned __int64 ulng;
@%:@  define SIZEOF_LNG SIZEOF___INT64
@%:@ endif
@%:@endif

@%:@ifdef HAVE___INT128
typedef __int128 hge;
typedef unsigned __int128 uhge;
@%:@ define HAVE_HGE 1
@%:@ define SIZEOF_HGE SIZEOF___INT128
@%:@else
@%:@ if defined(HAVE___INT128_T) && defined(HAVE___UINT128_T)
typedef __int128_t hge;
typedef __uint128_t uhge;
@%:@  define HAVE_HGE 1
@%:@  define SIZEOF_HGE SIZEOF___INT128_T
@%:@ endif
@%:@endif


@%:@ifndef HAVE_PTRDIFF_T
@%:@ if SIZEOF_SIZE_T == SIZEOF_INT
typedef int ptrdiff_t;
@%:@ else
typedef lng ptrdiff_t;
@%:@ endif
@%:@endif

/* define printf formats for printing size_t and ssize_t variables */
@%:@if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901 && !defined(__svr4__) && !defined(WIN32) && !defined(__sgi)) || __GNUC__ > 3
@%:@ define SZFMT "%zu"
@%:@ define SSZFMT "%zd"
@%:@elif SIZEOF_SIZE_T == SIZEOF_INT
@%:@ define SZFMT "%u"
@%:@ define SSZFMT "%d"
@%:@elif SIZEOF_SIZE_T == SIZEOF_LONG
@%:@ define SZFMT "%lu"
@%:@ define SSZFMT "%ld"
@%:@elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG || SIZEOF_SIZE_T == SIZEOF___INT64
@%:@ define SZFMT ULLFMT
@%:@ define SSZFMT LLFMT
@%:@else
@%:@ error no definition for SZFMT/SSZFMT
@%:@endif

/* define printf formats for printing ptrdiff_t variables */
@%:@if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901 && !defined(__svr4__) && !defined(WIN32) && !defined(__sgi) || defined(__hpux)
@%:@define PDFMT "%td"
@%:@elif SIZEOF_PTRDIFF_T == SIZEOF_INT
@%:@define PDFMT "%d"
@%:@elif SIZEOF_PTRDIFF_T == SIZEOF_LONG
@%:@define PDFMT "%ld"
@%:@elif SIZEOF_PTRDIFF_T == SIZEOF_LONG_LONG || SIZEOF_PTRDIFF_T == SIZEOF___INT64
@%:@define PDFMT LLFMT
@%:@else
@%:@error no definition for PDFMT
@%:@endif

/* define printf format for printing pointer values */
@%:@if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901
@%:@define PTRFMT		"%p"
@%:@define PTRFMTCAST		/* no cast needed */
@%:@elif defined(_MSC_VER)
@%:@define PTRFMT		"%p"
@%:@define PTRFMTCAST
@%:@elif SIZEOF_VOID_P == SIZEOF_INT
@%:@define PTRFMT		"%x"
@%:@define PTRFMTCAST	(unsigned int)
@%:@elif SIZEOF_VOID_P == SIZEOF_LONG
@%:@define PTRFMT		"%lx"
@%:@define PTRFMTCAST	(unsigned long)
@%:@elif SIZEOF_VOID_P == SIZEOF_LONG_LONG
@%:@define PTRFMT		"%llx"
@%:@define PTRFMTCAST	(unsigned long long)
@%:@else
@%:@error no definition for PTRFMT
@%:@endif

/* defines to help the compiler check printf-style format arguments */
@%:@if !defined(__GNUC__) || __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
/* This feature is available in gcc versions 2.5 and later.  */
@%:@ ifndef __attribute__
@%:@  define __attribute__(Spec) /* empty */
@%:@ endif
@%:@else
/* The __-protected variants of `format' and `printf' attributes are
 * accepted by gcc versions 2.6.4 (effectively 2.7) and later.  */
@%:@ if !defined(__format__) && (__GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7))
@%:@  define __format__ format
@%:@  define __printf__ printf
@%:@ endif
@%:@endif
@%:@if !defined(_MSC_VER) && !defined(_In_z_)
@%:@ define _In_z_
@%:@ define _Printf_format_string_
@%:@ define __declspec(x)
@%:@endif

/* work around problem compiling batxml.c with clang
 * clang 3.5.0 claims to be GnuC 4.3, but doesn't accept
 * __attribute__((alloc_size(N))), as libml2 2.9.2 expects
 */
@%:@if defined(__GNUC__) && defined(__clang__)
@%:@define LIBXML_ATTR_ALLOC_SIZE(x)
@%:@endif

/* These are required to make MonetDB's source tree CRAN-compliant */

@%:@ifdef HAVE_EMBEDDED_R
@%:@include "embedded/undef.h"
@%:@endif

@%:@define PROMPT1		"\001\001\n"	/* prompt: ready for new query */
@%:@define PROMPT2		"\001\002\n"	/* prompt: more data needed */

@%:@endif /* _SEEN_MONETDB_CONFIG_H */
])

# Disabling the following warning is only required for the Intel compiler
# and if assertions are enabled.
AS_CASE([$GCC-$CC-$enable_assert],
[-*icc*-yes|-*ecc*-yes], [
	# # 279: controlling expression is constant
	X_CFLAGS="$X_CFLAGS -wd279"])

# CFLAGS for our code are stricter than what autoconf can cope with.
CFLAGS="$CFLAGS \$(X_CFLAGS)"

# bootstrap adds a (long) list of file names inside the
# AC_CONFIG_FILES command
AC_CONFIG_FILES([
Makefile
common/Makefile
gdk/Makefile
mal/Makefile
sql/Makefile
embedded/Makefile
mapisplit/Makefile
common/stream/Makefile
common/options/Makefile
common/utils/Makefile
mal/mal/Makefile
mal/modules/Makefile
mal/optimizer/Makefile
mal/scheduler/Makefile
mal/tools/Makefile
sql/include/Makefile
sql/common/Makefile
sql/storage/Makefile
sql/server/Makefile
sql/backends/Makefile
sql/scripts/Makefile
mal/modules/atoms/Makefile
mal/modules/kernel/Makefile
mal/modules/mal/Makefile
sql/storage/bat/Makefile
sql/backends/monet5/Makefile
])
AC_OUTPUT

echo
echo
echo "MonetDB is configured as follows:"
echo "* Compilation specifics:"
echo "    Host:     " ${ac_cv_host}
echo "    Compiler: " ${CC} "("${CC_ver}"; "${CC_version}")"
echo "    CFLAGS:   " ${CFLAGS}
echo "    X_CFLAGS: " ${X_CFLAGS}
echo "    LDFLAGS:  " ${LDFLAGS}
echo
echo "* Enabled/disabled build options:"
for comp in \
	'strict    ' \
	'assert    ' \
	'debug     ' \
	'optimize  ' \
	'developer ' \
	'sanitizer ' \
	'instrument' \
	'profile   ' \
; do
	eval "dft=\$dft_$comp"
	eval "enable=\$enable_$comp"
	eval "disable=\$disable_$comp"
	AS_CASE([$disable-$enable-$dft],
		[-no-no], [disable="(by default)"],
		[-no-*], [disable="(by command line option)"])
	echo $ECHO_N "    $comp is $ECHO_C"
	AS_VAR_IF([enable], [no], [echo "disabled $disable"], [echo "enabled"])
done
echo
echo "* Enabled/disabled components:"
for comp in \
	'gdk         ' \
	'monetdb5    ' \
	'sql         ' \
	'geom        ' \
	'console     ' \
	'pyintegration' \
	'rintegration' \
	'odbc        ' \
	'testing     ' \
	'embedded    ' \
; do
	eval "dft=\$dft_$comp"
	eval "enable=\$enable_$comp"
	eval "disable=\$disable_$comp"
	AS_CASE([$disable-$enable-$dft],
		[-no-no], [disable="(by default)"],
		[-no-*], [disable="(by command line option)"])
	echo $ECHO_N "    $comp is $ECHO_C"
	AS_VAR_IF([enable], [no], [echo "disabled $disable"], [echo "enabled"])
done
echo
echo "* Available features/extensions:"
for comp in \
	'atomic_ops   ' \
	'bz2          ' \
	'curl         ' \
	'fits         ' \
	'getaddrinfo  ' \
	'gdal         ' \
	'gsl          ' \
	'liblas       ' \
	'liblzma      ' \
	'libxml2      ' \
	'lidar        ' \
	'netcdf       ' \
	'openssl      ' \
	'pcre         ' \
	'perl         ' \
	'proj         ' \
	'pthread      ' \
	'python2      ' \
	'python3      ' \
	'readline     ' \
	'samtools     ' \
	'shp          ' \
	'sphinxclient ' \
	'unixodbc     ' \
	'valgrind     ' \
	'zlib         ' \
; do
	eval "org=\$org_have_$comp"
	eval "have=\$have_$comp"
	eval "why=\$why_have_$comp"

	AS_CASE([$why-$have-$org],
		[-no-no], [why="(by default)"],
		[-no-*], [why="(by command line option)"])
	echo $ECHO_N "    $comp = $ECHO_C"
	AS_VAR_IF([have], [no], [echo "no  $why"], [echo "yes"])
done
AS_VAR_IF([have_hge], [yes], [
	hge_bits='128'
	hge_digits=' 38'], [
	hge_bits=' 64'
	hge_digits=' 18'])
echo
echo "* Important options:"
echo "    OID size:              ${bits} bits"
echo "    largest integer type: ${hge_bits} bits"
echo "    largest decimal type: ${hge_digits} digits"
echo

# give some hints to the user
AS_VAR_IF([enable_embedded_r], [no],
	[AS_CASE([`make -v 2>&1`],
		['GNU Make '*], [],
		[echo "'make' not found in PATH or not GNU Make."
		 AS_CASE([`gmake -v 2>&1`],
			['GNU Make '*],
				[echo "Please use 'gmake' to build MonetDB."],
			[echo "'gmake' not found in PATH or not GNU Make."
			 echo "Please make sure you have GNU Make installed and use it to build MonetDB."])
		 echo])])
