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

AC_INIT(src/armdis.h)

AC_CANONICAL_HOST
AC_CANONICAL_TARGET

AM_INIT_AUTOMAKE(VisualBoyAdvance, 1.7.2)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_YACC
AM_PROG_LEX
AC_PROG_CXX
AC_PROG_RANLIB
AC_PATH_PROG(NASM, nasm)

case "$target" in
  i?86-*-*)
    VBA_USE_C_CORE=no
    VBA_USE_MMX=yes
    VBA_ENABLE_DEV=yes
    VBA_ENABLE_PROFILING=yes
    ;;
  *-psp-*)
    VBA_USE_C_CORE=yes
    VBA_USE_MMX=no
    VBA_ENABLE_DEV=no
    VBA_ENABLE_PROFILING=no
    CFLAGS="$CFLAGS -G0 -fsingle-precision-constant"
    CXXFLAGS="$CXXFLAGS -G0 -fsingle-precision-constant"
    ;;
  *)
    VBA_USE_C_CORE=yes
    VBA_USE_MMX=no
    VBA_ENABLE_DEV=yes
    VBA_ENABLE_PROFILING=yes
    ;;
esac

AC_ARG_ENABLE(c-core,
  AC_HELP_STRING([--enable-c-core],[enable C core (default is no on x86 targets)]),
  , enable_c_core=$VBA_USE_C_CORE)

AC_ARG_ENABLE(profiling,
  AC_HELP_STRING([--enable-profiling],[enable profiling (default is yes)]),
  , enable_profiling=$VBA_ENABLE_PROFILING)

AC_ARG_WITH(mmx,
  AC_HELP_STRING([--with-mmx],[use MMX (default is yes on x86 targets)]),
  , with_mmx=$VBA_USE_MMX)

AC_ARG_ENABLE(sdl,
  AC_HELP_STRING([--enable-sdl],[build the SDL interface (default is yes)]),
  , enable_sdl=yes)

AC_ARG_ENABLE(gtk,
[  --enable-gtk=[[VERSION]]  build the GTK+ interface (default is no)],[
if test "x$enable_gtk" != xno; then
  enable_gtk=yes
  if test "x$enableval" = xyes; then
    gtk_version=2.0
  else
    gtk_version=$enableval
  fi
fi
],[
enable_gtk=no
])

AC_ARG_ENABLE(dev,
  AC_HELP_STRING([--enable-dev],[enable development features (default is yes)]),
  , enable_dev=$VBA_ENABLE_DEV)

dnl Checks for libraries.
if test "x$enable_sdl" = xyes || test "x$enable_gtk" = xyes; then
  SDL_VERSION=1.2.2
  AM_PATH_SDL($SDL_VERSION,[
  ],[
  AC_MSG_ERROR([*** Couldn't find SDL library (version >= $SDL_VERSION).])
  ])
fi
CFLAGS="$CFLAGS $SDL_CFLAGS"
LIBS="$LIBS $SDL_LIBS"

AC_CHECK_LIB(z, gzopen,
  , AC_MSG_ERROR([*** Cannot compile without zlib.]))
AC_CHECK_LIB(png, png_create_write_struct,
  , AC_MSG_ERROR([*** Cannot compile without libpng.]), [-lz])
AC_CHECK_LIB(pthread, pthread_yield)

dnl Checks for header files.
AC_PATH_X
AC_HEADER_STDC
AC_CHECK_HEADERS(malloc.h strings.h unistd.h arpa/inet.h netinet/in.h zutil.h memory.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_TYPE_SIZE_T
AC_STRUCT_TM
AC_MSG_CHECKING(for socklen_t)
AC_TRY_COMPILE([
#include <sys/socket.h>
socklen_t x;
],[
],[
AC_MSG_RESULT(yes)
],[
AC_TRY_COMPILE([
#include <sys/socket.h>
int accept (int, struct sockaddr *, size_t *);
],[
],[
AC_MSG_RESULT(size_t)
AC_DEFINE(socklen_t,size_t)
],[
AC_MSG_RESULT(int)
AC_DEFINE(socklen_t,int)
])
])

AC_C_BIGENDIAN

VBA_LIBS="../gb/libgb.a"

if test "x$enable_c_core" = xyes; then
  CXXFLAGS="$CXXFLAGS -DC_CORE"
fi

if test "x$enable_profiling" = xyes; then
  CXXFLAGS="$CXXFLAGS -DPROFILING"
  VBA_SRC_EXTRA="$VBA_SRC_EXTRA prof"
  VBA_LIBS="$VBA_LIBS ../prof/libprof.a"
fi

if test "x$with_mmx" = xyes; then
  if test "x$NASM" = x; then
    AC_MSG_ERROR([*** Couldn't find nasm program.])
  fi
  CXXFLAGS="$CXXFLAGS -DMMX"
  VBA_SRC_EXTRA="$VBA_SRC_EXTRA i386"
  VBA_LIBS="$VBA_LIBS ../i386/lib386.a"
fi

if test "x$enable_sdl" = xyes; then
  VBA_SRC_EXTRA="$VBA_SRC_EXTRA sdl"
fi

if test "x$enable_gtk" = xyes; then
  dnl Internationalisation support.
  ALL_LINGUAS="fr"
  AM_GNU_GETTEXT(external)
  AC_CHECK_HEADERS(libintl.h)
  GETTEXT_PACKAGE=vba-1.7.2
  AC_SUBST(GETTEXT_PACKAGE)

  PKG_CHECK_MODULES(GTKMM, gtkmm-$gtk_version >= 2.0.0 libglademm-$gtk_version >= 2.1.0)
  AC_SUBST(GTKMM_CFLAGS)
  AC_SUBST(GTKMM_LIBS)
  if test "x$gtk_version" = x2.0; then
    GTKMM_CPPFLAGS="-DGTKMM20"
  fi
  AC_SUBST(GTKMM_CPPFLAGS)
  VBA_EXTRA="$VBA_EXTRA po"
  VBA_SRC_EXTRA="$VBA_SRC_EXTRA gtk"
fi

if test "x$enable_dev" = xyes; then
  CXXFLAGS="$CXXFLAGS -DDEV_VERSION"
fi

if test "x$enable_sdl" != xyes && test "x$enable_gtk" != xyes; then
  AC_MSG_WARN([*** You have chosen not to build any frontend.])
fi

AC_SUBST(VBA_EXTRA)
AC_SUBST(VBA_SRC_EXTRA)
AC_SUBST(VBA_LIBS)

dnl Generated files.
AC_CONFIG_FILES([
Makefile
m4/Makefile
po/Makefile.in
src/Makefile
src/gb/Makefile
src/gtk/Makefile
src/gtk/images/Makefile
src/i386/Makefile
src/prof/Makefile
src/sdl/Makefile
win32/Makefile
])
AC_OUTPUT