#---------------------------------------------------------------------------------
# xexkit: build native Xbox 360 XEX titles with GCC, without the Microsoft XDK.
#
# Include this from a title's Makefile, run inside the free60/libxenon docker
# image (it provides GCC, newlib and libstdc++; libxenon itself is not used):
#
#   XEXKIT       := path/to/xexkit
#   TITLE        := mygame                  # output: $(BUILD)/$(TITLE)/default.xex
#   TITLE_SRC    := src/main.cpp src/util.c
#   TITLE_TOML   := xex.toml                # title id, flags (see examples/)
#   all: title
#   include $(XEXKIT)/mk/xexkit.mk
#
# Optional, before the include:
#   BUILD              output directory (default: build)
#   TITLE_FLAGS        extra compiler flags for the title's sources
#   TITLE_C_AS_CXX=1   compile the title's .c files as C++ (for C code that
#                      C++ exceptions pass through, as Lua built as C++ does)
#   TITLE_OBJ_EXTRA    more objects to link (built by your own rules)
#
# `make title` builds $(BUILD)/$(TITLE)/default.xex; the ELF next to it
# (app.elf) keeps the symbols for xenon-addr2line.
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITXENON)),)
$(error "Please set DEVKITXENON (run inside the free60/libxenon image)")
endif
ifeq ($(strip $(XEXKIT)),)
$(error "Set XEXKIT to the xexkit directory before including xexkit.mk")
endif

XEXKIT     := $(abspath $(XEXKIT))
BUILD      ?= build
XEX_LINK   ?= $(XEXKIT)/tools/bin/xex-link

CC         := xenon-gcc
CXX        := xenon-g++

# 32-bit pointers in 64-bit registers, as titles run; no small-data or PIC.
XEXKIT_MACH     := -m32 -mpowerpc64 -mhard-float -maltivec -fno-pic -G0 -msdata=none
XEXKIT_INCLUDES := -I$(XEXKIT)/runtime/include -I$(XEXKIT)/runtime -I$(XEXKIT)/sdk/include
XEXKIT_CFLAGS   := $(XEXKIT_MACH) -O2 -g -Wall -Wno-attributes -ffunction-sections -fdata-sections \
                   $(XEXKIT_INCLUDES)
XEXKIT_CXXFLAGS := $(XEXKIT_CFLAGS) -std=gnu++17
XEXKIT_ASFLAGS  := $(XEXKIT_MACH) -Wa,-mppc64
XEXKIT_LDFLAGS  := $(XEXKIT_MACH) -nostartfiles -nostdlib -T $(XEXKIT)/runtime/link.ld -Wl,--gc-sections
XEXKIT_LIBS     := -L$(DEVKITXENON)/xenon/lib/32 -Wl,--start-group -lstdc++ -lm -lc -lgcc -Wl,--end-group

XEXKIT_RUNTIME_SRC := runtime/crt0.S runtime/crt.c runtime/sys.c runtime/fs.c runtime/posix.c \
                      runtime/video.c runtime/input.c runtime/audio.c runtime/fault.c \
                      runtime/kernel_entries.S runtime/extra_imports.S sdk/thunks/ppc32.S
XEXKIT_RUNTIME_OBJ := $(patsubst %,$(BUILD)/xexkit/%.o,$(XEXKIT_RUNTIME_SRC))

.SECONDEXPANSION:

$(BUILD)/xexkit/%.S.o: $(XEXKIT)/%.S
	@mkdir -p $(dir $@)
	@echo [$(notdir $<)]
	@$(CC) $(XEXKIT_ASFLAGS) -c $< -o $@
$(BUILD)/xexkit/%.c.o: $(XEXKIT)/%.c
	@mkdir -p $(dir $@)
	@echo [$(notdir $<)]
	@$(CC) $(XEXKIT_CFLAGS) -MMD -MP -c $< -o $@

# --- the title ------------------------------------------------------------------
ifneq ($(strip $(TITLE)),)
TITLE_DIR := $(BUILD)/$(TITLE)
# sources outside the project ("../x.c") are built under obj/__/x.c.o
TITLE_OBJ := $(patsubst %,$(TITLE_DIR)/obj/%.o,$(subst ../,__/,$(TITLE_SRC)))

ifeq ($(TITLE_C_AS_CXX),1)
TITLE_CC := $(CXX) -x c++ $(XEXKIT_CXXFLAGS)
else
TITLE_CC := $(CC) $(XEXKIT_CFLAGS)
endif

.PHONY: title
title: $(TITLE_DIR)/default.xex

$(TITLE_DIR)/obj/%.c.o: $$(subst __/,../,%.c)
	@mkdir -p $(dir $@)
	@echo [$(notdir $<)]
	@$(TITLE_CC) $(TITLE_FLAGS) -MMD -MP -c $< -o $@
$(TITLE_DIR)/obj/%.cpp.o: $$(subst __/,../,%.cpp)
	@mkdir -p $(dir $@)
	@echo [$(notdir $<)]
	@$(CXX) $(XEXKIT_CXXFLAGS) $(TITLE_FLAGS) -MMD -MP -c $< -o $@
$(TITLE_DIR)/obj/%.S.o: $$(subst __/,../,%.S)
	@mkdir -p $(dir $@)
	@echo [$(notdir $<)]
	@$(CC) $(XEXKIT_ASFLAGS) -c $< -o $@

$(TITLE_DIR)/app.elf: $(TITLE_OBJ) $(TITLE_OBJ_EXTRA) $(XEXKIT_RUNTIME_OBJ) $(XEXKIT)/runtime/link.ld
	@mkdir -p $(dir $@)
	@echo linking $@
	@$(CXX) $(XEXKIT_LDFLAGS) -Wl,-Map,$(@:.elf=.map) -o $@ $(filter %.o,$^) $(XEXKIT_LIBS)

$(TITLE_DIR)/default.xex: $(TITLE_DIR)/app.elf $(TITLE_TOML)
	@test -x $(XEX_LINK) || (echo "xex-link missing: build it with $(XEXKIT)/tools/build-xex-link.sh" && false)
	@$(XEX_LINK) $< $@ --manifest $(TITLE_TOML)
endif

# keep the ELFs (symbols for addr2line)
.SECONDARY:
-include $(shell find $(BUILD) -name '*.d' 2>/dev/null)
