#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------

ifeq ($(strip $(DEVKITARM)),)
$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM)
endif

include $(DEVKITARM)/ds_rules

#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
#---------------------------------------------------------------------------------
TARGET		:=	$(shell basename $(CURDIR))
BUILD		:=	build
SOURCES		:=	source

#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH	:=	-mthumb-interwork

# note: -O2 causes the libfb library to corrupt video memory, so I'm using -O1 for this project to be safe
CFLAGS	:=	-Wall -O1\
 			-mcpu=arm946e-s -mtune=arm946e-s -fomit-frame-pointer\
			-ffast-math -fPIC \
			$(ARCH) $(INCLUDE) -DARM9

CXXFLAGS	:= $(CFLAGS)

# ???WHG For C only, not for C++
CFLAGS += -std=c99

ASFLAGS	:=	-g $(ARCH)
LDFLAGS	=	-specs=ds_arm9.specs -g $(ARCH) -mno-fpu -Wl,-Map,$(notdir $*.map)

#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project
#---------------------------------------------------------------------------------
LIBS	:= -lfb -ldskeyboard -lfat -lnds9 
 
#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------
 
export OUTPUT	:=	$(CURDIR)/$(TARGET)
 
export VPATH	:=	$(foreach dir,$(SOURCES),$(CURDIR)/$(dir))
export DEPSDIR	:=	$(CURDIR)/$(BUILD)

CFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES	:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
BINFILES	:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.bin)))
 
#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#---------------------------------------------------------------------------------
	export LD	:=	$(CC)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
	export LD	:=	$(CXX)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------

export OFILES	:=	$(BINFILES:.bin=.o) \
					$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
 
export INCLUDE	:=	-I$(CURDIR)/source -I$(LIBNDS)/include  \
					-I$(CURDIR)/../../libraries/include  -I$(CURDIR)/../libraries/include \
					-I$(CURDIR)/$(BUILD)
 
export LIBPATHS	:=	-L$(LIBNDS)/lib -L$(CURDIR)/../../libraries/nintendo_ds -L$(CURDIR)/../libraries/nintendo_ds
 
.PHONY: $(BUILD) clean
 
#---------------------------------------------------------------------------------
$(BUILD):
	@[ -d $@ ] || mkdir -p $@
	@make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
 
#---------------------------------------------------------------------------------
clean:
	@echo clean ...
	@rm -fr $(BUILD) *.elf *.nds* *.bin *.gba

deploy_local:
	@cp -u dsdictionary.nds ..
	
package:
	@echo creating dsdictionary_source.zip...
	@rm -rf ../dsdictionary_source
	# Copy dsdictionary
	@mkdir -p ../dsdictionary_source/dsdictionary
	@cp -R * ../dsdictionary_source/dsdictionary
	# Copy fb
	@mkdir -p ../dsdictionary_source/fb
	@cp -R ../../graphics/fb/* ../dsdictionary_source/fb
	# Copy dskeyboard
	@mkdir -p ../dsdictionary_source/dskeyboard
	@cp -R ../../graphics/dskeyboard/* ../dsdictionary_source/dskeyboard
	# Copy Makefile.package to Makefile
	@cp Makefile.package ../dsdictionary_source/Makefile
	@rm ../dsdictionary_source/dsdictionary/Makefile.package
	# Delete .svn files
	@rm -rf `find ../dsdictionary_source/ | grep .svn`
	# Delete the dictionary, since we don't want that in the source distribution
	@rm -f ../dsdictionary_source/dsdictionary/GCIDE.dsdict
	# Make the zip file
	@rm -rf ../dsdictionary_source.zip
	@cd ../dsdictionary_source; "c:\util\zip" -r "..\dsdictionary_source.zip" "*"
  
#---------------------------------------------------------------------------------
else
 
DEPENDS	:=	$(OFILES:.o=.d)
 
#-----------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).ds.gba	: 	$(OUTPUT).nds
$(OUTPUT).nds	: 	$(OUTPUT).bin
$(OUTPUT).bin	:	$(OUTPUT).elf
$(OUTPUT).elf	:	$(OFILES)

%.elf:
	@echo linking $(notdir $@)
	$(LD)  $(LDFLAGS) $(OFILES) $(LIBPATHS) $(LIBS) -o $@

%.nds: %.arm9
	@ndstool -c $@ -9 $< -g "MDC " MD DSDictionary 1
	@echo built ... $(notdir $@)
 
#---------------------------------------------------------------------------------
%.o	:	%.bin
#---------------------------------------------------------------------------------
	@echo $(notdir $<)
	$(bin2o)
 
 
-include $(DEPENDS)
 
#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------
