#
# Nintendo DS Makefile
#

.PHONY: package clean

ifeq ($(strip $(DEVKITPRO)),)
$(error "DEVKITPRO must be set in your environment.")
endif

ifeq ($(strip $(DEVKITARM)),)
$(error "DEVKITARM must be set in your environment.")
endif

include ${DEVKITARM}/ds_rules

#
# NDS target rules
#
STRIP  = /bin/true

#
# Override library paths.
#

#
# As of devkitARM r51 $(PORTLIBS) has multiple paths...
#
PORTLIBS_INCLUDES := $(foreach dir, $(PORTLIBS), -isystem $(dir)/include)
PORTLIBS_LIBS     := $(foreach dir, $(PORTLIBS), -L$(dir)/lib)

EXTRA_INCLUDES := ${PORTLIBS_INCLUDES} \
                 -isystem arch/nds/ndsScreens/build \
                 -isystem $(DEVKITPRO)/libfat-nds/include \
                 -isystem $(LIBNDS)/include

EXTRA_LIBS := ${PORTLIBS_LIBS} \
             -Larch/nds/ndsScreens/lib \
             -L$(DEVKITPRO)/libfat-nds/lib \
             -L$(LIBNDS)/lib -lfat -lm -lnds9

ARCH_CFLAGS   += ${EXTRA_INCLUDES} -DARM9 -D__NDS__ -Iarch/nds
ARCH_CXXFLAGS += ${ARCH_CFLAGS}
ARCH_LDFLAGS  += ${EXTRA_LIBS} -specs=ds_arm9.specs

#
# Vile hack, remove me ASAP
#
arch/nds/%.o: arch/nds/%.c
	$(if ${V},,@echo "  CC      " $<)
	${CC} -MD ${core_cflags} ${core_flags} -Wno-unused-macros -c $< -o $@

package: mzx
	${MAKE} -C arch/nds TARGET=${mzxrun}
	ndstool -c ${mzxrun}.nds -7 arch/nds/${mzxrun}.arm7.elf -9 ${mzxrun} -b arch/nds/icon.bmp "MegaZeux ${VERSION}"

clean:
	@${MAKE} -C arch/nds TARGET=${mzxrun} clean
	@rm -f ${mzxrun}.nds arch/nds/*.{d,o}

#
# We're only interested in our packaged binary; remove the ELF intermediaries
#
build := build/${SUBPLATFORM}/games/megazeux
build: package ${build}
	${CP} arch/nds/pad.config ${build}
	${CP} ${mzxrun}.nds ${build}
	${RM} ${build}/${mzxrun} ${build}/${mzxrun}.debug

include arch/zip.inc
