#
# Makefile for compiling programs to run on brickOS
# Builds several programs, each consisting of one file.
# List the programs below, substituting .lx for .c or .C/.cxx/.cpp
#
# run "make" to build the .lx files. To download the programs to the
# RCX, define variables progname_pnum=program_num, where progname is the
# name of the .c file without the .c, and program_num is an integer
# telling which program slot you want to install it in (0-7). Then run
# "make install" to install all programs, or 
# "make progname.inst" to just install that program.
# See below for examples
#


# programs go here.
PROGRAMS= helloworld.lx 

# program slots go here.
helloworld_pnum=0

#extra dynamic sources
DOBJECTS= 


#took out math library (log,exp)
# If want to have math functions (math.h) need to inlcude library.
LIBS=		-lc -lmint -lfloat -lc++ 
#LIBS=		-lc -lmint -lm -lfloat -lc++



#
# First need to find OSTYPE
#
ifndef OSTYPE
	OSTYPE=$(shell uname)
endif

#
#  Debian GNU/Linux
#
ifneq (,$(findstring $(OSTYPE),linux-gnu Linux))

EXT=
BASEDIR=	/course/cs148
KERNEL=		$(BASEDIR)/lib/brickOS
TOOLPREFIX=	/course/cs148/bin/h8300-hitachi-hms/h8300-hitachi-hms-
BINDIR=		$(BASEDIR)/bin

endif


#
# WindowsNT/Cygnwin, test against several values:
#
ifneq (,$(findstring $(OSTYPE),cygwin32 cygwin CYGWIN_NT-4.0 CYGWIN_NT-5.0 CYGWIN_NT-5.1 WindowsNT> Windows_NT))

EXT=.exe

#if on windows machine IN the CS department (@CIT)
#BASEDIR= 	/cygdrive/l
#KERNEL=		$(BASEDIR)/winlib/brickOS
#TOOLPREFIX=	$(BASEDIR)/winbin/h8300-hitachi-hms/h8300-hms-
#BINDIR=		$(BASEDIR)/winbin


#If on windows machine not in CS department (@HOME)
BASEDIR=		/cygdrive/c/brickOS
KERNEL=		$(BASEDIR)/boot/brickOS
TOOLPREFIX=	/usr/local/bin/h8300-hms-
BINDIR=		$(BASEDIR)/util


endif



#######################################################
#
# You shouldn't need to adjust anything below here
#
#######################################################


# options      
COPT=		-O2 -fno-builtin -fomit-frame-pointer
CWARN=		-Wall
CINC=		-I. -I$(BASEDIR)/include
CFLAGS=		$(COPT) $(CWARN) $(CINC)
CXXFLAGS=	-DCXX $(CFLAGS) 

MAKELX=		$(BINDIR)/makelx$(EXT)
DLL=		$(BINDIR)/dll$(EXT)
FIRMDL3=	$(BINDIR)/firmdl3$(EXT)

CC=		$(TOOLPREFIX)gcc$(EXT)
CXX=	$(TOOLPREFIX)g++$(EXT)
LD=		$(TOOLPREFIX)ld$(EXT)
AS=		$(TOOLPREFIX)as$(EXT)

MAKEDEPEND=	$(CC) -M $(CINC)

# linker command files dynamic executables.
DYNAMIC_LDS=	$(KERNEL).lds
DLDFLAGS=	-T $(DYNAMIC_LDS) -relax -L$(BASEDIR)/lib


# base addresses to determine relocation info.
BASE1=		0xb000
BASE2=		0xb210

CWD=		`pwd`

#
# make targets:
#

all: depend $(PROGRAMS)

brick:
	$(FIRMDL3) $(KERNEL).srec

install: $(PROGRAMS:.lx=.inst)

depend:
	$(MAKEDEPEND) *.c > .depend

.depend:
	$(MAKEDEPEND) *.c > .depend

tag:
	ctags --format=1 *.c include/*.h include/*/*.h *.c

clean:
	rm -f .depend *.o *~ *.bak tags *.ds1 *.ds2 *.s *.lx

#
# generic rules
#

# how to compile C source
%.o: %.c
	$(CC) $(CFLAGS) -c $<

# how to compile C++ source
%.o: %.cpp
	$(CXX) $(CXXFLAGS) -c $<
%.o: %.C
	$(CXX) $(CXXFLAGS) -c $<

# how to generate assembly listing
%.s: %.c
	$(CC) $(CFLAGS) -S $<

# how to assemble
%.o: %.s
	$(AS) $< 

# how to make srec files of dynamic executables
%.ds1: %.o $(DOBJECTS) $(DYNAMIC_LDS)
	$(LD) $(DLDFLAGS) $*.o $(DOBJECTS) $(LIBS) -o $*.ds1 -Ttext $(BASE1)

# how to make srec files of dynamic executables
%.ds2: %.o $(DOBJECTS) $(DYNAMIC_LDS)	
	$(LD) $(DLDFLAGS) $*.o $(DOBJECTS) $(LIBS) -o $*.ds2 -Ttext $(BASE2)

# how to produce the final .lx
%.lx: %.ds1 %.ds2
	$(MAKELX) $*.ds1 $*.ds2 $*.lx

# how to install the programs on the RCX
%.inst: %.lx
	$(DLL) -p$($*_pnum) $(PWD)/$<

.INTERMEDIATE: *.o

