# SPDX-License-Identifier: GPL-2.0-only
# hz has been C++ since 2026-11-16: ONE binary with nvcm and nvjpg linked
# in and libzstd/liblzma/libz in process. The retired Python implementation
# stays in the tree as the v1 format reference (src/nvz.py) but is built by
# nothing and called by nothing. The portable cross-platform tool is
# portable/nvzpy.py — pure stdlib Python, needs no build at all.
#
# -O3 for the C codecs: nvcm is the hottest loop in mode `small`, and -O3
# measured 14 % faster with byte-identical output (same for nvjpg).
CFLAGS ?= -O3 -Wall -Wextra -Wno-implicit-fallthrough
CXXFLAGS ?= -std=c++17 -O2 -Wall -Wextra
CPP_SOURCES = src/cpp/main.cpp src/cpp/cli.cpp src/cpp/archive.cpp src/cpp/codec.cpp \
              src/cpp/transform.cpp src/cpp/tree.cpp src/cpp/json.cpp \
              src/cpp/worker.cpp src/cpp/util.cpp
CPPOBJ = $(CPP_SOURCES:src/cpp/%.cpp=src/cpp/%.o)

# The GPU part is only built where the machine has the prerequisites. The
# Vulkan headers and glslc are not everywhere, and the rest of hz must
# build without them.
HAS_GPU := $(shell [ -f /usr/include/vulkan/vulkan.h ] && command -v glslc >/dev/null 2>&1 && echo yes)
SPV = src/band.spv src/blake2b.spv src/matcher.spv

all: src/hz src/nvjpg src/nvcm $(if $(HAS_GPU),gpu,no-gpu)

# --- hz: the C++ binary -----------------------------------------------------
# Dependencies are generated by the compiler (-MMD -MP) and included below.
# The old rule read `%.o: %.cpp src/cpp/hz.hpp` and therefore tracked ONE
# header. A field added to `struct PackOpts` in archive.hpp then rebuilt
# main.o and archive.o but not cli.o, and the binary was linked from object
# files that disagreed about the struct's layout: `hz -t` crashed with
# SIGSEGV while `hz pack` worked. The fault was found by prov/slumpprov.py
# on 2026-11-18 and was invisible to every compiler warning — which is why
# the explanation lives here and not in a commit line.
src/cpp/%.o: src/cpp/%.cpp
	$(CXX) $(CXXFLAGS) -MMD -MP -c $< -o $@

-include $(CPPOBJ:.o=.d)

# nvcm.c and nvjpg.c are compiled as C and linked in through wrappers that
# swap out their main. They are the same files that build the standalone
# binaries.
src/cpp/cwrap_nvcm.o: src/cpp/cwrap_nvcm.c src/nvcm.c
	$(CC) $(CFLAGS) -c $< -o $@
src/cpp/cwrap_nvjpg.o: src/cpp/cwrap_nvjpg.c src/nvjpg.c
	$(CC) $(CFLAGS) -c $< -o $@

src/hz: $(CPPOBJ) src/cpp/cwrap_nvcm.o src/cpp/cwrap_nvjpg.o
	$(CXX) $(CXXFLAGS) -o $@ $^ -lzstd -llzma -lz -lpthread

src/nvjpg: src/nvjpg.c
	$(CC) $(CFLAGS) -o $@ $<

gpu: src/nvgpu $(SPV)

no-gpu:
	@echo "skipping the gpu part: Vulkan headers or glslc missing (apt install libvulkan-dev glslc)"

src/nvgpu: src/nvgpu.c
	$(CC) $(CFLAGS) -o $@ $< -lvulkan -lpthread

# The shaders compile to SPIR-V and are read by nvgpu at run time, from the
# same directory as the binary.
src/%.spv: src/%.comp
	glslc -O -fshader-stage=compute $< -o $@

# The blake2b kernel is generated — unrolled, to avoid scratch memory.
src/blake2b.comp: src/gen-blake2b.py
	python3 $<

test: src/nvjpg src/nvcm
	@if [ -d korpus/kod ]; then ./src/hz test korpus/kod; \
	 else echo "korpus/kod absent (release tarball) — self-testing on src/"; \
	      ./src/hz test src; fi
	@if [ -d korpus/foto ]; then ./prov/kontroll-jpg.sh korpus/foto; \
	 else echo "korpus/foto absent — jpeg check skipped"; fi
	@if [ -x src/nvgpu ]; then ./src/nvgpu prov --tyst && echo "nvgpu: tests passed"; \
	 else echo "nvgpu not built (make gpu) — gpu tests skipped"; fi

# The installation puts the three tools and their man pages under PREFIX.
PREFIX ?= /usr/local
BINDIR  = $(DESTDIR)$(PREFIX)/bin
MANDIR  = $(DESTDIR)$(PREFIX)/share/man/man1

install: all
	install -d $(BINDIR) $(MANDIR)
	install -m 755 src/hz src/nvcm src/nvjpg $(BINDIR)
	install -m 644 man/nvcm.1 man/hz.1 man/nvjpg.1 $(MANDIR)
	@echo "installed under $(PREFIX). Try: man hz"

uninstall:
	rm -f $(BINDIR)/nvcm $(BINDIR)/nvjpg $(BINDIR)/hz
	rm -f $(MANDIR)/nvcm.1 $(MANDIR)/hz.1 $(MANDIR)/nvjpg.1

# The man pages read straight from the tree without installing:
#   man -l man/hz.1
man: man/nvcm.1 man/hz.1 man/nvjpg.1
	@for m in $^; do man --warnings -E UTF-8 -l $$m >/dev/null || exit 1; done
	@echo "man pages are clean against groff's warnings"

pdf: rapport/hz-rapport.pdf

rapport/hz-rapport.pdf: rapport/bygg-tryck.py $(wildcard resultat/*.json)
	bash rapport/bygg-pdf.sh

# Release tarball from git, the artifact a distribution package builds from.
# git archive guarantees the tarball holds exactly what is committed.
VERSION ?= 2.0
dist:
	mkdir -p dist
	git archive --format=tar.gz --prefix=hz-$(VERSION)/ \
	    -o dist/hz-$(VERSION).tar.gz HEAD
	@ls -l dist/hz-$(VERSION).tar.gz

clean:
	rm -f src/nvjpg src/nvcm src/nvgpu src/hz src/cpp/*.o src/cpp/*.d $(SPV)
	rm -rf dist .bygg-enfil

# Swedish target names remain as aliases.
prov: test
installera: install
avinstallera: uninstall
rent: clean

.PHONY: dist all test prov pdf clean rent gpu no-gpu install installera uninstall avinstallera man

src/nvcm: src/nvcm.c
	$(CC) $(CFLAGS) -o $@ $<
