
m4_define([ECM_VERSION], [6.0.1])

AC_INIT(ecm, ECM_VERSION, [ecm-dev@lists.fousse.info])

AM_INIT_AUTOMAKE(ecm, ECM_VERSION)

AC_ARG_WITH(gmp_include, [  --with-gmp-include=DIR  GMP include directory ], with_gmp_include=$withval)
AC_ARG_WITH(gmp_lib,     [  --with-gmp-lib=DIR      GMP lib directory ], with_gmp_lib=$withval)
AC_ARG_WITH(gmp_build,   [  --with-gmp-build=DIR    GMP source directory], with_gmp_include=$withval/ with_gmp_lib=$withval/.libs)
AC_ARG_WITH(gmp,         [  --with-gmp=DIR          GMP install directory ], with_gmp_include=$withval/include with_gmp_lib=$withval/lib)
AC_ARG_WITH(gwnum,       [  --with-gwnum=DIR        GWNUM source directory ], with_gwnum=$withval)

AC_ARG_ENABLE(assert,
   [  --enable-assert         enable ASSERT checking [[default=no]]],
   [ case $enableval in
      yes) AC_DEFINE([WANT_ASSERT],1,[Want assertion]) ;;
      no)  ;;
      *) AC_MSG_ERROR([bad value for --enable-assert: yes, or no]) ;;
     esac])

dnl Checks for programs.
AC_PROG_CC
AC_PROG_RANLIB
AC_CANONICAL_HOST

dnl Don't use -ansi, since it forbids inline
dnl Tests concerning the include directories.
if test -d "$with_gmp_include"; then
  CPPFLAGS="$CPPFLAGS -I$with_gmp_include"
else
  with_gmp_include=
fi

AC_C_CONST
AC_C_VOLATILE

dnl Check for GMP
AC_MSG_CHECKING(for gmp.h)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
#include "gmp.h"
]], [[]])],[AC_MSG_RESULT(yes)],[AC_MSG_RESULT(no)
     AC_MSG_ERROR([gmp.h may be missing ${with_gmp_include:+in $with_gmp_include}])
])

AC_MSG_CHECKING(for recent GMP)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
#include "gmp.h"
#if (__GNU_MP_VERSION*100+__GNU_MP_VERSION_MINOR*10 < 410)
# error "min GMP version is 4.1.0"
#endif
]], [[]])],[AC_MSG_RESULT(yes)],[AC_MSG_RESULT(no)
     AC_MSG_ERROR([GMP 4.1.0 min required])
])

dnl AC_CHECK_LIB doesn't do what we want
if test -r "$with_gmp_lib/libgmp.a"; then
  LIBS="$LIBS $with_gmp_lib/libgmp.a"
elif test -r "$with_gmp_lib/libgmp.so"; then
  LIBS="$LIBS $with_gmp_lib/libgmp.so"
elif test -r "$with_gmp_lib/libgmp.lib"; then
  LIBS="$LIBS $with_gmp_lib/libgmp.lib"
else
  if test -d "$with_gmp_lib"; then
    LDFLAGS="-L$with_gmp_lib $LDFLAGS"
  fi  
  LIBS="$LIBS -lgmp"
fi

LIBS="$LIBS -lm"

dnl Check for corresponding 'gmp.h' and libgmp.a
AC_MSG_CHECKING(if gmp.h version and libgmp version are the same)
AC_RUN_IFELSE([AC_LANG_SOURCE([[
#include <stdio.h>
#include <string.h>
#include "gmp.h"
int main()
{
  char buffer[100];
  if (__GNU_MP_VERSION_PATCHLEVEL != 0)
    sprintf (buffer, "%d.%d.%d", __GNU_MP_VERSION, __GNU_MP_VERSION_MINOR,
	    __GNU_MP_VERSION_PATCHLEVEL);
  else
    sprintf (buffer, "%d.%d", __GNU_MP_VERSION, __GNU_MP_VERSION_MINOR);
  printf ("(%s/%s) ", buffer, gmp_version);
  return (strcmp (buffer, gmp_version) != 0) ? 1 : 0;
}
]])], AC_MSG_RESULT(yes),
   [AC_MSG_RESULT(no)
    AC_MSG_ERROR(['gmp.h' and 'libgmp' have different versions! you have to properly reinstall GMP.])],
    AC_MSG_RESULT([can't test])
)

dnl Check if GMP has mpn_mul_fft
AC_CHECK_FUNC(__gmpn_mul_fft, AC_DEFINE(HAVE_FFT))
AC_CHECK_FUNC(__gmpn_add_nc, AC_DEFINE(HAVE_NATIVE_mpn_add_nc))
AC_CHECK_FUNC(__gmpn_mod_34lsub1, AC_DEFINE(HAVE_NATIVE_mpn_mod_34lsub1))

AC_CHECK_FUNC(snprintf, AC_DEFINE(HAVE_snprintf))

dnl If user wants GWNUM, check if the file exists (either as .a or .lib)
if test "x$with_gwnum" = x; then :; else
  AC_CHECK_FILE($with_gwnum/gwnum.a, [
    AC_DEFINE(HAVE_GWNUM)
    LIBS="$LIBS $with_gwnum/gwnum.a"
    CPPFLAGS="$CPPFLAGS -I$with_gwnum"
  ],[
    AC_CHECK_FILE($with_gwnum/gwnum.lib, [
      AC_DEFINE(HAVE_GWNUM)
      LIBS="$LIBS $with_gwnum/gwnum.lib"
      CPPFLAGS="$CPPFLAGS -I$with_gwnum"
    ],[
      with_gwnum=
      AC_MSG_WARN([Woltman's GWNUM library not found, have you compiled it?])
    ])
  ])
fi

AC_CONFIG_FILES(Makefile)

AC_SUBST(INCLUDES)
AC_SUBST(LDADD)
AC_SUBST(LDFLAGS)
AC_SUBST(CFLAGS)
AC_OUTPUT
