TARGET		    := retroarch_vita
DEBUG               ?= 0
GRIFFIN_BUILD        = 0
WHOLE_ARCHIVE_LINK   = 0

VITA_TITLE_ID	:= RETROARCH
VITA_TITLE_NAME	:= RetroArch

PC_DEVELOPMENT_IP_ADDRESS  =
PC_DEVELOPMENT_UDP_PORT	   =

OBJ :=

DEFINES :=

ifeq ($(GRIFFIN_BUILD), 1)
	OBJ += griffin/griffin.o
	DEFINES += -DHAVE_GRIFFIN=1
	DEFINES += -DHAVE_NEON -DHAVE_MENU -DHAVE_XMB -DHAVE_MATERIALUI -DHAVE_LIBRETRODB
	DEFINES += -DHAVE_ZLIB -DHAVE_RPNG -DHAVE_RJPEG -DHAVE_RBMP -DHAVE_RTGA -DWANT_ZLIB -DHAVE_CC_RESAMPLER
	ifeq ($(DEBUG), 1)
		DEFINES += -DHAVE_NETLOGGER
	endif
else

	HAVE_NEON		:= 1
	HAVE_FILTERS_BUILTIN	:= 1
	HAVE_LANGEXTRA		:= 1
	HAVE_RPNG		:= 1
	HAVE_RJPEG		:= 1
	HAVE_RBMP		:= 1
	HAVE_RTGA		:= 1
	HAVE_ZLIB		:= 1
	HAVE_7ZIP		:= 1
	HAVE_VITA2D		:= 1
	HAVE_NETWORKING		:= 1
	HAVE_SOCKET_LEGACY	:= 1
	HAVE_MENU		:= 1
	HAVE_MENU_COMMON	:= 1
	HAVE_OVERLAY		:= 1
	HAVE_MATERIALUI		:= 1
	HAVE_XMB		:= 1
	HAVE_RGUI		:= 1
	HAVE_STB_FONT		:= 1
	HAVE_THREADS		:= 1
	HAVE_LIBRETRODB		:= 1
	HAVE_CC_RESAMPLER	:= 1
	RARCH_CONSOLE		:= 1

	ifeq ($(DEBUG), 1)
		HAVE_NETLOGGER = 1
	endif

	include Makefile.common
	BLACKLIST :=
	OBJ := $(filter-out $(BLACKLIST),$(OBJ))

	OBJ += input/drivers/psp_input.o
	OBJ += input/drivers_joypad/psp_joypad.o
	OBJ += audio/drivers/psp_audio.o
	OBJ += frontend/drivers/platform_psp.o
	OBJ += gfx/video_filters/2xsai.o
	OBJ += gfx/video_filters/super2xsai.o
	OBJ += gfx/video_filters/supereagle.o
	OBJ += gfx/video_filters/2xbr.o
	OBJ += gfx/video_filters/darken.o
	OBJ += gfx/video_filters/epx.o
	OBJ += gfx/video_filters/scale2x.o
	OBJ += gfx/video_filters/blargg_ntsc_snes.o
	OBJ += gfx/video_filters/lq2x.o
	OBJ += gfx/video_filters/phosphor2x.o
	OBJ += libretro-common/audio/dsp_filters/echo.o
	OBJ += libretro-common/audio/dsp_filters/eq.o
	OBJ += libretro-common/audio/dsp_filters/chorus.o
	OBJ += libretro-common/audio/dsp_filters/iir.o
	OBJ += libretro-common/audio/dsp_filters/panning.o
	OBJ += libretro-common/audio/dsp_filters/phaser.o
	OBJ += libretro-common/audio/dsp_filters/reverb.o
	OBJ += libretro-common/audio/dsp_filters/wahwah.o
endif


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

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

PREFIX := arm-vita-eabi-

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

INCDIRS := -I. -Ideps/libz -Ideps/7zip -Ilibretro-common/include
LIBDIRS := -L.

ARCHFLAGS := -march=armv7-a -mfpu=neon -mfloat-abi=hard -DVITA
CFLAGS    += $(ARCHFLAGS) -mword-relocations -fno-optimize-sibling-calls

ifeq ($(DEBUG), 1)
   CFLAGS += -O2 -g
else
   CFLAGS += -O3
endif

ASFLAGS := $(CFLAGS)
LDFLAGS := -Wl,-q

CFLAGS += -Wall -ffast-math
CFLAGS += -DRARCH_INTERNAL -DRARCH_CONSOLE
CFLAGS += -DHAVE_FILTERS_BUILTIN $(DEFINES)

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

ifneq ($(PC_DEVELOPMENT_UDP_PORT),)
   CFLAGS += -DPC_DEVELOPMENT_UDP_PORT=$(PC_DEVELOPMENT_UDP_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

VITA_LIBS := -lSceDisplay_stub -lSceGxm_stub -lSceNet_stub -lSceNetCtl_stub \
	-lSceSysmodule_stub -lSceCtrl_stub -lSceTouch_stub -lSceAudio_stub \
	-lScePower_stub -lSceRtc_stub -lSceCommonDialog_stub -lScePgf_stub \
	-lSceFiber_stub -lSceMotion_stub -lSceAppMgr_stub -lpthread -lpng -lz

LIBS	:= $(WHOLE_START) -lretro_vita $(WHOLE_END) $(VITA_LIBS) -lm -lc

TARGETS := $(TARGET).vpk

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)

%.depend: ;

$(TARGET).elf: $(OBJ) libretro_vita.a
	$(LD) $(OBJ) $(LDFLAGS) $(LIBDIRS) $(LIBS) -o $@

%.velf: %.elf
	vita-elf-create $< $@

%.self: %.velf
	vita-make-fself -c -s $< $@

%.vpk: %.self
	vita-mksfoex -s TITLE_ID=$(VITA_TITLE_ID) "$(VITA_TITLE_NAME)" param.sfo
	vita-pack-vpk -s param.sfo -b $< $@

clean:
	rm -f $(OBJ) $(TARGET).elf $(TARGET).velf $(TARGET).self param.sfo $(TARGET).vpk
	rm -f $(OBJ:.o=.depend)

# Useful for developers
vpksend: $(TARGET).vpk
	curl -T $< ftp://$(PSVITAIP):1337/ux0:/

send: $(TARGET).self
	curl -T $< ftp://$(PSVITAIP):1337/ux0:/app/$(VITA_TITLE_ID)/eboot.bin

.PHONY: clean all send vpksend
.PRECIOUS: %.depend

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