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

include $(DEVKITARM)/3ds_rules


#---------------------------------------------------------------------------------
# configurable options
#---------------------------------------------------------------------------------
APP_ICON	:= $(CURDIR)/misc/CC_48x48.png
APP_TITLE 	:= ClassiCube
APP_DESCRIPTION := Simple block building sandbox
APP_AUTHOR 	:= UnknownShadow200
TARGET 		:= ClassiCube-3ds


#---------------------------------------------------------------------------------
# compilation input
#---------------------------------------------------------------------------------
DEPSDIR		:= $(CURDIR)/build
CFILES		:= $(wildcard src/*.c)
SFILES		:= $(wildcard src/*.s)
PICAFILES	:= $(wildcard src/*.v.pica)
SMDH_FILE	:= src/ClassiCube.smdh

OFILES_SOURCES := $(CFILES:.c=.o) $(SFILES:.s=.o)
OFILES_SHADERS := $(PICAFILES:.v.pica=.shbin.o)
OFILES 		:= $(OFILES_SHADERS) $(OFILES_SOURCES)

INCLUDE 	:= $(foreach dir,$(CTRULIB),-I$(dir)/include)
LIBPATHS	:= $(foreach dir,$(CTRULIB),-L$(dir)/lib)

_3DSXFLAGS 	+= --smdh=$(SMDH_FILE)


#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH	:=	-march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft

CFLAGS	:=	-g -Wall -O2 -mword-relocations -ffunction-sections \
			$(ARCH) $(INCLUDE) -D__3DS__

ASFLAGS	:=	-g $(ARCH)
LDFLAGS	=	-specs=3dsx.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
LIBS	:= 	-lcitro3d -lctru -lm
LD		:=	$(CC)


#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
default: $(DEPSDIR) $(TARGET).3dsx

$(DEPSDIR):
	mkdir -p $(DEPSDIR)
	mkdir -p $(DEPSDIR)/src

clean:
	@rm $(OFILES) $(TARGET).elf $(TARGET).3dsx $(SMDH_FILE)

$(TARGET).elf	: $(OFILES)

$(TARGET).3dsx	: $(TARGET).elf $(SMDH_FILE)

Graphics_3DS.o	: $(OFILES_SHADERS)


#---------------------------------------------------------------------------------
# rules for assembling GPU shaders
#---------------------------------------------------------------------------------
%.shbin.o : %.v.pica
	$(eval CUR_SH := $<)
	$(eval CURBIN := $(subst .v.pica,.shbin,$<))
	picasso -o $(CURBIN) $(CUR_SH)
	bin2s $(CURBIN) | $(AS) -o $(CURBIN).o