# Makefile for pz80, a native PASM-compatible assembler writing Microsoft REL.
#
# Copyright (c) 2026, Robert Cousins.  SPDX-License-Identifier: BSD-2-Clause
#
#   make               build pz80
#   make test          the golden objects, the must-refuse cases, the command line
#   make oracle        the cases against PASM 2.00 on tdosbox (needs the emulator)
#   make sanitize-test the tests under AddressSanitizer and UBSan
#   make man           the man page formats cleanly
#   make install       into $(PREFIX)/bin and $(PREFIX)/share/man
#   make clean
#
# The targets and variables follow mz80's, so that the top-level Makefile
# can drive every subpackage the same way.
#
# Versions:
#   1.0  2026-11-11  Robert Cousins  Initial version.

PKG      = pz80
VERSION  = 0.1
DISTDIR  = $(PKG)-$(VERSION)

CC       ?= cc
CSTD     ?= -std=c2x
CWARN    ?= -Wall -Wextra -Wshadow -Wpointer-arith -Wcast-qual \
            -Wstrict-prototypes
COPT     ?= -O2
CFLAGS   ?= $(CSTD) $(CWARN) $(COPT)
LIBREL   ?= ../librel
CPPFLAGS ?= -I. -I$(LIBREL)
LDFLAGS  ?=
LIBS     ?=
INSTALL  ?= install

PREFIX     ?= /usr/local
BINDIR     ?= $(PREFIX)/bin
MANDIR     ?= $(PREFIX)/share/man

PROG     = pz80
HDRS     = pz80.h
SRCS     = main.c asm.c src.c lex.c expr.c enc.c emit.c sym.c list.c util.c
OBJS     = $(SRCS:.c=.o)
RELOBJS  = relread.o relwrite.o
TESTS    =
MANS     = pz80.1
DISTFILES = Makefile README SPEC.md $(HDRS) $(SRCS) $(MANS) \
            tests/run.sh tests/oracle.sh tests/oracle.py tests/cmpmap.sh \
            $(wildcard tests/oracle/*.ASM tests/oracle/*.INC) \
            $(wildcard tests/bad/*.asm) $(wildcard tests/golden/*.rel)

all: $(PROG)

$(PROG): $(OBJS) $(RELOBJS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(RELOBJS) $(LIBS)

$(OBJS): $(HDRS) $(LIBREL)/rel.h

relread.o: $(LIBREL)/relread.c $(LIBREL)/rel.h
	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $(LIBREL)/relread.c

relwrite.o: $(LIBREL)/relwrite.c $(LIBREL)/rel.h
	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $(LIBREL)/relwrite.c

# A build for one target must not reuse another's objects (a cross-compile
# after a native build, or the reverse); the stamp names the target.
TARGET  := $(subst /,_,$(shell $(CC) -dumpmachine 2>/dev/null || uname -m))
STAMP    = .target-$(TARGET)
$(STAMP):
	rm -f .target-*
	@touch $@
$(OBJS) $(RELOBJS): $(STAMP)

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

tests: $(TESTS)

test check: $(PROG) tests
	@sh tests/run.sh

oracle: $(PROG)
	@sh tests/oracle.sh

sanitize-test:
	$(MAKE) clean
	$(MAKE) CFLAGS="$(CSTD) $(CWARN) -g -O1 \
	    -fsanitize=address,undefined" test
	$(MAKE) clean

man:
	@for m in $(MANS); do \
	    (mandoc -Tascii $$m || groff -mdoc -Tascii $$m) > /dev/null || \
	    exit 1; \
	done
	@echo "man pages format cleanly"

docs: man

install: $(PROG)
	$(INSTALL) -d $(DESTDIR)$(BINDIR)
	$(INSTALL) -m 755 $(PROG) $(DESTDIR)$(BINDIR)/$(PROG)
	$(INSTALL) -d $(DESTDIR)$(MANDIR)/man1
	$(INSTALL) -m 644 pz80.1 $(DESTDIR)$(MANDIR)/man1/pz80.1

uninstall:
	rm -f $(DESTDIR)$(BINDIR)/$(PROG)
	rm -f $(DESTDIR)$(MANDIR)/man1/pz80.1

distfiles:
	@for f in $(DISTFILES); do \
	    mkdir -p $(DESTBASE)/`dirname $$f`; \
	    cp $$f $(DESTBASE)/$$f; \
	done

clean:
	rm -f .target-*
	rm -f $(PROG) $(OBJS) $(RELOBJS) core *.core
	rm -rf tests/out/run tests/out/pz80

distclean: clean

.PHONY: all tests test check oracle sanitize-test man docs install uninstall \
        distfiles clean distclean
.SUFFIXES: .c .o
