#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/bigloo/autoconf/stacksize            */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Fri Apr  5 08:43:23 2002                          */
#*    Last change :  Mon Jul 10 13:57:32 2023 (serrano)                */
#*    Copyright   :  2002-23 Manuel Serrano                            */
#*    -------------------------------------------------------------    */
#*    Check if the stack is large enough for compiling Bigloo.         */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    flags                                                            */
#*---------------------------------------------------------------------*/
cflags=-O
stacksize=3

#*---------------------------------------------------------------------*/
#*    We parse the arguments                                           */
#*---------------------------------------------------------------------*/
while : ; do
  case $1 in
    "")
      break;;

    --cflags=*|-cflags=*)
      cflags="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --stacksize=*|-s=*)
      stacksize="`echo $1 | sed 's/^[-a-z]*=//'`";;

    -*)
      echo "Unknown option \"$1\", ignored" >&2;;
  esac
  shift
done

file=$TMP/actest$USER
aout=$TMP/Xactest$USER

#*---------------------------------------------------------------------*/
#*    compile                                                          */
#*---------------------------------------------------------------------*/
compile="$CC $cflags $file.c -o $aout >/dev/null"

#*---------------------------------------------------------------------*/
#*    The test C file                                                  */
#*---------------------------------------------------------------------*/
if( test -f $file.c ); then
   rm -f $file.c || exit $?
fi

rm -f $aout 2> /dev/null
rm -f $aout.exe 2> /dev/null

#*---------------------------------------------------------------------*/
#*    Test                                                             */
#*---------------------------------------------------------------------*/
cat > $file.c <<EOF
int (*fun)();
int glob;

int foo( int x, int y ) {
  return x * y;
}

int bar( int x, int y ) {
  return x + y;
}

int ssize( int size ) {
   glob++;

   if( size == 0 )
      return 0;
   else
      return fun( size, ssize( size - 1 ) );
}

int main( int argc, char *argv[] ) {
   fun = argc > 2 ? &bar : &foo;

   if( ssize( $stacksize * 1024 * 32 ) >= 0 )
      return 0;
   else 
      return argc-1;
}
EOF

#*---------------------------------------------------------------------*/
#*    Compilation test                                                 */
#*---------------------------------------------------------------------*/
status=1
if eval "$BUILDSH $compile"; then
   \rm -f $file.*
   if eval "$HOSTSH $aout"; then
     status=0;
   fi
fi

rm -f $aout
rm -rf $aout*

echo $status
