# SPDX-License-Identifier: Apache-2.0
# Copyright 2025 Doubly Linked List Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Compiler and flags
CC = gcc
CFLAGS = -Wall -Wextra -Werror -std=c99 -pedantic -g
LDFLAGS = 

# Source files
SOURCES = doubly_linked_list.c test_doubly_linked_list.c
OBJECTS = $(SOURCES:.c=.o)
HEADERS = doubly_linked_list.h

# Target executable
TARGET = test_dll

# Default target
all: $(TARGET)

# Build the executable
$(TARGET): $(OBJECTS)
	$(CC) $(OBJECTS) -o $(TARGET) $(LDFLAGS)

# Compile source files to object files
%.o: %.c $(HEADERS)
	$(CC) $(CFLAGS) -c $< -o $@

# Run the test program
test: $(TARGET)
	./$(TARGET)

# Clean build artifacts
clean:
	rm -f $(OBJECTS) $(TARGET)

# Install (copy to /usr/local/bin - requires sudo)
install: $(TARGET)
	cp $(TARGET) /usr/local/bin/

# Uninstall
uninstall:
	rm -f /usr/local/bin/$(TARGET)

# Check for memory leaks with valgrind
memcheck: $(TARGET)
	valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./$(TARGET)

# Static analysis with cppcheck
static-analysis:
	cppcheck --enable=all --std=c99 --suppress=missingIncludeSystem $(SOURCES) $(HEADERS)

# Format code with clang-format
format:
	clang-format -i $(SOURCES) $(HEADERS)

# Show help
help:
	@echo "Available targets:"
	@echo "  all           - Build the test program (default)"
	@echo "  test          - Build and run the test program"
	@echo "  clean         - Remove build artifacts"
	@echo "  install       - Install to /usr/local/bin (requires sudo)"
	@echo "  uninstall     - Remove from /usr/local/bin"
	@echo "  memcheck      - Run with valgrind memory checker"
	@echo "  static-analysis - Run cppcheck static analysis"
	@echo "  format        - Format code with clang-format"
	@echo "  help          - Show this help message"

# Declare phony targets
.PHONY: all test clean install uninstall memcheck static-analysis format help