	dnl
dnl portaudio V19 configure.in script
dnl
dnl Dominic Mazzoni, Arve Knudsen
dnl

dnl Require autoconf >= 2.13
AC_PREREQ(2.13)

dnl Init autoconf and make sure configure is being called
dnl from the right directory
AC_INIT([include/portaudio.h])

dnl Specify options

AC_ARG_WITH(alsa, 
            [  --with-alsa (default=yes)],
            with_alsa=$withval, with_alsa="yes")

AC_ARG_WITH(jack, 
            [  --with-jack (default=yes)],
            with_jack=$withval, with_jack="yes")

AC_ARG_WITH(oss, 
            [  --with-oss (default=yes)],
            with_oss=$withval, with_oss="yes")

AC_ARG_WITH(asihpi,
            [  --with-asihpi (default=auto)],
            with_asihpi=$withval, with_asihpi="yes")

AC_ARG_WITH(host_os, 
            [  --with-host_os (no default)],
            host_os=$withval)

AC_ARG_WITH(winapi,
            [  --with-winapi ((wmme/directx/asio) default=wmme)],
            with_winapi=$withval, with_winapi="wmme")

dnl Mac API added for ASIO, can have other api's listed
AC_ARG_WITH(macapi,
            [  --with-macapi ((asio/core/sm) default=core)],
            with_macapi=$withval, with_macapi="core")

AC_ARG_WITH(asiodir,
            [  --with-asiodir (default=/usr/local/asiosdk2)],
            with_asiodir=$withval, with_asiodir="/usr/local/asiosdk2")

AC_ARG_WITH(dxdir,
            [  --with-dxdir (default=/usr/local/dx7sdk)],
            with_dxdir=$withval, with_dxdir="/usr/local/dx7sdk")

AC_ARG_ENABLE(debug-output,
	[  --enable-debug-output],
        [if test x$enableval != xno ; then
	     AC_DEFINE(PA_ENABLE_DEBUG_OUTPUT,,[Enable debugging messages])
          fi
        ])

AC_ARG_ENABLE(cxx,
              [  --enable-cxx (default=no)],
              enable_cxx=$enableval, enable_cxx="no")

dnl Checks for programs.

AC_PROG_CC
AC_LIBTOOL_WIN32_DLL
AC_PROG_LIBTOOL
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PATH_PROG(AR, ar, no)
if [[ $AR = "no" ]] ; then
    AC_MSG_ERROR("Could not find ar - needed to create a library");
fi

dnl This must be one of the first tests we do or it will fail...
AC_C_BIGENDIAN

dnl checks for various host APIs and arguments to configure that
dnl turn them on or off

AC_CHECK_LIB(asound, snd_pcm_open, have_alsa=yes, have_alsa=no)
AC_CHECK_LIB(hpi, HPI_SubSysCreate, have_asihpi=yes, have_asihpi=no, -lm)

dnl Determine the host description for the subsequent test.
dnl PKG_CHECK_MODULES seems to check and set the host variable also, but
dnl that then requires pkg-config availability which is not standard on
dnl MinGW systems and can be a pain to install.
dnl AC_CANONICAL_HOST

PKG_CHECK_MODULES(JACK, jack, have_jack=yes, have_jack=no)

dnl checks for header files
AC_CHECK_HEADERS([sys/soundcard.h linux/soundcard.h machine/soundcard.h])

dnl sizeof checks: we will need a 16-bit and a 32-bit type

AC_CHECK_SIZEOF(short)
AC_CHECK_SIZEOF(int)
AC_CHECK_SIZEOF(long)

save_LIBS="${LIBS}"
AC_CHECK_LIB(rt, clock_gettime, [rt_libs=" -lrt"])
LIBS="${LIBS}${rt_libs}"
DLL_LIBS="${DLL_LIBS}${rt_libs}"
AC_CHECK_FUNCS([clock_gettime nanosleep])
LIBS="${save_LIBS}"

dnl LT_RELEASE=19
LT_CURRENT=2
LT_REVISION=0
LT_AGE=0

dnl AC_SUBST(LT_RELEASE)
AC_SUBST(LT_CURRENT)
AC_SUBST(LT_REVISION)
AC_SUBST(LT_AGE)

dnl extra variables
AC_SUBST(OTHER_OBJS)
AC_SUBST(PADLL)
AC_SUBST(SHARED_FLAGS)
AC_SUBST(THREAD_CFLAGS)
AC_SUBST(DLL_LIBS)
AC_SUBST(CXXFLAGS)
AC_SUBST(NASM)
AC_SUBST(NASMOPT)

CFLAGS=${CFLAGS:-"-g -O2 -Wall -pedantic -pipe -fPIC"}

if [[ $ac_cv_c_bigendian = "yes" ]] ; then
   CFLAGS="$CFLAGS -DPA_BIG_ENDIAN"
else
   CFLAGS="$CFLAGS -DPA_LITTLE_ENDIAN"
fi

case "${host_os}" in
  darwin* )
	dnl Mac OS X configuration

	AC_DEFINE(PA_USE_COREAUDIO)
	CFLAGS="$CFLAGS -arch i386 -arch ppc -isysroot /Developer/SDKs/MacOSX10.4u.sdk -mmacosx-version-min=10.3";
	OTHER_OBJS="src/os/mac_osx/pa_mac_hostapis.o src/os/unix/pa_unix_util.o src/hostapi/coreaudio/pa_mac_core.o src/hostapi/coreaudio/pa_mac_core_utilities.o src/hostapi/coreaudio/pa_mac_core_blocking.o src/common/pa_ringbuffer.o";
	LIBS="-framework CoreAudio -framework AudioToolbox -framework AudioUnit -framework Carbon";
	PADLL="libportaudio.dylib";
	SHARED_FLAGS="-framework CoreAudio -framework AudioToolbox -framework AudioUnit -framework Carbon -dynamiclib  -arch i386 -arch ppc -isysroot /Developer/SDKs/MacOSX10.4u.sdk -mmacosx-version-min=10.3";
        if [[ $with_macapi = "asio" ]] ; then
            if [[ $with_asiodir ]] ; then
              ASIODIR="$with_asiodir";
            else
              ASIODIR="/usr/local/asiosdk2";
            fi
            echo "ASIODIR: $ASIODIR";

            OTHER_OBJS="$CFLAGS pa_asio/iasiothiscallresolver.o $ASIODIR/host/asiodrivers.o $ASIODIR/common/asio.o $ASIODIR/host/mac/asioshlib.o";
            CFLAGS="$CFLAGS -I\$(top_srcdir)/pa_asio -I$ASIDIR/host/mac -I$ASIODIR/common";
            CXXFLAGS="$CFLAGS";
        fi
	;;

  mingw* )
        dnl MingW configuration

        echo "WINAPI: $with_winapi"
        if [[ $with_winapi = "directx" ]] ; then
            if [[ $with_dxdir ]] ; then
              DXDIR="$with_dxdir";
            else
              DXDIR="/usr/local/dx7sdk";
            fi
            echo "DXDIR: $DXDIR"
            OTHER_OBJS="src/hostapi/dsound/pa_win_ds.o src/hostapi/dsound/pa_win_ds_dynlink.o src/os/win/pa_win_hostapis.o src/os/win/pa_win_util.o";
            LIBS="-lwinmm -lm -ldsound -lole32";
            PADLL="portaudio.dll";
	    THREAD_CFLAGS="-mthreads"
            SHARED_FLAGS="-shared";
            DLL_LIBS="${DLL_LIBS} -lwinmm -lm -L./dx7sdk/lib -ldsound -lole32";
            #VC98="\"/c/Program Files/Microsoft Visual Studio/VC98/Include\"";
            #CFLAGS="$CFLAGS -I$VC98 -DPA_NO_WMME -DPA_NO_ASIO";
            CFLAGS="$CFLAGS -I\$(top_srcdir)/include -I$DXDIR/include -DPA_NO_WMME -DPA_NO_ASIO" -DPA_NO_WDMKS;
        elif [[ $with_winapi = "asio" ]] ; then
            if [[ $with_asiodir ]] ; then
              ASIODIR="$with_asiodir";
            else
              ASIODIR="/usr/local/asiosdk2";
            fi
            echo "ASIODIR: $ASIODIR"

            OTHER_OBJS="pa_asio/pa_asio.o src/os/win/pa_win_hostapis.o src/os/win/pa_win_util.o pa_asio/iasiothiscallresolver.o $ASIODIR/common/asio.o $ASIODIR/host/asiodrivers.o $ASIODIR/host/pc/asiolist.o";
            LIBS="-lwinmm -lm -lstdc++ -lole32 -luuid";
            PADLL="portaudio.dll";
	    THREAD_CFLAGS="-mthreads"
            SHARED_FLAGS="-shared";
            DLL_LIBS="${DLL_LIBS} -lwinmm -lm -lstdc++ -lole32 -luuid";
            CFLAGS="$CFLAGS -ffast-math -fomit-frame-pointer -I\$(top_srcdir)/src/common -I\$(top_srcdir)/pa_asio -I$ASIODIR/host/pc -I$ASIODIR/common -I$ASIODIR/host -DPA_NO_WMME -DPA_NO_DS -DPA_NO_WDMKS -DWINDOWS";
            CXXFLAGS="$CFLAGS";
        elif [[ $with_winapi = "wdmks" ]] ; then
            if [[ $with_dxdir ]] ; then
              DXDIR="$with_dxdir";
            else
              DXDIR="/usr/local/dx7sdk";
            fi
            echo "DXDIR: $DXDIR"
            OTHER_OBJS="src/hostapi/wdmks/pa_win_wdmks.o src/os/win/pa_win_hostapis.o src/os/win/pa_win_util.o";
            LIBS="-lwinmm -lm -luuid -lsetupapi -lole32";
            PADLL="portaudio.dll";
	    THREAD_CFLAGS="-mthreads"
            SHARED_FLAGS="-shared";
            DLL_LIBS="${DLL_LIBS} -lwinmm -lm -L./dx7sdk/lib -luuid -lsetupapi -lole32";
            #VC98="\"/c/Program Files/Microsoft Visual Studio/VC98/Include\"";
            #CFLAGS="$CFLAGS -I$VC98 -DPA_NO_WMME -DPA_NO_ASIO";
            CFLAGS="$CFLAGS -I\$(top_srcdir)/src/common -I$DXDIR/include -DPA_NO_WMME -DPA_NO_DS -DPA_NO_ASIO";
        else   # WMME default
            OTHER_OBJS="src/hostapi/wmme/pa_win_wmme.o src/os/win/pa_win_hostapis.o src/os/win/pa_win_util.o";
            LIBS="-lwinmm -lm -lstdc++ -lole32 -luuid";
            PADLL="portaudio.dll";
	    THREAD_CFLAGS="-mthreads"
            SHARED_FLAGS="-shared";
            DLL_LIBS="${DLL_LIBS} -lwinmm";
            CFLAGS="$CFLAGS -I\$(top_srcdir)/src/common -DPA_NO_DS -DPA_NO_ASIO -DPA_NO_WDMKS";
        fi
        ;;

  cygwin* )
	dnl Cygwin configuration

	OTHER_OBJS="src/hostapi/wmme/pa_win_wmme.o src/os/win/pa_win_hostapis.o src/os/win/pa_win_util.o";
	CFLAGS="$CFLAGS -DPA_NO_DS -DPA_NO_WDMKS -DPA_NO_ASIO -DPA_NO_WASAPI"
	LIBS="-lwinmm -lm";
	PADLL="portaudio.dll";
	THREAD_CFLAGS="-mthreads"
	SHARED_FLAGS="-shared";
	DLL_LIBS="${DLL_LIBS} -lwinmm";
	;;

  irix* )
	dnl SGI IRIX audio library (AL) configuration (Pieter, oct 2-13, 2003).
	dnl The 'dmedia' library is needed to read the Unadjusted System Time (UST).
    dnl
	AC_CHECK_LIB(pthread, pthread_create, , AC_MSG_ERROR([IRIX posix thread library not found!]))
	AC_CHECK_LIB(audio,   alOpenPort,     , AC_MSG_ERROR([IRIX audio library not found!]))
	AC_CHECK_LIB(dmedia,  dmGetUST,       , AC_MSG_ERROR([IRIX digital media library not found!]))

	dnl See the '#ifdef PA_USE_SGI' in file pa_unix/pa_unix_hostapis.c
	dnl which selects the appropriate PaXXX_Initialize() function.
	dnl
	AC_DEFINE(PA_USE_SGI)

	dnl The _REENTRANT option for pthread safety. Perhaps not necessary but it 'll do no harm.
	dnl
	THREAD_CFLAGS="-D_REENTRANT"
    
	OTHER_OBJS="pa_sgi/pa_sgi.o src/os/unix/pa_unix_hostapis.o src/os/unix/pa_unix_util.o";
	
	dnl SGI books say -lpthread should be the last of the libs mentioned.
	dnl
	LIBS="-lm -ldmedia -laudio -lpthread";
	PADLL="libportaudio.so";
	SHARED_FLAGS="";
	;;

  *)
	dnl Unix configuration

   AC_CHECK_LIB(pthread, pthread_create,[have_pthread="yes"]
                ,
                AC_MSG_ERROR([libpthread not found!]))

	if [[ $have_alsa = "yes" ] && [ $with_alsa != "no" ]] ; then
		DLL_LIBS="$DLL_LIBS -lasound"
		OTHER_OBJS="$OTHER_OBJS src/hostapi/alsa/pa_linux_alsa.o"
                AC_DEFINE(PA_USE_ALSA)
	fi

	if [[ $have_jack = "yes" ] && [ $with_jack != "no" ]] ; then
		DLL_LIBS="$DLL_LIBS $JACK_LIBS"
		CFLAGS="$CFLAGS $JACK_CFLAGS"
		OTHER_OBJS="$OTHER_OBJS src/hostapi/jack/pa_jack.o src/common/pa_ringbuffer.o"
                AC_DEFINE(PA_USE_JACK)
	fi

	if [[ $with_oss != "no" ]] ; then
		OTHER_OBJS="$OTHER_OBJS src/hostapi/oss/pa_unix_oss.o"
		AC_DEFINE(PA_USE_OSS)
	fi
	
	if [[ $have_asihpi = "yes" ] && [ $with_asihpi != "no" ]] ; then
                LIBS="$LIBS -lhpi"
		DLL_LIBS="$DLL_LIBS -lhpi"
		OTHER_OBJS="$OTHER_OBJS src/hostapi/asihpi/pa_linux_asihpi.o"
                AC_DEFINE(PA_USE_ASIHPI)
	fi
	
	THREAD_CFLAGS="-pthread"
	DLL_LIBS="$DLL_LIBS -lm -lpthread";
	LIBS="$LIBS -lm -lpthread";
	PADLL="libportaudio.so";
	SHARED_FLAGS="-shared -fPIC";

        OTHER_OBJS="$OTHER_OBJS src/os/unix/pa_unix_hostapis.o src/os/unix/pa_unix_util.o"
esac
CFLAGS="$CFLAGS $THREAD_CFLAGS"

if test "$enable_cxx" = "yes"; then
   AC_CONFIG_SUBDIRS([bindings/cpp])
   ENABLE_CXX_TRUE=""
   ENABLE_CXX_FALE="#"
else
   ENABLE_CXX_TRUE="#"
   ENABLE_CXX_FALE=""
fi
AC_SUBST(ENABLE_CXX_TRUE)
AC_SUBST(ENABLE_CXX_FALSE)

AC_OUTPUT([Makefile portaudio-2.0.pc])
