# ---------------------------------------------------------------------------
# libllm -- a C library for invoking a locally hosted language model.
#
# $Id: Makefile,v 1.2 2026/08/21 16:15:54 rec Exp $
#
# Author:  Robert Cousins
# Purpose: Build the library, the examples, the test programs, the man
#          pages and the LaTeX manual, and roll a release tarball.
#
# The release contract: unpack libllm-<version>.tar.gz in an empty
# directory and type "make ; sudo make install".  There is no configure
# script; the uname block and pkg-config are the whole of the automatic
# configuration this package needs.
#
# Versions:
#   1.0  2026-08-21  Robert Cousins  Initial version.
# ---------------------------------------------------------------------------

PKG      = libllm
VERSION  = 1.1.0
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 itself, 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  ?=
AR       ?= ar
RANLIB   ?= ranlib
INSTALL  ?= install

CURL_CFLAGS := $(shell pkg-config --cflags libcurl 2>/dev/null || \
                       curl-config --cflags 2>/dev/null)
CURL_LIBS   := $(shell pkg-config --libs libcurl 2>/dev/null || \
                       curl-config --libs 2>/dev/null || echo -lcurl)

# ----- platform ------------------------------------------------------------

UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),FreeBSD)
    NROFF ?= mandoc
else
    NROFF ?= groff -mandoc
endif

# libm for isnan(3) and the cosine check in t_embed.
LIBS = $(CURL_LIBS) -lm

PREFIX  ?= /usr/local
BINDIR  ?= $(PREFIX)/bin
LIBDIR  ?= $(PREFIX)/lib
INCDIR  ?= $(PREFIX)/include
MANDIR  ?= $(PREFIX)/share/man
DOCDIR  ?= $(PREFIX)/share/doc/$(PKG)

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

LIB      = libllm.a
HDRS     = llm.h
LIBSRCS  = llm_json.c llm_err.c llm_chat.c llm_reply.c llm_conn.c \
           llm_ollama.c llm_openai.c llm_embed.c
LIBOBJS  = $(LIBSRCS:.c=.o)

EXAMPLES = examples/ask examples/chat examples/tools examples/structured \
           examples/embed

OFFLINE  = tests/t_json tests/t_chat
ONLINE   = tests/t_send tests/t_tools tests/t_embed tests/t_openai
TESTS    = $(OFFLINE) $(ONLINE)

MANS     = llm.3
MANUAL   = $(PKG)_manual.pdf

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

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

all: lib examples tests

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

lib: $(LIB)

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

%.o: %.c $(HDRS) llm_priv.h
	$(CC) $(CPPFLAGS) $(CURL_CFLAGS) $(CFLAGS) -c -o $@ $<

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

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

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

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

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

docs: man manual

man: $(MANS)
	@for m in $(MANS); do $(NROFF) -Tps $$m > $$m.ps || exit 1; 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 -------------------------------------------------------------
# offline-test needs no server and runs anywhere; test needs one.

offline-test: $(OFFLINE)
	@fail=0; for t in $(OFFLINE); do ./$$t || fail=1; done; exit $$fail

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

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 $(MANS)
	$(INSTALL) -d $(DESTDIR)$(LIBDIR) $(DESTDIR)$(INCDIR) \
	              $(DESTDIR)$(MANDIR)/man3 $(DESTDIR)$(DOCDIR)
	$(INSTALL) -m 644 $(LIB) $(DESTDIR)$(LIBDIR)
	$(INSTALL) -m 644 $(HDRS) $(DESTDIR)$(INCDIR)
	$(INSTALL) -m 644 $(MANS) $(DESTDIR)$(MANDIR)/man3
	@test -f $(MANUAL) && $(INSTALL) -m 644 $(MANUAL) $(DESTDIR)$(DOCDIR) || true

uninstall:
	rm -f $(DESTDIR)$(LIBDIR)/$(LIB)
	rm -f $(DESTDIR)$(INCDIR)/$(HDRS)
	rm -f $(DESTDIR)$(MANDIR)/man3/$(MANS)
	rm -rf $(DESTDIR)$(DOCDIR)

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

clean:
	rm -f $(LIBOBJS) $(LIB) $(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 -------------------------------------------------------------
# The manual is built first so the tarball ships the PDF: a reader without
# LaTeX still gets the documentation.

dist: $(MANUAL)
	rm -rf $(DISTDIR) $(DISTDIR).tar.gz
	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:  tar xzf $(DISTDIR).tar.gz && cd $(DISTDIR) && make && make test"
