SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
include $(SELF_DIR)common/ExportPath.mk

substitute   = $(subst $(JABY_ENGINE_DIR)/include/modules,!JABYENGINEMODULES,$(subst ..,!super,$1))
desubstitute = $(subst !JABYENGINEMODULES,$(JABY_ENGINE_DIR)/include/modules,$(subst !super,..,$1))

#Build architecture/variant string, possible values: x86, armv7le, etc...
PLATFORM ?= PSX

#Build profile, possible values: release, debug, profile, coverage
BUILD_DIR     ?= bin/$(PSX_TV_FORMAT)
BUILD_PROFILE ?= debug
PSX_TV_FORMAT     ?= PAL

CONFIG_NAME ?= $(PLATFORM)-$(BUILD_PROFILE)
OUTPUT_DIR   = $(BUILD_DIR)/$(CONFIG_NAME)
TARGET       = $(OUTPUT_DIR)/$(ARTIFACT)

#Compiler definitions
HAS_LINUX_MIPS_GCC = $(shell which mipsel-linux-gnu-gcc > /dev/null 2> /dev/null && echo true || echo false)
ifeq ($(HAS_LINUX_MIPS_GCC),true)
PREFIX ?= mipsel-linux-gnu
FORMAT ?= elf32-tradlittlemips
else
PREFIX ?= mipsel-none-elf
FORMAT ?= elf32-littlemips
endif

#Take this to PSEXETarget.mk??
#LDSCRIPT ?= $(PSCX_REDUX_DIR)/ps-exe.ld
#ifneq ($(strip $(OVERLAYSCRIPT)),)
#LDSCRIPT := $(addprefix $(OVERLAYSCRIPT) , -T$(LDSCRIPT))
#else
#LDSCRIPT := $(addprefix $(PSCX_REDUX_DIR)/default.ld , -T$(LDSCRIPT))
#endif

CC  = $(PREFIX)-gcc
CXX = $(PREFIX)-g++
LD  = $(CXX)
AR  = ar

#architecture flags
ARCHFLAGS  = -march=r2000 -mtune=r2000 -mabi=32 -EL -fno-pic -mno-shared -nostdinc -nostdinc++ -mno-abicalls -mfp32 -mno-llsc
ARCHFLAGS += -fno-stack-protector -nostdlib -ffreestanding

#Compiler flags for build profiles
CCFLAGS_release += -O3
CCFLAGS_debug   += -O0

CXXFLAGS += -fno-exceptions -fno-rtti -fno-threadsafe-statics

CCFLAGS += -mno-gpopt -fomit-frame-pointer -ffunction-sections -fdata-sections
CCFLAGS += -fno-builtin -fno-strict-aliasing -Wno-attributes
CCFLAGS += -std=c++20 -fmodules-ts
CCFLAGS += $(CCFLAGS_$(BUILD_PROFILE))
CCFLAGS += $(ARCHFLAGS)
CCFLAGS += $(INCLUDES)
CCFLAGS += -DJABYENGINE_$(PSX_TV_FORMAT)

#Linker flags
LDFLAGS_release += -Os

LDFLAGS_all += -Wl,-Map=$(TARGET).map -nostdlib -T$(JABY_ENGINE_DIR)/mkfile/psexe.ld -static -Wl,--gc-sections -Wl,--build-id=none -Wl,--no-check-sections
LDFLAGS_all += $(ARCHFLAGS) -Wl,--oformat=$(FORMAT)
LDFLAGS_all += $(LDFLAGS_$(BUILD_PROFILE))

LIBS_all    += $(LIBS_$(BUILD_PROFILE))

DEPS = -Wp,-MMD,$(@:%.o=%.d),-MT,$@

#Object files list
MODS += $(call rwildcard,$(JABY_ENGINE_DIR)/include/modules, cxx)
SRCS := $(MODS) $(SRCS)
OBJS  = $(addprefix $(OUTPUT_DIR)/,$(addsuffix .o, $(call substitute,$(basename $(SRCS)))))

#Compiling rule
.SECONDEXPANSION:
$(OUTPUT_DIR)/%.o: $$(call desubstitute,%.s)
	@mkdir -p $(dir $@)
	$(CC) -c $(DEPS) -o $@ $(CCFLAGS) $<

.SECONDEXPANSION:
$(OUTPUT_DIR)/%.o: $$(call desubstitute,%.c)
	@mkdir -p $(dir $@)
	$(CC) -c $(DEPS) -o $@ $(CCFLAGS) $<

.SECONDEXPANSION:
$(OUTPUT_DIR)/%.o: $$(call desubstitute,%.cpp)
	@mkdir -p $(dir $@)
	$(CXX) -c $(DEPS) -o $@ $(CCFLAGS) $(CXXFLAGS) $<

.SECONDEXPANSION:
$(OUTPUT_DIR)/%.o: $$(call desubstitute,%.cxx)
	@mkdir -p $(dir $@)
	$(CXX) -c $(DEPS) -o $@ $(CCFLAGS) $(CXXFLAGS) $<

#Inclusion of dependencies (object files to source and includes)
-include $(OBJS:%.o=%.d)