#!/bin/sh
# RCS $Id: r_make.bat,v 1.3 1994/11/02 14:05:40 ae59 Exp ae59 $
####################################################################
# Name       : r_make.bat                                          #
# Description: compiles C sources, generates library for PXSC      #
# Author     : H. Berlejung                                        #
# Systems    : Korn-shell on SUN Os 4.1, HP UX                     #
#                                                                  #
# Copyright  : (C) All rights reserved to University of Karlsruhe  #
####################################################################
#
# pathes
#
#rtssrc=$HOME/sources/rts  # special path for developer 
#rtslib=$HOME/sources/lib
rtssrc=.
rtslib=.
hwhp=$rtssrc/hw-hp
hwsun=$rtssrc/hw-sun
#
# constants
#
onumber=713                     # number of C sources of current version
sunonumber=`expr $onumber - 11` #   "     "  "        of sun hardware version
hponumber=`expr $onumber - 11`  #   "     "  "           hp     "        "  
rtonumber=$onumber              #   "     "  "           rt     "        "
isonum=0                        # current number of object files 

#
# message for user
#
if test $# -eq 0
then
   echo
   echo " r_make.bat  compiles all runtime C source files in"
   echo "             "$rtssrc" to "$rtslib
   echo
   echo " Call: r_make.bat -a [afile] [-h] [-l] [-g] [-t] [-d] "
   echo
   echo " -a [afile] : generate archive of object files in afile"
   echo "              omitting afile switches to default settings:"
   echo "              using :rts.a , software version , o_tsrt.txt"
   echo " -h         : use IEEE hardware (sets IEEE_HARDWARE switch)"
   echo " -l         : don't compile, link only"
   echo " -g         : use gcc instead of cc (can be omitted for SUN)."
   echo " -t         : save sorted object list in o_tsrt.txt        "
   echo " -tv        : generate Testversion (define TEST_VERSION)     "
   echo " -d         : compile with debug option                      "
   echo 
   echo " At least -a option MUST be specified."
   echo " WARNING: Existing objects and libraries will be replaced !!"
   echo " Example: r_make.bat -a rts.a # generates rts.a in current directory."
   echo "          r_make.bat -a       # generates rts.a in current directory."
   echo 
   exit 0
fi

#
#default settings:
# o all existing objects and libraries with the same name will be replaced !!
# o software arithmetic
# o compile all sources in current directory
# o delete help files r_make.out.x
# o use cc -c -O to compile C and S sources
#

#
#aliases
#
CCOMP=cc
OPT="-c"
ADD="-O"
DEFS=""
#
# help files
#
rmfile=r_make.out
hfile=$rmfile.1                 # list of all C sources
sfile=$rmfile.2                 # list of neccessary sources
tfile=$rmfile.3                 # list of ordered library files
tfname=$rtssrc/o_tsrt.txt

#
# flags
#
default=0                       # no standard values unless -s specified
compile=1                       # compile all sources
afile=""                        # no default library name
hwon=0                          # software arithmetic 2=SUN, 3=HP, 4=RT
save=0                          # don't save topologicAL sorted list

while [ $# -ne 0 ]
do
   case $1 in
   -a)         
       if [ $# -eq 1 ]
       then
          echo "Empty Argument of option -a , using library : rts.a"
          afile=$rtslib/rts.a
          default=1
       else 
		shift
          case $1 in
          -*) echo "unallowed filename" $1 " (too dangerous) aborted."
		    exit 1
		;;
		*) afile=$rtslib/$1
		esac
       fi
       ;;
   -l) echo Link only objects in $rtslib.
          compile=3
       ;;
   -g) echo Use GNU C instead of cc for compiling !!
       CCOMP=gcc
       ;;
   -d) echo "Compile with debug option -g !!"        
       ADD="-g"
       ;;
   -h) hwon=1
       ;;
   -t) save=1
          ;;        
   -tv) DEFS="-DTEST_VERSION"
   ;;
   *) echo Invalid option.
      ;;
   esac
   shift
done

if test "$afile" = ""
then
        echo '-a  must be specified , r_make.bat aborted.'
        exit 1
fi

#
# move to library folder
#
cd $rtslib

#
# get selected compiler switch
#
if [ $compile -eq 1 ]
then
#
# compile files             
#
if [ $default -eq 0 ]
then
#
# check for CCOMP    usage
#
grep '#define[        ]*SUN4_CPP_C[   ]*1' $rtssrc/o_slct.h> $hfile
if [ -s $hfile ]
then
        rm $hfile
        echo Use SUN C++ precompiler
        CCOMP=CC
        if [ $hwon -eq 1 ]
        then
                hwon=2
                echo Use SUN IEEE Hardware
                onumber=$sunonumber
        fi
else
#
# check for gnu c usage
#
grep '#define[        ]*SUN4_GNU_C[   ]*1' $rtssrc/o_slct.h> $hfile
if test -s $hfile
then
        rm $hfile
        CCOMP=gcc
        echo Use SUN GNU C 
        if test $hwon -eq 1
        then
                hwon=2
                echo Use SUN IEEE Hardware
                onumber=$sunonumber
        fi
fi

fi

if test $hwon -eq 1
then
        grep '#define[        ]*SUN4_OS4_C[   ]*1' $rtssrc/o_slct.h> $hfile
        if test -s $hfile
        then
                hwon=2
                echo Use SUN IEEE_HARDWARE
                onumber=$sunonumber
        else
                rm $hfile
                grep '#define[        ]*HP_9000_C[    ]*1' $rtssrc/o_slct.h> $hfile
             if test -s $hfile
                then
                        hwon=3
                        echo Use HP IEEE_HARDWARE
                        onumber=$hponumber
                else
                        rm $hfile
                        grep '#define[        ]*IBM_RT_C[     ]*1' $rtssrc/o_slct.h> $hfile
                if test -s $hfile
                        then
                                hwon=4
                                echo Use IBM RT IEEE_HARDWARE
                                onumber=$rtonumber
                        else
                                echo Sorry, no hardware support available!
                                hwon=0
                        fi
                fi
        fi
        rm $hfile
fi # if hwon

#
# get source names according selected options
#
# default setting
ls $rtssrc/*.c > $sfile 

if test $hwon -eq 2
then
#
# sun hardware
# 

#changed after hw correction of std functions
#       egrep -v 'r_add[dnu]|r_sub[dnu]|r_mul[dnu]|r_div[dnu]' $sfile |
#       egrep -v 'b_tad[dj]\.c|b_tcom\.c|b_tdek\.c|b_tdiv\.c|b_tmdv\.c|t_cmpe\.c|t_sle_\.c' >$hfile
#  ls $rtssrc/hw-sun/*.s >> $hfile

        egrep -v 'r_add[dnu]|r_sub[dnu]|r_mul[dnu]|r_div[dnu]' $sfile  >$hfile
        ls $rtssrc/hw-sun/r_ari.s >> $hfile
        mv $hfile $sfile
        OPT="-c -DIEEE_HARDWARE"
else
        if test $hwon -eq 3
        then
#
# hp hardware
#
                egrep -v 'r_add[dnu]|r_sub[dnu]|r_mul[dnu]|r_div[dnu]' $sfile > $hfile
                ls $rtssrc/hw-hp/r_ari.s >> $hfile
                mv $hfile $sfile
                OPT="-c -DIEEE_HARDWARE"
        else
                if test $hwon -ne 0
                then
#
# rt hardware
#
                        OPT="-c -DIEEE_HARDWARE"
                fi
        fi
fi #if hwon =2

#
# Compile all C sources with selected settings 
#     #undef NODSTD
#     #undef HEAP_CHECK
#
echo Removing old object files in `pwd`.
rm *.o
echo Compiling C sources in $rtssrc with default settings.
cat $sfile| xargs -t -n1 -i $CCOMP $OPT $DEFS $ADD {}

rm -f $sfile
rm -f $hfile
else
	#default compilation
	echo Removing old object files in `pwd`.
	rm *.o
	$CCOMP $OPT $ADD $rtssrc/*.c
fi # if default =0
fi # compile =1

if [ $default -eq 0 ]
then
 echo Determine dependencies of object files in `pwd`.
 lorder *.o >$hfile

 echo Determine topological ordering of object files.
 tsort <$hfile >$tfile
else
	cp $rtssrc/o_tsrt.txt $tfile
fi
#
# Archive object files in library.
#
if test "$afile" != ""
then
      if test -f $afile
      then
         echo Remove old library file $afile.
         rm $afile
      fi
      echo Archive object files in `pwd` in library $afile.
      cat $tfile | xargs -n200 ar cq $afile
      ranlib $afile
      echo "Number of runtime files in current version : "$onumber
      isonum=`ar t $afile | grep '.o' | wc  -l`
#        isonum=`expr $isonum - 1`
      echo "Actual number of object files in archive   :"$isonum

      if test $isonum -eq $onumber
      then
          echo "Library " $afile " seems complete !"  
      else
          if test $isonum -lt $onumber
          then
		 echo "WARNING: Library " $afile " seems incomplete !"
		else
		 echo "WARNING: Library " $afile " has more files than expected !!"
		fi
         fi
fi
#
# Remove unused files
#
rm -f $sfile
rm -f $hfile

if test -f $tfile
then
        if test $save -eq 0
        then
                rm $tfile
        else
                echo Save sorted file list in $tfname
                mv $tfile $tfname
        fi
fi
exit 0
#---- eof
