AC_INIT(build_pgtk.pike)
AC_CONFIG_HEADER(config.h)

AC_SUBST(BUILDDIR)
AC_SUBST(CC)
AC_SUBST(CFLAGS)
AC_SUBST(LIBS)
AC_SUBST(LD)
AC_SUBST(AR)
AC_SUBST(SO)
AC_SUBST(LDSHARED)
AC_SUBST(CCSHARED)
AC_SUBST(LINKFORSHARED)
AC_SUBST(PIKE)
AC_SUBST(PIKEINCLUDE)
AC_SUBST(DEST)
AC_SUBST(WARN)
AC_SUBST(OPTIMIZE)
AC_SUBST(srcdir)

case "$srcdir" in
    /*)
    ;;
    *)
	oldsrcdir="$srcdir"
	srcdir="`cd \"$srcdir\";pwd`"
	AC_MSG_WARN([ Converted $oldsrcdir to $srcdir,
If this does not work, please use an absolute path to the configure script. ])
    ;;
esac

AC_SET_MAKE
AC_PROG_CC
BUILDDIR=`pwd`
LD='$(CC)'

# for ld shared...
AC_PATH_PROG(uname_prog,uname,no)
AC_MSG_CHECKING(operating system)
AC_CACHE_VAL(pike_cv_sys_os,
[
if test "$uname_prog" != "no"; then
  pike_cv_sys_os="`uname`"

  case "$pike_cv_sys_os" in
    SunOS)
      case "`uname -r`" in
        5.*) pike_cv_sys_os="Solaris";
      esac
    ;;
  esac
else
  echo Cannot determine OS
  exit
fi
])
AC_MSG_RESULT($pike_cv_sys_os)

case "$pike_cv_sys_os" in
  SCO*)
     case "$CFLAGS" in
       *-belf*) ;;
       *)
	  echo "Adding -belf option."
          CFLAGS="$CFLAGS -belf"
       ;;
     esac
     case "$LDFLAGS" in
       *-belf*) ;;
       *)
	  echo "Adding -belf option."
          LDFLAGS="$LDFLAGS -belf"
       ;;
     esac
  ;;
  SunOS*)
    # gcc on SunOS 4 forgets to define this:
    echo "SunOS. Adding -D__USE_FIXED_PROTOTYPES__"
    CFLAGS="$CFLAGS -D__USE_FIXED_PROTOTYPES__"
  ;;
  BSD/OS*)
    LD="smartlink shlicc"
  ;;
esac

AC_DEFUN(AC_SYS_COMPILER_FLAG,
[
  AC_MSG_CHECKING($1)
  OLD_CFLAGS="[$]CFLAGS"
  AC_CACHE_VAL(pike_cv_option_$2,
  [
    CFLAGS="[$]OLD_CFLAGS $1"
    AC_TRY_RUN([int main(){exit(0);}],pike_cv_option_$2=yes,pike_cv_option_$2=no,pike_cv_option_$2=no)
  ])
  
  CFLAGS="[$]OLD_CFLAGS"
  
  if test x"[$]pike_cv_option_$2" = "xyes" ; then
    $3="[$]$3 $1"
    AC_MSG_RESULT(yes)
    $5
  else
    AC_MSG_RESULT(no)
    $4
  fi
])

# arch, option, cache_name, variable
AC_DEFUN(AC_SYS_CPU_COMPILER_FLAG,
[
 if test "`uname -m 2>/dev/null`" = "$1" ; then
    AC_SYS_COMPILER_FLAG($2,$3,$4)
 fi
])

# os, option, cache_name, variable
AC_DEFUN(AC_SYS_OS_COMPILER_FLAG,
[
 if test "x$pike_cv_sys_os" = "x$1" ; then
    AC_SYS_COMPILER_FLAG($2,$3,$4)
 fi
])

WARN=""
OPTIMIZE=""

AC_SYS_COMPILER_FLAG(-O,O,OPTIMIZE)

if test "x$GCC" = xyes ; then
  WARN="-Wimplicit-function-declaration -W -Wpointer-arith -Wno-unused"

  if test "x$with_cdebug" = "xno" ; then
    AC_SYS_COMPILER_FLAG(-fomit-frame-pointer,fomit_frame_pointer,OPTIMIZE)
    CFLAGS=`echo " $CFLAGS " | sed -e 's@ -g @ @g'`
  else
    :
  fi  

### Optimize for different SUN machines. If there is no working 'uname'
### no extra optimization will be done, but nothing should break either.
  case "`uname -m 2>/dev/null`" in
   sun4c)
    AC_SYS_COMPILER_FLAG(-msparclite,sparclite,OPTIMIZE) 
   ;;
   sun4m)
    AC_SYS_COMPILER_FLAG(-mv8,microsparc,OPTIMIZE)
   ;;
   sun4d)
    AC_SYS_COMPILER_FLAG(-msupersparc,supersparc,OPTIMIZE)
   ;;
   sun4u)
    AC_SYS_COMPILER_FLAG(-msupersparc,ultrasparc,OPTIMIZE)
   ;;
   i586)
    AC_SYS_COMPILER_FLAG(-mpentium,pentium,OPTIMIZE)
   ;;
   i686)
    AC_SYS_COMPILER_FLAG(-mpentiumpro,pentiumpro,OPTIMIZE,[
       AC_SYS_COMPILER_FLAG(-mpentium,pentium,OPTIMIZE)
    ])
   ;;
  esac
 else

### Non-gcc thingies. This should probably be enhanced...

  # Solaris
  AC_SYS_OS_COMPILER_FLAG(Solaris,-fast,fast,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(Solaris,-xdepend,xdepend,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(Solaris,-xs,xs,CFLAGS)
  # Causes __STDC__ to be 1 which causes several headerfiles to behave
  # differently.
  # AC_SYS_OS_COMPILER_FLAG(Solaris,-Xc,Xc,WARN)

  # HP-UX
  AC_SYS_OS_COMPILER_FLAG(HP-UX,+Odataprefetch,plus_Odataprefetch,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(HP-UX,+Ofailsafe,plus_Ofailsafe,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(HP-UX,+O3,plus_O3,OPTIMIZE)
  # Ignore warnings 530 and 414 if you turn this on.
  # AC_SYS_OS_COMPILER_FLAG(HP-UX,+w1,plus_w1,WARN)

  # OSF/1
  AC_SYS_OS_COMPILER_FLAG(OSF1,-fast,fast,OPTIMIZE)
  # The need for -ieee is not detected properly by the FPE tests below.
  # AC_SYS_OS_COMPILER_FLAG(OSF1,-ieee,ieee,CFLAGS)
  AC_SYS_OS_COMPILER_FLAG(OSF1,-readonly_strings,readonly_strings,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(OSF1,-assume trusted_short_alignment,
    assume_trusted_short_alignment,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(OSF1,-assume aligned_objects,
    assume_aligned_objects,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(OSF1,-no_misalign,no_misalign,OPTIMIZE)

  AC_SYS_OS_COMPILER_FLAG(OSF1,-host,host,OPTIMIZE)
  AC_SYS_OS_COMPILER_FLAG(OSF1,-std1,std1,WARN)
  AC_SYS_OS_COMPILER_FLAG(OSF1,-warnprotos,warnprotos,WARN)

  # IRIX
  # sopt disabled since it takes too long time on interpret.c (>45min on an OČ)
  # AC_SYS_OS_COMPILER_FLAG(IRIX,-sopt,sopt,OPTIMIZE)

  # -n32 moved to smartlink so it will affekt cc as well as linker
  # this way the user can override it by setting LDFLAGS and/or CFLAGS
  # AC_SYS_OS_COMPILER_FLAG(IRIX,-n32,n32,CFLAGS)
  AC_SYS_OS_COMPILER_FLAG(IRIX,-fullwarn,fullwarn,WARN)

dnl ### Ok. This is for apcc...
case "$CC" in
  *apcc)
    AC_SYS_COMPILER_FLAG(-XO=5,apcc_opt,OPTIMIZE)
    AC_SYS_COMPILER_FLAG(-Xcallmod=2,apcc_opt_2,OPTIMIZE)
  ;;
esac

# Old cc for solaris.
#  AC_SYS_CPU_COMPILER_FLAG(sun4m,-Xpipe=micro,microsparc,OPTIMIZE)
#  AC_SYS_CPU_COMPILER_FLAG(sun4d,-Xpipe=super,supersparc,OPTIMIZE)
#  AC_SYS_CPU_COMPILER_FLAG(sun4u,-Xpipe=super,ultrasparc,OPTIMIZE)

  case "${CFLAGS-} $OPTIMIZE" in
     *-O*) ;;
     *+O*) ;;
     *-fast*) ;;
     *)
      AC_SYS_COMPILER_FLAG(-O2,O2,OPTIMIZE)
      if test "x$pike_cv_option_O2" != "xyes"; then
        AC_SYS_COMPILER_FLAG(-O,O,OPTIMIZE)
      fi
     ;;
  esac
 fi




if test "$GCC" = "yes"; then
  AC_SYS_COMPILER_FLAG(-pipe, pipe, CFLAGS)
else
#
# Special hack for HP-UX stupid cc
#
  if test "$pike_cv_sys_os" = "HP-UX"; then
    AC_TRY_COMPILE([int foo(int bar);],[],[],
    [
      OLD_CC="${CC-cc}"
      OLD_CFLAGS="$CFLAGS"
      if test "$CC" = "$BINDIR/smartlink cc" -a -x /usr/bin/cc ; then
        CC="$BINDIR/smartlink /usr/bin/cc"
      fi
      CFLAGS="-Ae +Olibcalls $CFLAGS"
      AC_TRY_COMPILE([int foo(int bar);],[],[],[
        CC="$OLD_CC"
	CFLAGS="$OLD_CFLAGS"
      ])
    ])
  fi
fi

CPPFLAGS="${CPPFLAGS} -I$srcdir"

if test ! -f confdefs.h ; then
  AC_MSG_WARN([Warning, lost confdefs.h, testing will be flawed!!
You might need to use bash instead of /bin/sh to get correct results.
To do this, please type the following at an sh prompt:
	\$ rm ./config.cache
	\$ CONFIG_SHELL=/path/to/bash
	\$ export CONFIG_SHELL
	\$ $CONFIG_SHELL ./configure
])
fi

AC_DEFINE(POSIX_SOURCE)

AC_MSG_CHECKING(ansi prototype capability)
AC_CACHE_VAL(pike_cv_sys_ansi_prototypes,
[
  AC_TRY_COMPILE([int foo(int bar);],[],
  [pike_cv_sys_ansi_prototypes=yes],
  [pike_cv_sys_ansi_prototypes=no])
])

if test "$pike_cv_sys_ansi_prototypes" = "yes"; then
  AC_MSG_RESULT(yes)
else
  AC_MSG_RESULT(no, giving up...)
  exit 1
fi


AC_PROG_INSTALL
AC_PROG_CPP
AC_PROG_RANLIB
AC_PATH_PROG(PIKE,pike,no)
if test $PIKE = no ; then
  echo No pike installed '(in your path)'
  exit 0
else
# ask pike where modules are
 DEST=`$PIKE --show-paths 2>&1| grep lib | grep modules | sed -e 's/.*: //'`
# asume include files $(PREFIX)/include/
 PIKEINCLUDE=`echo $DEST | sed -e "s,lib/pike/modules,include/pike,"`
fi


#############################################################################
# installbsd seems to be broken on some versions of AIX
#
# /grubba 1997-08-16

case "$ac_cv_path_install" in
  *installbsd*)
    AC_MSG_CHECKING(if $ac_cv_path_install works)
    AC_CACHE_VAL(pike_cv_install_works,
[
    if $ac_cv_path_install "$srcdir/configure.in" ./conftest >/dev/null 2>&1
    then
      if cmp "$srcdir/configure.in" ./conftest >/dev/null 2>&1; then
        pike_cv_install_works=yes;
      else
        pike_cv_install_works=no;
      fi
    else
      pike_cv_install_works=no;
    fi
    if test -f ./conftest; then
      rm -f ./conftest
    fi
])
    AC_MSG_RESULT($pike_cv_install_works)
    if test "$pike_cv_install_works" = "no"; then
      echo "Warning: Falling back to $ac_install_sh"
      INSTALL="$ac_install_sh"
    fi
  ;;
esac


AC_C_CONST

if test "$ac_cv_c_const" = "yes"; then
  AC_MSG_CHECKING(if const really does work)
  AC_CACHE_VAL(pike_cv_c_really_const,[
    AC_TRY_COMPILE([
const int foo(int bar) { return bar; }
    ], [
      return bar(0);
    ], [
       pike_cv_c_really_const=yes
    ], [
       pike_cv_c_really_const=no
    ])
  ])

  AC_MSG_RESULT($pike_cv_c_really_const)
  if test "x$pike_cv_c_really_const" = xno ; then
    AC_DEFINE(const, [])
  else
    :
  fi
fi

AC_CHECK_FUNCS( signal sigaction )

AC_HEADER_STDC
AC_CHECK_HEADERS( signal.h arpa/inet.h )
if test "${pike_cv_sys_os}" = "Linux"; then
  if test "${ac_cv_lib_m_floor}" = "no"; then
    AC_MSG_WARN(I will compensate for this by adding -lc -lm)
    LIBS="${LIBS} -lc -lm"
  fi

  if test "$ac_cv_header_sched_h" = "yes"; then
    AC_MSG_CHECKING(if sched.h has a typo)
    AC_CACHE_VAL(pike_cv_broken_sched_h, [
      AC_TRY_COMPILE([
#include <sched.h>
      ],[ ],[ pike_cv_broken_shed_h=no ], [
        OLDCPPFLAGS="$CPPFLAGS"
	CPPFLAGS="$CPPFLAGS -D_P=__P"
	AC_TRY_COMPILE([
#include <sched.h>
        ],[ ],[ pike_cv_broken_sched_h=yes ],[
          pike_cv_broken_sched_h="very broken"
        ])
	CPPFLAGS="$OLDCPPFLAGS"
      ])
    ])
    AC_MSG_RESULT($pike_cv_broken_sched_h)
    if test "$pike_cv_broken_sched_h" = "yes"; then
      AC_MSG_WARN(Applying workaround)
      CPPFLAGS="$CPPFLAGS -D_P=__P"
    fi
  fi
fi

AC_MSG_CHECKING(SO)
if test -z "$SO"
then
	case "$pike_cv_sys_os" in
	hp*|HP*)   SO=sl;;
	*)	   SO=so;;
	esac
fi
AC_MSG_RESULT($SO)

# LDSHARED is the ld *command* used to create shared library
# -- "ld" on SunOS 4.x.x, "ld -G" on SunOS 5.x, "ld -shared" on IRIX 5
AC_MSG_CHECKING(LDSHARED)
if test -z "$LDSHARED"
then
	case "$pike_cv_sys_os" in
	AIX*)
         if test "$GCC" = yes ; then
	   if test "`uname -r`" -ge 2 -a "`uname -v`" -ge 4 ; then
             LDSHARED="ld -G -bexpall"
	   else if test "`uname -v`" -gt 4 ; then
	       # Extra forward compatibility...
	       # AIX 5+
	       LDSHARED="ld -G -bexpall"
	     else
	       # AIX 4.1 and earlier don't have/have unreliable
	       # handling of dlopen et al.
	       #
	       # Even AIX 4.2.1 has buggy dlopen -- NEVER attempt
	       # to dlopen a file that hasn't been linked with -G.
	       pike_cv_sys_dynamic_loading=no
	       LDSHARED="ld"
	     fi
	   fi
         else
           # Dynamic linking turned off for AIX-cc
	   #
	   # In addition, the dynamic loading test seems to
	   # cause our RS/6000 to hang, which causes the
	   # watchdog-timer to PANIC.
	   pike_cv_sys_dynamic_loading=no
           LDSHARED="ld"
         fi
	;;
	IRIX*)
	  if test "$GCC" = yes ; then
	    # egcs defaults to building -n32 objects
	    LDSHARED="ld -shared -n32"
	  else
            LDSHARED="ld -shared"
	  fi
	;;
	SunOS*) LDSHARED="ld";;
	Solaris) LDSHARED="/usr/ccs/bin/ld -G";;
	hp*|HP*) LDSHARED="ld -b";;
	OSF*)
           LDSHARED="ld -expect_unresolved '*' -shared -msym -O3"
        ;;
	DYNIX/ptx*) LDSHARED="ld -G";;
	next) LDSHARED="$CC $CFLAGS -nostdlib -r";;
	Linux*) LDSHARED="$CC -shared";;
        GNU*) LDSHARED="ld -Bshareable";;
	FreeBSD*) LDSHARED="ld -Bshareable";;
	NetBSD) LDSHARED="ld -Bshareable";;
	OpenBSD*) LDSHARED="ld -Bshareable";;
	BSD/OS*) LDSHARED="shlicc -r";;
	SCO_SV*) LDSHARED="cc -G -KPIC -Wl,-Bexport";;
	UNIX_SV) LDSHARED="ld -G";;
        Windows_NT*) LDSHARED="$CC -shared";;
	*) LDSHARED="ld";;
	esac
fi
AC_MSG_RESULT($LDSHARED)
# CCSHARED are the C *flags* used to create objects to go into a shared
# library -- this is only needed for a few systems
AC_MSG_CHECKING(CCSHARED)
if test -z "$CCSHARED" ; then
  if test "$GCC" = yes ; then
    case $pike_cv_sys_os in
      BSD/OS*) CCSHARED="";;
      *) CCSHARED="-fpic";;
    esac
  else
    case $pike_cv_sys_os in
	hp*|HP*) CCSHARED="+z"; ;;
	FreeBSD*) CCSHARED="-fpic";;
	SCO_SV*) CCSHARED="-KPIC -dy -Bdynamic";;
	Solaris*) CCSHARED="-KPIC -Bdynamic";;
        Windows_NT*) CCSHARED="-shared" ;;
    esac
  fi
fi
AC_MSG_RESULT($CCSHARED)

AC_MSG_CHECKING(LINKFORSHARED)

# LINKFORSHARED are the flags passed to the $(CC) command that links
# the pike executable -- this is only needed for a few systems
LINKFORSHARED=""
case $pike_cv_sys_os in
 AIX*)	LINKFORSHARED="-Wl,-bexpall -Wl,-brtl";;
 hp*|HP*) LINKFORSHARED="-Wl,-E";;
 Linux*) LINKFORSHARED="-rdynamic";;
 GNU*) LINKFORSHARED="-rdynamic";;
 next) LINKFORSHARED="-u libsys_s";;
 SCO_SV*)
   if test "$GCC" = yes ; then
     LINKFORSHARED="-fpic -Wl,-Bexport"
   else
     LINKFORSHARED="-Bdynamic -dy -Wl,-Bexport"
   fi
 ;;
esac
AC_MSG_RESULT($LINKFORSHARED)

#LIBS="`gtk-config --libs` $LIBS"
#CFLAGS="`gtk-config --cflags` $CFLAGS"

AM_PATH_GTK(1.1.13,,exit 1)

CFLAGS=`echo " $CFLAGS " | sed -e 's@-g@ @g'`
GTK_CFLAGS=`echo " $GTK_CFLAGS " | sed -e 's@-g@ @g' -e 's@-O@ @g'`
OPTIMIZE=`echo " $OPTIMIZE " | sed -e 's@-g@ @g'`


echo ""
echo "Compiler summary:"
echo ""
echo "GTK_FLAGS:     $GTK_CFLAGS"
echo "CC:            $CC"
echo "LD:            $LD"
echo "LDSHARED:      $LDSHARED"
echo "CPPFLAGS:     $CPPFLAGS"
echo "CFLAGS:      $CFLAGS"
echo "CCSHARED:      $CCSHARED"
echo "WARN:          $WARN"
echo "LDFLAGS:       $LDFLAGS"
echo "LIBS:          $GTK_LIBS $LIBS"
echo "DEST:          $DEST"
echo "PIKEINCLUDE:   $PIKEINCLUDE"
echo ""

AC_OUTPUT(Makefile)
