-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMakefile
More file actions
76 lines (59 loc) · 1.91 KB
/
Makefile
File metadata and controls
76 lines (59 loc) · 1.91 KB
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
# ==========================================
# memscope - Universal Makefile
# ==========================================
# --- CONFIGURATION ---
CC := gcc
CFLAGS := -Wall -Wextra -pedantic -std=c11 -O2 -g -MMD -MP -Iinc
LDFLAGS := -lncursesw -lpthread
# Paths
SRC_DIR := src
TEST_DIR:= tests
OBJ_DIR := obj
BIN_DIR := bin
# Main Output Binary
TARGET := $(BIN_DIR)/memscope
# --- SOURCE MANAGEMENT ---
# 1. Find all .c files in src/
ALL_SRCS := $(shell find $(SRC_DIR) -name '*.c')
# 2. Identify the Main App Sources (Everything)
APP_OBJS := $(ALL_SRCS:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
# 3. Identify Library Sources (Everything EXCEPT main.c)
# We need this so we don't have two 'main()' functions when compiling tests.
LIB_SRCS := $(filter-out $(SRC_DIR)/main.c, $(ALL_SRCS))
LIB_OBJS := $(LIB_SRCS:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
# --- KEYWORDS ---
.PHONY: all compile run clean directories list_tests
# Default: Build the main app
all: directories $(TARGET)
# Compile and Run the Main App
run: all
@echo " [RUN] $(TARGET)"
@./$(TARGET)
# --- BUILD RULES (APP) ---
# Link the Main Application
$(TARGET): $(APP_OBJS) | directories
@echo " [LD] $@"
@$(CC) $(APP_OBJS) -o $@ $(LDFLAGS)
# Compile Generic Source Files
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
@mkdir -p $(dir $@)
@echo " [CC] $<"
@$(CC) $(CFLAGS) -c $< -o $@
# --- BUILD RULES (TESTS) ---
# Magic Rule: 'make test_stat' compiles tests/test_stat.c + src files (no main)
# Usage: make test_stat
test_%: $(TEST_DIR)/test_%.c $(LIB_OBJS) | directories
@echo " [TEST] Building $@"
@$(CC) $(CFLAGS) $< $(LIB_OBJS) -o $(BIN_DIR)/$@
@echo " [EXEC] Running $(BIN_DIR)/$@"
@./$(BIN_DIR)/$@
# --- UTILS ---
directories:
@mkdir -p $(BIN_DIR)
@mkdir -p $(OBJ_DIR)
clean:
@echo " [RM] $(OBJ_DIR) $(BIN_DIR)"
@rm -rf $(OBJ_DIR) $(BIN_DIR)
# Helper to see what tests are available
list_tests:
@ls $(TEST_DIR) | grep "test_" | sed 's/test_//g' | sed 's/.c//g'