#
# VDI Makefile
#

ZLIB=../zlib-1.1.4/libz.a


#  Helper targets
all : debug release

debug : 
	@make alltargets CONFIG=DEBUG TGTDIR=Debug

release : 
	@make alltargets CONFIG=RELEASE TGTDIR=Release

#  Configuration
CONFIG?=DEBUG
ifneq ($(CONFIG),RELEASE)
        PILRCDEFINES=-D VFS_SUPPORT
        CFLAGS=-DPALMOS -DNEWGCC -DASMV -DPALMOS_5 -DPALMOS_5_EMU -DVFS_SUPPORT -Wall -Werror -g -O0
        OPT_SPEED=
        OPT_DEF=
        ARM_CFLAGS=-DPALMOS_5 -DARM -nostartfiles -mshort-load-bytes -ffixed-r9 -O3 -Xlinker -T -Xlinker ./ld-arm-script
else
        PILRCDEFINES=-D VFS_SUPPORT
        CFLAGS=-DPALMOS -DNEWGCC -DASMV -DPALMOS_5 -DVFS_SUPPORT  -DERROR_CHECK_LEVEL=1
        OPT_SPEED=-O3
        OPT_DEF=-O2
        ARM_CFLAGS=-DPALMOS_5 -DARM -nostartfiles -mshort-load-bytes -ffixed-r9 -O0 -Xlinker -T -Xlinker ./ld-arm-script
endif

# Targets
alltargets : ./$(TGTDIR)/VDI.prc ../vdivc/prcfile.c

#  Target Clean
clean :
	cd ./Release/ && \
	rm -f *.bin bin.res *~ *.grc *.xxx *.prc *.o *.ld *.s *.d *.hxx VDI.prc VDI VDI_arm
	cd ./Debug/ && \
	rm -f *.bin bin.res *~ *.grc *.xxx *.prc *.o *.ld *.s *.d *.hxx VDI.prc VDI VDI_arm

#  Rule for building sourcefile VDI.c
./$(TGTDIR)/VDI.o : VDI.c VDI.h ./$(TGTDIR)/armlet_info.hxx
	@echo "Compiling VDI.c..." &&\
	m68k-palmos-gcc -palmos4.0 $(CFLAGS) $(OPT_SPEED) -o./$(TGTDIR)/VDI.o -c VDI.c -MD -I ./$(TGTDIR)
	@mv  VDI.d ./$(TGTDIR)/VDI.d

#  Rule for building sourcefile EvtMain.c
./$(TGTDIR)/EvtMain.o : EvtMain.c VDI.h VDI_res.h Main.h
	@echo "Compiling EvtMain.c..." && \
	m68k-palmos-gcc -palmos4.0  $(CFLAGS) $(OPT_DEF) -o./$(TGTDIR)/EvtMain.o -c EvtMain.c -MD
	@mv  EvtMain.d ./$(TGTDIR)/EvtMain.d

#  Rule for building sourcefile Browse.c
./$(TGTDIR)/Browse.o : Browse.c VDI.h VDI_res.h Main.h
	@echo "Compiling Browse.c..." && \
	m68k-palmos-gcc -palmos4.0  $(CFLAGS) $(OPT_DEF) -o./$(TGTDIR)/Browse.o -c Browse.c -MD
	@mv  Browse.d ./$(TGTDIR)/Browse.d

#  Rule for building sourcefile Main.c
./$(TGTDIR)/Main.o : Main.c VDI.h VDI_res.h Main.h
	@echo "Compiling Main.c..." && \
	m68k-palmos-gcc -palmos4.0 $(CFLAGS) $(OPT_DEF) -o./$(TGTDIR)/Main.o -c Main.c -MD
	@mv  Main.d ./$(TGTDIR)/Main.d

#  Rule for building resources
./$(TGTDIR)/*.bin : VDI.rcp VDI_res.h
	@echo "Compiling resource VDI.rcp..." && \
	pilrc ${PILRCDEFINES} -q VDI.rcp ./$(TGTDIR)/

#  Rules for multilink preparations
./$(TGTDIR)/vdi-sections.o : ./$(TGTDIR)/vdi-sections.s
	@echo "Compiling the ASM stubs..." && \
	m68k-palmos-gcc -c -o ./$(TGTDIR)/vdi-sections.o ./$(TGTDIR)/vdi-sections.s

./$(TGTDIR)/vdi-sections.s ./$(TGTDIR)/vdi-sections.ld : ./vdi.def
	@echo "Processing the definition file..." && \
	CURDIR=`pwd` && cd ./$(TGTDIR) && \
        m68k-palmos-multigen ../vdi.def && cd ..

#  Rule for Linking the project
./$(TGTDIR)/VDI : ./$(TGTDIR)/Browse.o ./$(TGTDIR)/EvtMain.o ./$(TGTDIR)/Main.o ./$(TGTDIR)/VDI.o ./$(TGTDIR)/vdi-sections.o ${ZLIB} ./$(TGTDIR)/vdi-sections.ld
	@echo "Linking..." && \
	m68k-palmos-gcc ./$(TGTDIR)/Browse.o ./$(TGTDIR)/EvtMain.o ./$(TGTDIR)/Main.o ./$(TGTDIR)/VDI.o ./$(TGTDIR)/vdi-sections.o ${ZLIB} ./$(TGTDIR)/vdi-sections.ld -lcrt -palmos4.0 -g -o "$@"

#  Rule for building from VDI_arm_intel.c for ARM
./$(TGTDIR)/VDI_arm : VDI_arm_intel.c VDI_arm_intel.h
	@echo "Compiling VDI_arm_intel..." && \
	arm-palmos-gcc -palmos4.0 $(ARM_CFLAGS) -o ./$(TGTDIR)/VDI_arm VDI_arm_intel.c -MD

./$(TGTDIR)/*.xxx : ./$(TGTDIR)/VDI_arm
	@echo "Postprocessing ARM files" && \
	arm-palmos-objcopy -j .text -j .got -j .got.plt -O binary ./$(TGTDIR)/VDI_arm ./$(TGTDIR)/ARMC0001.xxx && \
	arm-palmos-objcopy -j .got -j .got.plt -O binary ./$(TGTDIR)/VDI_arm ./$(TGTDIR)/ARMG0001.xxx
	arm-palmos-objcopy -j .text -O binary ./$(TGTDIR)/VDI_arm ./$(TGTDIR)/ARMT0001.xxx
	arm-palmos-objcopy -j .data -j .bss -O binary ./$(TGTDIR)/VDI_arm ./$(TGTDIR)/ARMD0001.xxx

./$(TGTDIR)/armlet_info.hxx : ./$(TGTDIR)/*.xxx
	@echo "Building armlet_info.hxx..." && \
	echo -n "#define ARM_TEXT_SIZE " > ./$(TGTDIR)/armlet_info.hxx && \
	ls -l ./$(TGTDIR)/ARMT0001.xxx | sed -r -e "s/^[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+ +//" | sed -r -e "s/ .*/\n/" >> ./$(TGTDIR)/armlet_info.hxx &&\
	cat ./$(TGTDIR)/ARMG0001.xxx | perl b2c.pl armGot "__attribute__ ((aligned (4)))" >> ./$(TGTDIR)/armlet_info.hxx &&\
	cat ./$(TGTDIR)/ARMD0001.xxx | perl b2c.pl armData "__attribute__ ((aligned (4)))" >> ./$(TGTDIR)/armlet_info.hxx

#  Rule for building PRCFile
./$(TGTDIR)/VDI.prc : ./vdi.def ./$(TGTDIR)/VDI ./$(TGTDIR)/*.bin ./$(TGTDIR)/*.xxx
	@echo "Building PRC, compilation flags used $(CFLAGS) $(OPT_SPEED)" && \
	cd ./$(TGTDIR)/ && \
	cp -f ARMC0001.xxx ARMC0001.bin && \
	build-prc ../vdi.def VDI *.bin && \
	cp VDI.prc /d/PalmDev/sim/AutoRun/

#  Rule for transferring PRCFile to VC project
../vdivc/prcfile.c : ./$(TGTDIR)/VDI.prc
	@echo "Converting PRC to C text ../vdivc/prcfile.c ..." && \
	cat ./$(TGTDIR)/VDI.prc | perl b2c.pl prcFile > ../vdivc/prcfile.c


.PHONY : release debug
