# Makefile for Xinu builds on SAPC (standalone PCs) or unix
# Created by eoneil, modified by mam to add support for unix builds
# modified for hw4/proj: localize initialize from Xinu sources
# and compile it with -traditional (for pre-ANSI C)
# For hw4/proj: copy $xuker/initialize.c to your directory first. This
# Makefile builds using the local initialize instead of the Xinu one.
#
# Xinu builds use  cross-compilations  and depend on the ulab module
# Put "module add ulab" in your .cshrc if it's not already there
#
# To build for xinu
#	make U=yourprog yourprog.lnx	 (or just "make U=yourprog")
# To build for unix
#	make U=yourprog yourprog

# hw4/proj: make U=timeout timeout.lnx for example
#
# Defines for building under XINU 
# Startadddress for SAPC--code starts 0x20 after this, at 0x100100
A0=1000e0

# Root of Xinu-pentium distribution, from env var set up by ulab module
# --make sure you have "module load ulab" in your .cshrc
XINU    =      $(xinu)
# library dir for Xinu--
XLIB	=	$(XINU)/lib
# include dir for Xinu--
XINC 	=	$(XINU)/include
# Use Gnu cross-development tools--SAPC_GNUBIN env var is set up by ulab module
XCC	=	$(SAPC_GNUBIN)/i386-gcc
# warning flags that help in code development
# Note: Xinu sources are not ANSI C, so don't try to compile them
# with these flags on!  Luckily, we can call from ANSI to non-ANSI safely
# hw4/proj: see special rule for initialize.opc below
WARNFLAGS =-Wall -Wstrict-prototypes -Wmissing-prototypes \
		-Wno-uninitialized -Wshadow -ansi
XCFLAGS	=	-c -g -I$(XINC) $(WARNFLAGS)

XAS	=	$(SAPC_GNUBIN)/i386-as
XLD	=	$(SAPC_GNUBIN)/i386-ld
XNM	=	$(SAPC_GNUBIN)/i386-nm

# XOBJ--object files unconditionally loaded for Xinu.  The
# rest of the object files reside in libx.a, the Xinu library,
# and are only loaded into the executable if needed.
# hw4/proj: edited to use the initialize.opc in the local directory
# LP proj: dropped lpstubs.opc from this list
XOBJ = $(XLIB)/startup0.opc $(XLIB)/xinu_startup.opc \
	$(XLIB)/support.opc initialize.opc $(XLIB)/intr.opc \
	$(XLIB)/clkint.opc $(XLIB)/ctxsw.opc $(XLIB)/cpureg.opc \
	$(XLIB)/comint.opc $(XLIB)/kbmint.opc $(XLIB)/nullnet.opc

# Defines for building under UNIX
CC	=	gcc
CFLAGS	= 	-g $(WARNFLAGS)

# Default rules for building object files--add to built in ones
.SUFFIXES :
.SUFFIXES :	.c .s .opc .o
.s.opc:
		$(XAS) -o $*.opc $*.s

.c.opc:
		$(XCC) $(XCFLAGS) -o $*.opc $*.c

# Xinu executable
# Make executable file for downloading from host to target machine
# repeated library loads are necessary in some cases because syscalls in
# libx.a use the C library, in libxc.a, and C library uses syscalls.
# also make syms file for Tutor debugging--
# hw4/proj: copy $xuker/initialize.c to your directory first
$(U).lnx:	$(U).opc lp.opc \
				$(XLIB)/libxc.a $(XLIB)/libx.a $(XOBJ) 
		$(XLD) -N -Ttext $(A0) -o $(U).lnx \
		$(XOBJ)	$(U).opc lp.opc ${pclibsrc}/irq7.opc \
		$(XLIB)/libxc.a $(XLIB)/libx.a $(XLIB)/libxc.a
		$(XNM) -n $(U).lnx > syms

$(U).opc:	$(U).c 

# rules added for hw4: use -traditional for old Xinu kernel sources only
# (write in ANSI C for all .c files you create for this assignment) 
# for hardware headers, make $pcinc available
lp.opc:	lp.c
	${XCC} -c -g -I${xinu}/src/h -I${pcinc} lp.c -o lp.opc

# hw4: pre-ANSI C needs -traditional, include of kernel.h, proc.h, etc.
# needs the -I flag adding ${xinu}/src/h to the include path
initialize.opc:	initialize.c
	${XCC} -c -g -traditional -I${xinu}/src/h -DVERSION=6 initialize.c -o initialize.opc

# kill: if you want to change kill.c, use this rule and add kill.opc to
# the .lnx rule above (and its dependency list)
kill.opc:	kill.c 
	${XCC} -c -g -traditional -I${xinu}/src/h kill.c -o kill.opc

# UNIX executable (not usable for hw4)

$(U):	$(U).c
	$(CC) $(CFLAGS) $(U).c -o $(U)

clean:
		rm -f *.opc *.lnx syms core *.o
