#!/bin/csh -f
# $Id: mcc,v 1.4 1996/07/09 04:47: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.

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

#	Elf executables are statically linked.  The standard library list
#       (specified by -Y) is $MX_LIB.
set 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
set stdcompileopts = (-I$MX_INCL -D_EM_WSIZE=4 -D_EM_PSIZE=4 -D_EM_LSIZE=4)
set compileopts = ()

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

set runcmd = ""

# Process everything on the command line, setting the variables listed
# above as needed.
while ($#argv != 0)
	switch ($argv[1])
                case -N:
			set delete = 0
			breaksw;

		case -o:
			set ofile = $argv[2]
			shift
			breaksw;

		case -v:
			set runcmd = "echo"
			breaksw;

		case -c:
			set nolink = 1
			set minusc = 1
			breaksw;

		case -l*:
			set libs = ($libs $argv[1])
			breaksw;

		case -S:
			set stksize = $argv[2]
			shift
			breaksw;

		case -E:
			set nolink = 1      # Don't try linking with -E
		case -*:
			set compileopts = ($compileopts $argv[1])
			breaksw;

		default:
			set srcfiles = ($srcfiles $argv[1])
			breaksw;
	endsw	
	shift
end

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

if ($minusc == 1 && $ofile != "a.out") then
	set compileopts = (-o $ofile $compileopts)
endif
set objfiles = ()
foreach f ($srcfiles)
	if ("$f" =~ *.c) then
		echo $f":"
		set objfile = `basename $f .c`.o
		$runcmd rm -f $objfile
		$runcmd gcc $stdgccopts $compileopts $stdcompileopts -c $f
		set objfiles = ($objfiles $objfile)
		set compiledobjs = ($objfiles $objfile)

	else if ($f =~ *.s) then
		echo $f":"
		set objfile = `basename $f .s`.o
		$runcmd rm -f $objfile
		$runcmd as -P $compileopts $stdcompileopts $f
		set objfiles = ($objfiles $objfile)
		set compiledobjs = ($objfiles $objfile)

	else
		set objfiles = ($objfiles $f)
	endif
end

if ($nolink == 1) then
	exit 0;
endif

if ($#objfiles != 0) 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) then
		$runcmd rm $ofile.elf
	endif
endif

if ($minusc == 0 && $#compiledobjs > 0) then
	$runcmd rm -f $compiledobjs
endif
