# Enhanced Makefile for ISO 26262 test suite
# Common usage:
#   make              # build (with optional sanitizers via SAN=...)
#   make test         # run the tests
#   make clean        # remove binaries/objects
#   make coverage     # build with gcov coverage and generate .gcov files
#   make asan         # convenient Address/UBSan build
#   make zip          # create a zip of source + Makefile
#
# You can tune constants at compile-time, e.g.:
#   make CPPFLAGS="-DFUZZ_ITERS=5000 -DDEADLINE_MS=10"

CC = gcc
CFLAGS ?= -O2 -std=c11 -Wall -Wextra -Wpedantic
CPPFLAGS ?=
LDFLAGS ?=
LDLIBS ?=

UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
  # clock_gettime may require librt on some libcs
  LDLIBS += -lrt
endif

SRC    := iso26262_part6_tests.c
TARGET := iso26262_tests

all: $(TARGET)

$(TARGET): $(SRC)
	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) $(SAN)

test: $(TARGET)
	./$(TARGET)

asan: SAN += -fsanitize=address,undefined -fno-omit-frame-pointer
asan: CFLAGS := -O1 -g -std=c11 -Wall -Wextra -Wpedantic
asan: clean all

coverage: clean
	$(MAKE) CFLAGS="-O0 -g -std=c11 -Wall -Wextra -Wpedantic --coverage" LDFLAGS="--coverage" all
	./$(TARGET)
	- gcov -b $(SRC)

zip: iso26262_tests_suite_extended.zip

iso26262_tests_suite_extended.zip: $(SRC) Makefile
	zip -9 $@ $^

clean:
	$(RM) $(TARGET) *.o *.gcda *.gcno *.gcov

distclean: clean
	$(RM) iso26262_tests_suite_extended.zip

.PHONY: all test asan coverage zip clean distclean
