# Copyright (C) 1999-2002 Erik de Castro Lopo (erikd AT zip DOT com DOT au).

dnl Require autoconf version >= 2.13)
AC_PREREQ(2.13)

AC_INIT(src/sndfile.c)
AC_CANONICAL_SYSTEM
AM_INIT_AUTOMAKE(libsndfile,1.0.1)
AM_CONFIG_HEADER(src/config.h)

#------------------------------------------------------------------------------------
# This is the version info according to the libtool versioning system.

SHARED_VERSION_INFO="1:1:0"

AC_CHECK_PROG(autogen, autogen, yes, no)

AC_PROG_CC
AC_PROG_INSTALL

AC_PROG_MAKE_SET
AC_PROG_LN_S
AM_PROG_LIBTOOL

AC_HEADER_STDC

#====================================================================================
# Finished checking, handle options.

AC_ARG_ENABLE(justsrc,  
		[  --enable-justsrc             only compile library sources ],
		ac_arg_justsrc=$enableval, ac_arg_justsrc="yes")


AC_ARG_ENABLE(force-broken-float,
		[  --enable-force-broken-float  force use of broken float code ],
		ac_arg_broken_float=1, ac_arg_broken_float=0)
AC_DEFINE_UNQUOTED([FORCE_BROKEN_FLOAT],${ac_arg_broken_float},
		[Set to 1 to force the use of code for processors with broken floats])
if test $ac_arg_broken_float = 1 ; then
	AC_MSG_WARN([[*** Testing code for processors that can't read/write IEEE floats.]])
	fi


AC_ARG_ENABLE(experimental,
		[  --enable-experimental        enable experimental code ],
		ac_arg_experimental=1, ac_arg_experimental=0)
AC_DEFINE_UNQUOTED([ENABLE_EXPERIMENTAL_CODE],${ac_arg_experimental},
		[Set to 1 to enable experimental code.])


AC_ARG_ENABLE(gcc-pipe, 
		[  --disable-gcc-pipe           disable gcc -pipe option ],
		ac_arg_gcc_pipe="N", ac_arg_gcc_pipe="Y")

AC_ARG_ENABLE(gcc-opt,  
		[  --disable-gcc-opt            disable gcc optimisations ],
		ac_arg_gcc_opt="N", ac_arg_gcc_opt="Y")

if test $ac_cv_prog_gcc = yes ; then
	CFLAGS="$CFLAGS -W -Wstrict-prototypes -Wmissing-prototypes -Wall -pedantic -Waggregate-return -Wcast-align -Wcast-qual -Wnested-externs -Wshadow"
	# -Wpointer-arith -Wundef -Wbad-function-cast -Wmissing-declarations -Wconversion -Winline"
	if test "$ac_arg_gcc_opt" = "N" ; then
		temp_CFLAGS=`echo $CFLAGS | sed "s/O2/O0/"`
		CFLAGS=$temp_CFLAGS
		AC_MSG_WARN([[*** Compiler optimisations switched off. ***]])
		fi

	# Disable -Wall and -pedantic for Apple Darwin/Rhapsody.
	# System headers on these systems are broken.
	case "$target_os" in
		darwin* | rhapsody*)
			temp_CFLAGS=`echo $CFLAGS | sed "s/-Wall -pedantic//"`
		CFLAGS=$temp_CFLAGS
			;;
		*)
			;;
		esac
	if test $ac_arg_gcc_pipe != "N" ; then
	 	CFLAGS="$CFLAGS -pipe"
	 	fi
	fi

#====================================================================================
# Check types and their sizes.

AC_CHECK_SIZEOF(short,2)
AC_CHECK_SIZEOF(int,4)
AC_CHECK_SIZEOF(long,8)
AC_CHECK_SIZEOF(float,4)
AC_CHECK_SIZEOF(double,4)
AC_CHECK_SIZEOF(void*,8)

#====================================================================================
# Find an appropriate type for sf_count_t.
# On systems supporting files larger than 2 Gig, sf_count_t must be a 64 bit value.
# Unfortunately there is more than one way of ensuring this so need to do some
# pretty rigourous testing here.

unset ac_cv_sizeof_off_t

AC_CHECK_SIZEOF(off_t,1)	# Fake default value.

if test "x$ac_cv_sizeof_off_t" = "x8" ; then
	# If sizeof (off_t) is 8, no further checking is needed.
	TYPEOF_SF_COUNT_T="off_t"
	SIZEOF_SF_COUNT_T=8
else
	# Check for common 64 bit file offset types.
	AC_CHECK_SIZEOF(loff_t,1)	# Fake default value.
	AC_CHECK_SIZEOF(off64_t,1)	# Fake default value.

	TYPEOF_SF_COUNT_T="unknown"
	if test "x$ac_cv_sizeof_loff_t" = "x8" ; then
		TYPEOF_SF_COUNT_T="loff_t"
		SIZEOF_SF_COUNT_T=8
	elif test "x$ac_cv_sizeof_off64_t" = "x8" ; then
		TYPEOF_SF_COUNT_T="off64_t"
		SIZEOF_SF_COUNT_T=8
		fi

	# Save the old sizeof (off_t) value  and then unset it to see if it
	# changes when Large File Support is enabled.

	pre_largefile_sizeof_off_t=$ac_cv_sizeof_off_t
	unset ac_cv_sizeof_off_t

	AC_SYS_EXTRA_LARGEFILE

	if test "x$ac_cv_sys_largefile_CFLAGS" = "xno" ; then
		ac_cv_sys_largefile_CFLAGS=""
		fi
	if test "x$ac_cv_sys_largefile_LDFLAGS" = "xno" ; then
		ac_cv_sys_largefile_LDFLAGS=""
		fi
	if test "x$ac_cv_sys_largefile_LIBS" = "xno" ; then
		ac_cv_sys_largefile_LIBS=""
		fi

	AC_CHECK_SIZEOF(off_t,1)	# Fake default value.

	if test "x$ac_cv_sizeof_off_t" = "x$pre_largefile_sizeof_off_t" ; then
		AC_MSG_WARN([[This machine does not seem to support 64 bit file offsets.]])
		TYPEOF_SF_COUNT_T="off_t"
		SIZEOF_SF_COUNT_T=$SIZEOF_OFF_T
	elif test "x$TYPEOF_SF_COUNT_T" = "xunknown" ; then
		echo
		echo "*** The configure process has determined that this system is capable"
		echo "*** of Large File Support but has not been able to find a type which"
		echo "*** is an unambiguous 64 bit file offset."
		echo "*** Please contact the author to help resolve this problem."
		echo
		AC_ERROR([[Bad file offset type.]])
		fi
	fi

AC_DEFINE_UNQUOTED([TYPEOF_SF_COUNT_T],${TYPEOF_SF_COUNT_T}, [Set to long if unknown.])
AC_SUBST(TYPEOF_SF_COUNT_T)

AC_DEFINE_UNQUOTED([SIZEOF_SF_COUNT_T],${SIZEOF_SF_COUNT_T}, [Set to sizeof (long) if unknown.])
AC_SUBST(SIZEOF_SF_COUNT_T)

#====================================================================================
# Determine endian-ness of target processor.

AC_CHECK_HEADERS(endian.h)

AC_C_FIND_ENDIAN

AC_DEFINE_UNQUOTED(CPU_IS_BIG_ENDIAN, ${ac_cv_c_big_endian},
	[Target processor is big endian.])
AC_DEFINE_UNQUOTED(CPU_IS_LITTLE_ENDIAN, ${ac_cv_c_little_endian},
	[Target processor is little endian.])

#====================================================================================
# Check for functions.

AC_CHECK_FUNCS(malloc free)
AC_CHECK_FUNCS(open read write lseek fstat ftruncate)
AC_CHECK_FUNCS(snprintf vsnprintf)
AC_CHECK_FUNCS(gmtime gmtime_r)

if test $ac_cv_c99_lrint = no || test $ac_cv_c99_lrintf = no ; then
	AC_MSG_WARN([[*** Missing C99 standard functions lrint() and lrintf().]])
	AC_MSG_WARN([[*** This may cause benign compiler warnings on some systems (ie Solaris).]])
	fi

#====================================================================================
# Find known target OS.

OS_SPECIFIC_INCLUDES=""

case "$target_os" in
	darwin* | rhapsody*)
		OS_SPECIFIC_INCLUDES="-fpascal-strings -I/Developer/Headers/FlatCarbon"
		OS_SPECIFIC_LINKS="-framework CoreAudio"
		;;
	*)
		OS_SPECIFIC_INCLUDES=""
		OS_SPECIFIC_LINKS=""

# dmazzoni: only check for these if we're NOT running on Darwin.
		AC_C99_FUNC_LRINT
		AC_C99_FUNC_LRINTF
		;;
	esac

#====================================================================================
# Test for sanity when cross-compiling.

if test $cross_compiling = yes ; then
	AC_MSG_WARN([[******************************************************************]])
	AC_MSG_WARN([[*** We are cross-compiling, so have to assume sizeof (short) == 2 ]])
	AC_MSG_WARN([[*** and sizeof (int) == 4. If this is not the case there is no    ]])
	AC_MSG_WARN([[*** chance of this working. Please contact the mantainer.         ]])
	AC_MSG_WARN([[******************************************************************]])
	fi

if test $ac_cv_sizeof_short != 2 ; then
	AC_MSG_WARN([[******************************************************************]])
	AC_MSG_WARN([[*** sizeof (short) != 2.                                          ]])
	AC_MSG_WARN([[******************************************************************]])
	fi

if test $ac_cv_sizeof_int != 4 ; then
	AC_MSG_WARN([[******************************************************************]])
	AC_MSG_WARN([[*** sizeof (int) != 4                                             ]])
	AC_MSG_WARN([[******************************************************************]])
	fi

if test $ac_cv_sizeof_float != 4 ; then
	AC_MSG_WARN([[******************************************************************]])
	AC_MSG_WARN([[*** sizeof (float) != 4.                                          ]])
	AC_MSG_WARN([[******************************************************************]])
	fi

if test $ac_cv_sizeof_double != 8 ; then
	AC_MSG_WARN([[******************************************************************]])
	AC_MSG_WARN([[*** sizeof (double) != 8.                                         ]])
	AC_MSG_WARN([[******************************************************************]])
	fi

if test x"$ac_cv_prog_autogen" = "xno" ; then
	touch tests/*.{c,h}
	fi

#====================================================================================
# Now use the information from the checking stage.

subdirs="man doc Win32 MacOS Octave src"    # Do not add src/GSM610 or src/G72x here.
if test $ac_arg_justsrc = "no" ; then
	subdirs="$subdirs examples tests"
	fi

AC_DEFINE([OS_IS_WIN32],0, [Set to 1 if compiling for Win32])

if test x"$CFLAGS" = x ; then
	echo "Error in configure script. CFLAGS has been screwed up."
	exit
	fi

AC_SUBST(subdirs)
AC_SUBST(SHARED_VERSION_INFO)
AC_SUBST(OS_SPECIFIC_INCLUDES)
AC_SUBST(OS_SPECIFIC_LINKS)
AC_SUBST(ENABLE_EXPERIMENTAL_CODE)

AC_OUTPUT(Makefile src/sndfile.h \
			src/Makefile src/GSM610/Makefile src/G72x/Makefile \
			man/Makefile examples/Makefile tests/Makefile doc/Makefile \
			Win32/Makefile MacOS/Makefile Octave/Makefile \
			libsndfile.spec sndfile.pc)

#====================================================================================

echo
echo "          -=-=-=-= Configuration Complete =-=-=-=-"
echo

