# ---------------------------------------------------------------------------
# mz80 -- a native MACRO-80 compatible assembler for the Z80 and Z180
#
# $Id: Makefile,v 1.6 2026/11/10 18:55:22 rec Exp $
#
# Author:  Robert Cousins
# Purpose: Build the assembler, its test programs, its man page and its LaTeX
#          manual, and roll a release tarball.
#
# The release contract: unpack mz80-<version>.tar.gz in an empty directory and
#
#       make ; sudo make install
#
# There is no configure script.  Platform differences are handled by the
# uname-driven block below.
#
# Note: the REL encoder is NOT copied into this package.  It comes from
#       librel, so that mz80, mlink, gen and the reconstruction tools all
#       encode from one item table -- a second copy is the drift rel.h's own
#       header warns about.  LIBREL points at it and can be overridden.
#
# Versions:
#   1.4  2026-11-10  Robert Cousins  Version 1.4.  tests/grid, tests/cases.sh
#                                    and the bad/ and good/ cases ship; the
#                                    suite refuses what it should.
#   1.3  2026-11-09  Robert Cousins  Objects depend on a stamp named for the
#                                    compiler's target, so a tree built for
#                                    one CPU is rebuilt, not patched, for
#                                    another.
#   1.2  2026-09-10  Robert Cousins  The tests/% rule passes LDFLAGS; without
#                                    it a cross-compiled "make LDFLAGS=-static"
#                                    left the test programs dynamic, and they
#                                    would not load on the target.
#   1.0  2026-08-31  Robert Cousins  Initial version.
# ---------------------------------------------------------------------------

PKG      = mz80
VERSION  = 1.4
DISTDIR  = $(PKG)-$(VERSION)

# ----- toolchain -----------------------------------------------------------
# -std=c2x is the newest standard accepted by BOTH gcc 13 (Linux) and
# clang 19 (FreeBSD); gcc 13 rejects -std=c23.  Each .c also defines
# _POSIX_C_SOURCE 200809L before its includes, because c2x is strict ISO.

CC       ?= cc
CSTD     ?= -std=c2x
CWARN    ?= -Wall -Wextra -Wshadow -Wpointer-arith -Wcast-qual \
            -Wstrict-prototypes
COPT     ?= -O2
CFLAGS   ?= $(CSTD) $(CWARN) $(COPT)
# In the reconstruction tree librel is beside us; a release tarball carries a
# copy in ./librel so that it unpacks and builds in an empty directory.
LIBREL   ?= ../librel
CPPFLAGS ?= -I. -I$(LIBREL)
LDFLAGS  ?=
LIBS     ?=
INSTALL  ?= install

# ----- platform detection --------------------------------------------------

UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
    PLATFORM_LIBS =
endif
ifeq ($(UNAME_S),FreeBSD)
    PLATFORM_LIBS =
endif
ifeq ($(UNAME_S),Darwin)
    PLATFORM_LIBS =
endif

# ----- install layout ------------------------------------------------------

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

# ----- what we build -------------------------------------------------------

PROG     = mz80
HDRS     = mz80.h
SRCS     = main.c asm.c src.c lex.c expr.c enc.c emit.c sym.c list.c tex.c \
           util.c
OBJS     = $(SRCS:.c=.o)
RELOBJS  = relread.o relwrite.o

TESTS    = tests/t_expr tests/t_sym tests/grid
MANS     = mz80.1

DISTFILES = Makefile README SPEC.md $(HDRS) $(SRCS) $(MANS) \
            $(TESTS:=.c) tests/run.sh tests/cases.sh tests/m80cmp.sh \
            tests/lstcmp.sh tests/m80cases.sh tests/m80quirks.txt \
            tests/z180.mac tests/edge.mac tests/undef.mac \
            tests/phasecom.mac tests/xcommon.mac \
            tests/z180.rel.golden tests/edge.rel.golden \
            tests/phasecom.rel.golden tests/xcommon.rel.golden \
            $(wildcard tests/bad/*.mac tests/bad/*.inc tests/good/*.mac)

# ----- targets -------------------------------------------------------------

all: $(PROG)

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

# Every object depends on the one header: struct asmb's layout is shared by
# all of them, and a stale object silently disagrees about field offsets.
$(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

# ----- one architecture per tree -------------------------------------------

# Every object depends on a stamp named for the compiler's target, so that a
# build for another CPU in the same tree -- a native make after a cross
# build, or the reverse -- rebuilds everything instead of recompiling only
# what changed and archiving objects of two architectures together.  That
# happened once: a make on an x86-64 host put one x86 object into a RISC-V
# librel.a, and the link that followed failed.
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)

# A pattern rule, so that a test depends on ITS OWN SOURCE.  Listing only the
# program, as the obvious form does, means editing a test does not rebuild it
# and the suite silently keeps testing the previous version.
tests/%: tests/%.c $(OBJS) $(RELOBJS) $(HDRS)
	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $< \
	    $(filter-out main.o,$(OBJS)) $(RELOBJS) $(LIBS)

test check: $(PROG) tests
	@sh tests/run.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 mz80.1 $(DESTDIR)$(MANDIR)/man1/mz80.1

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


# Copy this package's files into the staging tree the top-level dist builds.
# DESTBASE is passed in; the package does not decide where a release goes.
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) $(TESTS) core *.core
	rm -f *.aux *.log *.idx *.ilg *.ind *.toc *.out

distclean: clean

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