#
#
#              Copyright (C) 2021-2026 by Robert E. Cousins
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 
#
#	$Id: Makefile,v 1.6 2026/11/28 20:57:37 rec Exp $
#
# File:    Makefile
# Author:  Robert Cousins
#
# Purpose: Builds libsls.a, the examples, the test programs, the man page and
#          the LaTeX manual, runs the tests, installs, and rolls a tarball.
#
#              make ; sudo make install
#
# Depends: A make with ?= and suffix rules: GNU make or FreeBSD's make.  A C11
#          compiler.  pdflatex and makeindex for `make manual' only; the tarball
#          ships the PDF.  python3 for `make test-exact' only.  POSIX
#          threads for sls_test_thread and sls_example_merge.  A C++ compiler
#          is used, when there is one, only to check that sls.h is C++.
#
# Note:    There is no configure script and nothing here depends on the
#          platform.  Keep it that way: no GNU make conditionals, because plain
#          `make' on FreeBSD must work.  Variables have the same names as in
#          2.0.0, except that the default MANDIR moved to share/man, the only
#          one of the two that exists on FreeBSD 15.
#
# Versions:
#   1.0    2021-08-15  Robert Cousins  Initial version.
#   2.0.0  2025-11-05  Robert Cousins  test, release, install-man targets.
#   2.1.0  2026-11-04  Robert Cousins  Added lib, examples, tests, docs, man,
#                                      manual, check, sanitize-test, tsan-test,
#                                      test-exact, bench, distclean and dist;
#                                      test now runs the whole suite; removed
#                                      the rule for sls_test.c, which is not in
#                                      the package; default MANDIR is now
#                                      $(PREFIX)/share/man/man3.
#   2.1.1  2026-11-05  Robert Cousins  Added sls_test_nist.
#   2.2.0  2026-11-28  Robert Cousins  sls_infer.o, sls_deming.o, sls_merge.o and
#                                      sls_priv.h; examples sls_example_calib
#                                      and sls_example_merge; sls_test_priv;
#                                      test checks the header's version and
#                                      compiles it as C++; test-inline-fma.

# Version
PKG		=	libsls
VERSION		=	2.2.0
DISTDIR		=	$(PKG)-$(VERSION)

# Installation directories
PREFIX		?=	/usr/local
LIBDIR		?=	$(PREFIX)/lib
INCLUDEDIR	?=	$(PREFIX)/include
MANDIR		?=	$(PREFIX)/share/man/man3
DOCDIR		?=	$(PREFIX)/share/doc/$(PKG)

# Compiler settings
CC		?=	cc
CXX		?=	c++
AR		?=	ar
PYTHON		?=	python3
CSTD		?=	-std=c17
CWARN		?=	-Wall -Wextra -Wshadow -Wpointer-arith -Wcast-qual \
			-Wstrict-prototypes
COPT		?=	-g -O2
# Not ?=: FreeBSD's sys.mk presets CFLAGS, which would silently drop all three
CFLAGS		=	$(CSTD) $(CWARN) $(COPT)
LDFLAGS		?=
LIBS		=	libsls.a -lm

# Installation
INSTALL		?=	install
INSTALL_DATA	=	$(INSTALL) -m 644

LIBOBJS		=	sls.o sls_dump.o sls_init.o sls_sample.o sls_stats.o \
			sls_infer.o sls_deming.o sls_merge.o
EXAMPLES	=	sls_example sls_example_calib sls_example_merge
TESTS		=	sls_test_new sls_test_header sls_test_api sls_test_regress \
			sls_test_diff sls_test_thread sls_test_nist sls_test_priv
TOOLS		=	sls_bench sls_accuracy
MANUAL		=	libsls_manual.pdf

DISTFILES	=	$(LIBOBJS:.o=.c) sls.h sls_priv.h sls_example.c \
			sls_example_calib.c sls_example_merge.c \
			sls_test_new.c sls_test_header.c sls_test_api.c \
			sls_test_regress.c sls_test_diff.c sls_test_thread.c \
			sls_test_exact.py sls_test_nist.c sls_nist_data.h \
			sls_test_priv.c \
			sls_bench.c sls_accuracy.c bench.h \
			sls.3 libsls_manual.tex $(MANUAL) Makefile \
			README.md LICENSE CHANGELOG.md MIGRATION_GUIDE.md \
			IMPROVEMENTS.md RELEASE_v$(VERSION).md RELEASE_v2.1.1.md \
			RELEASE_v2.1.0.md VERSION

all:	lib examples tests tools

lib:	libsls.a

examples:	$(EXAMPLES)

tests:	$(TESTS)

tools:	$(TOOLS)

libsls.a:	$(LIBOBJS)
	rm -f libsls.a
	$(AR) -rc libsls.a $(LIBOBJS)

$(LIBOBJS):	sls.h sls_priv.h

.SUFFIXES:	.c .o

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

sls_example:	sls_example.c sls.h libsls.a
	$(CC) $(CFLAGS) $(LDFLAGS) -o sls_example sls_example.c $(LIBS)

sls_example_calib:	sls_example_calib.c sls.h libsls.a
	$(CC) $(CFLAGS) $(LDFLAGS) -o sls_example_calib sls_example_calib.c $(LIBS)

sls_example_merge:	sls_example_merge.c sls.h libsls.a
	$(CC) $(CFLAGS) -pthread $(LDFLAGS) -o sls_example_merge \
	    sls_example_merge.c $(LIBS)

sls_test_new:	sls_test_new.c sls.h libsls.a
	$(CC) $(CFLAGS) $(LDFLAGS) -o sls_test_new sls_test_new.c $(LIBS)

sls_test_header:	sls_test_header.c sls.h libsls.a Makefile
	$(CC) $(CFLAGS) -DSLS_EXPECT_VERSION='"$(VERSION)"' $(LDFLAGS) \
	    -o sls_test_header sls_test_header.c $(LIBS)

sls_test_api:	sls_test_api.c sls.h libsls.a
	$(CC) $(CFLAGS) $(LDFLAGS) -o sls_test_api sls_test_api.c $(LIBS)

sls_test_regress:	sls_test_regress.c sls.h libsls.a
	$(CC) $(CFLAGS) $(LDFLAGS) -o sls_test_regress sls_test_regress.c $(LIBS)

sls_test_diff:	sls_test_diff.c sls.h libsls.a
	$(CC) $(CFLAGS) $(LDFLAGS) -o sls_test_diff sls_test_diff.c $(LIBS)

sls_test_nist:	sls_test_nist.c sls_nist_data.h sls.h libsls.a
	$(CC) $(CFLAGS) $(LDFLAGS) -o sls_test_nist sls_test_nist.c $(LIBS)

sls_test_priv:	sls_test_priv.c sls_priv.h sls.h
	$(CC) $(CFLAGS) $(LDFLAGS) -o sls_test_priv sls_test_priv.c -lm

sls_test_thread:	sls_test_thread.c sls.h libsls.a
	$(CC) $(CFLAGS) -pthread $(LDFLAGS) -o sls_test_thread sls_test_thread.c $(LIBS)

sls_bench:	sls_bench.c sls.h bench.h libsls.a
	$(CC) $(CFLAGS) $(LDFLAGS) -o sls_bench sls_bench.c $(LIBS)

sls_accuracy:	sls_accuracy.c sls.h libsls.a
	$(CC) $(CFLAGS) $(LDFLAGS) -o sls_accuracy sls_accuracy.c $(LIBS)

# Testing.  Every program runs even after one fails; the exit status says
# whether any did.  sls_test_new is wordy, so its output is shown only on failure.
test:	tests
	@fail=0; \
	if ./sls_test_new > sls_test_new.out 2>&1; then \
	    tail -5 sls_test_new.out | sed -n 's/^  Passed:/sls_test_new: passed:/p'; \
	else \
	    cat sls_test_new.out; echo "sls_test_new: FAILED"; fail=1; \
	fi; \
	if command -v $(CXX) >/dev/null 2>&1; then \
	    if $(CXX) -x c++ -Wall -Wextra -fsyntax-only sls_test_header.c; \
	    then echo "sls_test_header: compiles as C++"; \
	    else echo "sls_test_header: FAILED as C++"; fail=1; fi; \
	fi; \
	for t in sls_test_header sls_test_api sls_test_regress \
	    sls_test_diff sls_test_thread sls_test_nist sls_test_priv; do \
	    ./$$t > $$t.out 2>&1 && st=0 || st=1; \
	    if [ $$st -ne 0 ]; then cat $$t.out; fail=1; \
	    else tail -2 $$t.out; fi; \
	done; \
	if [ $$fail -eq 0 ]; then echo "make test: all passed"; \
	else echo "make test: FAILED"; fi; \
	exit $$fail

# Exact rational arithmetic, and numpy if installed.  Needs python3.
test-exact:	sls_test_diff
	./sls_test_diff -e sls_cases.txt > /dev/null
	$(PYTHON) sls_test_exact.py sls_cases.txt
	rm -f sls_cases.txt

check:	test test-exact test-inline-fma

# The inline exact product used where fma(3) is slow (see sls_priv.h), tested
# here on any platform.
test-inline-fma:
	$(MAKE) clean
	$(MAKE) COPT="$(COPT) -DSLS_INLINE_FMA" test
	$(MAKE) clean

# -fno-sanitize-recover makes UBSan findings fail the run, not just print.
SANFLAGS	=	-g -O1 -fsanitize=address,undefined \
			-fno-sanitize-recover=all -fno-omit-frame-pointer

sanitize-test:
	$(MAKE) clean
	$(MAKE) COPT="$(SANFLAGS)" LDFLAGS="-fsanitize=address,undefined" test
	$(MAKE) clean

# Where ASLR upsets TSan (WSL2): make tsan-test TSAN_RUN="setarch -R"
TSAN_RUN	?=

tsan-test:
	$(MAKE) clean
	$(MAKE) COPT="-g -O1 -fsanitize=thread" LDFLAGS="-fsanitize=thread" \
	    sls_test_thread
	$(TSAN_RUN) ./sls_test_thread
	$(MAKE) clean

bench:	sls_bench
	./sls_bench

# Documentation
docs:	man manual

man:	sls.3
	@if command -v mandoc >/dev/null 2>&1; then \
	    mandoc -T lint -W warning sls.3 || exit 1; \
	elif command -v groff >/dev/null 2>&1; then \
	    groff -mdoc -Tutf8 -ww sls.3 > /dev/null || exit 1; \
	else echo "no mandoc or groff: man page not checked"; fi

manual:	$(MANUAL)

$(MANUAL):	libsls_manual.tex
	pdflatex -interaction=nonstopmode -halt-on-error libsls_manual.tex > /dev/null
	makeindex libsls_manual.idx > /dev/null 2>&1
	pdflatex -interaction=nonstopmode -halt-on-error libsls_manual.tex > /dev/null
	pdflatex -interaction=nonstopmode -halt-on-error libsls_manual.tex > /dev/null

# Installation targets
install:	install-lib install-headers install-man install-doc

install-lib:	libsls.a
	mkdir -p $(DESTDIR)$(LIBDIR)
	$(INSTALL_DATA) libsls.a $(DESTDIR)$(LIBDIR)/

install-headers:	sls.h
	mkdir -p $(DESTDIR)$(INCLUDEDIR)
	$(INSTALL_DATA) sls.h $(DESTDIR)$(INCLUDEDIR)/

install-man:	sls.3
	mkdir -p $(DESTDIR)$(MANDIR)
	$(INSTALL_DATA) sls.3 $(DESTDIR)$(MANDIR)/

install-doc:
	mkdir -p $(DESTDIR)$(DOCDIR)
	$(INSTALL_DATA) README.md LICENSE RELEASE_v$(VERSION).md $(DESTDIR)$(DOCDIR)/
	if [ -f $(MANUAL) ]; then $(INSTALL_DATA) $(MANUAL) $(DESTDIR)$(DOCDIR)/; fi

uninstall:
	rm -f $(DESTDIR)$(LIBDIR)/libsls.a
	rm -f $(DESTDIR)$(INCLUDEDIR)/sls.h
	rm -f $(DESTDIR)$(MANDIR)/sls.3
	rm -f $(DESTDIR)$(DOCDIR)/README.md $(DESTDIR)$(DOCDIR)/LICENSE
	rm -f $(DESTDIR)$(DOCDIR)/RELEASE_v$(VERSION).md
	rm -f $(DESTDIR)$(DOCDIR)/$(MANUAL)
	-rmdir $(DESTDIR)$(DOCDIR) 2>/dev/null

clean:
	rm -f *core *.o *.a *.out sls_cases.txt
	rm -f $(EXAMPLES) $(TESTS) $(TOOLS)
	rm -f *.aux *.log *.toc *.idx *.ind *.ilg

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

# Release packaging.  Builds the manual first, because the tarball ships it.
dist:	$(MANUAL)
	rm -rf $(DISTDIR) $(DISTDIR).tar.gz
	mkdir -p $(DISTDIR)
	cp $(DISTFILES) $(DISTDIR)/
	chmod 644 $(DISTDIR)/*
	tar czf $(DISTDIR).tar.gz $(DISTDIR)
	rm -rf $(DISTDIR)
	if command -v sha256sum >/dev/null 2>&1; then \
	    sha256sum $(DISTDIR).tar.gz > $(DISTDIR).tar.gz.sha256; \
	else \
	    sha256 -r $(DISTDIR).tar.gz > $(DISTDIR).tar.gz.sha256; \
	fi
	@echo "release: $(DISTDIR).tar.gz"

release:	dist

.PHONY: all lib examples tests tools test test-exact test-inline-fma check \
	sanitize-test \
	tsan-test bench docs man manual install install-lib install-headers \
	install-man install-doc uninstall clean distclean dist release
