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(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
        ])

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)

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 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)
	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/ringbuffer.o src/hostapi/coreaudio/pa_mac_core_utilities.o src/hostapi/coreaudio/pa_mac_core_blocking.o";
	LIBS="-framework CoreAudio -framework AudioToolbox -framework AudioUnit -framework Carbon";
	PADLL="libportaudio.dylib";
	SHARED_FLAGS="-framework CoreAudio -framework AudioToolbox -framework AudioUnit -framework Carbon -dynamiclib";
        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/dsound_wrapper.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";
	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"
                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
	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"

AC_OUTPUT([Makefile portaudio-2.0.pc])
