CPP04 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
Makefile
[詳解]
1# **************************************************************************** #
2# #
3# ::: :::::::: #
4# Makefile :+: :+: :+: #
5# +:+ +:+ +:+ #
6# By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ #
7# +#+#+#+#+#+ +#+ #
8# Created: 2025/05/29 22:58:19 by kamitsui #+# #+# #
9# Updated: 2025/06/03 01:42:13 by kamitsui ### ########.fr #
10# #
11# **************************************************************************** #
12
13#******************************************************************************#
14# @file ex03/Makefile #
15# @brief Build system for C++ Module 04 Exercise 03: "Interface & Recap". #
16# #
17# This Makefile orchestrates the compilation, linking, and management #
18# of all source files for Exercise 03. It's designed to automate the #
19# build process, manage object and dependency files, and provide #
20# convenient targets for cleaning, rebuilding, and running tests. #
21# #
22# @note This Makefile incorporates advanced features like automatic #
23# dependency generation (`-MMD -MP`) and integration with an external #
24# C++ module tester repository. #
25# #
26# @section features Features #
27# - **Automated Compilation:** Compiles all `.cpp` files into object files. #
28# - **Automatic Dependency Generation:** Generates `.d` files to ensure correct recompilation. #
29# - **Standard Targets:** Provides `all`, `clean`, `fclean`, `re`. #
30# - **Test Integration:** Includes a `test` target to interact with an external module tester. #
31# - **Output Management:** Creates `objs` and `.deps` directories. #
32# - **User Prompts:** Uses `ASK_AND_EXECUTE_ON_YES` for interactive commands. #
33# @section usage Usage #
34# - `make`: Builds the main executable (`a.out`). #
35# - `make clean`: Removes object and dependency files. #
36# - `make fclean`: Removes object, dependency, and the executable. #
37# - `make re`: Cleans and then rebuilds the project. #
38# - `make test`: Clones (if not present) and runs the associated test from the `cpp_module_tester` repository. #
39#******************************************************************************#
40
41# Directories
42OBJ_DIR = objs
43DEP_DIR = .deps
44
45# Source files
46SRCS = \
47 main.cpp \
48 AMateria.cpp \
49 Character.cpp \
50 Cure.cpp Ice.cpp MateriaSource.cpp \
51 DroppedMateriaManager.cpp Logger.cpp
52
53#vpath %.cpp ./
54
55# Object files and dependency files
56OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS:.cpp=.o))
57DEPS = $(addprefix $(DEP_DIR)/, $(SRCS:.cpp=.d))
58
59# Build target
60NAME = a.out
61
62vpath %.cpp ./
63
64# Compiler
65CXX = c++
66CXXFLAGS = -Wall -Wextra -Werror -std=c++98
67CF_DEP = -MMD -MP -MF $(@:$(OBJ_DIR)/%.o=$(DEP_DIR)/%.d)
68
69# Rules for building object files
70$(OBJ_DIR)/%.o: %.cpp
71 $(CXX) $(CXXFLAGS) $(CF_DEP) -c $< -o $@
72
73# Default target
74all: directories $(NAME)
75.PHONY: all
76
77# Make Directories
78directories:
79 @mkdir -p $(OBJ_DIR)
80 @mkdir -p $(DEP_DIR)
81.PHONY: directories
82
83# Build Target
84$(NAME): $(OBJS)
85 $(CXX) $(CXXFLAGS) $(OBJS) -o $(NAME)
86 $(call ASCII_ART,$(NAME))
87
88# REPOSITORY for Test
89TEST_ROOT = ../../cpp_module_tester
90TEST_DIR = $(TEST_ROOT)/cpp04
91URL_TEST_CPP = "https://github.com/kamitsui/cpp_module_tester.git"
92TEST_TARGET = $(TEST_DIR)/ex03_app
93EX_NUM = ex03
94
95# Test
96test:
97 @echo "No support!"
98#ifeq ($(shell test -d $(TEST_DIR) && echo exist),)
99# @echo "Please clone the test repository"
100# $(call ASK_AND_EXECUTE_ON_YES, git clone $(URL_TEST_CPP) $(TEST_ROOT))
101#endif
102# $(call ASK_AND_EXECUTE_ON_YES, make -C $(TEST_DIR) $(EX_NUM))
103.PHONY: test
104
105# More Test : Enable Logger
106debug: fclean
107 make WITH_DEBUG=1
108.PHONY: debug
109
110# DEBUG_MODE
111ifdef WITH_DEBUG
112CXXFLAGS += -D DEBUG_MODE
113endif
114
115# Rule for removing object & dependency files
116clean:
117 rm -rf $(OBJ_DIR) $(DEP_DIR) $(TEST_DIR)/objs $(TEST_DIR)/.deps
118.PHONY: clean
119
120# Rule for removing Target & others
121fclean: clean
122 rm -f $(NAME) $(TEST_TARGET)
123.PHONY: fclean
124
125# Rule for Clean & Build Target
126re: fclean all
127.PHONY: re
128
129# Enable dependency file
130-include $(DEPS)
131
132# ASCII Art : Display Tips the way to use.
133define ASCII_ART
134 @echo " _____________________________________________"
135 @echo "< Usage : >"
136 @echo "< $$ ./a.out >"
137 @echo "< >"
138 @echo "< Debug : >"
139 @echo "< $$ make debug >"
140 @echo "< $$ ./a.out >"
141 @echo "< >"
142 @echo "< Test Case : >"
143 @echo "< $$ make test >"
144 @echo "----------------------------------------------"
145 @echo " \ ^__^"
146 @echo " \ (oo)\_______"
147 @echo " (__)\ )\\/\\"
148 @echo " ||----w |"
149 @echo " || ||\n"
150endef
151
152# Ask and Exit on No
153define ASK_AND_EXECUTE_ON_YES
154 @echo "Execute? $(1) (y/n)"
155 @read -r answer; \
156 case "$$answer" in \
157 [yY]) \
158 echo $(1); \
159 $(1); \
160 ;; \
161 *) \
162 echo "Skip ..."; \
163 ;; \
164 esac
165endef
166
167# Makefile Option : Disable '--print-directory'
168MAKEFLAGS += --no-print-directory