# Attempt at a generic makefile for small Pilot apps using cross tools.
# targets:
#    *.msend:  build .prc and squirt over to pilot
#    *.prc: build .prc
#    *.clean: delete working files
#    *.realclean: and the prc file too
#    *.tgz: built tarfile of release (prc + sources)
# This makefile is free. Mark Eichin <eichin@thok.org>
# At this writing, it depends on a patch to build-prc to handle subdirs,
# but otherwise works with the Debian installation of prc-tools et. al.
#
CC = m68k-palmos-coff-gcc

#uncomment this if you want to build a gdb debuggable version
#DEFINES = -DDEBUG

CSFLAGS = -O2 -S $(DEFINES)
CFLAGS = -Wall -O2 -g $(DEFINES)

LIBS = -static

PILRC = pilrc
TXT2BITM = txt2bitm
OBJRES = m68k-palmos-coff-obj-res
BUILDPRC = build-prc

%.x: %.o
	$(CC) $(CFLAGS) $< -o $@ $(LIBS)

%.rcp: %.srcp rcp.m4
	m4 rcp.m4 $< > $@

%.res: %.rcp %.pbitm
	if [ ! -d $*_tmp ]; then mkdir $*_tmp ; fi
	$(PILRC) $*.rcp $*_tmp
	(cd $*_tmp; $(TXT2BITM) ../$*.pbitm)
	touch $@

%.prc: code0000.%.x.grc code0001.%.x.grc data0000.%.x.grc pref0000.%.x.grc %.res %.labels
	$(BUILDPRC) $@ "$(shell sed -n "/ICONTEXT/s/ICONTEXT: //p" $*.labels)" $(shell sed -n "/APPID/s/APPID: //p" $*.labels) code0001.$*.x.grc code0000.$*.x.grc data0000.$*.x.grc $*_tmp/*.bin pref0000.$*.x.grc

pref0000.%.x.grc code0000.%.x.grc code0001.%.x.grc data0000.%.x.grc: %.x
	$(OBJRES) $<

.PRECIOUS: %.x %.prc %.o %.res
.PRECIOUS: %.grc 

%.msend:	%.prc
	./pilotsquirt $<

%.clean:
	rm -rf *.[oa] $* $*_tmp/*.bin $*.res *.$*.x.grc $*.x

%.realclean: %.clean
	rm -rf $*.prc

%.tgz: %.prc
	tar czvf $@ $< $*.c $*.srcp $*.labels $*.pbitm rcp.m4 Makefile

.PHONY: %.realclean %.clean %.msend
