dnl Hey Emacs, I want this in -*- C -*- mode, please.

dnl Copyright (C) 1994, 1995 Free Software Foundation, Inc.
dnl Please see COPYING for a description your rights and responsibilities
dnl with this software.
AC_REVISION($Revision: 1.12 $)dnl
AC_INIT(main.c)
AC_PREREQ(2.1)

ST_MAJOR_VERSION=1
ST_MINOR_VERSION=2
ST_EDIT_VERSION=5
ST_EDIT_PREFIX="alpha"

PROGS="gst"
AC_CONFIG_HEADER(config.h)

dnl Cannot use square brackets for testing in Autoconf scripts.
if test "${ST_EDT_VER}" = "0"
then
    VERSION=${ST_MAJOR_VERSION}.${ST_MINOR_VERSION}
else
    VERSION=${ST_MAJOR_VERSION}.${ST_MINOR_VERSION}.${ST_EDIT_PREFIX}${ST_EDIT_VERSION}
fi
echo "GNU Smalltalk Version $VERSION"
AC_SUBST(VERSION)dnl
AC_DEFINE_UNQUOTED(VERSION, $VERSION)
AC_DEFINE_UNQUOTED(ST_MAJOR_VERSION, $ST_MAJOR_VERSION)
AC_DEFINE_UNQUOTED(ST_MINOR_VERSION, $ST_MINOR_VERSION)
AC_DEFINE_UNQUOTED(ST_EDIT_VERSION, $ST_EDIT_VERSION)
AC_DEFINE_UNQUOTED(ST_EDIT_PREFIX, "$ST_EDIT_PREFIX")

AC_SUBST(VERSION)
AC_SUBST(ST_MAJOR_VERSION)
AC_SUBST(ST_MINOR_VERSION)
AC_SUBST(ST_EDIT_VERSION)
AC_SUBST(ST_EDIT_PREFIX)

test -z "$CDEBUG" && CDEBUG=-g AC_SUBST(CDEBUG)
test -z "$CFLAGS" && CFLAGS= AC_SUBST(CFLAGS)
test -z "$LDFLAGS" && LDFLAGS= AC_SUBST(LDFLAGS)

dnl Use of the local directory is temporary
: ${KERNEL_PATH:=`pwd`}
: ${IMAGE_PATH:=$KERNEL_PATH}

AC_SUBST(PROGS)dnl

AC_PROG_CC
AC_ISC_POSIX

AC_PROG_CPP
AC_PROG_GCC_TRADITIONAL
if test "$GCC" = yes
then
    GCC_EXTRA_FLAGS='-Wimplicit -Wreturn-type -Wunused'
fi

AC_PROG_INSTALL


AC_AIX
case $target in
    dos|winnt)
        AC_DEFINE(HAVE_ALLOCA, 1)
        ;;
    *)
        AC_FUNC_ALLOCA
        ;;

esac
AC_STRUCT_TIMEZONE
dnl this was here, but neither mntent or pwnam are used : AC_IRIX_SUN


AC_PROG_RANLIB
AC_PROG_MAKE_SET
AC_TYPE_SIGNAL
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h stdarg.h io.h stdlib.h)
AC_HEADER_DIRENT

dnl dnl Are we cross compiling?
dnl AC_C_CROSS
dnl dnl [ need to do something with the answer, like print that cross compiling
dnl dnl   is not currently supported ]

dnl if test "$cross_compiling" = "yes" 
dnl then
dnl     echo "can't cross compile"
dnl     exit 1
dnl fi

dnl - Macro: AC_SYS_LONG_FILE_NAMES
dnl     If the system supports file names longer than 14 characters, define
dnl     `HAVE_LONG_FILE_NAMES'.

AC_C_BIGENDIAN
AC_TYPE_OFF_T
AC_CHECK_FUNCS(bcopy bzero memcpy memset)
AC_CHECK_FUNCS(sighold ldexp)
dnl the extra brackets below quote this through m4
BISON=bison
dnl "AC_TRY_RUN( ACTION-IF-TRUE [, ACTION-IF-FALSE"
AC_TRY_RUN([
main()
{
    long vec[3];
    double test, *testptr;

    test = *(double *)&vec[1];  /* Should not be double aligned */
    testptr = (double *)&vec[1];
    *testptr = test;

    exit(0);
}
],
AC_DEFINE(DOUBLE_ALIGNMENT, sizeof(long)),
AC_DEFINE(DOUBLE_ALIGNMENT, sizeof(double)),
AC_DEFINE(DOUBLE_ALIGNMENT, sizeof(double)))

AC_CHECK_SIZEOF(long)
AC_CHECK_SIZEOF(char *)


MAKE_LIBRARY_COMMAND='cd lib; $(MAKE) all'
RM='rm -f'
COMPLETE_MAKE=
GST_TAB=gst.tab

OBJ_EXTENSION=o
LIB_EXTENSION=a

case $target in
    dos)
        AC_DEFINE(BINARY_MODE_NEEDED)
        AC_DEFINE(HAVE_SYS_TIME_H)
        AC_DEFINE(HAVE_GETTIMEOFDAY)
        AC_DEFINE(TIME_WITH_SYS_TIME)

        AC_CHECK_HEADERS(unistd.h)

        AC_CHECKING(builtin preprocessor symbol set)
        MACHINE_DEFINES='"__GNUC__", "__GNUC_MINOR__", "unix", "i386", "GO32", "MSDOS", "__unix__", "__i386__", "__GO32__", "__MSDOS__", "__unix", "__i386", "__GO32", "__MSDOS", '
        AC_DEFINE_UNQUOTED(MACHINE_DEFINES, $MACHINE_DEFINES)

        RM=del
        COMPLETE_MAKE='coff2exe gst'
        GST_TAB=gst_tab
        MAKE_LIBRARY_COMMAND=
        AC_CHECK_PROG(MAKEINFO, makeinfo, makeinfo, :)
        ;;

    os2)
        echo "os2 target not supported yet"
        AC_DEFINE(BINARY_MODE_NEEDED)
	AC_DEFINE(HAVE_IO_H)

        AC_CHECKING(builtin preprocessor symbol set)
        dnl net yet accurate
        MACHINE_DEFINES='"__OS2__", "i386", "__i386__", "__i386", '
        AC_DEFINE_UNQUOTED(MACHINE_DEFINES, $MACHINE_DEFINES)

        RM=del
	OBJ_EXTENSION=obj
	LIB_EXTENSION=lib
        GST_TAB=gst_tab
        MAKEINFO=:
        AC_SUBST(MAKEINFO)
        exit 1
        ;;

    winnt)
        AC_DEFINE(BINARY_MODE_NEEDED)
        AC_DEFINE(HAVE_IO_H)

        AC_CHECKING(builtin preprocessor symbol set)
        dnl net yet accurate
        MACHINE_DEFINES='"_WIN32", '
        AC_DEFINE_UNQUOTED(MACHINE_DEFINES, $MACHINE_DEFINES)

dnl this probably makes no difference since the makefile is ignored
        GST_TAB=gst_tab
        ;;

    *)
        dnl -- need to make a truncate.c for the truncate function
        AC_HEADER_TIME
        AC_CHECK_HEADERS(unistd.h sys/time.h)
        AC_EGREP_HEADER([ *ioctl[[^A-Za-z0-9_]]], unistd.h, AC_DEFINE(IOCTL_IN_UNISTD_H))

        AC_CHECK_PROG(MAKEINFO, makeinfo, makeinfo, :)
        AC_FUNC_MMAP
	AC_PATH_X
	AC_PATH_XTRA

        AC_CHECK_FUNCS(ioctl popen truncate alarm gettimeofday getwd sigsetmask getdtablesize)

    AC_TRY_RUN([
#ifdef HAVE_FCNTL_H
#include <fcntl.h> /* for open, read, etc */
#endif

void
put(fd, c)
int fd;
char c;
{
    write(fd, &c, 1);
}

int
get(fd)
{
    char c;
    read(fd, &c, 1);
    return c;
}

main()
{
    int fd;
    fd = open("mstTeSt", O_WRONLY | O_CREAT, 0666); /* write mode + create */
    if (fd < 0) {
      printf("Error: could not open binary test file for writing\n");
      exit(1);
    }

    put(fd, '\000');
    put(fd, '\012');
    put(fd, '\012');
    put(fd, '\015');
    put(fd, '\012');
    close(fd);

    fd = open("mstTeSt", O_RDONLY);	/* read mode */
    if (fd < 0) {
      printf("Error: Could not open binary test file\n");
      exit(1);
    }
    if (get(fd) != '\000') exit(1);
    if (get(fd) != '\012') exit(1);
    if (get(fd) != '\012') exit(1);
    if (get(fd) != '\015') exit(1);
    if (get(fd) != '\012') exit(1);
    exit(0);
}
],
,
    AC_DEFINE(BINARY_MODE_NEEDED),
    AC_MSG_RESULT(Dont know if target system needs binary mode for open))
    rm -f mstTeSt

    AC_CHECKING(builtin preprocessor symbol set)
    $CC -o getcppsyms getcppsyms.c
    MACHINE_DEFINES=[`getcppsyms | sed -e 's/ -D\([^ ]*\)/"\1", /g'`]
    AC_DEFINE_UNQUOTED(MACHINE_DEFINES, $MACHINE_DEFINES)
    rm -f getcppsyms

    AC_CHECK_PROG(has_autoconf, autoconf, 1)
    if test "x$has_autoconf" != "x"
    then
        REBUILD_CONFIG_RULES=' \
${srcdir}/configure: configure.in # not yet: aclocal.m4 \
	cd ${srcdir} \&\& autoconf \
\
# autoheader might not change config.h.in, so touch a stamp file. \
${srcdir}/config.h.in: stamp-h.in \
${srcdir}/stamp-h.in: configure.in acconfig.h \
	cd ${srcdir} \&\& autoheader \
	echo timestamp > ${srcdir}/stamp-h.in \
\
config.h: stamp-h \
stamp-h: config.h.in config.status \
	./config.status \
\
# Not clear that having this depend on subdirectory Makefiles is the right \
# thing. \
Makefile: Makefile.in $(CONFIG_INPUT) config.status \
	./config.status \
\
config.status: ${srcdir}/configure \
	./config.status --recheck \
'
    fi

    ;;
esac
AC_MSG_RESULT(setting MACHINE_DEFINES to $MACHINE_DEFINES) 

AC_SUBST(MAKE_LIBRARY_COMMAND)
AC_SUBST(RM)
AC_SUBST(COMPLETE_MAKE)
AC_SUBST(GST_TAB)
AC_SUBST(OBJ_EXTENSION)
AC_SUBST(LIB_EXTENSION)
AC_SUBST(X_PRE_LIBS)
AC_SUBST(X_CFLAGS)
AC_SUBST(X_LIBS)
AC_SUBST(X_EXTRA_LIBS)

dnl this shows that even the way I have things set up, DEFS is what the
dnl cc command line arguments would be.
dnl echo "defs are $DEFS"

dnl add the sunos 4 case for optimization -- if CC = cc & cc is not gcc
dnl and some sunos 4 specific test and optimization turned on.
case "$DEFS" in
    *\"NeXT\",*)
	SPECIAL_INTERP='# Hack around NeXTs CPP bugs/limitations \
mstinterp.o: mstinterp.c \
	$(GENERIC_CC) -traditional-cpp mstinterp.c \
'
	;;
esac


AC_CHECK_LIB(m, atan, [LIBS="$LIBS -lm"])
AC_CHECK_LIB(socket, listen, [LIBS="$LIBS -lsocket"])
AC_CHECK_LIB(nsl, gethostbyname, [LIBS="$LIBS -lnsl"])


dnl Many C runtime libraries do not have this useful function.
AC_REPLACE_FUNCS(strdup)

dnl NeXT does not have this function
AC_REPLACE_FUNCS(putenv)

dnl The use of LIBPATH instead of LDFLAGS is deliberate: the definition we
dnl want to use is only valid inside the context of the Makefile, so we cannot
dnl use it here -- in particular, the tests for other libraries lose because
dnl the compilation fails with a non-valid LDFLAGS value.

AC_ARG_WITH(dld, [  --with-dld		Include the GNU Dynamic Loading package], 
[#echo "With: $withval"
case $withval in
    yes)
	LDPATH="$LDPATH -L\$(libdir)"
        ;;
    *)
	LDPATH="$LDPATH -L$withval"
	;;
esac	      
dnl Note that prefix is undefined here if not set via cmd line
dnl prefix is $prefix
LIBS="-ldld $LIBS" 
AC_DEFINE(USE_DLD)])

AC_ARG_WITH(readline, 
[  --with-readline 	Include the readline package
			(arg is path to lib)], 
[case $withval in
    yes)
	LDPATH="$LDPATH -L\$(libdir)"
        ;;
    *)
	LDPATH="$LDPATH -L$withval"
	;;
esac	      
dnl Note that prefix is undefined here if not set via cmd line
dnl prefix is $prefix
LIBS="-lreadline $LIBS" 
AC_DEFINE(USE_READLINE)])


AC_ARG_WITH(optimize, 
[  --with-optimize	Make compiles all be with optimization turned on 
			(not functional yet)], 
[case $withval in
    yes)
	DO_OPTIMIZE=yes
	CFLAGS="$CFLAGS "'$(OPT_FLAG)'   # need to find out how to be more selective -O3
        CDEBUG=
        CPPFLAGS="$CPPFLAGS -DOPTIMIZE"
        ;;
    *)
	;;
esac	      
])



dnl This enables the install make tag to install the st files into a standard 
dnl location derived from prefix.  The default is to have the files run in 
dnl place (mst is configured to use the main .st files from there normal place
dnl in the build directory hierarchy).
dnl
dnl Note that IMAGE_PATH is probably set to the wrong value here
AC_ARG_ENABLE(install-st, [], [INSTALL_ST_FILES='for file in $(SMALLTALK_KERNEL); do \\\
	  $(INSTALL_PROGRAM) $$file $(stdir)/$$file ; \\\
	done'
KERNEL_PATH=${prefix}/smalltalk
IMAGE_PATH=${KERNEL_PATH}
])

AC_ARG_ENABLE(gc-torture,
	      [  --enable-gc-torture	Makes every allocation do a GC],
	      AC_DEFINE(GC_TORTURE))


dnl enable some performance monitoring stuff
AC_ARG_ENABLE(profblock, [  --enable-profblock	Enables performance monitoring (not currently working)], AC_DEFINE(PROFBLOCK))

dnl I like putting the SUBSTs all in one place.

AC_SUBST(GCC_EXTRA_FLAGS)
AC_SUBST(BISON)dnl
AC_SUBST(IMAGE_PATH)
AC_SUBST(INSTALL_ST_FILES)
AC_SUBST(KERNEL_PATH)
AC_SUBST(LDPATH)
AC_SUBST(REBUILD_CONFIG_RULES)
AC_SUBST(SPECIAL_INTERP)
AC_SUBST(LIBOBJS)
AC_SUBST(USE_READLINE)dnl

AC_OUTPUT(Makefile Makefile.defs lib/Makefile
info/gst.txi lib/gstpaths.h,  [
echo timestamp > stamp-h
createMake blox
createMake contrib
createMake cxtnsn
createMake examples
createMake info
createMake stix
createMake tcp
createMake test
])
