# ---------------------------------------------------------------------------
# expr -- arithmetic expression compiler and evaluator
#
# Copyright (c) 2026, Robert Cousins.  All rights reserved.
# Redistribution and use are permitted under the three-clause BSD licence
# reproduced in expr.h.
#
# $Id: Makefile,v 1.4 2026/10/12 22:05:48 rec Exp $
#
# Author:  Robert Cousins
# Purpose: Build the static and shared libraries, the demo calculator, the
#          test program, the man page and the LaTeX manual, and roll a
#          release tarball.
#
# The release contract: the user downloads expr-<version>.tar.gz, unpacks it
# in an empty directory, and types
#
#       make ; sudo make install
#
# There is no configure script.  Platform differences are handled by the
# uname-driven block below.
#
# Note:    The test program and the demo link the static library, so that
#          running them exercises the code just built and not whatever
#          libexpr.so happens to be installed; 1.2 linked the shared one
#          without an rpath and ran against a stale /usr/local copy.  The
#          library objects are built -fPIC -fvisibility=hidden once and
#          go into both libraries; only EXPR_API symbols are exported.
#          The shared library's major number changes whenever the ABI
#          does: 1.0 through 1.2 shipped as .so.1, 1.3 as .so.2 and 1.4,
#          whose report structure grew, as .so.3.
#
# Versions:
#   1.0  2025-06-22  Robert Cousins  Initial version.
#   1.3  2026-10-12  Robert Cousins  House-style rewrite: static link for
#                                    tests, dist target without GNU tar
#                                    extensions, sanitize-test, manual.
#   1.4  2026-10-12  Robert Cousins  expr_program.c, expr_bench, soname 3.
# ---------------------------------------------------------------------------

PKG      = expr
VERSION  = 1.4
SOMAJOR  = 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.  Each .c also 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     ?= -lm
AR       ?= ar
RANLIB   ?= ranlib
INSTALL  ?= install

# ----- platform detection --------------------------------------------------

UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
    LDCONFIG = ldconfig
endif
ifeq ($(UNAME_S),FreeBSD)
    LDCONFIG = ldconfig -m $(DESTDIR)$(LIBDIR)
endif
ifeq ($(UNAME_S),Darwin)
    LDCONFIG = true
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      = lib$(PKG).a
SHLIB    = lib$(PKG).so.$(SOMAJOR)
SHLINK   = lib$(PKG).so
HDRS     = expr.h expr_private.h
PUBHDRS  = expr.h
LIBSRCS  = expr_context.c expr_symbol_table.c expr_symbol_batch.c expr_ast.c \
           expr_tokenizer.c expr_parser_core.c expr_parser_validation.c \
           expr_evaluator.c expr_optimizer.c expr_program.c
LIBOBJS  = $(LIBSRCS:.c=.o)

EXAMPLES = examples/demo_calculator examples/expr_bench
TESTS    = tests/test_expr
MANS     = expr.3
MANUAL   = $(PKG)_manual.pdf

DISTFILES = Makefile README.md BUGS.md $(HDRS) $(LIBSRCS) \
            $(MANS) $(PKG)_manual.tex $(MANUAL) \
            $(EXAMPLES:=.c) $(TESTS:=.c) tests/bench.h

.PHONY: all lib shared examples tests docs man manual test check \
        sanitize-test install uninstall clean distclean dist

all: lib shared examples tests docs

# ----- library -------------------------------------------------------------

lib: $(LIB)
shared: $(SHLIB)

$(LIB): $(LIBOBJS)
	$(AR) rcs $@ $(LIBOBJS)
	$(RANLIB) $@

$(SHLIB): $(LIBOBJS)
	$(CC) -shared -Wl,-soname,$(SHLIB) $(LDFLAGS) -o $@ $(LIBOBJS) $(LIBS)
	ln -sf $(SHLIB) $(SHLINK)

%.o: %.c $(HDRS)
	$(CC) $(CPPFLAGS) $(CFLAGS) -fPIC -fvisibility=hidden -c -o $@ $<

# ----- examples and tests --------------------------------------------------

examples: $(EXAMPLES)
tests: $(TESTS)

examples/%: examples/%.c $(LIB)
	$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LIB) $(LIBS)

tests/%: tests/%.c tests/bench.h $(LIB)
	$(CC) $(CPPFLAGS) -Itests $(CFLAGS) $(LDFLAGS) -o $@ $< $(LIB) $(LIBS)

# ----- documentation -------------------------------------------------------

docs: man

# Rendered man pages are a convenience, not a build product; the .3 source
# ships as-is.  groff on Linux, mandoc on FreeBSD -- both read mdoc.
man: $(MANS)
	@for m in $(MANS); do \
	    if command -v groff >/dev/null 2>&1; then \
	        groff -mandoc -Tps $$m > $$m.ps || exit 1; \
	    elif command -v mandoc >/dev/null 2>&1; then \
	        mandoc -Tps $$m > $$m.ps || exit 1; \
	    fi; \
	done

manual: $(MANUAL)

$(MANUAL): $(PKG)_manual.tex
	pdflatex -interaction=batchmode $< >/dev/null
	makeindex $(PKG)_manual.idx >/dev/null 2>&1 || true
	pdflatex -interaction=batchmode $< >/dev/null
	pdflatex -interaction=batchmode $< >/dev/null

# ----- testing -------------------------------------------------------------

test check: tests
	@fail=0; \
	for t in $(TESTS); do \
	    echo "== $$t"; ./$$t || fail=1; \
	done; \
	exit $$fail

# Run the same suite under ASan/UBSan.  Both gcc and clang support these.
sanitize-test:
	$(MAKE) clean
	$(MAKE) COPT="-O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer" \
	        LDFLAGS="-fsanitize=address,undefined" test
	$(MAKE) clean

# ----- install -------------------------------------------------------------

install: lib shared
	$(INSTALL) -d $(DESTDIR)$(LIBDIR) $(DESTDIR)$(INCDIR) \
	              $(DESTDIR)$(MANDIR)/man3 $(DESTDIR)$(DOCDIR)
	$(INSTALL) -m 644 $(LIB) $(DESTDIR)$(LIBDIR)
	$(INSTALL) -m 755 $(SHLIB) $(DESTDIR)$(LIBDIR)
	ln -sf $(SHLIB) $(DESTDIR)$(LIBDIR)/$(SHLINK)
	$(INSTALL) -m 644 $(PUBHDRS) $(DESTDIR)$(INCDIR)
	$(INSTALL) -m 644 $(MANS) $(DESTDIR)$(MANDIR)/man3
	if [ -f $(MANUAL) ]; then $(INSTALL) -m 644 $(MANUAL) $(DESTDIR)$(DOCDIR); fi
	if [ -z "$(DESTDIR)" ]; then $(LDCONFIG) || true; fi

uninstall:
	cd $(DESTDIR)$(LIBDIR) && rm -f $(LIB) $(SHLIB) $(SHLINK)
	cd $(DESTDIR)$(INCDIR) && rm -f $(PUBHDRS)
	cd $(DESTDIR)$(MANDIR)/man3 && rm -f $(MANS)
	rm -rf $(DESTDIR)$(DOCDIR)
	if [ -z "$(DESTDIR)" ]; then $(LDCONFIG) || true; fi

# ----- housekeeping --------------------------------------------------------

clean:
	rm -f $(LIBOBJS) $(LIB) $(SHLIB) $(SHLINK) $(EXAMPLES) $(TESTS) *.o *.ps
	rm -f *.aux *.log *.toc *.out *.idx *.ilg *.ind

distclean: clean
	rm -f $(MANUAL) $(PKG)-*.tar.gz
	rm -rf $(DISTDIR)

# ----- release -------------------------------------------------------------
# Builds the manual first: the tarball ships the PDF so that a user without
# LaTeX can still read the documentation.  No tar extensions are used, so
# the target works with GNU tar and with BSD tar alike.

dist: $(MANUAL)
	rm -rf $(DISTDIR) $(DISTDIR).tar.gz
	mkdir -p $(DISTDIR)/examples $(DISTDIR)/tests
	for f in $(DISTFILES); do \
	    mkdir -p $(DISTDIR)/`dirname $$f`; \
	    cp $$f $(DISTDIR)/$$f || exit 1; \
	done
	tar czf $(DISTDIR).tar.gz $(DISTDIR)
	rm -rf $(DISTDIR)
	@echo "release: $(DISTDIR).tar.gz"
	@echo "verify with: tar xzf $(DISTDIR).tar.gz && cd $(DISTDIR) && make && make test"
