#---------------------------------------------------------------------------------
# FAKE-08 for Xbox 360 (libxenon / XeLL)
#
# Build from the repository root with `make xenon`, inside the free60/libxenon
# docker image (see .github/workflows/buildXenon.sh).
#---------------------------------------------------------------------------------
.SUFFIXES:

ifeq ($(strip $(DEVKITXENON)),)
$(error "Please set DEVKITXENON in your environment. export DEVKITXENON=<path to>/xenon")
endif

include $(DEVKITXENON)/rules

TARGET		:=	FAKE-08_Xenon
BUILD		:=	build
SOURCES		:=	${SOURCES} source
INCLUDES	:=	${INCLUDES}

#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
# z8lua's .c files are C++ (fix32 math, throw/catch for Lua errors)
CC		:=	$(CXX)

CFLAGS		=	-g -O2 -Wall -Wno-sign-compare \
			$(MACHDEP) $(INCLUDE) \
			-D__XENON__ -DMINIZ_NO_TIME -DVER_STR=\"$(APP_VERSION)\"
CXXFLAGS	=	$(CFLAGS) -std=gnu++17
# No --gc-sections: libxenon's app.lds does not KEEP(.ctors), so the linker
# would drop every C++ static constructor.
LDFLAGS		=	-g $(MACHDEP) -Wl,-Map,$(notdir $@).map

# libfat must come before libxenon (libfat uses libxenon's disc drivers)
LIBS		:=	-lfat -lxenon -lm
LIBDIRS		:=

#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------

export OUTPUT	:=	$(CURDIR)/$(TARGET)
export VPATH	:=	$(foreach dir,$(SOURCES),$(CURDIR)/$(dir))
export DEPSDIR	:=	$(CURDIR)/$(BUILD)

CFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES	:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))

export LD	:=	$(CXX)

export OFILES	:=	$(CPPFILES:.cpp=.o) $(CFILES:.c=.o)

export INCLUDE	:=	$(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
			$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
			-I$(CURDIR)/$(BUILD) \
			-I$(LIBXENON_INC)

export LIBPATHS	:=	$(foreach dir,$(LIBDIRS),-L$(dir)/lib) \
			-L$(LIBXENON_LIB)

.PHONY: $(BUILD) clean

$(BUILD):
	@[ -d $@ ] || mkdir -p $@
	@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile

clean:
	@echo clean ...
	@rm -fr $(BUILD) $(OUTPUT).elf $(OUTPUT).elf32 $(OUTPUT).elf.map

#---------------------------------------------------------------------------------
else

DEPENDS	:=	$(OFILES:.o=.d)

$(OUTPUT).elf32: $(OUTPUT).elf
$(OUTPUT).elf: $(OFILES)

-include $(DEPENDS)

#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------
