TARGET		    := retroarch_wiiu_salamander
BUILD_HBL_ELF	     = 1
BUILD_RPX	     = 1
DEBUG                = 0

PC_DEVELOPMENT_IP_ADDRESS  =
PC_DEVELOPMENT_TCP_PORT	   =

OBJ :=
OBJ += wiiu/system/memory.o
OBJ += wiiu/system/exception_handler.o
OBJ += wiiu/fs/sd_fat_devoptab.o
OBJ += wiiu/fs/fs_utils.o
OBJ += wiiu/hbl.o
OBJ += frontend/frontend_salamander.o
OBJ += frontend/frontend_driver.o
OBJ += frontend/drivers/platform_wiiu.o
OBJ += frontend/drivers/platform_null.o
OBJ += libretro-common/compat/compat_strcasestr.o
OBJ += libretro-common/file/file_path.o
OBJ += libretro-common/string/stdstring.o
OBJ += libretro-common/lists/string_list.o
OBJ += libretro-common/lists/dir_list.o
OBJ += libretro-common/file/retro_dirent.o
OBJ += libretro-common/compat/compat_strl.o
OBJ += libretro-common/file/config_file.o
OBJ += libretro-common/streams/file_stream.o
OBJ += libretro-common/file/retro_stat.o
OBJ += libretro-common/hash/rhash.o
OBJ += file_path_str.o
OBJ += verbosity.o

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

export PATH	  := $(PATH):$(DEVKITPPC)/bin

PREFIX := powerpc-eabi-

CC      := $(PREFIX)gcc
CXX     := $(PREFIX)g++
AS      := $(PREFIX)as
AR      := $(PREFIX)ar
OBJCOPY := $(PREFIX)objcopy
STRIP   := $(PREFIX)strip
NM      := $(PREFIX)nm
LD      := $(CXX)

ELF2RPL   := wiiu/wut/elf2rpl/elf2rpl

ifneq ($(findstring Linux,$(shell uname -a)),)
else ifneq ($(findstring Darwin,$(shell uname -a)),)
else
   ELF2RPL   := $(ELF2RPL).exe
endif


INCDIRS := -I. -Ideps/libz -Ideps/7zip -Ilibretro-common/include -Iwiiu -Iwiiu/include -I$(DEVKITPRO)/portlibs/ppc/include
LIBDIRS := -L. -L$(DEVKITPRO)/portlibs/ppc/lib

CFLAGS  := -mwup -mcpu=750 -meabi -mhard-float
LDFLAGS :=

ifeq ($(DEBUG), 1)
   CFLAGS += -O0 -g
else
   CFLAGS += -O3
endif
LDFLAGS := $(CFLAGS)

ASFLAGS := $(CFLAGS) -mregnames

CFLAGS +=  -ffast-math -Werror=implicit-function-declaration
#CFLAGS += -fomit-frame-pointer -mword-relocations
#CFLAGS	+= -Wall

#todo: remove -DWIIU and use the built-in macros instead (HW_WUP or __wiiu__).
CFLAGS += -DWIIU -DMSB_FIRST
CFLAGS += -DHAVE_MAIN
CFLAGS += -DRARCH_CONSOLE -DIS_SALAMANDER

ifneq ($(PC_DEVELOPMENT_IP_ADDRESS),)
   CFLAGS += -DPC_DEVELOPMENT_IP_ADDRESS='"$(PC_DEVELOPMENT_IP_ADDRESS)"'
endif

ifneq ($(PC_DEVELOPMENT_TCP_PORT),)
   CFLAGS += -DPC_DEVELOPMENT_TCP_PORT=$(PC_DEVELOPMENT_TCP_PORT)
endif

ifeq ($(WHOLE_ARCHIVE_LINK), 1)
   WHOLE_START := -Wl,--whole-archive
   WHOLE_END := -Wl,--no-whole-archive
endif
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions

LDFLAGS  += -Wl,--gc-sections

LIBS	:= $(WHOLE_START) -lretro_wiiu $(WHOLE_END) -lm -lfat -liosuhax


RPX_OBJ           = wiiu/system/stubs_rpl.o
HBL_ELF_OBJ       = wiiu/system/dynamic.o wiiu/system/stubs_elf.o

RPX_LDFLAGS      := -pie -fPIE
RPX_LDFLAGS      += -z common-page-size=64 -z max-page-size=64
RPX_LDFLAGS      += -T wiiu/link_rpl.ld
RPX_LDFLAGS      += -nostartfiles

HBL_ELF_LDFLAGS  := -T wiiu/link_elf.ld

TARGETS :=
ifeq ($(BUILD_RPX), 1)
TARGETS += $(TARGET).rpx
endif

ifeq ($(BUILD_HBL_ELF), 1)
TARGETS += $(TARGET).elf
endif

DEPFLAGS    = -MT $@ -MMD -MP -MF $*.Tdepend
POSTCOMPILE = mv -f $*.Tdepend $*.depend


all: $(TARGETS)

%.o: %.cpp
%.o: %.cpp %.depend
	$(CXX) -c -o $@ $< $(CXXFLAGS) $(INCDIRS) $(DEPFLAGS)
	$(POSTCOMPILE)

%.o: %.c
%.o: %.c %.depend
	$(CC) -c -o $@ $< $(CFLAGS) $(INCDIRS) $(DEPFLAGS)
	$(POSTCOMPILE)


%.o: %.S
%.o: %.S %.depend
	$(CC) -c -o $@ $< $(ASFLAGS) $(INCDIRS) $(DEPFLAGS)
	$(POSTCOMPILE)

%.o: %.s
%.o: %.s %.depend
	$(CC) -c -o $@ $< $(ASFLAGS) $(INCDIRS) $(DEPFLAGS)
	$(POSTCOMPILE)
%.a:
	$(AR) -rc $@ $^

%.depend: ;


$(ELF2RPL):
	$(MAKE) -C wiiu/wut/elf2rpl/

$(TARGET).elf: $(OBJ) $(HBL_ELF_OBJ) libretro_wiiu.a wiiu/link_elf.ld
	$(LD) $(OBJ) $(HBL_ELF_OBJ) $(LDFLAGS) $(HBL_ELF_LDFLAGS) $(LIBDIRS) $(LIBS) -o $@

$(TARGET).rpx.elf: $(OBJ) $(RPX_OBJ) libretro_wiiu.a wiiu/link_elf.ld
	$(LD) $(OBJ) $(RPX_OBJ) $(LDFLAGS) $(RPX_LDFLAGS) $(LIBDIRS)  $(LIBS) -o $@

$(TARGET).rpx: $(TARGET).rpx.elf $(ELF2RPL)
	-$(ELF2RPL) $(TARGET).rpx.elf $@

clean:
	rm -f $(OBJ) $(RPX_OBJ) $(HBL_ELF_OBJ) $(TARGET).elf $(TARGET).rpx.elf $(TARGET).rpx
	rm -f $(OBJ:.o=.depend) $(RPX_OBJ:.o=.depend) $(HBL_ELF_OBJ:.o=.depend)

.PHONY: clean all
.PRECIOUS: %.depend

-include $(OBJ:.o=.depend) $(RPX_OBJ:.o=.depend) $(HBL_ELF_OBJ:.o=.depend)

