define(pkg_major, 0)
define(pkg_minor, 99)
define(pkg_extra, -beta)
define(pkg_maintainer, libunwind-devel@nongnu.org)
define(mkvers, $1.$2$3)
dnl Process this file with autoconf to produce a configure script.
AC_INIT(libunwind, mkvers(pkg_major, pkg_minor, pkg_extra), pkg_maintainer)
AC_CONFIG_SRCDIR(src/mi/backtrace.c)
AC_CONFIG_AUX_DIR(config)
AC_CANONICAL_SYSTEM
AM_INIT_AUTOMAKE([1.6 subdir-objects])
AM_MAINTAINER_MODE
AM_CONFIG_HEADER(include/config.h)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CXX
AC_PROG_INSTALL
AC_PROG_MAKE_SET
AM_PROG_LIBTOOL
AM_PROG_AS
AM_PROG_CC_C_O

dnl Checks for libraries.
AC_CHECK_LIB(uca, __uc_get_grs)
OLD_LIBS=${LIBS}
AC_SEARCH_LIBS(dlopen, dl)
LIBS=${OLD_LIBS}
case "$ac_cv_search_dlopen" in
  -l*) DLLIB=$ac_cv_search_dlopen;;
  *) DLLIB="";;
esac

CHECK_ATOMIC_OPS

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(asm/ptrace_offsets.h endian.h sys/endian.h execinfo.h \
		ia64intrin.h sys/uc_access.h unistd.h signal.h sys/types.h \
		sys/procfs.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_TYPE_SIGNAL
AC_TYPE_SIZE_T

CPPFLAGS="${CPPFLAGS} -D_GNU_SOURCE"
if test x$enable_debug = xyes; then
  CPPFLAGS="${CPPFLAGS} -DDEBUG"
else
  CPPFLAGS="${CPPFLAGS} -DNDEBUG"
fi

AC_CHECK_MEMBERS([struct dl_phdr_info.dlpi_subs],,,[#include <link.h>])
AC_CHECK_TYPES([sighandler_t], [], [],
[$ac_includes_default
#if HAVE_SIGNAL_H
# include <signal.h>
#endif
])

AC_CHECK_DECLS([PTRACE_POKEUSER, PTRACE_POKEDATA, PTRACE_CONT,
PTRACE_TRACEME, PTRACE_CONT, PTRACE_SIGNLESTEP,
PTRACE_SYSCALL, PT_IO, PT_GETREGS,
PT_GETFPREGS, PT_CONTINUE, PT_TRACE_ME,
PT_STEP, PT_SYSCALL], [], [],
[$ac_includes_default
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <sys/ptrace.h>
])

dnl Checks for library functions.
AC_FUNC_MEMCMP
AC_TYPE_SIGNAL
AC_CHECK_FUNCS(dl_iterate_phdr dl_phdr_removals_counter dlmodinfo getunwind \
		ttrace)
is_gcc_m64() {
 if test `echo $CFLAGS | grep "\-m64" -c` -eq 1 ; then echo ppc64;
 else
  if test `echo $CC | grep "\-m64" -c` -eq 1 ; then echo ppc64; else echo ppc32; fi;
 fi;
}

is_gcc_altivec() {
 if test `echo $CFLAGS | grep "\-maltivec" -c` -eq 1 ; then echo has_altivec;
 else
  if test `echo $CC | grep "\-maltivec" -c` -eq 1 ; then echo has_altivec; else echo no_altivec; fi;
 fi;
}

use_altivec=`is_gcc_altivec`
AM_CONDITIONAL(USE_ALTIVEC, test x$use_altivec = xhas_altivec)

get_arch() {
 case "$1" in
  arm*) echo arm;;
  i?86) echo x86;;
  hppa*) echo hppa;;
  mips*) echo mips;;
  powerpc*) is_gcc_m64;;
  amd64) echo x86_64;;
  *) echo $1;;
 esac
}

build_arch=`get_arch $build_cpu`
host_arch=`get_arch $host_cpu`
target_arch=`get_arch $target_cpu`

AM_CONDITIONAL(REMOTE_ONLY, test x$target_arch != x$host_arch)
AM_CONDITIONAL(ARCH_ARM, test x$target_arch = xarm)
AM_CONDITIONAL(ARCH_IA64, test x$target_arch = xia64)
AM_CONDITIONAL(ARCH_HPPA, test x$target_arch = xhppa)
AM_CONDITIONAL(ARCH_MIPS, test x$target_arch = xmips)
AM_CONDITIONAL(ARCH_X86, test x$target_arch = xx86)
AM_CONDITIONAL(ARCH_X86_64, test x$target_arch = xx86_64)
AM_CONDITIONAL(ARCH_PPC32, test x$target_arch = xppc32)
AM_CONDITIONAL(ARCH_PPC64, test x$target_arch = xppc64)
AM_CONDITIONAL(OS_LINUX, expr x$target_os : xlinux >/dev/null)
AM_CONDITIONAL(OS_HPUX, expr x$target_os : xhpux >/dev/null)
AM_CONDITIONAL(OS_FREEBSD, expr x$target_os : xfreebsd >/dev/null)

if test x$target_arch = xppc64; then
        libdir='${exec_prefix}/lib64'
        AC_MSG_NOTICE([PowerPC64 detected, lib will be installed ${libdir}]);
        AC_SUBST([libdir])
fi

if test x$target_arch != x$host_arch; then
  CPPFLAGS="${CPPFLAGS} -DUNW_REMOTE_ONLY"
fi

AC_ARG_ENABLE(debug,
[  --enable-debug          turn on debug support (slows down execution)],
[enable_debug=yes], [])

AC_ARG_ENABLE(cxx_exceptions,
[  --enable-cxx-exceptions use libunwind to handle C++ exceptions],
[enable_cxx_exceptions=$enableval], 
[
# C++ exception handling doesn't work too well on x86
case $target_arch in
  x86*) enable_cxx_exceptions=no;;
  arm*) enable_cxx_exceptions=no;;
  mips*) enable_cxx_exceptions=no;;
  *) enable_cxx_exceptions=yes;;
esac
])

if test x$enable_cxx_exceptions = xyes; then
  AC_MSG_NOTICE([Enabling C++ exception support])
fi
AM_CONDITIONAL([SUPPORT_CXX_EXCEPTIONS], [test x$enable_cxx_exceptions = xyes])

AC_ARG_ENABLE(debug_frame,
[  --enable-debug-frame Load the ".debug_frame" section if available],
[enable_debug_frame=$enableval], [enable_debug_frame=no])
if test x$enable_debug_frame = xyes; then
  AC_DEFINE([CONFIG_DEBUG_FRAME], [], [Enable Debug Frame])
fi

AC_ARG_ENABLE(block_signals,
[  --enable-block-signals Block signals before performing mutex operations],
[enable_block_signals=$enableval], [enable_block_signals=yes])
if test x$enable_block_signals = xyes; then
  AC_DEFINE([CONFIG_BLOCK_SIGNALS], [], [Block signals before mutex operations])
fi


LIBUNWIND___THREAD

save_LDFLAGS="$LDFLAGS"
LDFLAGS="$LDFLAGS -static-libcxa"
AC_TRY_LINK([], [], [have_static_libcxa=yes])
LDFLAGS="$save_LDFLAGS"
if test "x$have_static_libcxa" = xyes; then
  LDFLAGS_STATIC_LIBCXA="-XCClinker -static-libcxa"
fi

AC_TRY_COMPILE([], [#ifndef __INTEL_COMPILER
#error choke me
#endif], [intel_compiler=yes])

if test x$GCC = xyes -a x$intel_compiler != xyes; then
  CFLAGS="${CFLAGS} -fexceptions -Wall -Wsign-compare"
  LIBCRTS="-lgcc"
fi

CCASFLAGS="${CCASFLAGS} ${CPPFLAGS}"

arch="$target_arch"
ARCH=`echo $target_arch | tr [a-z] [A-Z]`

dnl create shell variables from the M4 macros:
PKG_MAJOR=pkg_major
PKG_MINOR=pkg_minor
PKG_EXTRA=pkg_extra
PKG_MAINTAINER=pkg_maintainer

AC_SUBST(build_arch)
AC_SUBST(target_os)
AC_SUBST(arch)
AC_SUBST(ARCH)
AC_SUBST(LDFLAGS_STATIC_LIBCXA)
AC_SUBST(LIBCRTS)
AC_SUBST(PKG_MAJOR)
AC_SUBST(PKG_MINOR)
AC_SUBST(PKG_EXTRA)
AC_SUBST(PKG_MAINTAINER)
AC_SUBST(enable_cxx_exceptions)
AC_SUBST(DLLIB)

AC_CONFIG_FILES(Makefile src/Makefile tests/Makefile tests/check-namespace.sh
		doc/Makefile doc/common.tex include/libunwind-common.h)
AC_OUTPUT