-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
89 lines (75 loc) · 3.32 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
##############################################################################
# Compiler and Flags
##############################################################################
CC = gcc
CFLAGS = -std=c17 -Wall -Wextra -Werror -Iinclude
SDL_FLAGS = `sdl2-config --cflags --libs` -lSDL2_ttf -lSDL2_mixer
# Optional debug mode: make DEBUG=1
ifdef DEBUG
CFLAGS += -g -O0
else
CFLAGS += -O2
endif
##############################################################################
# Project Directories
##############################################################################
SRC_DIR = src
OBJ_DIR = obj
BIN_DIR = bin
##############################################################################
# Output Binary
##############################################################################
TARGET = chip8
##############################################################################
# Source Files and Corresponding Object Files
##############################################################################
# Find all .c files in SRC_DIR
SRCS = $(wildcard $(SRC_DIR)/*.c)
# Convert each .c file in SRCS into a .o file in OBJ_DIR
OBJS = $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.o, $(SRCS))
# Create .d files to track header dependencies
DEPS = $(OBJS:.o=.d)
##############################################################################
# Phony Targets
##############################################################################
.PHONY: all clean clean-all help
##############################################################################
# Default Target
##############################################################################
all: $(BIN_DIR)/$(TARGET)
##############################################################################
# Link Final Executable
##############################################################################
$(BIN_DIR)/$(TARGET): $(OBJS)
@mkdir -p $(BIN_DIR)
@$(CC) $(CFLAGS) -o $@ $^ $(SDL_FLAGS)
@echo "[LINK] $@"
##############################################################################
# Compile Each .c -> .o (+ Generate .d for Dependencies)
##############################################################################
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
@mkdir -p $(OBJ_DIR)
@$(CC) $(CFLAGS) -MMD -MP -c $< -o $@ $(SDL_FLAGS)
@echo "[CC] $< -> $@"
##############################################################################
# Auto-Include Dependency Files
##############################################################################
-include $(DEPS)
##############################################################################
# Utility Targets
##############################################################################
help:
@echo "-------------------------------------------------------"
@echo " Available targets:"
@echo " make [all] - Build the project (default)"
@echo " make DEBUG=1 - Build in debug mode (-g -O0)"
@echo " make clean - Remove object files and binary"
@echo " make clean-all - Remove all build artifacts (.o, .d, binary)"
@echo " make help - Print this help message"
@echo "-------------------------------------------------------"
clean:
@rm -f $(OBJ_DIR)/*.o $(BIN_DIR)/$(TARGET)
@echo "[CLEAN] Removed object files and binary."
clean-all: clean
@rm -f $(OBJ_DIR)/*.d
@echo "[CLEAN-ALL] Removed dependency files."