forked from jballoffet/c-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
183 lines (146 loc) · 6.11 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
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
178
179
180
181
182
183
################################################################
# AUTHOR INFORMATION #
################################################################
AUTHOR = "JavierBalloffet"
CLASS = "R1042"
EXERCISE = "1_1"
YEAR = "2020"
################################################################
# SOURCES, HEADERS, OBJECTS, LIBRARIES AND EXECUTABLES #
################################################################
BIN_DIR = bin
BUILD_DIR = build
INC_DIR = include
LIB_DIR = lib
SRC_DIR = src
TEST_DIR = test
APP_SOURCES = $(shell find $(SRC_DIR) -name '*.c')
APP_HEADERS = $(shell find $(SRC_DIR) -name '*.h')
APP_OBJS = $(patsubst %.c, $(BUILD_DIR)/%.o, $(APP_SOURCES))
APP_LIBS = baz
APP_EXEC = app.out
TEST_SRC_DIR = $(TEST_DIR)/src
TEST_SOURCES = $(shell find $(TEST_SRC_DIR) -name '*.c')
TEST_OBJS = $(patsubst %.c, $(BUILD_DIR)/%.o, $(TEST_SOURCES))
TEST_UNITY_SRC_DIR = $(TEST_DIR)/unity/src
TEST_UNITY_INC_DIR = $(TEST_DIR)/unity/include
TEST_UNITY_SOURCES = $(shell find $(TEST_UNITY_SRC_DIR) -name '*.c')
TEST_UNITY_OBJS = $(patsubst %.c, $(BUILD_DIR)/%.o, $(TEST_UNITY_SOURCES))
TEST_APP_SOURCES = $(filter-out $(SRC_DIR)/main.c, $(APP_SOURCES))
TEST_APP_OBJS = $(patsubst %.c, $(BUILD_DIR)/%.app.o, $(TEST_APP_SOURCES))
TEST_EXEC = test.out
################################################################
# COMPILER AND ARGUMENTS #
################################################################
ifeq ($(COMPILER), clang)
CC = clang
else
CC = gcc
endif
CFLAGS = -c -Wall -I$(SRC_DIR) -I$(TEST_UNITY_INC_DIR) -I$(INC_DIR)
EXTRA_CFLAGS =
LDFLAGS = -L$(LIB_DIR) -l$(APP_LIBS)
EXTRA_LDFLAGS =
ifeq ($(DEBUG), on)
EXTRA_CFLAGS += -g -O0 -DDEBUG
endif
ifeq ($(COVERAGE), on)
COVERAGE_CFLAGS = -ftest-coverage -fprofile-arcs
COVERAGE_LDFLAGS = -lgcov --coverage
endif
ifeq ($(GOOGLE_SANITIZER), asan)
EXTRA_CFLAGS += -fsanitize=address
EXTRA_LDFLAGS += -fsanitize=address
else ifeq ($(GOOGLE_SANITIZER), msan)
EXTRA_CFLAGS += -fsanitize=memory
EXTRA_LDFLAGS += -fsanitize=memory
else ifeq ($(GOOGLE_SANITIZER), ubsan)
EXTRA_CFLAGS += -fsanitize=undefined
EXTRA_LDFLAGS += -fsanitize=undefined
endif
################################################################
# TAR FILE INFORMATION #
################################################################
FILE_NAME = $(AUTHOR)-$(CLASS)-$(EXERCISE)-$(YEAR).tar.gz
################################################################
# TEXT EDITOR #
################################################################
TEXT_EDITOR = code
################################################################
# DOCUMENTATION #
################################################################
DOC_GEN = doxygen
DOC_FILE = Doxyfile
DOC_DIR = doc
################################################################
# OTHER TOOLS #
################################################################
FORMAT_TOOL = clang-format
FORMAT_FLAGS = -style=file -i -fallback-style=none
STATIC_ANALYSIS_TOOL = clang-tidy
DYNAMIC_ANALYSIS_TOOL = valgrind
DYNAMIC_ANALYSIS_FLAGS = --tool=memcheck --leak-check=full --track-fds=yes --trace-children=yes --error-exitcode=1
################################################################
# MAKE TARGETS #
################################################################
.PHONY: all test clean compress edit doc format tidy memcheck help
all: $(APP_EXEC)
$(APP_EXEC): $(APP_OBJS)
@echo '[LD] Linking C executable $@'
@mkdir -p $(BIN_DIR)
@$(CC) $(APP_OBJS) $(LDFLAGS) $(EXTRA_LDFLAGS) -o $(BIN_DIR)/$@
@echo 'Built target $@'
test: $(TEST_EXEC)
@echo '[TEST] Running unit tests'
@./$(BIN_DIR)/$(TEST_EXEC)
$(TEST_EXEC): $(TEST_APP_OBJS) $(TEST_OBJS) $(TEST_UNITY_OBJS)
@echo '[LD] Linking C executable $@'
@mkdir -p $(BIN_DIR)
@$(CC) $(TEST_APP_OBJS) $(TEST_OBJS) $(TEST_UNITY_OBJS) $(LDFLAGS) $(EXTRA_LDFLAGS) $(COVERAGE_LDFLAGS) -o $(BIN_DIR)/$@
@echo 'Built target $@'
$(BUILD_DIR)/%.o: %.c Makefile
@echo '[CC] Compiling C object $@'
@mkdir -p $(dir $@)
@$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $< -o $@
$(BUILD_DIR)/%.app.o: %.c Makefile
@echo '[CC] Compiling C object $@'
@mkdir -p $(dir $@)
@$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(COVERAGE_CFLAGS) $< -o $@
clean:
@echo '[CLEAN] Cleaning workspace'
@rm -rf $(BUILD_DIR) $(BIN_DIR) $(DOC_DIR)
compress:
@echo '[COMPRESS] Packing workspace'
@tar -zcvf $(FILE_NAME) $(SRC_DIR) $(TEST_SRC_DIR) Makefile $(DOC_FILE)
edit:
@echo '[EDIT] Editing workspace'
@$(TEXT_EDITOR) .
doc:
@echo '[DOC] Generating documentation'
@mkdir -p $(DOC_DIR)
@$(DOC_GEN) $(DOC_FILE)
format:
@echo '[FORMAT] Formatting source code'
@$(FORMAT_TOOL) $(FORMAT_FLAGS) $(APP_SOURCES) $(APP_HEADERS) $(TEST_SOURCES)
tidy:
@echo '[TIDY] Running ClangTidy for static analysis'
@$(STATIC_ANALYSIS_TOOL) $(APP_SOURCES) $(APP_HEADERS) $(TEST_SOURCES) -- -I$(SRC_DIR) -I$(TEST_UNITY_INC_DIR) -I$(INC_DIR)
memcheck: $(TEST_EXEC)
@echo '[MEMCHECK] Running Valgrind for dynamic analysis'
@$(DYNAMIC_ANALYSIS_TOOL) $(DYNAMIC_ANALYSIS_FLAGS) ./$(BIN_DIR)/$(TEST_EXEC)
help:
@echo ''
@echo '********************************************************'
@echo ' Uso:'
@echo ' make all: Compiles and links the application'
@echo ' make test: Runs unit tests'
@echo ' make clean: Removes objects and executable'
@echo ' make compress: Generates .tar.gz file'
@echo ' make edit: Opens code files with text editor'
@echo ' make doc: Generates code documentation'
@echo ' make format: Formats source code'
@echo ' make tidy: Performs static analysis'
@echo ' make memcheck: Performs dynamic analysis'
@echo ' make help: Shows help'
@echo '********************************************************'
@echo ''