
export DEVKITARM := 1
#export USELIBGBA := 1
include ../gbalib/master.mk

# un-comment out the following for various debugging features
FEATURES:=

# # use malloc instead of sbss section. sbss preferred, dka r11 up supports it.
#CFLAGS += -DUSE_MALLOC
# # inventory screen replaced by a list of ships, types and positions (for debugging tactics + scanner)
#CFLAGS-flying.c := -DDEBUG_SCANNER
# # gprof calls are included 
#CFLAGS += -DENABLE_PROFILING -pg

# # BBC compass is a bit buggy - jumps around when close to station
# # BBC scanner is not really suitable for a smaller screen - GBA scanner has zoom
#CFLAGS-scanner.c := -DHAVE_BBC_SCANNER -DHAVE_BBC_COMPASS

# # KEY_SELECT swaps between n-poly and old triangle renderer on title screena.  
# # n-poly preferred, it's faster with fewer tearing, this was mainly for testing
# FEATURES += -DALLOW_RENDER_CHANGE

# # The GBA random ship allocation has more variety than the BBC equivalent
FEATURES += -DHAVE_GBA_SHIPSETS

# use the bios divide , use the c64 rating scale, 
# use vanishing points (ship details that are not drawn above a certain distance)
# bios divide define is no longer needed, use regular / and % and it pulls in the swi routine
FEATURES +=   -DHAVE_C64_KILLS -DUSE_VANISHING_POINTS
#-DUSE_BIOS_DIVIDE 
.SUFFIX : .s .c .S

PROGNAME = eliteagb
PROJECT = $(PROGNAME).gba
# get the palette directories..
IMGDIR := images
export IMGDIRS := $(wildcard $(CURDIR)/$(IMGDIR)/palette*)
PALLIBS :=  $(foreach file,$(IMGDIRS),-L$(file) -l$(notdir $(file)) )
LIBDEPENDS := $(foreach file,$(IMGDIRS),$(file)/lib$(notdir $(file)).a )


SND = ./snd
OUTPUTDIR = obj
ifdef DEVKITARM
LDFLAGS = -Wl,-Map,$(PROGNAME).map -mthumb-interwork -specs=gba.specs
else
LDFLAGS = -Wl,-Map,$(PROGNAME).map -mthumb-interwork -nostartfiles -Tlnkscript 
endif
#LIBS = -L../gbalib -lquirkygba -Limages -limg
ifdef USELIBGBA
LIBS = $(PALLIBS) -L../libgba/lib -lgba  -L../libsxmp -lsxmp  -Lmodules -lmodule
INCLUDES = -I../libgba -Iincludes -I../libsxmp -Imodules
else
LIBS = $(PALLIBS) -L../gbalib -lquirkygba  -L../libsxmp -lsxmp  -Lmodules -lmodule
INCLUDES = -I../gbalib -Iincludes -I../libsxmp -Imodules
endif

#-Ldata -ldata

ifdef test
  DEBUG=-g -DDEBUG
else
  DEBUG=
endif
ifdef DEVKITARM
  DEBUG += -DDevkitARM
endif

CFLAGS +=    -g -O2

#-I../krawall

ASFLAGS= -mthumb-interwork -g

ifdef usezip
ZIP = zip
ZIPFLAGS =  -rq
ZIPSUFFIX = zip
else
ifdef usebzip 
ZIPFLAGS =  cjf
ZIPSUFFIX = tar.bz2
endif
endif

CFILES := $(wildcard *.c)
ifndef DEVKITARM
NONDEVKITARM_ASM := crt0.S 
else
NONDEVKITARM_ASM := 
endif
ASMFILES := $(NONDEVKITARM_ASM) video.S vw_text_fn.S triangle.S clear.S churn.S detex.S
 

OFILES1 =   $(ASMFILES:%.S=%.o)  $(CFILES:%.c=%.o)

OFILES := $(OFILES1:%.o=$(OUTPUTDIR)/%.o) 
DEPENDS := $(CFILES:%.c=$(OUTPUTDIR)/%.d)

#SH := sh
.PHONY : clean cleanall releasedir dist distsrc

all: $(DEPENDS)
	@$(SH) ./images/makeimages.sh
	$(MAKE) -C modules all
	@echo "Compile with features: $(FEATURES)"
	$(MAKE) $(PROJECT)

clean: 
	-rm -f $(OUTPUTDIR)/*.o

allimages:
	@$(SH) ./images/makeimages.sh


relink:
	@-rm -f $(PROGNAME).elf
	@$(MAKE) all

cleanall: clean
	rm -f  $(OUTPUTDIR)/*.d
	$(MAKE) -C modules clean
	@$(SH) ./images/makeimages.sh clean

RELEASEDIR := $(PROGNAME)-

releasedir :
# create a file that contains, eg. pocketbeeb-0.68
	@echo -n \
	$(RELEASEDIR)`grep version_str version.c | tr '"' ' ' | cut -d ' ' -f7` \
	> .fname
# remove the dist directory, remake it, then copy all the stuff in there 
	@-rm -rf `cat .fname`
	@-mkdir `cat .fname`

releasedirsrc: releasedir
	@for i in `find . -mindepth 1 -type d -not -name "$(RELEASEDIR)*" -not -regex '.*/.svn/?.*' `; do \
	mkdir `cat .fname`/$${i};\
	done

# make the distribution tar files 
dist: releasedir
	@echo "Building the distribution.."
	@cp $(PROJECT)  `cat .fname`
	@cp EliteAGB.pdf  `cat .fname`
	@$(ZIP) $(ZIPFLAGS) `cat .fname`.$(ZIPSUFFIX) `cat .fname`
	@echo "... done for" `cat .fname` "!" 
	@-rm -rf `cat .fname` .fname

DISTSRC_FILES=$(shell find "." -newer morning -type f -and \( -iname "*.[chs]" -or -iname "*.pcx" \
-or -name "Makefile" -or -name "*.asm" -or -name "mkimg*" -or -name "newest.sh" \
-or -name "*.txt" \) -not -regex '.*/.svn/.*')
distsrc: releasedirsrc
	@for i in `find "." -type f -and \( -iname "*.[chs]" -or -iname "*.pcx" \
-or -iname "make*" -or -name "*.xm" -or -name "mkimg*" -or -name "newest.sh" \
-or -name "*.txt" \) -not -name module_data.s -not -regex '.*/.svn/?.*' ` ; do \
	cp $${i} `cat .fname`/$${i};\
	done;
	@echo "... done for" `cat .fname` "!" 
	@$(ZIP) $(ZIPFLAGS) `cat .fname`.src.$(ZIPSUFFIX) `cat .fname`
	@-rm -rf `cat .fname` .fname
	

# final step, create bin file from elf file
$(PROJECT) : $(PROGNAME).elf
	@$(OBJCOPY) -v -O binary $(PROGNAME).elf $(PROGNAME).tofix.gba
	@$(GBAFIX) $(GBAFIX_FLAGS) -n "Elite-AGB" -o $@ $(PROGNAME).tofix.gba
	@-rm $(PROGNAME).tofix.gba

# link all the object files into an elf file 
$(PROGNAME).elf : $(OFILES) $(LIBDEPENDS)
	@echo link $(PROGNAME)
	@$(GCC) $(LDFLAGS) -o $(PROGNAME).elf $(OFILES) $(LIBS)


# make object files from the source code
$(OUTPUTDIR)/%.arm.o : %.arm.c
	@echo compile arm $(CFLAGS) $(DEBUG) $<
	@$(GCC) $(INCLUDES) $(CFLAGS) $(FEATURES) -marm -mthumb-interwork $(DEBUG)  $< -o $@

# make object files from the source code
$(OUTPUTDIR)/%.o : %.c
	@echo compile thumb $(CFLAGS) $(DEBUG) $< 
	@$(GCC) $(INCLUDES) $(CFLAGS) $(FEATURES) $(CFLAGS-$<) -mthumb $(DEBUG)  $< -o $@

# make object files from binary data
$(OUTPUTDIR)/%.all.rodata.o : %.bin
	@echo objcopy rodata bin $<
	@$(OBJCOPY) $(OBJCOPYFLAGS) $< $@

$(OUTPUTDIR)/%.d:%.c
	@echo "build $@ from $<"
	@echo -n $@ $(OUTPUTDIR)/ > $@.tmp
	@set -e;$(GCC) $(INCLUDES) -M $< >> $@.tmp
	@mv $@.tmp $@

#$(OUTPUTDIR)/%.d: %.c
#	@echo "build $@ from $<"
#	set -e; $(GCC) -M $(INCLUDES) $< \
#	| sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' > $@; \
#	[ -s $@ ] || rm -f $@

# make .s object file
$(OUTPUTDIR)/%.o : %.S
	@echo assemble $(ASFLAGS) $(DEBUG) $<
	@$(GCC) $(FEATURES) $(ASFLAGS) -c $(INCLUDES) $(DEBUG) -o $@ $<

$(OUTPUTDIR)/%.o : %.s
	@echo assemble $(ASFLAGS) $(DEBUG) $<
	@$(GCC) $(ASFLAGS) -c $(INCLUDES) $(DEBUG) -o $@ $<

-include $(DEPENDS)
