-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
244 lines (204 loc) · 7.54 KB
/
Copy pathMakefile
File metadata and controls
244 lines (204 loc) · 7.54 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# This Makefile compiles the 'streamlog_example' binary using the g++ compiler.
# The source files are located in the 'src' directory and the binary will be
# generated in the 'build' directory. The Makefile includes flags for
# specifying include directories, macro definitions, and warnings. It also
# handles linker flags and libraries for Boost. The user can set the desired
# debug level with the 'DEBUG_LEVEL' variable and color theme with the 'THEME'
# variable. The Makefile provides 'all', 'clean', 'distclean', and 'install'
# targets to build, clean, and install the outputs.
####################################################
# DEBUG LEVELS
####################################################
# Lower levels encompass higher levels.
# 1 TRACE
# 2 DEBUG
# 3 INFO
# 4 WARN
# 5 ERROR
# 6 FATAL
####################################################
# build switches
####################################################
aarch64 ?= 0
mac ?= 0
####################################################
# includes
####################################################
include mk/aarch64.mk
include mk/example.mk
include mk/helper.mk
####################################################
# Set compiler
####################################################
CXX ?= g++
####################################################
# Installation paths
####################################################
PREFIX ?= /usr/local
DESTDIR ?=
####################################################
# Output directories
####################################################
BUILD_DIR := build
OBJ_DIR := $(BUILD_DIR)/obj
BIN_DIR := $(BUILD_DIR)/bin
LIB_DIR := $(BUILD_DIR)/lib
####################################################
# Compiler flags
####################################################
# Initialize flags for include directories, macro definitions, and warnings
INCLUDE_FLAGS ?=
MACRO_FLAGS ?=
WARNING_FLAGS ?=
EXTRA_FLAGS ?= --std=c++11
# Assemble the compilation flags
CFLAGS = $(INCLUDE_FLAGS) $(MACRO_FLAGS) $(WARNING_FLAGS) $(EXTRA_FLAGS)
LDFLAGS ?=
LDLIBS ?=
####################################################
# Project paths
####################################################
SRC_PATH := src
INCLUDE_PATH := include
# Add include directories
INCLUDE_FLAGS += -I$(INCLUDE_PATH)
# Add warning flags
WARNING_FLAGS += -Wall -Wextra
# Set the debug level
DEBUG_LEVEL ?= 1
MACRO_FLAGS += -DDEBUG_LEVEL=$(DEBUG_LEVEL)
####################################################
# Theme
####################################################
THEME ?= default
ifeq ($(THEME),rose_pine_moon)
MACRO_FLAGS += -DSTREAMLOG_THEME_ROSE_PINE_MOON
endif
# Enable specific logging
MACRO_FLAGS += -DENABLE_VECTOR_LOGGING
MACRO_FLAGS += -DENABLE_MAP_LOGGING
####################################################
# Source and object files
####################################################
# Define the source files
SRC_FILES := $(SRC_PATH)/streamlog.cpp
# Set the target library name
LIB_TARGET := streamlog
# Define the object files
CORE_OBJ := $(OBJ_DIR)/$(notdir $(SRC_FILES:.cpp=.o))
####################################################
# Library targets
####################################################
LIB_STATIC := $(LIB_DIR)/lib$(LIB_TARGET).a
LIB_DYNAMIC := $(LIB_DIR)/lib$(LIB_TARGET).so
ifeq ($(mac),1)
LIB_MAC := $(LIB_DIR)/lib$(LIB_TARGET).dylib
endif
# Define all library targets
LIB_ALL := $(LIB_STATIC) $(LIB_DYNAMIC)
# Add the dynamic library target if on macOS
ifeq ($(mac),1)
LIB_ALL += $(LIB_MAC)
endif
####################################################
# Test configuration
####################################################
TEST_DIR := tests
TEST_SRC := $(TEST_DIR)/test_streamlog.cpp
TEST_BIN := $(BIN_DIR)/test_streamlog
TEST_BIN_ASAN := $(BIN_DIR)/test_streamlog_asan
TEST_BIN_UBSAN := $(BIN_DIR)/test_streamlog_ubsan
####################################################
# Recipes
####################################################
# Declare phony targets to avoid conflicts with files of the same name
.PHONY: all clean distclean install prepare docs test test-asan test-ubsan test-memcheck test-all
# Default target
all: $(LIB_ALL)
# Prepare build directories
prepare:
@$(MKDIR) $(OBJ_DIR) $(BIN_DIR) $(LIB_DIR)
# Compile the object file
$(OBJ_DIR)/%.o: $(SRC_PATH)/%.cpp | prepare
$(CXX) $(CFLAGS) -c $< -o $@
# Compile the static library
$(LIB_STATIC): $(CORE_OBJ) | prepare
@$(AR) rcs $@ $^
# Compile the shared library (recompile with -fPIC)
$(LIB_DYNAMIC): $(SRC_FILES) | prepare
$(CXX) -fPIC -shared $(CFLAGS) -o $@ $^
# Compile the dynamic library on macOS
ifeq ($(mac),1)
$(LIB_MAC): $(SRC_FILES) | prepare
$(CXX) -dynamiclib -fPIC $(CFLAGS) -o $@ $^
endif
# Clean up build artifacts
clean:
@echo "clean"
@$(RM) -r $(BUILD_DIR)
# Extended cleanup
distclean: clean
@$(RM) *~ *.swp *.bak *.tmp
@$(RM) -r docs test_logs
@$(RM) -f test_custom.log output.log
# Generate documentation
docs:
@command -v doxygen >/dev/null || { echo "Error: doxygen not found"; exit 1; }
@echo "Generating documentation..."
@doxygen Doxyfile
@echo "Documentation generated in docs/html/index.html"
# Build and run tests
test: $(TEST_BIN)
@echo "Running unit tests..."
@LD_LIBRARY_PATH=$(LIB_DIR):$$LD_LIBRARY_PATH $(TEST_BIN)
# Compile test binary
$(TEST_BIN): $(TEST_SRC) $(LIB_DYNAMIC) | prepare
$(CXX) $(CFLAGS) -I$(TEST_DIR) $(TEST_SRC) -L$(LIB_DIR) -l$(LIB_TARGET) -o $(TEST_BIN)
# AddressSanitizer - detects memory errors (use-after-free, buffer overflow, leaks)
test-asan: $(TEST_BIN_ASAN)
@echo "Running tests with AddressSanitizer..."
@LD_LIBRARY_PATH=$(LIB_DIR):$$LD_LIBRARY_PATH ASAN_OPTIONS=detect_leaks=1 $(TEST_BIN_ASAN)
$(TEST_BIN_ASAN): $(TEST_SRC) $(LIB_DYNAMIC) | prepare
$(CXX) $(CFLAGS) -fsanitize=address -fno-omit-frame-pointer -g -I$(TEST_DIR) $(TEST_SRC) -L$(LIB_DIR) -l$(LIB_TARGET) -o $(TEST_BIN_ASAN)
# UndefinedBehaviorSanitizer - detects undefined behavior
test-ubsan: $(TEST_BIN_UBSAN)
@echo "Running tests with UndefinedBehaviorSanitizer..."
@LD_LIBRARY_PATH=$(LIB_DIR):$$LD_LIBRARY_PATH $(TEST_BIN_UBSAN)
$(TEST_BIN_UBSAN): $(TEST_SRC) $(LIB_DYNAMIC) | prepare
$(CXX) $(CFLAGS) -fsanitize=undefined -fno-omit-frame-pointer -g -I$(TEST_DIR) $(TEST_SRC) -L$(LIB_DIR) -l$(LIB_TARGET) -o $(TEST_BIN_UBSAN)
# Memory leak check with Valgrind (if available)
test-memcheck: $(TEST_BIN)
@if command -v valgrind >/dev/null 2>&1; then \
echo "Running tests with Valgrind memcheck..."; \
LD_LIBRARY_PATH=$(LIB_DIR):$$LD_LIBRARY_PATH valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --error-exitcode=1 $(TEST_BIN); \
else \
echo "Valgrind not found, skipping memcheck (install with: sudo pacman -S valgrind)"; \
exit 1; \
fi
# Run all test variations
test-all: test test-asan test-ubsan
@echo ""
@echo "=== All Tests Passed ==="
@echo "✓ Standard tests"
@echo "✓ AddressSanitizer (memory errors)"
@echo "✓ UndefinedBehaviorSanitizer"
# Install rule for the library
install: $(LIB_DYNAMIC) $(LIB_STATIC)
@if [ -z "$(DESTDIR)" ]; then \
target="$(PREFIX)"; \
while [ ! -e "$$target" ] && [ "$$target" != "/" ]; do \
target=$$(dirname "$$target"); \
done; \
if [ ! -w "$$target" ]; then \
echo "Error: No write permission to $(PREFIX)"; \
echo "Try: sudo make install"; \
echo " or: make install PREFIX=~/.local"; \
exit 1; \
fi; \
fi
install -d $(DESTDIR)$(PREFIX)/lib
install -d $(DESTDIR)$(PREFIX)/include
install -m 644 $(LIB_DYNAMIC) $(DESTDIR)$(PREFIX)/lib/
install -m 644 $(LIB_STATIC) $(DESTDIR)$(PREFIX)/lib/
install -m 644 $(INCLUDE_PATH)/streamlog.hpp $(DESTDIR)$(PREFIX)/include/
@command -v ldconfig >/dev/null && ldconfig || true