dnl Copyright © 2003-2005 Keith Packard, Daniel Stone
dnl Copyright © 2013 Lauri Kasanen
dnl
dnl Permission to use, copy, modify, distribute, and sell this software and its
dnl documentation for any purpose is hereby granted without fee, provided that
dnl the above copyright notice appear in all copies and that both that
dnl copyright notice and this permission notice appear in supporting
dnl documentation, and that the names of Keith Packard and Daniel Stone not be
dnl used in advertising or publicity pertaining to distribution of the software
dnl without specific, written prior permission.  Keith Packard and Daniel Stone
dnl make no representations about the suitability of this software for any
dnl purpose.  It is provided "as is" without express or implied warranty.
dnl
dnl KEITH PACKARD AND DANIEL STONE DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
dnl SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
dnl IN NO EVENT SHALL KEITH PACKARD OR DANIEL STONE BE LIABLE FOR ANY SPECIAL,
dnl INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
dnl LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
dnl OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
dnl PERFORMANCE OF THIS SOFTWARE.
dnl
dnl Process this file with autoconf to create configure.

AC_PREREQ(2.57)
AC_INIT([tinyx], 1.3)
AC_PREREQ([2.60])
AC_CONFIG_SRCDIR([Makefile.am])
AC_CONFIG_MACRO_DIR([m4])
AC_USE_SYSTEM_EXTENSIONS
AM_INIT_AUTOMAKE([dist-bzip2 no-dist-gzip foreign -Wall])

dnl this gets generated by autoheader, and thus contains all the defines.  we
dnl don't ever actually use it, internally.
AC_CONFIG_HEADERS(include/do-not-use-config.h)
dnl dix-config.h covers most of the DIX (i.e. everything but the DDX, not just
dnl dix/).
AC_CONFIG_HEADERS(include/dix-config.h)
dnl kdrive-config.h covers the kdrive DDX
AC_CONFIG_HEADERS(include/kdrive-config.h)

AC_PROG_CC
AM_PROG_CC_C_O
m4_ifdef([AM_PROG_AR], [AM_PROG_AR])
AM_PROG_AS
AC_PROG_INSTALL
AC_PROG_LN_S
AC_DISABLE_SHARED
AC_PROG_LIBTOOL
PKG_PROG_PKG_CONFIG
AC_PROG_LEX
AC_PROG_YACC

AC_HEADER_DIRENT
AC_HEADER_STDC
AC_CHECK_HEADERS([fcntl.h stdlib.h string.h unistd.h])

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_BIGENDIAN([ENDIAN="X_BIG_ENDIAN"], [ENDIAN="X_LITTLE_ENDIAN"])

AC_CHECK_SIZEOF([unsigned long])
if test "$ac_cv_sizeof_unsigned_long" = 8; then
	AC_DEFINE(_XSERVER64, 1, [Define to 1 if unsigned long is 64 bits.])
fi

AC_TYPE_PID_T

dnl Checks for library functions.
AC_FUNC_VPRINTF
AC_CHECK_FUNCS([geteuid getuid link memmove memset mkstemp strchr strrchr \
		strtol getopt getopt_long vsnprintf])
AC_FUNC_ALLOCA
dnl Old HAS_* names used in os/*.c.
AC_CHECK_FUNC([getdtablesize],
	AC_DEFINE(HAS_GETDTABLESIZE, 1, [Have the `getdtablesize' function.]))
AC_CHECK_FUNC([getifaddrs],
	AC_DEFINE(HAS_GETIFADDRS, 1, [Have the `getifaddrs' function.]))
AC_CHECK_FUNC([getpeereid],
	AC_DEFINE(HAS_GETPEEREID, 1, [Have the `getpeereid' function.]))
AC_CHECK_FUNC([getpeerucred],
	AC_DEFINE(HAS_GETPEERUCRED, 1, [Have the `getpeerucred' function.]))
AC_CHECK_FUNC([strlcat], HAVE_STRLCAT=yes, HAVE_STRLCAT=no)
AM_CONDITIONAL(NEED_STRLCAT, [test x$HAVE_STRLCAT = xno])	

AM_CONDITIONAL(NEED_VSNPRINTF, [test x$HAVE_VSNPRINTF = xno])

dnl Check for mmap support for Xvfb
AC_CHECK_FUNC([mmap], AC_DEFINE(HAS_MMAP, 1, [Have the `mmap' function.]))

dnl Find the math libary
AC_CHECK_LIB(m, sqrt)

dnl APM header
AC_CHECK_HEADERS([linux/apm_bios.h], LNXAPM=yes)
AM_CONDITIONAL(LNXAPM, [test "x$LNXAPM" = xyes])

dnl fbdev header
AC_CHECK_HEADERS([linux/fb.h], FBDEV=yes)
AM_CONDITIONAL(FBDEVHW, [test "x$FBDEV" = xyes])

dnl MTRR header
AC_CHECK_HEADERS([asm/mtrr.h], ac_cv_asm_mtrr_h=yes)
if test "x$ac_cv_asm_mtrr_h" = xyes; then
	HAVE_MTRR=yes
fi

dnl BSD MTRR header
AC_CHECK_HEADERS([sys/memrange.h], ac_cv_memrange_h=yes)
if test "x$ac_cv_memrange_h" = xyes; then
	HAVE_MTRR=yes
fi

if test "x$HAVE_MTRR" = xyes; then
	AC_DEFINE(HAS_MTRR_SUPPORT, 1, [MTRR support available])
fi

dnl A NetBSD MTRR header
AC_CHECK_HEADERS([machine/mtrr.h], ac_cv_machine_mtrr_h=yes)
if test "x$ac_cv_machine_mtrr_h" = xyes; then
	AC_DEFINE(HAS_MTRR_BUILTIN, 1, [Define to 1 if NetBSD built-in MTRR
		support is available])
fi

dnl ---------------------------------------------------------------------------
dnl Bus options and CPU capabilities.  Replaces logic in
dnl hw/xfree86/os-support/bus/Makefile.am, among others.
dnl ---------------------------------------------------------------------------
DEFAULT_INT10="x86emu"

dnl Override defaults as needed for specific platforms:

case $host_cpu in
  alpha*)
	ALPHA_VIDEO=yes
	case $host_os in
		*netbsd*)	AC_DEFINE(USE_ALPHA_PIO, 1, [NetBSD PIO alpha IO]) ;;
	esac
	;;
  arm*)
	ARM_VIDEO=yes
	;;
  i*86)
	I386_VIDEO=yes
	case $host_os in
		*linux*)	DEFAULT_INT10=vm86 ;;
		*freebsd*)	AC_DEFINE(USE_DEV_IO) ;;
		*netbsd*)	AC_DEFINE(USE_I386_IOPL)
				SYS_LIBS=-li386
				;;
		*openbsd*)	AC_DEFINE(USE_I386_IOPL) 
				SYS_LIBS=-li386
				;;
	esac
        ;;
  powerpc*)
	PPC_VIDEO=yes
	case $host_os in
		*freebsd*)	DEFAULT_INT10=stub ;;
	esac
	;;
  sparc*)
	xorg_loader_sparcmuldiv="yes"
	SPARC64_VIDEO=yes
	BSD_ARCH_SOURCES="sparc64_video.c ioperm_noop.c"
	;;
  x86_64*|amd64*)
	I386_VIDEO=yes
	case $host_os in
		*freebsd*)	AC_DEFINE(USE_DEV_IO, 1, [BSD /dev/io]) ;;
		*netbsd*)	AC_DEFINE(USE_I386_IOPL, 1, [BSD i386 iopl])
				SYS_LIBS=-lx86_64
				;;
		*openbsd*)	AC_DEFINE(USE_AMD64_IOPL, 1, [BSD AMD64 iopl])
				SYS_LIBS=-lamd64
				;;
	esac
	;;
esac

dnl BSD *_video.c selection
AM_CONDITIONAL(ALPHA_VIDEO, [test "x$ALPHA_VIDEO" = xyes])
AM_CONDITIONAL(ARM_VIDEO, [test "x$ARM_VIDEO" = xyes])
AM_CONDITIONAL(I386_VIDEO, [test "x$I386_VIDEO" = xyes])
AM_CONDITIONAL(PPC_VIDEO, [test "x$PPC_VIDEO" = xyes])
AM_CONDITIONAL(SPARC64_VIDEO, [test "x$SPARC64_VIDEO" = xyes])

dnl it would be nice to autodetect these *CONS_SUPPORTs
case $host_os in
  *freebsd*)
	case $host_os in
		kfreebsd*-gnu) ;;
		*) AC_DEFINE(CSRG_BASED, 1, [System is BSD-like]) ;;
	esac
	;;
  *netbsd*)
	AC_DEFINE(CSRG_BASED, 1, [System is BSD-like])
	;;
  *openbsd*)
	AC_DEFINE(CSRG_BASED, 1, [System is BSD-like])
	;;
esac

OSNAME=`uname -srm`
AC_DEFINE_UNQUOTED(OSNAME, "$OSNAME", 
	[Define to OS Name string to display for build OS in Xorg log])

DEFAULT_VENDOR_NAME="TinyCore Linux"
DEFAULT_VENDOR_NAME_SHORT="TinyCore"
VERSION_MAJOR=1
VERSION_MINOR=0
VERSION_PATCH=0
VERSION_SNAP=0
RELEASE_DATE="22 Sep 2015"
DEFAULT_VENDOR_WEB="http://tinycorelinux.com"

m4_ifdef([AS_HELP_STRING], , [m4_define([AS_HELP_STRING], m4_defn([AC_HELP_STRING]))])

dnl Build options.
AC_ARG_ENABLE(werror,        AS_HELP_STRING([--enable-werror],
				  [Treat warnings as errors (default: disabled)]),
			        [WERROR=$enableval], [WERROR=no])
AC_ARG_ENABLE(debug,         AS_HELP_STRING([--enable-debug],
				  [Enable debugging (default: disabled)]),
			        [DEBUGGING=$enableval], [DEBUGGING=no])
AC_ARG_WITH(int10,           AS_HELP_STRING([--with-int10=BACKEND], [int10 backend: vm86, x86emu or stub]),
				[INT10="$withval"],
				[INT10="$DEFAULT_INT10"])
AC_ARG_WITH(vendor-name,     AS_HELP_STRING([--with-vendor-string=VENDOR],
				  [Vendor string reported by the server]),
			     	[ VENDOR_STRING="$withval" ],
			     	[ VENDOR_STRING="$DEFAULT_VENDOR_NAME" ])
AC_ARG_WITH(vendor-name-short, AS_HELP_STRING([--with-vendor-string-short=VENDOR],
				  [Short version of vendor string reported by the server]),
			     	[ VENDOR_STRING_SHORT="$withval" ],
			     	[ VENDOR_STRING_SHORT="$DEFAULT_VENDOR_NAME_SHORT" ])
AC_ARG_WITH(vendor-web,      AS_HELP_STRING([--with-vendor-web=URL],
				  [Vendor web address reported by the server]),
				[ VENDOR_WEB="$withval" ],
				[ VENDOR_WEB="$DEFAULT_VENDOR_WEB" ])
AC_ARG_WITH(builder-addr,    AS_HELP_STRING([--with-builder-addr=ADDRESS],
				  [Builder address (default: xorg@lists.freedesktop.org)]),
				[ BUILDERADDR="$withval" ],
				[ BUILDERADDR="xorg@lists.freedesktop.org" ])
AC_ARG_WITH(fontdir,         AS_HELP_STRING([--with-fontdir=FONTDIR], [Path to top level dir where fonts are installed (default: ${libdir}/X11/fonts)]),
                                [ FONTDIR="$withval" ],
                                [ FONTDIR="${libdir}/X11/fonts" ])
DEFAULT_FONT_PATH="${FONTDIR}/misc/,${FONTDIR}/100dpi/,${FONTDIR}/75dpi/"
AC_ARG_WITH(default-font-path, AS_HELP_STRING([--with-default-font-path=PATH], [Comma separated list of font dirs]),
				[ FONTPATH="$withval" ],
				[ FONTPATH="${DEFAULT_FONT_PATH}" ])

dnl Extensions.
AC_ARG_ENABLE(xres,           AS_HELP_STRING([--disable-xres], [Build XRes extension (default: enabled)]), [RES=$enableval], [RES=yes])
AC_ARG_ENABLE(screensaver,    AS_HELP_STRING([--disable-screensaver], [Build ScreenSaver extension (default: enabled)]), [SCREENSAVER=$enableval], [SCREENSAVER=yes])
AC_ARG_ENABLE(xdmcp,          AS_HELP_STRING([--disable-xdmcp], [Build XDMCP extension (default: auto)]), [XDMCP=$enableval], [XDMCP=auto])
AC_ARG_ENABLE(xdm-auth-1,     AS_HELP_STRING([--disable-xdm-auth-1], [Build XDM-Auth-1 extension (default: auto)]), [XDMAUTH=$enableval], [XDMAUTH=auto])
AC_ARG_ENABLE(dbe,            AS_HELP_STRING([--disable-dbe], [Build DBE extension (default: enabled)]), [DBE=$enableval], [DBE=yes])
AC_ARG_ENABLE(xf86bigfont,    AS_HELP_STRING([--disable-xf86bigfont], [Build XF86 Big Font extension (default: enabled)]), [XF86BIGFONT=$enableval], [XF86BIGFONT=yes])
AC_ARG_ENABLE(dpms,           AS_HELP_STRING([--disable-dpms], [Build DPMS extension (default: enabled)]), [DPMSExtension=$enableval], [DPMSExtension=yes])

dnl kdrive and its subsystems
AC_ARG_ENABLE(kdrive,         AS_HELP_STRING([--enable-kdrive], [Build kdrive servers (default: yes)]), [KDRIVE=$enableval], [KDRIVE=yes])


dnl chown/chmod to be setuid root as part of build
dnl Replaces InstallXserverSetUID in imake
AC_ARG_ENABLE(install-setuid, 
    AS_HELP_STRING([--enable-install-setuid],
       [Install Xorg server as owned by root with setuid bit (default: auto)]),
    [SETUID=$enableval], [SETUID=auto])
AC_MSG_CHECKING([to see if we can install the Xorg server as root])
if test "x$SETUID" = "xauto" ; then
	case $host_os in
	    darwin*)		SETUID="no"  ;;
	    *)
	   	case $host_cpu in
		    sparc)	SETUID="no"  ;;
		    *)		SETUID="yes" ;;
		esac
	esac
	if test "x$SETUID" = xyes; then
		touch testfile
		chown root testfile > /dev/null 2>&1 || SETUID="no"
		rm -f testfile
	fi
fi
AC_MSG_RESULT([$SETUID])
AM_CONDITIONAL(INSTALL_SETUID, [test "x$SETUID" = "xyes"])

dnl Issue an error if xtrans.m4 was not found and XTRANS_CONNECTION_FLAGS macro
dnl was not expanded, since xorg-server with no transport types is rather useless.
dnl
dnl If you're seeing an error here, be sure you installed the lib/xtrans module
dnl first and if it's not in the default location, that you set the ACLOCAL
dnl environment variable to find it, such as:
dnl	ACLOCAL="aclocal -I ${PREFIX}/share/aclocal"
m4_pattern_forbid([^XTRANS_CONNECTION_FLAGS$])

# Transport selection macro from xtrans.m4
XTRANS_CONNECTION_FLAGS

# Secure RPC detection macro from xtrans.m4
XTRANS_SECURE_RPC_FLAGS

AM_CONDITIONAL(INT10_VM86, [test "x$INT10" = xvm86])
AM_CONDITIONAL(INT10_X86EMU, [test "x$INT10" = xx86emu])
AM_CONDITIONAL(INT10_STUB, [test "x$INT10" = xstub])
if test "x$INT10" = xyes; then
	dnl VM86 headers
	AC_CHECK_HEADERS([sys/vm86.h sys/io.h])
fi

dnl ---------------------------------------------------------------------------
dnl Extension section
dnl ---------------------------------------------------------------------------
XEXT_INC='-I$(top_srcdir)/Xext'
XEXT_LIB='$(top_builddir)/Xext/libXext.la'
XEXTXORG_LIB='$(top_builddir)/Xext/libXextbuiltin.la'

dnl Core modules for most extensions, et al.
REQUIRED_MODULES="[randrproto >= 1.2] renderproto [fixesproto >= 4.0] [damageproto >= 1.1] xcmiscproto xextproto xproto xtrans xf86bigfontproto [scrnsaverproto >= 1.1] bigreqsproto resourceproto fontsproto inputproto [kbproto >= 1.0.3]"
REQUIRED_LIBS="xfont fontenc"

AM_CONDITIONAL(SCREENSAVER, [test "x$SCREENSAVER" = xyes])
if test "x$SCREENSAVER" = xyes; then
	AC_DEFINE(SCREENSAVER, 1, [Support MIT-SCREEN-SAVER extension])
fi

AM_CONDITIONAL(RES, [test "x$RES" = xyes])
if test "x$RES" = xyes; then
	AC_DEFINE(RES, 1, [Support X resource extension])
	REQUIRED_MODULES="$REQUIRED_MODULES resourceproto"
fi

AM_CONDITIONAL(DBE, [test "x$DBE" = xyes])
if test "x$DBE" = xyes; then
	AC_DEFINE(DBE, 1, [Support DBE extension])
	DBE_LIB='$(top_builddir)/dbe/libdbe.la'
fi

AM_CONDITIONAL(XF86BIGFONT, [test "x$XF86BIGFONT" = xyes])
if test "x$XF86BIGFONT" = xyes; then
	AC_DEFINE(XF86BIGFONT, 1, [Support XF86 Big font extension])
fi

AM_CONDITIONAL(DPMSExtension, [test "x$DPMSExtension" = xyes])
if test "x$DPMSExtension" = xyes; then
	AC_DEFINE(DPMSExtension, 1, [Support DPMS extension])
fi

RENDER_LIB='$(top_builddir)/render/librender.la'
RENDER_INC='-I$(top_srcdir)/render'

RANDR_LIB='$(top_builddir)/randr/librandr.la'
RANDR_INC='-I$(top_srcdir)/randr'

FIXES_LIB='$(top_builddir)/xfixes/libxfixes.la'
FIXES_INC='-I$(top_srcdir)/xfixes'

DAMAGE_LIB='$(top_builddir)/damageext/libdamageext.la'
DAMAGE_INC='-I$(top_srcdir)/damageext'
MIEXT_DAMAGE_LIB='$(top_builddir)/miext/damage/libdamage.la'
MIEXT_DAMAGE_INC='-I$(top_srcdir)/miext/damage'

AC_CHECK_FUNC(strcasecmp, [], AC_DEFINE([NEED_STRCASECMP], 1,
                                        [Do not have `strcasecmp'.]))

PKG_CHECK_MODULES([XDMCP], [xdmcp], [have_libxdmcp="yes"], [have_libxdmcp="no"])
if test "x$have_libxdmcp" = xyes; then
	AC_CHECK_LIB(Xdmcp, XdmcpWrap, [have_xdmcpwrap="yes"], [have_xdmcpwrap="no"], [$XDMCP_LIBS])
fi
if test "x$XDMCP" = xauto; then
	if test "x$have_libxdmcp" = xyes; then
		XDMCP=yes
	else
		XDMCP=no
	fi
fi
if test "x$XDMAUTH" = xauto; then
	if test "x$have_libxdmcp" = xyes && test "x$have_xdmcpwrap" = xyes; then
		XDMAUTH=yes
	else
		XDMAUTH=no
	fi
fi

AM_CONDITIONAL(XDMCP, [test "x$XDMCP" = xyes])
if test "x$XDMCP" = xyes; then
	AC_DEFINE(XDMCP, 1, [Support XDM Control Protocol])
	REQUIRED_LIBS="$REQUIRED_LIBS xdmcp"
	XDMCP_MODULES="xdmcp"
fi

AM_CONDITIONAL(XDMAUTH, [test "x$XDMAUTH" = xyes])
if test "x$XDMAUTH" = xyes; then
	AC_DEFINE(HASXDMAUTH,1,[Support XDM-AUTH*-1])
	if ! test "x$XDMCP" = xyes; then
		REQUIRED_LIBS="$REQUIRED_LIBS xdmcp"
		XDMCP_MODULES="xdmcp"
	fi
fi

VENDOR_RELEASE="((($VERSION_MAJOR) * 10000000) + (($VERSION_MINOR) * 100000) + (($VERSION_PATCH) * 1000) + $VERSION_SNAP)"

if test $VERSION_SNAP = "0"; then
	if test $VERSION_PATCH = "0"; then
		VENDOR_VERSION_STRING="${VERSION_MAJOR}.${VERSION_MINOR}"
	else
		VENDOR_VERSION_STRING="${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}"
	fi
else
	VENDOR_VERSION_STRING="${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}.${VERSION_SNAP}"
fi

VENDOR_RELEASE_STRING="Release ${VENDOR_VERSION_STRING}"
VENDOR_MAN_VERSION="Version ${VENDOR_VERSION_STRING}"

AC_DEFINE_DIR(COMPILEDDEFAULTFONTPATH, FONTPATH, [Default font path])
AC_DEFINE_DIR(BASE_FONT_PATH, FONTDIR, [Default base font path])
AC_DEFINE_UNQUOTED(XVENDORNAME, ["$VENDOR_STRING"], [Vendor name])
AC_DEFINE_UNQUOTED(XVENDORNAMESHORT, ["$VENDOR_STRING_SHORT"], [Short vendor name])
AC_DEFINE_UNQUOTED(XORG_RELEASE, ["$VENDOR_RELEASE_STRING"], [Vendor release])
AC_DEFINE_UNQUOTED(XORG_DATE, ["$RELEASE_DATE"], [Vendor release])
AC_DEFINE_UNQUOTED(XORG_MAN_VERSION, ["$VENDOR_MAN_VERSION"], [Vendor man version])
AC_DEFINE_UNQUOTED(BUILDERADDR, ["$BUILDERADDR"], [Builder address])
AC_DEFINE_UNQUOTED(OSNAME, ["$OSNAME"], [Operating System Name])
AC_SUBST([VENDOR_STRING])
AC_SUBST([VENDOR_STRING_SHORT])
AC_SUBST([VENDOR_RELEASE])
AC_SUBST([VENDOR_MAN_VERSION])

AC_DEFINE(SMART_SCHEDULE, 1, [Include time-based scheduler])
AM_CONDITIONAL(DEBUG, test "x$DEBUGGING" = xyes)

if ! test "x$DEBUGGING" = xyes; then
  AC_DEFINE(NDEBUG, 1, [Disable some debugging code])
fi

DIX_LIB='$(top_builddir)/dix/libdix.la'
OS_LIB='$(top_builddir)/os/libos.la'
MI_LIB='$(top_builddir)/mi/libmi.la'
MI_EXT_LIB='$(top_builddir)/mi/libmiext.la'
MI_INC='-I$(top_srcdir)/mi'
FB_LIB='$(top_builddir)/fb/libfb.la'
FB_INC='-I$(top_srcdir)/fb'
MIEXT_SHADOW_INC='-I$(top_srcdir)/miext/shadow'
MIEXT_SHADOW_LIB='$(top_builddir)/miext/shadow/libshadow.la'
CORE_INCS='-I$(top_srcdir)/include -I$(top_builddir)/include'

PKG_CHECK_MODULES([XSERVERCFLAGS], [$REQUIRED_MODULES $REQUIRED_LIBS])
PKG_CHECK_MODULES([XSERVERLIBS], [$REQUIRED_LIBS])

XSERVER_CFLAGS="${XSERVERCFLAGS_CFLAGS}"
XSERVER_LIBS="${XSERVERLIBS_LIBS} ${SYS_LIBS} -lm"
AC_SUBST([SYS_LIBS])

AC_CHECK_FUNCS([clock_gettime], [have_clock_gettime=yes],
               [AC_CHECK_LIB([rt], [clock_gettime], [have_clock_gettime=-lrt],
                             [have_clock_gettime=no])])

AC_MSG_CHECKING([for a useful monotonic clock ...])

if ! test "x$have_clock_gettime" = xno; then
    if ! test "x$have_clock_gettime" = xyes; then
        LIBS="$have_clock_gettime"
    else
        LIBS=""
    fi

    AC_RUN_IFELSE([AC_LANG_SOURCE([
#define _POSIX_C_SOURCE 199309L
#include <time.h>

int main(int argc, char *argv[]) {
    struct timespec tp;

    if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
        return 0;
    else
        return 1;
}
    ])], [MONOTONIC_CLOCK=yes], [MONOTONIC_CLOCK=no],
       [MONOTONIC_CLOCK="cross compiling"])
else
    MONOTONIC_CLOCK=no
fi

AC_MSG_RESULT([$MONOTONIC_CLOCK])

if test "x$MONOTONIC_CLOCK" = xyes; then
    AC_DEFINE(MONOTONIC_CLOCK, 1, [Have monotonic clock from clock_gettime()])
    XSERVER_LIBS="$XSERVER_LIBS $LIBS"
fi

XSERVER_CFLAGS="$XSERVER_CFLAGS $CORE_INCS $XEXT_INC $DAMAGE_INC $FIXES_INC $MI_INC $MIEXT_SHADOW_INC $MIEXT_LAYER_INC $MIEXT_DAMAGE_INC $RENDER_INC $RANDR_INC $FB_INC"
AC_DEFINE_UNQUOTED(X_BYTE_ORDER,[$ENDIAN],[Endian order])

AC_SUBST([XSERVER_LIBS])

dnl ---------------------------------------------------------------------------
dnl DDX section.
dnl ---------------------------------------------------------------------------

# XORG_CORE_LIBS is needed even if you're not building the Xorg DDX
XORG_CORE_LIBS="$DIX_LIB"
AC_SUBST([XORG_CORE_LIBS])

xorg_bus_linuxpci=no
xorg_bus_freebsdpci=no
xorg_bus_netbsdpci=no
xorg_bus_ix86pci=no
xorg_bus_ppcpci=no
xorg_bus_sparcpci=no
xorg_bus_sparc=no

AM_CONDITIONAL([XORG], [test "x$XORG" = xyes])
AM_CONDITIONAL([XORG_BUS_LINUXPCI], [test "x$xorg_bus_linuxpci" = xyes])
AM_CONDITIONAL([XORG_BUS_FREEBSDPCI], [test "x$xorg_bus_freebsdpci" = xyes])
AM_CONDITIONAL([XORG_BUS_NETBSDPCI], [test "x$xorg_bus_netbsdpci" = xyes])
AM_CONDITIONAL([XORG_BUS_IX86PCI], [test "x$xorg_bus_ix86pci" = xyes])
AM_CONDITIONAL([XORG_BUS_PPCPCI], [test "x$xorg_bus_ppcpci" = xyes])
AM_CONDITIONAL([XORG_BUS_SPARCPCI], [test "x$xorg_bus_sparcpci" = xyes])
AM_CONDITIONAL([XORG_BUS_SPARC], [test "x$xorg_bus_sparc" = xyes])
AM_CONDITIONAL([XORG_LOADER_SPARC], [test "x$xorg_loader_sparcmuldiv" = xyes])
AM_CONDITIONAL([LINUX_IA64], [test "x$linux_ia64" = xyes])
AM_CONDITIONAL([LINUX_ALPHA], [test "x$linux_alpha" = xyes])
AM_CONDITIONAL([LNXACPI], [test "x$linux_acpi" = xyes])

dnl kdrive DDX

AM_CONDITIONAL(KDRIVE, [test x$KDRIVE = xyes])
if test "$KDRIVE" = yes; then
    AC_DEFINE(KDRIVESERVER,1,[Build Kdrive X server])
    AC_DEFINE(KDRIVEDDXACTIONS,,[Build kdrive ddx])

    AC_CHECK_HEADERS([asm/vm86.h sys/io.h])
    if test "$ac_cv_header_asm_vm86_h" = yes; then
        AC_DEFINE(KDRIVEVESA, 1, [Build VESA-based kdrive servers])
    fi

    AC_CHECK_HEADERS([linux/fb.h])
    if test "$ac_cv_header_linux_fb_h" = yes; then
        AC_DEFINE(KDRIVEFBDEV, 1, [Build fbdev-based kdrive server])
    fi
    
    # damage shadow extension fb mi
    KDRIVE_INC='-I$(top_srcdir)/kdrive/src'
    KDRIVE_PURE_INCS="$KDRIVE_INC $MIEXT_DAMAGE_INC $MIEXT_SHADOW_INC $XEXT_INC $FB_INC $MI_INC"
    KDRIVE_OS_INC='-I$(top_srcdir)/kdrive/linux'
    KDRIVE_INCS="$KDRIVE_PURE_INCS $KDRIVE_OS_INC"
    
    KDRIVE_CFLAGS="$XSERVER_CFLAGS -DHAVE_KDRIVE_CONFIG_H"

    # dix os fb mi extension glx (NOTYET) damage shadow
    #KDRIVE_PURE_LIBS="$DIX_LIB $OS_LIB $FB_LIB $XEXT_LIB $MIEXT_DAMAGE_LIB \
    #    $MIEXT_SHADOW_LIB"
    KDRIVE_PURE_LIBS="$FB_LIB $MI_LIB $FIXES_LIB $XEXT_LIB $DBE_LIB $RENDER_LIB $RANDR_LIB $DAMAGE_LIB $MIEXT_DAMAGE_LIB $MIEXT_SHADOW_LIB $OS_LIB"
    KDRIVE_LIB='$(top_builddir)/kdrive/src/libkdrive.a'
    case $host_os in
	*linux*)
	    KDRIVE_OS_LIB='$(top_builddir)/kdrive/linux/liblinux.a'
            KDRIVELINUX=yes
	    ;;
    esac
    KDRIVE_STUB_LIB='$(top_builddir)/kdrive/src/libkdrivestubs.a'
    KDRIVE_LIBS="$DIX_LIB $KDRIVE_LIB $KDRIVE_OS_LIB $KDRIVE_PURE_LIBS $KDRIVE_STUB_LIB"
fi
AC_SUBST(KDRIVE_INCS)
AC_SUBST(KDRIVE_PURE_INCS)
AC_SUBST(KDRIVE_CFLAGS)
AC_SUBST(KDRIVE_PURE_LIBS)
AC_SUBST(KDRIVE_LIBS)
AM_CONDITIONAL(KDRIVELINUX, [test "x$KDRIVELINUX" = xyes])
AM_CONDITIONAL(KDRIVEVESA, [test x"$ac_cv_header_asm_vm86_h" = xyes])
AM_CONDITIONAL(KDRIVEFBDEV, [test x"$ac_cv_header_linux_fb_h" = xyes])

dnl and the rest of these are generic, so they're in config.h
AC_DEFINE(XResExtension, 1, [Build XRes extension])

dnl ---------- Compiler arguments

AX_CHECK_COMPILE_FLAG([-flto],
	[CFLAGS="$CFLAGS -flto"; LDFLAGS="$LDFLAGS -flto"; ac_cv_lto_supported=yes],
	[ac_cv_lto_supported=no],
	[])

if test x"$ac_cv_lto_supported" = xno; then
	AC_MSG_NOTICE([LTO not supported, checking sections instead...])

	AX_CHECK_COMPILE_FLAG([-ffunction-sections],
		[CFLAGS="$CFLAGS -ffunction-sections -fdata-sections"
		LDFLAGS="$LDFLAGS -Wl,-gc-sections"
		ac_cv_sections_supported=yes],
		[ac_cv_sections_supported=no],
		[])
fi

AX_CHECK_COMPILE_FLAG([-fvisibility=hidden],
	[CFLAGS="$CFLAGS -fvisibility=hidden"; ac_cv_visibility_supported=yes],
	[ac_cv_visiblity_supported=no],
	[])

# Defeat .eh_frame bloat (gcc 4.6.3 x86-32 defconfig: 20% smaller busybox binary)
AX_CHECK_COMPILE_FLAG([-fno-unwind-tables],
	[CFLAGS="$CFLAGS -fno-unwind-tables"],
	[],
	[])
AX_CHECK_COMPILE_FLAG([-fno-asynchronous-unwind-tables],
	[CFLAGS="$CFLAGS -fno-asynchronous-unwind-tables"],
	[],
	[])

AX_CHECK_LINK_FLAG([-Wl,-as-needed], [LDFLAGS="$LDFLAGS -Wl,-as-needed"],
	[], [])

dnl ----------

AC_CHECK_LIB([ife],[meaning])

CFLAGS="$XSERVER_CFLAGS $CFLAGS -Wall"
AC_SUBST([CFLAGS])

LDFLAGS="$LDFLAGS -Wl,-O1"
AC_SUBST([LDFLAGS])

BUILD_DATE="$(date +'%Y%m%d')"
AC_SUBST([BUILD_DATE])

DIX_CFLAGS="-DHAVE_DIX_CONFIG_H"
AC_SUBST([DIX_CFLAGS])

AC_SUBST([libdir])
AC_SUBST([exec_prefix])
AC_SUBST([prefix])

# XORG in this case refers to the roll-up releases, not the Xorg DDX.
XORG_RELEASE_VERSION

AC_OUTPUT([
Makefile
include/Makefile
damageext/Makefile
dbe/Makefile
dix/Makefile
fb/Makefile
mi/Makefile
miext/Makefile
miext/damage/Makefile
miext/shadow/Makefile
os/Makefile
randr/Makefile
render/Makefile
Xext/Makefile
xfixes/Makefile
kdrive/Makefile
kdrive/fbdev/Makefile
kdrive/linux/Makefile
kdrive/src/Makefile
kdrive/vesa/Makefile
])
