# ---------------------------------------------------------------------------
# REL_tools -- a toolchain for the Microsoft REL object format
#
# $Id: Makefile,v 1.1 2026/09/02 14:49:35 rec Exp $
#
# 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 mz80 and mlink, which link
#       against it; "make -j" is safe because the dependency is expressed, but
#       the SUBDIRS list is not merely alphabetical.
#
# Versions:
#   1.1  2026-09-02  Robert Cousins  Initial version; consolidates librel,
#                                    mz80 and mlink 1.0.
# ---------------------------------------------------------------------------

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

SUBDIRS  = librel mz80 mlink

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

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

DISTFILES = Makefile README $(MANSRC) $(MANUAL) $(PARTS)

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

all: librel-built
	@for d in mz80 mlink; 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

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

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)

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)

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 clean distclean
