EXTENSION = pgbreaker
MODULE_big = pgbreaker
DATA = sql/pgbreaker--0.1.0.sql

CORE_SRCS = \
	src/core/pgb_matcher.c \
	src/core/pgb_limiter.c \
	src/core/pgb_ledger.c

PG_SRCS = \
	src/pg/pgb_shmem.c \
	src/pg/pgb_hooks.c \
	src/pg/pgb_api.c

CORE_HDRS = src/core/pgb_core.h src/core/pgb_atomic.h
PG_HDRS = src/pg/pgb_pg.h $(CORE_HDRS)

OBJS = $(CORE_SRCS:.c=.o) $(PG_SRCS:.c=.o)

PG_CPPFLAGS = -I$(CURDIR)/src/core -I$(CURDIR)/src/pg -DPGB_USE_PG_ATOMICS
PG_CFLAGS ?= -Wall -Wextra -Wno-unused-parameter

BUILD_DIR = build
BUILD_RUN_PREFIX = $(if $(filter /%,$(BUILD_DIR)),,./)
EXTRA_CLEAN = $(BUILD_DIR)

CORE_ONLY_GOALS = \
	test-core \
	test-core-asan \
	test-core-tsan \
	test-core-c11 \
	test-core-c11-asan \
	test-core-c11-tsan \
	test-core-release

ifneq ($(MAKECMDGOALS),)
ifeq ($(filter-out $(CORE_ONLY_GOALS),$(MAKECMDGOALS)),)
SKIP_PGXS = 1
endif
endif

ifndef SKIP_PGXS
PG_CONFIG ?= pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs 2>/dev/null)
endif

ifneq ($(wildcard $(PGXS)),)
include $(PGXS)

# Track the active PG_CONFIG target so switching PostgreSQL majors without
# running `make clean` forces a clean rebuild of all objects and bitcode.
PG_STAMP_FILE = $(BUILD_DIR)/.pg_config.stamp
PG_STAMP_VAL := $(shell $(PG_CONFIG) --version --bindir --pkglibdir 2>/dev/null | tr '\n' '|')
PG_PREV_STAMP := $(shell cat $(PG_STAMP_FILE) 2>/dev/null)

ifneq ($(PG_STAMP_VAL),$(PG_PREV_STAMP))
.PHONY: $(PG_STAMP_FILE)
endif

$(PG_STAMP_FILE): | $(BUILD_DIR)
	@printf '%s\n' '$(PG_STAMP_VAL)' > $@

$(CORE_SRCS:.c=.o) $(CORE_SRCS:.c=.bc): $(CORE_HDRS) $(PG_STAMP_FILE)
$(PG_SRCS:.c=.o) $(PG_SRCS:.c=.bc): $(PG_HDRS) $(PG_STAMP_FILE)

# Some RHEL/Rocky/Fedora PG packages embed -specs=/usr/lib/rpm/redhat/...
# into PGXS CFLAGS/LDFLAGS without pulling in redhat-rpm-config. Strip only
# absent Red Hat RPM spec paths so plain `make` works out of the box.
MISSING_SPECS := $(foreach f,$(filter -specs=/usr/lib/rpm/redhat/%,$(CFLAGS) $(LDFLAGS) $(LDFLAGS_SL)),$(if $(wildcard $(patsubst -specs=%,%,$(f))),,$(f)))
ifneq ($(strip $(MISSING_SPECS)),)
override CFLAGS := $(filter-out $(MISSING_SPECS),$(CFLAGS))
override LDFLAGS := $(filter-out $(MISSING_SPECS),$(LDFLAGS))
override LDFLAGS_SL := $(filter-out $(MISSING_SPECS),$(LDFLAGS_SL))
endif
else
ifndef SKIP_PGXS
.PHONY: pgxs-missing clean
pgxs-missing:
	@echo "ERROR: '$(PG_CONFIG)' not found or PGXS makefile missing ('$(PGXS)')." >&2
	@echo "Install postgresql-server-devel or pass PG_CONFIG=/path/to/pg_config." >&2
	@echo "Standalone core tests still work without PostgreSQL: make test-core" >&2
	@exit 1

clean:
	rm -rf $(BUILD_DIR) $(OBJS) pgbreaker.so pgbreaker.bc $(CORE_SRCS:.c=.bc) $(PG_SRCS:.c=.bc)
endif
endif

# ---------------------------------------------------------------------------
# Standalone Core Tests (ZERO postgres.h dependency)
# ---------------------------------------------------------------------------

CORE_CC ?= gcc
SAN_CC ?= clang
CORE_CFLAGS = -std=c99 -D_POSIX_C_SOURCE=200809L -Wall -Wextra -Werror -pthread -Isrc/core
CORE_C11_CFLAGS = -std=c11 -D_POSIX_C_SOURCE=200809L -Wall -Wextra -Werror -pthread -Isrc/core
CORE_TEST_SRCS = $(CORE_SRCS) tests/core/test_pgb_core.c

.PHONY: test-core test-core-asan test-core-tsan test-core-c11 test-core-c11-asan test-core-c11-tsan test-core-release test-pg

$(BUILD_DIR):
	mkdir -p $(BUILD_DIR)

test-core-asan: $(BUILD_DIR)
	$(SAN_CC) $(CORE_CFLAGS) -O1 -g -fno-omit-frame-pointer \
		-fsanitize=address,undefined -fno-sanitize-recover=all \
		$(CORE_TEST_SRCS) -o $(BUILD_DIR)/test_pgb_core_asan
	$(BUILD_RUN_PREFIX)$(BUILD_DIR)/test_pgb_core_asan

test-core-tsan: $(BUILD_DIR)
	$(SAN_CC) $(CORE_CFLAGS) -O1 -g -fno-omit-frame-pointer \
		-fsanitize=thread \
		$(CORE_TEST_SRCS) -o $(BUILD_DIR)/test_pgb_core_tsan
	$(BUILD_RUN_PREFIX)$(BUILD_DIR)/test_pgb_core_tsan

test-core-c11-asan: $(BUILD_DIR)
	$(SAN_CC) $(CORE_C11_CFLAGS) -O1 -g -fno-omit-frame-pointer \
		-fsanitize=address,undefined -fno-sanitize-recover=all \
		$(CORE_TEST_SRCS) -o $(BUILD_DIR)/test_pgb_core_c11_asan
	$(BUILD_RUN_PREFIX)$(BUILD_DIR)/test_pgb_core_c11_asan

test-core-c11-tsan: $(BUILD_DIR)
	$(SAN_CC) $(CORE_C11_CFLAGS) -O1 -g -fno-omit-frame-pointer \
		-fsanitize=thread \
		$(CORE_TEST_SRCS) -o $(BUILD_DIR)/test_pgb_core_c11_tsan
	$(BUILD_RUN_PREFIX)$(BUILD_DIR)/test_pgb_core_c11_tsan

test-core-c11: test-core-c11-asan test-core-c11-tsan

test-core-release: $(BUILD_DIR)
	$(CORE_CC) $(CORE_CFLAGS) -O2 \
		$(CORE_TEST_SRCS) -o $(BUILD_DIR)/test_pgb_core_release
	$(BUILD_RUN_PREFIX)$(BUILD_DIR)/test_pgb_core_release

test-core: test-core-asan test-core-tsan test-core-c11 test-core-release

# ---------------------------------------------------------------------------
# PostgreSQL Integration, Chaos & Benchmark Suite
# ---------------------------------------------------------------------------

test-pg:
	PG_CONFIG="$(PG_CONFIG)" bash tests/pg/run_pg_tests.sh
