-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
177 lines (153 loc) · 6.64 KB
/
Copy pathMakefile
File metadata and controls
177 lines (153 loc) · 6.64 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
##############################################################################
# Makefile - Layered Architecture Example Project
##############################################################################
# Project name
PROJECT = architecture-example
# Directories
SRC_DIR = src
BUILD_DIR = build
OBJ_DIR = $(BUILD_DIR)/obj
# Layers
APP_DIR = $(SRC_DIR)/app
CONTROL_DIR = $(SRC_DIR)/control
DRIVER_DIR = $(SRC_DIR)/driver
MCU_DIR = $(SRC_DIR)/mcu-peripherals
# Source files (now in src/ subdirectories)
APP_SRCS = $(wildcard $(APP_DIR)/src/*.c)
CONTROL_SRCS = $(wildcard $(CONTROL_DIR)/src/*.c)
DRIVER_SRCS = $(wildcard $(DRIVER_DIR)/src/*.c)
MCU_SRCS = $(wildcard $(MCU_DIR)/src/*.c)
ALL_SRCS = $(APP_SRCS) $(CONTROL_SRCS) $(DRIVER_SRCS) $(MCU_SRCS)
# Objects
APP_OBJS = $(patsubst $(APP_DIR)/src/%.c,$(OBJ_DIR)/app_%.o,$(APP_SRCS))
CONTROL_OBJS = $(patsubst $(CONTROL_DIR)/src/%.c,$(OBJ_DIR)/control_%.o,$(CONTROL_SRCS))
DRIVER_OBJS = $(patsubst $(DRIVER_DIR)/src/%.c,$(OBJ_DIR)/driver_%.o,$(DRIVER_SRCS))
MCU_OBJS = $(patsubst $(MCU_DIR)/src/%.c,$(OBJ_DIR)/mcu_%.o,$(MCU_SRCS))
ALL_OBJS = $(APP_OBJS) $(CONTROL_OBJS) $(DRIVER_OBJS) $(MCU_OBJS)
# Executable
TARGET = $(BUILD_DIR)/$(PROJECT).elf
# Compiler
CC = gcc
# Flags - Add include paths for all layer headers
CFLAGS = -Wall -Wextra -std=c99 \
-I$(APP_DIR)/inc \
-I$(CONTROL_DIR)/inc \
-I$(DRIVER_DIR)/inc \
-I$(MCU_DIR)/inc \
-DSIMULATION # Enable simulation mode with printf
LDFLAGS =
##############################################################################
# Rules
##############################################################################
.PHONY: help all clean validate info format
# Default target: show help
help:
@echo "╔════════════════════════════════════════════════════════════════════╗"
@echo "║ 4-Layer Architecture Example - Build System ║"
@echo "╚════════════════════════════════════════════════════════════════════╝"
@echo ""
@echo "Available targets:"
@echo ""
@echo " make build - Compile entire project and generate executable"
@echo " make clean - Remove all compiled files (build/ directory)"
@echo " make validate - Validate architecture rules compliance"
@echo " make format - Auto-format all source files (Barr C Coding Standard)"
@echo " make info - Show project information and dependencies"
@echo " make help - Show this help message"
@echo ""
@echo "Quick commands:"
@echo ""
@echo " make clean build - Clean build from scratch"
@echo " make build validate - Build and validate architecture"
@echo " make clean build validate - Full clean, build and validation"
@echo ""
@echo "Running the program:"
@echo ""
@echo " ./build/$(PROJECT).elf - Run in simulation mode"
@echo " timeout 3 ./build/$(PROJECT).elf - Run for 3 seconds"
@echo ""
@echo "Architecture layers:"
@echo ""
@echo " APP → calls → CONTROL only"
@echo " CONTROL → calls → DRIVER, MCU-PERIPHERALS"
@echo " DRIVER → calls → MCU-PERIPHERALS only"
@echo " MCU-PERIPHERALS → base layer (no dependencies)"
@echo ""
build: $(TARGET)
# Link
$(TARGET): $(ALL_OBJS)
@echo "Linking..."
@mkdir -p $(BUILD_DIR)
$(CC) $(ALL_OBJS) $(LDFLAGS) -o $@
@echo "Build complete: $(TARGET)"
# Compilation - APP
$(OBJ_DIR)/app_%.o: $(APP_DIR)/src/%.c
@echo "Compiling APP: $<"
@mkdir -p $(OBJ_DIR)
$(CC) $(CFLAGS) -c $< -o $@
# Compilation - CONTROL
$(OBJ_DIR)/control_%.o: $(CONTROL_DIR)/src/%.c
@echo "Compiling CONTROL: $<"
@mkdir -p $(OBJ_DIR)
$(CC) $(CFLAGS) -c $< -o $@
# Compilation - DRIVER
$(OBJ_DIR)/driver_%.o: $(DRIVER_DIR)/src/%.c
@echo "Compiling DRIVER: $<"
@mkdir -p $(OBJ_DIR)
$(CC) $(CFLAGS) -c $< -o $@
# Compilation - MCU-PERIPHERALS
$(OBJ_DIR)/mcu_%.o: $(MCU_DIR)/src/%.c
@echo "Compiling MCU-PERIPHERALS: $<"
@mkdir -p $(OBJ_DIR)
$(CC) $(CFLAGS) -c $< -o $@
# Cleanup
clean:
@echo "Cleaning..."
@rm -rf $(BUILD_DIR)
@echo "Clean complete!"
# Architecture validation
validate:
@echo "Validating 4-layer architecture..."
@echo ""
@echo "1. Checking if APP includes DRIVER or MCU-PERIPHERALS..."
@! grep -r '#include.*["/]driver' $(APP_DIR)/src/ | grep -v "driverlib" && echo "✅ APP OK (only includes CONTROL)" || echo "❌ APP violation: should only include CONTROL!"
@echo ""
@echo "2. Checking if CONTROL includes APP..."
@! grep -r '#include.*["/]app' $(CONTROL_DIR)/src/ && echo "✅ CONTROL OK (doesn't include APP)" || echo "❌ CONTROL violation found!"
@echo ""
@echo "3. Checking if DRIVER includes CONTROL or APP..."
@! grep -r '#include.*["/]\(control\|app\)' $(DRIVER_DIR)/src/ && echo "✅ DRIVER OK (only includes MCU-PERIPHERALS)" || echo "❌ DRIVER violation found!"
@echo ""
@echo "4. Checking if MCU-PERIPHERALS includes anything..."
@! grep -r '#include.*["/]\(app\|control\|driver\)' $(MCU_DIR)/src/ | grep -v "driverlib" && echo "✅ MCU-PERIPHERALS OK (base layer)" || echo "❌ MCU-PERIPHERALS violation found!"
@echo ""
@echo "Architecture validation complete!"
# Info
info:
@echo "Project Info:"
@echo " APP sources: $(APP_SRCS)"
@echo " CONTROL sources: $(CONTROL_SRCS)"
@echo " DRIVER sources: $(DRIVER_SRCS)"
@echo " MCU-PERIPH sources: $(MCU_SRCS)"
@echo ""
@echo "Dependencies:"
@echo " APP → can call → CONTROL only"
@echo " CONTROL → can call → DRIVER, MCU-PERIPHERALS"
@echo " DRIVER → can call → MCU-PERIPHERALS only"
@echo " MCU-PERIPHERALS → base layer (no dependencies)"
# Auto-format all source files
format:
@echo "Formatting source files with clang-format (Barr C Coding Standard)..."
@if command -v clang-format >/dev/null 2>&1; then \
for file in $(ALL_SRCS) $(wildcard $(APP_DIR)/inc/*.h) $(wildcard $(CONTROL_DIR)/inc/*.h) $(wildcard $(DRIVER_DIR)/inc/*.h) $(wildcard $(MCU_DIR)/inc/*.h); do \
echo " Formatting $$file"; \
clang-format -i -style=file $$file; \
done; \
echo "✅ Formatting complete!"; \
else \
echo "❌ clang-format not found. Install it with:"; \
echo " Ubuntu/Debian: sudo apt install clang-format"; \
echo " Fedora: sudo dnf install clang-tools-extra"; \
echo " macOS: brew install clang-format"; \
exit 1; \
fi