# FOL 2026-11-01: Build the embedded helper as a small kernel PRX with its own module_start, matching the established KCall PRX pattern.
TARGET := pspuae-oc
# FOL 2026-11-09: Prefer PSPSDK's standard environment aliases; psp-config is only an optional PATH convenience.
PSP_CONFIG := $(shell command -v psp-config 2>/dev/null)
PSPDEV ?= $(if $(PSP_CONFIG),$(shell $(PSP_CONFIG) --pspdev-path))
PSPSDK ?= $(if $(PSP_CONFIG),$(shell $(PSP_CONFIG) --pspsdk-path),$(if $(PSPDEV),$(PSPDEV)/psp/sdk))
ifeq ($(strip $(PSPSDK)),)
$(error PSPSDK is not set. Source your PSPSDK environment, or export PSPDEV and PSPSDK before running make)
endif

CC := psp-gcc
FIXUP := psp-fixup-imports
PRXGEN := psp-prxgen
INCDIR := -I. -I$(PSPDEV)/psp/include -I$(PSPSDK)/include
LIBDIR := -L. -L$(PSPDEV)/psp/lib -L$(PSPSDK)/lib
CFLAGS := -G0 -Os -march=allegrex -mabi=eabi -fno-strict-aliasing -Wall $(INCDIR)
LDFLAGS := $(LIBDIR) -Wl,-q,-T$(PSPSDK)/lib/linkfile.prx -nostartfiles -Wl,-zmax-page-size=128 -Wl,--gc-sections
LIBS := -lpsppower -lpspsdk
OBJS := pspuae_overclock.o

.PHONY: all clean
all: $(TARGET).prx

$(TARGET).elf: $(OBJS)
	$(CC) $(LDFLAGS) $^ $(LIBS) -o $@ && $(FIXUP) $@

$(TARGET).prx: $(TARGET).elf
	$(PRXGEN) $< $@

pspuae_overclock.o: pspuae_overclock.c
	$(CC) $(CFLAGS) -c -o $@ $<

clean:
	rm -f $(TARGET).prx $(TARGET).elf $(OBJS)
