# ---------------------------------------------------------------------------
# REL_tools -- a toolchain for the Microsoft REL object format
#
# $Id: Makefile,v 1.8 2026/11/12 17:12:44 rec Exp rec $
#
# Author:  Robert Cousins
# Purpose: Build the library, the assembler and the linker, their man pages
#          and the manual, and roll a release tarball.
#
#          The three shipped separately at 1.0 and are one package from 1.1.
#          They share the object format, they are tested against each other,
#          and each needed the others' documentation to be readable; there was
#          never a reason for three tarballs.
#
# The release contract: unpack REL_tools-<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 in each subdirectory's Makefile.
#
# Note: order matters.  librel must be built before everything else, which
#       links against it; "make -j" is safe because the dependency is
#       expressed, but the SUBDIRS list is not merely alphabetical.  utils
#       comes last because mlib's tests use relcmp when it is present.
#
# Versions:
#   1.7  2026-11-23  Robert Cousins  Version 1.7: one example in the mz80
#                                    chapter of the manual no longer names a
#                                    particular vendor's boards.  The code is
#                                    unchanged.
#   1.6  2026-11-10  Robert Cousins  Version 1.6: the assembler accepts only
#                                    legal input (mz80 1.4, tests/grid.c);
#                                    every reader refuses a directory, an empty
#                                    file and an oversized file through
#                                    librel's rel_slurp(); the loader rejects
#                                    what gen and mlink used to crash on;
#                                    tests/cli.sh and tests/downstream.sh;
#                                    TESTPLAN.md and IDEAS.md.
#   1.5  2026-11-09  Robert Cousins  Bug-fix release, from the tdosbox build.
#                                    librel: a word in one COMMON block
#                                    naming a label in another was deposited
#                                    into the other block.  mz80: undefined
#                                    names in operands are U errors, .PHASE
#                                    inside a COMMON loads at the block's
#                                    counter, and a cross-block word no
#                                    longer reselects.  Every Makefile stamps
#                                    the compiler's target so one tree is
#                                    never built for two CPUs at once.
#   1.4  2026-09-10  Robert Cousins  Verified on a third architecture: the
#                                    package is built and its suite run on
#                                    64-bit RISC-V, both cross-compiled and
#                                    with a native riscv64 gcc, and the
#                                    objects it produces are byte-identical
#                                    to the x86-64 ones.  The one change the
#                                    exercise required is in librel's and
#                                    mz80's Makefiles, whose test rules
#                                    dropped LDFLAGS.
#   1.3  2026-09-03  Robert Cousins  mz80 grows -I, an INCLUDE search path, so
#                                    that a source tree can keep its include
#                                    files in one directory and its modules in
#                                    several.  No other change.
#   1.2  2026-09-02  Robert Cousins  Added mlib and the utils directory: the
#                                    librarian and the four inspection tools
#                                    move here from the reconstruction, so
#                                    that the package's own tests no longer
#                                    reach outside the tarball for relcmp.
#   1.1  2026-09-02  Robert Cousins  Initial version; consolidates librel,
#                                    mz80 and mlink 1.0.
# ---------------------------------------------------------------------------

PKG      = REL_tools
VERSION  = 1.7
DISTDIR  = $(PKG)-$(VERSION)

SUBDIRS  = librel mz80 pz80 mlink mlib utils

MANUAL   = $(PKG)_manual.pdf
MANSRC   = $(PKG)_manual.tex
PARTS    = manual/format.tex manual/librel.tex manual/mz80.tex \
           manual/pz80.tex manual/mlink.tex manual/mlib.tex \
           manual/utils.tex manual/verify.tex manual/appendix.tex

PREFIX   ?= /usr/local
DOCDIR   ?= $(PREFIX)/share/doc/$(PKG)
INSTALL  ?= install

DISTFILES = Makefile README TESTPLAN.md IDEAS.md $(MANSRC) $(MANUAL) $(PARTS) \
            tests/cli.sh tests/downstream.sh

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

# Derived from SUBDIRS rather than listed again: this line said
# "mz80 mlink" for a release after mlib and utils were added, so `make all`
# quietly built four fifths of the package and the test suite then compared
# 193 objects with a comparator that was not there.
OTHERDIRS = $(filter-out librel,$(SUBDIRS))

all: librel-built
	@for d in $(OTHERDIRS); do $(MAKE) -C $$d || exit 1; done

librel-built:
	@$(MAKE) -C librel

tests:
	@for d in $(SUBDIRS); do $(MAKE) -C $$d tests || exit 1; done

# Each package's suite in turn.  librel's are unit tests and always run; the
# other two compare against the original MACRO-80 and LINK-80 and say so when
# the oracle is not in the tree.
test check: all
	@for d in $(SUBDIRS); do \
	    echo "=== $$d ==="; \
	    $(MAKE) -s -C $$d test || exit 1; \
	done
	@echo "=== cli ==="
	@sh tests/cli.sh
	@echo "=== downstream ==="
	@sh tests/downstream.sh

# Each package instruments and tests itself, then rebuilds clean; the two
# cross-package scripts run last, against the clean binaries.
sanitize-test:
	@for d in $(SUBDIRS); do $(MAKE) -C $$d sanitize-test || exit 1; done
	@# A package's sanitize-test may have rebuilt librel.a instrumented
	@# on its way; start the clean rebuild from nothing.
	@$(MAKE) -s clean
	@$(MAKE) -s all
	@$(MAKE) -s tests
	@sh tests/cli.sh
	@sh tests/downstream.sh

man:
	@for d in $(SUBDIRS); do $(MAKE) -s -C $$d man || exit 1; done

manual: $(MANUAL)

$(MANUAL): $(MANSRC) $(PARTS)
	pdflatex -interaction=batchmode $(MANSRC)
	-makeindex $(PKG)_manual.idx
	pdflatex -interaction=batchmode $(MANSRC)
	pdflatex -interaction=batchmode $(MANSRC)
	@# batchmode hides errors: a run with a hundred of them still exits 0
	@# and still writes a PDF, silently missing whatever it could not set.
	@# This has bitten twice, so the log is checked rather than trusted.
	@if grep -q '^!' $(PKG)_manual.log; then \
	    echo "*** LaTeX errors in $(PKG)_manual.log:"; \
	    grep -A3 '^!' $(PKG)_manual.log | head -40; \
	    exit 1; \
	fi
	@echo "$(MANUAL): `pdfinfo $(MANUAL) | awk '/^Pages/{print $$2}'` pages, no errors"

docs: manual man

install: all
	@for d in $(SUBDIRS); do $(MAKE) -C $$d install || exit 1; done
	$(INSTALL) -d $(DESTDIR)$(DOCDIR)
	-$(INSTALL) -m 644 $(MANUAL) $(DESTDIR)$(DOCDIR)/$(MANUAL)

uninstall:
	@for d in $(SUBDIRS); do $(MAKE) -C $$d uninstall || exit 1; done
	rm -f $(DESTDIR)$(DOCDIR)/$(MANUAL)

# The tarball ships the manual PDF so a reader without LaTeX still gets
# documentation.
dist: $(MANUAL)
	rm -rf $(DISTDIR)
	mkdir -p $(DISTDIR)/manual
	for f in $(DISTFILES); do \
	    mkdir -p $(DISTDIR)/`dirname $$f`; \
	    cp $$f $(DISTDIR)/$$f; \
	done
	for d in $(SUBDIRS); do \
	    mkdir -p $(DISTDIR)/$$d/tests; \
	    $(MAKE) -s -C $$d distfiles DESTBASE=../$(DISTDIR)/$$d || exit 1; \
	done
	tar czf $(DISTDIR).tar.gz $(DISTDIR)
	rm -rf $(DISTDIR)

# Copy this package's files into a staging tree, for the reconstruction's
# dist target, which vendors REL_tools inside its own tarball because
# turbotools needs it to build.
distfiles: $(MANUAL)
	@for f in $(DISTFILES); do \
	    mkdir -p $(DESTBASE)/`dirname $$f`; \
	    cp $$f $(DESTBASE)/$$f; \
	done
	@for d in $(SUBDIRS); do \
	    mkdir -p $(DESTBASE)/$$d/tests; \
	    $(MAKE) -s -C $$d distfiles DESTBASE=../$(DESTBASE)/$$d || exit 1; \
	done

clean:
	@for d in $(SUBDIRS); do $(MAKE) -C $$d clean; done
	rm -f *.aux *.log *.idx *.ilg *.ind *.toc *.out core *.core

distclean: clean
	rm -f $(MANUAL) $(DISTDIR).tar.gz

.PHONY: all librel-built tests test check sanitize-test man manual docs \
        install uninstall dist distfiles clean distclean
