dnl ####################################################################
dnl #   Copyright (c) 1996 NPACT Working Group. You may copy,          #
dnl #   distribute, and modify it freely as long as you preserve this  #
dnl #   copyright and permission notice.                               #
dnl #                                                                  #
dnl #   NPACT Working Group is a joint force of                        #
dnl #      NPAC at Syracuse University,                                #
dnl #      PACT at Harbin Institute of Technology, and                 #
dnl #      PACT at Peking University                                   #
dnl #                                                                  #
dnl ####################################################################
dnl
dnl written by Zheng Gengbin
dnl   e-mail:  zheng.gb@svr.bimp.pku.edu.cn
dnl
dnl Process this file and acconfig.h by autoconf to produce a configure script.

AC_INIT(./config)

dnl AC_PREFIX_DEFAULT(`pwd`)

AC_CONFIG_AUX_DIR(./config)

AC_CANONICAL_SYSTEM

AC_CONFIG_HEADER(./src/include/config.h:./src/include/config.hin)

AC_ARG_PROGRAM

VERSION=1.57
AC_DEFINE_UNQUOTED(VERSION, "$VERSION")
AC_SUBST(VERSION)

CONFIGSHELL=${CONFIG_SHELL:-/bin/sh}
AC_SUBST(CONFIGSHELL)

AC_PROG_CC
AC_PROG_CPP
AC_PROG_CXX

AC_PROG_AWK
AC_PROG_RANLIB

AC_HEADER_STDC

dnl determine CC initial flag
if eval "test \"`echo '$''{'ac_cv_prog_CC'}'`\" = 'gcc'"
then
  CFLAGS="-g"
else
  CFLAGS="-g"
fi

dnl determine CXX initial flag
if eval "test \"`echo '$''{'ac_cv_prog_CXX'}'`\" = 'c++'"
then
  CXXFLAGS="-g"
else
  CXXFLAGS="-g"
fi

AC_CHECK_TYPE(u_short,unsigned short)

AC_PROG_YACC
AC_PROG_LEX

AC_DECL_YYTEXT

AC_CHECK_LIB(y,main,YYLIB_L="-ly",YYLIB_L="")
AC_SUBST(YYLIB_L)

AC_CHECK_LIB(l,main,LEXLIB_L="-ll",LEXLIB_L="")
AC_SUBST(LEXLIB_L)

AC_CHECK_LIB(m,main,MATHLIB_L="-lm",MATHLIB_L="")
AC_SUBST(MATHLIB_L)

dnl check  yacc output file is y.tab.c or y_tab.c
AC_MSG_CHECKING($ac_cv_prog_YACC output file root )
AC_CACHE_VAL(ac_cv_prog_yacc_root,
cat > conftest.y <<EOF
%%
main: {};
EOF
$YACC conftest.y 1> /dev/null 2> /dev/null
if test -f y.tab.c; then
  ac_cv_prog_yacc_root='.tab'
  rm y.tab.c
elif test -f y_tab.c; then
  ac_cv_prog_yacc_root='_tab'
  rm y_tab.c
else
  { echo "configure: error: cannot find output from $YACC; giving up" 1>&2; exit 1; }
fi
rm -f conftest.y
)
AC_MSG_RESULT(y$ac_cv_prog_yacc_root)
YACC_ROOT=$ac_cv_prog_yacc_root
AC_SUBST(YACC_ROOT)

dnl check if flex add -l flag
AC_MSG_CHECKING(whether $ac_cv_prog_LEX accept -l flag)
AC_CACHE_VAL(ac_cv_lex_l_flag,
ac_cv_lex_l_flag=no
if eval "test \"`echo '$''{'ac_cv_prog_LEX'}'`\" = 'flex'"
then
cat > conftest.l <<EOF
%{
%}
%%
%{
%}
EOF
  flex -l conftest.l 1> /dev/null 2> /dev/null
  test $? = 0  && ac_cv_lex_l_flag=yes
fi
)
if eval "test \"`echo '$''{'ac_cv_lex_l_flag'}'`\" = 'yes'"
then
        LLFLAG=-l
        AC_MSG_RESULT(yes)
else
        LLFLAG=
        AC_MSG_RESULT(no)
fi
AC_SUBST(LLFLAG)

dnl determine the special yacc flag used in HP9000
AC_MSG_CHECKING(special YYFLAGS )
YYFLAG_L=" -dvt"
if eval "test \"`echo '$''{'YACC'}'`\" = 'yacc'"
then
case "$target" in
*hpux* )   YYFLAG_L=" -Nc10000 -Na20000 -Nm20000 -Ns2500 -Nl1200 -Np2000 -dvt";;
*rs6000* | *powerpc* )  YYFLAG_L=" -Nn10000 -dvt";;
esac
fi
AC_MSG_RESULT(set as \"$YYFLAG_L\")

AC_SUBST(YYFLAG_L)

dnl determine char or unsigned char yytext
AC_MSG_CHECKING(whether the yytext is defined as unsigned char)
AC_CACHE_VAL(ac_cv_prog_yytext_unsigned, 
cat > conftest.l <<EOF
%{
%}
%%
%{
%}
EOF
dnl since now lex is forced , so comment next line for future .
${ac_cv_prog_LEX} ${LLFLAG} conftest.l 1> /dev/null 2> /dev/null
dnl lex conftest.l   1>/dev/null 2>/dev/null 
LEX_OUTPUT_ROOT=$ac_cv_prog_lex_root
AC_EGREP_CPP(
  changequote(,)dnl
[[unsigned.*yytext[^a-z_]]],
  changequote([,])dnl
  [
	`cat $LEX_OUTPUT_ROOT.c`
  ]
  , ac_cv_prog_yytext_unsigned=yes ,
    ac_cv_prog_yytext_unsigned=no  )
)

if eval "test \"`echo '$''{'ac_cv_prog_yytext_unsigned'}'`\" = 'yes'"
then
  AC_DEFINE(IS_UNSIGNED_CHAR)  AC_MSG_RESULT(yes)
else
  AC_MSG_RESULT(no)
fi
	

dnl check whether the yytext is pointer .
AC_MSG_CHECKING(whether the yytext is defined as pointer )
AC_CACHE_VAL(ac_cv_prog_yytext_pointer, 
AC_EGREP_CPP(
  changequote(,)dnl
[[char[ ]*\*[ ]*yytext;]],
  changequote([,])dnl
  [
	`cat $LEX_OUTPUT_ROOT.c`
  ]
  , ac_cv_prog_yytext_pointer=yes ,
    ac_cv_prog_yytext_pointer=no )
)

if eval "test \"`echo '$''{'ac_cv_prog_yytext_pointer'}'`\" = 'yes'"
then
  AC_DEFINE(YYTEXT_IS_POINTER)  AC_MSG_RESULT(yes)
else
  AC_MSG_RESULT(no)
fi

rm -f "$LEX_OUTPUT_ROOT.c"

AC_DEFUN(AC_CHECK_G77,
[
dnl AC_MSG_CHECKING([For Fortran Compiler])
FCC=
FC_OPT=
case $target in
	
  *ibm* )

  if test "$MPICC" = yes 
  then
    AC_CHECK_PROG(FCC, mpxlf, mpxlf)
  fi
  if test "$FCC" = "" 
  then 
    AC_CHECK_PROGS(FCC, xlf90 xlf f77 g77)
  fi
  if test "$FCC" != "" 
  then 
    FC_OPT=
  fi
  if test "$FCC" = "xlf90"
  then
    AC_DEFINE(USE_F90)
  fi
  ;;

  *dec* )

  AC_CHECK_PROGS(FCC, f90 f77 g77)
  if test "$FCC" = "f90"
  then
    AC_DEFINE(USE_F90)
  fi
  ;;

  *solaris* )

  AC_CHECK_PROG(FCC, f90, f90)
  if test "$FCC" = "f90" 
  then
    cat > conftest.f <<EOF
      SUBROUTINE FOO
      END
EOF
    $FCC -c conftest.f > /dev/null 2> /dev/null
    if test $? = 0 
    then
      AC_DEFINE(USE_F90)
      FCC=f90
    else
      FCC=
    fi
  fi

  rm -f conftest.f conftest.o	

  if test "$FCC" = "" 
  then
    AC_CHECK_PROG(F77, f77, f77)
    FCC=$F77
  fi

  ;;

  *mips* )

  AC_CHECK_PROG(FCC, f90, f90)
  if test "$FCC" = "f90"
  then
    AC_DEFINE(USE_F90)
    case $ARCH in
      IRIX64)
	FCC="f90 -64"
	;;
      IRIXN32)
	FCC="f90 -n32"
	;;
      IRIX32)
	FCC="f90 -32"
	;;
      *)
	;;
    esac
  fi
  ;;

  *linux* ) 

  AC_CHECK_PROGS(FCC, g77 f77 fort77)
  if test "$FCC" = "fort77"
  then
    FC_OPT="-w"
  fi
  ;;

  *hpux* ) 

  AC_CHECK_PROGS(FCC, fort77 f77)
  if test "$FCC" != ""
  then
    FC_OPT="+T"
  fi
  if test "$FCC" = "fort77"
  then
    FC_OPT=${FC_OPT}" +w"
  fi
  ;;

  * ) 
  AC_CHECK_PROGS(FCC, f77 g77)        	
  ;; 

esac
])

dnl check Fortran 77 compiler
AC_CHECK_G77

dnl determine the special Fortran compiler's feature
if test x$FCC != x
then
dnl check if we can unparse :: in var decl
AC_MSG_CHECKING(Fortran compiler can unparse '::')
AC_CACHE_VAL(ac_cv_prog_unparse_colon, ac_cv_prog_unparse_colon=$ac_cv_prog_unparse_colon)
if test x"$ac_cv_prog_unparse_colon" = x
then
  ac_cv_prog_unparse_colon="no"
  cat > conftest.f <<EOF
        program try
        integer :: a,b
        end
EOF
  if $FCC -c $FC_OPT conftest.f 1> /dev/null 2> /dev/null && test -f conftest.o
  then
	ac_cv_prog_unparse_colon="yes"
  fi
  rm -f conftest.f
fi
AC_MSG_RESULT($ac_cv_prog_unparse_colon)
if test "$ac_cv_prog_unparse_colon" = "yes"
then
   AC_DEFINE(UNPARSE_COLON)
fi

dnl determine the special Fortran compiler's feature
AC_MSG_CHECKING(unparse 'end program')
AC_CACHE_VAL(ac_cv_prog_unparse_endprogram, ac_cv_prog_unparse_endprogram=$ac_cv_prog_unparse_endprogram)
if test x"$ac_cv_prog_unparse_endprogram" = x
then
  ac_cv_prog_unparse_endprogram="no"
  cat > conftest.f <<EOF
        program try
        end program
EOF
  if $FCC -c $FC_OPT conftest.f 1> /dev/null 2> /dev/null && test -f conftest.o
  then
	ac_cv_prog_unparse_endprogram="yes"
  fi
  rm -f conftest.f
fi
AC_MSG_RESULT($ac_cv_prog_unparse_endprogram)
if test "$ac_cv_prog_unparse_endprogram" = "yes"
then
   AC_DEFINE(UNPARSE_END_PROGRAM)
fi

dnl determine the if array is not constant declared.
AC_MSG_CHECKING(array inconstant size in $FCC)
AC_CACHE_VAL(ac_cv_prog_unparse_inconstant, ac_cv_prog_unparse_inconstant=$ac_cv_prog_unparse_inconstant)
if test x"$ac_cv_prog_unparse_inconstant" = x
then
  ac_cv_prog_unparse_inconstant="no"
  cat > conftest.f <<EOF
        subroutine try(N)
	integer N
        integer a(N)
        end
EOF
  if $FCC -c $FC_OPT conftest.f 1> /dev/null 2> /dev/null && test -f conftest.o
  then
	ac_cv_prog_unparse_inconstant="yes"
  fi
  rm -f conftest.f
fi
AC_MSG_RESULT($ac_cv_prog_unparse_inconstant)
if test "$ac_cv_prog_unparse_inconstant" = "yes"
then
   AC_DEFINE(FORTRAN_DYNAMIC_ARRAY_SIZE)
fi

dnl determine the if fortran can accept do while
AC_MSG_CHECKING(do while in $FCC)
AC_CACHE_VAL(ac_cv_prog_fortran_dowhile, ac_cv_prog_fortran_dowhile=$ac_cv_prog_fortran_dowhile)
if test x"$ac_cv_prog_fortran_dowhile" = x
then
  ac_cv_prog_fortran_dowhile="no"
  cat > conftest.f <<EOF
        program try
        do while (.true.)
        enddo
        end
EOF
  if $FCC -c $FC_OPT conftest.f 1> /dev/null 2> /dev/null && test -f conftest.o
  then
	ac_cv_prog_fortran_dowhile="yes"
  fi
  rm -f conftest.f
fi
AC_MSG_RESULT($ac_cv_prog_fortran_dowhile)
if test "$ac_cv_prog_fortran_dowhile" = "yes"
then
   AC_DEFINE(FORTRAN_DO_WHILE)
fi

dnl determine the if fortran can accept select-case
AC_MSG_CHECKING(select-case in $FCC)
AC_CACHE_VAL(ac_cv_prog_fortran_select, ac_cv_prog_fortran_select=$ac_cv_prog_fortran_select)
if test x"$ac_cv_prog_fortran_select" = x
then
  ac_cv_prog_fortran_select="no"
  cat > conftest.f <<EOF
        program try
        select case(1)	
        case default
            i=i+1
	end select
        end
EOF
  if $FCC -c $FC_OPT conftest.f 1> /dev/null 2> /dev/null && test -f conftest.o
  then
	ac_cv_prog_fortran_select="yes"
  fi
  rm -f conftest.f
fi
AC_MSG_RESULT($ac_cv_prog_fortran_select)
if test "$ac_cv_prog_fortran_select" = "yes"
then
   AC_DEFINE(FORTRAN_SELECTCASE)
fi

dnl determine the special Fortran compiler's feature
AC_MSG_CHECKING(support 'byte' in Fortran Compiler $FCL)
AC_CACHE_VAL(ac_cv_prog_fortran_byte, ac_cv_prog_fortran_byte=$ac_cv_prog_fortran_byte)
if test x"$ac_cv_prog_fortran_byte" = x
then
  ac_cv_prog_fortran_byte=""
  cat > conftest.f <<EOF
        program try
        BYTE A
        end 
EOF
  if $FCC -c $FC_OPT conftest.f 1> /dev/null 2> /dev/null && test -f conftest.o
  then
	ac_cv_prog_fortran_byte="byte"
  else 
        cat > conftest.f <<EOF
          program try
          integer*1 A
          end 
EOF
        if $FCC -c $FC_OPT conftest.f 1> /dev/null 2> /dev/null && test -f conftest.o
        then
	    ac_cv_prog_fortran_byte="integer*1"
        fi
  fi
  rm -f conftest.*
fi
AC_MSG_RESULT($ac_cv_prog_fortran_byte)
if test x"$ac_cv_prog_fortran_byte" != "x"
then
   FORTRAN_BYTE=$ac_cv_prog_fortran_byte
dnl   AC_DEFINE(FORTRAN_BYTE)
   AC_DEFINE_UNQUOTED(FORTRAN_BYTE, "$FORTRAN_BYTE")
fi


AC_MSG_CHECKING(support 'word' in Fortran Compiler $FCL)
AC_CACHE_VAL(ac_cv_prog_fortran_word, ac_cv_prog_fortran_word=$ac_cv_prog_fortran_word)
if test x"$ac_cv_prog_fortran_word" = x
then
  ac_cv_prog_fortran_word=""
  cat > conftest.f <<EOF
        program try
        WORD A
        end 
EOF
  if $FCC -c $FC_OPT conftest.f 1> /dev/null 2> /dev/null && test -f conftest.o
  then
	ac_cv_prog_fortran_word="word"
  else
        cat > conftest.f <<EOF
          program try
          integer*2 A
          end 
EOF
        if $FCC -c $FC_OPT conftest.f 1> /dev/null 2> /dev/null && test -f conftest.o
        then
	    ac_cv_prog_fortran_word="integer*2"
        fi
  fi
  rm -f conftest.*
fi
AC_MSG_RESULT($ac_cv_prog_fortran_word)
if test x"$ac_cv_prog_fortran_word" != "x"
then
   FORTRAN_WORD=$ac_cv_prog_fortran_word
dnl   AC_DEFINE(FORTRAN_WORD)
   AC_DEFINE_UNQUOTED(FORTRAN_WORD, "$FORTRAN_WORD")
fi


else
   echo "No Fortran Compiler found!"
fi

AC_HEADER_SYS_WAIT
AC_HEADER_TIME
AC_CHECK_HEADERS(sys/time.h)
AC_CHECK_HEADERS(malloc.h)

dnl AC_FUNC_VPRINTF
dnl AC_C_CROSS

AC_CHECK_SIZEOF(short,2)
AC_CHECK_SIZEOF(int,4)
AC_CHECK_SIZEOF(float,4)
AC_CHECK_SIZEOF(double,8)

dnl check cbrt
AC_MSG_CHECKING(for cbrt)
ac_link_save=$ac_link
ac_link="$ac_link $MATHLIB_L"
AC_TRY_LINK([#include <math.h>] , 
 [ cbrt(10.0) ] , 
 [AC_MSG_RESULT(yes)] 
 AC_DEFINE(HAVE_CBRT),
 [AC_MSG_RESULT(no)]
)
ac_link=$ac_link_save


dnl AC_PATH_X
dnl set configure for Motif
AC_MSG_CHECKING(for X)
AC_CACHE_VAL(ac_cv_char_xincludes_val, X_INCLUDES=$ac_cv_char_xincludes_val)
AC_CACHE_VAL(ac_cv_char_ldoptions_val, LDOPTIONS=$ac_cv_char_ldoptions_val)
AC_CACHE_VAL(ac_cv_char_ldlibs_val, X_LDLIBS=$ac_cv_char_ldlibs_val)
if eval "test -z \"`echo '$''{'X_INCLUDES'}'`\" " && eval "test -z \"`echo '$''{'LDOPTIONS'}'`\" "
then
rm -fr conftestdir
if mkdir conftestdir
then
  cd conftestdir
  # Make sure to not put "make" in the Imakefile rules, since we grep it out.
  cat > Imakefile <<'EOF'
acfindx:
	@echo 'X_INCLUDES="${STD_INCLUDES}";LDOPTIONS="${LDOPTIONS}";INCROOT="${INCROOT}";X_LDLIBS="${LDLIBS}"'
EOF
 if (xmkmf) >/dev/null 2>/dev/null && test -f Makefile
 then
	eval `${MAKE-make} acfindx 2>/dev/null | grep -v make`
	have_X=yes
 fi
 cd ..
 rm -fr conftestdir
fi
if eval "test -z \"`echo '$''{'X_INCLUDES'}'`\" "
then
  if eval "test -n \"`echo '$''{'INCROOT'}'`\" "
  then
    X_INCLUDES=-I$INCROOT
  fi
fi
ac_cv_char_xincludes_val=$X_INCLUDES
ac_cv_char_ldoptions_val=$LDOPTIONS
ac_cv_char_ldlibs_val=$X_LDLIBS
fi  
AC_MSG_RESULT(\"header.. $X_INCLUDES lib.. $LDOPTIONS $X_LDLIBS\")
AC_SUBST(X_INCLUDES)
AC_SUBST(LDOPTIONS)
AC_SUBST(X_LDLIBS)

AC_DEFUN(AC_XT_VERSION6,
[AC_MSG_CHECKING([For Xt Revision Number 6])
  ac_link_save=$ac_link
  ac_link="$ac_link $X_INCLUDES"
  AC_TRY_LINK([#include <X11/Intrinsic.h>],
[#if XtSpecificationRelease < 6
fail;
#endif
] , XTversion="6",
   [AC_MSG_RESULT(no)])
  ac_link=$ac_link_save
])

AC_DEFUN(AC_XT_VERSION5,
[AC_MSG_CHECKING([For Xt Revision Number 5])
  ac_link_save=$ac_link
  ac_link="$ac_link $X_INCLUDES"
  AC_TRY_LINK([#include <X11/Intrinsic.h>],
[#if XtSpecificationRelease < 5
fail;
#endif
], XTversion="5",
   [AC_MSG_RESULT(no)])
  ac_link=$ac_link_save
])

AC_DEFUN(AC_XT_VERSION4,
[AC_MSG_CHECKING([For Xt Revision Number 4])
  ac_link_save=$ac_link
  ac_link="$ac_link $X_INCLUDES"
  AC_TRY_LINK([#include <X11/Intrinsic.h>],
[#if XtSpecificationRelease < 4
fail;
#endif
], XTversion="4",
   [AC_MSG_RESULT(no)])
  ac_link=$ac_link_save
])

if test "$have_X" = "yes"
then
  XTversion=""
  AC_XT_VERSION6
  if test "x$XTversion" = x ; then
    AC_XT_VERSION5
    if test "x$XTversion" = x; then
      AC_XT_VERSION4
      if test "x$XTversion" = x; then
        AC_MSG_WARN(You must have X11 Revision 4 or higher to compile Vdumpdep)
        have_X="no"
      else
        AC_MSG_RESULT(revision 4)
      fi
    else
      AC_MSG_RESULT(revision 5)
    fi
  else
    AC_MSG_RESULT(revision 6)
    R6libs="-lXext -lSM -lICE"
  fi
fi
AC_SUBST(R6libs)

if test -d src/tools/Vdump -a "$have_X" = "yes"
then
   AC_DEFINE(WANT_LIST)
   ac_cpp_save=$ac_cpp
   ac_cpp="$ac_cpp_save $X_INCLUDES"
   AC_CHECK_HEADER(Xm/Xm.h,
         VDUMP_MAKEFILE="src/tools/Vdump/Makefile  src/tools/Vdump/lib/Makefile src/tools/Vdump/proflib/Makefile"
         VDUMP="Vdump"
   )
   AC_SUBST(VDUMP)
   ac_cpp=$ac_cpp_save
fi

if test "$VDUMP" = "Vdump"
then
   if test "$ac_cv_prog_gxx" = "yes"
   then
   	AC_DEFINE(NEED_PROFILE)
   	VDUMP_OBJ='$(OBJS_PROFILE)'
	echo "Vdumpdep profiling tools enabled."
   else
   	VDUMP_OBJ='$(OBJS_COMPACT)'
	echo "Vdumpdep profiling tools disabled."
   fi
   AC_SUBST(VDUMP_OBJ)
fi

dnl  to deal with the intrinsic library's path
PREFIX=$prefix ; EXEC_PREFIX=$exec_prefix
test "x$prefix" = xNONE && PREFIX=$ac_default_prefix
test "x$exec_prefix" = xNONE && EXEC_PREFIX='${PREFIX}'
SRCDIR=`pwd`
LIBDIR=`eval echo $EXEC_PREFIX/lib`
test ! x`echo $LIBDIR | cut -f2 -d: | cut -f1 -d/` = x  &&  LIBDIR="$SRCDIR/$LIBDIR"
BINDIR=`eval echo $EXEC_PREFIX/bin`
test ! x`echo $BINDIR | cut -f2 -d: | cut -f1 -d/` = x  &&  BINDIR="$SRCDIR/$BINDIR"
AC_DEFINE_UNQUOTED(LIBDIR,"$LIBDIR")
AC_DEFINE_UNQUOTED(SRCDIR,"$SRCDIR")
AC_SUBST(SRCDIR)
AC_SUBST(BINDIR)

AC_OUTPUT(Makefile src/Makefile 
 src/include/Makefile src/basicop/Makefile  src/hpf2dep/Makefile 
 src/hpf2dep/fpp:src/hpf2dep/fpp.in src/java2dep/jpp:src/java2dep/jpp.in
 src/hpfsc/Makefile  src/xsageop/Makefile src/trans2/Makefile src/trans0/Makefile
 src/trans1/Makefile  src/tof77/Makefile  
 src/generator/Makefile
 src/tools/Makefile  src/tools/dump/Makefile  src/tools/unparse/Makefile 
 src/java2dep/Makefile
 src/SageAnalysisTool/Makefile src/SageAnalysisTool/OmegaForSage/Makefile
 src/SageAnalysisTool/DemoDataDep/Makefile 
 src/SageAnalysisTool/DemoDataDep/DEMOFILE1/Makefile 
 src/SageAnalysisTool/DemoDataDep/DEMOFILE2/Makefile 

 $VDUMP_MAKEFILE
 script/Makefile
 script/ss:script/ss.in
 testsuite/Makefile 
 testsuite/examples/Makefile testsuite/syntax/Makefile testsuite/semantics/Makefile
 testsuite/hpfA/Makefile testsuite/simple/Makefile ,

chmod u+x src/hpf2dep/fpp
chmod u+x script/ss
)


echo Done!

