# ---------------------------------------------------------------------------
# mlink -- a native LINK-80 compatible linker for the Microsoft REL format
#
# $Id: Makefile,v 1.4 2026/11/10 18:55:23 rec Exp $
#
# Author:  Robert Cousins
# Purpose: Build the linker, its test programs, its man page and its LaTeX
#          manual, and roll a release tarball.
#
# The release contract: unpack mlink-<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 linking core is NOT in this package.  It comes from librel, which
#       mlink, mz80, gen and the reconstruction tools all share, so there is
#       exactly one implementation of the REL format in the project.  LIBREL
#       points at it and can be overridden; a release tarball carries a copy.
#
# Versions:
#   1.3  2026-11-10  Robert Cousins  Version 1.3: reads through rel_slurp(); an
#                                    empty object is refused.
#   1.2  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.0  2026-09-01  Robert Cousins  Initial version.
# ---------------------------------------------------------------------------

PKG      = mlink
VERSION  = 1.3
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.  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
DOCDIR     ?= $(PREFIX)/share/doc/$(PKG)

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

PROG     = mlink
HDRS     = mlink.h
SRCS     = main.c layout.c libsearch.c map.c
OBJS     = $(SRCS:.c=.o)
RELLIB   = $(LIBREL)/librel.a

TESTS    =
MANS     = mlink.1

DISTFILES = Makefile README $(HDRS) $(SRCS) $(MANS) \
            tests/run.sh tests/l80cmp.sh

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

all: $(PROG)

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

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

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

# ----- 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 $@ $<

# mlink's own tests are behavioural: what matters about a linker is the image
# it produces, which is checked by linking and looking, not by calling its
# functions.  The unit tests live in librel, where the functions are.
tests:

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

sanitize-test:
	$(MAKE) clean
# The behavioural tests drive mz80 to assemble their inputs.  mz80's own
# sanitize-test ends by cleaning its tree, so by the time this target runs
# the assembler is gone and the tests would quietly skip themselves.  Build
# it back -- unsanitized is fine, here it is only an input generator.
	$(MAKE) -C ../mz80
	$(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/mlink.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) $(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
