EXE = gearsystem

EMULATOR_SRC=../../src
EMULATOR_DESKTOP_SHARED_SRC=../desktop-shared
EMULATOR_AUDIO_SRC=$(EMULATOR_SRC)/audio
EMULATOR_AUDIO_SHARED_SRC=../audio-shared
IMGUI_SRC=$(EMULATOR_DESKTOP_SHARED_SRC)/imgui
IMGUI_FILEBROWSER_SRC=$(EMULATOR_DESKTOP_SHARED_SRC)/FileBrowser

SOURCES = $(EMULATOR_DESKTOP_SHARED_SRC)/main.cpp $(EMULATOR_DESKTOP_SHARED_SRC)/emu.cpp $(EMULATOR_DESKTOP_SHARED_SRC)/gui.cpp $(EMULATOR_DESKTOP_SHARED_SRC)/gui_debug.cpp $(EMULATOR_DESKTOP_SHARED_SRC)/application.cpp $(EMULATOR_DESKTOP_SHARED_SRC)/config.cpp $(EMULATOR_DESKTOP_SHARED_SRC)/renderer.cpp

SOURCES += $(IMGUI_SRC)/imgui_impl_sdl.cpp $(IMGUI_SRC)/imgui_impl_opengl2.cpp $(IMGUI_SRC)/imgui.cpp $(IMGUI_SRC)/imgui_demo.cpp $(IMGUI_SRC)/imgui_draw.cpp $(IMGUI_SRC)/imgui_widgets.cpp $(IMGUI_FILEBROWSER_SRC)/ImGuiFileBrowser.cpp

SOURCES += $(EMULATOR_SRC)/Audio.cpp $(EMULATOR_SRC)/Cartridge.cpp $(EMULATOR_SRC)/CodemastersMemoryRule.cpp $(EMULATOR_SRC)/GameGearIOPorts.cpp $(EMULATOR_SRC)/GearsystemCore.cpp $(EMULATOR_SRC)/Input.cpp $(EMULATOR_SRC)/KoreanMemoryRule.cpp $(EMULATOR_SRC)/Memory.cpp $(EMULATOR_SRC)/MemoryRule.cpp $(EMULATOR_SRC)/MSXMemoryRule.cpp $(EMULATOR_SRC)/opcodes.cpp $(EMULATOR_SRC)/opcodes_cb.cpp $(EMULATOR_SRC)/opcodes_ed.cpp $(EMULATOR_SRC)/Processor.cpp $(EMULATOR_SRC)/RomOnlyMemoryRule.cpp $(EMULATOR_SRC)/SegaMemoryRule.cpp $(EMULATOR_SRC)/SG1000MemoryRule.cpp $(EMULATOR_SRC)/SmsIOPorts.cpp $(EMULATOR_SRC)/Video.cpp $(EMULATOR_SRC)/BootromMemoryRule.cpp $(EMULATOR_SRC)/JanggunMemoryRule.cpp

SOURCES += $(EMULATOR_AUDIO_SRC)/Blip_Buffer.cpp $(EMULATOR_AUDIO_SRC)/Effects_Buffer.cpp $(EMULATOR_AUDIO_SRC)/Sms_Apu.cpp $(EMULATOR_AUDIO_SRC)/Multi_Buffer.cpp

SOURCES += $(EMULATOR_AUDIO_SHARED_SRC)/Sound_Queue.cpp

OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))

UNAME_S := $(shell uname -s)

CXXFLAGS += -I../ -I../../
CXXFLAGS += -Wall -Wextra -Wformat -std=c++11 -DEMULATOR_BUILD=\"$(GIT_VERSION)\"

DEBUG ?= 0
ifeq ($(DEBUG), 1)
    CXXFLAGS +=-DDEBUG -g3
else
    CXXFLAGS +=-DNDEBUG
endif

SANITIZE ?= 0
ifeq ($(SANITIZE), 1)
	CXXFLAGS +=-fsanitize=address
endif

LIBS =

##---------------------------------------------------------------------
## BUILD FLAGS PER PLATFORM
##---------------------------------------------------------------------

ifeq ($(UNAME_S), Linux) #LINUX
	ECHO_MESSAGE = "Linux"
	LIBS += -lGL -lGLEW -ldl `sdl2-config --libs`

	CXXFLAGS += `sdl2-config --cflags`
	CFLAGS += $(CXXFLAGS)
else ifeq ($(UNAME_S), Darwin) #APPLE
	ECHO_MESSAGE = "macOS"
	LIBS += -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo `sdl2-config --libs`
	LIBS += -L/usr/local/lib

	CXXFLAGS += `sdl2-config --cflags`
	CXXFLAGS += -I/usr/local/include -I/opt/local/include
	CFLAGS += $(CXXFLAGS)
else ifeq ($(findstring MINGW,$(UNAME_S)),MINGW)
	ECHO_MESSAGE = "MinGW"
	LIBS += -lgdi32 -lopengl32 -lglew32 -limm32 `pkg-config --static --libs sdl2`

	CXXFLAGS += `pkg-config --cflags sdl2`
	CFLAGS += $(CXXFLAGS)
else
	ECHO_MESSAGE = "Generic Unix-like/BSD"
	LIBS += `sdl2-config --libs` -lSDL2
	LIBS += `pkg-config --libs glew` -lGLEW

	CXXFLAGS += -std=gnu++11
	CXXFLAGS += `sdl2-config --cflags`
	CXXFLAGS += `pkg-config --cflags glew`
	CFLAGS += $(CXXFLAGS)
endif

##---------------------------------------------------------------------
## BUILD RULES
##---------------------------------------------------------------------

%.o:$(IMGUI_SRC)/%.cpp
	$(CXX) $(CXXFLAGS) -c -o $@ $<

%.o:$(IMGUI_FILEBROWSER_SRC)/%.cpp
	$(CXX) $(CXXFLAGS) -c -o $@ $<

%.o:$(EMULATOR_SRC)/%.cpp
	$(CXX) $(CXXFLAGS) -c -o $@ $<

%.o:$(EMULATOR_DESKTOP_SHARED_SRC)/%.cpp
	$(CXX) $(CXXFLAGS) -c -o $@ $<

%.o:$(EMULATOR_AUDIO_SRC)/%.cpp
	$(CXX) $(CXXFLAGS) -c -o $@ $<

%.o:$(EMULATOR_AUDIO_SHARED_SRC)/%.cpp
	$(CXX) $(CXXFLAGS) -c -o $@ $<

all: $(EXE)
	@echo Build complete for $(ECHO_MESSAGE)

$(EXE): $(OBJS)
	$(CXX) -o $@ $^ $(LDFLAGS) $(LIBS)

clean:
	rm -f $(EXE) $(OBJS)
