SRC_DIR := src
BIN_DIR := bin
OBJ_DIR := build
INCLUDE_DIR := include

TARGET_DEBUG := $(BIN_DIR)/game-dbg
TARGET_RELEASE := $(BIN_DIR)/game

PCH_DEBUG := $(OBJ_DIR)/debug/pch.h.gch
PCH_RELEASE := $(OBJ_DIR)/release/pch.h.gch

CCACHE := ccache
CXX := $(CCACHE) $(shell which clang++)
CC := $(CCACHE) $(shell which clang)

# ---------------- SDL3 ----------------

SDL_CFLAGS := $(shell pkg-config --cflags sdl3 sdl3-ttf sdl3-image)
SDL_LIBS_DEBUG := $(shell pkg-config --libs sdl3 sdl3-ttf sdl3-image)
SDL_LIBS_RELEASE := $(shell pkg-config --libs sdl3 sdl3-ttf sdl3-image)

# ---------------- FLAGS ----------------

CFLAGS_DEBUG := \
	-std=c++20 -Wall -Wextra \
	-Og -g -fno-omit-frame-pointer \
	-Wno-unused-command-line-argument \
	-I./include -I./libs/mruby/build/host/include \
	$(SDL_CFLAGS)

CFLAGS_RELEASE := \
	-std=c++20 -O3 -march=x86-64 -mtune=generic \
	-flto=thin \
	-fno-rtti -fomit-frame-pointer -DNDEBUG -s \
	-Wno-unused-command-line-argument \
	-I./include -I./libs/mruby/build/host/include \
	$(SDL_CFLAGS)

PCH_CFLAGS_DEBUG := $(CFLAGS_DEBUG) -x c++-header
PCH_CFLAGS_RELEASE := $(CFLAGS_RELEASE) -x c++-header

# ---------------- LIBS ----------------

LIBS := ./libs/mruby/build/host/lib/libmruby.a

MGEM_DIR := ./libs/l_mgems

# ---------------- SOURCES ----------------

SRC := $(shell find $(SRC_DIR) -name "*.cc")

OBJ_DEBUG := $(patsubst $(SRC_DIR)/%.cc,$(OBJ_DIR)/debug/%.o,$(SRC))
OBJ_RELEASE := $(patsubst $(SRC_DIR)/%.cc,$(OBJ_DIR)/release/%.o,$(SRC))

DEP_DEBUG := $(OBJ_DEBUG:.o=.d)
DEP_RELEASE := $(OBJ_RELEASE:.o=.d)

# ---------------- TARGETS ----------------

.PHONY: all debug release clean mgems

all: debug

debug: mgems $(TARGET_DEBUG)

release: mgems $(TARGET_RELEASE)

# ---------------- MRB_MGEMS ----------------

mgems:
	@$(MGEM_DIR)/compile.sh

# ---------------- PCH ----------------

$(PCH_DEBUG): $(INCLUDE_DIR)/pch.h
	@mkdir -p $(dir $@)
	@$(CXX) $(PCH_CFLAGS_DEBUG) -o $@ $<
	@echo "Precompiled header (debug) generated at $@"

$(PCH_RELEASE): $(INCLUDE_DIR)/pch.h
	@mkdir -p $(dir $@)
	@$(CXX) $(PCH_CFLAGS_RELEASE) -o $@ $<
	@echo "Precompiled header (release) generated at $@"

# ---------------- LINK ----------------

$(TARGET_DEBUG): $(PCH_DEBUG) $(OBJ_DEBUG)
	@mkdir -p $(BIN_DIR)
	@$(CXX) $(CFLAGS_DEBUG) -o $@ $(OBJ_DEBUG) $(SDL_LIBS_DEBUG) $(LIBS)
	@echo "Debug build complete: $@"

$(TARGET_RELEASE): $(PCH_RELEASE) $(OBJ_RELEASE)
	@mkdir -p $(BIN_DIR)
	@$(CXX) $(CFLAGS_RELEASE) -o $@ $(OBJ_RELEASE) $(SDL_LIBS_RELEASE) $(LIBS)
	@echo "Release build complete: $@"

# ---------------- COMPILE ----------------

$(OBJ_DIR)/debug/%.o: $(SRC_DIR)/%.cc $(PCH_DEBUG)
	@mkdir -p $(dir $@)
	@$(CXX) $(CFLAGS_DEBUG) -include $(INCLUDE_DIR)/pch.h -MMD -MP -c $< -o $@
	@echo "[CXX] Debug object file generated: $@ from $<"

$(OBJ_DIR)/release/%.o: $(SRC_DIR)/%.cc $(PCH_RELEASE)
	@mkdir -p $(dir $@)
	@$(CXX) $(CFLAGS_RELEASE) -include $(INCLUDE_DIR)/pch.h -MMD -MP -c $< -o $@
	@echo "[CXX] Release object file generated: $@ from $<"

# ---------------- DEP ----------------

-include $(DEP_DEBUG)
-include $(DEP_RELEASE)

# ---------------- CLEAN ----------------

clean:
	@rm -rf $(OBJ_DIR) $(BIN_DIR)
	@$(MGEM_DIR)/compile.sh clean
	@echo "Cleaned build artifacts."
