#!/bin/sh
#
# Occam For All project KROC (Kent Retargetable Occam Compiler) Driver program
#
# $Source: /proj/kroc/develop/bin/RCS/kroc.in,v $
#
# $Id: kroc.in,v 1.34 1997/02/17 13:29:46 djb1 Exp $
#
# (C) Copyright 1995,1996,1997 Dave Beckett <D.J.Beckett@ukc.ac.uk>
# University of Kent at Canterbury, England
#
# If this file has the name "kroc.in" then it is a template for the kroc
# driver program; to generate the actual kroc script, run
# "./configure" at the root of the distribution. This is a
# configuration script generated by hand (constructs like "@foo@" will get
# replaced in the actual script.

# Only ever calls echo and sed UNIX programs (via `s)
progname=`echo $0 | sed 's/^.*\///'`

KROC=/home/jm40/linux_kroc/kroc-1.0.3beta-linux-glibc1

# Only the 'kroc' program need be in the users path
PATH=$KROC/bin:$PATH

package_version=1.0.3beta
host_canonical=i586-pc-linux-gnulibc1
octranopts=""
ccopts=""
ocllibs="-loccam8 -loccamutl"
ocrtslibs="-lm"

fatal=

# Options to TRANPC (octran for 386)
octranopts="$octranopts -e" 
#octranopts="$octranopts -s" for asm output

# Options to occ21

# Warnings:
# -nwca : No warnings for CHAN OF ANY
# -nwgy : No warnings on GUY construct
# -nwu  : No Unused Name Warnings
# -nwp  : No Unused Parameter Warnings
occ_no_warn_opts="-nwca -nwgy -nwp -nwu"
# -walign : Warn on alignment checks
# -wall   : Enable all warnings
# -wd     : Provide descoped name warnings
# -wo     : Provide overlap check warnings
occ_warn_opts="-walign -wall -wd -wo -wqual"

# Processor class:  -tX argument to occ21 and .kXX temporary file suffix
proc_class='t8'
tsuffix='tce'

#
# -w    : Full Code Insertion
# -y    : Disable Library I/O
# -etc  : Enable ETC code generation.
# -znd  : No Debug Information At All
# -znec : Allow comments anywhere (makes folded source legal in more cases)

# Default: medium warnings
occwopts=
occopts="-etc -w -y -znd -znec -udo -zncc"

occ21=${OCC21:-occ21}
octran=${OCTRAN:-tranpc}
ocrtsdir=${OCRTSDIR:-$KROC/lib}
ocrts=${OCRTS:-$ocrtsdir/libkroc.a}

cc=${CC:-gcc}
as=${AS:-as}
asflags=

# Assembler options needed A1 and A2 are used to define alignment.
# Different versions of the gnu assembler require different values:
# try A1=2, A=1 (powers rather than absolutes).
asflags="$asflags --defsym A1=4 --defsym A2=2 -W"

ocspath=
if [ "X$OCSEARCH" != "X" ]; then
  ocspath=$OCSEARCH
fi
ocspath="$ocspath $ocrtsdir"

oclibpath=
ocsearch=
clincludepath=
for dir in $ocspath; do
  dir=`echo $dir | sed -e 's:/$::'`
  oclibpath="$oclibpath -L$dir"
  ocsearch="$ocsearch $dir"
  cincludepath="$cincludepath -I$dir"
done

srcs=
verbose=
compile=yes
translate=yes
link=yes
outfile=
dorun=yes
showversion=
showlibpath=
showincpath=
delete_temp=yes

while :
do
	case $# in
	0)
		break
		;;
	esac

	option=$1
	shift

	orig_option=$option     # Save original for error messages

# Split out the argument for options that take them
	case $option in
	--*=*)
		optarg=`echo $option | sed -e 's/^[^=]*=//'`
		;;
# These options have mandatory values.  Since we didn't find an = sign,
# the value must be in the next argument
	--octran-opts | --oc-opts | --cc-opts)
		optarg=$1
		shift
		;;
	esac


	case "$option" in
	-b | --brief)	occopts="$occopts -b"
		;;
	-c | --compile)	link=;
		octranopts="$octranopts -c"
		;;
	-p | --pre-compile) translate=;  link=
		;;
	-n | --dry-run)  dorun=;
		verbose=yes
		;;
	--dryrun)  dorun=;
	        echo "$progname: --dryrun is obsolete, please use --dry-run" 1>&2
		verbose=yes
		;;
	-o)     outfile=$1;
		shift
		;;
	-o*)	outfile=`echo $option | sed -e 's/^..//'`
		;;
	--octran-opts*)	octranopts="$octranopts $optarg"
		;;
	--oc-opts*) occopts="$occopts $optarg"
		;;
	--cc-opts*) ccopts="$ccopts $optarg"
		;;
	-l*)	ocllibs="$ocllibs $option"
		;;
	-L*)	dir=`echo $option | sed -e 's:/$::' -e 's:^..::'`
		oclibpath="-L$dir $oclibpath"
		ocsearch="$dir $ocsearch"
		;;
	-I*)	dir=`echo $option | sed -e 's:/$::' -e 's:^..::'`
		ocsearch="$dir $ocsearch"
		cincludepath="-I$dir $cincludepath"
		;;
	-v | --verbose | --v | --verb*)
		verbose=yes
		;;
	-k | --keep-temp)
		delete_temp=
		;;
	--version)
		showversion=yes
		;;
	--libpath)
		showlibpath=yes
		;;
	--incpath)
		showincpath=yes
		;;
	-h | --help | --he*)
		fatal=yes
		;;
	-w | --warn)
		occwopts=$occ_warn_opts
		;;
	-nw | --no-warn)
		occwopts=$occ_no_warn_opts
		;;
	--nowarn)
	        echo "$progname: --nowarn is obsolete, please use --no-warn" 1>&2
		occwopts=$occ_no_warn_opts
		;;
	-t2 | -t4 | -t8 | -t9 )
		proc_class=`echo $option | sed -e 's/^.//'`
		tsuffix="k$proc_class"
	        echo "$progname: Option $option is experimental: setting processor class to $proc_class" 1>&2
		;;
	-X1 )
	        echo "$progname: Selecting experimental ETC output" 1>&2
		proc_class=etc
		tsuffix=etc
		occopts="$occopts -etc"
		;;
	-X2 )
	        echo "$progname: Allowing experimental user defined operators" 1>&2
		occopts="$occopts -udo"
		;;
	--* | -*)
		echo "$progname: Unrecognized option: \"$orig_option\"; use --help for usage." 1>&2
		exit 1
		;;
	*)      srcs="$srcs $option"
		;;

	esac
done

# Add current directory first, always
oclibpath="-L. $oclibpath"
ocsearch=". $ocsearch"
cincludepath="-I. $cincludepath"
procopts="-$proc_class"
occopts="$procopts $occopts"

if [ $showlibpath ]; then
 echo $oclibpath | sed -e 's:-L::g'
 exit 0
fi

if [ $showincpath ]; then
 echo "$ocsearch " | sed -e 's:^ ::' -e 's: $::'
 exit 0
fi

if [ "X${fatal}" != "X" -o "X$srcs" = "X" -o "X$showversion" != "X" ]; then
   dversion=`echo '$Id: kroc.in,v 1.34 1997/02/17 13:29:46 djb1 Exp $' | awk '{print $3}'`

   exec 1>&2
   echo "KROC version $package_version targeting $host_canonical (driver V$dversion)"

   if [ $verbose ]; then
     showversion=yes
   fi

   if [ $showversion ]; then
     cat $KROC/AUTHORS
     #echo "(C) Copyright 1995,1996,1997 Dave Beckett <D.J.Beckett@ukc.ac.uk>"
     #echo "University of Kent at Canterbury, England"
     exit 0
   fi

	exec 1>&2
echo "Usage: $progname [options] [occam sources/pre-compiled sources]"
echo "Options:"
echo "  -b,  --brief         Give brief occ21 error messages"
echo "  -c,  --compile       Compile source to objects, do not link"
echo "  -h,  --help          Print this message and exit"
echo "  -IDIRECTORY          Search DIRECTORY for occam libs & includes, C headers"
echo "  -k,  --keep-temp     Keep temporary files"
echo "  -v,  --version       Print version"
echo "  -lNAME               Link with native library libNAME.a"
echo "  -LDIRECTORY          Search DIRECTORY for native libraries"
echo "  -n,  --dry-run       Do not run any commands; just print them."
echo "       --oc-opts=opts  Use these occam compiler ($occ21) options"
echo "       --oc-opts opts    \"\""
echo "       --cc-opts=opts  Use these C compiler ($cc) options"
echo "       --cc-opts opts    \"\""
echo "  -oFILE, -o FILE      Place output in file FILE"
echo "  -p,  --pre-compile   Pre-compile occam source to .ktx files"
echo "  -t2, -t4, -t8, -t9   Set processor compilation class (default $proc_class)"
echo "  -v,  --verbose       Show commands run during compilation"
echo "  -w,  --warn          Additional warnings from $occ21"
echo "  -X1, -X2             Experimental options (ETC, UDO)"
echo "  -nw, --no-warn       Give no warnings from $occ21"
	exit 0
fi

objs=
tmp_objs=
efile=
for src in $srcs; do
  origsrc=$src
  suffix=`echo $src | sed -e 's/^.*\.\(.*\)$/\1/'`

  # Handle file. (no suffix)
  if [ "X$suffix" = "X"  ]; then
    nsrc="${src}occ"
    if [ -r $nsrc ]; then
      echo "$progname: Assuming you meant to compile $nsrc" 1>&2
      src=$nsrc
      suffix=occ
    else
      echo "$progname: Ignoring $src (no $nsrc found)" 1>&2
      continue
    fi
  # Handle file (no . at all)
  elif [ "$suffix" = "$src" ]; then
    if [ -r "$src.occ" ]; then
      echo "$progname: Assuming you meant to compile $src.occ" 1>&2
      suffix=occ
    else
      echo "$progname: Ignoring $src (no $src.occ found)" 1>&2
      continue
    fi
  fi

  dir=`echo $src | sed -e 's%[^/][^/]*$%%'`
  if [ "X$dir" = "X$src" ]; then
    dir=
  else
    src=`echo $src | sed -e 's%^.*/%%'`
    base=`echo $src | sed -e 's/\.[^\.]*$//'`
  fi

  base=`echo $src | sed -e 's/\.[^\.]*$//'`

  # Pick output name, either from -o name if given or basename of name
  if [ "X$link" != "X" -a "X$efile" = "X" ]; then
    if [ "X$outfile" = "X" ]; then
      efile=$base
    else
      efile=$outfile
      outfile=
    fi
  fi


  # Using pre-compiled file?   FIXME: Handle this better
  if [ $suffix = "etc" -o $suffix = "kt2" -o $suffix = "kt4" -o $suffix = "kt8" -o $suffix = "kt9" ]; then
    compile=
    tfile="$dir$base.$suffix"
    tcefile="$dir$base.tce"

    if [ ! -r $tfile ]; then
      echo "$progname: Cannot find pre-compiled file '$tfile'" 1>&2
      exit 1;
    fi
    if [ ! -r $tcefile ]; then
      echo "$progname: Cannot find pre-compiled TCE file '$tcefile'" 1>&2
      exit 1;
    fi
  fi

  if [ $suffix != "occ" -a "X$compile" != "X" ]; then
    objs="$objs $origsrc"
    continue
  fi

  src="$dir$base.occ"

  if [ ! -r $src ]; then
    echo "$progname: Cannot find occam source file '$src'" 1>&2
    exit 1
  fi

  if [ $link ]; then
    if [ $efile = $src ]; then
      efile="$efile.out";
      echo "$progname: Compiling '$src' to '$efile'" 1>&2
    fi
  fi

  tmpfiles=

  # .ktX files and TCO files
  if [ $compile ]; then
    tcefile="$base.tce"
    if [ "X$translate" != "X" -a "X$outfile" != "X" ]; then
      tfile=$outfile
    else
      tfile="$base.$tsuffix"
    fi
    tmpfiles="$tmpfiles $tcefile $tfile"
  fi

  # Native assembler file
  sfile="$base.s"
  tmpfiles="$tmpfiles $sfile"

  # Native object file
  if [ "X$link" != "X" -a "X$outfile" != "X" ]; then
    ofile=$outfile
  else
    ofile="$base.o"
  fi
  tmpfiles="$tmpfiles $ofile"

  if [ $delete_temp ]; then
    trap "rm -f $tmpfiles" 1 2 3 9 15
  fi

  ISEARCH=$ocsearch; export ISEARCH
  if [ $verbose ]; then
    echo "$progname: Search path for native libraries:" 1>&2
    echo "  $oclibpath" | sed -e 's:-L::g' 1>&2
    echo "$progname: Search path for occam libraries and includes; C headers:" 1>&2
    echo "  $ocsearch" 1>&2
  fi


  if [ $compile ]; then
    if [ $dorun ]; then
      rm -f $tfile $tcefile
    fi

    cmd="$occ21 $occopts $occwopts $src -o $tfile"
    if [ $verbose ]; then
      echo $cmd 1>&2
    fi
    if [ $dorun ]; then
      $cmd
      if [ $? -ne 0 ]; then
        echo "$progname: $occ21 failed to compile $src to $tfile" 1>&2
        exit 1;
      fi
      if [ $link ]; then
        if [ $delete_temp ]; then
	  tmp_objs="$tmp_objs $tfile $tcefile"
        fi
      fi
    fi
  fi


  if [ $translate ]; then
    if [ $dorun ]; then
      rm -f $sfile
    fi

    cmd="$octran $octranopts $tfile" # $sfile"
    if [ $verbose ]; then
      echo $cmd 1>&2
    fi
    if [ $dorun ]; then
      $cmd
      if [ $? -ne 0 ]; then
        echo "$progname: $octran failed to translate $tfile to $sfile" 1>&2
        exit 1;
      fi
    fi

    #if [ $dorun ]; then
    #  rm -f $ofile
    #fi

    #cmd="$as $asflags $sfile -o $ofile"
    #if [ $verbose ]; then
    #  echo $cmd 1>&2
    #fi
    #if [ $dorun ]; then
    #  $cmd
    #  if [ $? -ne 0 ]; then
    #    echo "$progname: $as failed to assemble $sfile to $ofile" 1>&2
    #    exit 1;
    #  fi
    #fi
    objs="$objs $ofile"
    tmp_objs="$tmp_objs $ofile"

    if [ $dorun ]; then
      if [ $delete_temp ]; then
        rm -f $sfile
      fi
    fi
  fi

done


if [ "X$link" = "X" ]; then
  exit 0
fi

if [ "X$efile" = "X" ]; then
  echo "$progname: No output executable file name found (and no -oname)" 1>&2
  exit 1
fi

if [ $dorun ]; then
  rm -f $efile
fi

cmd="$cc $ccopts $objs $oclibpath $cincludepath $ocllibs $ocrts $ocrtslibs -o $efile"
if [ $verbose ]; then
  echo $cmd  1>&2
fi
if [ $dorun ]; then
  $cmd
  if [ $? -ne 0 ]; then
    echo "$progname: $cc failed to link $efile" 1>&2
    exit 1;
  fi
fi
if [ $delete_temp ]; then
  if [ "X$tmp_objs" != "X" ]; then
    rm -f $tmp_objs
  fi
fi

exit 0
