# portions Copyright 2002 Sony Corporation 
#
# Permission to use, copy, modify, and redistribute this software for
# non-commercial use is hereby granted.
#
# This software is provided "as is" without warranty of any kind,
# either expressed or implied, including but not limited to the
# implied warranties of fitness for a particular purpose.
#

# generated headers
GEN_HS += $(STUB_BASE).h entry.h def.h

PREFIX=/usr/local/OPEN_R_SDK
INSTALLDIR=../MS
CXX=mipsel-linux-g++
STRIP=$(PREFIX)/bin/mipsel-linux-strip
MKBIN=$(PREFIX)/OPEN_R/bin/mkbin
STUBGEN=$(PREFIX)/OPEN_R/bin/stubgen2
MKBINFLAGS=-p $(PREFIX)
LIBS=-L$(PREFIX)/OPEN_R/lib -lObjectComm -lOPENR -lInternet -lantMCOOP -lERA201D1

CXXFLAGS= \
	-I. \
	-I$(PREFIX)/OPEN_R/include/R4000 \
	-I$(PREFIX)/OPEN_R/include/MCOOP \
	-I$(PREFIX)/OPEN_R/include \
	-DPLATFORM_APERIOS_SDK \

CXXFLAGS+= -Wall
CXXFLAGS+= -g
CXXFLAGS+= -ffast-math -O2 
#  -march=r4000

DEP_PROCESS:=dep_process

#
# When OPENR_DEBUG is defined, OSYSDEBUG() is available.
#
#CXXFLAGS+= -DOPENR_DEBUG

.DELETE_ON_ERROR:

.PHONY: all install clean

all: install

%.mips.o: %.cc
	$(CXX) $(CXXFLAGS) -o $@ -c $<

%.mips.d: %.cc $(GEN_HS)
	$(CXX) -M $(CXXFLAGS) $< >$@.tmp
	$(DEP_PROCESS) $@.tmp $(basename $@).o $(GEN_HS) >$@
	rm $@.tmp

dep: depend	

depend:
	rm -rf $(CC_DEPS)

$(STUB_BASE).cc $(STUB_BASE).h entry.h def.h: stub.cfg
	$(STUBGEN) stub.cfg	

$(TARGET_USER_OBJECT).bin: $(STUB_BASE).mips.o $(CC_OBJS) $(TARGET_USER_OBJECT).ocf
	$(MKBIN) $(MKBINFLAGS) -o $@ $^ $(LIBS)
	$(STRIP) $@

install: $(INSTALLDIR)/OPEN-R/MW/OBJS/$(TARGET_USER_OBJECT_BIN).BIN

$(INSTALLDIR)/OPEN-R/MW/OBJS/$(TARGET_USER_OBJECT_BIN).BIN: $(TARGET_USER_OBJECT).bin $(INSTALLDIR)/OPEN-R/MW/OBJS/
	gzip -c $(TARGET_USER_OBJECT).bin > $(INSTALLDIR)/OPEN-R/MW/OBJS/$(TARGET_USER_OBJECT_BIN).BIN

$(INSTALLDIR)/OPEN-R/MW/OBJS/:
	mkdir -p $@

clean:
	rm -f $(CC_OBJS) *.bin *.elf *.snap.cc
	rm -f $(STUB_BASE).h $(STUB_BASE).cc def.h entry.h
	rm -f $(INSTALLDIR)/OPEN-R/MW/OBJS/$(TARGET_USER_OBJECT_BIN).BIN

include $(CC_DEPS)
