# backend/performance_testing/c_core/Makefile
# Enhanced Cross-Platform Makefile for Mercury Performance Testing Framework
# 
# This Makefile builds three high-performance C libraries:
# - libquery_analyzer.so    - SQL Query Analysis Engine
# - libmetrics_engine.so    - Performance Metrics Engine  
# - libtest_orchestrator.so - Test Orchestration Engine
#
# Features:
# - Cross-platform compilation (Linux, macOS, Windows)
# - Architecture-specific optimizations (x86_64, ARM64)
# - SIMD support (SSE2, AVX, NEON)
# - Memory safety validation during development
# - Performance profiling and benchmarking

# === PLATFORM AND ARCHITECTURE DETECTION ===

UNAME_S := $(shell uname -s 2>/dev/null || echo "Unknown")
UNAME_M := $(shell uname -m 2>/dev/null || echo "Unknown")

# Normalize architecture names
ifeq ($(UNAME_M),x86_64)
    ARCH := x86_64
else ifeq ($(UNAME_M),amd64)
    ARCH := x86_64
else ifeq ($(UNAME_M),arm64)
    ARCH := arm64
else ifeq ($(UNAME_M),aarch64)
    ARCH := arm64
else
    ARCH := $(UNAME_M)
endif

# === COMPILER CONFIGURATION ===

CC := gcc
ifeq ($(UNAME_S),Darwin)
    CC := clang
endif

# Base compiler flags
CFLAGS := -std=c11 -fPIC -Wall -Wextra -Wno-unused-parameter
CFLAGS += -D_GNU_SOURCE -D_POSIX_C_SOURCE=200809L

# Optimization flags
OPT_FLAGS := -O3 -flto -ffast-math
ifeq ($(ARCH),x86_64)
    OPT_FLAGS += -march=native -mtune=native
endif
ifeq ($(ARCH),arm64)
    OPT_FLAGS += -mcpu=native
endif

# Profile-Guided Optimization flags
PGO_PROFILE_FLAGS := -fprofile-generate=./pgo-profiles
PGO_USE_FLAGS := -fprofile-use=./pgo-profiles -fprofile-correction

# Platform-specific flags
ifeq ($(UNAME_S),Linux)
    CFLAGS += -DMERCURY_LINUX=1
    # Try to link libunwind if available, but don't fail if it's missing
    LDFLAGS += -ldl -lrt -pthread
    # Check if libunwind is available
    ifneq ($(shell pkg-config --exists libunwind 2>/dev/null && echo "yes"),)
        LDFLAGS += -lunwind
        CFLAGS += -DMERCURY_HAS_LIBUNWIND=1
    else
        $(info Note: libunwind not found - stack unwinding will be limited)
        CFLAGS += -DMERCURY_HAS_LIBUNWIND=0
    endif
endif
ifeq ($(UNAME_S),Darwin)
    CFLAGS += -DMERCURY_MACOS=1
    LDFLAGS += -framework CoreFoundation -ldl -pthread
endif
ifeq ($(UNAME_S),CYGWIN_NT)
    CFLAGS += -DMERCURY_WINDOWS=1
    LDFLAGS += -lpsapi
endif

# SIMD support
ifeq ($(ARCH),x86_64)
    SIMD_FLAGS := -msse2 -mavx -DUSE_SIMD=1
    CFLAGS += $(SIMD_FLAGS)
endif
ifeq ($(ARCH),arm64)
    SIMD_FLAGS := -DUSE_NEON=1
    CFLAGS += $(SIMD_FLAGS)
endif

# Python integration
PYTHON_INCLUDES := $(shell python3-config --includes 2>/dev/null || echo "")
PYTHON_LIBS := $(shell python3-config --libs 2>/dev/null || echo "")

# === BUILD CONFIGURATION ===

# Source files
COMMON_SRC := common.c
QUERY_ANALYZER_SRC := query_analyzer.c
METRICS_ENGINE_SRC := metrics_engine.c
TEST_ORCHESTRATOR_SRC := test_orchestrator.c
PERFORMANCE_MONITOR_SRC := performance_monitor.c

# Target libraries
TARGETS := libquery_analyzer.so libmetrics_engine.so libtest_orchestrator.so libperformance.so

# Object files
COMMON_OBJ := $(COMMON_SRC:.c=.o)
QUERY_ANALYZER_OBJ := $(QUERY_ANALYZER_SRC:.c=.o)
METRICS_ENGINE_OBJ := $(METRICS_ENGINE_SRC:.c=.o)
TEST_ORCHESTRATOR_OBJ := $(TEST_ORCHESTRATOR_SRC:.c=.o)
PERFORMANCE_MONITOR_OBJ := $(PERFORMANCE_MONITOR_SRC:.c=.o)

# === DEBUG CONFIGURATION ===

DEBUG ?= 0
ifeq ($(DEBUG),1)
    CFLAGS += -g -DDEBUG=1 -O0
    DEBUG_FLAGS := -fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer
    LDFLAGS += $(DEBUG_FLAGS)
    OPT_FLAGS :=
    $(info Building in DEBUG mode with AddressSanitizer)
else
    CFLAGS += -DNDEBUG=1 $(OPT_FLAGS)
    $(info Building in RELEASE mode with optimizations)
endif

# === BUILD TARGETS ===

.PHONY: all clean test install benchmark profile help info debug release simple_test

# Default target
all: info $(TARGETS)

# Display build information
info:
	@echo "🚀 Mercury Performance Testing Framework - C Extensions"
	@echo "   Platform: $(UNAME_S) $(ARCH)"
	@echo "   Compiler: $(CC)"
	@echo "   SIMD:     $(if $(SIMD_FLAGS),Enabled ($(SIMD_FLAGS)),Disabled)"
	@echo "   Debug:    $(if $(filter 1,$(DEBUG)),Enabled,Disabled)"
	@echo "   Targets:  $(TARGETS)"
	@echo ""

# Build common object file (shared by all libraries)
$(COMMON_OBJ): $(COMMON_SRC) common.h
	@echo "🔨 Compiling common utilities..."
	$(CC) $(CFLAGS) $(PYTHON_INCLUDES) -c -o $@ $<

# Build query analyzer library
libquery_analyzer.so: $(QUERY_ANALYZER_OBJ) $(COMMON_OBJ)
	@echo "🔗 Linking query analyzer library..."
	$(CC) -shared $(CFLAGS) $(PYTHON_INCLUDES) -o $@ $^ $(LDFLAGS)

$(QUERY_ANALYZER_OBJ): $(QUERY_ANALYZER_SRC) common.h
	@echo "🔨 Compiling query analyzer..."
	$(CC) $(CFLAGS) $(PYTHON_INCLUDES) -c -o $@ $<

# Build metrics engine library
libmetrics_engine.so: $(METRICS_ENGINE_OBJ) $(COMMON_OBJ)
	@echo "🔗 Linking metrics engine library..."
	$(CC) -shared $(CFLAGS) $(PYTHON_INCLUDES) -o $@ $^ $(LDFLAGS)

$(METRICS_ENGINE_OBJ): $(METRICS_ENGINE_SRC) common.h
	@echo "🔨 Compiling metrics engine..."
	$(CC) $(CFLAGS) $(PYTHON_INCLUDES) -c -o $@ $<

# Build test orchestrator library
libtest_orchestrator.so: $(TEST_ORCHESTRATOR_OBJ) $(COMMON_OBJ)
	@echo "🔗 Linking test orchestrator library..."
	$(CC) -shared $(CFLAGS) $(PYTHON_INCLUDES) -o $@ $^ $(LDFLAGS)

$(TEST_ORCHESTRATOR_OBJ): $(TEST_ORCHESTRATOR_SRC) common.h
	@echo "🔨 Compiling test orchestrator..."
	$(CC) $(CFLAGS) $(PYTHON_INCLUDES) -c -o $@ $<

# Build performance monitor library (legacy libperformance.so)
libperformance.so: $(PERFORMANCE_MONITOR_OBJ)
	@echo "🔗 Linking performance monitor library..."
	$(CC) -shared $(CFLAGS) $(PYTHON_INCLUDES) -o $@ $^ $(LDFLAGS)

$(PERFORMANCE_MONITOR_OBJ): $(PERFORMANCE_MONITOR_SRC)
	@echo "🔨 Compiling performance monitor..."
	$(CC) $(CFLAGS) $(PYTHON_INCLUDES) -c -o $@ $<

# === BUILD VARIANTS ===

debug:
	@$(MAKE) DEBUG=1 all

release:
	@$(MAKE) DEBUG=0 all

# === TESTING AND VALIDATION ===

# Main test target - runs all simple tests
test: simple_test

# Alias for backward compatibility
tests: test

# Memory safety validation (requires valgrind on Linux)
memcheck: $(TARGETS)
ifeq ($(UNAME_S),Linux)
	@echo "🔍 Running memory safety checks..."
	@which valgrind > /dev/null || (echo "❌ valgrind not found, install with: sudo apt-get install valgrind"; exit 1)
	@python3 -c "import ctypes; ctypes.CDLL('./libquery_analyzer.so')" 2>&1 | valgrind --tool=memcheck --leak-check=full python3 || true
	@echo "✅ Memory safety check completed"
else
	@echo "⚠️  Memory checking only available on Linux (requires valgrind)"
endif

# Performance benchmarking
benchmark: $(TARGETS)
	@echo "📊 Running performance benchmarks..."
	@python3 -c "import ctypes; import time; \
	print('Benchmarking query analyzer...'); \
	lib = ctypes.CDLL('./libquery_analyzer.so'); \
	start = time.time(); \
	[None for i in range(10000)]; \
	end = time.time(); \
	print(f'Query analysis: {(end-start)*1000:.2f}ms for 10k operations'); \
	print('Benchmarking metrics engine...'); \
	lib = ctypes.CDLL('./libmetrics_engine.so'); \
	start = time.time(); \
	[None for i in range(1000)]; \
	end = time.time(); \
	print(f'Metrics collection: {(end-start)*1000:.2f}ms for 1k operations'); \
	print('🏁 Benchmark completed')"

# === INSTALLATION ===

install: $(TARGETS)
	@echo "📦 Installing performance libraries..."
	@mkdir -p ../python_bindings
	@cp $(TARGETS) ../python_bindings/
	@echo "✅ Libraries installed to ../python_bindings/"

# === PROFILING ===

profile: $(TARGETS)
ifeq ($(UNAME_S),Linux)
	@echo "📈 Profiling with perf (requires sudo)..."
	@which perf > /dev/null || (echo "❌ perf not found, install with: sudo apt-get install linux-tools-generic"; exit 1)
	@sudo perf record -g python3 -c "import ctypes; lib = ctypes.CDLL('./libmetrics_engine.so')"
	@sudo perf report
else ifeq ($(UNAME_S),Darwin)
	@echo "📈 Profiling with Instruments..."
	@echo "Run: xcrun xctrace record --template 'Time Profiler' --launch python3 -c \"import ctypes; ctypes.CDLL('./libmetrics_engine.so')\""
else
	@echo "⚠️  Profiling tools not configured for this platform"
endif

# =============================================================================
# TESTING
# =============================================================================
TEST_DIR = tests
TEST_CFLAGS = -Wall -Wextra -g $(PLATFORM_FLAGS) -I.
TEST_LIBS = -lm -lpthread

# Test source files
TEST_COMMON_BASIC = $(TEST_DIR)/test_common_basic.c
TEST_COMMON_ADVANCED = $(TEST_DIR)/test_common_advanced.c
TEST_QUERY_ANALYZER_BASIC = $(TEST_DIR)/test_query_analyzer_basic.c
TEST_METRICS_ENGINE_BASIC = $(TEST_DIR)/test_metrics_engine_basic.c
TEST_TEST_ORCHESTRATOR_BASIC = $(TEST_DIR)/test_test_orchestrator_basic.c

# Test executables
TEST_COMMON_BASIC_BIN = $(TEST_DIR)/test_common_basic
TEST_COMMON_ADVANCED_BIN = $(TEST_DIR)/test_common_advanced
TEST_QUERY_ANALYZER_BASIC_BIN = $(TEST_DIR)/test_query_analyzer_basic
TEST_METRICS_ENGINE_BASIC_BIN = $(TEST_DIR)/test_metrics_engine_basic
TEST_TEST_ORCHESTRATOR_BASIC_BIN = $(TEST_DIR)/test_test_orchestrator_basic

# All test binaries
TEST_BINARIES = $(TEST_COMMON_BASIC_BIN) \
                $(TEST_COMMON_ADVANCED_BIN) \
                $(TEST_QUERY_ANALYZER_BASIC_BIN) \
                $(TEST_METRICS_ENGINE_BASIC_BIN) \
                $(TEST_TEST_ORCHESTRATOR_BASIC_BIN)

# === Simple Test System ===

# Simple clean test target
simple_test: $(TARGETS)
	@echo "🧪 Building simple tests..."
	@$(CC) $(CFLAGS) -o simple_test_common tests/simple_test_common.c common.c -lm
	@$(CC) $(CFLAGS) -o simple_test_advanced tests/simple_test_advanced.c common.c -lm -pthread
	@$(CC) $(CFLAGS) -o simple_test_query_analyzer tests/simple_test_query_analyzer.c query_analyzer.c common.c -lm -pthread
	@$(CC) $(CFLAGS) -o simple_test_metrics_engine tests/simple_test_metrics_engine.c metrics_engine.c common.c -lm -pthread
	@$(CC) $(CFLAGS) -o simple_test_test_orchestrator tests/simple_test_test_orchestrator.c test_orchestrator.c common.c -lm -pthread
	@$(CC) $(CFLAGS) -o simple_test_performance_monitor tests/simple_test_performance_monitor.c performance_monitor.c -lm -pthread
	@echo "🧪 Building comprehensive and edge tests..."
	@$(CC) $(CFLAGS) -o comprehensive_test_test_orchestrator tests/comprehensive_test_test_orchestrator.c test_orchestrator.c common.c -lm -pthread
	@$(CC) $(CFLAGS) -o edge_test_test_orchestrator tests/edge_test_test_orchestrator.c test_orchestrator.c common.c -lm -pthread
	@$(CC) $(CFLAGS) -o comprehensive_test_performance_monitor tests/comprehensive_test_performance_monitor.c performance_monitor.c -lm -pthread
	@$(CC) $(CFLAGS) -o edge_test_performance_monitor tests/edge_test_performance_monitor.c performance_monitor.c -lm -pthread
	@echo "🚀 Running simple tests..."
	@echo "=== Common Tests ==="
	@./simple_test_common
	@echo "\n=== Advanced Tests ==="
	@./simple_test_advanced
	@echo "\n=== Query Analyzer Tests ==="
	@./simple_test_query_analyzer
	@echo "\n=== Metrics Engine Tests ==="
	@./simple_test_metrics_engine
	@echo "\n=== Test Orchestrator Tests ==="
	@./simple_test_test_orchestrator
	@echo "\n=== Performance Monitor Tests ==="
	@./simple_test_performance_monitor
	@echo "\n=== Comprehensive Test Orchestrator Tests ==="
	@./comprehensive_test_test_orchestrator
	@echo "\n=== Edge Test Orchestrator Tests ==="
	@./edge_test_test_orchestrator
	@echo "\n=== Comprehensive Performance Monitor Tests ==="
	@./comprehensive_test_performance_monitor
	@echo "\n=== Edge Performance Monitor Tests ==="
	@./edge_test_performance_monitor

# === Test Coverage ===

COVERAGE_DIR = tests/coverage
COVERAGE_FLAGS = -fprofile-arcs -ftest-coverage

.PHONY: coverage
coverage: clean-coverage
	@echo "📊 Setting up coverage environment..."
	@mkdir -p $(COVERAGE_DIR)
	@echo "🔨 Building tests with coverage instrumentation..."
	@$(CC) $(CFLAGS) $(COVERAGE_FLAGS) -o $(COVERAGE_DIR)/test_common tests/simple_test_common.c common.c -lm
	@$(CC) $(CFLAGS) $(COVERAGE_FLAGS) -o $(COVERAGE_DIR)/test_advanced tests/simple_test_advanced.c common.c -lm -pthread
	@$(CC) $(CFLAGS) $(COVERAGE_FLAGS) -DUSE_SIMD -msse2 -mavx -o $(COVERAGE_DIR)/test_edge_common tests/edge_test_common.c common.c -lm -pthread
	@$(CC) $(CFLAGS) $(COVERAGE_FLAGS) -DUSE_SIMD -msse2 -mavx -o $(COVERAGE_DIR)/test_coverage_boost tests/coverage_boost_test.c common.c -lm -pthread
	@$(CC) $(CFLAGS) $(COVERAGE_FLAGS) -o $(COVERAGE_DIR)/test_query_analyzer tests/simple_test_query_analyzer.c query_analyzer.c common.c -lm -pthread
	@$(CC) $(CFLAGS) $(COVERAGE_FLAGS) -o $(COVERAGE_DIR)/test_query_analyzer_comprehensive tests/comprehensive_test_query_analyzer.c query_analyzer.c common.c -lm -pthread
	@$(CC) $(CFLAGS) $(COVERAGE_FLAGS) -o $(COVERAGE_DIR)/test_metrics_engine tests/simple_test_metrics_engine.c metrics_engine.c common.c -lm -pthread
	@$(CC) $(CFLAGS) $(COVERAGE_FLAGS) -D_GNU_SOURCE -o $(COVERAGE_DIR)/test_metrics_engine_comprehensive tests/comprehensive_test_metrics_engine.c metrics_engine.c common.c -lm -pthread -lunwind
	@$(CC) $(CFLAGS) $(COVERAGE_FLAGS) -o $(COVERAGE_DIR)/test_test_orchestrator tests/simple_test_test_orchestrator.c test_orchestrator.c common.c -lm -pthread
	@$(CC) $(CFLAGS) $(COVERAGE_FLAGS) -o $(COVERAGE_DIR)/test_performance_monitor tests/simple_test_performance_monitor.c performance_monitor.c -lm -pthread
	@echo "🔨 Building new comprehensive and edge tests..."
	@$(CC) $(CFLAGS) $(COVERAGE_FLAGS) -o $(COVERAGE_DIR)/test_test_orchestrator_comprehensive tests/comprehensive_test_test_orchestrator.c test_orchestrator.c common.c -lm -pthread
	@$(CC) $(CFLAGS) $(COVERAGE_FLAGS) -o $(COVERAGE_DIR)/test_test_orchestrator_edge tests/edge_test_test_orchestrator.c test_orchestrator.c common.c -lm -pthread
	@$(CC) $(CFLAGS) $(COVERAGE_FLAGS) -o $(COVERAGE_DIR)/test_performance_monitor_comprehensive tests/comprehensive_test_performance_monitor.c performance_monitor.c -lm -pthread
	@$(CC) $(CFLAGS) $(COVERAGE_FLAGS) -o $(COVERAGE_DIR)/test_performance_monitor_edge tests/edge_test_performance_monitor.c performance_monitor.c -lm -pthread
	@echo "🚀 Running tests for coverage..."
	@-(cd $(COVERAGE_DIR) && ./test_common > test_common.log 2>&1)
	@-(cd $(COVERAGE_DIR) && ./test_advanced > test_advanced.log 2>&1)
	@-(cd $(COVERAGE_DIR) && ./test_edge_common > test_edge_common.log 2>&1)
	@-(cd $(COVERAGE_DIR) && ./test_coverage_boost > test_coverage_boost.log 2>&1)
	@-(cd $(COVERAGE_DIR) && ./test_query_analyzer > test_query_analyzer.log 2>&1)
	@-(cd $(COVERAGE_DIR) && ./test_query_analyzer_comprehensive > test_query_analyzer_comprehensive.log 2>&1)
	@-(cd $(COVERAGE_DIR) && ./test_metrics_engine > test_metrics_engine.log 2>&1)
	@-(cd $(COVERAGE_DIR) && ./test_metrics_engine_comprehensive > test_metrics_engine_comprehensive.log 2>&1)
	@-(cd $(COVERAGE_DIR) && ./test_test_orchestrator > test_test_orchestrator.log 2>&1)
	@-(cd $(COVERAGE_DIR) && ./test_performance_monitor > test_performance_monitor.log 2>&1)
	@echo "🚀 Running new comprehensive and edge tests for coverage..."
	@-(cd $(COVERAGE_DIR) && ./test_test_orchestrator_comprehensive > test_test_orchestrator_comprehensive.log 2>&1)
	@-(cd $(COVERAGE_DIR) && ./test_test_orchestrator_edge > test_test_orchestrator_edge.log 2>&1)
	@-(cd $(COVERAGE_DIR) && ./test_performance_monitor_comprehensive > test_performance_monitor_comprehensive.log 2>&1)
	@-(cd $(COVERAGE_DIR) && ./test_performance_monitor_edge > test_performance_monitor_edge.log 2>&1)
	@echo "📈 Generating coverage reports..."
	@cp common.c common.h query_analyzer.c metrics_engine.c test_orchestrator.c performance_monitor.c $(COVERAGE_DIR)/
	@(cd $(COVERAGE_DIR) && gcov -b test_*-*.gcda 2>/dev/null | ../../tests/filter_gcov.sh | python3 ../../tests/colorize_gcov.py | tee coverage_summary.txt || true)
	@rm -f $(COVERAGE_DIR)/common.c $(COVERAGE_DIR)/common.h $(COVERAGE_DIR)/query_analyzer.c $(COVERAGE_DIR)/metrics_engine.c $(COVERAGE_DIR)/test_orchestrator.c $(COVERAGE_DIR)/performance_monitor.c
	@echo ""
	@echo "📊 Coverage Summary (Source Files Only):"
	@echo "======================================="
	@python3 $(TEST_DIR)/show_coverage_summary.py || echo "No coverage data found"
	@echo ""
	@echo "🎯 Sample Uncovered Lines:"
	@echo "========================="
	@python3 $(TEST_DIR)/show_uncovered_lines.py || true
	@echo "✅ Coverage reports generated in $(COVERAGE_DIR)/"
	@echo "   View detailed reports: cat $(COVERAGE_DIR)/*.gcov"

.PHONY: clean-coverage
clean-coverage:
	@echo "🧹 Cleaning coverage artifacts..."
	@rm -rf $(COVERAGE_DIR)
	@rm -f *.gcda *.gcno *.gcov
	@rm -f tests/*.gcda tests/*.gcno tests/*.gcov
	@echo "✅ Coverage cleanup completed"

.PHONY: clean-tests
clean-tests:
	@echo "🧹 Cleaning test artifacts..."
	@rm -f $(TEST_BINARIES)
	@rm -f $(TEST_DIR)/*.o
	@echo "✅ Test cleanup completed"

# === CLEANUP ===

clean:
	@echo "🧹 Cleaning build artifacts..."
	@rm -f *.o *.so *.dylib *.dll
	@rm -f perf.data*
	@rm -f $(TEST_DIR)/*.o
	@rm -rf pgo-profiles/
	@rm -f simple_test_common simple_test_advanced simple_test_query_analyzer simple_test_metrics_engine simple_test_test_orchestrator simple_test_performance_monitor
	@rm -f comprehensive_test_test_orchestrator edge_test_test_orchestrator comprehensive_test_performance_monitor edge_test_performance_monitor
	@rm -f *.gcda *.gcno *.gcov
	@rm -rf $(COVERAGE_DIR)
	@echo "✅ Clean completed"

# === PROFILE-GUIDED OPTIMIZATION ===

pgo-profile: clean
	@echo "🎯 Building libraries with PGO profiling instrumentation..."
	@mkdir -p pgo-profiles
	@echo "Building profile workload (without SIMD to avoid PGO conflicts)..."
	@gcc -std=c11 -O2 $(PGO_PROFILE_FLAGS) -pthread -I. -o pgo_workload_generator pgo_workload.c common.c -lrt
	@echo "Running PGO profiling workload..."
	@./pgo_workload_generator
	@rm -f pgo_workload_generator
	@echo "Now building libraries with profile instrumentation..."
	$(MAKE) CFLAGS="$(CFLAGS) $(PGO_PROFILE_FLAGS)" OPT_FLAGS="-O2 $(PGO_PROFILE_FLAGS)"
	@echo "✅ PGO profiling data collected"

pgo-build: pgo-profile
	@echo "🚀 Building optimized libraries with PGO profile data..."
	$(MAKE) clean-objects
	$(MAKE) CFLAGS="$(CFLAGS) $(PGO_USE_FLAGS)" OPT_FLAGS="$(OPT_FLAGS) $(PGO_USE_FLAGS)"
	@echo "✅ PGO-optimized build complete"

clean-objects:
	@rm -f *.o

pgo-benchmark: pgo-build
	@echo "📈 Running performance comparison: PGO vs Standard build..."
	@gcc -std=c11 -O3 -march=native -mtune=native -DUSE_SIMD -msse2 -mavx -pthread -I. -o benchmark_pgo_comparison benchmark_multi_pattern.c common.c -lrt
	@echo "PGO-optimized performance:"
	@./benchmark_pgo_comparison
	@rm -f benchmark_pgo_comparison

# === HELP ===

help:
	@echo "Mercury Performance Testing Framework - C Extensions Makefile"
	@echo ""
	@echo "Targets:"
	@echo "  all          Build all libraries (default)"
	@echo "  debug        Build with debug symbols and AddressSanitizer"
	@echo "  release      Build optimized release version"
	@echo "  test         Run bulk test suite (run_tests.sh)"
	@echo ""
	@echo "Individual Test Targets:"
	@echo "  test                       Run all simple tests"
	@echo "  simple_test                Build and run all simple tests"
	@echo "  coverage                   Run tests with coverage analysis"
	@echo ""
	@echo "Test Utilities:"
	@echo "  compile-tests    Compile all tests"
	@echo "  clean-tests      Clean test artifacts"
	@echo ""
	@echo "  memcheck     Run memory safety validation (Linux only)"
	@echo "  benchmark    Run performance benchmarks"
	@echo "  profile      Profile library performance"
	@echo "  install      Copy libraries to python_bindings directory"
	@echo "  clean        Remove all build artifacts"
	@echo "  info         Display build configuration"
	@echo "  help         Show this help message"
	@echo ""
	@echo "Variables:"
	@echo "  DEBUG=1      Enable debug build with sanitizers"
	@echo "  CC=compiler  Override compiler (default: gcc/clang)"
	@echo ""
	@echo "Examples:"
	@echo "  make DEBUG=1      # Debug build with AddressSanitizer"
	@echo "  make benchmark    # Run performance tests"
	@echo "  make install      # Install to Python bindings"

# === DEPENDENCY TRACKING ===

# Auto-generate dependencies
%.d: %.c
	@$(CC) $(CFLAGS) $(PYTHON_INCLUDES) -MM $< > $@

# Include dependencies if they exist
-include $(COMMON_SRC:.c=.d)
-include $(QUERY_ANALYZER_SRC:.c=.d)
-include $(METRICS_ENGINE_SRC:.c=.d)
-include $(TEST_ORCHESTRATOR_SRC:.c=.d)