# SPDX-License-Identifier: Apache-2.0
# Copyright 2025 Median of Two Sorted Arrays Example
#
# 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
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -Wpedantic -O2 -g
LDFLAGS = 

# Source files
SOURCES = main.cpp MedianFinder.cc
OBJECTS = $(SOURCES:.cpp=.o)
OBJECTS := $(OBJECTS:.cc=.o)

# Target executable
TARGET = median_finder

# Default target
all: $(TARGET)

# Build the main executable
$(TARGET): $(OBJECTS)
	$(CXX) $(OBJECTS) -o $(TARGET) $(LDFLAGS)
	@echo "Build complete! Run with: ./$(TARGET)"

# Build the standalone example
standalone: median_of_two_sorted_arrays.cpp
	$(CXX) $(CXXFLAGS) median_of_two_sorted_arrays.cpp -o median_standalone $(LDFLAGS)
	@echo "Standalone build complete! Run with: ./median_standalone"

# Compile source files
%.o: %.cpp
	$(CXX) $(CXXFLAGS) -c $< -o $@

%.o: %.cc
	$(CXX) $(CXXFLAGS) -c $< -o $@

# Clean build artifacts
clean:
	rm -f $(OBJECTS) $(TARGET) median_standalone
	@echo "Clean complete!"

# Install (copy to /usr/local/bin)
install: $(TARGET)
	sudo cp $(TARGET) /usr/local/bin/
	@echo "Installed $(TARGET) to /usr/local/bin/"

# Uninstall
uninstall:
	sudo rm -f /usr/local/bin/$(TARGET)
	@echo "Uninstalled $(TARGET) from /usr/local/bin/"

# Run tests
test: $(TARGET)
	./$(TARGET) <<< "1"

# Run with debug information
debug: CXXFLAGS += -DDEBUG -g3
debug: $(TARGET)

# Run with optimization
release: CXXFLAGS += -O3 -DNDEBUG
release: clean $(TARGET)

# Check for memory leaks (requires valgrind)
memcheck: $(TARGET)
	valgrind --leak-check=full --show-leak-kinds=all ./$(TARGET) <<< "1"

# Static analysis (requires cppcheck)
analyze:
	cppcheck --enable=all --std=c++17 --suppress=missingIncludeSystem $(SOURCES) MedianFinder.h

# Format code (requires clang-format)
format:
	clang-format -i -style=Google $(SOURCES) MedianFinder.h main.cpp

# Show help
help:
	@echo "Available targets:"
	@echo "  all        - Build the main executable (default)"
	@echo "  standalone - Build the standalone example"
	@echo "  clean      - Remove build artifacts"
	@echo "  install    - Install to /usr/local/bin"
	@echo "  uninstall  - Remove from /usr/local/bin"
	@echo "  test       - Build and run automated tests"
	@echo "  debug      - Build with debug information"
	@echo "  release    - Build optimized release version"
	@echo "  memcheck   - Run with valgrind memory checker"
	@echo "  analyze    - Run static analysis with cppcheck"
	@echo "  format     - Format code with clang-format"
	@echo "  help       - Show this help message"

# Declare phony targets
.PHONY: all standalone clean install uninstall test debug release memcheck analyze format help