#!/bin/sh
# $Id: mcc,v 1.6 1998/06/29 02:23:02 paul Exp $

# This script is used in compiling all smx .c and .s files to .o files, and 
# in linking all user programs except init.  The four programs that are
# combined to form image have custom ld command lines in their make files.
# The script tries to provide standard cc options, but the emulation
# is not 100% accurate.  Some options are special to mcc:
#
#     -N - if linking occurs, do not delete the SunOS executable
#     -v - echo all compile and link commands rather than executing them
#     -S - specifies the size of the space to left at smx run-time for the
#          heap and stack.

# 1997/01/21 Coverted to sh (PJA)

#	-fnocommon is included as without the elf files produced contain
#       sections that smx2elf can't handle.
stdgccopts="-nostdinc -funsigned-char -fno-common -O2"

#	Elf executables are statically linked.  The standard library list
#       (specified by -Y) is $MX_LIB.
stdldopts="-dn -e _crtso -M $MX_LIB/smx_userprog.map -Y P,$MX_LIB"

#	The various EM defines are needed because the ack compiler
#       (which is assumed) defines them
stdcompileopts="-I$MX_INCL -D_EM_WSIZE=4 -D_EM_PSIZE=4 -D_EM_LSIZE=4"
compileopts=

ofile=a.out       # -o overrides this
nolink=0
delete=1
minusc=0
compiledobjs=
srcfiles=
libs=
stksize=4kw      # 16Kb is the default.

runcmd=

# Process everything on the command line, setting the variables listed
# above as needed.
while [ $# != 0 ]
do
    case $1 in
        "-N")
	    delete=0
	    ;;
        "-o")
	    ofile=$2
	    shift
	    ;;
	"-v")
	    runcmd="echo"
	    ;;
	"-c")
	    nolink=1
	    minusc=1
	    ;;
	-l*)
	    libs="$libs $1"
	    ;;
	"-S")
	    stksize=$2
	    shift
	    ;;
	"-E")
	    nolink=1      # Don't try linking with -E
	    compileopts="$compileopts -E"
	    ;;
	-*)
	    compileopts="$compileopts $1"
	    ;;
	*)
	    srcfiles="$srcfiles $1"
	    ;;
    esac
    shift
done

# Process all source files.  Compile .c and .s files.  All other files
# are justed passed through to ld.

if [ $minusc = 1 -a $ofile != "a.out" ]
    then
	compileopts="-o $ofile $compileopts"
fi
objfiles=
for f in $srcfiles
do
    if [ `basename $f .c` != `basename $f` ]
        then          # a .c file
	    echo $f":"
	    objfile=`basename $f .c`.o
	    $runcmd rm -f $objfile
	    $runcmd gcc $stdgccopts $compileopts $stdcompileopts -c $f
	    objfiles="$objfiles $objfile"
 	    compiledobjs="$objfiles $objfile"

	elif [ `basename $f .s` != `basename $f` ] 
	    then      # a .s file
		echo $f":"
		objfile=`basename $f .s`.o
		$runcmd rm -f $objfile
		$runcmd as -P $compileopts $stdcompileopts $f
		objfiles="$objfiles $objfile"
		compiledobjs="$objfiles $objfile"

	else
	    objfiles="$objfiles $f"
    fi
done

if [ $nolink = 1 ]
    then
	exit 0;
fi

if [ "$objfiles" != "" ]
    then
# Linking involves using ld to create an elf executable, then elf2smx
# to create the smx executable.
	echo "Linking:" 
	$runcmd rm -f $ofile
	$runcmd ld $stdldopts \-o $ofile.elf $MX_LIB/crtso.o $objfiles $libs $MX_LIB/libc.a
	$runcmd elf2smx -S $stksize $ofile.elf $ofile
	if [ $delete = 1 ]
            then
		$runcmd rm $ofile.elf
	fi
fi

if [ $minusc = 0 -a "$compiledobjs" != "" ]
    then
	$runcmd rm -f $compiledobjs
fi
