# ---------------------------------------------------------------------------
# librel -- the Microsoft REL (LINK-80 compatible) object format library
#
# $Id: Makefile,v 1.5 2026/11/10 18:55:23 rec Exp $
#
# Author:  Robert Cousins
# Purpose: Build the one implementation of the REL object format used by
#          everything in this project: the reconstruction tools (relmap,
#          relir, relcmp, relfix, serial, package, disasm, gen), the assembler
#          mz80, and the linker mlink.
#
#          The library is three layers.  rel.h and relread/relwrite are the
#          codec: a bit-level reader and its strict inverse over one shared
#          item table.  irload is the loader: it turns an item stream into a
#          structured module -- segments, publics, externals, COMMON blocks.
#          rellink is the linking core: a global symbol table, an image, chain
#          resolution and the output writers.
#
# The release contract: unpack librel-<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:    Every link rule here passes LDFLAGS, the test programs included.
#          Omitting it from the tests/% rule builds a suite that ignores what
#          the caller asked for -- which is invisible on a native build and
#          fatal on a cross-compiled one, where "make LDFLAGS=-static" then
#          yields dynamic test binaries the target cannot load.
#
# Versions:
#   1.4  2026-11-10  Robert Cousins  Version 1.4: rel_slurp(); the loader
#                                    refuses an empty file and a COMMON
#                                    reference with no block.
#   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; found by
#                                    cross-compiling the suite for riscv64.
#   1.0  2026-09-01  Robert Cousins  Initial version, from tools/.
# ---------------------------------------------------------------------------

PKG      = librel
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.  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)
CPPFLAGS ?= -I.
LDFLAGS  ?=
LIBS     ?=
AR       ?= ar
RANLIB   ?= ranlib
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
LIBDIR     ?= $(PREFIX)/lib
INCDIR     ?= $(PREFIX)/include
MANDIR     ?= $(PREFIX)/share/man
DOCDIR     ?= $(PREFIX)/share/doc/$(PKG)

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

LIB      = librel.a
HDRS     = rel.h irload.h rellink.h
SRCS     = relread.c relwrite.c irload.c rellink.c
OBJS     = $(SRCS:.c=.o)

TESTS    = tests/t_rel tests/t_link
MANS     = librel.3

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

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

all: $(LIB)

$(LIB): $(OBJS)
	$(AR) rc $@ $(OBJS)
	$(RANLIB) $@

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

# A pattern rule, so that a test depends on ITS OWN SOURCE.  Listing only the
# library, 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 $(LIB) $(HDRS)
	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $< $(LIB) $(LIBS) \
	    $(PLATFORM_LIBS)

test check: $(LIB) 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: $(LIB)
	$(INSTALL) -d $(DESTDIR)$(LIBDIR)
	$(INSTALL) -m 644 $(LIB) $(DESTDIR)$(LIBDIR)/$(LIB)
	$(INSTALL) -d $(DESTDIR)$(INCDIR)
	$(INSTALL) -m 644 $(HDRS) $(DESTDIR)$(INCDIR)/
	$(INSTALL) -d $(DESTDIR)$(MANDIR)/man3
	$(INSTALL) -m 644 $(MANS) $(DESTDIR)$(MANDIR)/man3/

uninstall:
	rm -f $(DESTDIR)$(LIBDIR)/$(LIB)
	for h in $(HDRS); do rm -f $(DESTDIR)$(INCDIR)/$$h; done
	for m in $(MANS); do rm -f $(DESTDIR)$(MANDIR)/man3/$$m; done


# 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 $(LIB) $(OBJS) $(TESTS) core *.core
	rm -f *.aux *.log *.idx *.ilg *.ind *.toc *.out

distclean: clean

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