dnl Process this file with autoconf to produce a configure script.
dnl Written in 1999 by Christian Bauer et al.

AC_INIT(main_unix.cpp)
AC_PREREQ(2.12)
AC_CONFIG_HEADER(config.h)

dnl Options.
AC_ARG_ENABLE(xf86-dga,      [  --enable-xf86-dga       use the XFree86 DGA extension [default=yes]], [WANT_XF86_DGA=$enableval], [WANT_XF86_DGA=yes])
AC_ARG_ENABLE(xf86-vidmode,  [  --enable-xf86-vidmode   use the XFree86 VidMode extension [default=yes]], [WANT_XF86_VIDMODE=$enableval], [WANT_XF86_VIDMODE=yes])
AC_ARG_ENABLE(fbdev-dga,     [  --enable-fbdev-dga      use direct frame buffer access via /dev/fb [default=yes]], [WANT_FBDEV_DGA=$enableval], [WANT_FBDEV_DGA=yes])
AC_ARG_WITH(esd,             [  --with-esd              support ESD for sound under Linux/FreeBSD [default=yes]], [WANT_ESD=$withval], [WANT_ESD=yes])
AC_ARG_WITH(gtk,             [  --with-gtk              use GTK user interface [default=yes]], [WANT_GTK=$withval], [WANT_GTK=yes])
AC_ARG_WITH(mon,             [  --with-mon              use mon as debugger [default=yes]], [WANT_MON=$withval], [WANT_MON=yes])

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CPP
AC_PROG_CXX
AC_PROG_MAKE_SET
AC_PROG_INSTALL

dnl We use mon if possible.
MONSRCS=
AC_MSG_CHECKING(for mon)
if grep mon_init ../../../mon/src/mon.h >/dev/null 2>/dev/null; then
  AC_MSG_RESULT(yes)
  WANT_MON=yes
  DEFINES="$DEFINES -DENABLE_MON=1"
  MONSRCS="../../../mon/src/mon.cpp ../../../mon/src/mon_6502.cpp ../../../mon/src/mon_68k.cpp ../../../mon/src/mon_8080.cpp ../../../mon/src/mon_cmd.cpp ../../../mon/src/mon_ppc.cpp ../../../mon/src/mon_x86.cpp"
  CXXFLAGS="$CXXFLAGS -I../../../mon/src"
  AC_CHECK_LIB(readline, readline)
  AC_CHECK_HEADERS(readline/readline.h readline/history.h)
else
  AC_MSG_RESULT(no)
  WANT_MON=no
  DEFINES="$DEFINES -DENABLE_MON=0"
fi

dnl Checks for libraries.
AC_CHECK_LIB(posix4, sem_init)

dnl We need X11.
AC_PATH_XTRA
if [[ "x$no_x" = "xyes" ]]; then
  AC_MSG_ERROR([You need X11 to run Basilisk II.])
fi
CFLAGS="$CFLAGS $X_CFLAGS"
CXXFLAGS="$CXXFLAGS $X_CFLAGS"
LIBS="$LIBS $X_PRE_LIBS $X_LIBS -lX11 -lXext $X_EXTRA_LIBS"

dnl We need pthreads. Try libpthread first, then libc_r (FreeBSD), then PTL.
AC_CHECK_LIB(pthread, pthread_create, , [
  AC_CHECK_LIB(c_r, pthread_create, , [
    AC_CHECK_LIB(PTL, pthread_create, , [
      AC_MSG_ERROR([You need pthreads to run Basilisk II.])
    ])
  ])
])

dnl If POSIX.4 semaphores are not available, we emulate them with pthread mutexes.
SEMSRCS=
AC_CHECK_FUNCS(sem_init, , [
  SEMSRCS=posix_sem.cpp
])

dnl We use DGA (XFree86 or fbdev) if possible.
if [[ "x$WANT_XF86_DGA" = "xyes" ]]; then
  AC_CHECK_LIB(Xxf86dga, XF86DGAQueryExtension, [
    DEFINES="$DEFINES -DENABLE_XF86_DGA=1"
    LIBS="$LIBS -lXxf86dga"
    if [[ "x$WANT_FBDEV_DGA" = "xyes" ]]; then
      AC_MSG_WARN([Cannot have both --enable-xf86-dga and --enable-fbdev-dga, ignoring --enable-fbdev-dga.])
      WANT_FBDEV_DGA=no
    fi
  ], [
    AC_MSG_WARN([Could not find XFree86 DGA extension, ignoring --enable-xf86-dga.])
    WANT_XF86_DGA=no
    DEFINES="$DEFINES -DENABLE_XF86_DGA=0"
  ])
else
  DEFINES="$DEFINES -DENABLE_XF86_DGA=0"
fi
if [[ "x$WANT_FBDEV_DGA" = "xyes" ]]; then
  DEFINES="$DEFINES -DENABLE_FBDEV_DGA=1"
else
  DEFINES="$DEFINES -DENABLE_FBDEV_DGA=0"
fi

dnl We use XFree86 VidMode if possible.
if [[ "x$WANT_XF86_VIDMODE" = "xyes" ]]; then
  AC_CHECK_LIB(Xxf86vm, XF86VidModeQueryExtension, [
    DEFINES="$DEFINES -DENABLE_XF86_VIDMODE=1"
    LIBS="$LIBS -lXxf86vm"
  ], [
    AC_MSG_WARN([Could not find XFree86 VidMode extension, ignoring --enable-xf86-vidmode.])
    WANT_XF86_VIDMODE=no
    DEFINES="$DEFINES -DENABLE_XF86_VIDMODE=0"
  ])
fi

dnl We use GTK+ if possible.
UISRCS=../dummy/prefs_editor_dummy.cpp
if [[ "x$WANT_GTK" = "xyes" ]]; then
  AM_PATH_GTK(1.2.0, [
    DEFINES="$DEFINES -DENABLE_GTK=1"
    CFLAGS="$CFLAGS $GTK_CFLAGS"
    CXXFLAGS="$CXXFLAGS $GTK_CFLAGS"
    LIBS="$LIBS $GTK_LIBS"
    UISRCS=prefs_editor_gtk.cpp
  ], [
    AC_MSG_WARN([Could not find GTK+, disabling user interface.])
    WANT_GTK=no
    DEFINES="$DEFINES -DENABLE_GTK=0"
  ])
fi

dnl We use ESD if possible.
if [[ "x$WANT_ESD" = "xyes" ]]; then
  AM_PATH_ESD(0.2.8, [
    DEFINES="$DEFINES -DENABLE_ESD=1"
    CFLAGS="$CFLAGS $ESD_CFLAGS"
    CXXFLAGS="$CXXFLAGS $ESD_CFLAGS"
    LIBS="$LIBS $ESD_LIBS"
  ], [
    AC_MSG_WARN([Could not find ESD, disabling ESD support.])
    WANT_ESD=no
    DEFINES="$DEFINES -DENABLE_ESD=0"
  ])
fi

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(unistd.h fcntl.h sys/time.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_BIGENDIAN
AC_C_CONST
AC_C_INLINE
AC_CHECK_SIZEOF(short, 2)
AC_CHECK_SIZEOF(int, 4)
AC_CHECK_SIZEOF(long, 4)
AC_CHECK_SIZEOF(long long, 8)
AC_TYPE_OFF_T
AC_CHECK_TYPE(loff_t, off_t)
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_STRUCT_TM

dnl Checks for library functions.
AC_CHECK_FUNCS(strdup cfmakeraw)
AC_CHECK_FUNCS(nanosleep clock_gettime timer_create)
AC_CHECK_FUNCS(pthread_cancel)

dnl Select system-dependant source files.
SYSSRCS="../dummy/ether_dummy.cpp ../dummy/scsi_dummy.cpp ../dummy/audio_dummy.cpp"
if MACHINE=`uname -a 2>/dev/null`; then
  case "$MACHINE" in
  Linux*)
    SYSSRCS="Linux/ether_linux.cpp Linux/scsi_linux.cpp audio_oss_esd.cpp"
    ;;
  FreeBSD*3.*)
    dnl Check for the CAM library
    AC_CHECK_LIB(cam, cam_open_btl, HAVE_LIBCAM=yes, HAVE_LIBCAM=no) 
    if [[ "x$HAVE_LIBCAM" = "xno" ]]; then
      AC_MSG_ERROR([Cannot find libcam for SCSI management.])
    else
      dnl Check for the sys kernel includes
      AC_CHECK_HEADER(/sys/cam/cam.h)
      if [[ "x$ac_cv_header__sys_cam_cam_h" = "xno" ]]; then
        dnl In this case I should fix this thing including a "patch"
        dnl to access directly to the functions in the kernel :) --Orlando
        AC_MSG_ERROR([Cannot find kernel includes for CAM library.])
      fi
      SYSSRCS="../dummy/ether_dummy.cpp FreeBSD/scsi_freebsd.cpp audio_oss_esd.cpp"
      CXXFLAGS="$CXXFLAGS -I/sys"
      CFLAGS="$CFLAGS -I/sys"
      LIBS="$LIBS -lcam"
      DEFINES="$DEFINES -DBSD_COMP -DCAM"
    fi
    ;;
  FreeBSD*)
    dnl Check for the SCSI library
    AC_CHECK_LIB(scsi, scsi_open, HAVE_LIBSCSI=yes, HAVE_LIBSCSI=no)
    if [[ "x$HAVE_LIBSCSI" = "xno" ]]; then
      AC_MSG_ERROR([Cannot find libscsi for SCSI management.])
    else
      dnl Check for the sys kernel includes
      AC_CHECK_HEADER(scsi.h sys/scsiio.h)
      if [[ "x$ac_cv_header_scsi_h" = "xno" ]]; then
        AC_MSG_ERROR([Cannot find includes for the SCSI library.])
      fi
      SYSSRCS="../dummy/ether_dummy.cpp FreeBSD/scsi_freebsd.cpp ../dummy/audio_dummy.cpp"
      LIBS="$LIBS -lscsi"
      DEFINES="$DEFINES -DBSD_COMP"
    fi
    ;;
  SunOS*)
    SYSSRCS="../dummy/ether_dummy.cpp ../dummy/scsi_dummy.cpp Solaris/audio_solaris.cpp"
    DEFINES="$DEFINES -DBSD_COMP -D_POSIX_PTHREAD_SEMANTICS"
    ;;
  IRIX*)
    SYSSRCS="../dummy/ether_dummy.cpp ../dummy/scsi_dummy.cpp ../dummy/audio_dummy.cpp Irix/unaligned.c"
    DEFINES="$DEFINES -DCRTSCTS=CNEW_RTSCTS -DB230400=B115200"
    LIBS="$LIBS -lm"
    ;;
  esac
fi
SYSSRCS="$SYSSRCS $SEMSRCS $UISRCS $MONSRCS"

dnl Check for i386 CPU.
HAVE_I386=no
AC_MSG_CHECKING(for x86 target CPU)
AC_EGREP_CPP(yes,
[
#ifdef __i386__
  yes
#endif
], [AC_MSG_RESULT(yes); HAVE_I386=yes], AC_MSG_RESULT(no))

dnl Check for SPARC CPU.
HAVE_SPARC=no
AC_MSG_CHECKING(for SPARC target CPU)
AC_EGREP_CPP(yes,
[
#ifdef __sparc__
  yes
#endif
], [AC_MSG_RESULT(yes); HAVE_SPARC=yes], AC_MSG_RESULT(no))

dnl Check for GAS.
HAVE_GAS=no
AC_MSG_CHECKING(for GAS .p2align feature)
cat >conftest.S << EOF
	.text
	.p2align 5
EOF
if $CC conftest.S -c -o conftest.o >/dev/null 2>&1 ; then HAVE_GAS=yes; fi
AC_MSG_RESULT($HAVE_GAS)

dnl Check for GCC 2.7 or higher.
HAVE_GCC27=no
AC_MSG_CHECKING(for GCC 2.7 or higher)
AC_EGREP_CPP(yes,
[#if __GNUC__ - 1 > 1 || __GNUC_MINOR__ - 1 > 5
  yes
#endif
], [AC_MSG_RESULT(yes); HAVE_GCC27=yes], AC_MSG_RESULT(no))

dnl Set "-fomit-frame-pointer" on GCC 2.7 or higher.
if [[ "x$HAVE_GCC27" = "xyes" ]]; then
  CFLAGS="$CFLAGS -fomit-frame-pointer"
  CXXFLAGS="$CXXFLAGS -fomit-frame-pointer"
fi

dnl Select appropriate CPU source and REGPARAM define.
WANT_X86_ASSEMBLY=no
WANT_SPARC_V8_ASSEMBLY=no
WANT_SPARC_V9_ASSEMBLY=no
CPUSRCS="cpuemu1.cpp cpuemu2.cpp cpuemu3.cpp cpuemu4.cpp cpuemu5.cpp cpuemu6.cpp cpuemu7.cpp cpuemu8.cpp"
if [[ "x$HAVE_GCC27" = "xyes" -a "x$HAVE_I386" = "xyes" ]]; then
  dnl i386 CPU
  DEFINES="$DEFINES -DREGPARAM=\"__attribute__((regparm(3)))\""
  if [[ "x$HAVE_GAS" = "xyes" ]]; then
    WANT_X86_ASSEMBLY=yes
    DEFINES="$DEFINES -DX86_ASSEMBLY"
    CPUSRCS="../uae_cpu/compiler.cpp cpufast1.s cpufast2.s cpufast3.s cpufast4.s cpufast5.s cpufast6.s cpufast7.s cpufast8.s"
  fi
elif [[ "x$HAVE_GCC27" = "xyes" -a "x$HAVE_SPARC" = "xyes" -a "x$HAVE_GAS" = "xyes" ]]; then
  dnl SPARC CPU
  case "$MACHINE" in
  SunOS*)
    AC_MSG_CHECKING(SPARC CPU architecture)
    SPARC_TYPE=`Solaris/which_sparc`
    AC_MSG_RESULT($SPARC_TYPE)
    case "$SPARC_TYPE" in
    SPARC_V8)
      WANT_SPARC_V8_ASSEMBLY=yes
      DEFINES="$DEFINES -DSPARC_V8_ASSEMBLY"
      CFLAGS="$CFLAGS -Wa,-Av8"
      CXXFLAGS="$CXXFLAGS -Wa,-Av8"
      ;;
    SPARC_V9)
      WANT_SPARC_V9_ASSEMBLY=yes
      DEFINES="$DEFINES -DSPARC_V9_ASSEMBLY"
      CFLAGS="$CFLAGS -Wa,-Av9"
      CXXFLAGS="$CXXFLAGS -Wa,-Av9"
      ;;
    esac
    ;;
  esac
fi

dnl UAE CPU sources for all architectures.
CPUINCLUDES="-I../uae_cpu"
CPUSRCS="../uae_cpu/basilisk_glue.cpp ../uae_cpu/memory.cpp ../uae_cpu/newcpu.cpp ../uae_cpu/readcpu.cpp ../uae_cpu/fpp.cpp cpustbl.cpp cpudefs.cpp $CPUSRCS"

dnl Generate Makefile.
AC_SUBST(DEFINES)
AC_SUBST(SYSSRCS)
AC_SUBST(CPUINCLUDES)
AC_SUBST(CPUSRCS)
AC_OUTPUT(Makefile)

dnl Print summary.
echo
echo Basilisk II configuration summary:
echo
echo XFree86 DGA support .............. : $WANT_XF86_DGA
echo XFree86 VidMode support .......... : $WANT_XF86_VIDMODE
echo fbdev DGA support ................ : $WANT_FBDEV_DGA
echo ESD sound support ................ : $WANT_ESD
echo GTK user interface ............... : $WANT_GTK
echo mon debugger support ............. : $WANT_MON
echo i386 assembly optimizations ...... : $WANT_X86_ASSEMBLY
echo SPARC V8 assembly optimizations .. : $WANT_SPARC_V8_ASSEMBLY
echo SPARC V9 assembly optimizations .. : $WANT_SPARC_V9_ASSEMBLY
echo
echo "Configuration done. Now type \"make\"."
