CFLAGS ?= -O2 -Wall -Wextra -pthread
PYTHON ?= python3
all: bvm
ipc_gen.h: ../compiler/gen_templates.py
	cd ../.. && PYTHONPATH=. $(PYTHON) quiver/compiler/gen_templates.py quiver/exec/ipc_gen.h
clean:
	rm -f bvm ipc_gen.h

# --- bvm (the BLOCKS executor; docs/BLOCKS.md, docs/DATAFLOW.md) ---
# zstd, libsodium, liburing and BLAKE3 are linked STATICALLY so bvm depends only on glibc
# (portable: the PyPI wheel ships this one binary). They come from ONE prefix with include/
# and lib/:  ./build-deps.sh builds pinned versions into ./deps (the default), or pass
# DEPS=<prefix> for an existing install that has the four static libs.
HERE   := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
DEPS   ?= $(HERE)deps
ZSTD   ?= $(DEPS)
SODIUM ?= $(DEPS)
URING  ?= $(DEPS)
BLAKE3 ?= $(DEPS)
# Link to a temp name and RENAME into place. Writing the executable directly replaces the
# text of every process already running it -- and over a shared filesystem the kernel's
# ETXTBSY guard does not reach the nodes running remote executors. rename(2) is atomic and
# leaves them on their original inode. `mv -f` within one directory is that rename.
BVM_OUT ?= bvm
bvm: bvm.c ipc_gen.h
	@test -f $(ZSTD)/lib/libzstd.a -a -f $(SODIUM)/lib/libsodium.a -a -f $(URING)/lib/liburing.a -a -f $(BLAKE3)/lib/libblake3.a \
	  || { echo "bvm: static deps not found (looked in ZSTD=$(ZSTD) SODIUM=$(SODIUM) URING=$(URING) BLAKE3=$(BLAKE3))."; \
	       echo "     Run  $(HERE)build-deps.sh  (or pass DEPS=<prefix>)."; exit 1; }
	$(CC) -O2 -pthread -w -I$(ZSTD)/include -I$(SODIUM)/include -I$(URING)/include -I$(BLAKE3)/include -o $(BVM_OUT).tmp bvm.c \
	  $(ZSTD)/lib/libzstd.a $(SODIUM)/lib/libsodium.a $(URING)/lib/liburing.a $(BLAKE3)/lib/libblake3.a
	mv -f $(BVM_OUT).tmp $(BVM_OUT)

# --- qvm2 (ISA v3 prototype; docs/ISA3.md) — same static-link discipline as bvm ---
qvm2: qvm2.c qvm2_gen.h
	$(CC) -O2 -pthread -w -I$(ZSTD)/include -I$(URING)/include -I$(BLAKE3)/include -o qvm2.tmp qvm2.c \
	  $(ZSTD)/lib/libzstd.a $(URING)/lib/liburing.a $(BLAKE3)/lib/libblake3.a
	mv -f qvm2.tmp qvm2
qvm2_gen.h: ../compiler/gen_qvm2.py
	cd ../.. && PYTHONPATH=. python3 quiver/compiler/gen_qvm2.py quiver/exec/qvm2_gen.h
