-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
250 lines (219 loc) Β· 13.3 KB
/
Makefile
File metadata and controls
250 lines (219 loc) Β· 13.3 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
245
246
247
248
249
250
# =============================================================================
# Beautiful C++ Makefile with Colors and Dependency Tracking
# =============================================================================
# ================================ CONFIGURATION =============================
# Easily modifiable output file names and paths
TARGET_NAME := mc-server
BUILD_DIR := build
SOURCE_DIR := src
INCLUDE_DIR := include
DEPS_DIR := .deps
# Final executable path (easily modifiable)
TARGET := $(BUILD_DIR)/$(TARGET_NAME)
# Compiler and flags
CXX := g++
CXXFLAGS := -std=c++20 -Wall -Wextra -Wpedantic -O2
DEBUG_FLAGS := -g -DDEBUG -O0
RELEASE_FLAGS := -DNDEBUG -O3
INCLUDE_FLAGS := -I$(INCLUDE_DIR) -I$(INCLUDE_DIR)/data -I$(INCLUDE_DIR)/network -I$(INCLUDE_DIR)/world -I$(INCLUDE_DIR)/lib
# Linker flags (add your libraries here)
LDFLAGS :=
LIBS := -lz
# ================================ COLOR SETUP ===============================
# ANSI color codes for beautiful output
RESET := \033[0m
BOLD := \033[1m
DIM := \033[2m
# Text colors
BLACK := \033[30m
RED := \033[31m
GREEN := \033[32m
YELLOW := \033[33m
BLUE := \033[34m
MAGENTA := \033[35m
CYAN := \033[36m
WHITE := \033[37m
# Background colors
BG_BLACK := \033[40m
BG_RED := \033[41m
BG_GREEN := \033[42m
BG_YELLOW := \033[43m
BG_BLUE := \033[44m
BG_MAGENTA := \033[45m
BG_CYAN := \033[46m
BG_WHITE := \033[47m
# Bright colors
BRIGHT_RED := \033[91m
BRIGHT_GREEN := \033[92m
BRIGHT_YELLOW := \033[93m
BRIGHT_BLUE := \033[94m
BRIGHT_MAGENTA := \033[95m
BRIGHT_CYAN := \033[96m
BRIGHT_WHITE := \033[97m
# ============================= FILE DISCOVERY ==============================
# Automatically find all source files recursively
SOURCES := $(shell find $(SOURCE_DIR) -name "*.cpp" -type f)
HEADERS := $(shell find $(INCLUDE_DIR) -name "*.hpp" -type f)
OBJECTS := $(patsubst $(SOURCE_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(SOURCES))
DEPS := $(patsubst $(SOURCE_DIR)/%.cpp,$(DEPS_DIR)/%.d,$(SOURCES))
# ============================== BUILD MODES ===============================
# Default build mode
BUILD_MODE := release
# Set flags based on build mode
ifeq ($(BUILD_MODE),debug)
CXXFLAGS += $(DEBUG_FLAGS)
MODE_COLOR := $(BRIGHT_YELLOW)
MODE_NAME := DEBUG
else
CXXFLAGS += $(RELEASE_FLAGS)
MODE_COLOR := $(BRIGHT_GREEN)
MODE_NAME := RELEASE
endif
# ================================= TARGETS ==================================
.PHONY: all clean distclean clean-test debug release info help run install uninstall compile_commands
# Default target
all: info $(TARGET)
# Build in debug mode
debug:
@$(MAKE) BUILD_MODE=debug all
# Build in release mode
release:
@$(MAKE) BUILD_MODE=release all
# Create the main executable
$(TARGET): $(OBJECTS) | $(BUILD_DIR)
@printf "$(BOLD)$(BRIGHT_CYAN)π Linking executable: $(BRIGHT_WHITE)$@$(RESET)\n"
@$(CXX) $(OBJECTS) -o $@ $(LDFLAGS) $(LIBS)
@printf "$(BOLD)$(BRIGHT_GREEN)β
Build completed successfully!$(RESET)\n"
@printf "$(BOLD)$(BRIGHT_BLUE)π Executable: $(BRIGHT_WHITE)$@$(RESET)\n"
# Compile source files to object files
$(BUILD_DIR)/%.o: $(SOURCE_DIR)/%.cpp $(DEPS_DIR)/%.d | $(BUILD_DIR) $(DEPS_DIR)
@printf "$(BOLD)$(BRIGHT_BLUE)π¨ Compiling: $(BRIGHT_WHITE)$<$(RESET)\n"
@mkdir -p $(dir $@)
@$(CXX) $(CXXFLAGS) $(INCLUDE_FLAGS) -c $< -o $@
# Generate dependency files
$(DEPS_DIR)/%.d: $(SOURCE_DIR)/%.cpp | $(DEPS_DIR)
@printf "$(DIM)$(CYAN)π Generating dependencies: $<$(RESET)\n"
@mkdir -p $(dir $@)
@$(CXX) $(CXXFLAGS) $(INCLUDE_FLAGS) -MM -MT $(BUILD_DIR)/$*.o $< > $@
# Create build directories
$(BUILD_DIR):
@printf "$(BOLD)$(YELLOW)π Creating build directory: $(BRIGHT_WHITE)$@$(RESET)\n"
@mkdir -p $@
$(DEPS_DIR):
@printf "$(DIM)$(YELLOW)π Creating deps directory: $(BRIGHT_WHITE)$@$(RESET)\n"
@mkdir -p $@
# Include dependency files (only if they exist)
-include $(DEPS)
# Clean build artifacts
clean:
@printf "$(BOLD)$(BRIGHT_RED)π§Ή Cleaning build artifacts...$(RESET)\n"
@find $(BUILD_DIR) -type f -name "*.o" -delete 2>/dev/null || true
@find $(BUILD_DIR) -name "$(TARGET_NAME)" -delete 2>/dev/null || true
@find $(BUILD_DIR) -type d -empty -delete 2>/dev/null || true
@find $(DEPS_DIR) -type f -name "*.d" -delete 2>/dev/null || true
@find $(DEPS_DIR) -type d -empty -delete 2>/dev/null || true
@printf "$(BOLD)$(BRIGHT_GREEN)β¨ Clean completed! (Preserved directories and config.json)$(RESET)\n"
# Complete clean - removes everything including directories
distclean:
@printf "$(BOLD)$(BRIGHT_RED)π§Ή Complete cleanup (removing all build artifacts and directories)...$(RESET)\n"
@rm -rf $(BUILD_DIR) $(DEPS_DIR)
@printf "$(BOLD)$(BRIGHT_GREEN)β¨ Complete cleanup finished!$(RESET)\n"
# Clean test-server directory
clean-test:
@printf "$(BOLD)$(BRIGHT_RED)π§Ή Cleaning test-server directory...$(RESET)\n"
@rm -rf test-server
@printf "$(BOLD)$(BRIGHT_GREEN)β¨ Test-server cleanup finished!$(RESET)\n"
# Run the executable
run: $(TARGET)
@printf "$(BOLD)$(BRIGHT_MAGENTA)π Setting up test-server environment...$(RESET)\n"
@mkdir -p test-server
@printf "$(BOLD)$(BRIGHT_BLUE)π¦ Copying executable to test-server...$(RESET)\n"
@cp $(TARGET) test-server/$(TARGET_NAME)
@printf "$(BOLD)$(BRIGHT_BLUE)π¦ Copying config.json to test-server...$(RESET)\n"
@cp -f config.json test-server/ 2>/dev/null || printf "$(YELLOW)β οΈ config.json not found, skipping...$(RESET)\n"
@printf "$(BOLD)$(BRIGHT_BLUE)π¦ Copying world folder to test-server...$(RESET)\n"
@if [ -d "world" ]; then \
cp -r world test-server/; \
printf "$(BOLD)$(BRIGHT_GREEN)β
World folder copied successfully!$(RESET)\n"; \
else \
printf "$(YELLOW)β οΈ World folder not found, skipping...$(RESET)\n"; \
fi
@printf "$(BOLD)$(BRIGHT_GREEN)β
Test environment ready!$(RESET)\n"
@printf "$(BOLD)$(BRIGHT_MAGENTA)π Running $(TARGET_NAME)...$(RESET)\n"
@printf "$(DIM)$(WHITE)" && echo "================================================" && printf "$(RESET)"
@cd test-server && ./$(TARGET_NAME)
@printf "$(DIM)$(WHITE)" && echo "================================================" && printf "$(RESET)"
# Display project information
info:
@printf "$(BOLD)$(BG_BLUE)$(WHITE) ποΈ C++ BUILD SYSTEM $(RESET)\n"
@printf "$(BOLD)$(BRIGHT_CYAN)βββββββββββββββββββββββββββββββββββββββββββββββββ$(RESET)\n"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BOLD)Project:$(RESET) $(BRIGHT_WHITE)%-20s$(RESET) $(BOLD)$(BRIGHT_CYAN)β$(RESET)\n" "$(TARGET_NAME)"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BOLD)Mode:$(RESET) $(MODE_COLOR)%-20s$(RESET) $(BOLD)$(BRIGHT_CYAN)β$(RESET)\n" "$(MODE_NAME)"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BOLD)Compiler:$(RESET) $(BRIGHT_WHITE)%-20s$(RESET) $(BOLD)$(BRIGHT_CYAN)β$(RESET)\n" "$(CXX)"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BOLD)Target:$(RESET) $(BRIGHT_WHITE)%-20s$(RESET) $(BOLD)$(BRIGHT_CYAN)β$(RESET)\n" "$(TARGET)"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BOLD)Sources:$(RESET) $(BRIGHT_GREEN)%-20s$(RESET) $(BOLD)$(BRIGHT_CYAN)β$(RESET)\n" "$(words $(SOURCES)) files"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BOLD)Headers:$(RESET) $(BRIGHT_GREEN)%-20s$(RESET) $(BOLD)$(BRIGHT_CYAN)β$(RESET)\n" "$(words $(HEADERS)) files"
@printf "$(BOLD)$(BRIGHT_CYAN)βββββββββββββββββββββββββββββββββββββββββββββββββ$(RESET)\n"
# Install the executable (modify INSTALL_PREFIX as needed)
INSTALL_PREFIX := /usr/local
install: $(TARGET)
@printf "$(BOLD)$(BRIGHT_BLUE)π¦ Installing $(TARGET_NAME) to $(INSTALL_PREFIX)/bin...$(RESET)\n"
@sudo cp $(TARGET) $(INSTALL_PREFIX)/bin/$(TARGET_NAME)
@sudo chmod +x $(INSTALL_PREFIX)/bin/$(TARGET_NAME)
@printf "$(BOLD)$(BRIGHT_GREEN)β
Installation completed!$(RESET)\n"
# Uninstall the executable
uninstall:
@printf "$(BOLD)$(BRIGHT_RED)ποΈ Uninstalling $(TARGET_NAME)...$(RESET)\n"
@sudo rm -f $(INSTALL_PREFIX)/bin/$(TARGET_NAME)
@printf "$(BOLD)$(BRIGHT_GREEN)β
Uninstallation completed!$(RESET)\n"
# Generate compile_commands.json for LSP support
compile_commands:
@printf "$(BOLD)$(BRIGHT_BLUE)π Generating compile_commands.json for LSP...$(RESET)\n"
@echo '[' > compile_commands.json
@first=true; for src in $(SOURCES); do \
[ "$$first" = true ] && first=false || echo ',' >> compile_commands.json; \
echo ' {' >> compile_commands.json; \
echo ' "directory": "'$(shell pwd)'",' >> compile_commands.json; \
obj_path=$$(echo "$$src" | sed 's|$(SOURCE_DIR)/|$(BUILD_DIR)/|' | sed 's|\.cpp$$|.o|'); \
echo " \"command\": \"$(CXX) $(CXXFLAGS) $(INCLUDE_FLAGS) -c $$src -o $$obj_path\"," >> compile_commands.json; \
echo ' "file": "'$$src'"' >> compile_commands.json; \
echo ' }' >> compile_commands.json; \
done
@echo ']' >> compile_commands.json
@printf "$(BOLD)$(BRIGHT_GREEN)β
compile_commands.json generated successfully!$(RESET)\n"
# Display help information
help:
@printf "$(BOLD)$(BG_GREEN)$(WHITE) π MAKEFILE HELP $(RESET)\n"
@printf "$(BOLD)$(BRIGHT_CYAN)ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ$(RESET)\n"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BOLD)Available targets:$(RESET) $(BOLD)$(BRIGHT_CYAN)β$(RESET)\n"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BOLD)$(BRIGHT_CYAN)β$(RESET)\n"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BRIGHT_GREEN)%-10s$(RESET) %-39s $(BOLD)$(BRIGHT_CYAN) β$(RESET)\n" "all" "Build the project (default: release mode)"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BRIGHT_GREEN)%-10s$(RESET) %-39s $(BOLD)$(BRIGHT_CYAN) β$(RESET)\n" "debug" "Build in debug mode"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BRIGHT_GREEN)%-10s$(RESET) %-39s $(BOLD)$(BRIGHT_CYAN) β$(RESET)\n" "release" "Build in release mode"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BRIGHT_GREEN)%-10s$(RESET) %-39s $(BOLD)$(BRIGHT_CYAN) β$(RESET)\n" "clean" "Remove build artifacts (preserve dirs)"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BRIGHT_GREEN)%-10s$(RESET) %-39s $(BOLD)$(BRIGHT_CYAN) β$(RESET)\n" "distclean" "Remove all build artifacts and dirs"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BRIGHT_GREEN)%-10s$(RESET) %-39s $(BOLD)$(BRIGHT_CYAN) β$(RESET)\n" "clean-test" "Remove test-server directory"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BRIGHT_GREEN)%-10s$(RESET) %-39s $(BOLD)$(BRIGHT_CYAN) β$(RESET)\n" "run" "Setup test-server and run executable"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BRIGHT_GREEN)%-10s$(RESET) %-39s $(BOLD)$(BRIGHT_CYAN) β$(RESET)\n" "compile_commands" "Generate compile_commands.json for LSP"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BRIGHT_GREEN)%-10s$(RESET) %-39s $(BOLD)$(BRIGHT_CYAN) β$(RESET)\n" "install" "Install the executable to system"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BRIGHT_GREEN)%-10s$(RESET) %-39s $(BOLD)$(BRIGHT_CYAN) β$(RESET)\n" "uninstall" "Remove the executable from system"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BRIGHT_GREEN)%-10s$(RESET) %-39s $(BOLD)$(BRIGHT_CYAN) β$(RESET)\n" "info" "Display project information"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BRIGHT_GREEN)%-10s$(RESET) %-39s $(BOLD)$(BRIGHT_CYAN) β$(RESET)\n" "help" "Show this help message"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BOLD)$(BRIGHT_CYAN)β$(RESET)\n"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) $(BOLD)Customization:$(RESET) $(BOLD)$(BRIGHT_CYAN)β$(RESET)\n"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) β’ Modify $(BRIGHT_YELLOW)TARGET_NAME$(RESET) to change executable name $(BOLD)$(BRIGHT_CYAN)β$(RESET)\n"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) β’ Modify $(BRIGHT_YELLOW)BUILD_DIR$(RESET) to change build directory $(BOLD)$(BRIGHT_CYAN)β$(RESET)\n"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) β’ Add libraries to $(BRIGHT_YELLOW)LIBS$(RESET) variable $(BOLD)$(BRIGHT_CYAN)β$(RESET)\n"
@printf "$(BOLD)$(BRIGHT_CYAN)β$(RESET) β’ Add compiler flags to $(BRIGHT_YELLOW)CXXFLAGS$(RESET) $(BOLD)$(BRIGHT_CYAN)β$(RESET)\n"
@printf "$(BOLD)$(BRIGHT_CYAN)ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ$(RESET)\n"
# =============================================================================
# π¨ Beautiful Makefile - Features:
# β’ Colorful output with emojis
# β’ Automatic dependency tracking (.deps directory)
# β’ Debug and release build modes
# β’ Easy customization of output names
# β’ Clean directory structure
# β’ Install/uninstall targets
# β’ Comprehensive help system
# β’ No hardcoded file names (all auto-discovered)
# =============================================================================