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

AC_PREREQ(2.59)

AC_INIT(blitz, 1.0.2, blitz-support@lists.sourceforge.net)

AC_CONFIG_AUX_DIR(config)
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_SRCDIR(blitz/blitz.h)
AC_CONFIG_HEADERS(blitz/config.h)

AC_CANONICAL_TARGET

AM_INIT_AUTOMAKE([foreign 1.9 dist-bzip2 nostdinc no-installinfo])

# Avoid generating makefile rules to rebuild maintainer-only files by
# default.  Maintainers may override this default and generate these
# makefile rules using the `--enable-maintainer-mode' configure option.
AM_MAINTAINER_MODE

# Blitz++ configure utility, generated by autoconf.
#
# If you want to modify this file, make sure you are editing
# configure.ac, and not configure.  

AC_MSG_NOTICE([

Configuring $PACKAGE_STRING for $target

])

# C++ compiler
AC_PROG_CXX([xlc++ xlC icpc pathCC pgCC cxx aCC c++ CC g++ clang++ KCC FCC])
AC_LANG(C++)
AC_CXX_FLAGS_PRESET

# Option to disable Fortran (benchmarks will not compile)
AC_MSG_CHECKING([whether to enable Fortran compilation for benchmark codes])
AC_ARG_ENABLE(fortran,
  AS_HELP_STRING([--disable-fortran],[Disable Fortran compilation (used for benchmark codes)]),
  [], [enable_fortran=yes])
AC_MSG_RESULT([$enable_fortran])
AM_CONDITIONAL(FORTRAN_ENABLED, [test "$enable_fortran" != no])

# Fortran compiler
if test $enable_fortran = yes; then
  AC_PROG_F77([xlf ifort pathf90 pgf77 ftn frt gfortran f77 g77 g95])
  AC_PROG_FC([xlf90 ifort pathf90 pgf90 ftn frt f90 gfortran g95])
  AC_FORTRAN_FLAGS_PRESET

  AC_LANG_PUSH(Fortran 77)
  AC_F77_LIBRARY_LDFLAGS
  AC_LANG_POP(Fortran 77)
  AC_LANG_PUSH(Fortran)
  AC_FC_LIBRARY_LDFLAGS

  if test -n "$FC"; then
    AC_FC_FREEFORM(,FC=)
  fi
  if test -n "$FC"; then
    AC_FC_SRCEXT(f90,,FC=)
  fi
  AC_LANG_POP(Fortran)
fi
AM_CONDITIONAL(F90_COMPILER, [test -n "$FC"])

# Prefix config macros with BZ_ and create compiler-specific bzconfig.h
AC_SET_COMPILER_SPECIFIC_HEADER

# makeinfo documentation
AC_CHECK_PROG(MAKEINFO, [makeinfo], [makeinfo], [""])
AM_CONDITIONAL(MAKEINFO_DOC, [test -n "$MAKEINFO"])
# texinfo documentation
AC_CHECK_PROG(TEXI2HTML, [texi2html], [texi2html])
AM_CONDITIONAL(TEXI2HTML_DOC, [test -n "$TEXI2HTML"])
# Doxygen documentation
AC_PROG_DOXYGEN

# Check if the script was called with --enable-64bit
# and if so modify the compilation flags.
AC_COMPILERS_ENABLE_64BITS

# Disable shared library building by default (use --enable-shared to enable)
AM_DISABLE_SHARED
AC_PROG_LIBTOOL

AC_CHECK_FUNC([sin],,[AC_CHECK_LIB(m,sin)])

AC_LIB_BLAS

# Check if the script was called with --enable-optimize or
# --enable-debug, and if so modify the compilation flags.
AC_CXX_ENABLE_OPTIMIZE
AC_CXX_ENABLE_DEBUG

# Option to enable thread-safety (requires thread support or OpenMP)
AC_MSG_CHECKING([whether to enable Blitz thread-safety features])
AC_ARG_ENABLE(threadsafe,
  AS_HELP_STRING([--enable-threadsafe],[Enable thread-safety features]),
  [],[enable_threadsafe=no])
AC_MSG_RESULT([$enable_threadsafe])
if test "$enable_threadsafe" != no; then
  AC_DEFINE([THREADSAFE],[1],[Enable Blitz thread-safety features])
fi

AC_MSG_CHECKING([if we are using Intel Threading Building Blocks])
AC_ARG_WITH([tbb],
  AS_HELP_STRING([--with-tbb],[Use Intel Threading Building Blocks atomic types]),
  [], [with_tbb=no])
if test "x$with_tbb" != xno; then
    AC_MSG_RESULT([yes])
    AC_CHECK_HEADERS([tbb/atomic.h],,AC_MSG_ERROR([tbb/atomic.h not found]))
    AC_DEFINE([THREADSAFE_USE_TBB],[1],[Use TBB atomic types])
else
    AC_MSG_RESULT([no])
fi

AC_MSG_CHECKING([for SIMD instruction width])
AC_ARG_ENABLE([simd-width],
  AS_HELP_STRING([--enable-simd-width=n],[Facilitate compiler vectorization optimizations for SIMD instruction width of n bytes.]),
  [if test "x$enableval" != xno; then
   AC_ALIGNMENT_DIRECTIVE
   AC_DEFINE([USE_ALIGNMENT_PRAGMAS],[1],[Specifies whether compiler alignment pragmas should be used])
   fi ],
  [enable_simd_width=no]
  )
AC_MSG_RESULT([$enable_simd_width])
if test "$enable_simd_width" = no ; then
   enable_simd_width=1
   # if not set, also define ALIGN_VARIABLE to just declare a variable
   AC_DEFINE([ALIGN_VARIABLE(vartype,varname,alignment)],[vartype varname;])
fi
AC_DEFINE_UNQUOTED([SIMD_WIDTH],[$enable_simd_width],[Set SIMD instruction width in bytes])

AC_MSG_CHECKING([whether to pad array lengths to SIMD instruction width])
AC_ARG_ENABLE([array-length-padding],
  AS_HELP_STRING([--enable-array-length-padding],[Sets the default array padding policy to pad all lowest-rank lengths to nearest larger SIMD width. Caution: This means that arrays will in general be non-contiguous.]),
  [if test "x$enableval" != xno; then
  AC_MSG_RESULT([yes])
  if test "$enable_simd_width" = 1 ; then
     AC_MSG_WARN([This option is useless without a SIMD width >1.])
  fi
  AC_DEFINE([PAD_ARRAYS],1,[Pad array lengths to SIMD width.])
  else
  AC_MSG_RESULT([no])
  fi
  ],AC_MSG_RESULT([no]))

AC_MSG_CHECKING([whether to enable Blitz 64-bit dimensions])
AC_ARG_ENABLE([64-bit-dimensions],
  AS_HELP_STRING([--enable-64-bit-dimensions],[Enable dimensions with > 2^31 elements (not yet implemented)]),
  AC_MSG_WARN([Dimensions with > 2^31 elements are not yet implemented])
  AC_DEFINE([FULLY64BIT],[1],[Enable dimensions with > 2^31 elements (NOT IMPLEMENTED)]),AC_MSG_RESULT([no]))

AC_MSG_CHECKING([whether to enable serialization support])
AC_ARG_ENABLE([serialization],
	AS_HELP_STRING([--enable-serialization],[Enable serialization support using Boost::Serialization]), [
	if test "x$enableval" != xno; then
	AC_MSG_RESULT([yes])
	AX_BOOST_BASE([1.40.0])						       
	AX_BOOST_SERIALIZATION
	AC_CHECK_HEADERS([boost/mpi.hpp])
	else
	AC_MSG_RESULT([no])
	fi
	 ], AC_MSG_RESULT([no]))
  
# check for PAPI library
AC_CHECK_LIB([papi],[main])

# autoconf replacement of bzconfig
AC_CHECK_CXX_FEATURES

# Summarize variables
AC_ENV

# Process the Makefile.in's
AC_CONFIG_FILES([
	Makefile 
	bin/Makefile
	blitz/Makefile
	blitz/array/Makefile
	blitz/generate/Makefile
	blitz/meta/Makefile
	random/Makefile
	lib/Makefile
	testsuite/Makefile 
	examples/Makefile
	doc/Makefile
	doc/examples/Makefile
	doc/stencils/Makefile
	doc/doxygen/Makefile
	doc/doxygen/Doxyfile
	])

# Benchmarks require Fortran compiler
if test $enable_fortran = yes; then
AC_CONFIG_FILES([
	benchmarks/Makefile 
	benchmarks/plot_benchmarks.m
	])
fi

# check for valgrind for extra tests
AC_CHECK_PROG([valgrind],[valgrind],[yes],)
AM_CONDITIONAL([HAVE_VALGRIND],[test x$valgrind = xyes])

#	Obsolete makefiles
#
#	demos/Makefile
#	compiler/Makefile
#	manual/Makefile
#	manual/examples/Makefile
#	manual/stencils/Makefile
#	src/Makefile 

AX_CREATE_PKGCONFIG_INFO

AC_OUTPUT

# At some point in the future it would be nice to collect
# information about machines, OSes and compilers which
# people are using.  Obviously this has to be handled in
# a sensitive way.  Current idea: send the output
# of uname -a, $(target) and $(bz_compiler) via an
# anonymous remailer.  Prompt the user and give them
# a chance to override this behaviour.
# AC_SEND_CONFIG

AC_INFO
