dnl Process this file with autoconf to produce a configure script.
AC_PREREQ(2.54)
AC_INIT
AC_CONFIG_SRCDIR([src/mpeg2dec.c])
AC_CONFIG_AUX_DIR(autotools)
AC_CONFIG_FILES([Makefile autotools/Makefile include/Makefile test/Makefile
    doc/Makefile src/Makefile libmpeg2/Makefile libvo/Makefile vc++/Makefile
    libmpeg2/libmpeg2.pc])
AM_INIT_AUTOMAKE([mpeg2dec],[0.3.1])
AC_CONFIG_HEADERS([include/config.h])
AM_MAINTAINER_MODE
AC_CANONICAL_HOST

dnl Checks for compiler
AC_PROG_CC
AC_PROG_GCC_TRADITIONAL

dnl CC-specific flags
if test x"$CC" = x"checkergcc"; then
    enable_sdl=no
elif test x"$GCC" = x"yes"; then

    dnl GCC-specific flags

    dnl -Wall
    dnl -Werror moved to the end to not disturb the configure script
    OPT_CFLAGS="$CFLAGS -Wall"
    AC_TRY_CFLAGS([$OPT_CFLAGS],[CFLAGS=$OPT_CFLAGS])

    dnl -O3
    changequote(<<,>>)
    OPT_CFLAGS=`echo "$CFLAGS"|sed "s/-O[0-9]*//g"`
    changequote([,])
    OPT_CFLAGS="$OPT_CFLAGS -O3"
    AC_TRY_CFLAGS([$OPT_CFLAGS],[CFLAGS=$OPT_CFLAGS])

    AC_ARG_ENABLE([debug],
	[  --enable-debug          debug mode configuration])
    if test x"$enable_debug" = x"yes"; then
	AC_DEFINE([DEBUG],,[debug mode configuration])
    else
	dnl -fomit-frame-pointer
	OPT_CFLAGS="$CFLAGS -fomit-frame-pointer"
	AC_TRY_CFLAGS([$OPT_CFLAGS],[CFLAGS=$OPT_CFLAGS])
    fi

    dnl arch-specific flags
    case "$host" in
    i?86-* | k?-* | x86_64-*)
	AC_DEFINE([ARCH_X86],,[x86 architecture])
	case "$host" in
	i386-*) OPT_CFLAGS="$CFLAGS -mcpu=i386";;
	i486-*) OPT_CFLAGS="$CFLAGS -mcpu=i486";;
	i586-*) OPT_CFLAGS="$CFLAGS -mcpu=pentium";;
	i686-*) OPT_CFLAGS="$CFLAGS -mcpu=pentiumpro";;
	k6-*)   OPT_CFLAGS="$CFLAGS -mcpu=k6";;
	esac
	AC_TRY_CFLAGS([$OPT_CFLAGS],[CFLAGS=$OPT_CFLAGS]);;
    ppc-* | powerpc-*)
	OPT_CFLAGS="$CFLAGS -Wa,-m7400"
	AC_TRY_CFLAGS([$OPT_CFLAGS],
	    [CFLAGS=$OPT_CFLAGS; AC_DEFINE([ARCH_PPC],,[ppc architecture])]);;
    sparc-* | sparc64-*)
	OPT_CFLAGS="$CFLAGS -mcpu=ultrasparc -mvis -Wa,-Av9b"
	AC_TRY_CFLAGS([$OPT_CFLAGS],[CFLAGS=$OPT_CFLAGS]);;
    alpha*)
	OPT_CFLAGS="$CFLAGS -Wa,-mev6"
	AC_TRY_CFLAGS([$OPT_CFLAGS],
	    [CFLAGS=$OPT_CFLAGS;
	    AC_DEFINE([ARCH_ALPHA],,[alpha architecture])]);;
    esac
elif test x"$CC" = x"tcc" -a x"`$CC -version 2>&1 | grep TenDRA`" != x""; then
    dnl TenDRA portability checking compiler
    TENDRA=yes
    CFLAGS="-Xp -Yansi -f`pwd`/include/tendra.h -DELIDE_CODE"
    enable_mlib=no
    no_x=yes
    enable_sdl=no
elif test x"$CC" = x"icc" -a x"`$CC -V 2>&1 | grep Intel`" != x""; then
    dnl Intel C++ compiler
    CFLAGS="-g -O3 -unroll -ip"
else
    dnl non-gcc flags - we probably need exact configuration triplets here.
    case "$host" in
    sparc-sun-solaris*)
	OPT_CFLAGS="$CFLAGS -xCC -fast -xO5"
	AC_TRY_CFLAGS([$OPT_CFLAGS],[CFLAGS=$OPT_CFLAGS]);;
    esac
fi

dnl Checks for libtool - this must be done after we set cflags
AC_DISABLE_SHARED
AC_LIBTOOL_WIN32_DLL
AC_PROG_LIBTOOL

dnl Checks for libraries.

dnl Checks for header files.
INCLUDES='-I$(top_srcdir)/include -I$(top_builddir)/include'
AC_SUBST([INCLUDES])
AC_CHECK_HEADERS([sys/time.h sys/timeb.h io.h])
AC_CHECK_GENERATE_INTTYPES([include])

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_ALWAYS_INLINE
AC_C_RESTRICT
AC_C_BUILTIN_EXPECT
AC_TYPE_SIZE_T
AC_C_BIGENDIAN

dnl Checks for library functions.
AC_CHECK_FUNCS([memalign gettimeofday ftime])
AC_TYPE_SIGNAL

builtin([include],[libmpeg2/configure.incl])
builtin([include],[libvo/configure.incl])

AC_ARG_ENABLE([warnings],
    [  --enable-warnings       treat warnings as errors])
if test x"$enable_warnings" = x"yes" -a x"$GCC" = x"yes"; then
    dnl compiler warnings
    OPT_CFLAGS="$CFLAGS -Werror"
    AC_TRY_CFLAGS([$OPT_CFLAGS],[CFLAGS=$OPT_CFLAGS])
elif test x"$TENDRA" = x"yes"; then
    dnl TenDRA portability checking compiler
    CFLAGS="$CFLAGS -DTenDRA_check"
fi

AC_C_ATTRIBUTE_ALIGNED

AC_OUTPUT
