-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
128 lines (106 loc) · 3.4 KB
/
Makefile
File metadata and controls
128 lines (106 loc) · 3.4 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
NAME := ircserv
# SOURCE FILES
SRCS_DIR := src
SRCS_FILES := main.cpp \
Server.cpp \
ServerBot.cpp \
ServerUser.cpp \
ServerSocket.cpp \
ServerChannel.cpp \
User.cpp \
UserMessaging.cpp \
UserRegistration.cpp \
Command.cpp \
CommandRegistration.cpp \
CommandChannel.cpp \
CommandModes.cpp \
CommandMessaging.cpp \
CommandConnection.cpp \
CommandUtils.cpp \
Channel.cpp \
signal.cpp \
utils.cpp
SRCS := $(addprefix $(SRCS_DIR)/, $(SRCS_FILES))
# OBJECT FILES
OBJS_DIR := obj
OBJS := $(SRCS:$(SRCS_DIR)/%.cpp=$(OBJS_DIR)/%.o)
DEPS := $(OBJS:.o=.d)
# Detect the operating system
OS := $(shell uname -s)
# COMPILER
CXX := c++
CXXFLAGS := -std=c++98
CXXFLAGS += -Werror -Wextra -Wall
CXXFLAGS += -Wshadow # Warns about shadowed variables.
CXXFLAGS += -Wpedantic # Enforces strict ISO C++ compliance.
# CXXFLAGS += -g -O0
# CXXFLAGS += -g -O1 -fsanitize=address
# CPPFLAGS are for preprocessor-specific flags
ifeq ($(OS),Darwin) # Darwin is the kernel name for macOS
# Define a preprocessor macro for macOS
CPPFLAGS += -DMACOS_OS
else ifeq ($(OS),Linux)
# Define a preprocessor macro for Linux
CPPFLAGS += -DLINUX_OS
endif
# Used for progress bar
TOTAL_SRCS := $(words $(SRCS))
SRC_NUM := 0
RESET = \033[0m
BOLD = \033[1m
GREEN = \033[32m
YELLOW = \033[33m
RED := \033[91m
###########
## RULES ##
###########
all: $(NAME)
$(NAME): $(OBJS)
@$(CXX) $(CXXFLAGS) $(OBJS) -o $(NAME)
@echo "$(BOLD)$(YELLOW)\n$(NAME) successfully compiled.$(RESET)"
## MAKE BOT ##
# Adds a bot mode flag, which will activate the bot.
# Only print the "Bot mode activated!" message if bot mode was not already active.
bot:
@status=0; \
status=$$($(MAKE) -q --no-print-directory CPPFLAGS="$(CPPFLAGS) -DBOT_MODE" all; echo $$?); \
$(MAKE) --no-print-directory CPPFLAGS="$(CPPFLAGS) -DBOT_MODE" all; \
if [ $$status -ne 0 ]; then \
echo "$(BOLD)$(YELLOW)Bot mode activated!$(RESET)"; \
fi
## COMPILATION PROGRESS BAR ##
# Compiles individual .cpp files into .o object files without linking.
# Last line:
# -c: Generates o. files without linking.
# -$<: Represents the first prerequisite (the c. file).
# -o $@: Output file name; '$@' is replaced with target name (the o. file).
# -MMD: Generates a dependency file for each source file.
# -MP: Prevents make from failing if the header file is deleted.
$(OBJS_DIR)/%.o: $(SRCS_DIR)/%.cpp
@mkdir -p $(@D)
@$(eval SRC_NUM := $(shell expr $(SRC_NUM) + 1))
@$(eval PERCENT := $(shell printf "%.0f" $(shell echo "scale=4; $(SRC_NUM) / $(TOTAL_SRCS) * 100" | bc)))
@printf "$(BOLD)\rCompiling $(NAME): ["
@$(eval PROGRESS := $(shell expr $(PERCENT) / 5))
@printf "$(GREEN)%0.s#$(RESET)$(BOLD)" $(shell seq 1 $(PROGRESS))
@if [ $(PERCENT) -lt 100 ]; then printf "%0.s-" $(shell seq 1 $(shell expr 20 - $(PROGRESS))); fi
@printf "] "
@if [ $(PERCENT) -eq 100 ]; then printf "$(GREEN)"; fi
@printf "%d/%d - " $(SRC_NUM) $(TOTAL_SRCS)
@printf "%d%% $(RESET)" $(PERCENT)
@$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -MMD -MP $< -o $@
clean:
@rm -rf $(OBJS_DIR)
@echo "$(BOLD)$(RED)$(NAME) object files removed.$(RESET)"
clean_log:
@rm -f *.log
@echo "$(BOLD)$(RED)Log files removed.$(RESET)"
fclean: clean clean_log
@rm -f $(NAME)
@echo "$(BOLD)$(RED)$(NAME) removed.$(RESET)"
re: fclean all
re_bot: fclean bot
check_os:
@echo "Detected OS: $(OS)"
.PHONY: all bot clean clean_log fclean re re_bot check_os
-include $(DEPS)