CC ?= cc
CFLAGS ?= -std=c11 -O2 -Wall -Wextra -Wpedantic
CFLAGS += -I../../gps_system/header
LDLIBS ?= -lm

GPS_TIME_SOURCE := ../../gps_system/src/gps/gps_time_anchor.c
GPS_EPOCH_SOURCE := ../../gps_system/src/gps/gps_nav_epoch.c
ESKF_SOURCES := ../../gps_system/src/fusion/eskf.c ../../gps_system/src/fusion/fusion_motion.c ../../gps_system/src/fusion/fusion_rest_detector.c ../../gps_system/src/fusion/fusion_engine.c ../../gps_system/src/fusion/fusion_gps_adapter.c ../../gps_system/src/fusion/fusion_bus.c
IMU_PROFILE_SOURCE := ../../gps_system/src/adapters/imu_fusion_profile.c

.PHONY: all test gps-system-test scenario-logs sanitize profile benchmark clean

all: standalone_gps_integrity_test eskf_motion_test fusion_rest_detector_test fusion_synthetic_test imu_quality_synthetic_test async_tilt_recovery_test async_rate_matrix_test gps_reacquisition_test platform_scenario_matrix_test gps_failure_scenario_test

standalone_gps_integrity_test: standalone_gps_integrity_test.c $(GPS_TIME_SOURCE) $(GPS_EPOCH_SOURCE)
	$(CC) $(CFLAGS) $^ -o $@ $(LDLIBS)

eskf_motion_test: eskf_motion_test.c $(ESKF_SOURCES)
	$(CC) $(CFLAGS) $^ -o $@ $(LDLIBS)

fusion_rest_detector_test: fusion_rest_detector_test.c ../../gps_system/src/fusion/fusion_rest_detector.c
	$(CC) $(CFLAGS) $^ -o $@ $(LDLIBS)

fusion_synthetic_test: fusion_synthetic_test.c $(ESKF_SOURCES)
	$(CC) $(CFLAGS) $^ -o $@ $(LDLIBS)

imu_quality_synthetic_test: imu_quality_synthetic_test.c $(ESKF_SOURCES) $(IMU_PROFILE_SOURCE)
	$(CC) $(CFLAGS) $^ -o $@ $(LDLIBS)

async_tilt_recovery_test: async_tilt_recovery_test.c $(ESKF_SOURCES)
	$(CC) $(CFLAGS) $^ -o $@ $(LDLIBS)

async_rate_matrix_test: async_rate_matrix_test.c $(ESKF_SOURCES)
	$(CC) $(CFLAGS) $^ -o $@ $(LDLIBS)

gps_reacquisition_test: gps_reacquisition_test.c $(ESKF_SOURCES)
	$(CC) $(CFLAGS) $^ -o $@ $(LDLIBS)

platform_scenario_matrix_test: platform_scenario_matrix_test.c $(ESKF_SOURCES)
	$(CC) $(CFLAGS) $^ -o $@ $(LDLIBS)

gps_failure_scenario_test: gps_failure_scenario_test.c $(ESKF_SOURCES)
	$(CC) $(CFLAGS) $^ -o $@ $(LDLIBS)

test: standalone_gps_integrity_test eskf_motion_test fusion_rest_detector_test fusion_synthetic_test imu_quality_synthetic_test async_tilt_recovery_test async_rate_matrix_test gps_reacquisition_test platform_scenario_matrix_test gps_failure_scenario_test
	./standalone_gps_integrity_test
	./eskf_motion_test
	./fusion_rest_detector_test
	./fusion_synthetic_test
	./imu_quality_synthetic_test
	./async_tilt_recovery_test
	./async_rate_matrix_test
	./gps_reacquisition_test
	./platform_scenario_matrix_test
	./gps_failure_scenario_test

gps-system-test: standalone_gps_integrity_test
	./standalone_gps_integrity_test

scenario-logs: platform_scenario_matrix_test
	mkdir -p output/scenarios
	@for scenario in stationary pedestrian car aircraft; do \
		for gps in 200 500 1000; do \
			for imu in 19 20 200 300; do \
				./platform_scenario_matrix_test --log $$scenario $$gps $$imu \
					output/scenarios/$${scenario}_gps$${gps}_imu$${imu}.log >/dev/null || exit $$?; \
			done; \
		done; \
	done
	@echo "Wrote 48 replay logs under output/scenarios/"

sanitize:
	$(CC) -std=c11 -O1 -g -Wall -Wextra -Wpedantic -Werror \
		-fsanitize=address,undefined -fno-omit-frame-pointer \
		-I../../gps_system/header standalone_gps_integrity_test.c \
		$(GPS_TIME_SOURCE) $(GPS_EPOCH_SOURCE) \
		-o standalone_gps_integrity_test_san $(LDLIBS)
	ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 \
		UBSAN_OPTIONS=halt_on_error=1 ./standalone_gps_integrity_test_san
	$(CC) -std=c11 -O1 -g -Wall -Wextra -Wpedantic -Werror \
		-fsanitize=address,undefined -fno-omit-frame-pointer \
		-I../../gps_system/header fusion_rest_detector_test.c \
		../../gps_system/src/fusion/fusion_rest_detector.c \
		-o fusion_rest_detector_test_san $(LDLIBS)
	ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 \
		UBSAN_OPTIONS=halt_on_error=1 ./fusion_rest_detector_test_san
	$(CC) -std=c11 -O1 -g -Wall -Wextra -Wpedantic -Werror \
		-fsanitize=address,undefined -fno-omit-frame-pointer \
		-I../../gps_system/header eskf_motion_test.c $(ESKF_SOURCES) \
		-o eskf_motion_test_san $(LDLIBS)
	ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 \
		UBSAN_OPTIONS=halt_on_error=1 ./eskf_motion_test_san
	$(CC) -std=c11 -O1 -g -Wall -Wextra -Wpedantic -Werror \
		-fsanitize=address,undefined -fno-omit-frame-pointer \
		-I../../gps_system/header fusion_synthetic_test.c $(ESKF_SOURCES) \
		-o fusion_synthetic_test_san $(LDLIBS)
	ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 \
		UBSAN_OPTIONS=halt_on_error=1 ./fusion_synthetic_test_san
	$(CC) -std=c11 -O1 -g -Wall -Wextra -Wpedantic -Werror \
		-fsanitize=address,undefined -fno-omit-frame-pointer \
		-I../../gps_system/header imu_quality_synthetic_test.c $(ESKF_SOURCES) \
		$(IMU_PROFILE_SOURCE) -o imu_quality_synthetic_test_san $(LDLIBS)
	ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 \
		UBSAN_OPTIONS=halt_on_error=1 ./imu_quality_synthetic_test_san
	$(CC) -std=c11 -O1 -g -Wall -Wextra -Wpedantic -Werror \
		-fsanitize=address,undefined -fno-omit-frame-pointer \
		-I../../gps_system/header async_tilt_recovery_test.c $(ESKF_SOURCES) \
		-o async_tilt_recovery_test_san $(LDLIBS)
	ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 \
		UBSAN_OPTIONS=halt_on_error=1 ./async_tilt_recovery_test_san
	$(CC) -std=c11 -O1 -g -Wall -Wextra -Wpedantic -Werror \
		-fsanitize=address,undefined -fno-omit-frame-pointer \
		-I../../gps_system/header async_rate_matrix_test.c $(ESKF_SOURCES) \
		-o async_rate_matrix_test_san $(LDLIBS)
	ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 \
		UBSAN_OPTIONS=halt_on_error=1 ./async_rate_matrix_test_san
	$(CC) -std=c11 -O1 -g -Wall -Wextra -Wpedantic -Werror \
		-fsanitize=address,undefined -fno-omit-frame-pointer \
		-I../../gps_system/header gps_reacquisition_test.c $(ESKF_SOURCES) \
		-o gps_reacquisition_test_san $(LDLIBS)
	ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 \
		UBSAN_OPTIONS=halt_on_error=1 ./gps_reacquisition_test_san
	$(CC) -std=c11 -O1 -g -Wall -Wextra -Wpedantic -Werror \
		-fsanitize=address,undefined -fno-omit-frame-pointer \
		-I../../gps_system/header platform_scenario_matrix_test.c $(ESKF_SOURCES) \
		-o platform_scenario_matrix_test_san $(LDLIBS)
	ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 \
		UBSAN_OPTIONS=halt_on_error=1 ./platform_scenario_matrix_test_san
	$(CC) -std=c11 -O1 -g -Wall -Wextra -Wpedantic -Werror \
		-fsanitize=address,undefined -fno-omit-frame-pointer \
		-I../../gps_system/header gps_failure_scenario_test.c $(ESKF_SOURCES) \
		-o gps_failure_scenario_test_san $(LDLIBS)
	ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 \
		UBSAN_OPTIONS=halt_on_error=1 ./gps_failure_scenario_test_san

profile:
	$(CC) -std=c11 -O2 -g -pg -Wall -Wextra -Wpedantic \
		-I../../gps_system/header fusion_synthetic_test.c $(ESKF_SOURCES) \
		-o fusion_synthetic_test_gprof $(LDLIBS)
	./fusion_synthetic_test_gprof
	gprof ./fusion_synthetic_test_gprof gmon.out

benchmark: standalone_gps_integrity_test
	./standalone_gps_integrity_test --bench-clean 100000
	./standalone_gps_integrity_test --bench-single 100000
	./standalone_gps_integrity_test --bench-double 100000

clean:
	rm -f standalone_gps_integrity_test standalone_gps_integrity_test_san standalone_gps_integrity_test_gprof eskf_motion_test eskf_motion_test_san fusion_rest_detector_test fusion_rest_detector_test_san fusion_synthetic_test fusion_synthetic_test_san fusion_synthetic_test_gprof fusion_synthetic_trace imu_quality_synthetic_test imu_quality_synthetic_test_san async_tilt_recovery_test async_tilt_recovery_test_san async_rate_matrix_test async_rate_matrix_test_san gps_reacquisition_test gps_reacquisition_test_san platform_scenario_matrix_test platform_scenario_matrix_test_san gps_failure_scenario_test gps_failure_scenario_test_san gmon.out
