#
# Main makefile
#

CROSS		= powerpc-eabi-

export CC		:=	$(CROSS)gcc
export CXX		:=	$(CROSS)g++
export AR		:=	$(CROSS)ar
export OBJCOPY	:=	$(CROSS)objcopy
export AS		:=	$(CROSS)as
export LD		:=	$(CXX)
CFLAGS_LOCAL=
DOLTOOL=doltool
SOURCE=source
INCLUDE=include

# Configuration
CFLAGS =	-Wall $(CFLAGS_LOCAL) -I$(INCLUDE) -I../include \
			-mcpu=750 -meabi -mhard-float \
			-Os -fno-exceptions -nostdinc \
			-Werror-implicit-function-declaration -Wimplicit-int

AFLAGS	=	-Iinclude
LDFLAGS =	-mcpu=750 -meabi -mhard-float -nostdlib

# Files
#DVD_MODS = dvd forward jumptable cache memcpy
DVD_MODS = dvd forward jumptable cache

ifeq ($(MIOS_PLUGIN),yes)
CFLAGS += -DMIOS_PLUGIN
AFLAGS += -DMIOS_PLUGIN
TYPE=mios
ifeq ($(GEKKO_DEBUG),yes)
CFLAGS += -DGEKKO_DEBUG
AFLAGS += -DGEKKO_DEBUG
ifeq ($(GEKKO_CHANNEL),)
CFLAGS += -DGEKKO_CHANNEL=1
GECKO_MODS += usb_port1
else
CFLAGS += -DGEKKO_CHANNEL=$(GEKKO_CHANNEL)
ifeq ($(GEKKO_CHANNEL),0)
GECKO_MODS += usb_port$(GEKKO_CHANNEL)
endif
endif
GECKO_MODS += debugprintf snprintf strnlen stream
EXCEPTION_MODS = exception
endif
RELOAD_MODS = dvdreadpatch writebranch codecmp
else
ifeq ($(HIGH_PLUGIN),yes)
ifeq ($(GEKKO_DEBUG),yes)
TYPE=debug
else
TYPE=high
endif
CFLAGS += -DHIGH_PLUGIN
AFLAGS += -DHIGH_PLUGIN
CFLAGS += -DACTION_REPLAY
AFLAGS += -DACTION_REPLAY
ifeq ($(DONTTELL),yes)
CFLAGS += -DDONTTELL
DONTTELL_MODS = patchhook
endif
ifeq ($(REPLACE_DVD_CALLBACK),yes)
CFLAGS += -DREPLACE_DVD_CALLBACK
endif
ifeq ($(SHOW_DVD_DETAILS),yes)
CFLAGS += -DSHOW_DVD_DETAILS
endif
ifeq ($(DEBUG_RETAIL),yes)
CFLAGS += -DDEBUG_RETAIL
endif
ifeq ($(SHOW_MEMSETS),yes)
CFLAGS += -DSHOW_MEMSETS
endif
ifeq ($(GEKKO_DEBUG),yes)
CFLAGS += -DGEKKO_DEBUG -Os
AFLAGS += -DGEKKO_DEBUG
ifeq ($(GEKKO_CHANNEL),)
CFLAGS += -DGEKKO_CHANNEL=1
GECKO_MODS += usb_port1
else
CFLAGS += -DGEKKO_CHANNEL=$(GEKKO_CHANNEL)
ifeq ($(GEKKO_CHANNEL),0)
GECKO_MODS += usb_port$(GEKKO_CHANNEL)
endif
endif
GECKO_MODS += debugprintf snprintf strnlen stream
EXCEPTION_MODS = exception
endif
else
TYPE=low
CFLAGS += -DLOW_PLUGIN
AFLAGS += -DLOW_PLUGIN
ifeq ($(GAME_DEBUG),yes)
CFLAGS += -DGEKKO_DEBUG -Os
AFLAGS += -DGEKKO_DEBUG
ifeq ($(GEKKO_CHANNEL),)
CFLAGS += -DGEKKO_CHANNEL=1
GECKO_MODS += usb_port1
else
CFLAGS += -DGEKKO_CHANNEL=$(GEKKO_CHANNEL)
ifeq ($(GEKKO_CHANNEL),0)
GECKO_MODS += usb_port$(GEKKO_CHANNEL)
endif
endif
GECKO_MODS += debugprintf snprintf strnlen stream
EXCEPTION_MODS = exception
endif
endif
DVD_MODS += pluginprotector
ifeq ($(RELOAD_SUPPORT),yes)
CFLAGS += -DRELOAD_SUPPORT -Os
AFLAGS += -DRELOAD_SUPPORT
RELOAD_MODS = dvdreadpatch writebranch codecmp
endif
endif
ifeq ($(REPLACE_DVD_CALLBACK),yes)
CFLAGS += -DREPLACE_DVD_CALLBACK -Os
AFLAGS += -DREPLACE_DVD_CALLBACK
endif
BUILD=build-$(TYPE)

OBJS = $(addsuffix .o,$(addprefix $(BUILD)/,$(DVD_MODS) $(GECKO_MODS) $(RELOAD_MODS) $(DONTTELL_MODS) $(EXCEPTION_MODS) $(ULTRA_MODS))) 

# Build rules
all:
	make MIOS_PLUGIN=no HIGH_PLUGIN=no RELOAD_SUPPORT=yes lowplugin.bin
	install -m 644 lowplugin.bin ../gcbackuplauncher/data/
	make MIOS_PLUGIN=no HIGH_PLUGIN=yes RELOAD_SUPPORT=yes highplugin.bin
	install -m 644 highplugin.bin ../gcbackuplauncher/data/
	make MIOS_PLUGIN=no HIGH_PLUGIN=yes GEKKO_DEBUG=yes RELOAD_SUPPORT=yes debugplugin.bin
#	make MIOS_PLUGIN=no HIGH_PLUGIN=yes GEKKO_DEBUG=yes SHOW_DVD_DETAILS=yes SHOW_MEMSETS=yes REPLACE_DVD_CALLBACK=yes RELOAD_SUPPORT=yes debugplugin.bin
	install -m 644 debugplugin.bin ../gcbackuplauncher/data/

clean:
	rm -rf build-high
	rm -rf build-low
	rm -rf build-debug
	rm -f highplugin.elf highplugin.bin
	rm -f lowplugin.elf lowplugin.bin
	rm -f debugplugin.elf debugplugin.bin
	
realclean:
	find -name '*.o' | xargs -r rm
	find -name '*.a' | xargs -r rm
	find -name '*~' | xargs -r rm
	find -name '*.bin' | xargs -r rm
	find -name '*.dol' | xargs -r rm
	find -name '*.elf' | xargs -r rm
	find -name '*.map' | xargs -r rm
	find -name '*.tmp' | xargs -r rm


$(BUILD)/%.o: $(SOURCE)/%.c
	@mkdir -p $(BUILD)
	$(CC) $(CFLAGS)  -c -o $@ $^

$(BUILD)/%.o: $(SOURCE)/%.cpp
	@mkdir -p $(BUILD)
	$(CXX) $(CFLAGS) -c -o $@ $^

$(BUILD)/%.o: $(SOURCE)/%.s
	@mkdir -p $(BUILD)
	$(CC) $(AFLAGS) -c -o $@ $^

$(BUILD)/%.o: $(SOURCE)/%.S
	@mkdir -p $(BUILD)
	$(CC) $(AFLAGS) -c -o $@ $^

%.bin: %.elf
	$(OBJCOPY) -O binary $< $@

highplugin.elf: $(OBJS)
	$(CC) $(LDFLAGS) -Wl,-n -o $@ -Wl,-Tlinker-high.lds $(OBJS) -lm

lowplugin.elf: $(OBJS)
	$(CC) $(LDFLAGS) -Wl,-n -o $@ -Wl,-Tlinker-low.lds $(OBJS) -lm

debugplugin.elf: $(OBJS)
	$(CC) $(LDFLAGS) -Wl,-n -o $@ -Wl,-Tlinker-debug.lds $(OBJS) -lm

