# ---------------------------------------------------------------------------
# REL_tools utilities -- inspect, compare and repair Microsoft REL files
#
# $Id: Makefile,v 1.3 2026/11/10 18:55:22 rec Exp $
#
# Author:  Robert Cousins
# Purpose: Build the four inspection utilities -- relmap, relir, relcmp and
#          relfix -- their man pages, and the files they contribute to a
#          release tarball.
#
#          These are the tools that make the object format actionable rather
#          than merely documented.  Part I of the manual describes a bit
#          stream in which nothing is byte-aligned; relmap is what lets a
#          reader see one, and relcmp is what lets two be compared without
#          the answer being "differs at byte 38", which for a bit stream
#          says nothing at all.
#
# Depends: ../librel (librel.a and its headers); a C compiler accepting
#          -std=c2x (gcc 13 or clang 19); libc.
#
# Note:    Each utility is one translation unit that links the shared
#          library, so there is no per-program header and the pattern rule
#          below is the whole build.  They are applications, so they use
#          err(3) and exit; the decoder they drive is a library and does
#          neither.
#
# Versions:
#   1.4  2026-11-10  Robert Cousins  Version 1.4: rel_slurp(); relmap refuses a
#                                    file with no module.
#   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 four utilities
#                                    move here from the reconstruction's
#                                    tools/ directory.
# ---------------------------------------------------------------------------

PKG      = utils
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 -------------------------------------------------------

PROGS    = relmap relir relcmp relfix
SRCS     = relmap.c relir.c relcmp.c relfix.c
OBJS     = $(SRCS:.c=.o)
RELLIB   = $(LIBREL)/librel.a
HDRS     = $(LIBREL)/rel.h $(LIBREL)/irload.h

MANS     = relmap.1 relir.1 relcmp.1 relfix.1

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

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

all: $(PROGS)

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

$(PROGS): %: %.o $(RELLIB)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $@.o $(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 $@ $<

# These are applications, and what matters about them is what they print.
# The unit tests for the decoder they drive live in librel, where the
# functions are; tests/run.sh checks the programs behave.
tests:

test check: $(PROGS)
	@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: $(PROGS)
	$(INSTALL) -d $(DESTDIR)$(BINDIR)
	@for p in $(PROGS); do \
	    $(INSTALL) -m 755 $$p $(DESTDIR)$(BINDIR)/$$p || exit 1; \
	done
	$(INSTALL) -d $(DESTDIR)$(MANDIR)/man1
	$(INSTALL) -m 644 $(MANS) $(DESTDIR)$(MANDIR)/man1/

uninstall:
	@for p in $(PROGS); do rm -f $(DESTDIR)$(BINDIR)/$$p; done
	@for m in $(MANS); do rm -f $(DESTDIR)$(MANDIR)/man1/$$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 $(PROGS) $(OBJS) core *.core

distclean: clean

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