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


dnl  Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
dnl
dnl  This file is part of the GNU MP Library.
dnl
dnl  The GNU MP Library is free software; you can redistribute it and/or modify
dnl  it under the terms of the GNU Library General Public License as published
dnl  by the Free Software Foundation; either version 2 of the License, or (at
dnl  your option) any later version.
dnl
dnl  The GNU MP Library is distributed in the hope that it will be useful, but
dnl  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
dnl  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
dnl  License for more details.
dnl
dnl  You should have received a copy of the GNU Library General Public License
dnl  along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
dnl  the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
dnl  MA 02111-1307, USA.


dnl [AC_REVISION([$CVSid$])]
AC_PREREQ(2.14)dnl
AC_INIT(gmp-impl.h)

dnl Check system.
AC_CANONICAL_SYSTEM

dnl  Automake
AM_INIT_AUTOMAKE(gmp, GMP_VERSION)
AM_CONFIG_HEADER(config.h:config.in)
AM_MAINTAINER_MODE

dnl  GMP specific
GMP_INIT(config.m4)


AC_ARG_ENABLE(assert,
AC_HELP_STRING([--enable-assert],[enable ASSERT checking [default=no]]),
[case "${enableval}" in
yes|no) ;;
*) AC_MSG_ERROR([bad value ${enableval} for --enable-assert, need yes or no]) ;;
esac],
[enable_assert=no])

if test "$enable_assert" = "yes"; then
  AC_DEFINE(WANT_ASSERT)
fi


AC_ARG_ENABLE(alloca,
AC_HELP_STRING([--enable-alloca],[use alloca for temp space [default=yes]]),
[case "${enableval}" in
yes|no) ;;
*) AC_MSG_ERROR([bad value ${enableval} for --enable-alloca, need yes or no]) ;;
esac],
[enable_alloca=yes])

if test "$enable_alloca" = "no"; then
  AC_DEFINE(USE_STACK_ALLOC)
fi


AC_ARG_ENABLE(mpbsd,
AC_HELP_STRING([--enable-mpbsd],[build Berkley MP compatibility library [default=no]]),
[case "${enableval}" in
yes|no) ;;
*) AC_MSG_ERROR([bad value ${enableval} for --enable-mpbsd, need yes or no]) ;;
esac],
[enable_mpbsd=no])
AM_CONDITIONAL(WANT_MPBSD, test "$enable_mpbsd" = "yes")


dnl Switch on OS and determine what compiler to use.
dnl
dnl  os_64bit	           Set to "yes" if OS is 64-bit capable.
dnl                        FIXME: Rename to `check_64bit_compiler'!
dnl  cclist		   List of compilers, best first.
dnl  gmp_cflags_{cc}	   Flags for compiler named {cc}.
dnl  gmp_cflags64_{cc}     Flags for compiler named {cc} for 64-bit code.
dnl  gmp_optcflags_{cc}	   Optional compiler flags.
dnl  gmp_xoptcflags_{cc}   Exclusive optional compiler flags.
dnl
os_64bit="no"
cclist="gcc cc"		# FIXME: Prefer c89 to cc.
gmp_cflags_gcc="-g -O2"
gmp_cflags64_gcc="-mWHAT -g -O2"
gmp_cflags_cc="-g"
gmp_cflags64_cc="-g"

case "$target" in
  # Alpha
  alpha*-cray-unicos*)
    # Don't perform any assembly syntax tests on this beast.
    gmp_no_asm_syntax_testing=yes
    cclist=cc
    gmp_cflags_cc="$gmp_cflags_cc -O"
    ;;
  alpha*-*-osf*)
    flavour=`echo $target_cpu | sed 's/^alpha//g'`
    if test -n "$flavour"; then
      gmp_optcflags_gcc="-mcpu=$flavour"
      # FIXME: We shouldn't fail fatally if none of these work, but that's
      # how xoptcflags work and we don't have any other mechanism right now.
      gmp_xoptcflags_gcc="-Wa,-arch,${flavour} -Wa,-m${flavour}"
      gmp_cflags_cc="$gmp_cflags_cc -arch $flavour -tune $flavour"
    fi
    ;;
  alpha*-*-*)
    cclist="gcc"
    flavour=`echo $target_cpu | sed 's/^alpha//g'`
    if test -n "$flavour"; then
      gmp_optcflags_gcc="-mcpu=$flavour"
    fi
    ;;

  # x86
  [*-*-freebsd[3-9]*])	# hides generic *bsd* alternative
    gmp_cflags_gcc="-g -O -fomit-frame-pointer"
    ;;
  [i[3456]86*-*-*bsd* | pentium*-*-*bsd* | k[5678]*-*-*bsd* |] \
  [i[3456]86*-*-linuxaout* | pentium*-*-linuxaout* | k[5678]*-*-linuxaout* |] \
  [i[3456]86*-*-linuxoldld* | pentium*-*-linuxoldld* | k[5678]*-*-linuxoldld* |] \
  [i[3456]86*-*-nextstep* | i[3456]86*-*-nextstep* | k[5678]*-*-nextstep*])
    gmp_cflags_gcc="$gmp_cflags_gcc -DBROKEN_ALIGN -DOLD_GAS"
    gmp_cflags_cc="$gmp_cflags_cc -DBROKEN_ALIGN -DOLD_GAS"
    ;;
  [i[3456]86*-*-* | pentium*-*-* | k[5678]*-*-* | athlon-*-*])
    gmp_cflags_gcc="-g -O -fomit-frame-pointer"
    ;;

  # Sparc
  [ultrasparc*-*-solaris2.[7-9] | sparcv9-*-solaris2.[7-9]])
    os_64bit=yes
    gmp_cflags_gcc="$gmp_cflags_gcc -Wa,-xarch=v8plus"
    gmp_xoptcflags_gcc="-mcpu=v9 -mcpu=v8 -mv8"
    gmp_cflags64_gcc="$gmp_cflags_gcc -Wa,-xarch=v9 -mcpu=v9"
    gmp_cflags_cc="-xtarget=native -xarch=v8 -xO4"
    gmp_cflags64_cc="-xtarget=native -xarch=v9 -xO4"
    ;;
  ultrasparc*-*-* | sparcv9-*-*)
    gmp_cflags_gcc="$gmp_cflags_gcc -Wa,-xarch=v8plus"
    gmp_xoptcflags_gcc="-mcpu=v9 -mcpu=v8 -mv8"
    gmp_cflags_cc="-xtarget=native -xarch=v8 -xO4"
    ;;
  sparcv8*-*-solaris2.* | microsparc*-*-solaris2.*)
    gmp_cflags_gcc="$gmp_cflags_gcc"
    gmp_xoptcflags_gcc="-mcpu=v8 -mv8"
    gmp_cflags_cc="-xtarget=native -xarch=v8 -xO4"
    ;;
  sparcv8*-*-* | microsparc*-*-*)		# SunOS, Linux, *BSD
    cclist="gcc acc cc"
    gmp_cflags_gcc="$gmp_cflags_gcc"
    gmp_xoptcflags_gcc="-mcpu=v8 -mv8"
    gmp_cflags_acc="-g -O2 -cg92"
    gmp_cflags_cc="-O2"		# FIXME: Flag for v8?
    ;;
  supersparc*-*-solaris2.*)
    gmp_cflags_gcc="$gmp_cflags_gcc -DSUPERSPARC"
    gmp_xoptcflags_gcc="-mcpu=v8 -mv8"
    gmp_cflags_cc="-xtarget=native -xarch=v8 -xO4 -DSUPERSPARC"
    ;;
  supersparc*-*-*)		# SunOS, Linux, *BSD
    cclist="gcc acc cc"
    gmp_cflags_gcc="$gmp_cflags_gcc -DSUPERSPARC"
    gmp_xoptcflags_gcc="-mcpu=v8 -mv8"
    gmp_cflags_acc="-g -O2 -cg92 -DSUPERSPARC"
    gmp_cflags_cc="-O2 -DSUPERSPARC"	# FIXME: Flag for v8?
    ;;
  *sparc*-*-*)
    cclist="gcc acc cc"
    gmp_cflags_acc="-g -O2"
    gmp_cflags_cc="-g -O2"
    ;;

  # POWER/PowerPC
  powerpc64-*-aix*)
    cclist="gcc xlc"
    gmp_cflags_gcc="$gmp_cflags_gcc -maix64 -mpowerpc64"
    gmp_cflags_xlc="-g -O2 -q64 -qtune=pwr3"
    ;;
  powerpc*-*-aix*)
    cclist="gcc xlc"
    gmp_cflags_gcc="$gmp_cflags_gcc -mpowerpc"
    gmp_cflags_xlc="$gmp_cflags_cc -qarch=ppc -O2"
    ;;
  power-*-aix*)
    cclist="gcc xlc"
    gmp_cflags_gcc="$gmp_cflags_gcc -mpower"
    gmp_cflags_xlc="$gmp_cflags_cc -qarch=pwr -O2"
    ;;
  powerpc64*-*-*)
    gmp_cflags_gcc="$gmp_cflags_gcc -mpowerpc64"
    AC_DEFINE(_LONG_LONG_LIMB)	dnl FIXME: Remove.
    ;;
  powerpc*-*-*)
    gmp_cflags_gcc="$gmp_cflags_gcc -mpowerpc"
    ;;

  # MIPS
  mips-sgi-irix6.*)
    os_64bit=yes
    gmp_cflags64_gcc="-g -O2 -mabi=n32"
    gmp_cflags64_cc="$gmp_cflags64_cc -O2 -n32"
    ;;

  # Motorola 68k family
  m88110*-*-*)
    gmp_cflags_gcc="-g -O -m88110"	dnl  FIXME: Use `-O2'?
    ;;
  m68*-*-*)
    gmp_cflags_gcc="$gmp_cflags_gcc -fomit-frame-pointer"
    ;;

  # HP
  hppa1.0*-*-*)
    cclist="gcc c89 cc"
    gmp_cflags_c89="$gmp_cflags_cc +O2"
    gmp_cflags_cc="$gmp_cflags_cc +O2"
    ;;
  hppa2.0*-*-*)
    os_64bit=yes
    cclist="gcc c89 cc"
    gmp_cflags64_gcc="$gmp_cflags64_gcc -mWHAT -D_LONG_LONG_LIMB"
    # +O2 to cc triggers bug in mpz/powm.c (1.4)
    gmp_cflags64_c89="+DA2.0 +e +O3 -D_LONG_LONG_LIMB"
    gmp_cflags64_cc="+DA2.0 +e +O3 -D_LONG_LONG_LIMB"
    gmp_cflags_c89="$gmp_cflags_cc +O2"
    gmp_cflags_cc="$gmp_cflags_cc +O2"
    ;;

  # VAX
  vax*-*-*)
    gmp_cflags_gcc="$gmp_cflags_gcc -fomit-frame-pointer"
    ;;

  # Fujitsu
  [f30[01]-fujitsu-sysv*])
    cclist="gcc vcc"
    gmp_cflags_vcc="-g"		# FIXME: flags for vcc?
    ;;
esac

case "$target" in
  i386*-*-*)    gmp_optcflags_gcc="-mcpu=i386 -march=i386";;
  i486*-*-*)    gmp_optcflags_gcc="-mcpu=i486 -march=i486";;
  i586*-*-* | pentium-*-* | pentiummmx-*-*)
                gmp_optcflags_gcc="-mcpu=pentium -march=pentium";;
  [i686*-*-* | pentiumpro-*-* | pentium[23]-*-*])
                gmp_optcflags_gcc="-mcpu=pentiumpro";;
		# removed -march=pentiumpro since it triggers a bug in
		# mpn/powm.c (swox' cvs rev 1.4) with gcc 2.95.2
  k6*-*-*)      gmp_optcflags_gcc="-mcpu=k6 -march=k6";;

  # Athlon instruction costs are probably a bit closer to p6 than k6
  # (3 cycle load latency, 4-6 cycle mul, 40 cycle div, ...).
  # FIXME: Change this when gcc gets something specific for Athlon.
  athlon-*-*)   gmp_optcflags_gcc="-mcpu=pentiumpro";;
                # removed -march=pentiumpro, see i686 above for details
esac

# Save CFLAGS given on command line.
gmp_user_CFLAGS="$CFLAGS"

if test -z "$CC"; then
  # Find compiler.
  GMP_PROG_CC_FIND($cclist, $os_64bit)

  # If 64-bit OS and we have a 64-bit compiler, use it.
  if test -n "$os_64bit" && test -n "$CC64"; then
    CC=$CC64
    CFLAGS=$CFLAGS64
  else
    eval CFLAGS=\$gmp_cflags_$CC  
  fi

  # Try compiler flags that may work with only some compiler versions.
  # gmp_optcflags: All or nothing.
  eval optcflags=\$gmp_optcflags_$CC
  if test -n "$optcflags"; then
    CFLAGS_save="$CFLAGS"
    CFLAGS="$CFLAGS $optcflags"
    AC_MSG_CHECKING([whether $CC accepts $optcflags])
    AC_LANG_C
    AC_TRY_COMPILER([int main(){return(0);}], optok, cross)
    if test "$optok" = "yes"; then
      AC_MSG_RESULT([yes])
    else
      AC_MSG_RESULT([no])
      CFLAGS="$CFLAGS_save"
    fi
  fi
  # gmp_xoptcflags: First is best, one has to work.
  eval xoptcflags=\$gmp_xoptcflags_$CC
  if test -n "$xoptcflags"; then
    gmp_found="no"
    for xopt in $xoptcflags; do
      CFLAGS_save="$CFLAGS"
      CFLAGS="$CFLAGS $xopt"
      AC_MSG_CHECKING([whether $CC accepts $xopt])
      AC_LANG_C
      AC_TRY_COMPILER([int main(){return(0);}], optok, cross)
      if test "$optok" = "yes"; then
        AC_MSG_RESULT([yes])
        gmp_found="yes"
        break
      else
        AC_MSG_RESULT([no])
        CFLAGS="$CFLAGS_save"
      fi
    done
    if test "$gmp_found" = "no"; then
      echo ["$0: fatal: need a compiler that understands one of $xoptcflags"]
      exit 1
    fi
  fi
fi

# Restore CFLAGS given on command line.
# FIXME: We've run through quite some unnecessary code looking for a
# nice compiler and working flags for it, just to spoil that with user
# supplied flags.
test -n "$gmp_user_CFLAGS" && CFLAGS="$gmp_user_CFLAGS"

# Select chosen compiler.
GMP_PROG_CC_SELECT

# How to assemble.
CCAS="$CC -c"
dnl  FIXME: CCAS may change below.  Do we have to move the AC_SUBST until 
dnl  after that?
AC_SUBST(CCAS)

dnl Checks for programs.
dnl --------------------
AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_CHECK_PROG(M4, m4, m4, :)
if test "$M4" = ":"; then
  echo ["$0: fatal: need m4"]
  exit 1
fi  
AC_CHECK_TOOL(AR, ar, ar)
AC_CHECK_TOOL(NM, nm, :)
if test "$NM" = ":"; then
  echo ["$0: fatal: need nm"]
  exit 1
fi
# nm and ar on AIX needs to know the object file format 
case "$target" in
  powerpc64*-*-aix*)
    AR="$AR -X 64"
    NM="$NM -X 64"
    ;;
esac   
dnl  FIXME: Find good ld?  /usr/ucb/ld on Solaris won't work.

dnl Checks for assembly syntax.
if test "$gmp_no_asm_syntax_testing" != "yes"; then
  GMP_CHECK_ASM_TEXT
  GMP_CHECK_ASM_DATA
  GMP_CHECK_ASM_GLOBL
  GMP_CHECK_ASM_LABEL_SUFFIX
  GMP_CHECK_ASM_TYPE
  GMP_CHECK_ASM_SIZE
  GMP_CHECK_ASM_LSYM_PREFIX
  GMP_CHECK_ASM_W32
  GMP_CHECK_ASM_UNDERSCORE(underscore=yes, underscore=no)
  GMP_CHECK_ASM_ALIGN_LOG(asm_align=log, asm_align=nolog)
fi

dnl  FIXME: Check for FPU and set `floating_point' appropriately.

dnl  ========================================
dnl  Configuring mpn.
dnl  ----------------------------------------
dnl  Set the following target specific variables:
dnl  path		where to search for source files
dnl  family		processor family (Needed for building 
dnl 			asm-syntax.h for now.  FIXME: Remove.)
dnl  extra_functions	extra functions
dnl  gmp_mulfunc_files  files containing more than one function
family=generic
gmp_mulfunc_files=

case ${target} in
  arm*-*-*)
    path="arm"
    ;;
  [sparcv9*-*-solaris2.[789]* | sparc64*-*-solaris2.[789]* | ultrasparc*-*-solaris2.[789]*])
    family=sparc
    if test -n "$CC64"; then
      path="sparc64"
      extra_functions="copyi"
    else
      path="sparc32/v9 sparc32/v8 sparc32"
    fi
    ;;
  sparcv8*-*-* | microsparc*-*-*)
	path="sparc32/v8 sparc32"
	if test x${floating_point} = xno
	  then extra_functions="udiv_nfp"
	  else extra_functions="udiv_fp"
	fi
	family=sparc;;
  sparcv9*-*-* | ultrasparc*-*-*)
	path="sparc32/v9 sparc32/v8 sparc32"
	extra_functions="udiv_fp"
	family=sparc;;
  supersparc*-*-*)
	path="sparc32/v8/supersparc sparc32/v8 sparc32"
	extra_functions="udiv"
	family=sparc;;
  sparc*-*-*) path="sparc32"
	if test x${floating_point} = xno
	  then extra_functions="udiv_nfp"
	  else extra_functions="udiv_fp"
	fi
	family=sparc;;
  hppa7000*-*-*)
    path="hppa/hppa1_1 hppa"
    extra_functions="udiv_qrnnd"
    ;;
  hppa1.0*-*-*)
    path="hppa"
    extra_functions="udiv_qrnnd"
    ;;
  hppa2.0*-*-*)
    if test -n "$CC64"; then
      path="pa64"
      extra_functions="umul_ppmm udiv_qrnnd"
      # We need to use the system compiler, or actually the system assembler,
      # since GAS has not been ported to understand the 2.0 instructions.
      CCAS="$CC64 -c"
    else
      # FIXME: path should be "hppa/hppa2_0 hppa/hppa1_1 hppa"
      path="hppa/hppa1_1 hppa"
      extra_functions="udiv_qrnnd"
    fi
    ;;
  hppa*-*-*)					#assume pa7100
    path="hppa/hppa1_1/pa7100 hppa/hppa1_1 hppa"
    extra_functions="udiv_qrnnd";;
  [cray2-cray-unicos* | [xy]mp-cray-unicos* | [ctj]90-cray-unicos*])
    path="cray";;
  [f30[01]-fujitsu-sysv*])
    path=fujitsu;;
  alphaev6*-*-*) path="alpha/ev6 alpha"; extra_functions="invert_limb cntlz";;
  alphaev5*-*-*) path="alpha/ev5 alpha"; extra_functions="invert_limb cntlz";;
  alpha*-*-*) path="alpha"; extra_functions="invert_limb cntlz";;
  am29000*-*-*) path="a29k";;
  a29k*-*-*) path="a29k";;

# Amd and Intel x86 configurations
  [i[34]86*-*-*])
	path="x86"
	family=x86;;
  i586*-*-* | pentium-*-*)
	path="x86/pentium x86"
	family=x86;;
  pentiummmx-*-*)
	path=
	GMP_CHECK_ASM_MMX(path="$path x86/pentium/mmx")
	path="$path x86/pentium x86"
	family=x86;;
  i686*-*-* | pentiumpro-*-*)
	path="x86/p6 x86"
	gmp_mulfunc_files="aorsmul_1"
	gmp_mf_aorsmul_1="addmul_1 submul_1"
	family=x86;;
  [pentium[23]-*-*])
	# The pentium/mmx lshift and rshift are good on p6 and can be used
        # until there's something specific for p6.
	path=
	GMP_CHECK_ASM_MMX(path="$path x86/p6/mmx x86/pentium/mmx")
	path="$path x86/p6 x86"
	gmp_mulfunc_files="aorsmul_1"
	gmp_mf_aorsmul_1="addmul_1 submul_1"
	family=x86;;
  k6*-*-*)
	GMP_CHECK_ASM_MMX(tmp_mmx=yes, tmp_mmx=no)

	path=
	test "$tmp_mmx" = "yes" && path="$path x86/k6/mmx"
	path="$path x86/k6 x86"

	gmp_mulfunc_files="aors_n aorsmul_1"
	gmp_mf_aors_n="add_n sub_n"
	gmp_mf_aorsmul_1="addmul_1 submul_1"

	if test "$tmp_mmx" = "yes"; then
	  extra_functions="com_n and_n andn_n nand_n ior_n iorn_n nior_n xor_n xnor_n"
	  gmp_mulfunc_files="$gmp_mulfunc_files logops_n"
          gmp_mf_logops_n="and_n andn_n nand_n ior_n iorn_n nior_n xor_n xnor_n"
	fi

	family=x86;;
  athlon-*-*)
	path=
	GMP_CHECK_ASM_MMX(path="$path x86/k7/mmx")
	path="$path x86/k7 x86"
	gmp_mulfunc_files="aors_n aorsmul_1"
	gmp_mf_aors_n="add_n sub_n"
	gmp_mf_aorsmul_1="addmul_1 submul_1"
	family=x86;;
  i960*-*-*) path="i960";;

# Motorola 68k configurations.  Let m68k mean 68020-68040.
  [m680[234]0*-*-* | m68k*-*-* | \
  m68*-next-nextstep*])		# Nexts are at least '020
    path="m68k/mc68020 m68k"
    family=m68k
    ;;
  m68000*-*-*)
    path="m68k"
    family=m68k
    ;;

  m88k*-*-* | m88k*-*-*) path="m88k";;
  m88110*-*-*) path="m88k/mc88110 m88k";;
  ns32k*-*-*) path="ns32k";;

  pyramid-*-*) path="pyr";;

  ppc601-*-*) path="power powerpc32";;
  powerpc64*-*-*) path="powerpc64";;
  powerpc*-*-*) path="powerpc32";;
  rs6000-*-* | power-*-* | power2-*-*)
    path="power"
    extra_functions="udiv_w_sdiv"
    ;;

  sh-*-*) path="sh";;
  sh2-*-*) path="sh/sh2 sh";;

  [mips[34]*-*-*]) path="mips3";;
  mips*-*-irix6*) path="mips3";;
  mips*-*-*) path="mips2";;

  vax*-*-*) path="vax"; extra_functions="udiv_w_sdiv";;

  z8000x*-*-*) path="z8000x"; extra_functions="udiv_w_sdiv";;
  z8000*-*-*) path="z8000"; extra_functions="udiv_w_sdiv";;

  clipper*-*-*) path="clipper";;
esac

case ${target} in
  [i?86*-*-* | k[5-8]*-*-* | pentium*-*-* | athlon-*-*])
    extra_functions="$extra_functions copyi copyd"
    GMP_CHECK_ASM_SHLDL_CL(
      [GMP_DEFINE(WANT_SHLDL_CL,1)],
      [GMP_DEFINE(WANT_SHLDL_CL,0)])
    ;;
esac


dnl Extensions for executable and object files.
dnl -------------------------------------------
AC_EXEEXT
AC_OBJEXT

dnl Use Libtool.
dnl ------------
dnl  FIXME: Shared libs seem to fail on aix4.3.
dnl  FIXME: HP assembly code not PIC aware yet.
dnl  FIXME: Should invoke [AC_DISABLE_SHARED], but m4 recurses to death.
case "$target" in
  hppa*-*-*) enable_shared=no ;;
  *-*-aix*) enable_shared=no ;;
esac
AC_PROG_LIBTOOL

dnl Checks for libraries.
dnl ---------------------
AC_CHECK_DECLS((optarg))

dnl Checks for header files.
dnl ------------------------
AC_HEADER_STDC

dnl Checks for typedefs, structures, and compiler characteristics.
AC_CHECK_TYPES((void))
AC_C_STRINGIZE

dnl Checks for library functions.
dnl AC_FUNC_MEMCMP
dnl AC_TYPE_SIGNAL
dnl AC_CHECK_FUNCS(strtol strtoul)

dnl  Set `syntax' to one of <blank>, "mit", "elf", "aix", "macho".
syntax=
# For now, we use the old switch for setting syntax.  
# FIXME: Remove when conversion to .asm is completed.
changequote(,)dnl
case "${target}" in
  m680[234]0*-*-linuxaout* | m68k*-*-linuxaout* | \
  m68k-next-nextstep* | \
  m68000*-*-*)
    syntax=mit
    ;;
  m680[234]0*-*-linux* | m68k*-*-linux*)
    syntax=elf
    ;;
  m680[234]0*-*-* | m68k*-*-*)
    syntax=mit
    ;;
  *-*-cygwin*)		# x86-cygwin kludge
    syntax=cygwin
    ;;
esac
changequote([,])dnl

dnl  ----------------------------------------
# Now build an asm-syntax.h file for targets that include that from the
# assembly files.
# FIXME: Remove when conversion to .asm is completed.
case "${family}-${underscore}-${asm_align}-${syntax}" in
  x86-yes-log-*)
    echo '#define BSD_SYNTAX' >asm-syntax.h
    cat $srcdir/mpn/underscore.h >>asm-syntax.h
    echo '#include "'$srcdir'/mpn/x86/syntax.h"' >>asm-syntax.h;;
  x86-no-nolog-*)
    echo '#define ELF_SYNTAX' >asm-syntax.h
    echo '#define C_SYMBOL_NAME(name) name' >>asm-syntax.h
    echo '#include "'$srcdir'/mpn/x86/syntax.h"' >>asm-syntax.h;;
  x86-yes-nolog-cygwin)		# x86-cygwin kludge.
    # Select BSD_SYNTAX to avoid .type/.size in PROLOGUE.
    echo '#define BSD_SYNTAX' 			 >asm-syntax.h
    cat $srcdir/mpn/underscore.h 		 >>asm-syntax.h
    echo '#include "'$srcdir'/mpn/x86/syntax.h"' >>asm-syntax.h
    # KLUDGE: Override ALIGN definition.
    echo '#ifdef ALIGN' 			 >>asm-syntax.h
    echo '#undef ALIGN' 			 >>asm-syntax.h
    echo '#endif'        			 >>asm-syntax.h
    echo '#define ALIGN(log) .align 1<<(log)' 	 >>asm-syntax.h
    ;;
  x86-yes-nolog-*)	# FIXME: Anyone using this?
    echo '#define ELF_SYNTAX' >asm-syntax.h
    cat $srcdir/mpn/underscore.h >>asm-syntax.h
    echo '#include "'$srcdir'/mpn/x86/syntax.h"' >>asm-syntax.h
    ;;
  m68k-yes-log-mit)
    echo '#define MIT_SYNTAX' >asm-syntax.h
    cat $srcdir/mpn/underscore.h >>asm-syntax.h
    echo '#include "'$srcdir'/mpn/m68k/syntax.h"' >>asm-syntax.h;;
  m68k-no-nolog-elf)
    echo '#define ELF_SYNTAX' >asm-syntax.h
    echo '#define C_SYMBOL_NAME(name) name' >>asm-syntax.h
    echo '#include "'$srcdir'/mpn/m68k/syntax.h"' >>asm-syntax.h;;
esac

# Set up `gmp_links'.  It's a list of link:file pairs that configure will
# process to create link -> file.
gmp_links=

# Include assembly definition files and link them to mpn/.
# <src>/mpn/asm-defs.m4 gets linked to <build>mpn/asm.m4.  The change
# of name is necessary if we build in source directory, or the ln will
# fail.
gmp_links="$gmp_links mpn/asm.m4:mpn/asm-defs.m4"
GMP_INCLUDE(asm.m4)

dnl  config.m4 post-includes
dnl  -----------------------
changequote(,)dnl
gmp_m4postinc=
case "$target" in
  alpha*-cray-unicos*)
    gmp_m4postinc="$gmp_m4postinc mpn/alpha/unicos.m4"
    ;;
  alpha*-*-*)
    gmp_m4postinc="$gmp_m4postinc mpn/alpha/default.m4"
    ;;
  i?86*-*-* | k[5-8]*-*-* | pentium*-*-* | athlon-*-*)
    gmp_m4postinc="$gmp_m4postinc mpn/x86/x86-defs.m4"
    ;;
  power*-*-*)
    case "$target" in
      *-*-mach* | *-*-rhapsody* | *-*-nextstep*)
        ;;	# these use non-conventional assembly syntax.
      powerpc64-*-aix*)
	gmp_m4postinc="$gmp_m4postinc mpn/powerpc32/regmap.m4"
	gmp_m4postinc="$gmp_m4postinc mpn/powerpc64/aix.m4"
        ;;
      *-*-aix*)
	gmp_m4postinc="$gmp_m4postinc mpn/powerpc32/regmap.m4"
	gmp_m4postinc="$gmp_m4postinc mpn/powerpc32/aix.m4"
        ;;
      *)
	gmp_m4postinc="$gmp_m4postinc mpn/powerpc32/regmap.m4"
	;;
    esac
    ;;
esac
changequote([, ])dnl

# Include post-include files and link them to mpn/.
for tmp_f in $gmp_m4postinc; do
  tmp_basename_f=`echo $tmp_f | sed 's;.*/;;'`
  rm -f mpn/$tmp_f
  gmp_links="$gmp_links mpn/${tmp_basename_f}:${tmp_f}"
  GMP_INCLUDE($tmp_basename_f)
done

# Path from mpn build subdirectory to top source directory.
# The regexp testing for an absolute path is the same as in
# _AC_OUTPUT_FILES of autoconf acgeneral.m4.
case "$srcdir" in
[[\\/]]* | ?:[[\\/]]* )
     GMP_DEFINE_RAW(["define(<CONFIG_SRCDIR>,<\`$srcdir'>)"])    ;;
*)   GMP_DEFINE_RAW(["define(<CONFIG_SRCDIR>,<\`../$srcdir'>)"]) ;;
esac
 
# After including asm.m4.
GMP_DEFINE_RAW("define_not_for_expansion(\`HAVE_TARGET_CPU_$target_cpu')", POST)

# Pick the correct source files in `path' and link them to mpn/.
# `gmp_mpn_functions' lists all functions that we need.  

# The general rule is to suffix the function name with .asm, .S, .s,
# or .c to find a file containing the implementation.  Before looking
# for these, we have to search for files containing more than one
# function.  These are listed in `gmp_mulfunc_files'.  There is one
# `gmp_mf_<file>' for each multifunc file, listing which functions the
# file contains.  The multifunc files are symlinked to <funcname>.asm
# and preprocessed with -DOPERATION_<funcname>.

# If the user specified `mpn_path', use that instead of the path we've
# come up with.
if test -z "$mpn_path"; then
  path="$path generic"
else
  path="$mpn_path"
fi

mpn_objects=
gmp_mpn_functions="${extra_functions} inlines add_n sub_n mul_1 addmul_1 \
  submul_1 lshift rshift diveby3 divmod_1 divrem divrem_1 divrem_2 \
  mod_1 pre_mod_1 dump mul mul_n mul_basecase sqr_basecase random \
  random2 sqrtrem get_str set_str scan0 scan1 popcount hamdist cmp perfsqr \
  bdivmod gcd_1 gcd gcdext tdiv_qr bz_divrem_n sb_divrem_mn"
mpn_objs_in_libgmp="mpn/mp_bases.lo"

# Before possibly destroying `gmp_mpn_functions', set `mpn_objects'
# and `mpn_objs_in_libgmp' to the list of all object files, used by
# mpn/Makefile.in and top Makefile.in, respectively.

for tmp_fn in ${gmp_mpn_functions} ; do
  mpn_objects="$mpn_objects ${tmp_fn}.lo"
  mpn_objs_in_libgmp="$mpn_objs_in_libgmp mpn/${tmp_fn}.lo"
done

# Look for multi-function files.
for m in $gmp_mulfunc_files; do
  rm -f mpn/$m.asm
  for d in $path; do
    if test -f $srcdir/mpn/$d/$m.asm; then
      # find entry points
changequote(<<, >>)
      gmp_ep=`grep "^[ 	]*MULFUNC_PROLOGUE" $srcdir/mpn/$d/$m.asm \
             | sed 's/.*:[ 	]*MULFUNC_PROLOGUE(_*\(.*\))/\1/'`
changequote([, ])
      for gmp_tmp in $gmp_ep; do
        AC_DEFINE_UNQUOTED(HAVE_NATIVE_${gmp_tmp})
      done
      eval mf=\$gmp_mf_$m
      for f in $mf; do
        gmp_links="$gmp_links mpn/$f.asm:mpn/$d/$m.asm"
        # remove this function from `gmp_mpn_functions'
        tmp_funcs=
        for g in $gmp_mpn_functions; do
          test "$g" != "$f" && tmp_funcs="$tmp_funcs $g"
        done
        gmp_mpn_functions="$tmp_funcs"
      done	
      break
    fi
  done
done

# Create links for .asm, .S, .s, or .c files.
for tmp_fn in ${gmp_mpn_functions} ; do
  rm -f mpn/${tmp_fn}.[Ssc] mpn/${tmp_fn}.asm
  gmp_ep=
  gmp_tmp=
  for tmp_dir in $path ; do
    if test -f $srcdir/mpn/${tmp_dir}/${tmp_fn}.asm ; then
      gmp_links="$gmp_links mpn/${tmp_fn}.asm:mpn/${tmp_dir}/${tmp_fn}.asm"
changequote(<<, >>)
      gmp_ep=`grep "^[ 	]*PROLOGUE" $srcdir/mpn/${tmp_dir}/${tmp_fn}.asm \
             | sed 's/.*:[ 	]*PROLOGUE(_*\(.*\))/\1/'`
changequote([, ])
      break
    elif test -f $srcdir/mpn/${tmp_dir}/${tmp_fn}.S ; then
      gmp_links="$gmp_links mpn/${tmp_fn}.S:mpn/${tmp_dir}/${tmp_fn}.S"
changequote(<<, >>)
      gmp_ep=`grep "^[ 	]*PROLOGUE" $srcdir/mpn/${tmp_dir}/${tmp_fn}.S \
             | sed 's/.*:[ 	]*PROLOGUE(C_SYMBOL_NAME(_*\(.*\)))/\1/'`
changequote([, ])
      break
    elif test -f $srcdir/mpn/${tmp_dir}/${tmp_fn}.s ; then
      gmp_links="$gmp_links mpn/${tmp_fn}.s:mpn/${tmp_dir}/${tmp_fn}.s"
      break
    elif test -f $srcdir/mpn/${tmp_dir}/${tmp_fn}.c ; then
      gmp_links="$gmp_links mpn/${tmp_fn}.c:mpn/${tmp_dir}/${tmp_fn}.c"
      break
    fi
  done
  for gmp_tmp in $gmp_ep; do
    AC_DEFINE_UNQUOTED(HAVE_NATIVE_${gmp_tmp})
  done
done

# Create link for gmp-mparam.h.
for tmp_dir in $path ; do
  rm -f gmp-mparam.h
  if test -f $srcdir/mpn/${tmp_dir}/gmp-mparam.h ; then
    gmp_links="$gmp_links gmp-mparam.h:mpn/${tmp_dir}/gmp-mparam.h"
    break
  fi
done

# Dig out the links from `gmp_links' for inclusion in DISTCLEANFILES.
gmp_srclinks=
for f in $gmp_links; do
  gmp_srclinks="$gmp_srclinks `echo $f | sed 's/\(.*\):.*/\1/'`"
done

AC_SUBST(mpn_objects)
AC_SUBST(mpn_objs_in_libgmp)
AC_SUBST(gmp_srclinks)

dnl  ----------------------------------------
dnl  Make links.
AC_CONFIG_LINKS($gmp_links)

dnl  Create config.m4.
GMP_FINISH

dnl  Create Makefiles
dnl  FIXME: Upcoming version of autoconf/automake may not like broken lines.
AC_OUTPUT(Makefile mpf/Makefile mpz/Makefile mpn/Makefile mpq/Makefile \
  mpf/tests/Makefile mpz/tests/Makefile mpq/tests/Makefile \
  tests/Makefile tests/rand/Makefile demos/Makefile \
  mpbsd/Makefile mpbsd/tests/Makefile)
