dnl Process this file with autoconf to produce a configure script

AC_PREREQ(2.13)
AC_INIT(xmalloc.c)

# This works around the fact that libtool configuration may change LD
# for this particular configuration, but some shells, instead of
# keeping the changes in LD private, export them just because LD is
# exported.  We don't use libtool yet, but some day we might, so...
ORIGINAL_LD_FOR_MULTILIBS=$LD

dnl We use these options to decide which functions to include.
AC_ARG_WITH(target-subdir,
[  --with-target-subdir=SUBDIR      Configuring in a subdirectory for target])
AC_ARG_WITH(build-subdir,
[  --with-build-subdir=SUBDIR       Configuring in a subdirectory for build])
AC_ARG_WITH(cross-host,
[  --with-cross-host=HOST           Configuring with a cross compiler])
AC_ARG_WITH(newlib,
[  --with-newlib                    Configuring with newlib])

if test "${srcdir}" = "."; then
  if test -n "${with_build_subdir}"; then
    libiberty_topdir="${srcdir}/../.."
    with_target_subdir=
  elif test -z "${with_target_subdir}"; then
    libiberty_topdir="${srcdir}/.."
  else
    if test "${with_target_subdir}" != "."; then
      libiberty_topdir="${srcdir}/${with_multisrctop}../.."
    else
      libiberty_topdir="${srcdir}/${with_multisrctop}.."
    fi
  fi
else
  libiberty_topdir="${srcdir}/.."
fi
AC_SUBST(libiberty_topdir)
AC_CONFIG_AUX_DIR($libiberty_topdir)

dnl Very limited version of automake's enable-maintainer-mode

AC_MSG_CHECKING([whether to enable maintainer-specific portions of Makefiles])
  dnl maintainer-mode is disabled by default
  AC_ARG_ENABLE(maintainer-mode,
[  --enable-maintainer-mode
                          enable make rules and dependencies not useful
                          (and sometimes confusing) to the casual installer],
      maintainer_mode=$enableval,
      maintainer_mode=no)

AC_MSG_RESULT($maintainer_mode)

if test "$maintainer_mode" = "yes"; then
  MAINT=''
  NOTMAINT='#'
else
  MAINT='#'
  NOTMAINT=''
fi
AC_SUBST(MAINT)dnl
AC_SUBST(NOTMAINT)dnl

# Do we have a single-tree copy of texinfo?  Even if we do, we can't
# rely on it - libiberty is built before texinfo.
AC_CHECK_PROG(MAKEINFO, makeinfo, makeinfo, )
if test "x$MAKEINFO" = "x"; then
    MAKEINFO="@echo makeinfo missing; true"
    BUILD_INFO=
else
    BUILD_INFO=info
    case x"`$MAKEINFO --version | grep 'GNU texinfo'`" in
      x*\ [[1-3]].* )
	MAKEINFO="@echo $MAKEINFO is too old, 4.0 or newer required; true"
	BUILD_INFO=
	AC_MSG_WARN([
*** Makeinfo is too old. Info documentation will not be built.])
	;;
    esac
fi
AC_SUBST(MAKEINFO)
AC_SUBST(BUILD_INFO)

AC_CHECK_PROG(PERL, perl, perl, )
if test x"$PERL" = x""; then
  HAVE_PERL='#'
else
  HAVE_PERL=''
fi
AC_SUBST(HAVE_PERL)

AC_CANONICAL_HOST

dnl When we start using automake:
dnl AM_INIT_AUTOMAKE(libiberty, 1.0)

dnl These must be called before AM_PROG_LIBTOOL, because it may want
dnl to call AC_CHECK_PROG.
AC_CHECK_TOOL(AR, ar)
AC_CHECK_TOOL(RANLIB, ranlib, :)

LIB_AC_PROG_CC

AC_PROG_CC_C_O
# autoconf is lame and doesn't give us any substitution variable for this.
if eval "test \"`echo '$ac_cv_prog_cc_'${ac_cc}_c_o`\" = no"; then
  NO_MINUS_C_MINUS_O=yes
else
  OUTPUT_OPTION='-o $@'
fi
AC_SUBST(NO_MINUS_C_MINUS_O)
AC_SUBST(OUTPUT_OPTION)

AC_ISC_POSIX
AC_C_CONST
AC_C_INLINE

dnl When we start using libtool:
dnl Default to a non shared library.  This may be overridden by the
dnl configure option --enable-shared.
dnl AM_DISABLE_SHARED

dnl When we start using libtool:
dnl AM_PROG_LIBTOOL

dnl When we start using automake:
dnl AM_CONFIG_HEADER(config.h:config.in)
AC_CONFIG_HEADER(config.h:config.in)

dnl When we start using automake:
dnl AM_MAINTAINER_MODE
dnl AC_EXEEXT

dnl When we start using automake:
dnl AM_PROG_INSTALL
AC_PROG_INSTALL

. ${srcdir}/config.table
host_makefile_frag=${frag}
AC_SUBST_FILE(host_makefile_frag)

# It's OK to check for header files.  Although the compiler may not be
# able to link anything, it had better be able to at least compile
# something.
AC_CHECK_HEADERS(sys/file.h sys/param.h limits.h stdlib.h string.h unistd.h strings.h sys/time.h time.h sys/resource.h sys/stat.h sys/mman.h fcntl.h alloca.h sys/pstat.h sys/sysmp.h sys/sysinfo.h machine/hal_sysinfo.h sys/table.h sys/sysctl.h sys/systemcfg.h)
AC_HEADER_SYS_WAIT
AC_HEADER_TIME

libiberty_AC_DECLARE_ERRNO

AC_CHECK_TYPE(uintptr_t, unsigned long)
# Given the above check, we always have uintptr_t or a fallback
# definition.  So define HAVE_UINTPTR_T in case any imported code
# relies on it.
AC_DEFINE(HAVE_UINTPTR_T, 1, [Define if you have the \`uintptr_t' type.])

AC_TYPE_PID_T

# This is the list of functions which libiberty will provide if they
# are not available on the host.

funcs="asprintf"
funcs="$funcs atexit"
funcs="$funcs basename"
funcs="$funcs bcmp"
funcs="$funcs bcopy"
funcs="$funcs bsearch"
funcs="$funcs bzero"
funcs="$funcs calloc"
funcs="$funcs clock"
funcs="$funcs ffs"
funcs="$funcs getcwd"
funcs="$funcs getpagesize"
funcs="$funcs index"
funcs="$funcs insque"
funcs="$funcs memchr"
funcs="$funcs memcmp"
funcs="$funcs memcpy"
funcs="$funcs memmove"
funcs="$funcs mempcpy"
funcs="$funcs memset"
funcs="$funcs mkstemps"
funcs="$funcs putenv"
funcs="$funcs random"
funcs="$funcs rename"
funcs="$funcs rindex"
funcs="$funcs setenv"
funcs="$funcs snprintf"
funcs="$funcs sigsetmask"
funcs="$funcs stpcpy"
funcs="$funcs stpncpy"
funcs="$funcs strcasecmp"
funcs="$funcs strchr"
funcs="$funcs strdup"
funcs="$funcs strncasecmp"
funcs="$funcs strrchr"
funcs="$funcs strstr"
funcs="$funcs strtod"
funcs="$funcs strtol"
funcs="$funcs strtoul"
funcs="$funcs tmpnam"
funcs="$funcs vasprintf"
funcs="$funcs vfprintf"
funcs="$funcs vprintf"
funcs="$funcs vsnprintf"
funcs="$funcs vsprintf"
funcs="$funcs waitpid"

# Also in the old function.def file: alloca, vfork, getopt.

vars="sys_errlist sys_nerr sys_siglist _system_configuration"

checkfuncs="getrusage on_exit psignal strerror strsignal sysconf times sbrk gettimeofday"
checkfuncs="$checkfuncs realpath canonicalize_file_name pstat_getstatic pstat_getdynamic sysmp"
checkfuncs="$checkfuncs getsysinfo table sysctl"

# These are neither executed nor required, but they help keep
# autoheader happy without adding a bunch of text to acconfig.h.
if test "x" = "y"; then
  AC_CHECK_FUNCS(asprintf atexit basename bcmp bcopy bsearch bzero calloc clock)
  AC_CHECK_FUNCS(getcwd getpagesize index insque mkstemps memchr memcmp memcpy)
  AC_CHECK_FUNCS(memmove mempcpy memset putenv random rename rindex sigsetmask)
  AC_CHECK_FUNCS(strcasecmp setenv stpcpy stpncpy strchr strdup strncasecmp strrchr strstr)
  AC_CHECK_FUNCS(strtod strtol strtoul tmpnam vasprintf vfprintf vprintf)
  AC_CHECK_FUNCS(vsprintf waitpid getrusage on_exit psignal strerror strsignal)
  AC_CHECK_FUNCS(sysconf times sbrk gettimeofday ffs snprintf vsnprintf)
  AC_CHECK_FUNCS(pstat_getstatic pstat_getdynamic sysmp getsysinfo table sysctl)
  AC_CHECK_FUNCS(realpath canonicalize_file_name)
  AC_DEFINE(HAVE_SYS_ERRLIST, 1, [Define if you have the sys_errlist variable.])
  AC_DEFINE(HAVE_SYS_NERR,    1, [Define if you have the sys_nerr variable.])
  AC_DEFINE(HAVE_SYS_SIGLIST, 1, [Define if you have the sys_siglist variable.])
  AC_DEFINE(HAVE__SYSTEM_CONFIGURATION, 1, [Define if you have the _system_configuration variable.])
fi

# For each of these functions, if the host does not provide the
# function we want to put FN.o in LIBOBJS, and if the host does
# provide the function, we want to define HAVE_FN in config.h.

setobjs=
CHECK=
target_header_dir=
if test -n "${with_target_subdir}"; then

  # We are being configured as a target library.  AC_REPLACE_FUNCS
  # may not work correctly, because the compiler may not be able to
  # link executables.  Note that we may still be being configured
  # native.

  # If we are being configured for newlib, we know which functions
  # newlib provide and which ones we will be expected to provide.

  if test "x${with_newlib}" = "xyes"; then
    LIBOBJS="asprintf.o basename.o insque.o random.o strdup.o vasprintf.o"

    for f in $funcs; do
      case "$f" in
	asprintf | basename | insque | random | strdup | vasprintf)
	  ;;
	*)
	  n=HAVE_`echo $f | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'`
	  AC_DEFINE_UNQUOTED($n)
	  ;;
      esac
    done

    # newlib doesnt provide any of the variables in $vars, so we
    # dont have to check them here.

    # Of the functions in $checkfuncs, newlib only has strerror.
    AC_DEFINE_NOAUTOHEADER(HAVE_STRERROR)

    setobjs=yes

  fi

  # We may wish to install the target headers somewhere.
  AC_ARG_ENABLE(install-libiberty,
  [  --enable-install-libiberty       Install headers for end users],
  enable_install_libiberty=$enableval,
  enable_install_libiberty=no)dnl
  
  # Option parsed, now set things appropriately.
  case x"$enable_install_libiberty" in
    xyes|x)
      target_header_dir=libiberty
      ;;
    xno)   
      target_header_dir=
      ;;
    *) 
      # This could be sanity-checked in various ways...
      target_header_dir="${enable_install_libiberty}"
      ;;
  esac


else

   # Not a target library, so we set things up to run the test suite.
   CHECK=check-cplus-dem

fi

AC_SUBST(CHECK)
AC_SUBST(target_header_dir)

case "${host}" in
  *-*-cygwin* | *-*-mingw*)
    AC_DEFINE_NOAUTOHEADER(HAVE_SYS_ERRLIST)
    AC_DEFINE_NOAUTOHEADER(HAVE_SYS_NERR)
    ;;
esac

if test -z "${setobjs}"; then
  case "${host}" in

  *-*-vxworks*)
    # Handle VxWorks configuration specially, since on VxWorks the
    # libraries are actually on the target board, not in the file
    # system.
    LIBOBJS="basename.o getpagesize.o insque.o random.o strcasecmp.o"
    LIBOBJS="$LIBOBJS strncasecmp.o strdup.o vfork.o waitpid.o vasprintf.o"
    for f in $funcs; do
      case "$f" in
	basename | getpagesize | insque | random | strcasecmp)
	  ;;
	strncasecmp | strdup | vfork | waitpid | vasprintf)
	  ;;
	*)
	  n=HAVE_`echo $f | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'`
	  AC_DEFINE_UNQUOTED($n)
	  ;;
      esac
    done

    # VxWorks doesn't provide any of the variables in $vars, so we
    # don't have to check them here.

    # Of the functions in $checkfuncs, VxWorks only has strerror.
    AC_DEFINE_NOAUTOHEADER(HAVE_STRERROR)

    setobjs=yes
    ;;

  esac
fi

if test -z "${setobjs}"; then

  case "${host}" in

  *-*-cygwin*)
    # The Cygwin library actually uses a couple of files from
    # libiberty when it is built.  If we are building a native
    # Cygwin, and we run the tests, we will appear to have these
    # files.  However, when we go on to build winsup, we will wind up
    # with a library which does not have the files, since they should
    # have come from libiberty.

    # We handle this by removing the functions the winsup library
    # provides from our shell variables, so that they appear to be
    # missing.

    # DJ - only if we're *building* cygwin, not just building *with* cygwin
  
    if test -n "${with_target_subdir}"
    then
      funcs="`echo $funcs | sed -e 's/random//'`"
      LIBOBJS="$LIBOBJS random.o"
      vars="`echo $vars | sed -e 's/sys_siglist//'`"
      checkfuncs="`echo $checkfuncs | sed -e 's/strsignal//' -e 's/psignal//'`"
    fi
    ;;

  *-*-mingw32*)
    # Under mingw32, sys_nerr and sys_errlist exist, but they are
    # macros, so the test below won't find them.
    libiberty_cv_var_sys_nerr=yes
    libiberty_cv_var_sys_errlist=yes
    ;;

  *-*-uwin*)
    # Under some versions of uwin, vfork is notoriously buggy and the test 
    # can hang configure; on other versions, vfork exists just as a stub.
    # FIXME: This should be removed once vfork in uwin's runtime is fixed.
    ac_cv_func_vfork_works=no
    # Under uwin 2.0+, sys_nerr and sys_errlist exist, but they are
    # macros (actually, these are imported from a DLL, but the end effect 
    # is the same), so the test below won't find them.
    libiberty_cv_var_sys_nerr=yes
    libiberty_cv_var_sys_errlist=yes
    ;;

  *-*-*vms*)
    # Under VMS, vfork works very different than on Unix. The standard test 
    # won't work, and it isn't easily adaptable. It makes more sense to
    # just force it.
    ac_cv_func_vfork_works=yes
    ;;

  esac

  # We haven't set the list of objects yet.  Use the standard autoconf
  # tests.  This will only work if the compiler works.
  AC_PROG_CC_WORKS
  AC_REPLACE_FUNCS($funcs)
  libiberty_AC_FUNC_C_ALLOCA
  AC_FUNC_VFORK
  if test $ac_cv_func_vfork_works = no; then
    LIBOBJS="$LIBOBJS vfork.o"
  fi
  # We only need _doprnt if we might use it to implement v*printf.
  if test $ac_cv_func_vprintf != yes \
     || test $ac_cv_func_vfprintf != yes \
     || test $ac_cv_func_vsprintf != yes; then
    AC_REPLACE_FUNCS(_doprnt)
  else
    AC_CHECK_FUNCS(_doprnt)
  fi

  for v in $vars; do
    AC_MSG_CHECKING([for $v])
    AC_CACHE_VAL(libiberty_cv_var_$v,
      [AC_TRY_LINK([int *p;], [extern int $v []; p = $v;],
		   [eval "libiberty_cv_var_$v=yes"],
		   [eval "libiberty_cv_var_$v=no"])])
    if eval "test \"`echo '$libiberty_cv_var_'$v`\" = yes"; then
      AC_MSG_RESULT(yes)
      n=HAVE_`echo $v | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'`
      AC_DEFINE_UNQUOTED($n)
    else
      AC_MSG_RESULT(no)
    fi
  done
  AC_CHECK_FUNCS($checkfuncs)
  libiberty_NEED_DECLARATION(canonicalize_file_name)
fi

# Figure out which version of pexecute to use.
case "${host}" in
     *-*-mingw* | *-*-winnt*)	pexecute=pex-win32.o  ;;
     *-*-msdosdjgpp*)		pexecute=pex-djgpp.o  ;;
     *-*-msdos*)		pexecute=pex-msdos.o  ;;
     *-*-os2-emx*)		pexecute=pex-os2.o    ;;
     *)				pexecute=pex-unix.o   ;;
esac
AC_SUBST(pexecute)

libiberty_AC_FUNC_STRNCMP

# Install a library built with a cross compiler in $(tooldir) rather
# than $(libdir).
if test -z "${with_cross_host}"; then
  INSTALL_DEST=libdir
else
  INSTALL_DEST=tooldir
fi
AC_SUBST(INSTALL_DEST)

# We need multilib support, but only if configuring for the target.
AC_OUTPUT(Makefile testsuite/Makefile,
[test -z "$CONFIG_HEADERS" || echo timestamp > stamp-h
if test -n "$CONFIG_FILES"; then
  if test -n "${with_build_subdir}" || test -n "${with_target_subdir}"; then
    # FIXME: We shouldn't need to set ac_file
    ac_file=Makefile
    LD="${ORIGINAL_LD_FOR_MULTILIBS}"
    . ${libiberty_topdir}/config-ml.in
  fi
fi],
srcdir=${srcdir}
host=${host}
target=${target}
with_target_subdir=${with_target_subdir}
with_build_subdir=${with_build_subdir}
with_multisubdir=${with_multisubdir}
ac_configure_args="--enable-multilib ${ac_configure_args}"
CONFIG_SHELL=${CONFIG_SHELL-/bin/sh}
ORIGINAL_LD_FOR_MULTILIBS="${ORIGINAL_LD_FOR_MULTILIBS}"
libiberty_topdir=${libiberty_topdir}
)