dnl------------------------------------------------------------------
dnl File: configure.in
dnl Top configuration file for autoconf/automake of agfl
dnl
dnl Process this file with autoconf to produce a configure script.
dnl
dnl CVS ID: "$Id: configure.in,v 1.67 2006/10/11 13:41:03 marcs Exp $"
dnl------------------------------------------------------------------
AC_INIT(gen/src/gen.c)
AM_INIT_AUTOMAKE(agfl, 2.5)
AM_CONFIG_HEADER(config.h)
AM_SANITY_CHECK

dnl------------------------------------------------------------------
dnl Check the local build system and report
dnl------------------------------------------------------------------
AC_CANONICAL_SYSTEM
echo 'Building AGFL and its runtime system for a' $host 'system'
dnl AC_SUBST(TCPU,$host_cpu)
dnl AC_SUBST(TOS,$host_os)

dnl------------------------------------------------------------------
dnl Check for libtool
dnl------------------------------------------------------------------
AM_PROG_LIBTOOL
AC_SUBST(LIBTOOL_DEPS)

dnl------------------------------------------------------------------
dnl Checks for programs.
dnl------------------------------------------------------------------
AC_PROG_CC
AC_PROG_CXX
AC_PROG_INSTALL
AM_PROG_LEX
AC_PROG_YACC

dnl------------------------------------------------------------------
dnl Determine machine dependency
dnl The machine dependency is used in gen/src to select the proper
dnl abstract virtual machine code on the host architecture
dnl
dnl We must improve this one by checking the $host variable instead
dnl of checking with uname
dnl
dnl When we add Acode output to the generator and runtime system,
dnl all machine dependencies may disappear
dnl------------------------------------------------------------------
echo 'Checking which machdep to use...'
MACHDEPHOSTVAL=""
MACHDEPARCH=""
MACHDEPOS=""

# Architectures
case "`uname -m`" in
    sun4u|sun4m|sparc)
	MACHDEPARCH=sparc ;;
    alpha)
	MACHDEPARCH=alpha ;;
    i386|i486|i586|i686|i?86)
	MACHDEPARCH=i386 ;;
    amd64|x86_64)
	MACHDEPARCH=amd64 ;;
    "Power Macintosh")
	MACHDEPARCH=ppc ;;
    aix)
	MACHDEPHOSTVAL=machdep.aix ;;
esac

# Operating systems
case "`uname -s`" in
    SunOS)
	MACHDEPOS=solaris ;;
    NetBSD)
	MACHDEPOS=netbsd ;;
    FreeBSD)
	MACHDEPOS=freebsd ;;
    OpenBSD)
	MACHDEPOS=openbsd ;;
    Linux)
	MACHDEPOS=linux ;;
    Darwin)
	MACHDEPOS=darwin ;;
    IRIX64|IRIX)
	MACHDEPHOSTVAL=machdep.irix ;;
    dos)
	MACHDEPHOSTVAL=machdep.dos ;;
esac

if test "X$MACHDEPHOSTVAL" = "X" ; then
    MACHDEPHOSTVAL="machdep.$MACHDEPARCH-$MACHDEPOS"
fi
if test "X$MACHDEPHOSTVAL" = "X" -o ! -e "${srcdir}/gen/src/$MACHDEPHOSTVAL"; then
  echo 'Unsupported architecture'
else
  echo '-- using '$MACHDEPHOSTVAL
fi
AC_SUBST(MACHDEPHOSTVAL)

dnl------------------------------------------------------------------
dnl Determine the location of the CDL3 compiler
dnl
dnl First check if the user gave the location with --with-cdl3=
dnl If not try and locate cdlc automagically
dnl------------------------------------------------------------------
AC_ARG_WITH(cdl3,
  AC_HELP_STRING([--with-cdl3=DIR],[Use installed copy of cdl3]),
    [ if test x$with_cdl3 = xyes ; then
         AC_MSG_ERROR([  *** Directory must be specified for --with-cdl3])
      else
         CDLPATH=$with_cdl3
      fi
    ],
    [ AC_PATH_PROG(my_cdlbinpath, cdlc, [/usr/local/kun/bin/cdlc],
		   [$PATH:$prefix:$ac_default_prefix])
      CDLPATH=`echo $my_cdlbinpath | sed 's=\(.*\)/bin/cdlc=\1='`
    ])

dnl Verify that we have located the cdl3 compiler
AC_CHECK_FILE([$CDLPATH/bin/cdlc],
  [ echo '-- CDL3 in '$CDLPATH ],
  [ AC_MSG_ERROR([  *** Cannot find cdl3 compiler. Forgot --with-cdl3=DIR?])])

CDLBINPATH="$CDLPATH"/bin
CDLLIBFLAG=-L"$CDLPATH"/lib
CDLRTS="-lcdlrts"

dnl
dnl CDL include path (for a straight out of the box installation) is
dnl not in $CDLPATH/include but in $CDLPATH/share/cdl3/include
dnl Check for both.
dnl
dnl Checking for the include path based on the location of cdlc is
dnl not a good thing to do. Need a second look at this.
dnl
AC_CHECK_FILE([$CDLPATH/include/predef.k3],
  [ CDLINCPATH=$CDLPATH/include ],
  [ AC_CHECK_FILE([$CDLPATH/share/cdl3/include/predef.k3],
      [ CDLINCPATH=$CDLPATH/share/cdl3/include ],
      [ AC_MSG_ERROR([  *** Cannot find cdl3 include path. Not a standard installation CDL3? ])])
  ])

AC_SUBST(CDLPATH)
AC_SUBST(CDLBINPATH)
AC_SUBST(CDLLIBFLAG)
AC_SUBST(CDLINCPATH)
AC_SUBST(CDLRTS)

dnl------------------------------------------------------------------
dnl Check whether we must generate the debug version
dnl------------------------------------------------------------------
AC_ARG_ENABLE(debug,
   AC_HELP_STRING([--enable-debug],[enable debug code]),
   [ case "${enableval}" in
        yes) debug=true ;;
        no)  debug=false ;;
        *)   AC_MSG_ERROR(bad value ${enableval} for --enable-debug) ;;
     esac ], [debug=false] )
if test x$debug = xtrue; then
   echo 'Compiling with debugging enabled, optimization off'
   dbg_opt='-DDEBUG -g -ggdb -O0'
else
   dbg_opt='-O2'
fi
cflags='-g'
AC_SUBST(DBG_OPT,$dbg_opt)
AC_SUBST(CFLAGS,$cflags)

dnl------------------------------------------------------------------
dnl Check whether we must generate a version with memory logging
dnl------------------------------------------------------------------
AC_ARG_ENABLE(memlog,
   AC_HELP_STRING([--enable-memlog],[enable memory logging code]),
   [ case "${enableval}" in
        yes) memlog=true ;;
        no)  memlog=false ;;
        *)   AC_MSG_ERROR(bad value ${enableval} for --enable-memlog) ;;
     esac ], [memlog=false] )
if test x$memlog = xtrue; then
   echo 'Compiling with memory logging enabled'
   memlog_opt='-DMEMORY_LOG'
else
   memlog_opt=''
fi
AC_SUBST(MEMLOG_OPT,$memlog_opt)

dnl------------------------------------------------------------------
dnl Check whether we must generate a version with regexp debugging
dnl------------------------------------------------------------------
AC_ARG_ENABLE(debug_regexp,
   AC_HELP_STRING([--enable-debug-regexp],[enable regexp debugging code]),
   [ case "${enableval}" in
        yes) debug_regexp=true ;;
        no)  debug_regexp=false ;;
        *)   AC_MSG_ERROR(bad value ${enableval} for --enable-debug-regexp) ;;
     esac ], [debug_regexp=false] )
if test x$debug_regexp = xtrue; then
   echo 'Compiling with regexp debugging enabled'
   debug_regexp_opt='-DDEBUG_REGEX'
else
   debug_regexp_opt=''
fi
AC_SUBST(DEBUG_REGEXP_OPT,$debug_regexp_opt)

dnl------------------------------------------------------------------
dnl Check whether we must generate a version with lexicon debugging
dnl------------------------------------------------------------------
AC_ARG_ENABLE(debug_lex,
   AC_HELP_STRING([--enable-debug-lex],[enable lexicon debugging code]),
   [ case "${enableval}" in
        yes) debug_lex=true ;;
        no)  debug_lex=false ;;
        *)   AC_MSG_ERROR(bad value ${enableval} for --enable-debug-lex) ;;
     esac ], [debug_lex=false] )
if test x$debug_lex = xtrue; then
   echo 'Compiling with lexicon debugging enabled'
   debug_lex_opt='-DDEBUG_LEX'
else
   debug_lex_opt=''
fi
AC_SUBST(DEBUG_LEX_OPT,$debug_lex_opt)

dnl------------------------------------------------------------------
dnl Check whether we must generate a version with runtime debugging
dnl------------------------------------------------------------------
AC_ARG_ENABLE(debug_rts,
   AC_HELP_STRING([--enable-debug-rts],[enable runtime system debugging code]),
   [ case "${enableval}" in
        yes) debug_rts=true ;;
        no)  debug_rts=false ;;
        *)   AC_MSG_ERROR(bad value ${enableval} for --enable-debug-rts) ;;
     esac ], [debug_rts=false] )
if test x$debug_rts = xtrue; then
   echo 'Compiling with runtime system debugging enabled'
   debug_rts_opt='-DDEBUG_RTS'
else
   debug_rts_opt=''
fi
AC_SUBST(DEBUG_RTS_OPT,$debug_rts_opt)

dnl------------------------------------------------------------------
dnl Check whether we want libreadline support
dnl We do not store -lreadline in LIBS since we do not want to link
dnl the generator and lexgen to link against libreadline
dnl------------------------------------------------------------------
AC_ARG_WITH(readline,
   AC_HELP_STRING([--with-readline],[link runtime system with libreadline]),
   [ with_readline=yes ],[ with_readline=no ])
if test "$with_readline" = yes; then
   echo 'Configuring AGFL to use the readline package'
   echo 'Checking for readline availability...'
   save_LIBS=$LIBS
   AC_CHECK_LIB(readline,add_history)
   LIBS=$save_LIBS
fi

dnl------------------------------------------------------------------
dnl if our host os is a FreeBSD variant we include libkvm
dnl to be able to determine the memory usage
dnl------------------------------------------------------------------
case "$host_os" in
    *bsd*)
        AC_CHECK_LIB(kvm, kvm_openfiles)
        ;;
esac

dnl------------------------------------------------------------------
dnl Checks for header files.
dnl------------------------------------------------------------------
AC_HEADER_STDC
AC_CHECK_HEADERS(getopt.h fcntl.h limits.h sys/time.h unistd.h)

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

dnl------------------------------------------------------------------
dnl Checks for library functions.
dnl------------------------------------------------------------------
AC_TYPE_SIGNAL
AC_FUNC_VPRINTF
AC_CHECK_FUNCS(getopt getopt_long gettimeofday regcomp strcspn strtol)

dnl------------------------------------------------------------------
dnl Check if make macro gets set
dnl------------------------------------------------------------------
AC_PROG_MAKE_SET

dnl------------------------------------------------------------------
dnl Check if we need --rpath or some such to link programs,
dnl using libtool. gen.c needs it.
dnl------------------------------------------------------------------

RPATH_OPT=""
AC_MSG_CHECKING([what libtool found for hardcoding paths of runtime libraries])
find_rpath_option() {
    (
	./libtool --config >configure.tmp$$
	. configure.tmp$$
	rm configure.tmp$$
	libdir="%s"	# use sprintf to fill in $libdir
	eval echo $hardcode_libdir_flag_spec
    )
}
rpath_opt=`find_rpath_option`
if test -n "$rpath_opt"
then
    AC_DEFINE_UNQUOTED(RPATH_OPT, ["$rpath_opt"], [Set to a rpath option, if needed])
    AC_MSG_RESULT([$rpath_opt])
else
    AC_MSG_RESULT([nothing])
fi

dnl------------------------------------------------------------------
dnl Check if we need the underscore option on or off.
dnl------------------------------------------------------------------

AC_MSG_CHECKING([if we need a leading underscore])
dnl ${NM} is set above somewhere...
echo 'main() { return 0; } fnord() {}' >conftest.c
if ${CC} ${CFLAGS} ${CPPFLAGS} ${LDFLAGS} -o a.out conftest.c ${LIBS} &&
	${NM} a.out | grep _fnord >/dev/null; then
    AC_MSG_RESULT([yes])
    AC_DEFINE(LEADING_UNDERSCORE, 1, [Define to 1 if symbols start with _])
else
    AC_MSG_RESULT([no])
fi
rm -f conftest.c a.out

dnl------------------------------------------------------------------
dnl Finally specify which Makefiles should be generated
dnl------------------------------------------------------------------
AC_OUTPUT([
Makefile
libabase/Makefile
libabase/src/Makefile
liblexicon/Makefile
liblexicon/src/Makefile
liblexicon/tools/Makefile
gen/Makefile
gen/src/Makefile
gen/as/Makefile
gen/rts/Makefile
gen/rts/nmrts/Makefile
gen/rts/pmrts/Makefile
lexgen/Makefile
lexgen/src/Makefile
doc/Makefile
doc/userdoc/Makefile
gen/test/Makefile
man/Makefile
man/man1/Makefile
grammars/Makefile
grammars/latin/Makefile
grammars/npx/Makefile
utils/Makefile
utils/generator/Makefile
])
