################################################################################
#
#
#

CC = gcc
LD = gcc

#
################################################################################
#
#
#

ifeq ($(target),)
	target = Windows
endif

ifeq ($(target),Windows)
	exeext = .exe
	dllext = .dll
	objext = .o
	AS = nasm -f gnuwin32
	SHARED = dllwrap --def ../def/plugin.Windows.def
	LFLAGS = -lgdi32
else
	ifeq ($(target),Linux)
		exeext =
		dllext = .so
		objext = .o
	else
		ifeq ($(target),BeOS)
			exeext =
			dllext = .so
			objext = .o
			AS = nasm -f elf
			SHARED = gcc -no-start
		endif
	endif
endif

#
################################################################################
#
#
#

DIR_SRC_INCLUDE = include

ifeq ($(DIR_INSTALL),)
	DIR_INSTALL = ../bin/$(target)
endif

DIR_INSTALL_PLUGINS = $(DIR_INSTALL)/Plugins

#
################################################################################
#
#
#

DEFS = -D$(target)

INCLUDE = -I$(DIR_SRC_INCLUDE)

OPTIMIZE = -O1 -fomit-frame-pointer

WARNING = -Wall

CFLAGS = $(DEFS) $(INCLUDE) $(OPTIMIZE) $(WARNING)

#
################################################################################
#
#
#

LFLAGS += -Wl,-x,-s

#
################################################################################
#
#
#

OBJ_RBPSE		= RbPSe$(objext) interface.$(target)$(objext)

#
#
################################################################################
#
#
#

all:		build

install:	build
	mkdir -p $(DIR_INSTALL)
	cp RbPSe$(exeext) $(DIR_INSTALL)
	mkdir -p $(DIR_INSTALL_PLUGINS)
	cp plugin_*/plugin_*$(dllext) $(DIR_INSTALL_PLUGINS)

build:		rbpse plugins

rbpse:      RbPSe$(exeext)

plugins:
	make -C plugin_cpu AS="$(AS)" CC="$(CC)" SHARED="$(SHARED)" dllext="$(dllext)" objext="$(objext)"
	@make -C plugin_gpu_psemu target=$(target) dllext="$(dllext)" objext="$(objext)"
	@make -C plugin_spu_psemu target=$(target)
	@make -C plugin_boot dllext=$(dllext) objext=$(objext)
	@make -C plugin_bios dllext=$(dllext) objext=$(objext)
	@make -C plugin_gte dllext=$(dllext) objext=$(objext)
	@make -C plugin_map_load dllext=$(dllext) objext=$(objext)

uninstall:
	rm -f $(DIR_INSTALL)/RbPSe$(exeext)
	rm -f $(DIR_INSTALL_PLUGINS)/plugin_*$(dllext)

clean: cleanrbpse cleanplugins

cleanrbpse:
	rm -f RbPSe$(exeext)
	rm -f *$(objext)

cleanplugins:
	@make -C plugin_cpu clean AS="$(AS)" CC="$(CC)" SHARED="$(SHARED)" dllext="$(dllext)" objext="$(objext)"
	@make -C plugin_gpu_psemu clean
	@make -C plugin_spu_psemu clean
	@make -C plugin_boot clean
	@make -C plugin_bios clean
	@make -C plugin_gte clean
	@make -C plugin_map_load clean

RbPSe$(exeext): $(OBJ_RBPSE)
	$(LD) -o $@ $(OBJ_RBPSE) $(LFLAGS)

%$(objext): %.c
	$(CC) $(CFLAGS) -c -o $@ $<

#
################################################################################
