# GNU make / gmake. Native target requires an illumos 64-bit compiler + headers.
CC = gcc
CPPFLAGS = -D__EXTENSIONS__ -D_POSIX_C_SOURCE=200809L -Isrc
CFLAGS = -std=c99 -m64 -O2 -Wall -Wextra -Werror -Wformat=2
LDLIBS = -lkstat -lrt -lpthread
SOURCES = src/main.c src/core.c src/config.c src/support.c src/illumos.c src/validation.c src/result.c src/signal.c
TESTFLAGS = -std=c99 -O1 -g -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -Isrc -Wall -Wextra -Werror -Wformat=2 -pedantic -pthread

.PHONY: all native-check test test-distribution test-sanitize portability clean
all: o6h-memory-guard

native-check:
	@test "`uname -s`" = SunOS || { echo "Native build requires SmartOS/illumos; use make test and make portability here."; exit 1; }

o6h-memory-guard: $(SOURCES) src/guard.h | native-check
	$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(SOURCES) $(LDLIBS)

test-guard: tests/test_guard.c tests/fixtures/pageout_deadman_real_incident_2026_09.csv tests/fixtures/successful_pageout_deadman_avoidance_2026_09_21.csv src/core.c src/config.c src/guard.h
	$(CC) $(TESTFLAGS) -o $@ tests/test_guard.c src/core.c src/config.c

test-runtime: tests/test_runtime.c tests/fixtures/global_victim_ranking_smartos_2026_09.csv tests/fixtures/panic32_daemon_stall_2026_09_22.csv tests/fixtures/floor_free_pages_486_2026_09_23.csv src/main.c src/core.c src/config.c src/result.c src/guard.h
	$(CC) $(TESTFLAGS) -o $@ tests/test_runtime.c src/core.c src/config.c src/result.c

test-distribution: tests/test_distribution.sh smf/o6h-memory-guard.xml etc/o6h-memory-guard.conf src/main.c
	sh tests/test_distribution.sh

test-validation: tests/test_validation.c src/validation.c src/signal.c src/guard.h
	$(CC) $(TESTFLAGS) -Itests/api-sketch -o $@ tests/test_validation.c

test-prepare: tests/test_prepare.c src/illumos.c src/core.c src/config.c src/guard.h
	$(CC) $(TESTFLAGS) -Itests/api-sketch -o $@ tests/test_prepare.c src/core.c src/config.c

test: test-guard test-runtime test-validation test-prepare test-distribution
	./test-guard
	./test-runtime
	./test-validation
	./test-prepare

test-sanitize:
	$(CC) $(TESTFLAGS) -fsanitize=address,undefined -fno-omit-frame-pointer -o test-sanitize-guard tests/test_guard.c src/core.c src/config.c
	./test-sanitize-guard
	$(CC) $(TESTFLAGS) -fsanitize=address,undefined -fno-omit-frame-pointer -o test-sanitize-runtime tests/test_runtime.c src/core.c src/config.c src/result.c
	./test-sanitize-runtime
	$(CC) $(TESTFLAGS) -Itests/api-sketch -fsanitize=address,undefined -fno-omit-frame-pointer -o test-sanitize-validation tests/test_validation.c
	./test-sanitize-validation
	$(CC) $(TESTFLAGS) -Itests/api-sketch -fsanitize=address,undefined -fno-omit-frame-pointer -o test-sanitize-prepare tests/test_prepare.c src/core.c src/config.c
	./test-sanitize-prepare

# Local API sketches only: NOT real illumos headers, ABI, linking or runtime validation.
portability:
	$(CC) $(TESTFLAGS) -Itests/api-sketch -fsyntax-only $(SOURCES)
	@for source in $(SOURCES); do $(CC) $(TESTFLAGS) -Itests/api-sketch -fanalyzer -c $$source -o /dev/null || exit 1; done
	sh -n install.sh tests/test_distribution.sh
	xmllint --nonet --noout smf/o6h-memory-guard.xml

clean:
	rm -f o6h-memory-guard test-guard test-runtime test-validation test-prepare test-sanitize-guard test-sanitize-runtime test-sanitize-validation test-sanitize-prepare
