# PS4GS Makefile
#
# Targets
#   make       (default)  Build PS4GS.prx
#   make clean            Remove all build artefacts
#
# Prerequisites (installed by setup.sh)
#   clang++ and ld.lld available on PATH  (apt: clang lld)
#
# The OpenOrbis PS4Toolchain is bundled at ../PS4Toolchain – no environment
# variable or external installation is required.

.DEFAULT_GOAL := ps4


TOOLCHAIN := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))/../PS4Toolchain)

CXX := clang++
LD  := ld.lld


SRCDIR  := source
OBJDIR  := build
HOOKLIB := ../OrbisHooker


SRCS := \
    $(SRCDIR)/main.cpp              \
    $(SRCDIR)/platform/ps4.cpp      \
    $(SRCDIR)/offsets/offsets.cpp   \
    $(SRCDIR)/logic/game.cpp

HOOKAH_SRCS := \
    $(HOOKLIB)/source/hook/hook.cpp     \
    $(HOOKLIB)/source/hook/pattern.cpp  \
    $(HOOKLIB)/source/hook/memory.cpp

ALL_SRCS := $(SRCS) $(HOOKAH_SRCS)
OBJS     := $(patsubst %.cpp, $(OBJDIR)/%.o, $(ALL_SRCS))

# ---------------------------------------------------------------------------
# Compile flags
#
# --target=x86_64-pc-freebsd12-elf  PS4 / FreeBSD ELF ABI
# -isysroot                          point sysroot at bundled headers+libs
# -isystem <include>                 PS4 C system headers
# -isystem <include/c++/v1>          libc++ headers
# ---------------------------------------------------------------------------

CXXFLAGS := \
    --target=x86_64-pc-freebsd12-elf    \
    -std=c++17                          \
    -O2                                 \
    -Wall                               \
    -Wextra                             \
    -Wno-unused-parameter               \
    -fno-rtti                           \
    -fno-exceptions                     \
    -fPIC                               \
    -funwind-tables                     \
    -D_LIBCPP_HAS_NO_THREADS            \
    -isysroot $(TOOLCHAIN)              \
    -isystem $(TOOLCHAIN)/include       \
    -isystem $(TOOLCHAIN)/include/c++/v1

INCLUDES := \
    -I$(HOOKLIB)/include    \
    -I$(HOOKLIB)/source     \
    -Isource

# ---------------------------------------------------------------------------
# Link flags
#
# -shared                build a PRX (PS4 shared module) instead of an EBOOT
# --script link.x        OpenOrbis PS4 ELF linker script
# --eh-frame-hdr         emit .eh_frame_hdr for unwinding
# -lkernel               PS4 kernel stub  (libkernel.so from PS4Toolchain/lib)
# -lc                    PS4 C runtime    (libc.a from PS4Toolchain/lib)
# ---------------------------------------------------------------------------

LDFLAGS := \
    -m elf_x86_64                   \
    -shared                         \
    --script $(TOOLCHAIN)/link.x    \
    --eh-frame-hdr                  \
    -L$(TOOLCHAIN)/lib

LIBS := -lkernel -lc


TARGET := PS4GS.prx

.PHONY: ps4
ps4: $(TARGET)
	@echo "────────────────────────────────────────────────────"
	@echo "  PS4GS.prx built successfully"
	@echo "  Deploy: scp PS4GS.prx root@<ps4-ip>:/data/homebrew/"
	@echo "────────────────────────────────────────────────────"

$(TARGET): $(OBJS)
	$(LD) $(LDFLAGS) -o $@ $^ $(LIBS)

$(OBJDIR)/%.o: %.cpp
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<


.PHONY: clean
clean:
	$(RM) -r $(OBJDIR) $(TARGET)
	@echo "Clean complete"
