# -*- mode: Makefile; compile-command: "make ARCH=ARM_ONLY"; -*-

PALMDIR = /usr/local/share/palmdev/sdk-5r3/include

# Which arm suite to use?  Well, the suite in prc-tools-arm is broken
# on debian at the moment: builds code that doesn't work.  But older
# versions, e.g. on current debian stable, are ok.  For current debian
# the workaround is to build your own crosscompilation toolchain,
# which should put arm-elf-gcc in your path.  We'll use that if we
# find it.  Otherwise we fall back to the prc-tools-arm chain.
WHICH = $(shell if which arm-elf-gcc > /dev/null ; then echo elf; else echo palmos; fi)

CC_ARM = arm-$(WHICH)-gcc
OC_ARM= arm-$(WHICH)-objcopy
OD_ARM = arm-$(WHICH)-objdump

OPT = -Os
MINUS_G = -g

ifeq ($(MEMDEBUG),TRUE)
DEBUG = -DMEM_DEBUG -DDEBUG
endif

INCLUDES += \
	-I$(PALMDIR) \
	-I$(PALMDIR)/Libraries \
	-I$(PALMDIR)/Dynamic \
	-I$(PALMDIR)/Core \
	-I$(PALMDIR)/Extensions/ExpansionMgr \
	-I$(PALMDIR)/Core/UI \
	-I$(PALMDIR)/Core/System \
	-I$(PALMDIR)/Core/System/Unix \
	-I$(PALMDIR)/Core/Hardware \
	-I$(PALMDIR)/Extensions/Bluetooth \

ifneq (, $(findstring XWFEATURE_FIVEWAY,$(MYDEFS)))
INCLUDES += -I/usr/local/share/palmdev/Handspring5
INCLUDES += -I/usr/local/share/palmdev/Handspring5/68K
GENDEFS += -DXWFEATURE_FIVEWAY
endif

ifneq (, $(findstring XWFEATURE_BLUETOOTH,$(MYDEFS)))
GENDEFS += -DXWFEATURE_BLUETOOTH
endif

# NOTE!!!! Added the -w flag to supress all warnings since arm-elf-gcc
# does so much bitching about the Palm headers.  Need to fix that, or
# use the arm-palmos-gcc, or something.  But for now, -w....
CCFLAGS = $(OPT) $(MINUS_G) -Wall -Wunused-parameter -D__PALMOS__ -D__palmos__ -Asystem=palmos
ARMCCFS = $(CCFLAGS) -DNATIVE $(DEBUG) \
	-nostartfiles -mshort-load-bytes -nodefaultlibs -ffixed-r9 \
	-mpic-register=r10 -msingle-pic-base -fpic \
	-Wno-multichar \
	$(INCLUDES)

ARMLDFS = -Xlinker --script=./ldscript.arm -lgcc

# ARM-specific
ARM_DEFINES = $(MYDEFS) -DXW_TARGET_PNO -D__LITTLE_ENDIAN -include undef_hack.h

# turn show progress off for now
# ARM_DEFINES += -USHOW_PROGRESS

include ../common/config.mk

OBJS = $(PLATFORM)/pnolet.o \
	$(PLATFORM)/palmmain.o \
	$(PLATFORM)/palmsavg.o \
	$(PLATFORM)/gameutil.o \
	$(PLATFORM)/newgame.o \
	$(PLATFORM)/palmdict.o \
	$(PLATFORM)/palmdraw.o \
	$(PLATFORM)/palmutil.o \
	$(PLATFORM)/dictui.o \
	$(PLATFORM)/dictlist.o \
	$(PLATFORM)/palmir.o \
	$(PLATFORM)/palmip.o \
	$(PLATFORM)/palmbt.o \
	$(PLATFORM)/prefsdlg.o \
	$(PLATFORM)/connsdlg.o \
	$(PLATFORM)/palmdbg.o \
	$(PLATFORM)/pace_gen.o \
	$(PLATFORM)/pace_man.o \
	$(COMMONOBJ)

# The target of this makefile is the .bin files that comprise the
# pnolet.  There will be more than just PNOC0000.bin, but the others
# get made as a matter of course and as long as they all get included
# in the .prc we're fine.

all: Pnoc0000.bin Pnog0000.bin

$(PLATFORM)/%.o : %.c $(MAKEFILE)
	mkdir -p $(PLATFORM)
	$(CC_ARM) -o $@ -c $(ARM_DEFINES) -DHERE $(ARMCCFS) $<

$(COMMONOBJDIR)/%.o : $(COMMONDIR)/%.c $(MAKEFILE)
	mkdir -p $(COMMONOBJDIR)
	$(CC_ARM) -o $@ -c $(ARM_DEFINES) -DHERE $(ARMCCFS) $<

Pnoc_bin.pre Pnog0000.bin: pace_gen.h $(OBJS)
	$(CC_ARM) $(OBJS) $(ARMCCFS) $(ARMLDFS) -Wl,-Map,Pnoc_bin.map -o tmp.out
#	@$(OD_ARM) --disassemble-all tmp.out
	$(OC_ARM) -j .text -O binary tmp.out Pnoc_bin.pre
	$(OC_ARM) -j .got -O binary tmp.out Pnog0000.bin
#	@rm -f tmp.out 

# create not only Pnoc0000.bin but any others required to fit the
# pnolet into 50K chunks (for hotsyncing...)
Pnoc0000.bin: Pnoc_bin.pre
	rm -f Pnoc????.bin
	split -a 4 -b 50k -d $< Pnoc
	for f in $$(ls Pnoc????); do mv $$f $$f.bin; done

pace_gen.c pace_gen.h: ./gen_pace.pl funcfile.txt
	rm -f $@ _dirList
	./gen_pace.pl -func funcfile.txt \
		-file $(PALMDIR)/Core/UI \
		-file $(PALMDIR)/Core/System \
		-file $(PALMDIR)/Extensions/ExpansionMgr \
		-file $(PALMDIR)/Extensions/Bluetooth \
		-file /usr/local/share/palmdev/Handspring5/68K/System \
		-oc pace_gen.c -oh pace_gen.h $(GENDEFS)
	rm -f _dirList

clean:
	rm -rf $(OBJS) pace_gen.c pace_gen.h *.bin Pnoc_bin.pre