#---------------------------------------------------------------------------------
# Clear the implicit built in rules
#---------------------------------------------------------------------------------
.SUFFIXES:

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

include $(DEVKITPPC)/wii_rules

#---------------------------------------------------------------------------------
# Name of the output
#---------------------------------------------------------------------------------
TARGET				:=	boot

#---------------------------------------------------------------------------------
# Folder where object files & intermediate files will be placed
#---------------------------------------------------------------------------------
BUILD				:=	Build

#---------------------------------------------------------------------------------
# List of folders containing source code
#---------------------------------------------------------------------------------
SOURCES				:=	Source \
						Source/Language \
						Source/Libs \
						Source/Libs/LibFAT \
						Source/Libs/LibWiiGUI \
						Source/Libs/PNGu \
						Source/Menu \
						Source/MLoad \
						Source/MLoad/Modules \
						Source/Prompts \
						Source/Report \
						Source/Settings \
						Source/Text \
						Source/sysCheckGX \
						Source/System \
						Source/System/Sound \
						Source/System/Storage

#---------------------------------------------------------------------------------
# List of folders containing extra header files
#---------------------------------------------------------------------------------
INCLUDES			:=	Source

#---------------------------------------------------------------------------------
# Extra header files
#---------------------------------------------------------------------------------
DATA				:=	Data/Fonts \
						Data/Images \
						Data/Sounds \
						Data/sysCheck
				
#---------------------------------------------------------------------------------
# Options for code generation
#---------------------------------------------------------------------------------
CFLAGS				=	-g -O2 -Wall $(MACHDEP) $(INCLUDE)
CXXFLAGS			=	-save-temps -Xassembler -aln=$@.lst $(CFLAGS)
LDFLAGS				=	-g $(MACHDEP) -Wl,-Map,$(notdir $@).map,--section-start,.init=0x80e00000

#---------------------------------------------------------------------------------
# Extra libraries we wish to link with the project
#---------------------------------------------------------------------------------
LIBS 				:=	-lfat -lpng -lz -lwiiuse -lbte -lasnd -logc -lvorbisidec -lfreetype -lmxml -lwiilight

#---------------------------------------------------------------------------------
# List of folders containing libraries, this must be the top level containing include and lib
#---------------------------------------------------------------------------------
LIBDIRS				:=	$(PORTLIBS)

#---------------------------------------------------------------------------------
# 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 PROJECTDIR 	:=	$(CURDIR)
export OUTPUT		:=	$(CURDIR)/$(TARGETDIR)/$(TARGET)
export VPATH		:=	$(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
						$(foreach dir,$(DATA),$(CURDIR)/$(dir))
export DEPSDIR		:=	$(CURDIR)/$(BUILD)

#---------------------------------------------------------------------------------
# Automatically build a list of object files for our project
#---------------------------------------------------------------------------------
export CFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
export CPPFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
sFILES		    	:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
SFILES		    	:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S)))
TTFFILES	    	:=	$(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.ttf)))
PNGFILES	    	:=	$(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.png)))
OGGFILES	    	:=	$(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.ogg)))
PCMFILES	    	:=	$(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.pcm)))
BINFILES			:=	$(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))

#---------------------------------------------------------------------------------
# Use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
	export LD		:=	$(CC)
else
	export LD		:=	$(CXX)
endif

export OFILES		:=	$(addsuffix .o,$(BINFILES)) \
						$(TTFFILES:.ttf=.ttf.o) $(PNGFILES:.png=.png.o) \
						$(OGGFILES:.ogg=.ogg.o) $(PCMFILES:.pcm=.pcm.o) \
						$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) \
						$(sFILES:.s=.o) $(SFILES:.S=.o)

#---------------------------------------------------------------------------------
# Build a list of include folders
#---------------------------------------------------------------------------------
export INCLUDE		:=	$(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
						$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
						-I$(CURDIR)/$(BUILD) \
						-I$(LIBOGC_INC)

#---------------------------------------------------------------------------------
# Build a list of library folders
#---------------------------------------------------------------------------------
export LIBPATHS		:=	$(foreach dir,$(LIBDIRS),-L$(dir)/lib) \
						-L$(LIBOGC_LIB)

export OUTPUT		:=	$(CURDIR)/$(TARGET)
.PHONY: $(BUILD) clean

#---------------------------------------------------------------------------------
$(BUILD):
	@[ -d $@ ] || mkdir -p $@
	@make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile

#---------------------------------------------------------------------------------
clean:
	@echo Cleaning... Please wait...
	@rm -fr $(BUILD) $(OUTPUT).elf $(OUTPUT).dol

#---------------------------------------------------------------------------------
run:
	@echo Running... Please wait...
	WiiLoad $(OUTPUT).dol

#---------------------------------------------------------------------------------
reload:
	@echo Reloading... Please wait...
	WiiLoad -r $(OUTPUT).dol

#---------------------------------------------------------------------------------
else
	DEPENDS	:=	$(OFILES:.o=.d)

#---------------------------------------------------------------------------------
# Main targets
#---------------------------------------------------------------------------------
$(OUTPUT).dol: $(OUTPUT).elf
$(OUTPUT).elf: $(OFILES)

#---------------------------------------------------------------------------------
# This rule links in binary data with .ttf, .png, .ogg, .pcm and .dat extensions
#---------------------------------------------------------------------------------
%.ttf.o : %.ttf
	@echo $(notdir $<)
	@bin2s -a 32 $< | $(AS) -o $(@)

%.png.o : %.png
	@echo $(notdir $<)
	@bin2s -a 32 $< | $(AS) -o $(@)

%.ogg.o : %.ogg
	@echo $(notdir $<)
	@bin2s -a 32 $< | $(AS) -o $(@)

%.pcm.o : %.pcm
	@echo $(notdir $<)
	@bin2s -a 32 $< | $(AS) -o $(@)

%.dat.o : %.dat
	@echo $(notdir $<)
	$(bin2o)

-include $(DEPENDS)

endif
