CC = gcc
CFLAGS = -O2 -Wall -Wextra
LDLIBS = -llz
PREFIX = /usr/local

# CUDA (optional): nvcc is found on PATH first; on hot_house it lives
# at /usr/local/cuda/bin/nvcc (not on PATH by default), so fall back
# to that path when it exists. Two CUDA objects share this NVCC:
#   bch_cuda.o   -- CUDA BCH remainder (bch.c dispatches via weak
#                   symbols for large blobs); linked into liblzply.a
#                   and every tool. Without nvcc the build is
#                   unchanged (CPU-only).
#   chase_cuda.o -- CUDA Chase-1 scan for deep repair (t=8 only);
#                   linked into lzply and lzply_deep_repair, with a
#                   CPU fallback via weak references.
# Without nvcc, the *_cuda_stub.o objects are linked instead, so
# the CUDA entry points always resolve -- see the stub comment
# below. The stubs are required on macOS: ld64 does not resolve
# __attribute__((weak)) declarations to NULL the way GNU ld does,
# so a CPU-only build failed at link time with undefined
# _bch_cuda_available/_encode/_decode symbols (and, after the
# chase-wired merge, _chase_cuda_available/_scan1). A stub is
# never built alongside its CUDA object, so the symbols are
# defined exactly once.
# These definitions MUST stay above every rule that references
# $(CUDA_OBJ): make expands prerequisites when the rule is read, so
# a definition below the liblzply.a rule would silently leave
# bch_cuda.o out of the archive. NOTE: bch_cuda.o is still listed
# explicitly on each tool link line below, because the weak
# references from bch.o do not make the linker pull it out of the
# archive on their own. The stub is listed the same way.
NVCC ?= $(shell command -v nvcc 2>/dev/null)
ifeq ($(NVCC),)
NVCC := $(wildcard /usr/local/cuda/bin/nvcc)
endif
CUDA_ARCH ?= sm_86
NVCCFLAGS = -O3 -arch=$(CUDA_ARCH) -Xcompiler -O2
CUDA_OBJ =
CUDA_LIBS =
ifneq ($(NVCC),)
CUDA_OBJ = bch_cuda.o
CUDA_LIBS = -L$(dir $(NVCC))../lib64 -lcudart -lstdc++
endif
CHASE_CUDA_OBJ =
ifneq ($(wildcard $(NVCC)),)
CHASE_CUDA_OBJ = chase_cuda.o
endif

# CPU-only stubs: when nvcc is absent, bch_cuda_stub.o defines the
# three BCH CUDA entry points (available() = 0, encode = 0,
# decode = -2) and chase_cuda_stub.o defines the two Chase CUDA
# entry points (available() = 0, scan1 = -2), so bch.c's and
# deep_repair.c's references always resolve. This is required on
# macOS: ld64 does not resolve __attribute__((weak)) declarations
# to NULL the way GNU ld does, so a CPU-only build failed at link
# time with undefined _bch_cuda_available/_encode/_decode symbols
# (and, after the chase-wired merge, _chase_cuda_available/_scan1).
# A stub is never built alongside its CUDA object, so the symbols
# are defined exactly once.
CUDA_STUB_OBJ =
ifeq ($(NVCC),)
CUDA_STUB_OBJ = bch_cuda_stub.o chase_cuda_stub.o
endif

# lzcsv: standalone high-speed CSV compressor (column transpose + xz).
# Completely separate from the lzply FEC core: own source (lzcsv-src/lzcsv.c),
# own binary, links only liblzma -- no liblzply.a, no liblz, no CUDA.
# Built only when lzma.h is present; skipped cleanly otherwise (e.g. a
# minimal CI image without liblzma-dev), so `make all` never fails for
# lack of it.
LZCSV =
ifneq ($(wildcard /usr/include/lzma.h),)
LZCSV = lzcsv
endif

all: lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair $(LZCSV)

# Shared core: the v5 archive format, the per-entry pipeline
# (lzip -> BCH), header I/O, scan, and filters. The
# command-line tool (lzply), the FUSE filesystem (lzply_mount),
# the statistics tool (lzply_stat) and the heal tool (lzply_heal)
# all link it.
liblzply.a: lzply_lib.o bch.o sha256.o $(CUDA_OBJ) $(CUDA_STUB_OBJ)
	ar rcs $@ $^

lzply_lib.o: lzply_lib.c lzply_lib.h bch.h bch_polys.h sha256.h
	$(CC) $(CFLAGS) -c -o $@ lzply_lib.c

sha256.o: sha256.c sha256.h
	$(CC) $(CFLAGS) -c -o $@ sha256.c

bch.o: bch.c bch.h bch_polys.h
	$(CC) $(CFLAGS) -c -o $@ bch.c

bch_cuda_stub.o: bch_cuda_stub.c bch_cuda.h bch.h
	$(CC) $(CFLAGS) -c -o $@ bch_cuda_stub.c

chase_cuda_stub.o: chase_cuda_stub.c chase_cuda.h bch.h
	$(CC) $(CFLAGS) -c -o $@ chase_cuda_stub.c

# CUDA BCH remainder rules (variables are defined at the top).
ifneq ($(NVCC),)
bch_cuda.o: bch_cuda.cu bch_cuda.h bch.h bch_polys.h
	$(NVCC) -O3 -arch=$(CUDA_ARCH) -Xcompiler -O2 -c -o $@ bch_cuda.cu

bch_cuda_test.o: bch_cuda_test.c bch_cuda.h bch.h
	$(CC) $(CFLAGS) -c -o $@ bch_cuda_test.c

bch_cuda_test: bch_cuda_test.o bch_cuda.o bch.o
	$(NVCC) -arch=$(CUDA_ARCH) -o $@ bch_cuda_test.o bch_cuda.o bch.o

bch_bench.o: bch_bench.c bch.h
	$(CC) $(CFLAGS) -c -o $@ bch_bench.c

bch_bench: bch_bench.o bch.o bch_cuda.o
	$(NVCC) -arch=$(CUDA_ARCH) -o $@ bch_bench.o bch.o bch_cuda.o
else
bch_bench.o: bch_bench.c bch.h
	$(CC) $(CFLAGS) -c -o $@ bch_bench.c

bch_bench: bch_bench.o bch.o $(CUDA_STUB_OBJ)
	$(CC) $(CFLAGS) -o $@ bch_bench.o bch.o $(CUDA_STUB_OBJ)
endif

lzply.o: lzply.c lzply_lib.h bch.h bch_polys.h deep_repair.h repair_cmd.h
	$(CC) $(CFLAGS) -c -o $@ lzply.c

lzply: lzply.o deep_repair.o repair_cmd.o liblzrs.a liblzply.a $(CUDA_OBJ) $(CHASE_CUDA_OBJ) $(CUDA_STUB_OBJ)
ifeq ($(CHASE_CUDA_OBJ),)
	$(CC) $(CFLAGS) -o $@ lzply.o deep_repair.o repair_cmd.o liblzrs.a $(CUDA_OBJ) $(CUDA_STUB_OBJ) liblzply.a $(LDLIBS) $(CUDA_LIBS)
else
	$(NVCC) -arch=$(CUDA_ARCH) -o $@ lzply.o deep_repair.o repair_cmd.o liblzrs.a $(CUDA_OBJ) $(CUDA_STUB_OBJ) liblzply.a $(CHASE_CUDA_OBJ) $(LDLIBS) $(CUDA_LIBS)
endif

# FUSE filesystem. Needs libfuse3-dev; no pkg-config on the server, so
# the flags are explicit (-D_FILE_OFFSET_BITS=64 is required by FUSE).
FUSE_CFLAGS = -D_FILE_OFFSET_BITS=64
FUSE_LIBS = -lfuse3

lzply_mount.o: lzply_mount.c lzply_lib.h bch.h bch_polys.h
	$(CC) $(CFLAGS) $(FUSE_CFLAGS) -c -o $@ lzply_mount.c

lzply_mount: lzply_mount.o liblzply.a $(CUDA_OBJ) $(CUDA_STUB_OBJ)
	$(CC) $(CFLAGS) $(FUSE_CFLAGS) -o $@ lzply_mount.o $(CUDA_OBJ) $(CUDA_STUB_OBJ) liblzply.a $(LDLIBS) $(FUSE_LIBS) $(CUDA_LIBS)

# Shared helpers for lzply_stat and lzply_heal (mount resolution via
# /proc, human-size formatting). No FUSE needed.
lzply_util.o: lzply_util.c lzply_lib.h lzply_util.h
	$(CC) $(CFLAGS) -c -o $@ lzply_util.c

lzply_stat.o: lzply_stat.c lzply_lib.h lzply_util.h
	$(CC) $(CFLAGS) -c -o $@ lzply_stat.c

lzply_stat: lzply_stat.o lzply_util.o liblzply.a $(CUDA_OBJ) $(CUDA_STUB_OBJ)
	$(CC) $(CFLAGS) -o $@ lzply_stat.o lzply_util.o $(CUDA_OBJ) $(CUDA_STUB_OBJ) liblzply.a $(LDLIBS) $(CUDA_LIBS)

lzply_heal.o: lzply_heal.c lzply_lib.h lzply_util.h
	$(CC) $(CFLAGS) -c -o $@ lzply_heal.c

lzply_heal: lzply_heal.o lzply_util.o liblzply.a $(CUDA_OBJ) $(CUDA_STUB_OBJ)
	$(CC) $(CFLAGS) -o $@ lzply_heal.o lzply_util.o $(CUDA_OBJ) $(CUDA_STUB_OBJ) liblzply.a $(LDLIBS) $(CUDA_LIBS)

# Bit error rate detector. Read-only: decodes each BCH entry's stored
# bytes in memory and counts the corrected bit errors. Links the same
# shared util (mount resolution) as stat and heal.
lzply_check.o: lzply_check.c lzply_lib.h lzply_util.h
	$(CC) $(CFLAGS) -c -o $@ lzply_check.c

lzply_check: lzply_check.o lzply_util.o liblzply.a $(CUDA_OBJ) $(CUDA_STUB_OBJ)
	$(CC) $(CFLAGS) -o $@ lzply_check.o lzply_util.o $(CUDA_OBJ) $(CUDA_STUB_OBJ) liblzply.a $(LDLIBS) -lm $(CUDA_LIBS)

# In-place platter refresher. Reads a file (or every regular file
# under a directory / mount point) and rewrites it over itself, with
# SHA-256 verification. Links the same shared util (mount
# resolution) as stat, heal and check.
lzply_refresh.o: lzply_refresh.c lzply_lib.h lzply_util.h
	$(CC) $(CFLAGS) -c -o $@ lzply_refresh.c

lzply_refresh: lzply_refresh.o lzply_util.o liblzply.a $(CUDA_OBJ) $(CUDA_STUB_OBJ)
	$(CC) $(CFLAGS) -o $@ lzply_refresh.o lzply_util.o $(CUDA_OBJ) $(CUDA_STUB_OBJ) liblzply.a $(LDLIBS) $(CUDA_LIBS)

# Striped volume (bit-plane, 8 data stripes + 1-3 XOR/GF parity stripes
# + BCH). No compression, so it links only bch.o + sha256.o — no
# liblz, no lzply_lib.
lzply_hyper.o: lzply_hyper.c bch.h bch_polys.h sha256.h
	$(CC) $(CFLAGS) -c -o $@ lzply_hyper.c

lzply_hyper: lzply_hyper.o bch.o sha256.o $(CUDA_STUB_OBJ)
	$(CC) $(CFLAGS) -o $@ lzply_hyper.o bch.o sha256.o $(CUDA_STUB_OBJ)

bch_test: bch.c bch_test.c bch_cuda_stub.c bch.h bch_polys.h bch_cuda.h
	$(CC) $(CFLAGS) -o $@ bch.c bch_test.c bch_cuda_stub.c

# CUDA Chase-1 bench (experiments/cuda-chase): GPU port of the
# deep-repair Chase-1 scan (t=8 only). Needs CUDA 13.x; NVCC is
# /usr/local/cuda/bin/nvcc on hot_house (not on PATH by default).
# NVCC / NVCCFLAGS are defined at the top of this file.

chase_bench_cpu: chase_bench.c bch.c bch_cuda_stub.c bch.h bch_polys.h bch_cuda.h
	$(CC) $(CFLAGS) -o $@ chase_bench.c bch.c bch_cuda_stub.c

chase_bench.o: chase_bench.c bch.h chase_cuda.h
	$(CC) $(CFLAGS) -DHAVE_CUDA -c -o $@ chase_bench.c

chase_cuda.o: chase_cuda.cu chase_cuda.h bch.h bch_polys.h
	$(NVCC) $(NVCCFLAGS) -c -o $@ chase_cuda.cu

chase_bench_cuda: chase_bench.o bch.o chase_cuda.o
	$(NVCC) -arch=sm_86 -o $@ chase_bench.o bch.o chase_cuda.o

# Reed-Solomon codec (rs.c) and the LZRS v1 archive layer
# (lzrs_lib.c: LZRS entry encode/decode + header I/O + scan).
# lz_rs links both plus the shared v4 core (liblzply.a).
rs.o: rs.c rs.h
	$(CC) $(CFLAGS) -c -o $@ rs.c

lzrs_lib.o: lzrs_lib.c lzrs_lib.h lzply_lib.h rs.h bch.h sha256.h
	$(CC) $(CFLAGS) -c -o $@ lzrs_lib.c

liblzrs.a: lzrs_lib.o rs.o
	ar rcs $@ $^

lz_rs.o: lz_rs.c lzrs_lib.h lzply_lib.h bch.h bch_polys.h
	$(CC) $(CFLAGS) -c -o $@ lz_rs.c

lz_rs: lz_rs.o liblzrs.a liblzply.a $(CUDA_OBJ) $(CUDA_STUB_OBJ)
	$(CC) $(CFLAGS) -o $@ lz_rs.o liblzrs.a $(CUDA_OBJ) $(CUDA_STUB_OBJ) liblzply.a $(LDLIBS) $(CUDA_LIBS)

rs_test: rs.c rs_test.c rs.h
	$(CC) $(CFLAGS) -o $@ rs.c rs_test.c

# CRC-16/CCITT-FALSE unit test (vectors + cross-checks). Links the
# shared core for crc16_of.
crc16_test: crc16_test.c liblzply.a $(CUDA_STUB_OBJ)
	$(CC) $(CFLAGS) -o $@ crc16_test.c liblzply.a $(CUDA_STUB_OBJ) $(LDLIBS)

# Deep repair: Chase / list decoding past t (bch.c) + the escalating
# ladder (deep_repair.c) + the lzply_deep_repair CLI. Links both
# archive layers (LZPL v5 + LZRS v1).
deep_repair.o: deep_repair.c deep_repair.h lzply_lib.h lzrs_lib.h bch.h rs.h sha256.h
	$(CC) $(CFLAGS) -c -o $@ deep_repair.c

lzply_deep_repair.o: lzply_deep_repair.c deep_repair.h lzply_lib.h lzrs_lib.h bch.h repair_cmd.h
	$(CC) $(CFLAGS) -c -o $@ lzply_deep_repair.c

lzply_deep_repair: lzply_deep_repair.o repair_cmd.o deep_repair.o liblzrs.a liblzply.a $(CUDA_OBJ) $(CHASE_CUDA_OBJ) $(CUDA_STUB_OBJ)
ifeq ($(CHASE_CUDA_OBJ),)
	$(CC) $(CFLAGS) -o $@ lzply_deep_repair.o repair_cmd.o deep_repair.o liblzrs.a $(CUDA_OBJ) $(CUDA_STUB_OBJ) liblzply.a $(LDLIBS) $(CUDA_LIBS)
else
	$(NVCC) -arch=$(CUDA_ARCH) -o $@ lzply_deep_repair.o repair_cmd.o deep_repair.o liblzrs.a $(CUDA_OBJ) $(CUDA_STUB_OBJ) liblzply.a $(CHASE_CUDA_OBJ) $(LDLIBS) $(CUDA_LIBS)
endif

# bch_polys.h is generated by gen_bch_polys.py from the rssearch data
# (~/workspace/rssearch/polys.txt + finds.txt) and checked in; it is
# NOT rebuilt by make. Regenerate manually after a re-crawl with:
#   python3 gen_bch_polys.py

# The shell suite lives in ./tests when built from the repo layout
# (~/workspace/lzply) and in ../tests when built from /opt/lzply/src.
# run_tests.sh derives its ROOT from its own location, so it works
# in both layouts; LZPLY_TEST_ROOT overrides it explicitly.
# NB: lzply_mount is a test dep on purpose — without it the mount
# tests in run_tests.sh silently skip after a `make clean`, even on a
# machine with /dev/fuse. (Needs libfuse3-dev; present on the server
# and the dev VM. The mount tests themselves still skip cleanly
# where /dev/fuse is absent.)
# testdata/ is generated (it includes a 50 MiB random file) and gitignored,
# so a fresh clone has none. Build it on demand so `make test` works
# straight from a clone.
# lzcsv: standalone CSV compressor. CPU-only, links liblzma.
lzcsv-src/lzcsv.o: lzcsv-src/lzcsv.c
	$(CC) $(CFLAGS) -c -o $@ lzcsv-src/lzcsv.c

lzcsv: lzcsv-src/lzcsv.o
	$(CC) $(CFLAGS) -o $@ lzcsv-src/lzcsv.o -llzma

testdata:
	@if [ ! -d testdata/mixed ]; then ./tests/make_testdata.sh; fi

test: testdata bch_test rs_test crc16_test lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair $(LZCSV)
	./bch_test
	./rs_test
	./crc16_test
	if [ -x ./tests/run_rs_tests.sh ]; then \
		./tests/run_rs_tests.sh ./lz_rs; \
	elif [ -x ../tests/run_rs_tests.sh ]; then \
		../tests/run_rs_tests.sh ./lz_rs; \
	else echo "run_rs_tests.sh not found"; exit 1; fi
	if [ -x ./tests/run_tests.sh ]; then \
		./tests/run_tests.sh ./lzply; \
	elif [ -x ../tests/run_tests.sh ]; then \
		../tests/run_tests.sh ./lzply; \
	else echo "run_tests.sh not found"; exit 1; fi
	if [ -x ./tests/run_deep_repair_tests.sh ]; then \
		./tests/run_deep_repair_tests.sh ./lzply_deep_repair ./lzply ./lz_rs; \
	elif [ -x ../tests/run_deep_repair_tests.sh ]; then \
		../tests/run_deep_repair_tests.sh ./lzply_deep_repair ./lzply ./lz_rs; \
	else echo "run_deep_repair_tests.sh not found"; exit 1; fi
	if [ -x ./tests/run_hyper_tests.sh ]; then \
		./tests/run_hyper_tests.sh ./lzply_hyper; \
	elif [ -x ../tests/run_hyper_tests.sh ]; then \
		../tests/run_hyper_tests.sh ./lzply_hyper; \
	else echo "run_hyper_tests.sh not found"; exit 1; fi
	if [ -x ./tests/run_integration_tests.sh ]; then \
		./tests/run_integration_tests.sh; \
	elif [ -x ../tests/run_integration_tests.sh ]; then \
		../tests/run_integration_tests.sh; \
	else echo "run_integration_tests.sh not found"; exit 1; fi
	if [ -x ./lzcsv ]; then \
		if [ -x ./tests/run_lzcsv_tests.sh ]; then \
			./tests/run_lzcsv_tests.sh ./lzcsv; \
		elif [ -x ../tests/run_lzcsv_tests.sh ]; then \
			../tests/run_lzcsv_tests.sh ./lzcsv; \
		else echo "run_lzcsv_tests.sh not found"; exit 1; fi \
	else echo "lzcsv: skipped (no liblzma)"; fi

# Integrated experiment winners (docs/INTEGRATION.md): the ECC harness
# (interleave d64, small-block BCH, LDPC r90) and the compression
# prototypes (rANS core, dict transform). Not part of `all`: the archive
# format is unchanged (LZPL v5); these are standalone tools.
exp:
	$(MAKE) -C exp bench ldpc_diag pass2_diag

experiments:
	$(MAKE) -C experiments

install: lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair
	mkdir -p $(PREFIX)/bin
	install -m 755 lzply $(PREFIX)/bin/lzply
	install -m 755 lz_rs $(PREFIX)/bin/lz_rs
	install -m 755 lzply_deep_repair $(PREFIX)/bin/lzply_deep_repair
	install -m 755 lzply_mount $(PREFIX)/bin/lzply_mount
	install -m 755 lzply_stat $(PREFIX)/bin/lzply_stat
	install -m 755 lzply_heal $(PREFIX)/bin/lzply_heal
	install -m 755 lzply_check $(PREFIX)/bin/lzply_check
	install -m 755 lzply_refresh $(PREFIX)/bin/lzply_refresh
	install -m 755 lzply_hyper $(PREFIX)/bin/lzply_hyper
	install -m 755 mount.lzply /sbin/mount.lzply

clean:
	rm -f lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair lzcsv lzcsv-src/lzcsv.o bch_test rs_test crc16_test bch_cuda_test bch_bench chase_bench_cpu chase_bench_cuda *.o liblzply.a liblzrs.a

.PHONY: all test install clean exp experiments
