EX_03/level-2/rip/Makefile
2025-09-23 08:41:21 +01:00

80 lines
2.1 KiB
Makefile

# Program name
NAME = rip
# Compiler and flags
CC = gcc
CFLAGS = -Wall -Wextra -Werror
# Source files
SRCS = rip.c
# Object files
OBJS = $(SRCS:.c=.o)
# Colors for output
GREEN = \033[0;32m
RED = \033[0;31m
YELLOW = \033[0;33m
NC = \033[0m # No Color
# Default target
all: $(NAME)
# Build the executable
$(NAME): $(OBJS)
@echo "$(YELLOW)Linking $(NAME)...$(NC)"
@$(CC) $(CFLAGS) $(OBJS) -o $(NAME)
@echo "$(GREEN)$(NAME) created successfully!$(NC)"
# Compile source files to object files
%.o: %.c
@echo "$(YELLOW)Compiling $<...$(NC)"
@$(CC) $(CFLAGS) -c $< -o $@
# Clean object files
clean:
@echo "$(RED)🧹 Cleaning object files...$(NC)"
@rm -f $(OBJS)
@echo "$(GREEN)✅ Object files cleaned!$(NC)"
# Clean object files and executable
fclean: clean
@echo "$(RED)🧹 Cleaning executable...$(NC)"
@rm -f $(NAME)
@echo "$(GREEN)✅ Everything cleaned!$(NC)"
# Rebuild everything
re: fclean all
# Test the program with provided examples
test: $(NAME)
@echo "$(YELLOW)🧪 Running tests...$(NC)"
@echo "$(YELLOW)Test 1: '(()'$(NC)"
@./$(NAME) '(((' | cat -e
@echo "$(YELLOW)Test 2: '((()()())())'$(NC)"
@./$(NAME) '((()()())())' | cat -e
@echo "$(YELLOW)Test 3: '()())()''$(NC)"
@./$(NAME) '()())()' | cat -e
@echo "$(YELLOW)Test 4: '(()(()('$(NC)"
@./$(NAME) '(()(()(' | cat -e
@echo "$(GREEN)✅ All tests completed!$(NC)"
# Check for memory leaks (requires valgrind)
valgrind: $(NAME)
@echo "$(YELLOW)🔍 Checking for memory leaks...$(NC)"
@valgrind --leak-check=full --show-leak-kinds=all ./$(NAME) '(()'
@echo "$(GREEN)✅ Memory check completed!$(NC)"
# Show help
help:
@echo "$(GREEN)Available targets:$(NC)"
@echo " $(YELLOW)all$(NC) - Build the program"
@echo " $(YELLOW)clean$(NC) - Remove object files"
@echo " $(YELLOW)fclean$(NC) - Remove object files and executable"
@echo " $(YELLOW)re$(NC) - Rebuild everything (fclean + all)"
@echo " $(YELLOW)test$(NC) - Run all test cases"
@echo " $(YELLOW)valgrind$(NC) - Check for memory leaks"
@echo " $(YELLOW)help$(NC) - Show this help message"
# Declare phony targets
.PHONY: all clean fclean re test valgrind help