Repository navigation
Expand file tree
/
Copy pathmakefile
More file actions
105 lines (86 loc) · 2.7 KB
/
Copy pathmakefile
File metadata and controls
105 lines (86 loc) · 2.7 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Compiler and minimal options
CC = gcc
CFLAGS = -Os -s -ffunction-sections -fdata-sections \
-fno-asynchronous-unwind-tables -fno-ident -I./src
LDFLAGS = -Wl,--gc-sections
DEBUG_FLAGS = -g -Wall -I./src
# Directories
SRC_DIR = src
BUILD_DIR = build
BIN_DIR = bin
TESTS_DIR = tests
# Targets
TARGET = $(BIN_DIR)/basic.exe
DEBUG_TARGET = $(BIN_DIR)/basic_debug.exe
TEST_TARGET = $(BIN_DIR)/tests.exe
# Source files
SRCS = main.c repl.c error.c memory.c program.c token.c interpreter.c parser.c
OBJS = $(addprefix $(BUILD_DIR)/, $(SRCS:.c=.o))
# Pour les tests, on retire main.o
OBJS_NO_MAIN = $(filter-out $(BUILD_DIR)/main.o, $(OBJS))
# Test sources
TEST_SRCS = \
$(TESTS_DIR)/test_error.c \
$(TESTS_DIR)/test_memory.c \
$(TESTS_DIR)/test_token.c \
$(TESTS_DIR)/test_interpreter.c \
$(TESTS_DIR)/test_repl.c \
$(TESTS_DIR)/test_parser.c \
$(TESTS_DIR)/test_runner.c \
$(TESTS_DIR)/test_utilities.c
TEST_OBJS = $(addprefix $(BUILD_DIR)/, $(notdir $(TEST_SRCS:.c=.o)))
# Create build/bin directories if missing
$(shell mkdir -p $(BUILD_DIR) $(BIN_DIR))
# ------------------------------
# Main targets
# ------------------------------
all: $(TARGET)
# Minimal BASIC interpreter
$(TARGET): $(OBJS)
$(CC) $(CFLAGS) -o $@ $(OBJS) $(LDFLAGS)
strip $@
# Debug build
debug: $(OBJS)
$(CC) $(DEBUG_FLAGS) -o $(DEBUG_TARGET) $(OBJS)
# Compile source objects
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
$(CC) $(CFLAGS) -c $< -o $@
# ------------------------------
# Test targets
# ------------------------------
# Compile test objects
$(BUILD_DIR)/%.o: $(TESTS_DIR)/%.c
$(CC) $(CFLAGS) -c $< -o $@
# Build test executable (no main.c)
$(TEST_TARGET): $(TEST_OBJS) $(OBJS_NO_MAIN)
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
strip $@
# Run all tests
test: $(TEST_TARGET)
./$(TEST_TARGET)
# ------------------------------
# Header dependencies
# ------------------------------
$(BUILD_DIR)/repl.o: $(SRC_DIR)/repl.h $(SRC_DIR)/common.h
$(BUILD_DIR)/error.o: $(SRC_DIR)/error.h $(SRC_DIR)/common.h
$(BUILD_DIR)/memory.o: $(SRC_DIR)/memory.h $(SRC_DIR)/common.h
$(BUILD_DIR)/program.o: $(SRC_DIR)/program.h $(SRC_DIR)/common.h
$(BUILD_DIR)/token.o: $(SRC_DIR)/token.h $(SRC_DIR)/common.h
$(BUILD_DIR)/interpreter.o: $(SRC_DIR)/interpreter.h $(SRC_DIR)/common.h
$(BUILD_DIR)/parser.o: $(SRC_DIR)/parser.h $(SRC_DIR)/common.h
# ------------------------------
# Clean rules
# ------------------------------
clean:
rm -rf $(BUILD_DIR) $(BIN_DIR)
clean-tests:
rm -f $(BUILD_DIR)/*.o
rm -f $(TEST_TARGET)
# ------------------------------
# Show binary size
# ------------------------------
size:
@echo "----- Binary size -----"
@size $(TARGET)
@echo "Total size in KB:"
@size $(TARGET) | tail -n 1 | awk '{printf "%.2f KB\n", $$4/1024}'