#!/bin/sh
# $Id$
# Laurent Guerby <guerby@acm.org>
# Run ACATS with the GNU Ada compiler

# The following functions are to be customized if you run in cross
# environment or want to change compilation flags.  Note that for
# tests requiring checks not turned on by default, this script
# automatically adds the needed flags to pass (ie: -gnato or -gnatE).

EXTERNAL_OBJECTS=""
# Global variable to communicate external objects to link with, set by the script.

# gccflags="-O3 -fomit-frame-pointer -funroll-all-loops -finline-functions"
# gnatflags="-gnatN"

gccflags=""
gnatflags=""

host_gnatmake () {
gnatmake $gnatflags $gccflags $* -largs $EXTERNAL_OBJECTS
}

host_gcc () {
gcc $gccflags $*
}

target_gnatmake () {
time gnatmake $gnatflags $gccflags $* -largs $EXTERNAL_OBJECTS
}

target_gcc () {
time gcc $gccflags $*
}

target_run () {
time $*
}

# End of customization section.

dir=$(dirname $(pwd))

if [ $(basename $(pwd)) != "work" ]; then
   echo You must be in the work directory
   exit 1
fi

echo ==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%== configuration ==%%== $(date)

echo PATH=$PATH
type gcc
gcc -v 2>&1
type gnatmake
gnatls -v

echo ""
echo dir=$dir

echo ==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%== download ==%%== $(date)

if [ ! -d $dir/download ]; then
   mkdir $dir/download
   cd $dir/download
   wget http://www.adaic.org/compilers/acats/2.5/tars/acats251.tar.Z
   wget http://www.adaic.org/compilers/acats/2.5/tars/acats251.tar.Z
   wget http://www.adaic.org/compilers/acats/2.5/tars/acats252.tar.Z
   wget http://www.adaic.org/compilers/acats/2.5/tars/acats253.tar.Z
   wget http://www.adaic.org/compilers/acats/2.5/tars/acats254.tar.Z
   wget http://www.adaic.org/compilers/acats/2.5/tars/acats255.tar.Z
   wget http://www.adaic.org/compilers/acats/2.5/tars/acats256.tar.Z
   wget http://www.adaic.org/compilers/acats/2.5/tars/acats257.tar.Z
   wget http://www.adaic.org/compilers/acats/2.5/tars/acats258.tar.Z
   wget http://www.adaic.org/compilers/acats/2.5/mods/mod_2_5e.tar.Z
fi

echo ==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%== install ==%%== $(date)

if [ ! -d $dir/mod ]; then
   cd $dir
   tar xfz $dir/download/acats251.tar.Z
   tar xfz $dir/download/acats252.tar.Z
   tar xfz $dir/download/acats253.tar.Z
   tar xfz $dir/download/acats254.tar.Z
   tar xfz $dir/download/acats255.tar.Z
   tar xfz $dir/download/acats256.tar.Z
   tar xfz $dir/download/acats257.tar.Z
   tar xfz $dir/download/acats258.tar.Z
   
   mkdir $dir/mod
   cd $dir/mod
   tar xfz $dir/download/mod_2_5e.tar.Z 
fi

if [ ! -d $dir/mod ]; then
   echo Unsuccessfull download
   exit 1
fi

if [ ! -d $dir/c9.slow ]; then
   cp -r $dir/c9 $dir/c9.slow
   cd $dir/c9
   patch < $dir/acats4gnat/fastc9.patch
fi

cp -f $dir/mod/c34005p.ada $dir/c3
cp -f $dir/mod/c34005v.ada $dir/c3
cp -f $dir/mod/c380001.a $dir/c3
cp -f $dir/mod/c380002.a $dir/c3
cp -f $dir/mod/c380003.a $dir/c3
cp -f $dir/mod/c380004.a $dir/c3
cp -f $dir/mod/c390010.a $dir/c3
cp -f $dir/mod/c3a2003.a $dir/c3
cp -f $dir/mod/c460002.a $dir/c4
cp -f $dir/mod/c761010.a $dir/c7
cp -f $dir/mod/c761012.a $dir/c7
cp -f $dir/mod/c854002.a $dir/c8
cp -f $dir/mod/c854003.a $dir/c8
cp -f $dir/mod/c940004.a $dir/c9
cp -f $dir/mod/c954012.a $dir/c9
cp -f $dir/mod/cc51008.a $dir/cc
cp -f $dir/mod/cd10002.a $dir/cd
cp -f $dir/mod/cd72a02.a $dir/cd
cp -f $dir/mod/cdd2a01.a $dir/cd
cp -f $dir/mod/cdd2a02.a $dir/cd
cp -f $dir/mod/cdd2a03.a $dir/cd
cp -f $dir/mod/cxg2018.a $dir/cxg
cp -f $dir/mod/fdd2a00.a $dir/support
cp -f $dir/mod/la200012.am $dir/l
cp -f $dir/mod/lc300010.a $dir/l
cp -f $dir/mod/lc300011.a $dir/l
cp -f $dir/mod/lc300012.am $dir/l
cp -f $dir/mod/lc300020.a $dir/l
cp -f $dir/mod/lc300021.a $dir/l
cp -f $dir/mod/lc300022.am $dir/l
cp -f $dir/mod/lc300030.a $dir/l
cp -f $dir/mod/lc300031.a $dir/l
cp -f $dir/mod/lc300032.am $dir/l

echo ==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%== support ==%%== $(date)

rm -rf $dir/work/support
mkdir -p $dir/work/support
cd $dir/work/support

sed -e "s,ACATS4GNATDIR,$dir,g" < $dir/acats4gnat/impdef.a > $dir/support/impdef.a
sed -e "s,ACATS4GNATDIR,$dir,g" < $dir/acats4gnat/macro.dfs > $dir/work/support/MACRO.DFS
sed -e "s,ACATS4GNATDIR,$dir,g" < $dir/acats4gnat/tsttests.dat > $dir/work/support/TSTTESTS.DAT

cat < $dir/acats4gnat/fcndecl.ada > $dir/support/fcndecl.ada
cat < $dir/acats4gnat/impdefc.a > $dir/support/impdefc.a
cat < $dir/acats4gnat/impdefg.a > $dir/support/impdefg.a

cp $dir/cd/*.c $dir/work/support
cp $dir/cxb/*.c $dir/work/support

for i in $dir/support/{*.ada,*.a}; do 
   echo ===== $i 
   gnatchop $i
done

# These tools are used to preprocess some ACATS sources
# they need to be compiled native on the host.

host_gcc -c cd300051.c
EXTERNAL_OBJECTS="cd300051.o"
host_gnatmake macrosub
if [ $? -ne 0 ]; then
   echo Failed to compile macrosub
   exit 1
fi
EXTERNAL_OBJECTS=""
$dir/work/support/macrosub

host_gnatmake widechr
if [ $? -ne 0 ]; then
   echo Failed to compile widechr
   exit 1
fi
$dir/work/support/widechr

rm -f $dir/work/support/{macrosub,widechr,*.ali,*.o}

# From here, all compilations will be made by the target compiler

target_gcc -c *.c
if [ $? -ne 0 ]; then
   echo Failed to compile C code
   exit 1
fi

gnatchop $dir/support/*.adt

target_gnatmake -c -f -gnato report.adb

for i in *.ad?; do 
   file=$(echo $i | sed -e 's/\(.*\)\..*/\1/g')
   if [ "$i" = "report.adb" ]; then
      echo ===== $i "(done)"
   elif [ "$i" = "${file}.ads" -a -f ${file}.adb ]; then
      echo ===== $i "(spec)"
   else    
      echo ===== $i 
      target_gnatmake -c $i
   fi
done

echo ==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%== tests ==%%== $(date)

rm -rf $dir/work/run
mkdir -p $dir/work/run

# error only: b* lxd lxe lxh
for chapter in cz a c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cxa cxb cxc cxd cxe cxf cxg cxh d e l; do
   echo ""
   echo ""
   echo ""
   echo ==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%== $chapter ==%%== $(date)
   rm -rf $dir/work/$chapter
   mkdir -p $dir/work/$chapter
   cd $dir/$chapter
   ls -1 *.{a,ada,adt,am,dep} 2> /dev/null | sed -e 's/\(.*\)\..*/\1/g' | cut -c1-7 | sort | uniq | comm -23 - $dir/acats4gnat/norun.lst > $dir/work/$chapter/${chapter}.lst 
   countn=$(wc -l < $dir/work/$chapter/${chapter}.lst)
   echo "expected = $countn"
   countok=0
   counti=0
   for i in $(cat $dir/work/$chapter/${chapter}.lst); do 
      counti=$((counti + 1))
      echo ""
      echo "" 
      echo =%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%= $i =%=%= $(date) =%=%= $counti / $countn
      extraflags=""
      grep $i $dir/acats4gnat/overflow.lst > /dev/null 2>&1
      if [ $? -eq 0 ]; then
         extraflags="$extraflags -gnato"
      fi
      grep $i $dir/acats4gnat/elabd.lst > /dev/null 2>&1
      if [ $? -eq 0 ]; then
         extraflags="$extraflags -gnatE"
      fi
      mkdir $dir/work/$chapter/$i
      cd $dir/work/$chapter/$i
      gnatchop -c -w $(ls $dir/${chapter}/${i}*.{a,ada,adt,am,dep} 2> /dev/null)
      echo > ${i}.lst
      ls -1 ${i}?.adb >> ${i}.lst 2> /dev/null
      ls -1 ${i}*m.adb >> ${i}.lst 2> /dev/null
      ls -1 ${i}.adb >> ${i}.lst 2> /dev/null
      main=$(tail -1 ${i}.lst)
      binmain=$(echo $main | sed -e 's/\(.*\)\..*/\1/g')
      echo "main =" $main "binmain = " $binmain
      EXTERNAL_OBJECTS=""
      case $i in
        cxb30*) EXTERNAL_OBJECTS="$dir/work/support/cxb30040.o $dir/work/support/cxb30060.o $dir/work/support/cxb30130.o $dir/work/support/cxb30131.o";;
        ca1020e) rm -f ca1020e_func1.adb ca1020e_func2.adb ca1020e_proc1.adb ca1020e_proc2.adb > /dev/null 2>&1;;
        ca14028) rm -f ca14028_func2.ads ca14028_func3.ads ca14028_proc1.ads ca14028_proc3.ads > /dev/null 2>&1;;
        cxh1001) extraflags="-a -f"; echo "pragma Normalize_Scalars;" > gnat.adc
      esac
      if [ "$main" = "" ]; then
         echo SCRIPT-MAIN FAILED
      else
         target_gnatmake $extraflags -I$dir/work/support $main
      fi
      if [ $? -ne 0 ]; then
         echo SCRIPT-BUILD FAILED $i
      fi
      cd $dir/work/run
      target_run $dir/work/$chapter/$i/$binmain > $dir/work/$chapter/$i/${i}.log 2>&1
      cd $dir/work/$chapter/$i
      cat ${i}.log
      egrep -e '(==== |\+\+\+\+ |\!\!\!\! )' ${i}.log > /dev/null 2>&1
      if [ $? -ne 0 ]; then
         echo SCRIPT-RUN FAILED $i
      else
         countok=$((countok + 1))
      fi
      rm -f "$binmain" *.o *.ali > /dev/null 2>&1
   done
   echo ""
   echo "acats4gnat results $chapter $countok / $countn"
done

echo ==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%==%%== done ==%%== $(date)

exit 0
