# ---------------------------------------------------------------------------
# mlib -- a native LIB-80 compatible librarian for the Microsoft REL format
#
# $Id: Makefile,v 1.3 2026/11/10 18:55:23 rec Exp $
#
# Author:  Robert Cousins
# Purpose: Build the librarian, its man page, and the files it contributes
#          to a release tarball.
#
#          mlib is the third leg of the Microsoft triad.  mz80 is M80 and
#          mlink is L80; without a librarian the package could search
#          libraries it had no way to create, which is an odd shape for a
#          toolchain that claims to replace all of M80.COM, L80.COM and
#          LIB-80.
#
# Depends: ../librel (librel.a and its headers); a C compiler accepting
#          -std=c2x (gcc 13 or clang 19); libc.
#
# Note:    A library is NOT a plain concatenation of REL files.  Each module
#          ends with an end-program item (14) and only the last carries an
#          end-file item (15); joining them naively produces a file every
#          linker stops reading after the first module.  mlib.c does it
#          properly, which is the reason this program exists rather than a
#          line of shell using cat.
#
# Versions:
#   1.4  2026-11-10  Robert Cousins  Version 1.4: inputs are read before the
#                                    output is created.
#   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-02  Robert Cousins  Initial version.  The program is the
#                                    reconstruction's PACKAGE, renamed and
#                                    given proper UNIX options.
# ---------------------------------------------------------------------------

PKG      = mlib
VERSION  = 1.4

# ----- toolchain -----------------------------------------------------------
# -std=c2x is the newest standard accepted by BOTH gcc 13 (Linux) and
# clang 19 (FreeBSD); gcc 13 rejects -std=c23.  Every .c 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)
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

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

PROG     = mlib
SRCS     = mlib.c
OBJS     = $(SRCS:.c=.o)
RELLIB   = $(LIBREL)/librel.a
HDRS     = $(LIBREL)/rel.h

MANS     = mlib.1

DISTFILES = Makefile $(SRCS) $(MANS) tests/run.sh

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

all: $(PROG)

$(RELLIB):
	$(MAKE) -C $(LIBREL) librel.a

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

$(OBJS): $(HDRS)

# ----- 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): $(STAMP)

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

tests:

test check: $(PROG)
	@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 $(MANS) $(DESTDIR)$(MANDIR)/man1/

uninstall:
	rm -f $(DESTDIR)$(BINDIR)/$(PROG)
	rm -f $(DESTDIR)$(MANDIR)/man1/mlib.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) core *.core

distclean: clean

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