CC = gcc
# PICK THE CROSS strip WHEN THERE IS ONE. In the `amigadev/crosstools` container
# /usr/bin/gcc is symlinked to ppc-morphos-gcc, but `strip` is NOT -- it is host
# binutils, which cannot read elf32-morphos and fails the link step with
# "Unable to recognise the architecture of the input file". Falls back to plain
# `strip` so a native MorphOS build is unaffected. Found 09.09.2026.
STRIP := $(shell command -v ppc-morphos-strip 2>/dev/null || echo strip)
CHMOD = chmod
CP = cp
RM = rm

CFLAGS = -O2 -Wall -noixemul
LFLAGS = -lm -noixemul
CDEFS = \
	-D__AMIGADATE__=\"$(shell date "+%d.%m.%Y")\" \
	-D__YEAR__=\"$(shell date "+%Y")\" \
	-DNO_PPCINLINE_STDARG

EXE = CloneCollect

OBJ = \
	cc_startup.o \
	cc_analog.o \
	cc_args.o \
	cc_arexx.o \
	cc_crc32.o \
	cc_crc64.o \
	cc_functions.o \
	cc_main.o \
	cc_mcc_application.o \
	cc_mcc_custom.o \
	cc_mcc_main.o \
	cc_mcc_main_hooks.o \
	cc_mcc_misc.o \
	cc_mcc_list.o \
	cc_mcc_settings.o \
	cc_md5.o \
	cc_misc.o \
	cc_string.o \
	keep_dtors_nonempty.o

all: check-rexx $(EXE)

# The ARexx suites are source too, and the build host has no interpreter -- so a
# dialect mistake is only found ON THE HARDWARE, mid-run, after the suite has made
# its files, and it reads as a product failure. A sweep on 11.09.2026 found live
# examples in two projects. Generic; from knowledge-db/templates/arexx/.
# `all` belongs here too: it depends on the .PHONY check-rexx, and an aggregate
# target that is not itself declared .PHONY would stop working the day a file of
# that name appears. It does NOT currently force a rebuild -- $(EXE) is a separate
# file target with its own prerequisites -- but the declaration is what keeps
# "an unchanged binary is not rebuilt" true by construction. See knowledge-db
# standards/S14-build-only-what-changed.md; `s14lint.awk` checks it.
.PHONY: all check-rexx
check-rexx:
	@sh tests/check_rexx.sh

$(EXE): $(OBJ)
	$(CC) $(OBJ) -o $@.debug $(LFLAGS)
	$(STRIP) --strip-all --strip-unneeded --remove-section .comment $@.debug -o $@
# An EMPTY .dtors makes this binary crash on EVERY exit, after doing its job
# perfectly (knowledge-db 08 X1). BOTH artifacts are checked: `strip` is exactly
# the kind of step that could drop the section, so checking only the unstripped
# .debug would prove nothing about what ships.
	@sh check_dtors.sh $@.debug $@
	$(CHMOD) +x $(EXE)

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

clean:
	$(RM) -f $(OBJ) $(EXE) $(EXE).debug

#---------------------------------------------------------------------------

DIR_MOS = 0-REL:project/build/ppc-morphos/$(EXE)
DIR_SRC = 0-REL:project/source/$(EXE)

copy_cls:
	$(CP) -f ChangeLog $(DIR_MOS)/Docs
	$(CP) -f ChangeLog $(DIR_SRC)/Docs

copy_mos:
	$(CP) -f $(EXE) $(DIR_MOS)

copy_src:
	$(RM) -f $(DIR_SRC)/Source/*.c $(DIR_SRC)/Source/*.h
	$(CP) -f *.c *.h makefile $(DIR_SRC)/Source

