dnl
dnl audacity configure.in script
dnl
dnl Joshua Haberman
dnl Dominic Mazzoni
dnl

dnl
dnl Instructions: to create "configure" from "configure.in", run:
dnl
dnl aclocal
dnl autoconf
dnl
dnl Note: you need pkgconfig installed for this to work.  If pkg.m4 is
dnl not in the standard place, like /usr/share/aclocal, use the -I option
dnl to aclocal, for example on Mac OS X when pkgconfig was installed by
dnl fink:
dnl  aclocal -I /sw/share/aclocal
dnl

dnl TODO:
dnl
dnl automatically choose whether or not to make some libs
dnl based on:
dnl
dnl AC_CHECK_LIB(z, compress2, build_libz=false, build_libz=true)
dnl AM_CONDITIONAL(BUILD_LIBZ, test $build_libz = true)
dnl
dnl

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

dnl Require autoconf >= 2.50 (unfortunately 2.13 is standard)
AC_PREREQ(2.50)

dnl Init autoconf
AC_INIT

dnl Check for existence of Audacity.h
AC_CONFIG_SRCDIR([src/Audacity.h])

dnl Checks for programs.
AC_PROG_CC
AC_PROG_GCC_TRADITIONAL
AC_LANG([C++])
AC_PROG_CXX
AC_PROG_CXXCPP
AC_PROG_INSTALL

dnl extra variables
AC_SUBST(BUILDLIBS)
AC_SUBST(EXTRAOBJS)
AC_SUBST(EXTRATARGETS)
AC_SUBST(EXTRAINSTALLTARGETS)
AC_SUBST(EXTRAUNINSTALLTARGETS)
AC_SUBST(OPTOBJS)
AC_SUBST(CDEPEND)
AC_SUBST(PRECOMP_CFLAGS)
AC_SUBST(AFTERBUILD)
AC_SUBST(INSTALL_PREFIX)
AC_SUBST(CONFIGHEADER)
AC_SUBST(LOCAL_LIBS)

dnl allow the user to specify options to configure that change the
dnl name "audacity" anywhere it appears in a pathname.   This allows
dnl multiple versions of Audacity to be installed concurrently
dnl without collision

AC_ARG_PROGRAM
AC_SUBST(AUDACITY_NAME)

dnl autoconf's program_transform_name is set to spit out a sed expression --
dnl however it's "helpfully" already escaped for make.   Since we want to use
dnl it in shell and not make, we have to unescape this: this translates into
dnl turning two dollar signs into one.
dnl
dnl I feign at this monstrosity, but no one depends on this particular
dnl feature but me, as Debian package maintainer, so no one else should
dnl need to worry about understanding it...
program_transform_name=`echo $program_transform_name | sed 's/\\$\\$/$/'`
AUDACITY_NAME=`echo audacity | sed $program_transform_name`

if [[ "$AUDACITY_NAME" = "audacity" ]] ; then
    AC_DEFINE(AUDACITY_NAME, "audacity",
        [define if Audacity is being installed under a name other than "audacity",
         so it can find the files it needs at runtime])
else
    AC_DEFINE_UNQUOTED(AUDACITY_NAME, "$AUDACITY_NAME")
fi

dnl
dnl Make the install prefix available to the program so that it
dnl knows where to look for help files, plug-ins, etc.
dnl

AC_PREFIX_DEFAULT(/usr/local)
if [[ $prefix = "NONE" ]] ; then
   AC_DEFINE(INSTALL_PREFIX, "/usr/local",
        [define as prefix where Audacity is installed])
else
   AC_DEFINE_UNQUOTED(INSTALL_PREFIX, "$prefix")
fi

dnl
dnl Required sublibraries
dnl

AC_ARG_ENABLE(static,
            [AC_HELP_STRING([--enable-static],
                            [link wx statically (default=no)])],
            static_preference=$enableval,
            static_preference="no")

AC_ARG_WITH(lib-preference,
            [AC_HELP_STRING([--with-lib-preference],
                            [use local and/or system libraries, in preferred order [system,local]])],
            lib_preference=$withval,
            lib_preference="system local")

LIBRARIES="LIBVORBIS LIBMAD LIBFLAC LIBSNDFILE LIBID3TAG LIBSAMPLERATE LIBRESAMPLE LIBSOUNDTOUCH LIBNYQUIST"

AC_MSG_NOTICE([Determining what libraries are available in this tree and on the system])

AUDACITY_CHECKLIB_LIBVORBIS
AUDACITY_CHECKLIB_LIBMAD
AUDACITY_CHECKLIB_LIBFLAC
AUDACITY_CHECKLIB_LIBSNDFILE
AUDACITY_CHECKLIB_LIBID3TAG
AUDACITY_CHECKLIB_LIBSAMPLERATE
AUDACITY_CHECKLIB_LIBRESAMPLE
AUDACITY_CHECKLIB_LIBSOUNDTOUCH
AUDACITY_CHECKLIB_LIBNYQUIST

dnl Decide what libraries to build with, and whether to use system or local libraries
dnl Set variables based on choices.

AC_MSG_NOTICE([Figuring out what libraries to enable])

lib_preference=`echo $lib_preference | tr [[:lower:]], "[[:upper:]] "`

for lib in $LIBRARIES ; do
   eval LIB_ARGUMENT="\$${lib}_ARGUMENT"
   eval LIB_SYSTEM_AVAILABLE="\$${lib}_SYSTEM_AVAILABLE"
   eval LIB_LOCAL_AVAILABLE="\$${lib}_LOCAL_AVAILABLE"
   if test $LIB_ARGUMENT = unspecified ; then
      lib_activated="no"
      for sys_or_local in $lib_preference ; do
         # example: LIBMAD_SYSTEM_AVAILABLE
         eval AVAILABLE="\$${lib}_${sys_or_local}_AVAILABLE"
         if test "$AVAILABLE" = "yes" ; then
            eval ${lib}_USE_${sys_or_local}="yes"
            lib_activated="yes"
            AC_MSG_NOTICE([Using $sys_or_local libraries for $lib])
            break
         fi
      done
      if test $lib_activated = no ; then
         AC_MSG_NOTICE([disabling $lib])
      fi
   else
      dnl lib_ARGUMENT is something other than "unspecified"
      if test $LIB_ARGUMENT = local ; then
         if test $LIB_LOCAL_AVAILABLE = yes ; then
            eval ${lib}_USE_LOCAL="yes"
            AC_MSG_NOTICE([Using the LOCAL libraries for $lib because you specifically requested this])
         else
            AC_MSG_ERROR([You requested using the local libraries for $lib but they are not available])
         fi
      elif test $LIB_ARGUMENT = system ; then
         if test $LIB_SYSTEM_AVAILABLE = yes ; then
            eval ${lib}_USE_SYSTEM="yes"
            AC_MSG_NOTICE([Using the SYSTEM libraries for $lib because you specifically requested this])
         else
            AC_MSG_ERROR([You requested using the local libraries for $lib but they are not available])
         fi
      elif test $LIB_ARGUMENT = no ; then
         AC_MSG_NOTICE([disabling $lib at your request])
      else
         AC_MSG_ERROR(I didn't understand the argument $LIB_ARGUMENT for $lib)
      fi
   fi
done

# In some cases the choices made above might be inappropriate.
#   * we need to have libsndfile one way or another
#   * we can only use one of libsamplerate and libresample

if test "$LIBSNDFILE_USE_LOCAL" != "yes" && test "$LIBSNDFILE_USE_SYSTEM" != "yes" ; then
   AC_MSG_ERROR([Audacity requires libsndfile to be enabled])
fi

if test "$LIBRESAMPLE_USE_LOCAL" = "yes" || test "$LIBRESAMPLE_USE_SYSTEM" = "yes" ; then
   LIBSAMPLERATE_USE_LOCAL="no"
   LIBSAMPLERATE_USE_SYSTEM="no"
fi

#
# Based on the lib selections, modify LOCAL_LIBS, CXXFLAGS, etc
#

for lib in $LIBRARIES ; do
   eval LIB_USE_LOCAL=\$${lib}_USE_LOCAL
   eval LIB_USE_SYSTEM=\$${lib}_USE_SYSTEM
   if test "$LIB_USE_LOCAL" = "yes" ; then
      eval LOCAL_LIBS=\"$LOCAL_LIBS \$${lib}_LOCAL_LIBS\"
      eval OPTOBJS=\"$OPTOBJS \$${lib}_LOCAL_OPTOBJS\"
      eval CXXFLAGS=\"\$CXXFLAGS \$${lib}_LOCAL_CXXFLAGS\"
      eval CONFIG_SUBDIRS=\"\$${lib}_LOCAL_CONFIG_SUBDIRS\"
      AC_CONFIG_SUBDIRS($CONFIG_SUBDIRS)

      eval CPPSYMBOLS=\"\$${lib}_LOCAL_CPPSYMBOLS\"
      for symbol in $CPPSYMBOLS ; do
         AC_DEFINE_UNQUOTED($symbol, 1)
      done
   elif test "$LIB_USE_SYSTEM" = "yes" ; then
      eval LIBS=\"$LIBS \$${lib}_SYSTEM_LIBS\"
      eval OPTOBJS=\"$OPTOBJS \$${lib}_SYSTEM_OPTOBJS\"
      #LIBS="$LIBS $THESE_LIBS"
      eval CXXFLAGS=\"\$CXXFLAGS \$${lib}_SYSTEM_CXXFLAGS\"

      eval CPPSYMBOLS=\"\$${lib}_SYSTEM_CPPSYMBOLS\"
      for symbol in $CPPSYMBOLS ; do
         AC_DEFINE_UNQUOTED($symbol, 1)
      done
   fi
done

LOCAL_LIBS="$LOCAL_LIBS expat.a"
CXXFLAGS="$CXXFLAGS -I\$(top_srcdir)/lib-src/expat"

dnl TODO enable/disable allegro
LOCAL_LIBS="$LOCAL_LIBS allegro.a"
CXXFLAGS="$CXXFLAGS -I\$(top_srcdir)/lib-src/allegro"

dnl
dnl Optional sublibraries
dnl

dnl options



AC_ARG_WITH(ladspa,
   [AC_HELP_STRING([--with-ladspa],
      [compile with ladspa plug-in support [default=yes]])],
   use_ladspa=$withval,
   use_ladspa="yes")

AC_ARG_WITH(audiounits,
   [AC_HELP_STRING([--with-audiounits],
      [compile with audio unit plug-in support [default=auto]])],
   use_audiounits=$withval,
   use_audiounits="auto")

AC_ARG_WITH(portmixer,
   [AC_HELP_STRING([--with-portmixer],
      [compile with PortMixer [default=yes]])],
   use_portmixer=$withval,
   use_portmixer="yes")

AC_ARG_WITH(portaudio,
   [AC_HELP_STRING([--with-portaudio],
      [which version of PortAudio to use (=[v18,v19]) [default=v18]])],
   use_portaudio=$withval,
   use_portaudio="v18")

AC_ARG_WITH(help,
   [AC_HELP_STRING([--with-help],
      [Make the help file "audacity-1.2-help.htb" [default=yes]])],
   use_help=$withval,
   use_help="yes")

if [[ "$use_portmixer" = "yes" ]] ; then
   AC_DEFINE(USE_PORTMIXER, 1,   
   [Define if PortMixer support should be enabled])
   CXXFLAGS="-I\$(top_srcdir)/lib-src/portmixer/px_common $CXXFLAGS"
fi


if [[ "$use_portmixer" = "yes" ]] && [[ "$use_portaudio" = "v19" ]] ; then
   AC_MSG_ERROR([*** PortAudio v19 does not work with PortMixer (at least not yet) ])
fi


dnl if [[ $use_help = "yes" ]] ; then
dnl   enable help
dnl fi

dnl --- look for "ZIP"

AC_PATH_PROG(ZIP, zip, no)
if [[ $ZIP = "no" ]] ; then
        AC_MSG_ERROR("Could not find zip - needed to create the help file");

   dnl TODO

   dnl make non-fatal - i.e. just don't build help
fi

dnl --- check for required libraries ---

dnl wxWindows -- we assume that if wx-config is found, wxWindows is successfully installed.
AC_PATH_PROG(WX_CONFIG, wx-config, no, $PATH:/usr/local/bin )
if [[ "$WX_CONFIG" = "no" ]] ; then
   AC_MSG_ERROR("Could not find wx-config: is wxWindows installed? is wx-config in your path?")
fi

if [[ "$static_preference" = "yes" ]] ; then
   LIBS="$LIBS `$WX_CONFIG --static=yes --libs`"
else
   LIBS="$LIBS `$WX_CONFIG --static=no --libs`"
fi
CPPFLAGS="$CPPFLAGS `$WX_CONFIG --cxxflags`"

AC_CANONICAL_HOST

dnl OS-specific configuration

case "${host_os}" in
   darwin* | rhapsody*)
      dnl Mac OS X configuration
      CDEPEND="AudacityHeaders.h.gch"
      PRECOMP_CFLAGS="-include AudacityHeaders.h"
      CONFIGHEADER="configunix.h"
      CXXFLAGS="-I../mac $CXXFLAGS"
      AFTERBUILD=""
      EXTRATARGETS="../Audacity.app"
      if [[ "$use_audiounits" = "auto" ]] ; then
         use_audiounits="yes"
      fi
      EXTRAINSTALLTARGETS=""
      EXTRAUNINSTALLTARGETS=""

      ;;      

   cygwin*)
      dnl Windows/CygWin configuration
      LIBS="$LIBS -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -luuid -lodbc32 -lodbccp32 -lwsock32 -lwinmm"
      CONFIGHEADER="configwin.h"
    
      AC_DEFINE(__CYGWIN__,1,[We're using cygwin])
      AC_DEFINE(_FILE_OFFSET_BITS,32,[Placeholder for large file support])
    
      AFTERBUILD=""
      EXTRAINSTALLTARGETS=""
      EXTRAUNINSTALLTARGETS=""
      ;;

   *)
      dnl Unix OSS configuration
      CONFIGHEADER="configunix.h"
      AFTERBUILD=""
      EXTRAINSTALLTARGETS="install-extra-data"
      EXTRAUNINSTALLTARGETS="uninstall-extra-data"
esac

dnl PortAudio configuration

if [[ "$use_portaudio" = "v19" ]] ; then
   dnl PortAudio v19
   AC_DEFINE(USE_PORTAUDIO_V19, 1,
      [Define if PortAudio v19 is being used (instead of v18)])

   BUILDLIBS="$BUILDLIBS portaudio-v19/lib/libportaudio.a"
   EXTRAOBJS="$EXTRAOBJS ../lib-src/portaudio-v19/lib/libportaudio.a"
   CXXFLAGS="-I\$(top_srcdir)/lib-src/portaudio-v19/pa_common $CXXFLAGS"

   AC_CONFIG_SUBDIRS([[lib-src/portaudio-v19]])

   dnl TODO: Use same library selections as PortAudio v19's configure script.
   case "${host_os}" in
      darwin* | rhapsody*)
         LIBS="-framework AudioUnit -framework AudioToolbox $LIBS"
         LIBS="-framework CoreAudio $LIBS -lz"
         ;;
      cygwin*)
         ;;
      *)
         dnl Unix
         AC_CHECK_LIB(asound, snd_pcm_open, have_alsa=yes, have_alsa=no)
         if [[ $have_alsa = "yes" ]] ; then
            LIBS="$LIBS -lasound"
         fi
         PKG_CHECK_MODULES(JACK, jack, have_jack=yes, have_jack=no)
         if [[ $have_jack = "yes" ]] ; then
            LIBS="$LIBS $JACK_LIBS"
         fi
         ;;
   esac

else
   dnl PortAudio v18
   CXXFLAGS="-I\$(top_srcdir)/lib-src/portaudio/pa_common $CXXFLAGS"

   case "${host_os}" in
      darwin* | rhapsody*)
         BUILDLIBS="$BUILDLIBS portaudio/pa_mac_core/portaudio.a"
         EXTRAOBJS="$EXTRAOBJS ../lib-src/portaudio/pa_mac_core/portaudio.a"

         LIBS="-framework AudioUnit -framework AudioToolbox $LIBS"
         LIBS="-framework CoreAudio $LIBS -lz"
         ;;
      cygwin*)
         BUILDLIBS="$BUILDLIBS portaudio/pa_win_wmme/portaudio.a"
         EXTRAOBJS="$EXTRAOBJS ../lib-src/portaudio/pa_win_wmme/portaudio.a"
         ;;
      *)
         dnl Unix
         BUILDLIBS="$BUILDLIBS portaudio/pa_unix_oss/portaudio.a"
         EXTRAOBJS="$EXTRAOBJS ../lib-src/portaudio/pa_unix_oss/portaudio.a"
         ;;
   esac
fi

if [[ "$use_portmixer" = "yes" ]] ; then
   case "${host_os}" in
      darwin* | rhapsody*)
         BUILDLIBS="$BUILDLIBS portmixer/px_mac_core/portmixer.a"
         EXTRAOBJS="$EXTRAOBJS ../lib-src/portmixer/px_mac_core/portmixer.a"     
         ;;
      cygwin*)
         BUILDLIBS="$BUILDLIBS portmixer/px_win_wmme/portmixer.a"
         EXTRAOBJS="$EXTRAOBJS ../lib-src/portmixer/px_win_wmme/portmixer.a"
         ;;
      *)
         dnl Unix
         BUILDLIBS="$BUILDLIBS portmixer/px_unix_oss/portmixer.a"
         EXTRAOBJS="$EXTRAOBJS ../lib-src/portmixer/px_unix_oss/portmixer.a"     
         ;;
   esac
fi


case "$host_os" in
   darwin* | rhapsody*)
      AC_MSG_WARN([[Not using native lrint() and lrintf() because they are missing on 10.1.]])
      ;;
   *)
      dnl For all other Unix systems, we want to check for lrint/lrintf
      AC_C99_FUNC_LRINT
      AC_C99_FUNC_LRINTF
      ;;
esac


dnl Special configuration for LADSPA on Mac OS X
if [[ "$use_ladspa" = "yes" ]] ; then
   case "$host_os" in
      darwin* | rhapsody*)
         LOCAL_LIBS="$LOCAL_LIBS libdl.a"
         CXXFLAGS="$CXXFLAGS -I$(srcdir)/lib-src/dlcompat"
         if test ! -f "lib-src/dlcompat/Makefile"; then
            echo "--- Configuring dlcompat";
            AC_CONFIG_SUBDIRS([[lib-src/dlcompat]])
         fi    
         ;;
      *)
         ;;
   esac
fi

if [[ "$use_ladspa" = "yes" ]] ; then
   OPTOBJS="$OPTOBJS effects/ladspa/LoadLadspa.o"
   OPTOBJS="$OPTOBJS effects/ladspa/LadspaEffect.o"

   AC_DEFINE(USE_LADSPA, 1,
             [Define if LADSPA plug-ins are enabled])
fi

if [[ "$use_audiounits" = "yes" ]] ; then
   OPTOBJS="$OPTOBJS effects/audiounits/LoadAudioUnits.o"
   OPTOBJS="$OPTOBJS effects/audiounits/AudioUnitEffect.o"

   AC_DEFINE(USE_AUDIO_UNITS, 1,
             [Define if Audio Unit plug-ins are enabled (Mac OS X only)])
fi

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_SIZE_T

case "${host_os}" in
   cygwin*)
      AC_CONFIG_HEADER(src/configwin.h:src/configtemplate.h)
      ;;
   *)
      AC_CONFIG_HEADER(src/configunix.h:src/configtemplate.h)
      ;;
esac

AC_CONFIG_FILES([Makefile src/Makefile lib-src/Makefile \
                 lib-src/allegro/Makefile lib-src/expat/Makefile \
                 lib-src/libnyquist/Makefile lib-src/portaudio/pa_unix_oss/Makefile \
                 locale/Makefile tests/Makefile])

AC_OUTPUT

echo ""
echo "Finished configure:"


for lib in $LIBRARIES ; do
   eval LIB_USE_LOCAL=\$${lib}_USE_LOCAL
   eval LIB_USE_SYSTEM=\$${lib}_USE_SYSTEM
   if test "$LIB_USE_LOCAL" = "yes" ; then
      echo "$lib: using LOCAL libraries"
   elif test "$LIB_USE_SYSTEM" = "yes" ; then
      echo "$lib: using SYSTEM libraries"
   else
      echo "$lib: disabled"
   fi
done

if [[ "$use_ladspa" = "yes" ]] ; then
   echo "ladspa: enabled"
else
   echo "ladspa: disabled"        
fi

if [[ "$use_audiounits" = "yes" ]] ; then
   echo "audiounits: enabled"
else
   echo "audiounits: disabled"        
fi

echo "prefix=$prefix";

if [[ "$use_portaudio" = "v19" ]] ; then
   echo ""
   echo "Note: portaudio v19 supports ALSA and Jack, but may be less stable."
else
   echo ""
   echo "Note: portaudio v18 only supports OSS.  If your system supports"
   echo "ALSA, you may want to reconfigure --with-portaudio=v19, though"
   echo "portaudio v19 is newer and possibly less stable."
fi


echo ""
echo "Run 'configure --help' for an explanation of these options,"
echo "otherwise run 'make' to build Audacity."

dnl Indentation settings for Vim and Emacs and unique identifier for Arch, a
dnl version control system. Please do not modify past this point.

dnl arch-tag: aba52141-47c0-40c6-8629-aa61e7bee0c7

# Local Variables:
# c-basic-offset: 3
# indent-tabs-mode: nil
# End:
#
# vim: et sts=3 sw=3
