# OrbisHookah – PS4 PRX hooking library
# Builds with the OpenOrbis toolchain.
#
# Prerequisites
# =============
#   Export OO_PS4_TOOLCHAIN to point at your OpenOrbis installation, e.g.:
#     export OO_PS4_TOOLCHAIN=/opt/openorbis
#   or pass it on the command line:
#     make OO_PS4_TOOLCHAIN=/opt/openorbis
#
# Targets
# =======
#   all   – build OrbisHookah.prx  (default)
#   clean – remove build artefacts

OPENORBIS ?= $(OO_PS4_TOOLCHAIN)
ifeq ($(strip $(OPENORBIS)),)
$(error OO_PS4_TOOLCHAIN is not set. \
        Set it to your OpenOrbis installation directory and try again.)
endif

CC  := $(OPENORBIS)/bin/orbis-clang
CXX := $(OPENORBIS)/bin/orbis-clang++

TARGET_TRIPLE := x86_64-sie-ps4

CFLAGS := \
    -target $(TARGET_TRIPLE) \
    -O2                      \
    -Wall                    \
    -Wextra                  \
    -Wno-unused-parameter

CXXFLAGS := \
    $(CFLAGS)        \
    -std=c++17       \
    -fno-rtti        \
    -fno-exceptions

LDFLAGS := \
    -target $(TARGET_TRIPLE) \
    -shared                  \
    -fPIC                    \
    -L$(OPENORBIS)/lib

LIBS := -lkernel_stub -lc_stub

INCLUDES := \
    -I$(OPENORBIS)/include \
    -Iinclude              \
    -Isource

TARGET := OrbisHookah.prx

SRCDIR := source
OBJDIR := build

SRCS := \
    $(SRCDIR)/main.cpp         \
    $(SRCDIR)/hook/hook.cpp    \
    $(SRCDIR)/hook/pattern.cpp \
    $(SRCDIR)/hook/memory.cpp

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

.PHONY: all clean

all: $(TARGET)

$(TARGET): $(OBJS)
	$(CXX) $(LDFLAGS) -o $@ $^ $(LIBS)
	@echo "Built: $@"

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

clean:
	$(RM) -r $(OBJDIR) $(TARGET)
