SOURCEDIR := $(abspath $(patsubst %/,%,$(dir $(abspath $(lastword \
	$(MAKEFILE_LIST))))))

NAME := mgba
JGNAME := $(NAME)-jg

FLAGS := -std=gnu99
DEFS := -DM_CORE_GBA -DMINIMAL_CORE=1 -DMGBA_STANDALONE \
	-DDISABLE_THREADING -DHAVE_STRDUP -DHAVE_STRNDUP

SRCDIR := $(SOURCEDIR)/../src

INCLUDES = -I$(SOURCEDIR)/../include -I$(SRCDIR)
INCLUDES_JG = -I$(SOURCEDIR)/../include

LIBS = -lm

DOCS := ../LICENSE README

# Object dirs
MKDIRS := arm core gb \
	gba/cart gba/cheats gba/renderers gba/sio \
	platform/posix platform/windows \
	third-party/blip_buf third-party/inih \
	util/vfs

override INSTALL_DATA := 0
override INSTALL_EXAMPLE := 0
override INSTALL_SHARED := 0

include $(SOURCEDIR)/version.h
include $(SOURCEDIR)/mk/jg.mk

EXT := c
LINKER := $(CC)

ifeq ($(OS), Windows_NT)
	DEFS += -D_WIN32
	LIBS += -lole32 -lshlwapi
else
	DEFS += -DHAVE_LOCALE -DHAVE_LOCALTIME_R
	ifeq ($(UNAME), Darwin)
		DEFS += -DHAVE_STRTOF_L -DHAVE_SNPRINTF_L -DHAVE_XLOCALE \
			-DHAVE_STRLCPY -DHAVE_SETLOCALE
		LIBS += -framework CoreFoundation
	else ifeq ($(UNAME), NetBSD)
		DEFS += -DHAVE_POPCOUNT32 -DHAVE_SETLOCALE
	else
		DEFS += -DHAVE_USELOCALE
	endif
endif

# Windows specific
ifeq ($(OS), Windows_NT)
	CSRCS := platform/windows/memory.c \
		platform/windows/vfs-w32.c
else
	CSRCS := platform/posix/memory.c \
		util/vfs/vfs-dirent.c
endif

CSRCS += arm/arm.c \
	arm/decoder-arm.c \
	arm/decoder-thumb.c \
	arm/decoder.c \
	arm/isa-arm.c \
	arm/isa-thumb.c \
	core/bitmap-cache.c \
	core/lockstep.c \
	core/cache-set.c \
	core/cheats.c \
	core/config.c \
	core/core.c \
	core/directories.c \
	core/input.c \
	core/interface.c \
	core/library.c \
	core/log.c \
	core/map-cache.c \
	core/mem-search.c \
	core/rewind.c \
	core/serialize.c \
	core/sync.c \
	core/thread.c \
	core/tile-cache.c \
	core/timing.c \
	gb/audio.c \
	gba/audio.c \
	gba/bios.c \
	gba/cheats.c \
	gba/core.c \
	gba/dma.c \
	gba/gba.c \
	gba/hle-bios.c \
	gba/input.c \
	gba/io.c \
	gba/memory.c \
	gba/overrides.c \
	gba/savedata.c \
	gba/serialize.c \
	gba/timer.c \
	gba/sio.c \
	gba/video.c \
	gba/cart/ereader.c \
	gba/cart/gpio.c \
	gba/cart/matrix.c \
	gba/cart/vfame.c \
	gba/cheats/codebreaker.c \
	gba/cheats/gameshark.c \
	gba/cheats/parv3.c \
	gba/renderers/cache-set.c \
	gba/renderers/common.c \
	gba/renderers/software-bg.c \
	gba/renderers/software-mode0.c \
	gba/renderers/software-obj.c \
	gba/renderers/video-software.c \
	gba/sio/gbp.c \
	gba/sio/lockstep.c \
	third-party/blip_buf/blip_buf.c \
	third-party/inih/ini.c \
	util/circle-buffer.c \
	util/configuration.c \
	util/gbk-table.c \
	util/crc32.c \
	util/export.c \
	util/formatting.c \
	util/vfs/vfs-fifo.c \
	util/hash.c \
	util/patch.c \
	util/patch-fast.c \
	util/patch-ips.c \
	util/patch-ups.c \
	util/string.c \
	util/table.c \
	util/text-codec.c \
	util/vfs.c \
	util/vfs/vfs-fd.c \
	util/vfs/vfs-mem.c

JGSRCS := jg.c

# List of object files
OBJS := $(patsubst %,$(OBJDIR)/%,$(CSRCS:.c=.o))
OBJS_JG := $(patsubst %,$(OBJDIR)/%,$(JGSRCS:.c=.o))

# Core commands
BUILD_JG = $(call COMPILE_C, $(FLAGS) $(DEFS) $(INCLUDES_JG) $(CFLAGS_JG))
BUILD_MAIN = $(call COMPILE_C, $(FLAGS) $(DEFS) $(INCLUDES))

.PHONY: $(PHONY)

all: $(TARGET)

# Core rules
$(OBJDIR)/%.o: $(SRCDIR)/%.$(EXT) $(PREREQ)
	$(call COMPILE_INFO,$(BUILD_MAIN))
	@$(BUILD_MAIN)

include $(SOURCEDIR)/mk/rules.mk
