dnl - Process this file with autoconf to produce a configure script.
dnl
dnl Copyright (C) 1997-2007 ZSNES Team ( zsKnight, _Demo_, pagefault, Nach )
dnl 
dnl http://www.zsnes.com
dnl http://sourceforge.net/projects/zsnes
dnl https://zsnes.bountysource.com
dnl 
dnl This program is free software; you can redistribute it and/or
dnl modify it under the terms of the GNU General Public License
dnl version 2 as published by the Free Software Foundation.
dnl 
dnl This program is distributed in the hope that it will be useful,
dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dnl GNU General Public License for more details.
dnl 
dnl You should have received a copy of the GNU General Public License
dnl along with this program; if not, write to the Free Software
dnl Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

AC_INIT([zsnes], [], [http://www.zsnes.com])
AC_PREREQ(2.50)
AC_CONFIG_SRCDIR(init.asm)
AC_CONFIG_HEADER(config.h)

VERSION=1.51

CFLAGS="$CFLAGS -pipe -I. -I/usr/local/include -I/usr/include"
dnl - You might wanna change -I/usr/include if you're trying to do this with
dnl Mingw, because it doesnt have a standard file tree. Maybe if ming do this
dnl else do -I/usr/include. Or some shit. You figure it out.

LDFLAGS="$LDFLAGS -L/usr/local/lib -L/usr/lib"
dnl - Same thing here.

dnl - Where are our compilers, and who are we compiling for?
AC_CANONICAL_TARGET
AC_PROG_INSTALL
AC_PROG_CC
AC_PROG_CXX

AC_ARG_WITH(nasm-prefix,
  [  --with-nasm-prefix=PFX  Prefix where nasm is installed (optional)],
  nasm_prefix="$withval",
  nasm_prefix="")
AC_CHECK_TOOL(NASMPATH,nasm,"no",$nasm_prefix:$PATH)
if test x$NASMPATH = xno; then
  AC_MSG_ERROR(You need NASM installed to compile ZSNES)
fi

AC_ARG_WITH(zconf-path,
  [  --with-zconf-path=PATH  Path where ZSNES looks for zsnesl.cfg (default is .zsnes)],
  ZCONF="$withval",
  ZCONF="")

CFLAGS="$CFLAGS -D__UNIXSDL__"
NFLAGS="$NFLAGS -w-orphan-labels -D__UNIXSDL__"
PSRFLAGS="$PSRFLAGS -D__UNIXSDL__"
MMLIB_FILES="\$(MMLIB_D)/mm.o"
STRIP="-s"
case x$target in
  *-*-linux*)
    NFLAGS="$NFLAGS -f elf -DELF"
    MMLIB_FILES="$MMLIB_FILES \$(MMLIB_D)/linux.o"
    ;;
  *-*-beos*)
    CFLAGS="$CFLAGS -D__BEOS__"
    NFLAGS="$NFLAGS -D__BEOS__ -f elf -DELF"
    ;;
  *-*-*bsd*)
    CFLAGS="$CFLAGS -D__BSDSDL__"
    NFLAGS="$NFLAGS -D__BSDSDL__ -f elf -DELF"
    ;;
  *-*-darwin* )
    CFLAGS="$CFLAGS -D__MACOSX__ -D__OPENGL__ -arch i386 -mdynamic-no-pic -mstackrealign -fno-unit-at-a-time"
    NFLAGS="$NFLAGS -D__MACOSX__ -D__OPENGL__ -f macho -DMACHO"
    PSRFLAGS="$PSRFLAGS -D__OPENGL__"
    MMLIB_FILES="$MMLIB_FILES \$(MMLIB_D)/osx.o"
    STRIP=""
    LDFLAGS="$LDFLAGS -framework Carbon -framework IOKit -framework Foundation -framework OpenGL -mdynamic-no-pic -read_only_relocs suppress -undefined dynamic_lookup"
    GL_DRAW="\$(PORT_D)/gl_draw.o"
    found_opengl=yes
    ;;
  *-*-cygwin* | *-*-*ming*)
    NFLAGS="$NFLAGS -f win32"
    ;;
  *)
    AC_MSG_ERROR(This target is not supported)
    ;;
esac

if test x$ZCONF != x; then
  CFLAGS="$CFLAGS -DZCONF=\"\\\"$ZCONF\\\"\""
fi

dnl - Library checks (requisites marked *, others optionnal):
dnl - libSDL*, zlib*, libpng, curses, libao, OpenGL, JMAlib

dnl - libsdl stuff
AM_PATH_SDL(1.2.0,,[AC_MSG_ERROR(SDL >= 1.2.0 is required)])

dnl - zlib stuff
AM_PATH_ZLIB(1.2.3,,[AC_MSG_ERROR(zlib >= 1.2.3 is required)])
CFLAGS="$CFLAGS $ZLIB_CFLAGS"
LDFLAGS="$LDFLAGS $ZLIB_LIBS"

dnl - libpng stuff
AM_PATH_LIBPNG(1.2.0,,)
CFLAGS="$CFLAGS $SDL_CFLAGS"
LDFLAGS="$LDFLAGS $SDL_LIBS"

if test x$with_libpng != xyes; then
  CFLAGS="$CFLAGS -DNO_PNG"
  NFLAGS="$NFLAGS -DNO_PNG"
  PSRFLAGS="$PSRFLAGS -DNO_PNG"
else
  CFLAGS="$CFLAGS $LIBPNG_CFLAGS"
  LDFLAGS="$LDFLAGS $LIBPNG_LIBS"
fi

dnl - debugger stuff
AC_ARG_ENABLE(debugger,
  [  --disable-debugger      Build without the zsnes debugger],
  ,
  enable_debugger=yes)
AC_MSG_CHECKING(if you want the zsnes debugger)
AC_MSG_RESULT($enable_debugger)
if test x$enable_debugger != xyes; then
  CFLAGS="$CFLAGS -DNO_DEBUGGER"
  NFLAGS="$NFLAGS -DNO_DEBUGGER"
  PSRFLAGS="$PSRFLAGS -DNO_DEBUGGER"
  DEBUGGER_FILES=""
else
  DEBUGGER_FILES="debugasm.o debugger.o"

  AC_CHECK_LIB(curses, initscr, found_curses=yes,found_curses=no)
  AC_CHECK_LIB(ncurses, initscr, found_ncurses=yes,found_ncurses=no)
  AC_CHECK_LIB(pdcurses, initscr, found_pdcurses=yes,found_pdcurses=no)

  if test x$found_curses = xyes; then
    LDFLAGS="$LDFLAGS -lcurses"
  else
    if test x$found_ncurses = xyes; then
      CFLAGS="$CFLAGS -DNCURSES"
      LDFLAGS="$LDFLAGS -lncurses"
    else
      if test x$found_pdcurses = xyes; then
        LDFLAGS="$LDFLAGS -lpdcurses"
      else
        AC_MSG_ERROR(A curses library is required to use the debugger)
        DEBUGGER_FILES=""
      fi
    fi
  fi
fi

dnl - libao stuff
AC_ARG_ENABLE(libao,
  [  --enable-libao          Build with libao support],
    want_ao=$enableval,
    want_ao=no)
AC_MSG_CHECKING(if you want libao support)
AC_MSG_RESULT($want_ao)
if test x$want_ao = xyes; then
  AC_CHECK_LIB(ao, ao_initialize, got_ao=yes, got_ao=no)

  if test x$got_ao = xyes; then
    CFLAGS="$CFLAGS -D__LIBAO__"
    LDFLAGS="$LDFLAGS -lao"
  else
    AC_MSG_ERROR(Couldn't find libao)
  fi
fi

dnl - opengl stuff
if test x$found_opengl != xyes; then
  AC_ARG_ENABLE(opengl,
    [  --disable-opengl        Build without OpenGL support],
    ,
    enable_opengl=yes)
  AC_ARG_WITH(
    opengl-prefix,
    [  --with-opengl-prefix=PFX Prefix where OpenGL is installed (optional)],
    opengl_prefix="$withval",
    opengl_prefix="")
  if test x$enable_opengl != xno; then
    AC_PATH_X
    if test x$opengl_prefix != x; then
      CFLAGS="$CFLAGS -I$opengl_prefix/include"
      LDFLAGS-"$LDFLAGS -L$opengl_prefix/lib"
    fi
    if test x$x_libraries != x; then
      LDFLAGS="$LDFLAGS -L$x_libraries"
    fi
    AC_CHECK_LIB(GL, glGetError, found_opengl=yes,found_opengl=no)
  fi

  AC_MSG_CHECKING(for OpenGL)
  if test x$enable_opengl != xno; then
    if test x$found_opengl = xyes; then
      LDFLAGS="$LDFLAGS -lGL"
      CFLAGS="$CFLAGS -D__OPENGL__"
      NFLAGS="$NFLAGS -D__OPENGL__"
      PSRFLAGS="$PSRFLAGS -D__OPENGL__"
      GL_DRAW="\$(PORT_D)/gl_draw.o"
    fi
    AC_MSG_RESULT($found_opengl)
  else
    AC_MSG_RESULT(disabled by user)
  fi
fi

dnl - jma stuff
AC_ARG_ENABLE(jma,
  [  --disable-jma           Build without JMA support],
  ,
  enable_jma=yes)
AC_MSG_CHECKING(for JMA support)
AC_MSG_RESULT($enable_jma)
if test x$enable_jma != xyes; then
  CFLAGS="$CFLAGS -DNO_JMA"
  NFLAGS="$NFLAGS -DNO_JMA"
  PSRFLAGS="$PSRFLAGS -DNO_JMA"
fi

dnl - Various debug and optimization option checks
AM_ARCH_DETECT()

AC_ARG_ENABLE(debug,
  [  --enable-debug          Build GDB friendly binary (zsnesd)],
  debug=$enableval,
  debug=no)
AC_MSG_CHECKING(if you want gdb friendly executable)
AC_MSG_RESULT($debug)
if test x$debug = xyes; then
dnl - It is easier to debug zsnes with no optimization enabled.
  ZSNESEXE="zsnesd"
  PSR_TEMP=""
  CFLAGS="$CFLAGS -Wall -W -DDEBUG -O0 -fno-omit-frame-pointer -gstabs3"
  NFLAGS="$NFLAGS -DDEBUG -g -F stabs -s -O0"
else
  ZSNESEXE="zsnes"
  PSR_TEMP="	rm -f t_\$*.c"

  CFLAGSBAK="$CFLAGS"
  AC_ARG_VAR(force_arch, [Force architecture to optimize GCC/G++ for])
  AC_MSG_CHECKING(which cpu architecture to optimize for)
  if test x$force_arch != x; then
    CFLAGS="$CFLAGS -march=$force_arch"
    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]])],
    [AC_MSG_RESULT(forcing $force_arch)],
    [AC_MSG_RESULT($force_arch ??)
    AC_MSG_WARN(incorrect force_arch parameter)
    force_arch=""
    CFLAGS="$CFLAGSBAK"
    AC_MSG_CHECKING(for autodetected architecture)])
  else
    if test x$ARCH_INFO = x; then
      case x$target in
        i686-*-*)
          AC_MSG_RESULT(guessing i686)
          CFLAGS="$CFLAGS -march=i686"
          ;;
        i586-*-*)
          AC_MSG_RESULT(guessing i586)
          CFLAGS="$CFLAGS -march=i586"
          ;;
        i486-*-*)
          AC_MSG_RESULT(guessing i486)
          CFLAGS="$CFLAGS -march=i486"
          ;;
        *)
          AC_MSG_RESULT(guessing i386)
          CFLAGS="$CFLAGS -march=i386"
          AC_MSG_WARN([This is not what you want, use --target or force-arch])
          ;;
      esac
    else
      AC_MSG_RESULT($ARCH_INFO)
      CFLAGS="$CFLAGS -march=$ARCH_INFO"
    fi
  fi

  AC_ARG_ENABLE(release,
    [  --enable-release        Build ultra-optimized release binary (zsnes)],
    release=$enableval,
    release=no)
  AC_MSG_CHECKING(if you want crazy optimizations)
  AC_MSG_RESULT($release)
  if test x$release = xyes; then
    if test x$force_arch = x; then
      AC_MSG_WARN([If you intend to distribute this binary, make sure you use force_arch and set to i586 (or whichever CPU Arch you intend for)])
    fi
    CFLAGS="$CFLAGS -O3 -fomit-frame-pointer -fprefetch-loop-arrays -fforce-addr $STRIP -D__RELEASE__"
    NFLAGS="$NFLAGS -O99999999 -D__RELEASE__"
  else
    CFLAGS="$CFLAGS -O3 -fomit-frame-pointer $STRIP"
    NFLAGS="$NFLAGS -O1"
  fi
fi
CXXFLAGS="$CFLAGS -fno-rtti"

if test x$enable_jma != xno; then
  JMA_FILES="\$(JMA_D)/7zlzma.o \$(JMA_D)/crc32.o \$(JMA_D)/iiostrm.o\
	\$(JMA_D)/inbyte.o \$(JMA_D)/jma.o \$(JMA_D)/lzma.o\
	\$(JMA_D)/lzmadec.o \$(JMA_D)/winout.o \$(JMA_D)/zsnesjma.o"
  ZC="$CXX"
  ZCFLAGS="$CXXFLAGS"
else
  JMA_FILES=""
  ZC="$CC"
  ZCFLAGS="$CFLAGS"
fi

dnl - Check for header files
AC_HEADER_STDC
AC_HEADER_MAJOR

dnl - Create Makefile
AC_SUBST(VERSION)
AC_SUBST(NASMPATH)
AC_SUBST(ZC)
AC_SUBST(ZCFLAGS)
AC_SUBST(PSRFLAGS)
AC_SUBST(NFLAGS)
AC_SUBST(ZSNESEXE)
AC_SUBST(GL_DRAW)
AC_SUBST(JMA_FILES)
AC_SUBST(MMLIB_FILES)
AC_SUBST(DEBUGGER_FILES)
AC_SUBST(PSR_TEMP)

AC_CONFIG_FILES(Makefile)
touch -t 198001010000 makefile.dep

AC_OUTPUT

dnl - Info output
echo
echo
echo ZSNES v$VERSION
echo
echo "SDL support                   Version `$SDL_CONFIG --version`"
echo "NASM support                  `nasm -v`"
echo "zlib support                  Version $ZLIB_VERSION"
echo $ECHO_N "PNG support                   $ECHO_C"
if test x$with_libpng = xyes; then
  echo "${ECHO_T}Yes, version $LIBPNG_VERSION"
elif test x$enable_libpng = xno; then
  echo "${ECHO_T}Disabled by user"
else
  echo "${ECHO_T}Disabled, library not found"
fi
echo $ECHO_N "OpenGL support                $ECHO_C"
if test x$found_opengl = xyes; then
  echo "${ECHO_T}Yes"
elif test x$enable_opengl = xno; then
  echo "${ECHO_T}Disabled by user"
else
  echo "${ECHO_T}Disabled, library not found"
fi
echo $ECHO_N "JMA support                   $ECHO_C"
if test x$enable_jma = xno; then
  echo "${ECHO_T}Disabled by user"
else
  echo "${ECHO_T}Yes"
fi
echo $ECHO_N "ZSNES debugger                $ECHO_C"
if test x$enable_debugger = xno; then
  echo "${ECHO_T}Disabled"
else
  echo "${ECHO_T}Enabled"
fi
echo
echo The binary will be installed in $prefix/bin
echo
echo $ECHO_N "Configure complete, now type '$ECHO_C"
case x$target in
  *-*-*bsd*)
    echo $ECHO_N "${ECHO_T}g$ECHO_C"
    ;;
  *)
    ;;
esac
echo "${ECHO_T}make' and pray."
echo
