# <<project>> Makefile  (--basic build, no CMake)
#
# Targets:
#   make             Build extension(s)
#   make test        C tests + <<py_test_label>>
#   make bench       Build and run the C benchmarks
#   make just-build  PEP 517 hook for just-buildit
#   make clean       Remove build artifacts
#   make help        Show this message

ifeq ($(OS), Windows_NT)
SHELL  := cmd.exe
PYTHON ?= $(or $(JUST_BUILDIT_PYTHON),python)
else
SHELL  := /bin/sh
PYTHON ?= $(or $(JUST_BUILDIT_PYTHON),python3)
endif
CC     ?= cc
CFLAGS ?= -O2 -fPIC -std=c99 -Wall

PY_INC := $(shell $(PYTHON) -c "import sysconfig; print(sysconfig.get_path('include'))")
EXT    := $(shell $(PYTHON) -c "import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))")
INC     = -I$(PY_INC) -Inative/inc

# The compile rules below pass -I$(numpy.get_include()), so an importable
# numpy whose header tree is absent produces `numpy/arrayobject.h: No such
# file` rather than anything naming numpy (gh-824). Test the directory, and
# REinstall — in that state numpy is already present, so a plain install is a
# no-op and cannot repair it.
NUMPY_HEADERS_OK     = import numpy, os, sys; sys.exit(0 if os.path.isdir(numpy.get_include()) else 1)
NUMPY_HEADERS_VERIFY = import numpy, os, sys; p = numpy.get_include(); sys.exit(0 if os.path.isdir(p) else 'error: numpy is installed but its C headers are missing from ' + p + ' -- the extension cannot compile without them. Reinstall numpy for this interpreter (gh-824).')

TARGETS :=
C_TESTS :=
C_BENCHES :=

.PHONY: all test bench just-build clean help

all: $(TARGETS)

# ── Component rules ───────────────────────────────────────────────────────────

# ── Fixed targets ─────────────────────────────────────────────────────────────

test: all $(C_TESTS)
ifeq ($(OS), Windows_NT)
<<ensure_pytest_win>>	<<py_test_cmd_win>>
else
	@for t in $(C_TESTS); do echo "--- $$t ---" && ./$$t || exit 1; done
<<ensure_pytest_unix>>	<<py_test_cmd_unix>>
endif

# gh-832: the cmake backend has emitted a bench target per component since
# gh-15; this one emitted the SOURCE and no rule to build it, so on a
# make-built project you could not fill in the benchmark TODO and have
# anything run it. Green-from-day-0 means the scaffolded file is reachable,
# not merely present.
bench: $(C_BENCHES)
ifeq ($(OS), Windows_NT)
	@for %%b in ($(C_BENCHES)) do @echo --- %%b --- && %%b || exit /b 1
else
	@for b in $(C_BENCHES); do echo "--- $$b ---" && ./$$b || exit 1; done
endif

just-build: all
	mkdir -p $(JUST_BUILDIT_OUTPUT_DIR)
	cp -r src/<<package>> $(JUST_BUILDIT_OUTPUT_DIR)/<<package>>

clean:
	rm -f $(TARGETS) $(C_TESTS) $(C_BENCHES)
	find src -name "*.so" -o -name "*.pyd" | xargs rm -f 2>/dev/null; true

help:
	@echo ""
	@echo "<<project>> build targets"
	@echo ""
	@echo "  make          Build extension(s)"
	@echo "  make test     Run C tests + <<py_test_label>>"
	@echo "  make bench    Build and run the C benchmarks"
	@echo "  make clean    Remove build artifacts"
	@echo ""
