dnl Process this file with autoconf to produce configure.
dnl
dnl Copyright (C) 1997, 1998, 1999, 2000, 2001
dnl     National Center for Supercomputing Applications
dnl     All rights reserved.

dnl ----------------------------------------------------------------------
dnl Initialize configure.
dnl
AC_REVISION(Id)
AC_INIT(H5.c)
AC_CONFIG_HEADER(H5config.h)
AC_OUTPUT_COMMANDS([
  echo "creating H5pubconf.h"
  sed 's/#define /#define H5_/' <H5config.h |\
    sed 's/#undef /#undef H5_/' >pubconf
  if test ! -f H5pubconf.h; then
    /bin/mv -f pubconf H5pubconf.h
  elif (diff pubconf H5pubconf.h >/dev/null); then
    /bin/rm -f pubconf
    echo "H5pubconf.h is unchanged"
  else
    /bin/mv -f pubconf H5pubconf.h
  fi
])
AC_CANONICAL_HOST
AC_SUBST(CPPFLAGS)

dnl ----------------------------------------------------------------------
dnl Source any special files that we need.  These files normally aren\'t
dnl present but can be used by the maintainers to fine tune things like
dnl turning on debug or profiling flags for the compiler.  The search order
dnl is:
dnl
dnl	CPU-VENDOR-OS
dnl	VENDOR-OS
dnl	CPU-OS
dnl	CPU-VENDOR
dnl	OS
dnl	VENDOR
dnl	CPU
dnl
dnl If the 'OS' ends with a version number then remove it. For instance,
dnl 'freebsd3.1' would become 'freebsd'
case $host_os in
  aix4.*)
    host_os_novers=aix4.x
    ;;
  freebsd*)
    host_os_novers=freebsd
    ;;
  irix5.*)
    host_os_novers=irix5.x
    ;;
  irix6.*)
    host_os_novers=irix6.x
    ;;
  osf4.*)
    host_os_novers=osf4.x
    ;;
  solaris2.*)
    host_os_novers=solaris2.x
    ;;
  *)
    host_os_novers=$host_os
    ;;
esac

AC_MSG_CHECKING(for sizeof hsize_t and hssize_t)

AC_SUBST(HSIZET)
case $HSIZET in
  no|small)
    AC_MSG_RESULT(small)
    HSIZET=small
    ;;
  *)
    AC_MSG_RESULT(large)
    HSIZET=large
    AC_DEFINE(HAVE_LARGE_HSIZET)
    ;;
esac

host_config="none"
for f in $host_cpu-$host_vendor-$host_os \
	 $host_cpu-$host_vendor-$host_os_novers \
	 $host_vendor-$host_os \
         $host_vendor-$host_os_novers \
	 $host_cpu-$host_os \
         $host_cpu-$host_os_novers \
         $host_cpu-$host_vendor \
	 $host_os \
	 $host_os_novers \
	 $host_vendor \
	 $host_cpu ; do
  AC_MSG_CHECKING(for config $f)
  if test -f "$srcdir/config/$f"; then
    host_config=$srcdir/config/$f
    AC_MSG_RESULT(found)
    break
  fi
  AC_MSG_RESULT(no)
done
if test $host_config != "none"; then
  CC_BASENAME="`echo $CC | cut -f1 -d' ' | xargs basename 2>/dev/null`"
  . $host_config
fi

dnl ----------------------------------------------------------------------
dnl Check for programs.
dnl
AC_PROG_CC
CC_BASENAME="`echo $CC | cut -f1 -d' ' | xargs basename 2>/dev/null`"

AC_SUBST(config_dirs) config_dirs=""

enable_production="yes"
AC_MSG_RESULT("production")
CONFIG_MODE=production
CFLAGS="$CFLAGS $PROD_CFLAGS"
CPPFLAGS="$CPPFLAGS $PROD_CPPFLAGS"

dnl ----------------------------------------------------------------------
dnl Check for system libraries.
dnl
AC_CHECK_LIB(m,ceil)

if test "`uname`" = "SunOS" -o "`uname -sr`" = "HP-UX B.11.00"; then
  dnl ...for Solaris and hdf4
  AC_CHECK_LIB(nsl, xdr_int)
fi

dnl AC_CHECK_LIB(coug,main)		dnl ...for ASCI/Red

dnl ----------------------------------------------------------------------
dnl Check for system header files.
dnl
AC_HEADER_STDC
AC_HEADER_TIME
dnl Unix
AC_CHECK_HEADERS(sys/resource.h sys/time.h unistd.h sys/ioctl.h sys/stat.h)
AC_CHECK_HEADERS(sys/socket.h sys/types.h)
AC_CHECK_HEADERS(stddef.h setjmp.h)
AC_CHECK_HEADERS(stdint.h, C9x=yes)
dnl Windows
AC_CHECK_HEADERS(io.h winsock.h sys/timeb.h)

case "$host" in
  alpha*-dec*-osf*)
    dnl The <sys/sysinfo.h> and <sys/proc.h> are needed on the DEC
    dnl Alpha to turn off UAC fixing. We do *not* attempt to
    dnl locate these files on other systems because there are too
    dnl many problems with including them.
    AC_CHECK_HEADERS(sys/sysinfo.h sys/proc.h)
    ;;
esac

AC_TRY_COMPILE([#include <sys/types.h>],
	       [off64_t n = 0;],
	       AC_CHECK_FUNCS(lseek64 fseek64),
	       AC_MSG_RESULT([skipping test for lseek64() and fseek64()]))

dnl ----------------------------------------------------------------------
dnl Data types and their sizes.
dnl
AC_TYPE_OFF_T
AC_CHECK_TYPE(size_t, unsigned long)
AC_CHECK_TYPE(ssize_t, long)
AC_C_BIGENDIAN
AC_CHECK_SIZEOF(char, 1)
AC_CHECK_SIZEOF(short, 2)
AC_CHECK_SIZEOF(int, 4)
AC_CHECK_SIZEOF(long, 4)
AC_CHECK_SIZEOF(long long, 8)
AC_CHECK_SIZEOF(__int64, 8)
AC_CHECK_SIZEOF(float, 4)
AC_CHECK_SIZEOF(double, 8)
AC_CHECK_SIZEOF(long double, 8)

dnl Posix.1g types (C9x)
cat >>confdefs.h <<\EOF
#include <sys/types.h>
EOF

if test "X$C9x" = "Xyes"; then
  cat >>confdefs.h <<\EOF
#include <stdint.h>
EOF
fi

AC_CHECK_SIZEOF(        int8_t, 1)
AC_CHECK_SIZEOF(       uint8_t, 1)
AC_CHECK_SIZEOF(  int_least8_t, 1)
AC_CHECK_SIZEOF( uint_least8_t, 1)
AC_CHECK_SIZEOF(   int_fast8_t, 1)
AC_CHECK_SIZEOF(  uint_fast8_t, 1)

AC_CHECK_SIZEOF(       int16_t, 2)
AC_CHECK_SIZEOF(      uint16_t, 2)
AC_CHECK_SIZEOF( int_least16_t, 2)
AC_CHECK_SIZEOF(uint_least16_t, 2)
AC_CHECK_SIZEOF(  int_fast16_t, 2)
AC_CHECK_SIZEOF( uint_fast16_t, 2)

AC_CHECK_SIZEOF(       int32_t, 4)
AC_CHECK_SIZEOF(      uint32_t, 4)
AC_CHECK_SIZEOF( int_least32_t, 4)
AC_CHECK_SIZEOF(uint_least32_t, 4)
AC_CHECK_SIZEOF(  int_fast32_t, 4)
AC_CHECK_SIZEOF( uint_fast32_t, 4)

AC_CHECK_SIZEOF(       int64_t, 8)
AC_CHECK_SIZEOF(      uint64_t, 8)
AC_CHECK_SIZEOF( int_least64_t, 8)
AC_CHECK_SIZEOF(uint_least64_t, 8)
AC_CHECK_SIZEOF(  int_fast64_t, 8)
AC_CHECK_SIZEOF( uint_fast64_t, 8)

AC_CHECK_SIZEOF(size_t, 4)
AC_CHECK_SIZEOF(ssize_t, 4)
cat >>confdefs.h <<\EOF
#include <sys/types.h> /*for off_t definition*/
EOF
AC_CHECK_SIZEOF(off_t, 4)

AC_CHECK_HEADERS(zlib.h)
AC_CHECK_LIB(vtkzlib, compress2,, unset HAVE_ZLIB)

dnl ----------------------------------------------------------------------
dnl Is the Pthreads library present?  It has a header file `pthread.h' and
dnl a library `-lpthread' and their locations might be specified with the
dnl `--with-pthread' command-line switch.  The value is an include path
dnl and/or a library path.  If the library path is specified then it must
dnl be preceded by a comma.
dnl

#AC_CHECK_HEADERS(pthread.h)
#AC_CHECK_LIB(pthread, pthread_create,,unset PTHREAD)
unset PTHREAD

dnl Check that we can link a simple Pthread program.
#AC_TRY_LINK(,pthread_create(),
#  AC_MSG_RESULT(yes); THREADSAFE=yes,
#  AC_MSG_ERROR(needed pthread library not available))

#if test "X$THREADSAFE" = "Xyes"; then
#  AC_DEFINE(HAVE_THREADSAFE)
#fi

dnl ----------------------------------------------------------------------
dnl How does one figure out the local time zone?  Anyone know of a
dnl Posix way to do this?
dnl

dnl First check if 'struct tm' has a 'tm_gmtoff' member.
AC_MSG_CHECKING(for tm_gmtoff in struct tm)
AC_TRY_COMPILE([
#include <sys/time.h>
#include <time.h>],[struct tm tm; tm.tm_gmtoff=0;],
AC_DEFINE(HAVE_TM_GMTOFF)
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no))

dnl check if 'struct tm' has a '__tm_gmtoff' member.
AC_MSG_CHECKING(for __tm_gmtoff in struct tm)
AC_TRY_COMPILE([
#include <sys/time.h>
#include <time.h>],[struct tm tm; tm.__tm_gmtoff=0;],
AC_DEFINE(HAVE___TM_GMTOFF)
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no))

dnl Check whether the global variable 'timezone' is defined.
AC_MSG_CHECKING(for global timezone variable)
AC_TRY_LINK([
#include <sys/time.h>
#include <time.h>], [timezone=0;],
AC_DEFINE(HAVE_TIMEZONE)
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no))

dnl Check whether 'struct timezone' is defined.
AC_STRUCT_TIMEZONE
AC_MSG_CHECKING(for struct timezone)
AC_TRY_COMPILE([
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>],[struct timezone tz; tz.tz_minuteswest=0;],
AC_DEFINE(HAVE_STRUCT_TIMEZONE)
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no))

dnl ----------------------------------------------------------------------
dnl Does the struct stat have the st_blocks field?  This field is not Posix.
dnl
AC_MSG_CHECKING(for st_blocks in struct stat)
AC_TRY_COMPILE([
#include <sys/stat.h>],[struct stat sb; sb.st_blocks=0;],
AC_DEFINE(HAVE_STAT_ST_BLOCKS)
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no))

dnl ----------------------------------------------------------------------
dnl How do we figure out the width of a tty in characters?
dnl
AC_CHECK_FUNCS(_getvideoconfig gettextinfo GetConsoleScreenBufferInfo)
AC_CHECK_FUNCS(_scrsize ioctl)

AC_MSG_CHECKING(for struct videoconfig)
AC_TRY_COMPILE(,[struct videoconfig w; w.numtextcols=0;],
AC_DEFINE(HAVE_STRUCT_VIDEOCONFIG)
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no))

AC_MSG_CHECKING(for struct text_info)
AC_TRY_COMPILE(,[struct text_info w; w.screenwidth=0;],
AC_DEFINE(HAVE_STRUCT_TEXT_INFO)
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no))

AC_MSG_CHECKING(for TIOCGWINSZ)
AC_TRY_COMPILE([#include <sys/ioctl.h>],[int w=TIOCGWINSZ;],
AC_DEFINE(HAVE_TIOCGWINSZ)
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no))

AC_MSG_CHECKING(for TIOCGGETD)
AC_TRY_COMPILE([#include <sys/ioctl.h>],[int w=TIOCGETD;],
AC_DEFINE(HAVE_TIOCGETD)
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no))


dnl ----------------------------------------------------------------------
dnl Check for functions.
dnl
AC_CHECK_FUNCS(compress2 difftime fork gethostname getpwuid getrusage)
AC_CHECK_FUNCS(gettimeofday BSDgettimeofday longjmp setsysinfo sigaction)
AC_CHECK_FUNCS(signal snprintf vsnprintf strdup system waitpid)

dnl ----------------------------------------------------------------------
dnl Check compiler characteristics
dnl
AC_C_CONST
AC_C_INLINE

AC_MSG_CHECKING(for __attribute__ extension)
AC_TRY_COMPILE(,[int __attribute__((unused)) x],
               AC_DEFINE(HAVE_ATTRIBUTE)
               AC_MSG_RESULT(yes),
               AC_MSG_RESULT(no))

AC_MSG_CHECKING(for __FUNCTION__ extension)
AC_TRY_COMPILE(,[(void)__FUNCTION__],
               AC_DEFINE(HAVE_FUNCTION)
               AC_MSG_RESULT(yes),
               AC_MSG_RESULT(no))

dnl ----------------------------------------------------------------------
dnl Try to figure out how to print 'long long'.  Some machines use '%lld'
dnl and others use '%qd'.  There may be more!  The final 'l' is a
dnl default in case none of the others work.
dnl Need to patch up LD_LIBRARY_PATH so that the execution can find all
dnl the dynamic library.  The correct way to do it should be updating
dnl LD_LIBRARY_PATH along with LDFLAGS or do it with the AC_TRY_RUN macro.
dnl
AC_MSG_CHECKING(how to print long long)
AC_CACHE_VAL(hdf5_cv_printf_ll,
LD_LIBRARY_PATH="$LD_LIBRARY_PATH`echo $LDFLAGS | sed -e 's/-L/:/g' -e 's/ //g'`"
export LD_LIBRARY_PATH

for hdf5_cv_printf_ll in l L q ll unknown; do
  AC_TRY_RUN([
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
	char *s = malloc(128);
	long long x = (long long)1048576 * (long long)1048576;
	sprintf(s,"%${hdf5_cv_printf_ll}d",x);
	exit(strcmp(s,"1099511627776"));
}
   ], break,,continue)
done)

AC_MSG_RESULT(%${hdf5_cv_printf_ll}d and %${hdf5_cv_printf_ll}u)
AC_DEFINE_UNQUOTED(PRINTF_LL_WIDTH,"$hdf5_cv_printf_ll")

dnl ----------------------------------------------------------------------
dnl Set some variables for general configuration information to be saved
dnl and installed with the libraries.
dnl

dnl HDF5 version from the first line of the README.txt file.
H5_VERSION=`cut -d' ' -f3 $srcdir/README.txt | head -1`
AC_SUBST(H5_VERSION)

dnl Configuration date
AC_SUBST(CONFIG_DATE) CONFIG_DATE=`date`

dnl User doing the configuration
AC_SUBST(CONFIG_USER) CONFIG_USER="`whoami`@`hostname`"
if test -n "$ORGANIZATION"; then
  CONFIG_USER="$CONFIG_USER at $ORGANIZATION"
fi

dnl Configuration mode (production, development, profile, etc) saved above.
AC_SUBST(CONFIG_MODE)

dnl Byte sex from the AC_C_BIGENDIAN macro.
AC_SUBST(BYTESEX)
if test "X$ac_cv_c_bigendian" = "Xyes"; then
  BYTESEX="big-endian"
else
  BYTESEX="little-endian"
fi

STATIC_SHARED="static"

dnl Parallel support? (set above except empty if none)
PARALLEL=no

dnl Compiler with version information. This consists of the full path
dnl name of the compiler and the reported version number.
AC_SUBST(CC_VERSION)
if `echo $CC | grep / 2>&1 /dev/null`; then
  CC_VERSION="$CC"	
else
  CC_VERSION="$CC";
  for x in `echo $PATH | sed -e 's/:/ /g'`; do
    if test -x $x/$CC; then
      CC_VERSION="$x/$CC"
      break
    fi
  done
fi
if test -n "$cc_vendor" && test -n "$cc_version"; then
  CC_VERSION="$CC_VERSION ($cc_vendor-$cc_version)"
fi

dnl ----------------------------------------------------------------------
dnl Where is the root of the source tree. Give an absolute address so
dnl we can find it no matter which directory of the distribution is our
dnl current directory. The built-in pwd fails on some systems, but the
dnl /bin/pwd version works OK.
dnl
if test -x /bin/pwd; then
  pwd=/bin/pwd
else
  pwd=pwd
fi
AC_SUBST(ROOT) ROOT=`$pwd`

dnl ----------------------------------------------------------------------
dnl Determine the runtime libraries we may need to include in the
dnl libtools command so that executables will find the correct dynamic
dnl libraries.
dnl 
DYNAMIC_DIRS=""
if test -n "$LDFLAGS"; then
  for d in $LDFLAGS ; do
    case "$d" in
      -L*)
        d=`echo $d | sed -e 's/-L//g'`
        case "$d" in
          .*)
            dnl If the path isn\'t absolute, make it so by
            dnl prepending the ROOT directory to it.
            d=${ROOT}/$d
            ;;
        esac
        DYNAMIC_DIRS="-R${d} $DYNAMIC_DIRS"
        ;;
    esac
  done
fi
AC_SUBST(DYNAMIC_DIRS)

if test -n "$CPPFLAGS"; then
  TEMP_CPPFLAGS=""
  for d in $CPPFLAGS ; do
    case "$d" in
      -I.*)
        dnl If the path isn\'t absolute, make it so by prepending
        dnl the ROOT directory to it.
        d="`echo $d | sed -e 's/-I//g'`"
        d="-I${ROOT}/${d}"
        ;;
    esac
    TEMP_CPPFLAGS="$d $TEMP_CPPFLAGS"
  done
  CPPFLAGS=$TEMP_CPPFLAGS
fi

dnl ----------------------------------------------------------------------
dnl Build the Makefiles.  Almost every Makefile.in will begin with the line
dnl '@COMMENCE@' and end with the line '@CONCLUDE@'.  These lines insert
dnl various files from the config directory into the Makefile.
dnl
AC_SUBST_FILE(COMMENCE) COMMENCE=config/commence
AC_SUBST_FILE(CONCLUDE) CONCLUDE=config/conclude

dnl The directory search list
AC_SUBST(SEARCH) SEARCH='$(srcdir) $(top_builddir) $(top_srcdir)'
cmd='echo $SEARCH |sed "s/ /'$SEARCH_SEP'/g"'
SEARCH="$SEARCH_RULE`eval $cmd`"
export SEARCH

dnl We don\'t need to say when we\'re entering directories if we\'re using
dnl GNU make becuase make does it for us.
if test "X$GMAKE" = "Xyes"; then
  AC_SUBST(SETX) SETX=":"
else
  AC_SUBST(SETX) SETX="set -x"
fi

dnl Some cleanup stuff
rm -f conftest conftest.o conftest.c dummy.o

dnl Build config.status, touch the stamp files, and build all the Makefiles.
dnl The order is such that the first 'make' does not need to update any
dnl configuration information. See config/commence.in for the order in which
dnl things need to be done.

# Then the config.status file (but not makefiles)
saved_no_create=$no_create
no_create=yes

PARALLEL_MAKE=""

AC_OUTPUT(libhdf5.settings)
no_create=$saved_no_create

# Finally the makefiles
test "$no_create" = yes || ${CONFIG_SHELL-/bin/sh} $CONFIG_STATUS && exit 1
  
dnl We don't want inline defined for C++ compilers
dnl Don't worry about the C++ ifdef wrappers in the H5pubconf file, since
dnl 'H5_inline' isn\'t a C++ keyword.
cat >> H5config.h <<EOF

/* inline is a keyword in C++. If this is a C++ compiler, undefine it */
#if defined(__cplusplus) && defined(inline)
#undef inline
#endif
EOF


