# Copyright (C) 2001-2016 Quantum ESPRESSO group
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License. See the file `License' in the root directory
# of the present distribution.

AC_INIT([deviceXlib],[2.0.0],[],[deviceXlib])

# Do not use Autoconf 2.69 
AC_PREREQ([2.71])
AC_CONFIG_MACRO_DIR([m4/])
m4_include([m4/x_ac_python.m4])
m4_include([m4/x_ac_qe_fft_aux.m4])
m4_include([m4/ax_check_compile_flag.m4])
m4_include([m4/x_ac_qe_gpu.m4])
m4_include([m4/y_lib_gpu.m4])
#m4_include([m4/y_gpu.m4])
m4_include([m4/y_acx_get_fc_kind.m4])
m4_include([m4/acx_misc.m4])

# Initialize variables, filling with one comes from the environment...
X_AC_QE_DEFAULT_ENV()

# configure for current directory by default
if test "$topdir" = "" ; then topdir="`pwd`" ; fi

# check system type (no cross-compilation for now)
AC_CANONICAL_BUILD

# Checking Architecture...
X_AC_QE_ARCH()

# Add all needed -D options to try_dflags
try_dflags=""

# "-I../include" is required by IOTK ...
try_iflags="-I\$(TOPDIR)/include -I../include/"

# Checking archiver...
X_AC_QE_AR()

# Checking OpenMP...
X_AC_QE_OPENMP()

# Checking MPIF90...
X_AC_QE_MPIF90()

# Check environ...
#X_AC_QE_ENVIRON()

# Checking CC...
X_AC_QE_CC()

# Checking F90...
X_AC_QE_F90()

# Checking preprocessor...
X_AC_QE_CPP()

# Checking linker...
X_AC_QE_LD()

# ============================================================================
#
# GPUs suppport
#
GPU_SUPPORT="no_gpu"
#
AC_ARG_ENABLE(cuda_fortran,[AS_HELP_STRING([--enable-cuda-fortran],[Enable CUDA support @<:@default=no@:>@])],[],[enable_cuda_fortran="no"])
AC_ARG_ENABLE(openacc,[AS_HELP_STRING([--enable-openacc],[Enable OPENACC support @<:@default=no@:>@])],[],[enable_openacc="no"])
AC_ARG_ENABLE(openmp5,[AS_HELP_STRING([--enable-openmp5],[Enable OPENMP5 support @<:@default=no@:>@])],[],[enable_openmp5="no"])
#
if test x"$enable_cuda_fortran" != "xno" ; then GPU_SUPPORT="cudaf"   ; fi
if test x"$enable_openacc" != "xno"      ; then GPU_SUPPORT="openacc" ; enable_open_mp="no";  fi
if test x"$enable_openmp5" != "xno"      ; then GPU_SUPPORT="openmp5" ; fi
#
AC_SUBST(enable_cuda_fortran)
AC_SUBST(enable_openacc)
AC_SUBST(enable_openmp5)
AC_SUBST(GPU_SUPPORT)
#
ACX_GET_FC_KIND()
#
ACX_LIB_GPU
#AC_SET_GPU

# Checking CUDA...
X_AC_QE_GPU()

# Checking F90 rule...
X_AC_QE_F90RULE()

AC_LANG_PUSH(Fortran 77)
F77=$f90 # use Fortran 90 actually
FFLAGS="$test_fflags"
LDFLAGS="$test_ldflags"

# Checking BLAS...
X_AC_QE_BLAS()

# Checking LAPACK...
X_AC_QE_LAPACK()

# Checking for FFT...
#X_AC_QE_FFT()
#X_AC_QE_FFT_AUX()

# Checking for IBM MASS library...
X_AC_QE_MASS()

# check for MPI library...
X_AC_QE_MPI()

# Setting ScaLAPACK...
#X_AC_QE_SCALAPACK()

# Setting ELPA...
#X_AC_QE_ELPA()

# Enable XML ...
#X_AC_QE_XML()

# Setting HDF5...
#X_AC_QE_HDF5()

# Checking SIGNAL...
#X_AC_QE_SIGNAL()

# Checking for ranlib...
X_AC_QE_RANLIB()

# Checking wget or curl...
X_AC_QE_WGET()

# Checking python support
X_AC_PYTHON()

# Show dflags before adding $(MANUAL_DFLAGS) and adapt to XLF (if needed)
if test "$dflags" = "" ; then dflags="$try_dflags" ; fi
echo setting DFLAGS... $try_dflags

# xlf compilers (AIX and powerpc) want comma-separated -D directives
X_AC_QE_AIX_DFLAGS()

if test "$iflags" = "" ; then iflags="$try_iflags" ; fi

echo setting IFLAGS... $iflags

# export additional settings to generated files
AC_SUBST(dflags)
AC_SUBST(fdflags)
AC_SUBST(iflags)
AC_SUBST(ld_libs)
AC_SUBST(topdir)
AC_SUBST(host)           # <AF>
AC_SUBST(arch)           # <AF>
AC_SUBST(extlib_flags)

AC_CONFIG_HEADERS([include/c_defs.h:include/c_defs.h.in])
AC_CONFIG_FILES([include/configure.h:include/configure.h.in]) # AF

#AC_CONFIG_FILES(Makefile.cpu)
AC_CONFIG_FILES(make.inc)
AC_CONFIG_FILES(configure.msg)

AC_OUTPUT

# AF
# create also f_defs.h with a (not so) dirty trick
#
if test -e include/c_defs.h
then
   test -e include/f_defs.h && rm include/f_defs.h
   grep "#define " include/c_defs.h > include/f_defs.h
fi
# AF

# final messages
sed '/@delete@/d' configure.msg
echo configure: success

